tradle / rn-nodeify

hack to allow react-native projects to use node core modules, and npm modules that use them
MIT License
619 stars 112 forks source link

randombytes not supported #28

Closed josh-burton closed 7 years ago

josh-burton commented 7 years ago

I'm having trouble getting rn-nodeify to work with randombytes.

The read me says nodeify doesn't work with linked modules, so does that mean I can't use react-native-randombytes?

When I try to use a module that uses randombytes I get an error that secure number generation is not supported.

mvayngrib commented 7 years ago

re: linked modules: it doesn't work with symlinked modules (local modules linked via npm link). randombytes should be fine, this module's purpose is to enable modules like randombytes to work

the most common source of this issue is forgetting to run the rn-nodeify script (e.g. rn-nodeify --hack --install) after an npm install ... or npm uninstall ... operation

josh-burton commented 7 years ago

I've definitely run the script after install many times. What configuration should be in the browser and react-native sections of package.json for randombytes?

mvayngrib commented 7 years ago

in your project's package.json and in randombytes's package.json, if you're installing all the core shims, you should have something like this:

  "react-native": {
    "zlib": "browserify-zlib",
    "console": "console-browserify",
    "constants": "constants-browserify",
    "crypto": "react-native-crypto",
    "dns": "dns.js",
    "net": "react-native-tcp",
    "domain": "domain-browser",
    "http": "react-native-http",
    "https": "https-browserify",
    "os": "react-native-os",
    "path": "path-browserify",
    "querystring": "querystring-es3",
    "fs": "react-native-level-fs",
    "_stream_transform": "readable-stream/transform",
    "_stream_readable": "readable-stream/readable",
    "_stream_writable": "readable-stream/writable",
    "_stream_duplex": "readable-stream/duplex",
    "_stream_passthrough": "readable-stream/passthrough",
    "dgram": "react-native-udp",
    "stream": "stream-browserify",
    "timers": "timers-browserify",
    "tty": "tty-browserify",
    "vm": "vm-browserify"
  },
  "browser": {
    "zlib": "browserify-zlib",
    "console": "console-browserify",
    "constants": "constants-browserify",
    "crypto": "react-native-crypto",
    "dns": "dns.js",
    "net": "react-native-tcp",
    "domain": "domain-browser",
    "http": "react-native-http",
    "https": "https-browserify",
    "os": "os-browserify",
    "path": "path-browserify",
    "querystring": "querystring-es3",
    "fs": "react-native-level-fs",
    "_stream_transform": "readable-stream/transform",
    "_stream_readable": "readable-stream/readable",
    "_stream_writable": "readable-stream/writable",
    "_stream_duplex": "readable-stream/duplex",
    "_stream_passthrough": "readable-stream/passthrough",
    "dgram": "react-native-udp",
    "stream": "stream-browserify",
    "timers": "timers-browserify",
    "tty": "tty-browserify",
    "vm": "vm-browserify"
  }

if you can't get it to work, push your project and i'll take a look

josh-burton commented 7 years ago

Thanks I'll check my config and let you know :)

josh-burton commented 7 years ago

It seems some extra hacks for randombytes may be required in the webtorrent and bittorrent-tracker modules.

I've pushed a test project here: https://github.com/athornz/webtorrent-rn-nodeify

mvayngrib commented 7 years ago

@athornz looks like you forgot buffer in the list of --install. The easiest way to play around is to just let rn-nodeify install all available shims: rn-nodeify --install --hack . Seems to start ok after running that

josh-burton commented 7 years ago

When I did include buffer it gave me errors. Seems like the buffer provided wasn't compatible with safe-buffer that some modules were using.

I'll try again adding it back

josh-burton commented 7 years ago

Using all available shims I get the error: 'secure random number generation is not supported by this browser' (running on iOS).

Adding buffer to the list of shims gives the same error.

If I manually replace randombytes with react-native-randombytes in the package.json of webtorrent and bittorrent-tracker I start making progress (still have errors, but it's using the correct dependency), which is why I was thinking extra hacks are needed?

Are there any other dependencies or configuration that would cause me to see different results to you? I'm running node 7.4.0

mvayngrib commented 7 years ago

@athornz ah, I actually use a separate crypto shim in my project. Pasted below (import alongside './shim'):

const randomBytes = require('react-native-randombytes').randomBytes

if (typeof window === 'object') {
  const wCrypto = window.crypto = window.crypto || {}
  if (!wCrypto.getRandomValues) {
    wCrypto.getRandomValues = function getRandomValues (arr) {
      const bytes = randomBytes(arr.length)
      for (var i = 0; i < bytes.length; i++) {
        arr[i] = bytes[i]
      }
    }
  }
}

i guess i should add this to the crypto section in the 'shim.js' this library generates

mvayngrib commented 7 years ago

@athornz oh, actually i see shim.js already has an earlier variant of the above crypto shim. If you update https://github.com/athornz/webtorrent-rn-nodeify i can have another look

josh-burton commented 7 years ago

Ah ha! It seems I started the project with an old version of rn-nodeify, the shim.js was generated, and when I updated rn-nodeify, the shim was never updated.

I deleted the shim, re ran rn-nodeify and everything works!

Those sample projects probably need updating....

Thanks!

mvayngrib commented 7 years ago

@athornz great! And yes, sample projects need updating. PRs welcome :)