forked from coder/nbin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.patch
138 lines (128 loc) · 4.98 KB
/
node.patch
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
diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js
index 885546c5d6..f21e7993f1 100644
--- a/lib/internal/bootstrap/node.js
+++ b/lib/internal/bootstrap/node.js
@@ -215,11 +215,16 @@
// are running from a script and running the REPL - but there are a few
// others like the debugger or running --eval arguments. Here we decide
// which mode we run in.
+
+ if (NativeModule.exists('_third_party_main')) {
+ NativeModule.require('_third_party_main');
+ }
+
if (internalBinding('worker').getEnvMessagePort() !== undefined) {
// This means we are in a Worker context, and any script execution
// will be directed by the worker module.
NativeModule.require('internal/worker').setupChild(evalScript);
- } else if (NativeModule.exists('_third_party_main')) {
+ } else if (NativeModule.exists('_third_party_main_invalid')) {
// To allow people to extend Node in different ways, this hook allows
// one to drop a file lib/_third_party_main.js into the build
// directory which will be executed instead of Node's normal loading.
diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js
index fb3770b729..f4bad8d6de 100644
--- a/lib/internal/modules/cjs/loader.js
+++ b/lib/internal/modules/cjs/loader.js
@@ -44,6 +44,9 @@ const { getOptionValue } = require('internal/options');
const preserveSymlinks = getOptionValue('--preserve-symlinks');
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
const experimentalModules = getOptionValue('--experimental-modules');
+const nbin = require('nbin');
+const os = require('os');
+const zlib = require('zlib');
const {
ERR_INVALID_ARG_TYPE,
@@ -87,7 +90,8 @@ function stat(filename) {
const result = cache.get(filename);
if (result !== undefined) return result;
}
- const result = internalModuleStat(filename);
+ const s = nbin.statSync(filename);
+ const result = s.isDirectory ? 1 : s.isFile ? 0 : internalModuleStat(filename);
if (cache !== null) cache.set(filename, result);
return result;
}
@@ -154,8 +158,13 @@ function readPackage(requestPath) {
if (entry)
return entry;
+ let json;
const jsonPath = path.resolve(requestPath, 'package.json');
- const json = internalModuleReadJSON(path.toNamespacedPath(jsonPath));
+ if (nbin.existsSync(jsonPath)) {
+ json = nbin.readFileSync(jsonPath, 'utf8');
+ } else {
+ json = internalModuleReadJSON(path.toNamespacedPath(jsonPath));
+ }
if (json === undefined) {
return false;
@@ -199,6 +208,9 @@ function tryFile(requestPath, isMain) {
}
function toRealPath(requestPath) {
+ if (nbin.existsSync(requestPath)) {
+ return requestPath;
+ }
return fs.realpathSync(requestPath, {
[internalFS.realpathCacheKey]: realpathCache
});
@@ -696,14 +708,29 @@ Module.prototype._compile = function(content, filename) {
// Native extension for .js
Module._extensions['.js'] = function(module, filename) {
- var content = fs.readFileSync(filename, 'utf8');
+ let content;
+ if (nbin.existsSync(filename)) {
+ content = nbin.readFileSync(filename, 'utf8');
+ } else {
+ content = fs.readFileSync(filename, 'utf8');
+ }
module._compile(stripBOM(content), filename);
};
+// Native extension for .gz
+Module._extensions['.gz'] = function(module, filename) {
+ const content = zlib.gunzipSync(nbin.readFileSync(filename)).toString("utf8");
+ module._compile(stripBOM(content), filename);
+};
// Native extension for .json
Module._extensions['.json'] = function(module, filename) {
- var content = fs.readFileSync(filename, 'utf8');
+ let content;
+ if (nbin.existsSync(filename)) {
+ content = nbin.readFileSync(filename, 'utf8');
+ } else {
+ content = fs.readFileSync(filename, 'utf8');
+ }
try {
module.exports = JSON.parse(stripBOM(content));
} catch (err) {
@@ -715,7 +742,16 @@ Module._extensions['.json'] = function(module, filename) {
// Native extension for .node
Module._extensions['.node'] = function(module, filename) {
- return process.dlopen(module, path.toNamespacedPath(filename));
+ let isInternal = false;
+ if (nbin.existsSync(filename)) {
+ const tmpFile = path.join(os.tmpdir(), `.nbin${nbin.id}-${path.basename(filename)}`);
+ if (!fs.existsSync(tmpFile)) {
+ fs.writeFileSync(tmpFile, nbin.readFileSync(filename));
+ }
+ filename = tmpFile;
+ isInternal = true;
+ }
+ return process.dlopen(module, isInternal ? filename : path.toNamespacedPath(filename));
};
if (experimentalModules) {
diff --git a/src/node.cc b/src/node.cc
index 9fb5ab3b8e..d2c9ab3bc3 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -2439,6 +2439,12 @@ void ProcessArgv(std::vector<std::string>* args,
std::vector<std::string> v8_args;
std::vector<std::string> errors{};
+ if (!getenv("NBIN_BYPASS") && !is_env) {
+ // *exec_argc = 0;
+
+ return;
+ }
+
{
// TODO(addaleax): The mutex here should ideally be held during the
// entire function, but that doesn't play well with the exit() calls below.