Skip to content

Commit 4cb822e

Browse files
joyeecheungMylesBorins
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 <anna@addaleax.net> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Refael Ackermann <refack@gmail.com>
1 parent a93a1df commit 4cb822e

File tree

1 file changed

+59
-51
lines changed

1 file changed

+59
-51
lines changed

lib/internal/bootstrap/loaders.js

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,64 @@
241241
undefined;
242242
};
243243

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

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

361369
this.loaded = true;

0 commit comments

Comments
 (0)