Skip to content

Commit bb5ad4a

Browse files
committed
Correctly initialize Node environment in worker
1 parent b467c39 commit bb5ad4a

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

filenames.gypi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
'lib/renderer/extensions/i18n.js',
7777
'lib/renderer/extensions/storage.js',
7878
'lib/renderer/extensions/web-navigation.js',
79+
'lib/worker/init.js',
7980
],
8081
'js2c_sources': [
8182
'lib/common/asar.js',

lib/renderer/init.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const Module = require('module')
66
const resolvePromise = Promise.resolve.bind(Promise)
77

88
// We modified the original process.argv to let node.js load the
9-
// atom-renderer.js, we need to restore it here.
9+
// init.js, we need to restore it here.
1010
process.argv.splice(1, 1)
1111

1212
// Clear search paths.

lib/worker/init.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict'
2+
3+
const path = require('path')
4+
const Module = require('module')
5+
6+
// We modified the original process.argv to let node.js load the
7+
// init.js, we need to restore it here.
8+
process.argv.splice(1, 1)
9+
10+
// Clear search paths.
11+
require('../common/reset-search-paths')
12+
13+
// Import common settings.
14+
require('../common/init')
15+
16+
// Expose public APIs.
17+
Module.globalPaths.push(path.join(__dirname, 'api', 'exports'))
18+
19+
// Export node bindings to global.
20+
global.require = require
21+
global.module = module
22+
23+
// Set the __filename to the path of html file if it is file: protocol.
24+
if (self.location.protocol === 'file:') {
25+
let pathname = process.platform === 'win32' && self.location.pathname[0] === '/' ? self.location.pathname.substr(1) : self.location.pathname
26+
global.__filename = path.normalize(decodeURIComponent(pathname))
27+
global.__dirname = path.dirname(global.__filename)
28+
29+
// Set module's filename so relative require can work as expected.
30+
module.filename = global.__filename
31+
32+
// Also search for module under the html file.
33+
module.paths = module.paths.concat(Module._nodeModulePaths(global.__dirname))
34+
} else {
35+
global.__filename = __filename
36+
global.__dirname = __dirname
37+
}

0 commit comments

Comments
 (0)