Skip to content

Commit 879c0f1

Browse files
joyeecheungtargos
authored andcommitted
lib: move module exports proxy into a separate method
Also added the comment in f074612 to make NativeModule.prototype.compile() more readable. PR-URL: #24057 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent 10156c6 commit 879c0f1

File tree

1 file changed

+59
-51
lines changed

1 file changed

+59
-51
lines changed

lib/internal/bootstrap/loaders.js

+59-51
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,64 @@
235235
undefined;
236236
};
237237

238+
// Provide named exports for all builtin libraries so that the libraries
239+
// may be imported in a nicer way for esm users. The default export is left
240+
// as the entire namespace (module.exports) and wrapped in a proxy such
241+
// that APMs and other behavior are still left intact.
242+
NativeModule.prototype.proxifyExports = function() {
243+
this.exportKeys = ObjectKeys(this.exports);
244+
245+
const update = (property, value) => {
246+
if (this.reflect !== undefined &&
247+
ReflectApply(ObjectHasOwnProperty,
248+
this.reflect.exports, [property]))
249+
this.reflect.exports[property].set(value);
250+
};
251+
252+
const handler = {
253+
__proto__: null,
254+
defineProperty: (target, prop, descriptor) => {
255+
// Use `Object.defineProperty` instead of `Reflect.defineProperty`
256+
// to throw the appropriate error if something goes wrong.
257+
ObjectDefineProperty(target, prop, descriptor);
258+
if (typeof descriptor.get === 'function' &&
259+
!ReflectHas(handler, 'get')) {
260+
handler.get = (target, prop, receiver) => {
261+
const value = ReflectGet(target, prop, receiver);
262+
if (ReflectApply(ObjectHasOwnProperty, target, [prop]))
263+
update(prop, value);
264+
return value;
265+
};
266+
}
267+
update(prop, getOwn(target, prop));
268+
return true;
269+
},
270+
deleteProperty: (target, prop) => {
271+
if (ReflectDeleteProperty(target, prop)) {
272+
update(prop, undefined);
273+
return true;
274+
}
275+
return false;
276+
},
277+
set: (target, prop, value, receiver) => {
278+
const descriptor = ReflectGetOwnPropertyDescriptor(target, prop);
279+
if (ReflectSet(target, prop, value, receiver)) {
280+
if (descriptor && typeof descriptor.set === 'function') {
281+
for (const key of this.exportKeys) {
282+
update(key, getOwn(target, key, receiver));
283+
}
284+
} else {
285+
update(prop, getOwn(target, prop, receiver));
286+
}
287+
return true;
288+
}
289+
return false;
290+
}
291+
};
292+
293+
this.exports = new Proxy(this.exports, handler);
294+
};
295+
238296
NativeModule.prototype.compile = function() {
239297
const id = this.id;
240298
let source = NativeModule.getSource(id);
@@ -299,57 +357,7 @@
299357
fn(this.exports, requireFn, this, process, internalBinding);
300358

301359
if (config.experimentalModules && !NativeModule.isInternal(this.id)) {
302-
this.exportKeys = ObjectKeys(this.exports);
303-
304-
const update = (property, value) => {
305-
if (this.reflect !== undefined &&
306-
ReflectApply(ObjectHasOwnProperty,
307-
this.reflect.exports, [property]))
308-
this.reflect.exports[property].set(value);
309-
};
310-
311-
const handler = {
312-
__proto__: null,
313-
defineProperty: (target, prop, descriptor) => {
314-
// Use `Object.defineProperty` instead of `Reflect.defineProperty`
315-
// to throw the appropriate error if something goes wrong.
316-
ObjectDefineProperty(target, prop, descriptor);
317-
if (typeof descriptor.get === 'function' &&
318-
!ReflectHas(handler, 'get')) {
319-
handler.get = (target, prop, receiver) => {
320-
const value = ReflectGet(target, prop, receiver);
321-
if (ReflectApply(ObjectHasOwnProperty, target, [prop]))
322-
update(prop, value);
323-
return value;
324-
};
325-
}
326-
update(prop, getOwn(target, prop));
327-
return true;
328-
},
329-
deleteProperty: (target, prop) => {
330-
if (ReflectDeleteProperty(target, prop)) {
331-
update(prop, undefined);
332-
return true;
333-
}
334-
return false;
335-
},
336-
set: (target, prop, value, receiver) => {
337-
const descriptor = ReflectGetOwnPropertyDescriptor(target, prop);
338-
if (ReflectSet(target, prop, value, receiver)) {
339-
if (descriptor && typeof descriptor.set === 'function') {
340-
for (const key of this.exportKeys) {
341-
update(key, getOwn(target, key, receiver));
342-
}
343-
} else {
344-
update(prop, getOwn(target, prop, receiver));
345-
}
346-
return true;
347-
}
348-
return false;
349-
}
350-
};
351-
352-
this.exports = new Proxy(this.exports, handler);
360+
this.proxifyExports();
353361
}
354362

355363
this.loaded = true;

0 commit comments

Comments
 (0)