From cfb6e847a0f499005b2821e79436aa8484da2ba2 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Tue, 7 May 2019 06:44:32 -0700 Subject: [PATCH] refactor: allow embedder overriding of internal FS calls (#17906) (#18183) --- DEPS | 2 +- lib/common/asar.js | 11 +++++++---- spec/fixtures/api/electron-main-module/app.asar | Bin 0 -> 378 bytes .../api/electron-main-module/app/index.js | 8 ++++++++ .../app/node_modules/some-module/main2.js | 1 + .../app/node_modules/some-module/package.json | 4 ++++ spec/node-spec.js | 5 +++++ 7 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 spec/fixtures/api/electron-main-module/app.asar create mode 100644 spec/fixtures/api/electron-main-module/app/index.js create mode 100644 spec/fixtures/api/electron-main-module/app/node_modules/some-module/main2.js create mode 100644 spec/fixtures/api/electron-main-module/app/node_modules/some-module/package.json diff --git a/DEPS b/DEPS index 5958d694c318..14c96cda2cca 100644 --- a/DEPS +++ b/DEPS @@ -12,7 +12,7 @@ vars = { 'chromium_version': '84c40395c741fa24ccbd9fc2c5828e2e97472952', 'node_version': - '696d8fb66d6f65fc82869d390e0d2078970b1eb4', + 'a86a4a160dc520c61a602c949a32a1bc4c0fc633', 'boto_version': 'f7574aa6cc2c819430c1f05e9a1a1a666ef8169b', 'pyyaml_version': '3.12', diff --git a/lib/common/asar.js b/lib/common/asar.js index b2f21964fdea..f0977753d070 100644 --- a/lib/common/asar.js +++ b/lib/common/asar.js @@ -13,6 +13,9 @@ process.type !== 'renderer' const isAsarDisabled = () => process.noAsar || envNoAsar + const internalBinding = process.internalBinding + delete process.internalBinding + /** * @param {!Function} functionToCall * @param {!Array|undefined} args @@ -669,8 +672,8 @@ return files } - const { internalModuleReadJSON } = process.binding('fs') - process.binding('fs').internalModuleReadJSON = pathArgument => { + const { internalModuleReadJSON } = internalBinding('fs') + internalBinding('fs').internalModuleReadJSON = pathArgument => { const { isAsar, asarPath, filePath } = splitPath(pathArgument) if (!isAsar) return internalModuleReadJSON(pathArgument) @@ -694,8 +697,8 @@ return buffer.toString('utf8') } - const { internalModuleStat } = process.binding('fs') - process.binding('fs').internalModuleStat = pathArgument => { + const { internalModuleStat } = internalBinding('fs') + internalBinding('fs').internalModuleStat = pathArgument => { const { isAsar, asarPath, filePath } = splitPath(pathArgument) if (!isAsar) return internalModuleStat(pathArgument) diff --git a/spec/fixtures/api/electron-main-module/app.asar b/spec/fixtures/api/electron-main-module/app.asar new file mode 100644 index 0000000000000000000000000000000000000000..19b4bdb9a8950fa186d67ad32730b88955618810 GIT binary patch literal 378 zcmY*VOKQU~5cQ%fokGkeHn{#l2>1}aLJ{&fRvT+lNrAM+$Lg+&9<3w07)S6xym=pu zo{}W_W&AL{858v8f*#rljP3AVye)Tcz7X2FI_5xq?=e6N70fwU>hN`>?)Gom+@3~! zUI#I3<40v|^N`v!yHKu(u~((v)frhwHrm(zW4%{4Cz#j6?+)%$PC`nDA2;JLJ?#1q znV6+kK@T!T=dwkp$$GkA!34R5X7|;RM(+!}n~>IQ6XJoYGEtWDg~DL$Sq73F$pIaT YSPfQ<{7Dja)sV+Q+&K=4a@$_ae@5+f7XSbN literal 0 HcmV?d00001 diff --git a/spec/fixtures/api/electron-main-module/app/index.js b/spec/fixtures/api/electron-main-module/app/index.js new file mode 100644 index 000000000000..c56845bca42d --- /dev/null +++ b/spec/fixtures/api/electron-main-module/app/index.js @@ -0,0 +1,8 @@ +try { + require('some-module') +} catch (err) { + console.error(err) + process.exit(1) +} + +process.exit(0) diff --git a/spec/fixtures/api/electron-main-module/app/node_modules/some-module/main2.js b/spec/fixtures/api/electron-main-module/app/node_modules/some-module/main2.js new file mode 100644 index 000000000000..774b4e2041eb --- /dev/null +++ b/spec/fixtures/api/electron-main-module/app/node_modules/some-module/main2.js @@ -0,0 +1 @@ +// Nothing to do here diff --git a/spec/fixtures/api/electron-main-module/app/node_modules/some-module/package.json b/spec/fixtures/api/electron-main-module/app/node_modules/some-module/package.json new file mode 100644 index 000000000000..cf8d1202f373 --- /dev/null +++ b/spec/fixtures/api/electron-main-module/app/node_modules/some-module/package.json @@ -0,0 +1,4 @@ +{ + "name": "some-module", + "main": "./main2.js" +} \ No newline at end of file diff --git a/spec/node-spec.js b/spec/node-spec.js index 623ecacd2135..aa7b2dace92d 100644 --- a/spec/node-spec.js +++ b/spec/node-spec.js @@ -578,4 +578,9 @@ describe('node feature', () => { .that.is.a('string') .and.matches(/^\d+\.\d+\.\d+\.\d+$/) }) + + it('can find a module using a package.json main field', () => { + const result = ChildProcess.spawnSync(remote.process.execPath, [path.resolve(fixtures, 'api', 'electron-main-module', 'app.asar')]) + expect(result.status).to.equal(0) + }) })