From d3e46cd14a9baa03722ff6b685145c89d4e04ebe Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Fri, 20 Jan 2017 18:06:29 -0500 Subject: [PATCH 01/39] update crypto section of shim --- shim.js | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/shim.js b/shim.js index ac5eb5a..cebf717 100644 --- a/shim.js +++ b/shim.js @@ -31,29 +31,17 @@ if (require('./package.json').dependencies['react-native-crypto']) { } } + const randomBytes = require('react-native-randombytes').randomBytes + 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 + 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] + } + } } - - return arr } } From 48f57b1472df8d0270895bf2cda1a848bec57b05 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Wed, 13 Jul 2016 19:29:32 -0400 Subject: [PATCH 02/39] update rn-bundler hack to prevent function name mangling --- pkg-hacks.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg-hacks.js b/pkg-hacks.js index 2ddca82..7e0854a 100644 --- a/pkg-hacks.js +++ b/pkg-hacks.js @@ -135,10 +135,13 @@ var hackers = [ { name: 'rn-bundler', regex: [ - /react\-packager\/src\/bundler\/bundle\.js/i + /react\-packager\/src\/bundler\/bundle\.js/i, + /react\-packager\/src\/JSTransformer\/worker\/minify\.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(/(\s+)(fromString: true,)/, '$1$2$1mangle:false,') return contents === fixed ? null : fixed } }, From e6e4e08c3d52cb85bcb79a219df556dceca6229a Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Sat, 21 Jan 2017 23:21:37 -0500 Subject: [PATCH 03/39] fix crypto shim --- shim.js | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/shim.js b/shim.js index cebf717..5f28b72 100644 --- a/shim.js +++ b/shim.js @@ -22,6 +22,7 @@ if (typeof localStorage !== 'undefined') { } if (require('./package.json').dependencies['react-native-crypto']) { + // important that this comes before require('crypto') const algos = require('browserify-sign/algos') if (!algos.sha256) { algos.sha256 = { @@ -31,17 +32,25 @@ if (require('./package.json').dependencies['react-native-crypto']) { } } - const randomBytes = require('react-native-randombytes').randomBytes - + let crypto 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] - } - } + crypto = window.crypto = window.crypto || {} + } else { + crypto = require('crypto') + } + + if (!crypto.getRandomValues) { + crypto.getRandomValues = getRandomValues + } + + let randomBytes + + function getRandomValues (arr) { + if (!randomBytes) randomBytes = require('react-native-randombytes').randomBytes + + const bytes = randomBytes(arr.length) + for (var i = 0; i < bytes.length; i++) { + arr[i] = bytes[i] } } } From 81fd8bf49a6db6c968ef40452a907eb2bda5cb02 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Mon, 23 Jan 2017 14:45:55 -0500 Subject: [PATCH 04/39] don't overwrite window.crypto if not necessary --- shim.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shim.js b/shim.js index 5f28b72..6e7f184 100644 --- a/shim.js +++ b/shim.js @@ -34,7 +34,8 @@ if (require('./package.json').dependencies['react-native-crypto']) { let crypto if (typeof window === 'object') { - crypto = window.crypto = window.crypto || {} + if (!window.crypto) window.crypto = {} + crypto = window.crypto } else { crypto = require('crypto') } From a48a4fc306ca845272f1615acc0c885a2939a334 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 14 Feb 2017 15:03:18 +1100 Subject: [PATCH 05/39] package: add github url --- package.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/package.json b/package.json index 764bfca..c5b8f82 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,9 @@ { "name": "rn-nodeify", + "repository": { + "type": "git", + "url": "https://github.com/mvayngrib/rn-nodeify.git" + }, "version": "7.0.1", "preferGlobal": true, "bin": "./cmd.js", From 5fd5b7d8a29ad956d9077d82fa6f53ee22727b21 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Fri, 17 Feb 2017 18:36:16 -0500 Subject: [PATCH 06/39] add explanation --- readme.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/readme.md b/readme.md index c0b105f..3464370 100644 --- a/readme.md +++ b/readme.md @@ -2,6 +2,21 @@ 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 + +`--install` installs shims for core node modules, see ['./shims.js']('./shims.js') for the current mappings. +`--hack` recursively hacks your `node_modules` directory 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. + +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 [https://github.com/philikon/ReactNativify](ReactNativify). I haven't tested it myself, but I think [philikon](https://github.com/philikon) will be happy to help. + ## Usage ```bash From 1c0f1666041ac49dc1ad727e6cd373a5c3334a3b Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Sat, 18 Feb 2017 13:21:22 -0500 Subject: [PATCH 07/39] fix readme --- readme.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/readme.md b/readme.md index 3464370..e0c8411 100644 --- a/readme.md +++ b/readme.md @@ -10,9 +10,10 @@ However, with bigger projects that don't reimplement every wheel from scratch, s ## What it does -`--install` installs shims for core node modules, see ['./shims.js']('./shims.js') for the current mappings. -`--hack` recursively hacks your `node_modules` directory 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 --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 [https://github.com/philikon/ReactNativify](ReactNativify). I haven't tested it myself, but I think [philikon](https://github.com/philikon) will be happy to help. @@ -27,7 +28,7 @@ rn-nodeify ``` --install install node core shims (default: install all), fix the "browser" - (later "react-native") fields in the package.json's of dependencies + 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 ``` From 68977ebb7a6f1396c070af62811d2152d1418176 Mon Sep 17 00:00:00 2001 From: Ricardo Valeriano Date: Thu, 23 Feb 2017 21:59:30 +0100 Subject: [PATCH 08/39] :pencil: Make the `./shims.js` links clicable. It was pointing to an incorrect url (with actual `'` on it). --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index e0c8411..1f61477 100644 --- a/readme.md +++ b/readme.md @@ -4,14 +4,14 @@ Run after npm install and you can use node core modules and npm modules that use ## 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. +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. +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. From 9db38f5ca23773c2005561116b375dbea4279b3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Staltz?= Date: Mon, 5 Jun 2017 20:37:23 +0300 Subject: [PATCH 09/39] Support npm5 npm5 installs modules and changes their `package.json`::`version` field to be something like ``` "version": "https://registry.npmjs.org/rn-nodeify/-/rn-nodeify-7.0.1.tgz", ``` instead of ``` "version": "7.0.1", ``` This commit adds support for that first type of string but also supports npm4. --- cmd.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd.js b/cmd.js index 3e98ed4..237c3eb 100755 --- a/cmd.js +++ b/cmd.js @@ -105,7 +105,8 @@ function installShims (modulesToShim, done) { install = false } } else { - var existingVer = pkgJson.version + var existingVerNpm5 = (/\-([^\-]+)\.tgz/.exec(pkgJson.version) || [null, null])[1] + var existingVer = existingVerNpm5 || pkgJson.version if (semver.satisfies(existingVer, allShims[name])) { install = false } From 1898500e3a3bbfb91489a0a8e1c3750a25adf287 Mon Sep 17 00:00:00 2001 From: "Yair Manor (Kuszpet)" Date: Wed, 15 Mar 2017 21:34:58 +0200 Subject: [PATCH 10/39] pkg-hacks.js now converts Windows path separators to POSIX, so that hacks run on Windows --- pkg-hacks.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg-hacks.js b/pkg-hacks.js index 7e0854a..e7c1396 100644 --- a/pkg-hacks.js +++ b/pkg-hacks.js @@ -34,6 +34,8 @@ module.exports = function hackFiles (hacks) { 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) From 5d9ed9c6e1376310b7f3b2615f1d6870787aecf0 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Thu, 29 Jun 2017 09:42:32 -0400 Subject: [PATCH 11/39] Update readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 1f61477..407ee57 100644 --- a/readme.md +++ b/readme.md @@ -16,7 +16,7 @@ installs shims for core node modules, see ['./shims.js'](./shims.js) for the cur `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 [https://github.com/philikon/ReactNativify](ReactNativify). I haven't tested it myself, but I think [philikon](https://github.com/philikon) will be happy to help. +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 From 71aabdde42aabe2d6e4c01d2d2908d47626623fa Mon Sep 17 00:00:00 2001 From: Laurence Ion Date: Wed, 15 Mar 2017 13:47:27 +0200 Subject: [PATCH 12/39] fix ./pkg-hacks.js link (quotes) --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 407ee57..698a2d7 100644 --- a/readme.md +++ b/readme.md @@ -14,7 +14,7 @@ However, with bigger projects that don't reimplement every wheel from scratch, s 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. +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. From 00ea356ddbc61b15e2c2a5a37ccf2af3fa7e6b18 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Sat, 27 May 2017 20:09:48 -0400 Subject: [PATCH 13/39] another levelup hack --- pkg-hacks.js | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg-hacks.js b/pkg-hacks.js index e7c1396..dfdb7a4 100644 --- a/pkg-hacks.js +++ b/pkg-hacks.js @@ -232,6 +232,7 @@ var hackers = [ 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' From 4f9cc9b35eb57332ac2e2cc9cef231a36304c857 Mon Sep 17 00:00:00 2001 From: Gabriel Cebrian Date: Wed, 28 Sep 2016 12:09:54 +0100 Subject: [PATCH 14/39] Hack versions --- pkg-hacks.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/pkg-hacks.js b/pkg-hacks.js index dfdb7a4..15193b2 100644 --- a/pkg-hacks.js +++ b/pkg-hacks.js @@ -62,8 +62,6 @@ module.exports = function hackFiles (hacks) { }) } - - // loadDeps(hackFiles) var hackers = [ @@ -515,7 +513,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) { From c426753a499cd2c4171f06b7d259f93166505717 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Sat, 27 May 2017 20:11:48 -0400 Subject: [PATCH 15/39] prevent uglify from mangling things (latest packager) --- pkg-hacks.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg-hacks.js b/pkg-hacks.js index 15193b2..1c444e5 100644 --- a/pkg-hacks.js +++ b/pkg-hacks.js @@ -135,8 +135,8 @@ var hackers = [ { name: 'rn-bundler', regex: [ - /react\-packager\/src\/bundler\/bundle\.js/i, - /react\-packager\/src\/JSTransformer\/worker\/minify\.js/i, + /react\-(?:native\/)?packager\/src\/bundler\/bundle\.js/i, + /react\-(?:native\/)?packager\/src\/JSTransformer\/worker\/minify\.js/i, ], hack: function (file, contents) { if (contents.indexOf('mangle:false') !== -1) return From 97012a814e7e4413421c8d24fed9cda8c196fbb2 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Tue, 6 Jun 2017 16:56:45 -0400 Subject: [PATCH 16/39] exclude tls, as there is no shim for it --- browser.json | 3 ++- cmd.js | 2 +- coreList.js | 4 +++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/browser.json b/browser.json index 751b7ca..f7e2003 100644 --- a/browser.json +++ b/browser.json @@ -21,5 +21,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 237c3eb..995bf47 100755 --- a/cmd.js +++ b/cmd.js @@ -231,7 +231,7 @@ function fixPackageJSON (modules, file, overwrite) { } modules.forEach(function (p) { - if (depBrowser[p] === false) { + if (depBrowser[p] === false && browser[p] !== false) { console.log('removing browser exclude', file, p) delete depBrowser[p] } diff --git a/coreList.js b/coreList.js index 71f2d1a..0fd94bc 100644 --- a/coreList.js +++ b/coreList.js @@ -26,5 +26,7 @@ module.exports = [ "url", "util", "net", - "vm" + "vm", + "q", + "tls" ] From 56e8fde384afa3068471801a6d6e62b3fadd601a Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Thu, 20 Jul 2017 11:21:44 -0400 Subject: [PATCH 17/39] fix inconsistent main mappings (e.g. in mqtt and websocket-stream) --- cmd.js | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/cmd.js b/cmd.js index 995bf47..31ab412 100755 --- a/cmd.js +++ b/cmd.js @@ -113,7 +113,7 @@ function installShims (modulesToShim, done) { } if (!install) { - console.log('not reinstalling ' + name) + log('not reinstalling ' + name) shimPkgNames.splice(shimPkgNames.indexOf(name), 1) } @@ -130,11 +130,11 @@ function installShims (modulesToShim, done) { var installLine = 'npm install --save ' shimPkgNames.forEach(function (name) { if (allShims[name].indexOf('/') === -1) { - console.log('installing from npm', name) + log('installing from npm', name) installLine += name + '@' + allShims[name] } else { // github url - console.log('installing from github', name) + log('installing from github', name) installLine += allShims[name].match(/([^\/]+\/[^\/]+)$/)[1] } @@ -154,7 +154,7 @@ function installShims (modulesToShim, done) { finish() }) - console.log('installing:', installLine) + log('installing:', installLine) function finish () { copyShim(done) } @@ -203,7 +203,7 @@ function fixPackageJSON (modules, file, overwrite) { } // if (shims[pkgJson.name]) { - // console.log('skipping', pkgJson.name) + // log('skipping', pkgJson.name) // return // } @@ -223,7 +223,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] } @@ -232,11 +232,22 @@ function fixPackageJSON (modules, file, overwrite) { modules.forEach(function (p) { if (depBrowser[p] === false && browser[p] !== false) { - console.log('removing browser exclude', file, p) + 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 (!deepEqual(orgBrowser, depBrowser)) { pkgJson.browser = pkgJson['react-native'] = depBrowser delete pkgJson.browserify @@ -250,7 +261,7 @@ function rethrow (err) { } function runHelp () { - console.log(function () { + log(function () { /* Usage: rn-nodeify --install dns,stream,http,https @@ -267,3 +278,7 @@ function runHelp () { }.toString().split(/\n/).slice(2, -2).join('\n')) process.exit(0) } + +function log () { + console.log.apply(console, arguments) +} From 6d4d7db996669061ad31fdc00aa9b748b6b84498 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Tue, 22 Aug 2017 10:53:52 -0400 Subject: [PATCH 18/39] hack for constants-browserify --- cmd.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cmd.js b/cmd.js index 31ab412..35831cc 100755 --- a/cmd.js +++ b/cmd.js @@ -248,6 +248,11 @@ function fixPackageJSON (modules, file, overwrite) { } } + 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 From 04dbc55a48f968f7c5e2fbfb48551dcf98a98fc5 Mon Sep 17 00:00:00 2001 From: Joe Date: Sat, 19 Aug 2017 23:49:15 +0200 Subject: [PATCH 19/39] bumps constants-broswerify to ~1.0.0 --- shims-browserify.js | 2 +- shims.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/shims-browserify.js b/shims-browserify.js index 7c201e0..49a2c3d 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", diff --git a/shims.js b/shims.js index b295c4d..3431084 100644 --- a/shims.js +++ b/shims.js @@ -4,7 +4,7 @@ module.exports = { "buffer": "^4.9.1", "inherits": "^2.0.1", "console-browserify": "^1.1.0", - "constants-browserify": "~0.0.1", + "constants-browserify": "~1.0.0", "react-native-crypto": "^2.0.0", "react-native-randombytes": "^2.1.0", "dns.js": "^1.0.1", From 78652de5d310287e6d07f5a06bd9e3e5835ee1eb Mon Sep 17 00:00:00 2001 From: Joe Date: Sun, 20 Aug 2017 00:03:02 +0200 Subject: [PATCH 20/39] bumps constants-broswerify to ^1.0.0 --- shims-browserify.js | 2 +- shims.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/shims-browserify.js b/shims-browserify.js index 49a2c3d..c33de23 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": "~1.0.0", + "constants-browserify": "^1.0.0", "crypto-browserify": "~3.2.6", "dns.js": "^1.0.1", "domain-browser": "~1.1.0", diff --git a/shims.js b/shims.js index 3431084..71d2d92 100644 --- a/shims.js +++ b/shims.js @@ -4,7 +4,7 @@ module.exports = { "buffer": "^4.9.1", "inherits": "^2.0.1", "console-browserify": "^1.1.0", - "constants-browserify": "~1.0.0", + "constants-browserify": "^1.0.0", "react-native-crypto": "^2.0.0", "react-native-randombytes": "^2.1.0", "dns.js": "^1.0.1", From 66593df2547f218a836847aba376dfbd3133d75e Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Tue, 5 Sep 2017 10:02:02 -0400 Subject: [PATCH 21/39] rm 'q', fix no-param --install --- cmd.js | 16 +++++++++++----- coreList.js | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/cmd.js b/cmd.js index 35831cc..4ab30e8 100755 --- a/cmd.js +++ b/cmd.js @@ -129,22 +129,29 @@ function installShims (modulesToShim, done) { var installLine = 'npm install --save ' shimPkgNames.forEach(function (name) { - if (allShims[name].indexOf('/') === -1) { + const version = allShims[name] + if (!version) return + if (version.indexOf('/') === -1) { log('installing from npm', name) - installLine += name + '@' + allShims[name] + installLine += name + '@' + version } else { // github url log('installing from github', name) - installLine += allShims[name].match(/([^\/]+\/[^\/]+)$/)[1] + 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() === 'npm install --save') { + return finish() + } + + log('installing:', installLine) proc.execSync(installLine, { cwd: process.cwd(), env: process.env, @@ -154,7 +161,6 @@ function installShims (modulesToShim, done) { finish() }) - log('installing:', installLine) function finish () { copyShim(done) } diff --git a/coreList.js b/coreList.js index 0fd94bc..ebcf4c5 100644 --- a/coreList.js +++ b/coreList.js @@ -27,6 +27,6 @@ module.exports = [ "util", "net", "vm", - "q", + // note: tls doesn't have a shim "tls" ] From 1a2da7eb9e0a47ccebc816f07848d091524c91c3 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Tue, 5 Sep 2017 10:24:11 -0400 Subject: [PATCH 22/39] add --overwrite flag --- cmd.js | 56 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/cmd.js b/cmd.js index 4ab30e8..6ab649f 100755 --- a/cmd.js +++ b/cmd.js @@ -18,12 +18,20 @@ 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' + }, + boolean: [ + 'install', + 'hack', + 'overwrite' + ] }) +var BASE_INSTALL_LINE = 'npm install --save' + if (argv.help) { runHelp() process.exit(0) @@ -62,7 +70,10 @@ function run () { } if (argv.install) { - installShims(toShim, function (err) { + installShims({ + modules: toShim, + ovewrite: argv.overwrite + }, function (err) { if (err) throw err runHacks() @@ -83,14 +94,31 @@ 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() + } + parallel(shimPkgNames.map(function (name) { var modPath = path.resolve('./node_modules/' + name) return function (cb) { @@ -127,9 +155,9 @@ function installShims (modulesToShim, done) { return finish() } - var installLine = 'npm install --save ' + var installLine = BASE_INSTALL_LINE + ' ' shimPkgNames.forEach(function (name) { - const version = allShims[name] + let version = allShims[name] if (!version) return if (version.indexOf('/') === -1) { log('installing from npm', name) @@ -147,7 +175,7 @@ function installShims (modulesToShim, done) { fs.writeFile(pkgPath, JSON.stringify(pkg, null, 2), function (err) { if (err) throw err - if (installLine.trim() === 'npm install --save') { + if (installLine.trim() === BASE_INSTALL_LINE) { return finish() } @@ -160,11 +188,11 @@ function installShims (modulesToShim, done) { finish() }) - - function finish () { - copyShim(done) - } }) + + function finish () { + copyShim(done) + } } function copyShim (cb) { From b39690e439924e6060ffa5f4a8a646275bf205b0 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Tue, 5 Sep 2017 10:43:48 -0400 Subject: [PATCH 23/39] upgrade react-native-randombytes --- shims.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shims.js b/shims.js index 71d2d92..8f2cde3 100644 --- a/shims.js +++ b/shims.js @@ -6,7 +6,7 @@ module.exports = { "console-browserify": "^1.1.0", "constants-browserify": "^1.0.0", "react-native-crypto": "^2.0.0", - "react-native-randombytes": "^2.1.0", + "react-native-randombytes": "^3.0.0", "dns.js": "^1.0.1", "domain-browser": "^1.1.1", "events": "^1.0.0", From 6ee2f9a8b36b22199eb7fd7bbf487663506cb7f0 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Tue, 5 Sep 2017 10:49:44 -0400 Subject: [PATCH 24/39] improve logging --- cmd.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cmd.js b/cmd.js index 6ab649f..2fb4f32 100755 --- a/cmd.js +++ b/cmd.js @@ -197,7 +197,10 @@ function installShims ({ modules, overwrite }, 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) From 274f86b5a4bd714fa24421a260d359dfaaa34844 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Tue, 5 Sep 2017 10:53:57 -0400 Subject: [PATCH 25/39] improve readme --- readme.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/readme.md b/readme.md index 698a2d7..9f38321 100644 --- a/readme.md +++ b/readme.md @@ -104,3 +104,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 From 441e28b6161d51b52ccb8602d27ce92100f26ca8 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Tue, 5 Sep 2017 11:00:56 -0400 Subject: [PATCH 26/39] update react-native-udp for RN>0.40 --- shims.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shims.js b/shims.js index 8f2cde3..4ee8535 100644 --- a/shims.js +++ b/shims.js @@ -18,7 +18,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", From 7eb2614f628b461a187645b25f758f70a9237b1c Mon Sep 17 00:00:00 2001 From: mcarter00 Date: Wed, 22 Mar 2017 21:23:50 -0500 Subject: [PATCH 27/39] update the version of react-native-tcp to support react-native >= 0.40.0 --- shims-browserify.js | 2 +- shims.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/shims-browserify.js b/shims-browserify.js index c33de23..5952ae2 100644 --- a/shims-browserify.js +++ b/shims-browserify.js @@ -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 4ee8535..9f2b220 100644 --- a/shims.js +++ b/shims.js @@ -27,6 +27,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" } From a659f24d780471adde52f903a63520b811a2c903 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Tue, 5 Sep 2017 11:36:48 -0400 Subject: [PATCH 28/39] fix arg parsing --- cmd.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/cmd.js b/cmd.js index 2fb4f32..14d1f38 100755 --- a/cmd.js +++ b/cmd.js @@ -22,12 +22,7 @@ var argv = minimist(process.argv.slice(2), { i: 'install', e: 'hack', o: 'overwrite' - }, - boolean: [ - 'install', - 'hack', - 'overwrite' - ] + } }) var BASE_INSTALL_LINE = 'npm install --save' From c70957cefcf1cc06cff6fc88453c6edbacc1b288 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Tue, 5 Sep 2017 12:00:49 -0400 Subject: [PATCH 29/39] fix typo --- cmd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd.js b/cmd.js index 14d1f38..aa35dac 100755 --- a/cmd.js +++ b/cmd.js @@ -67,7 +67,7 @@ function run () { if (argv.install) { installShims({ modules: toShim, - ovewrite: argv.overwrite + overwrite: argv.overwrite }, function (err) { if (err) throw err From eb33e96795bee0f17ff5c2f8cc370f3d848f05de Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Wed, 13 Sep 2017 12:04:18 -0400 Subject: [PATCH 30/39] 8.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c5b8f82..dac4bed 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "type": "git", "url": "https://github.com/mvayngrib/rn-nodeify.git" }, - "version": "7.0.1", + "version": "8.0.0", "preferGlobal": true, "bin": "./cmd.js", "license": "MIT", From 91913e1f98611a9a648d57cd216cd42799f860b8 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Fri, 15 Sep 2017 09:57:36 -0400 Subject: [PATCH 31/39] add precommit lint --- .eslintrc.js | 283 +++++++++++++++++++++++++++++++++++++++++++++++++++ cmd.js | 2 +- package.json | 7 ++ pkg-hacks.js | 19 ++-- 4 files changed, 300 insertions(+), 11 deletions(-) create mode 100644 .eslintrc.js 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/cmd.js b/cmd.js index aa35dac..80f1f9f 100755 --- a/cmd.js +++ b/cmd.js @@ -116,7 +116,7 @@ function installShims ({ modules, overwrite }, done) { 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() diff --git a/package.json b/package.json index dac4bed..484e810 100644 --- a/package.json +++ b/package.json @@ -17,5 +17,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 1c444e5..bbbdd3c 100644 --- a/pkg-hacks.js +++ b/pkg-hacks.js @@ -26,11 +26,11 @@ 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 || hackers.map(h => h.name) finder.on('file', function (file) { - if (!/\.(js|json)$/.test(file) - || /\/tests?\//.test(file)) { + if (!/\.(js|json)$/.test(file) || + /\/tests?\//.test(file)) { return } @@ -163,7 +163,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') @@ -177,7 +177,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 } }, { @@ -224,7 +224,7 @@ var hackers = [ regex: [ /levelup\/lib\/util\.js$/ ], - hack: function(file, contents) { + hack: function (file, contents) { if (isInReactNative(file)) return var fixed = contents @@ -257,7 +257,7 @@ var hackers = [ regex: [ /level-jobs\/package\.json$/ ], - hack: function(file, contents) { + hack: function (file, contents) { if (isInReactNative(file)) return var pkg @@ -425,7 +425,7 @@ var hackers = [ regex: [ /bytewise\/bytewise\.js$/ ], - hack: function(file, contents) { + hack: function (file, contents) { if (isInReactNative(file)) return var fixed = contents @@ -532,8 +532,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 = {} } } From 55575c0e9cf0abb06cf3e750c0acf5d49f8ea5de Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 19 Sep 2017 20:19:27 -0700 Subject: [PATCH 32/39] Basic yarn support (#51) * Add yarn argument to control install command * Use lockfile to resolve installed pkg version when doing semver check --- cmd.js | 78 ++++++++++++++++++++++++++++++++++++++++++++-------- package.json | 1 + 2 files changed, 67 insertions(+), 12 deletions(-) diff --git a/cmd.js b/cmd.js index 80f1f9f..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') @@ -21,11 +22,12 @@ var argv = minimist(process.argv.slice(2), { h: 'help', i: 'install', e: 'hack', - o: 'overwrite' + o: 'overwrite', + y: 'yarn' } }) -var BASE_INSTALL_LINE = 'npm install --save' +var BASE_INSTALL_LINE = argv.yarn ? 'yarn add' : 'npm install --save' if (argv.help) { runHelp() @@ -114,6 +116,25 @@ function installShims ({ modules, overwrite }, done) { 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) { @@ -121,17 +142,44 @@ function installShims ({ modules, overwrite }, done) { 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 existingVerNpm5 = (/\-([^\-]+)\.tgz/.exec(pkgJson.version) || [null, null])[1] - var existingVer = existingVerNpm5 || 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 + } } } @@ -155,7 +203,11 @@ function installShims ({ modules, overwrite }, done) { let version = allShims[name] if (!version) return if (version.indexOf('/') === -1) { - log('installing from npm', name) + if (argv.yarn) { + log('installing from yarn', name) + } else { + log('installing from npm', name) + } installLine += name + '@' + version } else { // github url @@ -309,6 +361,8 @@ 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 */ diff --git a/package.json b/package.json index 484e810..e02ff90 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "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", From 14999e3e16019998cd172fe789c3c40fd4cf8dc8 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Tue, 19 Sep 2017 23:20:05 -0400 Subject: [PATCH 33/39] 8.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e02ff90..18f692e 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "type": "git", "url": "https://github.com/mvayngrib/rn-nodeify.git" }, - "version": "8.0.0", + "version": "8.1.0", "preferGlobal": true, "bin": "./cmd.js", "license": "MIT", From 02225a2d23791f46e3394200d219f571b688daa2 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 20 Sep 2017 13:06:08 -0700 Subject: [PATCH 34/39] Cleanup crypto code in shim.js (#54) - require newer react-native-crypto 2.1.0 - Add extra reminder in README to run 'react-native link' - bump version (minor) --- package.json | 2 +- readme.md | 10 ++++++---- shim.js | 35 ----------------------------------- shims.js | 2 +- 4 files changed, 8 insertions(+), 41 deletions(-) diff --git a/package.json b/package.json index 18f692e..b5d9f21 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "type": "git", "url": "https://github.com/mvayngrib/rn-nodeify.git" }, - "version": "8.1.0", + "version": "8.2.0", "preferGlobal": true, "bin": "./cmd.js", "license": "MIT", diff --git a/readme.md b/readme.md index 9f38321..b17240e 100644 --- a/readme.md +++ b/readme.md @@ -6,14 +6,14 @@ Run after npm install and you can use node core modules and npm modules that use 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. +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` +`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` +`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. @@ -27,7 +27,7 @@ rn-nodeify ## Options ``` ---install install node core shims (default: install all), fix the "browser" +--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 ``` @@ -64,6 +64,8 @@ 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. + ### 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 diff --git a/shim.js b/shim.js index 6e7f184..65d986e 100644 --- a/shim.js +++ b/shim.js @@ -20,38 +20,3 @@ process.env['NODE_ENV'] = isDev ? 'development' : 'production' if (typeof localStorage !== 'undefined') { localStorage.debug = isDev ? '*' : '' } - -if (require('./package.json').dependencies['react-native-crypto']) { - // important that this comes before require('crypto') - const algos = require('browserify-sign/algos') - if (!algos.sha256) { - algos.sha256 = { - "sign": "ecdsa", - "hash": "sha256", - "id": new Buffer("") - } - } - - let crypto - if (typeof window === 'object') { - if (!window.crypto) window.crypto = {} - crypto = window.crypto - } else { - crypto = require('crypto') - } - - if (!crypto.getRandomValues) { - crypto.getRandomValues = getRandomValues - } - - let randomBytes - - function getRandomValues (arr) { - if (!randomBytes) randomBytes = require('react-native-randombytes').randomBytes - - const bytes = randomBytes(arr.length) - for (var i = 0; i < bytes.length; i++) { - arr[i] = bytes[i] - } - } -} diff --git a/shims.js b/shims.js index 9f2b220..0b75384 100644 --- a/shims.js +++ b/shims.js @@ -5,7 +5,7 @@ module.exports = { "inherits": "^2.0.1", "console-browserify": "^1.1.0", "constants-browserify": "^1.0.0", - "react-native-crypto": "^2.0.0", + "react-native-crypto": "^2.1.0", "react-native-randombytes": "^3.0.0", "dns.js": "^1.0.1", "domain-browser": "^1.1.1", From 77bb54a2d0edbdfc4accb15f2ef154a9170cefa3 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Tue, 2 Feb 2016 19:25:37 -0500 Subject: [PATCH 35/39] opinionate: replace q with bluebird --- browser.json | 1 + shims.js | 1 + 2 files changed, 2 insertions(+) diff --git a/browser.json b/browser.json index f7e2003..36cf75e 100644 --- a/browser.json +++ b/browser.json @@ -1,4 +1,5 @@ { + "q": "bluebird-q", "zlib": "browserify-zlib", "console": "console-browserify", "constants": "constants-browserify", diff --git a/shims.js b/shims.js index 0b75384..3c9c99b 100644 --- a/shims.js +++ b/shims.js @@ -1,4 +1,5 @@ module.exports = { + "bluebird-q": "latest", "assert": "^1.1.1", "browserify-zlib": "~0.1.4", "buffer": "^4.9.1", From 05ab035aef53ba2fb6e3420a8bb697a068d57a4a Mon Sep 17 00:00:00 2001 From: Gabriel Cebrian Date: Mon, 4 Jul 2016 23:57:55 -0700 Subject: [PATCH 36/39] Load local hacks prototype --- pkg-hacks.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/pkg-hacks.js b/pkg-hacks.js index bbbdd3c..af72d8c 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,7 +29,7 @@ var path = require('path') module.exports = function hackFiles (hacks) { var finder = find('./node_modules') - hacks = hacks || hackers.map(h => h.name) + hacks = hacks || getHackers(hackers).map(function (h) { return h.name }) finder.on('file', function (file) { if (!/\.(js|json)$/.test(file) || @@ -62,6 +65,26 @@ 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) var hackers = [ From 76fda047035063af77d7665c770916a48e9db5bc Mon Sep 17 00:00:00 2001 From: Gabriel Cebrian Date: Tue, 4 Oct 2016 23:58:17 -0700 Subject: [PATCH 37/39] Added docs --- readme.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/readme.md b/readme.md index b17240e..eb61253 100644 --- a/readme.md +++ b/readme.md @@ -66,6 +66,10 @@ 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 From d0be8ec6ac6461146579a48e7c909ef335814d8f Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Mon, 19 Mar 2018 23:02:20 -0400 Subject: [PATCH 38/39] rm mangling in latest rn --- pkg-hacks.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg-hacks.js b/pkg-hacks.js index af72d8c..d2285ac 100644 --- a/pkg-hacks.js +++ b/pkg-hacks.js @@ -160,11 +160,15 @@ var hackers = [ regex: [ /react\-(?:native\/)?packager\/src\/bundler\/bundle\.js/i, /react\-(?:native\/)?packager\/src\/JSTransformer\/worker\/minify\.js/i, + /metro\/src\/JSTransformer\/worker\/minify\.js/i, ], hack: function (file, contents) { if (contents.indexOf('mangle:false') !== -1) return - var fixed = contents.replace(/(\s+)(fromString: true,)/, '$1$2$1mangle:false,') + var fixed = contents + .replace('mangle: { toplevel: true }', 'mangle:false') + .replace(/(\s+)(fromString: true,)/, '$1$2$1mangle:false,') + return contents === fixed ? null : fixed } }, From f223a6a51a637bd2f6d30abd3526c6f214ada2db Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Fri, 3 Aug 2018 09:55:31 -0400 Subject: [PATCH 39/39] update mangling opts for rn 0.55 --- pkg-hacks.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg-hacks.js b/pkg-hacks.js index d2285ac..f5d0991 100644 --- a/pkg-hacks.js +++ b/pkg-hacks.js @@ -158,9 +158,10 @@ var hackers = [ { name: 'rn-bundler', regex: [ - /react\-(?:native\/)?packager\/src\/bundler\/bundle\.js/i, - /react\-(?:native\/)?packager\/src\/JSTransformer\/worker\/minify\.js/i, - /metro\/src\/JSTransformer\/worker\/minify\.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) { if (contents.indexOf('mangle:false') !== -1) return