-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the nodejs_compat_v2 compat flag
The `nodejs_compat_v2` flag supersedes the 'nodejs_compat' flag. When enabled... 1. Node.js built-ins are available for import/require 2. Unlike the original `nodejs_compat` flag, Node.js imports do not require the 'node:' specifier prefix. Internally, the implementation will detect a Node.js raw specifier and convert it into the appropriate prefixed specifier, e.g. 'fs' becomes 'node:fs' 3. The `Buffer` and `process` global objects are exposed on the global 4. A user worker bundle can still provide it's own implementations of all `node:` modules which will take precendence over the built-ins 5. The new `process.getBuiltinModule(...)` API is implemented. See nodejs/node#52762 A worker can replace the implementation of `node:process` if they choose, which may mean that `getBuiltinModule(...)` is not available.
- Loading branch information
Showing
16 changed files
with
297 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) 2017-2022 Cloudflare, Inc. | ||
// Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
// https://opensource.org/licenses/Apache-2.0 | ||
// | ||
|
||
// Imports of Node.js built-ins should work both with and without | ||
// the 'node:' prefix. | ||
import { default as assert } from 'node:assert'; | ||
import { default as assert2 } from 'assert'; | ||
const assert3 = (await import('node:assert')).default; | ||
const assert4 = (await import('assert')).default; | ||
|
||
assert.strictEqual(assert, assert2); | ||
assert.strictEqual(assert, assert3); | ||
assert.strictEqual(assert, assert4); | ||
|
||
export const nodeJsExpectedGlobals = { | ||
async test() { | ||
// Expected Node.js globals Buffer, process, and global should be present. | ||
const { Buffer } = await import('node:buffer'); | ||
assert.strictEqual(Buffer, globalThis.Buffer); | ||
|
||
const { default: process } = await import('node:process'); | ||
assert.strictEqual(process, globalThis.process); | ||
|
||
assert.strictEqual(global, globalThis); | ||
} | ||
}; | ||
|
||
export const nodeJsGetBuiltins = { | ||
async test() { | ||
// node:* modules in the worker bundle should override the built-in modules... | ||
const { default: fs } = await import('node:fs'); | ||
const { default: path } = await import('node:path'); | ||
|
||
await import ('node:path'); | ||
|
||
// But process.getBuiltinModule should always return the built-in module. | ||
const builtInPath = process.getBuiltinModule('node:path'); | ||
const builtInFs = process.getBuiltinModule('node:fs'); | ||
|
||
// These are from the worker bundle.... | ||
assert.strictEqual(fs, 1); | ||
assert.strictEqual(path, 2); | ||
|
||
// But these are from the built-ins... | ||
// node:fs is not implemented currently so it should be undefined here. | ||
assert.strictEqual(builtInFs, undefined); | ||
|
||
// node:path is implemented tho... | ||
assert.notStrictEqual(path, builtInPath); | ||
assert.strictEqual(typeof builtInPath, 'object'); | ||
assert.strictEqual(typeof builtInPath.join, 'function'); | ||
|
||
// While process.getBuiltinModule(...) in Node.js only returns Node.js | ||
// built-ins, our impl will return cloudflare: and workerd: built-ins | ||
// also, for completeness. A key difference, however, is that for non-Node.js | ||
// built-ins, the return value is the module namespace rather than the default | ||
// export. | ||
|
||
const socket = await import('cloudflare:sockets'); | ||
assert.strictEqual(process.getBuiltinModule('cloudflare:sockets'), socket); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Workerd = import "/workerd/workerd.capnp"; | ||
|
||
const unitTests :Workerd.Config = ( | ||
services = [ | ||
( name = "node-compat-v2-test", | ||
worker = ( | ||
modules = [ | ||
(name = "worker", esModule = embed "node-compat-v2-test.js"), | ||
(name = "node:fs", esModule = "export default 1"), | ||
(name = "node:path", esModule = "export default 2"), | ||
], | ||
compatibilityDate = "2024-05-01", | ||
compatibilityFlags = ["nodejs_compat_v2", "experimental"] | ||
) | ||
), | ||
], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.