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
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import process from "node:process";

export const isBun = globalThis.Bun !== undefined;

export const isDeno = globalThis.Deno !== undefined;

export const isNode = process.versions?.node !== undefined && !isBun && !isDeno;
export const isNode =
globalThis.process?.versions?.node !== undefined && !isBun && !isDeno;

export const isEdgeLight =
globalThis.EdgeRuntime === undefined
Expand Down
46 changes: 46 additions & 0 deletions regression.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { execFile } from "node:child_process";
import process from "node:process";
import { promisify } from "node:util";
import test from "ava";

const execute = promisify(execFile);

test("loads the unmodified module in runtimes without Node built-ins", async (t) => {
const script = `
import {readFile} from 'node:fs/promises';
import {createContext, SourceTextModule} from 'node:vm';
const source = await readFile(new URL(${JSON.stringify(new URL("./index.js", import.meta.url).href)}), 'utf8');
const environments = [
{window: {}, document: {}},
{EdgeRuntime: 'edge-runtime'},
{caches: {}, HTMLRewriter: class {}},
{Bun: {}},
{Deno: {}},
{},
{process: {versions: {node: '22.0.0'}}},
];
const results = [];
for (const globals of environments) {
const module = new SourceTextModule(source, {context: createContext(globals)});
await module.link((specifier) => {throw new Error('Unavailable built-in: ' + specifier);});
await module.evaluate();
results.push(module.namespace.default());
}
console.log(JSON.stringify(results));
`;
const { stdout } = await execute(process.execPath, [
"--experimental-vm-modules",
"--input-type=module",
"--eval",
script,
]);
t.deepEqual(JSON.parse(stdout), [
"browser",
"edge-light",
"cloudflare-workers",
"bun",
"deno",
"unknown",
"node",
]);
});
Loading