Skip to content

Commit

Permalink
nodejs_compat flag
Browse files Browse the repository at this point in the history
  • Loading branch information
GregBrimble committed Jan 19, 2023
1 parent 1cd651c commit 5f526a6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
20 changes: 18 additions & 2 deletions packages/core/src/plugins/core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Blob } from "buffer";
import fs from "fs/promises";
import asyncHooks from "node:async_hooks";
import path from "path";
import {
ByteLengthQueuingStrategy,
Expand All @@ -25,6 +26,7 @@ import webStreams from "stream/web";
import { URL, URLSearchParams } from "url";
import { TextDecoder, TextEncoder } from "util";
import {
AdditionalModules,
CompatibilityFlag,
Context,
ModuleRule,
Expand Down Expand Up @@ -70,6 +72,7 @@ import {
} from "../standards";
import { assertsInRequest } from "../standards/helpers";
import type { BindingsOptions } from "./bindings";
import { additionalModules } from "./node";

const DEFAULT_MODULE_RULES: ModuleRule[] = [
{ type: "ESModule", include: ["**/*.mjs"] },
Expand Down Expand Up @@ -411,6 +414,7 @@ export class CorePlugin extends Plugin<CoreOptions> implements CoreOptions {

readonly upstreamURL?: URL;
readonly #globals: Context;
readonly #additionalModules?: AdditionalModules;

constructor(ctx: PluginContext, options?: CoreOptions) {
super(ctx);
Expand All @@ -421,6 +425,11 @@ export class CorePlugin extends Plugin<CoreOptions> implements CoreOptions {
);
}

const nodejsCompat = ctx.compat.isEnabled("nodejs_compat");
if (nodejsCompat) {
this.#additionalModules = additionalModules;
}

const extraGlobals: Context = {};

// Make sure the kFormDataFiles flag is set correctly when constructing
Expand Down Expand Up @@ -652,11 +661,13 @@ export class CorePlugin extends Plugin<CoreOptions> implements CoreOptions {

async setup(): Promise<SetupResult> {
const globals = this.#globals;
const additionalModules = this.#additionalModules;

// First, try to load script from string, no need to watch any files
if (this.script !== undefined) {
return {
globals,
additionalModules,
script: { filePath: STRING_SCRIPT_PATH, code: this.script },
};
}
Expand Down Expand Up @@ -689,11 +700,16 @@ export class CorePlugin extends Plugin<CoreOptions> implements CoreOptions {
scriptPath = path.resolve(this.ctx.rootPath, scriptPath);
const code = await fs.readFile(scriptPath, "utf8");
watch.push(scriptPath);
return { globals, script: { filePath: scriptPath, code }, watch };
return {
globals,
additionalModules,
script: { filePath: scriptPath, code },
watch,
};
}

// If we couldn't load a script yet, keep watching package.json anyways, it
// might get edited with a path
return { globals, watch };
return { globals, additionalModules, watch };
}
}
6 changes: 6 additions & 0 deletions packages/core/src/plugins/node/async_hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import async_hooks from "node:async_hooks";

export class AsyncHooksModule {
AsyncLocalStorage = async_hooks.AsyncLocalStorage;
AsyncResource = async_hooks.AsyncResource;
}
5 changes: 5 additions & 0 deletions packages/core/src/plugins/node/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { AsyncHooksModule } from "./async_hooks";

export const additionalModules = {
"node:async_hooks": { default: new AsyncHooksModule() },
};
14 changes: 14 additions & 0 deletions packages/core/test/plugins/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,20 @@ test("CorePlugin: setup: uses actual time if option enabled", async (t) => {
});
});

test("CorePlugin: nodejs_compat compatibiltiy flag includes Node.js modules", async (t) => {
const compat = new Compatibility(undefined, ["nodejs_compat"]);

const plugin = new CorePlugin(
{ ...ctx, compat },
{ compatibilityFlags: ["nodejs_compat"] }
);
const additionalModules = (await plugin.setup()).additionalModules;
t.deepEqual(
Object.keys(additionalModules?.["node:async_hooks"].default ?? {}),
["AsyncLocalStorage", "AsyncResource"]
);
});

// Test stream constructors
test("CorePlugin: setup: ReadableStream/WriteableStream constructors only enabled if compatibility flag enabled", async (t) => {
// Check without "streams_enable_constructors" compatibility flag (should throw)
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/src/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface CompatibilityFeature {
// will get a type error if they try to use an unsupported flag via the API,
// and they won't be logged in the "Enabled Compatibility Flags" section.
export type CompatibilityEnableFlag =
| "nodejs_compat"
| "streams_enable_constructors"
| "transformstream_enable_standard_constructor"
| "global_navigator"
Expand All @@ -30,6 +31,9 @@ export type CompatibilityFlag =
| CompatibilityDisableFlag;

const FEATURES: CompatibilityFeature[] = [
{
enableFlag: "nodejs_compat",
},
{
defaultAsOf: "2022-11-30",
enableFlag: "streams_enable_constructors",
Expand Down

0 comments on commit 5f526a6

Please sign in to comment.