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: 5 additions & 0 deletions .changeset/hungry-crews-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"miniflare": patch
---

fix: api proxy preserve multiple Set-Cookie headers
19 changes: 17 additions & 2 deletions packages/miniflare/src/workers/core/devalue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ export const structuredSerializableReducers: ReducersRevivers = {
];
}
},
RegExp(value) {
if (value instanceof RegExp) {
const { source, flags } = value;
const encoded = Buffer.from(source).toString("base64");
return flags ? ["RegExp", encoded, flags] : ["RegExp", encoded];
}
},
Error(value) {
for (const ctor of ALLOWED_ERROR_CONSTRUCTORS) {
if (value instanceof ctor && value.name === ctor.name) {
Expand Down Expand Up @@ -97,6 +104,14 @@ export const structuredSerializableRevivers: ReducersRevivers = {
if ("BYTES_PER_ELEMENT" in ctor) length /= ctor.BYTES_PER_ELEMENT;
return new ctor(buffer as ArrayBuffer, byteOffset, length);
},
RegExp(value) {
assert(Array.isArray(value));
const [name, encoded, flags] = value;
assert(typeof name === "string");
assert(typeof encoded === "string");
const source = Buffer.from(encoded, "base64").toString("utf-8");
return new RegExp(source, flags);
},
Error(value) {
assert(Array.isArray(value));
const [name, message, stack, cause] = value as unknown[];
Expand Down Expand Up @@ -134,7 +149,7 @@ export function createHTTPReducers(
): ReducersRevivers {
return {
Headers(val) {
if (val instanceof impl.Headers) return Object.fromEntries(val);
if (val instanceof impl.Headers) return [...val.entries()];
},
Request(val) {
if (val instanceof impl.Request) {
Expand All @@ -154,7 +169,7 @@ export function createHTTPRevivers<RS>(
return {
Headers(value) {
assert(typeof value === "object" && value !== null);
return new impl.Headers(value as Record<string, string>);
return new impl.Headers(value as string[][]);
},
Request(value) {
assert(Array.isArray(value));
Expand Down
50 changes: 50 additions & 0 deletions packages/miniflare/test/workers/core/serialize.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import test from "ava";
import { parse, stringify } from "devalue";
import {
createHTTPReducers,
createHTTPRevivers,
structuredSerializableReducers,
structuredSerializableRevivers,
} from "miniflare";
import { NODE_PLATFORM_IMPL } from "../../../src/plugins/core/proxy/types";

test("serialize Headers object consisting of multiple Set-Cookie headers", (t) => {
const impl = NODE_PLATFORM_IMPL;

const headers = new impl.Headers([
["content-type", "application/json"],
["authorization", "Bearer token"],
]);
headers.append("Set-Cookie", "cookie1=value_for_cookie_1; Path=/; HttpOnly;");
headers.append("Set-Cookie", "cookie2=value_for_cookie_2; Path=/; HttpOnly;");

const serialized = stringify(headers, createHTTPReducers(impl));
const deserialized = parse(serialized, createHTTPRevivers(impl));
t.true(deserialized instanceof impl.Headers);
t.is(deserialized.get("content-type"), "application/json");
t.is(deserialized.get("authorization"), "Bearer token");
t.deepEqual(deserialized.getSetCookie(), [
"cookie1=value_for_cookie_1; Path=/; HttpOnly;",
"cookie2=value_for_cookie_2; Path=/; HttpOnly;",
]);
});

test("serialize RegExp object consisting of only ascii chars", (t) => {
const input = new RegExp(/HelloWorld/);

const serialized = stringify(input, structuredSerializableReducers);
t.is(serialized, '[["RegExp",1],[2,3],"RegExp","SGVsbG9Xb3JsZA=="]');

const deserialized = parse(serialized, structuredSerializableRevivers);
t.deepEqual(deserialized, input);
});

test("serialize RegExp object containing non-ascii chars", (t) => {
const input = new RegExp(/こんにちは/);

const serialized = stringify(input, structuredSerializableReducers);
t.is(serialized, '[["RegExp",1],[2,3],"RegExp","44GT44KT44Gr44Gh44Gv"]');

const deserialized = parse(serialized, structuredSerializableRevivers);
t.deepEqual(deserialized, input);
});
Loading