{"record":{"id":"3a79227e32fe4ffd","repo":"denoland/deno","slug":"first-argument-to-respondwith-must-be-a-response-3a7922","errorCode":null,"errorMessage":"First argument to 'respondWith' must be a Response constructed via the Response constructor in this realm","messagePattern":"First argument to 'respondWith' must be a Response constructed via the Response constructor in this realm","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/http/01_http.js","lineNumber":213,"sourceCode":"  writeStreamRid,\n) {\n  return async function respondWith(resp) {\n    try {\n      resp = await resp;\n      if (!(ObjectPrototypeIsPrototypeOf(ResponsePrototype, resp))) {\n        throw new TypeError(\n          \"First argument to 'respondWith' must be a Response or a promise resolving to a Response\",\n        );\n      }\n\n      // The Response prototype check above passes for Response-like objects\n      // that don't carry the internal slot (e.g. `Object.create(Response.prototype)`\n      // or a polyfilled/foreign-realm Response). Reject those here instead of\n      // crashing later on `innerResp.body`. Mirrors the Deno.serve guard\n      // added in #34416.\n      const innerResp = toInnerResponse(resp);\n      if (innerResp === undefined) {\n        throw new TypeError(\n          \"First argument to 'respondWith' must be a Response constructed via the Response constructor in this realm\",\n        );\n      }\n\n      // If response body length is known, it will be sent synchronously in a\n      // single op, in other case a \"response body\" resource will be created and\n      // we'll be streaming it.\n      /** @type {ReadableStream<Uint8Array> | Uint8Array | null} */\n      let respBody = null;\n      if (innerResp.body !== null) {\n        if (innerResp.body.unusable()) {\n          throw new TypeError(\"Body is unusable\");\n        }\n        if (\n          ObjectPrototypeIsPrototypeOf(\n            ReadableStreamPrototype,\n            innerResp.body.streamOrStatic,\n          )","sourceCodeStart":195,"sourceCodeEnd":231,"githubUrl":"https://github.com/denoland/deno/blob/89f33cbef296a2b287f323d42de54c871fa69c77/ext/http/01_http.js#L195-L231","documentation":"A second, stricter guard after the prototype check: toInnerResponse(resp) must return an inner response, i.e. the object must carry Deno's internal Response slot, which only objects built by this realm's Response constructor have. Objects that fake it (Object.create(Response.prototype), Response subclasses instantiated in another realm/worker, or polyfilled Responses) pass the prototype test but lack the internal slot and are rejected here instead of crashing on innerResp.body.","triggerScenarios":"Object.create(Response.prototype) passed to respondWith; a Response created inside a Worker or vm-like realm and shipped to the main thread; a Response polyfill whose instances share Response.prototype but never went through the constructor; cloned structurally via property copying.","commonSituations":"Cross-realm code (node compat shims, sandboxed evaluators, import maps pulling a fetch polyfill); libraries that wrap/subclass Response from a different Deno instance; defensive Object.create tricks to avoid constructor validation.","solutions":["Construct the response in the same realm with the real constructor: new Response(body, init).","When receiving a value from another realm, rebuild it: httpConn.respondWith(new Response(await foreignResp.body, foreignResp)).","Remove polyfills/shims for Response from the serving path (don't polyfill built-ins when running under Deno)."],"exampleFix":"// before\nconst fake = Object.create(Response.prototype);\nfake.status = 200;\nhttpConn.respondWith(fake);\n\n// after\nhttpConn.respondWith(new Response(\"ok\"));","handlingStrategy":"validation","validationCode":"function isRealmResponse(v: unknown): boolean {\n  if (!(v instanceof Response)) return false;\n  try { return Object.getOwnPropertySymbols(v).length > 0 || new Response(v.body, v).body !== undefined || true; } catch { return false; }\n}","typeGuard":"function isRealResponse(v: unknown): v is Response { try { return v instanceof Response && !(Symbol.for(\"deno.inner\") in (v as object) && (v as any)[Symbol.for(\"deno.inner\")] === undefined); } catch { return false; } }","tryCatchPattern":"try { await httpConn.respondWith(resp); } catch (e) { if (e instanceof TypeError && e.message.includes(\"in this realm\")) { await httpConn.respondWith(new Response(null, { status: resp.status ?? 200, headers: resp.headers })); return; } throw e; }","preventionTips":["Construct responses with the global Response of the running realm - never Object.create(Response.prototype).","Rebuild cross-realm/polyfilled responses: new Response(foreign.body, foreign).","Don't ship Response polyfills under Deno; map imports so 'response' resolves to the builtin."],"tags":["http","servehttp","response","realms","polyfill"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}