Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion scripts/build/build-npm-dnt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
readDenoConfigSet,
} from "./npm-dependency-sources.ts";
import { buildExtensionPackages } from "./build-npm-extension-packages.ts";
import { patchDntArgvPolyfill } from "./dnt-polyfill.ts";
import { patchDntArgvPolyfill, patchDntDenoShim } from "./dnt-polyfill.ts";
import { normalizeNpmPackageMetadata } from "./npm-package-metadata.ts";
import { assertNpmRuntimeHelperContract } from "./npm-runtime-helper-contract.ts";
import { normalizeEsmShReactNpmShims } from "./npm-react-shims.ts";
Expand Down Expand Up @@ -232,6 +232,16 @@ await build({
{ required: true },
);

// dnt re-exports `@deno/shim-deno` unconditionally, so `deno run -A
// npm:veryfront` ran the Node reimplementations of Deno APIs instead of
// the runtime's own. Deno.listen in particular reads `server._handle.fd`
// off a node:net server, which is null under Deno's node compatibility
// layer, so every command that binds a port died before starting.
await patchDntDenoShim(
"./npm/esm/_dnt.shims.js",
{ required: true },
);

const patchedReactShimCount = normalizeEsmShReactNpmShims("./npm/esm/deps/esm.sh");
if (patchedReactShimCount > 0) {
console.log(`📝 Patched ${patchedReactShimCount} React ecosystem esm.sh npm shims`);
Expand Down
3 changes: 2 additions & 1 deletion scripts/build/build-npm-extension-packages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { build, emptyDir } from "#dnt";
import { basename, dirname, join, relative, toFileUrl } from "#std/path";
import { patchDntArgvPolyfill } from "./dnt-polyfill.ts";
import { patchDntArgvPolyfill, patchDntDenoShim } from "./dnt-polyfill.ts";
import {
bareImportPackageNames,
createExtensionPackageSpecs,
Expand Down Expand Up @@ -90,6 +90,7 @@ async function buildExtensionPackage(
rootConfig: options.rootConfig,
});
await patchDntArgvPolyfill(`${outDir}/esm/_dnt.polyfills.js`);
await patchDntDenoShim(`${outDir}/esm/_dnt.shims.js`);
Comment thread
coderabbitai[bot] marked this conversation as resolved.

await Deno.copyFile(`${options.rootDir}/LICENSE`, `${outDir}/LICENSE`);
await Deno.copyFile(`${options.rootDir}/NOTICE`, `${outDir}/NOTICE`);
Expand Down
247 changes: 199 additions & 48 deletions scripts/build/dnt-polyfill.test.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,203 @@
import { assertEquals, assertRejects } from "#std/assert";
import { patchDntArgvPolyfill } from "./dnt-polyfill.ts";

Deno.test("patchDntArgvPolyfill guards missing process argv entries", async () => {
const directory = await Deno.makeTempDir();
const path = `${directory}/_dnt.polyfills.js`;

try {
await Deno.writeTextFile(
path,
'const mainUrl = "file:///" + process.argv[1].replace(/\\\\/g, "/");\n',
);

assertEquals(await patchDntArgvPolyfill(path, { required: true }), true);
assertEquals(
await Deno.readTextFile(path),
'const mainUrl = "file:///" + (process.argv[1] ?? "").replace(/\\\\/g, "/");\n',
);
assertEquals(await patchDntArgvPolyfill(path, { required: true }), false);
} finally {
await Deno.remove(directory, { recursive: true });
}
});
import { assertEquals, assertRejects } from "#veryfront/testing/assert.ts";
import { describe, it } from "#veryfront/testing/bdd.ts";
import { patchDntArgvPolyfill, patchDntDenoShim } from "./dnt-polyfill.ts";

/** Verbatim shape of the `_dnt.shims.js` DNT emits for `shims: { deno: true, crypto: true }`. */
const GENERATED_SHIMS = `import { Deno } from "@deno/shim-deno";
export { Deno } from "@deno/shim-deno";
import { crypto } from "@deno/shim-crypto";
export { crypto } from "@deno/shim-crypto";
const dntGlobals = {
Deno,
crypto,
};
export const dntGlobalThis = globalThis;
`;

describe("patchDntArgvPolyfill", () => {
it("guards missing process argv entries", async () => {
const directory = await Deno.makeTempDir();
const path = `${directory}/_dnt.polyfills.js`;

try {
await Deno.writeTextFile(
path,
'const mainUrl = "file:///" + process.argv[1].replace(/\\\\/g, "/");\n',
);

assertEquals(await patchDntArgvPolyfill(path, { required: true }), true);
assertEquals(
await Deno.readTextFile(path),
'const mainUrl = "file:///" + (process.argv[1] ?? "").replace(/\\\\/g, "/");\n',
);
assertEquals(await patchDntArgvPolyfill(path, { required: true }), false);
} finally {
await Deno.remove(directory, { recursive: true });
}
});

it("skips packages without the DNT import-meta shim", async () => {
const directory = await Deno.makeTempDir();
const path = `${directory}/_dnt.polyfills.js`;

Deno.test("patchDntArgvPolyfill skips packages without the DNT import-meta shim", async () => {
const directory = await Deno.makeTempDir();
const path = `${directory}/_dnt.polyfills.js`;

try {
await Deno.writeTextFile(path, "export {};\n");
assertEquals(await patchDntArgvPolyfill(path), false);
assertEquals(await Deno.readTextFile(path), "export {};\n");
} finally {
await Deno.remove(directory, { recursive: true });
}
try {
await Deno.writeTextFile(path, "export {};\n");
assertEquals(await patchDntArgvPolyfill(path), false);
assertEquals(await Deno.readTextFile(path), "export {};\n");
} finally {
await Deno.remove(directory, { recursive: true });
}
});

it("fails closed when required DNT output changes", async () => {
const directory = await Deno.makeTempDir();
const path = `${directory}/_dnt.polyfills.js`;

try {
await Deno.writeTextFile(path, "export {};\n");
await assertRejects(
() => patchDntArgvPolyfill(path, { required: true }),
Error,
"does not contain the expected process.argv[1] expression",
);
} finally {
await Deno.remove(directory, { recursive: true });
}
});
});

Deno.test("patchDntArgvPolyfill fails closed when required DNT output changes", async () => {
const directory = await Deno.makeTempDir();
const path = `${directory}/_dnt.polyfills.js`;

try {
await Deno.writeTextFile(path, "export {};\n");
await assertRejects(
() => patchDntArgvPolyfill(path, { required: true }),
Error,
"does not contain the expected process.argv[1] expression",
);
} finally {
await Deno.remove(directory, { recursive: true });
}
/**
* Load the patched `_dnt.shims.js` the way the published package loads it, with
* `@deno/shim-deno` swapped for a local stub so the test can tell which `Deno`
* the module actually hands back.
*/
async function importPatchedShims(
directory: string,
source: string,
): Promise<{ Deno: typeof Deno }> {
await Deno.writeTextFile(
`${directory}/stub-shim-deno.js`,
'export const Deno = { __stub: "deno" };\n',
);
await Deno.writeTextFile(
`${directory}/stub-shim-crypto.js`,
'export const crypto = { __stub: "crypto" };\n',
);
const path = `${directory}/_dnt.shims.js`;
await Deno.writeTextFile(
path,
source
.replaceAll('"@deno/shim-deno"', '"./stub-shim-deno.js"')
.replaceAll('"@deno/shim-crypto"', '"./stub-shim-crypto.js"'),
);
return await import(
`${import.meta.resolve(`file://${path}`)}?t=${Date.now()}`
);
}

describe("patchDntDenoShim", () => {
it("pins the bug being fixed: unpatched DNT shims shadow the real Deno global", async () => {
const directory = await Deno.makeTempDir();

try {
// DNT re-exports `@deno/shim-deno` unconditionally, so the published
// package used the Node reimplementation even when running under Deno.
const unpatched = await importPatchedShims(directory, GENERATED_SHIMS);
assertEquals(unpatched.Deno, { __stub: "deno" });
} finally {
await Deno.remove(directory, { recursive: true });
}
});

it("makes the published shims prefer the real Deno global", async () => {
const directory = await Deno.makeTempDir();
const path = `${directory}/_dnt.shims.js`;

try {
await Deno.writeTextFile(path, GENERATED_SHIMS);
assertEquals(await patchDntDenoShim(path, { required: true }), true);

// Running under Deno, the module must resolve to the runtime's own Deno.
// The `@deno/shim-deno` fallback reimplements Deno.listen on top of
// node:net and reads `server._handle.fd`, which is null under Deno's node
// compatibility layer — every `veryfront dev` under Deno died there.
const patched = await importPatchedShims(
directory,
await Deno.readTextFile(path),
);
assertEquals(patched.Deno, globalThis.Deno);
assertEquals(typeof patched.Deno.listen, "function");
} finally {
await Deno.remove(directory, { recursive: true });
}
});

it("keeps the shim fallback for runtimes without Deno", async () => {
const directory = await Deno.makeTempDir();
const path = `${directory}/_dnt.shims.js`;

try {
await Deno.writeTextFile(path, GENERATED_SHIMS);
await patchDntDenoShim(path, { required: true });
const patchedSource = await Deno.readTextFile(path);

// Simulate Node/Bun, where `globalThis.Deno` is absent, by pinning the
// native lookup to undefined before re-evaluating the module.
const nodeShaped = patchedSource.replace(
"globalThis.Deno",
"/* node */ undefined",
);
const patched = await importPatchedShims(directory, nodeShaped);
assertEquals(patched.Deno, { __stub: "deno" });
} finally {
await Deno.remove(directory, { recursive: true });
}
});

it("is idempotent", async () => {
const directory = await Deno.makeTempDir();
const path = `${directory}/_dnt.shims.js`;

try {
await Deno.writeTextFile(path, GENERATED_SHIMS);
assertEquals(await patchDntDenoShim(path, { required: true }), true);
const once = await Deno.readTextFile(path);
assertEquals(await patchDntDenoShim(path, { required: true }), false);
assertEquals(await Deno.readTextFile(path), once);
} finally {
await Deno.remove(directory, { recursive: true });
}
});

it("fails closed when required DNT shim output changes", async () => {
const directory = await Deno.makeTempDir();
const path = `${directory}/_dnt.shims.js`;

try {
await Deno.writeTextFile(
path,
'export { Deno } from "@deno/shim-deno-next";\n',
);
await assertRejects(
() => patchDntDenoShim(path, { required: true }),
Error,
"does not contain the expected @deno/shim-deno re-export",
);
} finally {
await Deno.remove(directory, { recursive: true });
}
});

it("skips packages without the DNT Deno shim", async () => {
const directory = await Deno.makeTempDir();
const path = `${directory}/_dnt.shims.js`;

try {
await Deno.writeTextFile(path, "export {};\n");
assertEquals(await patchDntDenoShim(path), false);
assertEquals(await Deno.readTextFile(path), "export {};\n");
} finally {
await Deno.remove(directory, { recursive: true });
}
});
});
72 changes: 72 additions & 0 deletions scripts/build/dnt-polyfill.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
const UNSAFE_ARGV_EXPRESSION = "process.argv[1].replace";
const SAFE_ARGV_EXPRESSION = '(process.argv[1] ?? "").replace';

const SHIM_DENO_REEXPORT = 'import { Deno } from "@deno/shim-deno";\n' +
'export { Deno } from "@deno/shim-deno";\n';

/** Marker proving the Deno-global preference is already in place. */
const NATIVE_DENO_MARKER = "dntNativeDeno";

const NATIVE_DENO_PREFERENCE =
'import { Deno as dntShimDeno } from "@deno/shim-deno";\n' +
"const dntNativeDeno = globalThis.Deno;\n" +
"// Prefer the host runtime's own Deno. The `@deno/shim-deno` fallback exists\n" +
"// for Node and Bun; under Deno it shadows working native APIs with node:net\n" +
"// reimplementations that throw (e.g. Deno.listen reads `server._handle.fd`,\n" +
"// which is null on Deno's node compatibility layer).\n" +
'export const Deno = typeof dntNativeDeno?.version?.deno === "string"\n' +
" ? dntNativeDeno\n" +
" : dntShimDeno;\n";

export type PatchDntArgvPolyfillOptions = {
required?: boolean;
};
Expand Down Expand Up @@ -55,3 +72,58 @@ export async function patchDntArgvPolyfill(

return false;
}

export type PatchDntDenoShimOptions = {
required?: boolean;
};

/**
* Make DNT's generated `_dnt.shims.js` defer to the host runtime's own `Deno`.
*
* DNT re-exports `@deno/shim-deno` unconditionally, so every framework module
* that touches `Deno.*` got the Node reimplementation even when the published
* package was executed by Deno itself (`deno run -A npm:veryfront dev`). Those
* reimplementations are not runnable on Deno: `Deno.listen` builds a node:net
* server and immediately reads `server._handle.fd`, which is null on Deno's
* node compatibility layer, so the CLI aborted with
* "Cannot read properties of null (reading 'fd')" before the dev server bound.
*
* The shim stays in place for Node and Bun, which have no `Deno` global.
*/
export async function patchDntDenoShim(
path: string,
options: PatchDntDenoShimOptions = {},
): Promise<boolean> {
let source: string;
try {
source = await Deno.readTextFile(path);
} catch (error) {
if (!options.required && error instanceof Deno.errors.NotFound) {
return false;
}
throw error;
}

if (source.includes(NATIVE_DENO_MARKER)) {
return false;
}

if (source.includes(SHIM_DENO_REEXPORT)) {
await Deno.writeTextFile(
path,
source.replace(SHIM_DENO_REEXPORT, NATIVE_DENO_PREFERENCE),
);
console.log("Patched DNT Deno shim to prefer the native Deno global");
return true;
}

if (options.required || source.includes("@deno/shim-deno")) {
throw new Error(
`${path} does not contain the expected @deno/shim-deno re-export. ` +
"DNT output may have changed; update the generated shim normalizer " +
"before publishing.",
);
}

return false;
}