UNPKG

@lazy-random/cross-crypto

Version:
61 lines (56 loc) 2 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.LazyRandomCrossCrypto = {})); })(this, (function (exports) { 'use strict'; /** * Created by user on 2018/11/25/025. */ const crossCrypto = /*#__PURE__*/(() => { let crypto; return () => { if (typeof crypto === 'undefined') { var _crypto3; let _crypto; try { _crypto = crypto = require('crypto'); } catch (e) { var _crypto2; // @ts-ignore _crypto = global.crypto || global.msCrypto; if ((_crypto2 = _crypto) !== null && _crypto2 !== void 0 && _crypto2.getRandomValues) { crypto = _crypto; } } if (!((_crypto3 = crypto) !== null && _crypto3 !== void 0 && _crypto3.randomBytes)) { crypto.randomBytes = crypto.randomBytes || function randomBytes(size, cb) { if (size > 65536) throw new Error('requested too many random bytes'); let rawBytes = new Uint8Array(size); if (size > 0) { _crypto.getRandomValues(rawBytes); } // XXX: phantomjs doesn't like a buffer being passed here let bytes = Buffer.from(rawBytes.buffer); if (typeof cb === 'function') { cb(null, bytes); } return bytes; }; } } if (!crypto) { crypto = null; throw new Error(`not support crypto`); } return crypto; }; })(); function randomBytes(size, callback) { return crossCrypto().randomBytes(size, callback); } exports.crossCrypto = crossCrypto; exports.default = crossCrypto; exports.randomBytes = randomBytes; Object.defineProperty(exports, '__esModule', { value: true }); })); //# sourceMappingURL=index.umd.development.cjs.map