-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
293 lines (201 loc) · 6.82 KB
/
Copy pathindex.js
File metadata and controls
293 lines (201 loc) · 6.82 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
'use strict';
// see https://github.com/nodejs/node/blob/master/lib/module.js
// and https://github.com/nodejs/node/blob/master/lib/internal/module.js
const Module = module.constructor;
const boundCachedSym = Symbol();
const invalidateCallbacksSym = Symbol();
const validateCallbacksSym = Symbol();
const invalid = Symbol();
// see https://tc39.github.io/ecma262/#sec-toprimitive
function toPrimitive(value) {
var valueToPrimitive = value[Symbol.toPrimitive];
if ( valueToPrimitive !== undefined )
return valueToPrimitive;
return function(hint) {
if ( hint === 'number' )
return Number(value);
if ( hint === 'string' )
return String(value);
if ( typeof(value) !== 'object' && typeof(value) !== 'function' || value === null )
return value;
var val = value.valueOf();
if ( typeof(val) !== 'object' && typeof(val) !== 'function' || val === null )
return val;
return String(value);
}
}
function hasInstance(ctor) {
return function(instance) {
return instance instanceof ctor;
}
}
function bindSetProto(fct, value) {
function bound() {
return fct.apply(value, arguments);
}
Object.setPrototypeOf(bound, fct); // see test "exports property on function"
delete bound.name; // preserves the original function name
return bound;
}
Module.invalidate = function() {
for ( var filename in Module._cache )
Module._cache[filename].invalidate();
}
Module.invalidateByExports = function(exports) {
for ( var filename in Module._cache )
if ( Module._cache[filename].exports === exports )
Module._cache[filename].invalidate();
}
Module.prototype.invalidateByPath = function(path) {
Module._cache[Module._resolveFilename(path, this, this.parent === null)].invalidate();
}
Module.prototype.invalidate = function() {
if ( !this.invalidable )
return;
if ( invalidateCallbacksSym in this ) {
var validateCallbacks = this[validateCallbacksSym] || (this[validateCallbacksSym] = new Set);
this[invalidateCallbacksSym].forEach(callback => {
var validateCallback = callback(this._exports);
if ( typeof(validateCallback) === 'function' )
validateCallbacks.add(validateCallback);
});
this[invalidateCallbacksSym].clear();
}
this._exports = invalid;
}
Module.prototype.onInvalidate = function(callback) {
var invalidateCallbacks = this[invalidateCallbacksSym] || (this[invalidateCallbacksSym] = new Set);
return invalidateCallbacks.add(callback).delete.bind(invalidateCallbacks, callback);
}
function reload(mod) {
mod._exports = {}; // resets _exports
mod.loaded = false;
mod.load(mod.filename);
if ( validateCallbacksSym in mod ) {
mod[validateCallbacksSym].forEach(callback => callback(mod._exports) );
mod[validateCallbacksSym].clear();
}
}
function createProxy(mod) {
return new Proxy(function() {}, {
getPrototypeOf: function(target) {
mod._exports === invalid && reload(mod);
return Reflect.getPrototypeOf(mod._exports);
},
setPrototypeOf: function(target, prototype) {
mod._exports === invalid && reload(mod);
return Reflect.setPrototypeOf(mod._exports, prototype);
},
isExtensible: function(target) {
mod._exports === invalid && reload(mod);
return Reflect.isExtensible(mod._exports);
},
preventExtensions: function(target) {
mod._exports === invalid && reload(mod);
return Reflect.preventExtensions(mod._exports);
},
getOwnPropertyDescriptor: function(target, prop) {
mod._exports === invalid && reload(mod);
if ( prop === 'prototype' && typeof(mod._exports) !== 'function' ) // see ownKeys
return {};
return Reflect.getOwnPropertyDescriptor(mod._exports, prop);
},
defineProperty: function(target, property, descriptor) {
mod._exports === invalid && reload(mod);
return Reflect.defineProperty(mod._exports, property, descriptor);
},
has: function(target, prop) {
mod._exports === invalid && reload(mod);
return Reflect.has(mod._exports, prop);
},
get: function(target, property) {
mod._exports === invalid && reload(mod);
if ( property === Symbol.hasInstance )
return hasInstance(mod._exports);
if ( property === Symbol.toPrimitive )
return toPrimitive(mod._exports);
var val = Reflect.get(mod._exports, property);
if ( typeof(val) === 'function' && !('prototype' in val) ) { // native function has prototype === undefined
// needed for native function, like Promise.resolve().then, ...
return boundCachedSym in val ? val[boundCachedSym] : val[boundCachedSym] = bindSetProto(val, mod._exports);
}
return val;
},
set: function(target, property, value) {
mod._exports === invalid && reload(mod);
return Reflect.set(mod._exports, property, value);
},
deleteProperty: function(target, property) {
mod._exports === invalid && reload(mod);
return Reflect.deleteProperty(mod._exports, property);
},
ownKeys: function(target) {
mod._exports === invalid && reload(mod);
var ownKeys = Reflect.ownKeys(mod._exports);
// see https://tc39.github.io/ecma262/#sec-invariants-of-the-essential-internal-methods
if ( typeof mod._exports !== 'function' )
ownKeys.push('prototype');
return ownKeys;
},
apply: function(target, thisArg, argumentsList) {
mod._exports === invalid && reload(mod);
return Reflect.apply(mod._exports, thisArg, argumentsList);
},
construct: function(target, argumentsList, newTarget) {
mod._exports === invalid && reload(mod);
return Reflect.construct(mod._exports, argumentsList);
}
});
}
Object.defineProperty(Module.prototype, 'invalidable', {
get: function() {
return !!this._proxy;
},
set: function(value) {
if ( this._proxy ) {
if ( !value )
this._proxy = null;
} else {
if ( value )
this._proxy = createProxy(this);
}
}
});
Object.defineProperty(Module.prototype, 'exports', {
get: function() {
return this._proxy ? this._proxy : this._exports;
},
set: function(value) {
this._exports = value;
}
});
Module.prototype.unload = function() {
var exports = this._exports;
this.invalidate();
this._exports = exports;
this.invalidable = false;
delete Module._cache[this.filename];
// remove this module from all module children
for ( var filename in Module._cache ) {
var children = Module._cache[filename].children;
var pos = children.indexOf(this);
if ( pos !== -1 )
children.splice(pos);
}
this.parent = null;
this.children.length = 0;
// remove module from Module._pathCache
var pathCache = Module._pathCache;
var keys = Object.keys(pathCache);
for ( var i = 0; i < keys.length; ++i )
if ( pathCache[keys[i]] === this.filename )
delete pathCache[keys[i]];
}
Module.prototype.unloadByPath = function(path) {
Module._cache[Module._resolveFilename(path, this, this.parent === null)].unload();
}
Module.unloadByExports = function(exports) {
for ( var filename in Module._cache )
if ( Module._cache[filename].exports === exports )
Module._cache[filename].unload();
}