From 240aac568303deff14cbb2366b94c8c89cacefc1 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Thu, 27 Aug 2020 14:23:23 -0400 Subject: [PATCH] Updating shims for constrained environments (#944, #993). --- packages/shims/dist/index.js | 154 +++++++++++++++---------------- packages/shims/dist/index.min.js | 2 +- 2 files changed, 76 insertions(+), 80 deletions(-) diff --git a/packages/shims/dist/index.js b/packages/shims/dist/index.js index 47063ed747..5048c035d2 100644 --- a/packages/shims/dist/index.js +++ b/packages/shims/dist/index.js @@ -103,92 +103,87 @@ (function (global){ 'use strict'; -var shims = []; +(function() { -// Shim String.prototype.normalize -try { - var missing = []; + var shims = []; - // Some platforms are missing certain normalization forms - var forms = ["NFD", "NFC", "NFKD", "NFKC"]; - for (var i = 0; i < forms.length; i++) { - try { - if ("test".normalize(forms[i]) !== "test") { - throw new Error("failed to normalize"); + // Shim String.prototype.normalize + try { + var missing = []; + + // Some platforms are missing certain normalization forms + var forms = ["NFD", "NFC", "NFKD", "NFKC"]; + for (var i = 0; i < forms.length; i++) { + try { + if ("test".normalize(forms[i]) !== "test") { + throw new Error("failed to normalize"); + } + } catch(error) { + missing.push(forms[i]); } - } catch(error) { - missing.push(forms[i]); } - } - if (missing.length) { - shims.push("String.prototype.normalize (missing: " + missing.join(", ") + ")"); - throw new Error('bad normalize'); + if (missing.length) { + shims.push("String.prototype.normalize (missing: " + missing.join(", ") + ")"); + throw new Error('bad normalize'); + } + + // Some platforms have a native normalize, but it is broken; so we force our shim + if (String.fromCharCode(0xe9).normalize('NFD') !== String.fromCharCode(0x65, 0x0301)) { + shims.push("String.prototype.normalize (broken)"); + throw new Error('bad normalize'); + } + } catch (error) { + var unorm = require('./unorm.js'); + String.prototype.normalize = function(form) { + var func = unorm[(form || 'NFC').toLowerCase()]; + if (!func) { throw new RangeError('invalid form - ' + form); } + return func(this); + } } - // Some platforms have a native normalize, but it is broken; so we force our shim - if (String.fromCharCode(0xe9).normalize('NFD') !== String.fromCharCode(0x65, 0x0301)) { - shims.push("String.prototype.normalize (broken)"); - throw new Error('bad normalize'); + // Shim atob and btoa + var base64 = require('./base64.js'); + if (!global.atob) { + shims.push("atob"); + global.atob = base64.atob; + } + if (!global.btoa) { + shims.push("btoa"); + global.btoa = base64.btoa; } -} catch (error) { - var unorm = require('./unorm.js'); - String.prototype.normalize = function(form) { - var func = unorm[(form || 'NFC').toLowerCase()]; - if (!func) { throw new RangeError('invalid form - ' + form); } - return func(this); + + // Shim ArrayBuffer.isView + if (!ArrayBuffer.isView) { + shims.push("ArrayBuffer.isView"); + ArrayBuffer.isView = function(obj) { + // @TODO: This should probably check various instanceof aswell + return !!(obj.buffer); + } } -} - -// Shim atob and btoa -var base64 = require('./base64.js'); -if (!global.atob) { - shims.push("atob"); - global.atob = base64.atob; -} -if (!global.btoa) { - shims.push("btoa"); - global.btoa = base64.btoa; -} - -// Shim Promise -// @TODO: Check first? -//if (window.Promise == null) { -// var promise = require('./es6-promise.auto.js'); -//} - -// Shim ArrayBuffer.isView -if (!ArrayBuffer.isView) { - shims.push("ArrayBuffer.isView"); - ArrayBuffer.isView = function(obj) { - // @TODO: This should probably check various instanceof aswell - return !!(obj.buffer); + + // Shim nextTick + if (!global.nextTick) { + shims.push("nextTick"); + global.nextTick = function (callback) { setTimeout(callback, 0); } } -} - -// Shim nextTick -if (!global.nextTick) { - shims.push("nextTick"); - global.nextTick = function (callback) { setTimeout(callback, 0); } -} - -// Shim crypto.getRandomValues -// @TOOD: Investigate: https://github.com/brix/crypto-js/issues/7 -if (!global.crypto) { global.crypto = { }; } -if (!global.crypto.getRandomValues) { - shims.push("crypto.getRandomValues"); - console.log("WARNING: This environment is missing a secure random source; generated private keys may be at risk, think VERY carefully about not adding a better secure source."); - global.crypto.getRandomValues = function(buffer) { - var start = Math.floor((new Date()).getTime()) % buffer.length; - for (var i = 0; i < buffer.length; i++) { - buffer[(start + i) % buffer.length] = Math.floor(Math.random() * 256); + + // Shim crypto.getRandomValues + // @TOOD: Investigate: https://github.com/brix/crypto-js/issues/7 + if (!global.crypto) { global.crypto = { }; } + if (!global.crypto.getRandomValues) { + shims.push("crypto.getRandomValues"); + console.log("WARNING: This environment is missing a secure random source; generated private keys may be at risk, think VERY carefully about not adding a better secure source."); + global.crypto.getRandomValues = function(buffer) { + var start = Math.floor((new Date()).getTime()) % buffer.length; + for (var i = 0; i < buffer.length; i++) { + buffer[(start + i) % buffer.length] = Math.floor(Math.random() * 256); + } } } -} -// Shim FileReader.readAsArrayBuffer -// https://github.com/facebook/react-native/issues/21209 -(function() { + // Shim FileReader.readAsArrayBuffer + // https://github.com/facebook/react-native/issues/21209 try { var fr = new FileReader(); try { @@ -217,14 +212,15 @@ if (!global.crypto.getRandomValues) { } catch (error) { console.log("Missing FileReader; unsupported platform"); } -})(); -if (shims.length) { - console.log("Shims Injected:"); - for (var i = 0; i < shims.length; i++) { - console.log(' - ' + shims[i]); + if (shims.length) { + console.log("Shims Injected:"); + for (var i = 0; i < shims.length; i++) { + console.log(' - ' + shims[i]); + } } -} + +})(); diff --git a/packages/shims/dist/index.min.js b/packages/shims/dist/index.min.js index 2b305e1974..9a4083bd55 100644 --- a/packages/shims/dist/index.min.js +++ b/packages/shims/dist/index.min.js @@ -1 +1 @@ -!function(){return function t(r,e,n){function o(a,s){if(!e[a]){if(!r[a]){var u="function"==typeof require&&require;if(!s&&u)return u(a,!0);if(i)return i(a,!0);var f=new Error("Cannot find module '"+a+"'");throw f.code="MODULE_NOT_FOUND",f}var h=e[a]={exports:{}};r[a][0].call(h.exports,function(t){return o(r[a][1][t]||t)},h,h.exports,t,r,e,n)}return e[a].exports}for(var i="function"==typeof require&&require,a=0;a255||(o=t.charCodeAt(s++))>255||(i=t.charCodeAt(s++))>255)throw new TypeError("Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.");a+=n.charAt((r=e<<16|o<<8|i)>>18&63)+n.charAt(r>>12&63)+n.charAt(r>>6&63)+n.charAt(63&r)}return u?a.slice(0,u-3)+"===".substring(u):a},t.atob=function(t){if(t=String(t).replace(/[\t\n\f\r ]+/g,""),!o.test(t))throw new TypeError("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.");t+="==".slice(2-(3&t.length));for(var r,e,i,a="",s=0;s>16&255):64===i?String.fromCharCode(r>>16&255,r>>8&255):String.fromCharCode(r>>16&255,r>>8&255,255&r);return a}}},"function"==typeof define&&define.amd?define([],function(){o(n)}):o(n)},{}],2:[function(t,r,e){(function(r){"use strict";var e=[];try{for(var n=[],o=["NFD","NFC","NFKD","NFKC"],i=0;i{var t=atob(r.result.split(",").pop().trim()),e=new ArrayBuffer(t.length);new Uint8Array(e).set(Array.from(t).map(t=>t.charCodeAt(0))),this._result=e,this._setReadyState(this.DONE)}),r.readAsDataURL(t)}}catch(t){console.log("Missing FileReader; unsupported platform")}}(),e.length){console.log("Shims Injected:");for(i=0;i>8&255]>n&&(d[r]=o),o},function(t,r,e){return e?t(r,e):new p(r,null)},function(t,r,e){var n;if(r=55296&&t<=56319},p.isLowSurrogate=function(t){return t>=56320&&t<=57343},p.prototype.prepFeature=function(){this.feature||(this.feature=p.fromCharCode(this.codepoint,!0).feature)},p.prototype.toString=function(){if(this.codepoint<65536)return String.fromCharCode(this.codepoint);var t=this.codepoint-65536;return String.fromCharCode(Math.floor(t/1024)+55296,t%1024+56320)},p.prototype.getDecomp=function(){return this.prepFeature(),this.feature[0]||null},p.prototype.isCompatibility=function(){return this.prepFeature(),!!this.feature[1]&&256&this.feature[1]},p.prototype.isExclude=function(){return this.prepFeature(),!!this.feature[1]&&512&this.feature[1]},p.prototype.getCanonicalClass=function(){return this.prepFeature(),this.feature[1]?255&this.feature[1]:0},p.prototype.getComposite=function(t){if(this.prepFeature(),!this.feature[2])return null;var r=this.feature[2][t.codepoint];return r?p.fromCharCode(r):null};var y=function(t){this.str=t,this.cursor=0};y.prototype.next=function(){if(this.str&&this.cursor0;--e){if(this.resBuf[e-1].getCanonicalClass()<=t)break}this.resBuf.splice(e,0,r)}while(0!==t);return this.resBuf.shift()};var b=function(t){this.it=t,this.procBuf=[],this.resBuf=[],this.lastClass=null};b.prototype.next=function(){for(;0===this.resBuf.length;){var t=this.it.next();if(!t){this.resBuf=this.procBuf,this.procBuf=[];break}if(0===this.procBuf.length)this.lastClass=t.getCanonicalClass(),this.procBuf.push(t);else{var r=this.procBuf[0].getComposite(t),e=t.getCanonicalClass();r&&(this.lastClass255||(o=t.charCodeAt(s++))>255||(i=t.charCodeAt(s++))>255)throw new TypeError("Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.");a+=n.charAt((r=e<<16|o<<8|i)>>18&63)+n.charAt(r>>12&63)+n.charAt(r>>6&63)+n.charAt(63&r)}return u?a.slice(0,u-3)+"===".substring(u):a},t.atob=function(t){if(t=String(t).replace(/[\t\n\f\r ]+/g,""),!o.test(t))throw new TypeError("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.");t+="==".slice(2-(3&t.length));for(var r,e,i,a="",s=0;s>16&255):64===i?String.fromCharCode(r>>16&255,r>>8&255):String.fromCharCode(r>>16&255,r>>8&255,255&r);return a}}},"function"==typeof define&&define.amd?define([],function(){o(n)}):o(n)},{}],2:[function(t,r,e){(function(r){"use strict";!function(){var e=[];try{for(var n=[],o=["NFD","NFC","NFKD","NFKC"],i=0;i{var t=atob(r.result.split(",").pop().trim()),e=new ArrayBuffer(t.length);new Uint8Array(e).set(Array.from(t).map(t=>t.charCodeAt(0))),this._result=e,this._setReadyState(this.DONE)}),r.readAsDataURL(t)}}catch(t){console.log("Missing FileReader; unsupported platform")}if(e.length){console.log("Shims Injected:");for(i=0;i>8&255]>n&&(d[r]=o),o},function(t,r,e){return e?t(r,e):new p(r,null)},function(t,r,e){var n;if(r=55296&&t<=56319},p.isLowSurrogate=function(t){return t>=56320&&t<=57343},p.prototype.prepFeature=function(){this.feature||(this.feature=p.fromCharCode(this.codepoint,!0).feature)},p.prototype.toString=function(){if(this.codepoint<65536)return String.fromCharCode(this.codepoint);var t=this.codepoint-65536;return String.fromCharCode(Math.floor(t/1024)+55296,t%1024+56320)},p.prototype.getDecomp=function(){return this.prepFeature(),this.feature[0]||null},p.prototype.isCompatibility=function(){return this.prepFeature(),!!this.feature[1]&&256&this.feature[1]},p.prototype.isExclude=function(){return this.prepFeature(),!!this.feature[1]&&512&this.feature[1]},p.prototype.getCanonicalClass=function(){return this.prepFeature(),this.feature[1]?255&this.feature[1]:0},p.prototype.getComposite=function(t){if(this.prepFeature(),!this.feature[2])return null;var r=this.feature[2][t.codepoint];return r?p.fromCharCode(r):null};var y=function(t){this.str=t,this.cursor=0};y.prototype.next=function(){if(this.str&&this.cursor0;--e){if(this.resBuf[e-1].getCanonicalClass()<=t)break}this.resBuf.splice(e,0,r)}while(0!==t);return this.resBuf.shift()};var b=function(t){this.it=t,this.procBuf=[],this.resBuf=[],this.lastClass=null};b.prototype.next=function(){for(;0===this.resBuf.length;){var t=this.it.next();if(!t){this.resBuf=this.procBuf,this.procBuf=[];break}if(0===this.procBuf.length)this.lastClass=t.getCanonicalClass(),this.procBuf.push(t);else{var r=this.procBuf[0].getComposite(t),e=t.getCanonicalClass();r&&(this.lastClass