UNPKG

@lazy-random/cross-crypto

Version:
1 lines 3.01 kB
{"version":3,"file":"index.umd.development.cjs","sources":["../src/index.ts"],"sourcesContent":["/**\n * Created by user on 2018/11/25/025.\n */\n\nexport interface ICryptoLike\n{\n\trandomBytes(size: number, callback?: (err: Error | null, buf: Buffer) => void): Buffer;\n\n\tgetRandomValues?<T extends Int8Array | Int16Array | Int32Array | Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray | Float32Array | Float64Array | DataView | null>(array: T): T;\n}\n\nexport const crossCrypto = (() =>\n{\n\tlet crypto: ICryptoLike;\n\n\treturn (): ICryptoLike =>\n\t{\n\t\tif (typeof crypto === 'undefined')\n\t\t{\n\t\t\tlet _crypto: ICryptoLike;\n\t\t\ttry\n\t\t\t{\n\t\t\t\t_crypto = crypto = require('crypto')\n\t\t\t}\n\t\t\tcatch (e)\n\t\t\t{\n\t\t\t\t// @ts-ignore\n\t\t\t\t_crypto = global.crypto || global.msCrypto;\n\n\t\t\t\tif (_crypto?.getRandomValues)\n\t\t\t\t{\n\t\t\t\t\tcrypto = _crypto\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (!crypto?.randomBytes)\n\t\t\t{\n\t\t\t\tcrypto.randomBytes = crypto.randomBytes || function randomBytes(size: number, cb?: (err: Error | null, buf: Buffer) => void)\n\t\t\t\t{\n\t\t\t\t\tif (size > 65536) throw new Error('requested too many random bytes');\n\t\t\t\t\tlet rawBytes = new Uint8Array(size);\n\n\t\t\t\t\tif (size > 0)\n\t\t\t\t\t{\n\t\t\t\t\t\t_crypto.getRandomValues(rawBytes)\n\t\t\t\t\t}\n\n\t\t\t\t\t// XXX: phantomjs doesn't like a buffer being passed here\n\t\t\t\t\tlet bytes = Buffer.from(rawBytes.buffer);\n\n\t\t\t\t\tif (typeof cb === 'function')\n\t\t\t\t\t{\n\t\t\t\t\t\tcb(null, bytes)\n\t\t\t\t\t}\n\n\t\t\t\t\treturn bytes\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (!crypto)\n\t\t{\n\t\t\tcrypto = null;\n\t\t\tthrow new Error(`not support crypto`)\n\t\t}\n\n\t\treturn crypto\n\t};\n})();\n\nexport function randomBytes(size: number, callback?: (err: Error | null, buf: Buffer) => void): Buffer\n{\n\treturn crossCrypto().randomBytes(size, callback)\n}\n\nexport default crossCrypto\n"],"names":["crossCrypto","crypto","_crypto3","_crypto","require","e","_crypto2","global","msCrypto","randomBytes","size","cb","Error","rawBytes","Uint8Array","getRandomValues","bytes","Buffer","from","buffer"],"mappings":";;;;;;;CAAA;;AAEG,OAAAA,WAAA,gBAAA,CAAA,MAAA;;;CAsBO,IAAA,IAAA,OACNC,MAAA,KAAA,WAAA,EAAA;CAAA,MAAA,IAAAC,QAAA,CAAA;WAEOC,OAAA,CAAA;WAIA;gBAEP,GAAAF,MAAA,GAAAG,OAAA,CAED,QAAA,CAAA,CAAA;CAEC,OAAA,QAEmBC,CAAA,EAAA;CAAA,QAAA,IAAAC,QAAA,CAAA;CAClB;CAEAH,QAAAA,OAAA,GAAAI,MAAA,CAAAN,MAAA,IAAAM,MAAA,CAAAC,QAAA,CAAA;SAEC,IAAAF,CAAAA,QAAA,GAAAH,kCAAAG,KAAAA,CAAAA,IAAAA,QAAA;;;;+BAWD,MAAAJ,IAAAA,IAAAA,QAAA,wBAAAO,WAAA,CAAA,EAAA;SACDR,MAAA,CAAAQ,WAAA,GAAAR,MAAA,CAAAQ,WAAA,IAAAA,SAAAA,WAAAA,CAAAC,IAAA,EAAAC,EAAA,EAAA;6BAIS,MAAAC,IAAAA,KAAX,CAAW,iCAAA,CAAA,CAAA;CAGV,UAAA,IAAAC,QAAA,GAAU,IAAAC,UAAM,CAAAJ,IAAA,CAAA,CAAA;CAGjB,UAAA,IAAAA,IAAA;aAAaP,OAAA,CAAAY,eAAA,CAAAF,QAAA,CAAA,CAAA;CACb,WAAA;CAGF;CAGCG,UAAAA,IAAAA,KAAA,GAAAC,MAAA,CAAAC,IAAA,CAAAL,QAAA,CAAAM,MAAA,CAAA,CAAA;CAED,UAAA,IAAA,OAAAR,EAAA,KAAA,UAAA,EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}