-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
esm: provide named exports for builtin libs #20403
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,11 +59,18 @@ translators.set('cjs', async (url) => { | |
// through normal resolution | ||
translators.set('builtin', async (url) => { | ||
debug(`Translating BuiltinModule ${url}`); | ||
return createDynamicModule(['default'], url, (reflect) => { | ||
debug(`Loading BuiltinModule ${url}`); | ||
const exports = NativeModule.require(url.slice(5)); | ||
reflect.exports.default.set(exports); | ||
}); | ||
// slice 'node:' scheme | ||
const id = url.slice(5); | ||
NativeModule.require(id); | ||
const module = NativeModule.getCached(id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getCached returns the entire module object, require just gives the exports object There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah right, I see, we can reconsider the interface if necessary then for future work. |
||
return createDynamicModule( | ||
[...module.exportKeys, 'default'], url, (reflect) => { | ||
debug(`Loading BuiltinModule ${url}`); | ||
module.reflect = reflect; | ||
for (const key of module.exportKeys) | ||
reflect.exports[key].set(module.exports[key]); | ||
reflect.exports.default.set(module.exports); | ||
}); | ||
}); | ||
|
||
// Strategy for loading a node native module | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// Flags: --experimental-modules | ||
|
||
import '../common'; | ||
import assert from 'assert'; | ||
|
||
import fs, { readFile, readFileSync } from 'fs'; | ||
import events, { defaultMaxListeners } from 'events'; | ||
import util from 'util'; | ||
|
||
const readFileDescriptor = Reflect.getOwnPropertyDescriptor(fs, 'readFile'); | ||
const readFileSyncDescriptor = | ||
Reflect.getOwnPropertyDescriptor(fs, 'readFileSync'); | ||
|
||
const s = Symbol(); | ||
const fn = () => s; | ||
|
||
Reflect.deleteProperty(fs, 'readFile'); | ||
|
||
assert.deepStrictEqual([fs.readFile, readFile], [undefined, undefined]); | ||
|
||
fs.readFile = fn; | ||
|
||
assert.deepStrictEqual([fs.readFile(), readFile()], [s, s]); | ||
|
||
Reflect.defineProperty(fs, 'readFile', { | ||
value: fn, | ||
configurable: true, | ||
writable: true, | ||
}); | ||
|
||
assert.deepStrictEqual([fs.readFile(), readFile()], [s, s]); | ||
|
||
Reflect.deleteProperty(fs, 'readFile'); | ||
|
||
assert.deepStrictEqual([fs.readFile, readFile], [undefined, undefined]); | ||
|
||
let count = 0; | ||
|
||
Reflect.defineProperty(fs, 'readFile', { | ||
get() { return count; }, | ||
configurable: true, | ||
}); | ||
|
||
count++; | ||
|
||
assert.deepStrictEqual([readFile, fs.readFile, readFile], [0, 1, 1]); | ||
|
||
let otherValue; | ||
|
||
Reflect.defineProperty(fs, 'readFile', { // eslint-disable-line accessor-pairs | ||
set(value) { | ||
Reflect.deleteProperty(fs, 'readFile'); | ||
otherValue = value; | ||
}, | ||
configurable: true, | ||
}); | ||
|
||
Reflect.defineProperty(fs, 'readFileSync', { | ||
get() { | ||
return otherValue; | ||
}, | ||
configurable: true, | ||
}); | ||
|
||
fs.readFile = 2; | ||
|
||
assert.deepStrictEqual([readFile, readFileSync], [undefined, 2]); | ||
|
||
Reflect.defineProperty(fs, 'readFile', readFileDescriptor); | ||
Reflect.defineProperty(fs, 'readFileSync', readFileSyncDescriptor); | ||
|
||
const originDefaultMaxListeners = events.defaultMaxListeners; | ||
const utilProto = util.__proto__; // eslint-disable-line no-proto | ||
|
||
count = 0; | ||
|
||
Reflect.defineProperty(Function.prototype, 'defaultMaxListeners', { | ||
configurable: true, | ||
enumerable: true, | ||
get: function() { return ++count; }, | ||
set: function(v) { | ||
Reflect.defineProperty(this, 'defaultMaxListeners', { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: v, | ||
}); | ||
}, | ||
}); | ||
|
||
assert.strictEqual(defaultMaxListeners, originDefaultMaxListeners); | ||
assert.strictEqual(events.defaultMaxListeners, originDefaultMaxListeners); | ||
|
||
assert.strictEqual(++events.defaultMaxListeners, | ||
originDefaultMaxListeners + 1); | ||
|
||
assert.strictEqual(defaultMaxListeners, originDefaultMaxListeners + 1); | ||
assert.strictEqual(Function.prototype.defaultMaxListeners, 1); | ||
|
||
Function.prototype.defaultMaxListeners = 'foo'; | ||
|
||
assert.strictEqual(Function.prototype.defaultMaxListeners, 'foo'); | ||
assert.strictEqual(events.defaultMaxListeners, originDefaultMaxListeners + 1); | ||
assert.strictEqual(defaultMaxListeners, originDefaultMaxListeners + 1); | ||
|
||
count = 0; | ||
|
||
const p = { | ||
get foo() { return ++count; }, | ||
set foo(v) { | ||
Reflect.defineProperty(this, 'foo', { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: v, | ||
}); | ||
}, | ||
}; | ||
|
||
util.__proto__ = p; // eslint-disable-line no-proto | ||
|
||
assert.strictEqual(util.foo, 1); | ||
|
||
util.foo = 'bar'; | ||
|
||
assert.strictEqual(count, 1); | ||
assert.strictEqual(util.foo, 'bar'); | ||
assert.strictEqual(p.foo, 2); | ||
|
||
p.foo = 'foo'; | ||
|
||
assert.strictEqual(p.foo, 'foo'); | ||
|
||
events.defaultMaxListeners = originDefaultMaxListeners; | ||
util.__proto__ = utilProto; // eslint-disable-line no-proto | ||
|
||
Reflect.deleteProperty(util, 'foo'); | ||
Reflect.deleteProperty(Function.prototype, 'defaultMaxListeners'); | ||
|
||
assert.throws( | ||
() => Object.defineProperty(events, 'defaultMaxListeners', { value: 3 }), | ||
/TypeError: Cannot redefine/ | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ignorable nit: as this is a file, not a REPL, maybe add
assert()
orconsole.log()
here?