mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
lib: move module exports proxy into a separate method
Also added the comment in f074612b7 to make
NativeModule.prototype.compile() more readable.
PR-URL: https://github.com/nodejs/node/pull/24057
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Refael Ackermann <refack@gmail.com>
This commit is contained in:
1 parent
10156c612d
commit
879c0f1f3e
1 file changed
+59
-51
@@ -235,6 +235,64 @@
|
||||
undefined;
|
||||
};
|
||||
|
||||
// Provide named exports for all builtin libraries so that the libraries
|
||||
// may be imported in a nicer way for esm users. The default export is left
|
||||
// as the entire namespace (module.exports) and wrapped in a proxy such
|
||||
// that APMs and other behavior are still left intact.
|
||||
NativeModule.prototype.proxifyExports = function() {
|
||||
this.exportKeys = ObjectKeys(this.exports);
|
||||
|
||||
const update = (property, value) => {
|
||||
if (this.reflect !== undefined &&
|
||||
ReflectApply(ObjectHasOwnProperty,
|
||||
this.reflect.exports, [property]))
|
||||
this.reflect.exports[property].set(value);
|
||||
};
|
||||
|
||||
const handler = {
|
||||
__proto__: null,
|
||||
defineProperty: (target, prop, descriptor) => {
|
||||
// Use `Object.defineProperty` instead of `Reflect.defineProperty`
|
||||
// to throw the appropriate error if something goes wrong.
|
||||
ObjectDefineProperty(target, prop, descriptor);
|
||||
if (typeof descriptor.get === 'function' &&
|
||||
!ReflectHas(handler, 'get')) {
|
||||
handler.get = (target, prop, receiver) => {
|
||||
const value = ReflectGet(target, prop, receiver);
|
||||
if (ReflectApply(ObjectHasOwnProperty, target, [prop]))
|
||||
update(prop, value);
|
||||
return value;
|
||||
};
|
||||
}
|
||||
update(prop, getOwn(target, prop));
|
||||
return true;
|
||||
},
|
||||
deleteProperty: (target, prop) => {
|
||||
if (ReflectDeleteProperty(target, prop)) {
|
||||
update(prop, undefined);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
set: (target, prop, value, receiver) => {
|
||||
const descriptor = ReflectGetOwnPropertyDescriptor(target, prop);
|
||||
if (ReflectSet(target, prop, value, receiver)) {
|
||||
if (descriptor && typeof descriptor.set === 'function') {
|
||||
for (const key of this.exportKeys) {
|
||||
update(key, getOwn(target, key, receiver));
|
||||
}
|
||||
} else {
|
||||
update(prop, getOwn(target, prop, receiver));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
this.exports = new Proxy(this.exports, handler);
|
||||
};
|
||||
|
||||
NativeModule.prototype.compile = function() {
|
||||
const id = this.id;
|
||||
let source = NativeModule.getSource(id);
|
||||
@@ -299,57 +357,7 @@
|
||||
fn(this.exports, requireFn, this, process, internalBinding);
|
||||
|
||||
if (config.experimentalModules && !NativeModule.isInternal(this.id)) {
|
||||
this.exportKeys = ObjectKeys(this.exports);
|
||||
|
||||
const update = (property, value) => {
|
||||
if (this.reflect !== undefined &&
|
||||
ReflectApply(ObjectHasOwnProperty,
|
||||
this.reflect.exports, [property]))
|
||||
this.reflect.exports[property].set(value);
|
||||
};
|
||||
|
||||
const handler = {
|
||||
__proto__: null,
|
||||
defineProperty: (target, prop, descriptor) => {
|
||||
// Use `Object.defineProperty` instead of `Reflect.defineProperty`
|
||||
// to throw the appropriate error if something goes wrong.
|
||||
ObjectDefineProperty(target, prop, descriptor);
|
||||
if (typeof descriptor.get === 'function' &&
|
||||
!ReflectHas(handler, 'get')) {
|
||||
handler.get = (target, prop, receiver) => {
|
||||
const value = ReflectGet(target, prop, receiver);
|
||||
if (ReflectApply(ObjectHasOwnProperty, target, [prop]))
|
||||
update(prop, value);
|
||||
return value;
|
||||
};
|
||||
}
|
||||
update(prop, getOwn(target, prop));
|
||||
return true;
|
||||
},
|
||||
deleteProperty: (target, prop) => {
|
||||
if (ReflectDeleteProperty(target, prop)) {
|
||||
update(prop, undefined);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
set: (target, prop, value, receiver) => {
|
||||
const descriptor = ReflectGetOwnPropertyDescriptor(target, prop);
|
||||
if (ReflectSet(target, prop, value, receiver)) {
|
||||
if (descriptor && typeof descriptor.set === 'function') {
|
||||
for (const key of this.exportKeys) {
|
||||
update(key, getOwn(target, key, receiver));
|
||||
}
|
||||
} else {
|
||||
update(prop, getOwn(target, prop, receiver));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
this.exports = new Proxy(this.exports, handler);
|
||||
this.proxifyExports();
|
||||
}
|
||||
|
||||
this.loaded = true;
|
||||
|
||||
Reference in new issue
Block a user