Skip to content

Commit

Permalink
fix(deno-server): fix injections and enable back tests (#1625)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Aug 23, 2023
1 parent 84ac362 commit 2aa862c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 53 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"mime": "^3.0.0",
"mlly": "^1.4.0",
"mri": "^1.2.0",
"node-fetch-native": "^1.3.3",
"node-fetch-native": "^1.4.0",
"ofetch": "^1.2.1",
"ohash": "^1.1.3",
"openapi-typescript": "^6.5.3",
Expand Down
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 15 additions & 14 deletions src/presets/deno-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,23 @@ export const denoServer = defineNitroPreset({
commands: {
preview: "deno task --config ./deno.json start",
},
unenv: {
inject: {
global: ["unenv/runtime/polyfill/global-this", "default"],
Buffer: ["buffer", "Buffer"],
setTimeout: ["timers", "setTimeout"],
clearTimeout: ["timers", "clearTimeout"],
setInterval: ["timers", "setInterval"],
clearInterval: ["timers", "clearInterval"],
setImmediate: ["timers", "setImmediate"],
clearImmediate: ["timers", "clearImmediate"],
},
},
rollupConfig: {
output: {
hoistTransitiveImports: false,
},
plugins: [
inject({
modules: {
process: "process",
global: "global",
Buffer: ["buffer", "Buffer"],
setTimeout: ["timers", "setTimeout"],
clearTimeout: ["timers", "clearTimeout"],
setInterval: ["timers", "setInterval"],
clearInterval: ["timers", "clearInterval"],
setImmediate: ["timers", "setImmediate"],
clearImmediate: ["timers", "clearImmediate"],
},
}),
{
name: "rollup-plugin-node-deno",
resolveId(id) {
Expand Down Expand Up @@ -95,7 +94,9 @@ export const denoServer = defineNitroPreset({
}

const s = new MagicString(code);
s.prepend("import process from 'node:process';");
s.prepend(
"import __process__ from 'node:process';globalThis.process=globalThis.process||__process__;"
);

return {
code: s.toString(),
Expand Down
53 changes: 25 additions & 28 deletions test/presets/deno-server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,28 @@ const hasDeno =
execaCommandSync("deno --version", { stdio: "ignore", reject: false })
.exitCode === 0;

describe.runIf(hasDeno && false /* h3 latest needed */)(
"nitro:preset:deno-server",
async () => {
const ctx = await setupTest("deno-server");
testNitro(ctx, async () => {
const port = await getRandomPort();
const p = execa(
"deno",
["task", "--config", resolve(ctx.outDir, "deno.json"), "start"],
{
stdio: "inherit",
env: {
PORT: String(port),
},
}
);
ctx.server = {
url: `http://127.0.0.1:${port}`,
close: () => p.kill(),
} as any;
await waitForPort(port, { delay: 1000, retries: 20 });
return async ({ url, ...opts }) => {
const res = await ctx.fetch(url, opts);
return res;
};
});
}
);
describe.runIf(hasDeno)("nitro:preset:deno-server", async () => {
const ctx = await setupTest("deno-server");
testNitro(ctx, async () => {
const port = await getRandomPort();
const p = execa(
"deno",
["task", "--config", resolve(ctx.outDir, "deno.json"), "start"],
{
stdio: "inherit",
env: {
PORT: String(port),
},
}
);
ctx.server = {
url: `http://127.0.0.1:${port}`,
close: () => p.kill(),
} as any;
await waitForPort(port, { delay: 1000, retries: 20 });
return async ({ url, ...opts }) => {
const res = await ctx.fetch(url, opts);
return res;
};
});
});
5 changes: 4 additions & 1 deletion test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,10 @@ export function testNitro(
});

it.skipIf(
!ctx.nitro.options.node || ctx.preset === "bun" /* TODO: Investigate */
!ctx.nitro.options.node ||
// TODO: Investigate
ctx.preset === "bun" ||
ctx.preset === "deno-server"
)("sourcemap works", async () => {
const { data } = await callHandler({ url: "/error-stack" });
expect(data.stack).toMatch("test/fixture/routes/error-stack.ts:4:1");
Expand Down

0 comments on commit 2aa862c

Please sign in to comment.