Skip to content
Closed
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
4 changes: 3 additions & 1 deletion src/jsc/bindings/BakeAdditionsToGlobalObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ struct BakeAdditionsToGlobalObject {
return m_bakeEnsureAsyncLocalStorage.get(globalObject);
}

// Only the dev server sets the instance. `undefined` means it is not set.
JSValue getAsyncLocalStorage(JSGlobalObject* globalObject)
{
return m_asyncLocalStorageInstance.get();
JSValue instance = m_asyncLocalStorageInstance.get();
return instance ? instance : jsUndefined();
}

JSC::JSObject* JSBakeResponseConstructor(const JSGlobalObject* global) const { return m_JSBakeResponseClassStructure.constructorInitializedOnMainThread(global); }
Expand Down
48 changes: 47 additions & 1 deletion test/bake/dev/response-to-bake-response.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, test } from "bun:test";
import { describe, expect, test } from "bun:test";
import { bunEnv, bunExe, tempDir } from "harness";
import path from "node:path";

Expand Down Expand Up @@ -245,3 +245,49 @@ test("Response import is added when Response is global, but not when shadowed",
// Global Response is transformed to import_bun_app.Response
expect(serverResult).toContain("var lmao = new import_bun_app.Response");
});

// Only the dev server sets the AsyncLocalStorage instance that these calls read.
describe.concurrent('the Response of "bun:app" outside the dev server', () => {
async function run(source: string) {
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", `import { Response } from "bun:app";\n${source}`],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
return { stdout, stderr, exitCode };
}

test("Response.redirect() returns a Response", async () => {
const result = await run(`
const response = Response.redirect("/login", 302);
console.log(response instanceof globalThis.Response, response.status, response.headers.get("location"));
`);
expect(result).toEqual({ stdout: "true 302 /login\n", stderr: "", exitCode: 0 });
});

test("Response.render() throws", async () => {
const result = await run(`
try {
Response.render("/404");
} catch (error) {
console.log(error.message);
}
`);
expect(result).toEqual({
stdout: "Response.render() is only available in the Bun dev server\n",
stderr: "",
exitCode: 0,
});
});

test("new Response(<jsx />) returns the element as a component", async () => {
const result = await run(`
const element = { $$typeof: Symbol.for("react.transitional.element"), type: "div", key: null, props: {} };
const response = new Response(element, { status: 201 });
console.log(response.status, response.type() === element);
`);
expect(result).toEqual({ stdout: "201 true\n", stderr: "", exitCode: 0 });
});
});
Loading