diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..c9098bc --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,283 @@ +module.exports = { + "env": { + "es6": true, + "node": true + }, + "globals": { + // react native + "window": true, + "__DEV__": true, + "localStorage": true + }, + "extends": "eslint:recommended", + "parserOptions": { + "sourceType": "module" + }, + "rules": { + "accessor-pairs": "error", + "array-bracket-spacing": "warn", + "array-callback-return": "warn", + "arrow-body-style": "warn", + "arrow-parens": [ + "error", + "as-needed" + ], + "arrow-spacing": [ + "error", + { + "after": true, + "before": true + } + ], + "block-scoped-var": "error", + "block-spacing": "error", + "brace-style": [ + "error", + "1tbs" + ], + "callback-return": "error", + "camelcase": "warn", + "capitalized-comments": "off", + "class-methods-use-this": "error", + "comma-dangle": "off", + "comma-spacing": [ + "error", + { + "after": true, + "before": false + } + ], + "comma-style": [ + "error", + "last" + ], + "complexity": "error", + "computed-property-spacing": "error", + "consistent-return": "warn", + "consistent-this": "warn", + "curly": "off", + "default-case": "error", + "dot-location": "off", + "dot-notation": "warn", + "eol-last": "warn", + "eqeqeq": "warn", + "func-call-spacing": "error", + "func-name-matching": "error", + "func-names": "warn", + "func-style": "warn", + "generator-star-spacing": "off", + "global-require": "off", + "guard-for-in": "warn", + "handle-callback-err": "warn", + "id-blacklist": "error", + "id-length": "off", + "id-match": "error", + "indent": "off", + "init-declarations": "off", + "jsx-quotes": "error", + "key-spacing": "error", + "keyword-spacing": [ + "error", + { + "after": true, + "before": true + } + ], + "line-comment-position": "warn", + "linebreak-style": [ + "error", + "unix" + ], + "lines-around-comment": "warn", + "lines-around-directive": "error", + "max-depth": "error", + "max-len": "off", + "max-lines": "warn", + "max-nested-callbacks": "error", + "max-params": "warn", + "max-statements": "off", + "max-statements-per-line": "error", + "multiline-ternary": "warn", + "new-cap": "error", + "new-parens": "error", + "newline-after-var": "off", + "newline-before-return": "off", + "newline-per-chained-call": "warn", + "no-alert": "error", + "no-array-constructor": "error", + "no-await-in-loop": "error", + "no-bitwise": "error", + "no-caller": "error", + "no-case-declarations": "warn", + "no-catch-shadow": "warn", + "no-cond-assign": "warn", + "no-confusing-arrow": "error", + "no-console": "warn", + "no-constant-condition": "warn", + "no-continue": "warn", + "no-div-regex": "error", + "no-duplicate-imports": "error", + "no-else-return": "error", + "no-ex-assign": "off", + "no-empty": [ + "error", + { + "allowEmptyCatch": true + } + ], + "no-empty-function": "off", + "no-eq-null": "warn", + "no-eval": "error", + "no-extend-native": "error", + "no-extra-bind": "error", + "no-extra-label": "error", + "no-extra-parens": "warn", + "no-extra-semi": "warn", + "no-floating-decimal": "error", + "no-implicit-coercion": "warn", + "no-implicit-globals": "error", + "no-implied-eval": "error", + "no-inline-comments": "off", + "no-inner-declarations": "warn", + "no-invalid-this": "warn", + "no-iterator": "error", + "no-label-var": "error", + "no-labels": "error", + "no-lone-blocks": "error", + "no-lonely-if": "warn", + "no-loop-func": "error", + "no-magic-numbers": "off", + "no-mixed-operators": "error", + "no-mixed-requires": "error", + "no-multi-assign": "warn", + "no-multi-spaces": "error", + "no-multi-str": "error", + "no-multiple-empty-lines": "error", + "no-native-reassign": "error", + "no-negated-condition": "warn", + "no-negated-in-lhs": "error", + "no-nested-ternary": "error", + "no-new": "error", + "no-new-func": "error", + "no-new-object": "error", + "no-new-require": "error", + "no-new-wrappers": "error", + "no-octal-escape": "error", + "no-param-reassign": "warn", + "no-path-concat": "error", + "no-process-env": "off", + "no-process-exit": "off", + "no-proto": "error", + "no-prototype-builtins": "error", + "no-restricted-globals": "error", + "no-restricted-imports": "error", + "no-restricted-modules": "error", + "no-restricted-properties": "error", + "no-restricted-syntax": "error", + "no-return-assign": "warn", + "no-return-await": "error", + "no-script-url": "error", + "no-self-compare": "error", + "no-sequences": "error", + "no-shadow": "warn", + "no-shadow-restricted-names": "error", + "no-spaced-func": "error", + "no-sync": "off", + "no-tabs": "error", + "no-template-curly-in-string": "error", + "no-ternary": "off", + "no-throw-literal": "error", + "no-trailing-spaces": "error", + "no-undef-init": "warn", + "no-undefined": "warn", + "no-underscore-dangle": "warn", + "no-unmodified-loop-condition": "error", + "no-unneeded-ternary": "error", + "no-unused-expressions": "error", + "no-unused-vars": "warn", + "no-use-before-define": "off", + "no-useless-call": "error", + "no-useless-computed-key": "error", + "no-useless-concat": "error", + "no-useless-constructor": "error", + "no-useless-escape": "warn", + "no-useless-rename": "error", + "no-useless-return": "error", + "no-var": "warn", + "no-void": "error", + "no-warning-comments": "warn", + "no-whitespace-before-property": "error", + "no-with": "error", + "object-curly-newline": "off", + "object-curly-spacing": [ + "error", + "always" + ], + "object-property-newline": [ + "error", + { + "allowMultiplePropertiesPerLine": true + } + ], + "object-shorthand": "warn", + "one-var": "off", + "one-var-declaration-per-line": "error", + "operator-assignment": "error", + "operator-linebreak": "error", + "padded-blocks": "off", + "prefer-const": "warn", + "prefer-destructuring": "warn", + "prefer-numeric-literals": "error", + "prefer-promise-reject-errors": "error", + "prefer-reflect": "warn", + "prefer-rest-params": "warn", + "prefer-spread": "warn", + "prefer-template": "off", + "quote-props": "off", + "quotes": "off", + "radix": "error", + "require-await": "error", + "require-jsdoc": "off", + "require-yield": "off", + "rest-spread-spacing": [ + "error", + "never" + ], + "semi": "off", + "semi-spacing": "error", + "sort-imports": "error", + "sort-keys": "off", + "sort-vars": "error", + "space-before-blocks": "error", + "space-before-function-paren": "error", + "space-in-parens": [ + "error", + "never" + ], + "space-infix-ops": "off", + "space-unary-ops": "error", + "spaced-comment": [ + "error", + "always" + ], + "strict": "error", + "symbol-description": "error", + "template-curly-spacing": [ + "error", + "never" + ], + "unicode-bom": [ + "error", + "never" + ], + "valid-jsdoc": "warn", + "vars-on-top": "off", + "wrap-iife": "error", + "wrap-regex": "warn", + "yield-star-spacing": "error", + "yoda": [ + "error", + "never" + ] + } +}; diff --git a/browser.json b/browser.json index 751b7ca..36cf75e 100644 --- a/browser.json +++ b/browser.json @@ -1,4 +1,5 @@ { + "q": "bluebird-q", "zlib": "browserify-zlib", "console": "console-browserify", "constants": "constants-browserify", @@ -21,5 +22,6 @@ "stream": "stream-browserify", "timers": "timers-browserify", "tty": "tty-browserify", - "vm": "vm-browserify" + "vm": "vm-browserify", + "tls": false } diff --git a/cmd.js b/cmd.js index 3e98ed4..d3ccbc8 100755 --- a/cmd.js +++ b/cmd.js @@ -10,6 +10,7 @@ var deepEqual = require('deep-equal') var find = require('findit') var minimist = require('minimist') var parallel = require('run-parallel') +var yarnlock = require('@yarnpkg/lockfile') var allShims = require('./shims') var coreList = require('./coreList') var browser = require('./browser') @@ -18,12 +19,16 @@ var pkg = require(pkgPath) var hackFiles = require('./pkg-hacks') var argv = minimist(process.argv.slice(2), { alias: { + h: 'help', i: 'install', e: 'hack', - h: 'help' + o: 'overwrite', + y: 'yarn' } }) +var BASE_INSTALL_LINE = argv.yarn ? 'yarn add' : 'npm install --save' + if (argv.help) { runHelp() process.exit(0) @@ -62,7 +67,10 @@ function run () { } if (argv.install) { - installShims(toShim, function (err) { + installShims({ + modules: toShim, + overwrite: argv.overwrite + }, function (err) { if (err) throw err runHacks() @@ -83,36 +91,100 @@ function run () { } } -function installShims (modulesToShim, done) { - var shimPkgNames = modulesToShim.map(function (m) { +function installShims ({ modules, overwrite }, done) { + if (!overwrite) { + modules = modules.filter(name => { + const shimPackageName = browser[name] || name + if (pkg.dependencies[shimPackageName]) { + log(`not overwriting "${shimPackageName}"`) + return false + } + + return true + }) + } + + var shimPkgNames = modules + .map(function (m) { return browser[m] || m - }).filter(function (shim) { + }) + .filter(function (shim) { return !/^_/.test(shim) && shim.indexOf('/') === -1 }) - var existence = [] + if (!shimPkgNames.length) { + return finish() + } + + // Load the exact package versions from the lockfile + var lockfile + if (argv.yarn) { + if (fs.existsSync('yarn.lock')) { + let result = yarnlock.parse(fs.readFileSync('yarn.lock', 'utf8')) + if (result.type == 'success') { + lockfile = result.object + } + } + } else { + var lockpath = path.join(process.cwd(), 'package-lock.json') + if (fs.existsSync(lockpath)) { + let result = require(lockpath) + if (result && result.dependencies) { + lockfile = result.dependencies + } + } + } + parallel(shimPkgNames.map(function (name) { var modPath = path.resolve('./node_modules/' + name) - return function (cb) { + return function (cb) { fs.exists(modPath, function (exists) { if (!exists) return cb() var install = true - var pkgJson = require(modPath + '/package.json') - if (/^git\:\/\//.test(pkgJson._resolved)) { - var hash = allShims[name].split('#')[1] - if (hash && pkgJson.gitHead.indexOf(hash) === 0) { - install = false + if (lockfile) { + // Use the lockfile to resolve installed version of package + if (argv.yarn) { + if (`${name}@${allShims[name]}` in lockfile) { + install = false + } + } else { + var lockfileVer = (lockfile[name] || {}).version + var targetVer = allShims[name] + if (semver.valid(lockfileVer)) { + if (semver.satisfies(lockfileVer, targetVer)) { + install = false + } + } else if (lockfileVer) { + // To be considered up-to-date, we need an exact match, + // after doing some normalization of github url's + if (lockfileVer.startsWith('github:')) { + lockfileVer = lockfileVer.slice(7) + } + if (lockfileVer.indexOf(targetVer) == 0) { + install = false + } + } } } else { - var existingVer = pkgJson.version - if (semver.satisfies(existingVer, allShims[name])) { - install = false + // Fallback to using the version from the dependency's package.json + var pkgJson = require(modPath + '/package.json') + if (/^git\:\/\//.test(pkgJson._resolved)) { + var hash = allShims[name].split('#')[1] + if (hash && pkgJson.gitHead.indexOf(hash) === 0) { + install = false + } + } else { + var existingVerNpm5 = (/\-([^\-]+)\.tgz/.exec(pkgJson.version) || [null, null])[1] + var existingVer = existingVerNpm5 || pkgJson.version + if (semver.satisfies(existingVer, allShims[name])) { + install = false + } } } if (!install) { - console.log('not reinstalling ' + name) + log('not reinstalling ' + name) shimPkgNames.splice(shimPkgNames.indexOf(name), 1) } @@ -126,24 +198,35 @@ function installShims (modulesToShim, done) { return finish() } - var installLine = 'npm install --save ' + var installLine = BASE_INSTALL_LINE + ' ' shimPkgNames.forEach(function (name) { - if (allShims[name].indexOf('/') === -1) { - console.log('installing from npm', name) - installLine += name + '@' + allShims[name] + let version = allShims[name] + if (!version) return + if (version.indexOf('/') === -1) { + if (argv.yarn) { + log('installing from yarn', name) + } else { + log('installing from npm', name) + } + installLine += name + '@' + version } else { // github url - console.log('installing from github', name) - installLine += allShims[name].match(/([^\/]+\/[^\/]+)$/)[1] + log('installing from github', name) + installLine += version.match(/([^\/]+\/[^\/]+)$/)[1] } - pkg.dependencies[name] = allShims[name] + pkg.dependencies[name] = version installLine += ' ' }) fs.writeFile(pkgPath, JSON.stringify(pkg, null, 2), function (err) { if (err) throw err + if (installLine.trim() === BASE_INSTALL_LINE) { + return finish() + } + + log('installing:', installLine) proc.execSync(installLine, { cwd: process.cwd(), env: process.env, @@ -152,17 +235,19 @@ function installShims (modulesToShim, done) { finish() }) - - console.log('installing:', installLine) - function finish () { - copyShim(done) - } }) + + function finish () { + copyShim(done) + } } function copyShim (cb) { fs.exists('./shim.js', function (exists) { - if (exists) return cb() + if (exists) { + log('not overwriting shim.js. For the latest version, see rn-nodeify/shim.js') + return cb() + } fs.readFile(path.join(__dirname, 'shim.js'), { encoding: 'utf8' }, function (err, contents) { if (err) return cb(err) @@ -202,7 +287,7 @@ function fixPackageJSON (modules, file, overwrite) { } // if (shims[pkgJson.name]) { - // console.log('skipping', pkgJson.name) + // log('skipping', pkgJson.name) // return // } @@ -222,7 +307,7 @@ function fixPackageJSON (modules, file, overwrite) { depBrowser[p] = browser[p] } else { if (!overwrite && orgBrowser[p] !== browser[p]) { - console.log('not overwriting mapping', p, orgBrowser[p]) + log('not overwriting mapping', p, orgBrowser[p]) } else { depBrowser[p] = browser[p] } @@ -230,12 +315,28 @@ function fixPackageJSON (modules, file, overwrite) { } modules.forEach(function (p) { - if (depBrowser[p] === false) { - console.log('removing browser exclude', file, p) + if (depBrowser[p] === false && browser[p] !== false) { + log('removing browser exclude', file, p) delete depBrowser[p] } }) + + const { main } = pkgJson + if (main) { + const alt = main.startsWith('./') ? main.slice(2) : './' + main + if (depBrowser[alt]) { + depBrowser[main] = depBrowser[alt] + log(`normalized "main" browser mapping in ${pkgJson.name}, fixed here: https://github.com/facebook/metro-bundler/pull/3`) + delete depBrowser[alt] + } + } + + if (pkgJson.name === 'constants-browserify') { + // otherwise react-native packager chokes for some reason + delete depBrowser.constants + } + if (!deepEqual(orgBrowser, depBrowser)) { pkgJson.browser = pkgJson['react-native'] = depBrowser delete pkgJson.browserify @@ -249,7 +350,7 @@ function rethrow (err) { } function runHelp () { - console.log(function () { + log(function () { /* Usage: rn-nodeify --install dns,stream,http,https @@ -260,9 +361,15 @@ function runHelp () { -h --help show usage -e, --hack run package-specific hacks (list or leave blank to run all) -i, --install install shims (list or leave blank to install all) + -o, --overwrite updates installed packages if a newer version is available + -y, --yarn use yarn to install packages instead of npm (experimental) Please report bugs! https://github.com/mvayngrib/rn-nodeify/issues */ }.toString().split(/\n/).slice(2, -2).join('\n')) process.exit(0) } + +function log () { + console.log.apply(console, arguments) +} diff --git a/coreList.js b/coreList.js index 71f2d1a..ebcf4c5 100644 --- a/coreList.js +++ b/coreList.js @@ -26,5 +26,7 @@ module.exports = [ "url", "util", "net", - "vm" + "vm", + // note: tls doesn't have a shim + "tls" ] diff --git a/package.json b/package.json index 764bfca..b5d9f21 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,15 @@ { "name": "rn-nodeify", - "version": "7.0.1", + "repository": { + "type": "git", + "url": "https://github.com/mvayngrib/rn-nodeify.git" + }, + "version": "8.2.0", "preferGlobal": true, "bin": "./cmd.js", "license": "MIT", "dependencies": { + "@yarnpkg/lockfile": "^1.0.0", "deep-equal": "^1.0.0", "findit": "^2.0.0", "fs-extra": "^0.22.1", @@ -13,5 +18,12 @@ "run-parallel": "^1.1.2", "semver": "^5.0.1", "xtend": "^4.0.0" + }, + "scripts": { + "lint": "eslint --quiet --ignore-path .gitignore .", + "precommit": "npm run lint" + }, + "devDependencies": { + "husky": "^0.14.3" } } diff --git a/pkg-hacks.js b/pkg-hacks.js index 2ddca82..f5d0991 100644 --- a/pkg-hacks.js +++ b/pkg-hacks.js @@ -5,6 +5,9 @@ var proc = require('child_process') var fs = require('fs-extra') var find = require('findit') var path = require('path') + +var localHackersFilename = '.rn-nodeify-hacks' +var cwd = process.cwd() // var thisPkg = require('./package.json') // function loadDeps() { @@ -26,14 +29,16 @@ var path = require('path') module.exports = function hackFiles (hacks) { var finder = find('./node_modules') - hacks = hacks || hackers.map(function (h) { return h.name }) + hacks = hacks || getHackers(hackers).map(function (h) { return h.name }) finder.on('file', function (file) { - if (!/\.(js|json)$/.test(file) - || /\/tests?\//.test(file)) { + if (!/\.(js|json)$/.test(file) || + /\/tests?\//.test(file)) { return } + file = file.replace(/\\/g, path.posix.sep); + var matchingHackers = hackers.filter(function (hacker) { return hacks.indexOf(hacker.name) !== -1 && hacker.regex.some(function (regex) { return regex.test(file) @@ -60,7 +65,25 @@ module.exports = function hackFiles (hacks) { }) } +function getHackers(hackers) { + var allHacks = hackers + var localHacksFile = path.join(cwd, localHackersFilename) + + try { + var localHacks = require(localHacksFile) + + if (localHacks && localHacks.length) { + console.log('Loaded ' + localHacks.length + ' hacks from ' + localHacksFile) + + allHacks = allHacks.concat(localHacks) + } + } catch (e) { + console.log('Not loading local hacks') + } + + return allHacks +} // loadDeps(hackFiles) @@ -135,10 +158,18 @@ var hackers = [ { name: 'rn-bundler', regex: [ - /react\-packager\/src\/bundler\/bundle\.js/i + /react\-(?:native\/)?packager\/src\/bundler\/bundle\.js$/i, + /react\-(?:native\/)?packager\/src\/JSTransformer\/worker\/minify\.js$/i, + /metro\/src\/JSTransformer\/worker\/minify\.js$/i, + /metro-minify-uglify\/src\/minifier\.js$/i, ], hack: function (file, contents) { - var fixed = contents.replace(/(fromString: true,)(\s+)(outSourceMap)/, '$1$2mangle:false,$2$3') + if (contents.indexOf('mangle:false') !== -1) return + + var fixed = contents + .replace('mangle: { toplevel: true }', 'mangle:false') + .replace(/(\s+)(fromString: true,)/, '$1$2$1mangle:false,') + return contents === fixed ? null : fixed } }, @@ -160,7 +191,7 @@ var hackers = [ regex: [ /webtorrent\/lib\/fs-storage\.js/ ], - hack: function(file, contents) { + hack: function (file, contents) { if (isInReactNative(file)) return var fixed = contents.replace(/fs\.existsSync\([^\)]*\)/g, 'false') @@ -174,7 +205,7 @@ var hackers = [ ], hack: function (file, contents) { var fixed = contents.replace(/typeof\ FileReaderSync \!\=\= \'undefined\'/, 'false') - return contents === fixed ? null : fixed + return contents === fixed ? null : fixed } }, { @@ -221,12 +252,13 @@ var hackers = [ regex: [ /levelup\/lib\/util\.js$/ ], - hack: function(file, contents) { + hack: function (file, contents) { if (isInReactNative(file)) return var fixed = contents fixed = fixed.replace("require('../package.json').devDependencies.leveldown", "'1.0.0'") fixed = fixed.replace("require('leveldown/package').version", "'1.0.0'") + fixed = fixed.replace("require('leveldown/package.json').version", "'1.0.0'") fixed = fixed.replace("require('leveldown')", "null") // var bad = '\'leveldown' @@ -253,7 +285,7 @@ var hackers = [ regex: [ /level-jobs\/package\.json$/ ], - hack: function(file, contents) { + hack: function (file, contents) { if (isInReactNative(file)) return var pkg @@ -421,7 +453,7 @@ var hackers = [ regex: [ /bytewise\/bytewise\.js$/ ], - hack: function(file, contents) { + hack: function (file, contents) { if (isInReactNative(file)) return var fixed = contents @@ -509,7 +541,18 @@ var hackers = [ return contents.replace(/_crypto\s+=\s+\(\s+g\.crypto\s+\|\|\s+g.msCrypto\s+\|\|\s+require\('crypto'\)\s+\)/, hack) } - } + }, + { + name: 'version', + regex: [/pbkdf2/], + hack: function (file, contents) { + if (isInReactNative(file)) return + + var fixed = contents.replace('process.version', '"' + process.version + '"') + + return contents === fixed ? null : fixed + } + }, ] function rewireMain (pkg) { @@ -517,8 +560,7 @@ function rewireMain (pkg) { var main = pkg.browser || './index.js' pkg.browser = {} pkg.browser[pkg.main] = main - } - else if (typeof pkg.browser === 'undefined') { + } else if (typeof pkg.browser === 'undefined') { pkg.browser = {} } } diff --git a/readme.md b/readme.md index c0b105f..eb61253 100644 --- a/readme.md +++ b/readme.md @@ -2,6 +2,22 @@ Run after npm install and you can use node core modules and npm modules that use them in your React Native app. +## What is solves + +If your project has no non-React-Native dependencies, you don't need this module, and you should just check out ['./shims.js'](./shims.js) for the core node modules to use individually. + +However, with bigger projects that don't reimplement every wheel from scratch, somewhere in your dependency tree, something uses a core node module. I found myself building this because in my React Native app, I wanted to use [bitcoinjs-lib](https://github.com/bitcoinjs/bitcoinjs-lib), [levelup](https://github.com/Level/levelup), [bittorrent-dht](https://github.com/feross/bittorrent-dht), and lots of fun crypto. If that sounds like you, keep reading. + +## What it does + +`rn-nodeify --install` +installs shims for core node modules, see ['./shims.js'](./shims.js) for the current mappings. It recurses down `node_modules` and modifies all the `package.json`'s in there to add/update the `browser` and `react-native` fields. It sounds scary because it is. However, it does work. + +`rn-nodeify --hack` +Now that you're scared, I should also mention that there are some package-specific hacks (see ['./pkg-hacks.js'](./pkg-hacks.js)), for when the React Native packager choked on something that Webpack and Browserify swallowed. + +If you're looking for a saner approach, check out [ReactNativify](https://github.com/philikon/ReactNativify). I haven't tested it myself, but I think [philikon](https://github.com/philikon) will be happy to help. + ## Usage ```bash @@ -11,8 +27,8 @@ rn-nodeify ## Options ``` ---install install node core shims (default: install all), fix the "browser" - (later "react-native") fields in the package.json's of dependencies +--install install node core shims (default: install all), fix the "browser" + and "react-native" fields in the package.json's of dependencies --hack hack individual packages that are known to make the React Native packager choke ``` @@ -48,6 +64,12 @@ rn-nodeify will create a `shim.js` file in your project root directory. The firs import './shim' ``` +Some shims may require linking libraries, be sure to run `react-native link` after installing new shims if you run into problems. + +### Local hacks + +You can create your own local hacks by adding a file named `.rn-nodeify-hacks` on your cwd path. This file should `export` a javascript array with the same syntax as the `hackers` variable in the `pkg-hacks.js` file. You can use this functionality to extend the the `--hack` capabilities of this package. + ### Example Apps / Workflows * the [react-native-crypto](https://github.com/mvayngrib/react-native-crypto) package has an example workflow for using crypto in a React Native app @@ -88,3 +110,8 @@ copied from [react-native-crypto](https://github.com/mvayngrib/react-native-cryp - when installing a package from git, the postinstall hook isn't triggered, run it manually instead (`npm run postinstall`) - restart the react-native packager after installing a module! - removing the packager cache helps as well sometimes (`rm -fr $TMPDIR/react-*`) +- rn-nodeify currently uses `npm` to install shims. PRs are welcome to make it compatible with `yarn` +- use `npm@3`. `npm@5` has some issues that cause `node_modules` to disappear. See: + - https://github.com/mvayngrib/rn-nodeify/issues/42 + - https://github.com/infinitered/ignite/issues/1101 + - https://github.com/npm/npm/issues/16839 diff --git a/shim.js b/shim.js index ac5eb5a..65d986e 100644 --- a/shim.js +++ b/shim.js @@ -20,40 +20,3 @@ process.env['NODE_ENV'] = isDev ? 'development' : 'production' if (typeof localStorage !== 'undefined') { localStorage.debug = isDev ? '*' : '' } - -if (require('./package.json').dependencies['react-native-crypto']) { - const algos = require('browserify-sign/algos') - if (!algos.sha256) { - algos.sha256 = { - "sign": "ecdsa", - "hash": "sha256", - "id": new Buffer("") - } - } - - if (typeof window === 'object') { - const wCrypto = window.crypto = window.crypto || {} - wCrypto.getRandomValues = wCrypto.getRandomValues || getRandomValues - } - - const crypto = require('crypto') - const randomBytes = crypto.randomBytes - crypto.randomBytes = function (size, cb) { - if (cb) return randomBytes.apply(crypto, arguments) - - const arr = new Buffer(size) - getRandomValues(arr) - return arr - } - - crypto.getRandomValues = crypto.getRandomValues || getRandomValues - - function getRandomValues (arr) { - // console.warn('WARNING: generating insecure psuedorandom number') - for (var i = 0; i < arr.length; i++) { - arr[i] = Math.random() * 256 | 0 - } - - return arr - } -} diff --git a/shims-browserify.js b/shims-browserify.js index 7c201e0..5952ae2 100644 --- a/shims-browserify.js +++ b/shims-browserify.js @@ -4,7 +4,7 @@ module.exports = { "buffer": "^3.0.0", "inherits": "~2.0.1", "console-browserify": "^1.1.0", - "constants-browserify": "~0.0.1", + "constants-browserify": "^1.0.0", "crypto-browserify": "~3.2.6", "dns.js": "^1.0.1", "domain-browser": "~1.1.0", @@ -27,6 +27,6 @@ module.exports = { "url": "~0.10.1", "util": "~0.10.1", "utp": "0.0.8", - "react-native-tcp": "^2.0.4", + "react-native-tcp": "^3.2.1", "vm-browserify": "~0.0.1" } diff --git a/shims.js b/shims.js index b295c4d..3c9c99b 100644 --- a/shims.js +++ b/shims.js @@ -1,12 +1,13 @@ module.exports = { + "bluebird-q": "latest", "assert": "^1.1.1", "browserify-zlib": "~0.1.4", "buffer": "^4.9.1", "inherits": "^2.0.1", "console-browserify": "^1.1.0", - "constants-browserify": "~0.0.1", - "react-native-crypto": "^2.0.0", - "react-native-randombytes": "^2.1.0", + "constants-browserify": "^1.0.0", + "react-native-crypto": "^2.1.0", + "react-native-randombytes": "^3.0.0", "dns.js": "^1.0.1", "domain-browser": "^1.1.1", "events": "^1.0.0", @@ -18,7 +19,7 @@ module.exports = { "punycode": "^1.2.4", "querystring-es3": "~0.2.0", "react-native-level-fs": "^3.0.0", - "react-native-udp": "^1.2.0", + "react-native-udp": "^2.1.0", "readable-stream": "1.0.33", // "stream-browserify": "substack/stream-browserify#fa56e68", "stream-browserify": "^1.0.0", @@ -27,6 +28,6 @@ module.exports = { "tty-browserify": "0.0.0", "url": "~0.10.1", "util": "~0.10.3", - "react-native-tcp": "^2.0.4", + "react-native-tcp": "^3.2.1", "vm-browserify": "0.0.4" }