Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.7.0 #13

Merged
merged 21 commits into from
Apr 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix Headers output
  • Loading branch information
lxsmnsyc committed Apr 9, 2023
commit 3ce968b4dc4f9d5f733df38e3cd3f0da4005b40b
10 changes: 5 additions & 5 deletions packages/seroval/src/tree/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,19 +359,19 @@ async function generateHeadersNode(
current: Headers,
): Promise<SerovalHeadersNode> {
assert(ctx.features & Feature.WebAPI, 'Unsupported type "Headers"');
const items: [string, string][] = [];
const items: Record<string, string> = {};
current.forEach((value, key) => {
items.push([key, value]);
items[key] = value;
});
return {
t: SerovalNodeType.Headers,
i: id,
s: undefined,
l: items.length,
l: undefined,
c: undefined,
m: undefined,
d: undefined,
a: await generateNodeList(ctx, items),
d: await generateProperties(ctx, items),
a: undefined,
f: undefined,
b: undefined,
};
Expand Down
13 changes: 5 additions & 8 deletions packages/seroval/src/tree/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,11 @@ function deserializeHeaders(
node: SerovalHeadersNode,
) {
const result = assignIndexedValue(ctx, node.i, new Headers());
let item: SerovalNode;
let entry: [string, string];
for (let i = 0, len = node.l; i < len; i++) {
item = node.a[i];
if (item) {
entry = deserializeTree(ctx, item) as [string, string];
result.append.apply(result, entry);
}
for (let i = 0, len = node.d.s; i < len; i++) {
result.set(
deserializeString(node.d.k[i]),
deserializeTree(ctx, node.d.v[i]) as string,
);
}
return result;
}
Expand Down
5 changes: 2 additions & 3 deletions packages/seroval/src/tree/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,7 @@ function isIndexedValueInStack(

type SerovalNodeListNode =
| SerovalArrayNode
| SerovalIterableNode
| SerovalHeadersNode;
| SerovalIterableNode;

function serializeNodeList(
ctx: SerializationContext,
Expand Down Expand Up @@ -670,7 +669,7 @@ function serializeHeaders(
ctx: SerializationContext,
node: SerovalHeadersNode,
) {
return assignIndexedValue(ctx, node.i, 'new Headers(' + serializeNodeList(ctx, node) + ')');
return assignIndexedValue(ctx, node.i, 'new Headers(' + serializeProperties(ctx, node.i, node.d) + ')');
}

function serializeFormDataEntries(
Expand Down
10 changes: 5 additions & 5 deletions packages/seroval/src/tree/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,19 +315,19 @@ function generateHeadersNode(
current: Headers,
): SerovalHeadersNode {
assert(ctx.features & Feature.WebAPI, 'Unsupported type "Headers"');
const items: [string, string][] = [];
const items: Record<string, string> = {};
current.forEach((value, key) => {
items.push([key, value]);
items[key] = value;
});
return {
t: SerovalNodeType.Headers,
i: id,
s: undefined,
l: items.length,
l: undefined,
c: undefined,
m: undefined,
d: undefined,
a: generateNodeList(ctx, items),
d: generateProperties(ctx, items),
a: undefined,
f: undefined,
b: undefined,
};
Expand Down
3 changes: 1 addition & 2 deletions packages/seroval/src/tree/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,7 @@ export interface SerovalFileNode extends SerovalBaseNode {
export interface SerovalHeadersNode extends SerovalBaseNode {
t: SerovalNodeType.Headers;
i: number;
l: number;
a: SerovalNode[];
d: SerovalObjectRecordNode;
}

export interface SerovalFormDataNode extends SerovalBaseNode {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Vitest Snapshot v1

exports[`Headers > serialize > supports Headers 1`] = `"new Headers([[\\"content-type\\",\\"text/plain\\"]])"`;
exports[`Headers > serialize > supports Headers 1`] = `"new Headers({\\"content-type\\":\\"text/plain\\"})"`;

exports[`Headers > serializeAsync > supports Headers 1`] = `"Promise.resolve(new Headers([[\\"content-type\\",\\"text/plain\\"]]))"`;
exports[`Headers > serializeAsync > supports Headers 1`] = `"Promise.resolve(new Headers({\\"content-type\\":\\"text/plain\\"}))"`;

exports[`Headers > toJSON > supports Headers 1`] = `"{\\"t\\":{\\"t\\":32,\\"i\\":0,\\"l\\":1,\\"a\\":[{\\"t\\":15,\\"i\\":1,\\"l\\":2,\\"a\\":[{\\"t\\":1,\\"s\\":\\"content-type\\"},{\\"t\\":1,\\"s\\":\\"text/plain\\"}]}]},\\"r\\":0,\\"i\\":false,\\"f\\":16383,\\"m\\":[]}"`;
exports[`Headers > toJSON > supports Headers 1`] = `"{\\"t\\":{\\"t\\":32,\\"i\\":0,\\"d\\":{\\"k\\":[\\"content-type\\"],\\"v\\":[{\\"t\\":1,\\"s\\":\\"text/plain\\"}],\\"s\\":1}},\\"r\\":0,\\"i\\":false,\\"f\\":16383,\\"m\\":[]}"`;

exports[`Headers > toJSONAsync > supports Headers 1`] = `"{\\"t\\":{\\"t\\":18,\\"i\\":0,\\"f\\":{\\"t\\":32,\\"i\\":1,\\"l\\":1,\\"a\\":[{\\"t\\":15,\\"i\\":2,\\"l\\":2,\\"a\\":[{\\"t\\":1,\\"s\\":\\"content-type\\"},{\\"t\\":1,\\"s\\":\\"text/plain\\"}]}]}},\\"r\\":0,\\"i\\":false,\\"f\\":16383,\\"m\\":[]}"`;
exports[`Headers > toJSONAsync > supports Headers 1`] = `"{\\"t\\":{\\"t\\":18,\\"i\\":0,\\"f\\":{\\"t\\":32,\\"i\\":1,\\"d\\":{\\"k\\":[\\"content-type\\"],\\"v\\":[{\\"t\\":1,\\"s\\":\\"text/plain\\"}],\\"s\\":1}}},\\"r\\":0,\\"i\\":false,\\"f\\":16383,\\"m\\":[]}"`;