amazon-cognito-identity-js
Version:
Amazon Cognito Identity Provider JavaScript SDK
63 lines (60 loc) • 4.15 kB
JavaScript
;
exports.__esModule = true;
exports["default"] = getRandomValues;
var _fastBase64Decode = _interopRequireDefault(require("fast-base64-decode"));
var _getRandomBase = _interopRequireDefault(require("./getRandomBase64"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _inheritsLoose(t, o) { t.prototype = Object.create(o.prototype), t.prototype.constructor = t, _setPrototypeOf(t, o); }
function _wrapNativeSuper(t) { var r = "function" == typeof Map ? new Map() : void 0; return _wrapNativeSuper = function _wrapNativeSuper(t) { if (null === t || !_isNativeFunction(t)) return t; if ("function" != typeof t) throw new TypeError("Super expression must either be null or a function"); if (void 0 !== r) { if (r.has(t)) return r.get(t); r.set(t, Wrapper); } function Wrapper() { return _construct(t, arguments, _getPrototypeOf(this).constructor); } return Wrapper.prototype = Object.create(t.prototype, { constructor: { value: Wrapper, enumerable: !1, writable: !0, configurable: !0 } }), _setPrototypeOf(Wrapper, t); }, _wrapNativeSuper(t); }
function _construct(t, e, r) { if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments); var o = [null]; o.push.apply(o, e); var p = new (t.bind.apply(t, o))(); return r && _setPrototypeOf(p, r.prototype), p; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _isNativeFunction(t) { try { return -1 !== Function.toString.call(t).indexOf("[native code]"); } catch (n) { return "function" == typeof t; } }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
var TypeMismatchError = /*#__PURE__*/function (_Error) {
function TypeMismatchError() {
return _Error.apply(this, arguments) || this;
}
_inheritsLoose(TypeMismatchError, _Error);
return TypeMismatchError;
}(/*#__PURE__*/_wrapNativeSuper(Error));
var QuotaExceededError = /*#__PURE__*/function (_Error2) {
function QuotaExceededError() {
return _Error2.apply(this, arguments) || this;
}
_inheritsLoose(QuotaExceededError, _Error2);
return QuotaExceededError;
}(/*#__PURE__*/_wrapNativeSuper(Error));
var warned = false;
function insecureRandomValues(array) {
if (!warned) {
console.warn('Using an insecure random number generator, this should only happen when running in a debugger without support for crypto.getRandomValues');
warned = true;
}
for (var i = 0, r; i < array.length; i++) {
if ((i & 0x03) === 0) r = Math.random() * 0x100000000;
array[i] = r >>> ((i & 0x03) << 3) & 0xff;
}
return array;
}
/**
* @param {Int8Array|Uint8Array|Int16Array|Uint16Array|Int32Array|Uint32Array|Uint8ClampedArray} array
*/
function getRandomValues(array) {
if (!(array instanceof Int8Array || array instanceof Uint8Array || array instanceof Int16Array || array instanceof Uint16Array || array instanceof Int32Array || array instanceof Uint32Array || array instanceof Uint8ClampedArray)) {
throw new TypeMismatchError('Expected an integer array');
}
if (array.byteLength > 65536) {
throw new QuotaExceededError('Can only request a maximum of 65536 bytes');
}
// Calling getRandomBase64 in debug mode leads to the error
// "Calling synchronous methods on native modules is not supported in Chrome".
// So in that specific case we fall back to just using Math.random.
if (__DEV__) {
if (typeof global.nativeCallSyncHook === 'undefined') {
return insecureRandomValues(array);
}
}
(0, _fastBase64Decode["default"])((0, _getRandomBase["default"])(array.byteLength), new Uint8Array(array.buffer, array.byteOffset, array.byteLength));
return array;
}