-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathpreload.js
More file actions
178 lines (155 loc) · 5.05 KB
/
preload.js
File metadata and controls
178 lines (155 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
This file helps make Electron behave more
like Node.js by shimming certain features.
*/
(function () {
var module = require('module')
var path = require('path')
var electron = require('electron');
var serialize = require('serializerr');
var browserGlobals = require('./browser-globals');
var requireHook = require('./require-hook');
var consoleHook = require('./console-hook');
var timerHook = require('./timer-hook');
var mockProcess = require('mock-stdin');
var browserResolve = require('browser-resolve');
var nodeResolve = require('resolve');
var remote = electron.remote;
var ipc = electron.ipcRenderer;
var _process = remote.process;
var cwd = _process.cwd();
// get an absolute path to our entry point
var globals = remote.getGlobal('__electronDevtoolGlobals');
var entry = globals.entry;
// setup setTimeout/etc to be more Node-like
if (globals.nodeTimers !== false) {
timerHook();
}
// setup process to be more like Node.js
hookProcess();
// if we should pipe DevTools console back to terminal
if (globals.console) {
consoleHook();
}
// setup NODE_PATH
var isWin = /^win/.test(process.platform);
var pathDelimiter = isWin ? ':' : ';';
var nodePaths = process.env.NODE_PATH
? process.env.NODE_PATH.split(pathDelimiter)
: []
nodePaths.forEach(function (nodePath) {
var fullNodePath = path.join(cwd, process.env.NODE_PATH)
module.globalPaths.push(fullNodePath)
})
// in DevTools console (i.e. REPL), these will be
// undefined to mimic Node REPL
delete global.__dirname;
delete global.__filename;
// delete some browser globals
if (globals.browserGlobals === false) {
browserGlobals.forEach(function (key) {
delete global[key];
});
}
// When there is an uncaught exception in the entry
// script, we may want to quit the devtool (i.e. for CI)
// or just print an error in DevTools console (i.e. for dev)
var shouldQuit = globals.quit;
if (shouldQuit) {
window.onerror = function (a, b, c, d, err) {
fatalError(err);
return true;
};
}
// Enable service workers
electron.webFrame.registerURLSchemeAsPrivileged('file');
electron.webFrame.registerURLSchemeAsSecure('file');
electron.webFrame.registerURLSchemeAsBypassingCSP('file');
// hook into the internal require for a few features:
// - better error reporting on syntax errors and missing modules
// - require.main acts like node.js CLI
// - injecting debugger with --break
// - undefining window/document/self/navigator
// - adding source maps so the files show up in DevTools Sources
requireHook({
entry: entry,
sourceMaps: globals.sourceMaps,
debugBreak: globals.debugBreak,
browserGlobals: globals.browserGlobals,
browserField: globals.browserField,
basedir: cwd
});
var firstRun = true;
// boot up entry application when DOM is ready
ipc.on('run-entry', function () {
if (Array.isArray(globals.requirePaths)) {
globals.requirePaths.forEach(doRequire);
}
if (entry) require(entry);
if (firstRun) {
// Tell the main thread to send along stdin
firstRun = false;
ipc.send('first-run');
}
});
function doRequire (file) {
var result;
var resolveOpt = { basedir: cwd };
if (globals.browserField) {
result = browserResolve.sync(file, resolveOpt);
} else {
result = nodeResolve.sync(file, resolveOpt);
}
require(result);
}
function fatalError (err) {
ipc.send('error', JSON.stringify(serialize(err)));
}
function hookProcess () {
// setup renderer process to look a bit more like node
process.chdir(cwd);
process.argv = _process.argv.slice();
process.exit = function (code) {
_process.exit(code || 0);
};
// Remove the Electron argument to make it more
// like Node.js argv handling. User can still
// grab remote.process.argv for original.
process.argv.shift();
// if -- is passed, all options after it will be the
// new user arguments
var stopIdx = process.argv.indexOf('--');
if (stopIdx >= 0) {
var start = process.argv.slice(0, entry ? 2 : 1);
process.argv = start.concat(process.argv.slice(stopIdx + 1));
}
// like node, ensure we use the full file path instead
// of the relative path that the user specified in CLI
if (entry) {
process.argv[1] = entry;
}
// renderer streams should have same isTTY as main
var isTTY = globals._processTTY;
process.stdout.isTTY = isTTY.stdout;
process.stderr.isTTY = isTTY.stderr;
if (/^win/.test(process.platform)) {
// clear the property first to avoid error from mockProcess
Object.defineProperty(process, 'stdin', {
value: undefined,
configurable: true,
writable: false
});
// now mock stdin
mockProcess.stdin();
} else {
process.stdin._read = function () {
this.push('');
};
}
process.stdin.isTTY = isTTY.stdin;
// send along any stdin
ipc.on('stdin', function (event, data) {
process.stdin.push(data);
});
}
})();