shim.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. const SEA = require('./root')
  2. const Buffer = require('./buffer')
  3. const api = {Buffer: Buffer}
  4. var o = {};
  5. if(SEA.window){
  6. api.crypto = navigator && navigator.product === 'ReactNative' ? require('isomorphic-webcrypto') : window.crypto || window.msCrypto || require('isomorphic-webcrypto');
  7. api.subtle = (api.crypto||o).subtle || (api.crypto||o).webkitSubtle;
  8. api.TextEncoder = window.TextEncoder;
  9. api.TextDecoder = window.TextDecoder;
  10. api.random = (len) => Buffer.from(api.crypto.getRandomValues(new Uint8Array(Buffer.alloc(len))));
  11. }
  12. if(!api.TextDecoder)
  13. {
  14. const { TextEncoder, TextDecoder } = require('text-encoding');
  15. api.TextDecoder = TextDecoder;
  16. api.TextEncoder = TextEncoder;
  17. }
  18. if(!api.crypto){try{
  19. var crypto = require('crypto', 1);
  20. Object.assign(api, {
  21. crypto,
  22. random: (len) => Buffer.from(crypto.randomBytes(len))
  23. });
  24. const isocrypto = require('isomorphic-webcrypto');
  25. api.ossl = api.subtle = isocrypto.subtle;
  26. }catch(e){
  27. console.log("text-encoding and @peculiar/webcrypto may not be included by default, please add it to your package.json!");
  28. TEXT_ENCODING_OR_PECULIAR_WEBCRYPTO_NOT_INSTALLED;
  29. }}
  30. module.exports = api