Skip to content

Commit ef1b5a7

Browse files
committed
review
1 parent a9e5ff3 commit ef1b5a7

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

packages/open-next/src/core/routing/util.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Readable } from "node:stream";
55
import { BuildId, HtmlPages, NextConfig } from "config/index.js";
66
import type { IncomingMessage } from "http/index.js";
77
import { OpenNextNodeResponse } from "http/openNextResponse.js";
8-
import { parseHeaders } from "http/util.js";
8+
import { getQueryFromIterator, parseHeaders } from "http/util.js";
99
import type {
1010
FunctionsConfigManifest,
1111
MiddlewareManifest,
@@ -38,21 +38,11 @@ export function isExternal(url?: string, host?: string) {
3838
export function convertFromQueryString(query: string) {
3939
if (query === "") return {};
4040
const queryParts = query.split("&");
41-
return queryParts.reduce(
42-
(acc, part) => {
43-
const [key, value] = part.split("=");
44-
if (key in acc) {
45-
if (Array.isArray(acc[key])) {
46-
acc[key].push(value);
47-
} else {
48-
acc[key] = [acc[key], value];
49-
}
50-
} else {
51-
acc[key] = value;
52-
}
53-
return acc;
54-
},
55-
{} as Record<string, string | string[]>,
41+
return getQueryFromIterator(
42+
queryParts.map((p) => {
43+
const [key, value] = p.split("=");
44+
return [key, value] as const;
45+
}),
5646
);
5747
}
5848

packages/open-next/src/http/util.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,25 @@ export function parseCookies(
3939
? cookies.split(/(?<!Expires=\w+),/i).map((c) => c.trim())
4040
: cookies;
4141
}
42+
43+
/**
44+
*
45+
* Get the query object from an iterable of [key, value] pairs
46+
* @param it - The iterable of [key, value] pairs
47+
* @returns The query object
48+
*/
49+
export function getQueryFromIterator(it: Iterable<[string, string]>) {
50+
const query: Record<string, string | string[]> = {};
51+
for (const [key, value] of it) {
52+
if (key in query) {
53+
if (Array.isArray(query[key])) {
54+
query[key].push(value);
55+
} else {
56+
query[key] = [query[key], value];
57+
}
58+
} else {
59+
query[key] = value;
60+
}
61+
}
62+
return query;
63+
}

packages/open-next/src/overrides/converters/utils.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { getQueryFromIterator } from "http/util.js";
2+
13
export function removeUndefinedFromQuery(
24
query: Record<string, string | string[] | undefined>,
35
) {
@@ -29,17 +31,5 @@ export function extractHostFromHeaders(
2931
* @returns
3032
*/
3133
export function getQueryFromSearchParams(searchParams: URLSearchParams) {
32-
const query: Record<string, string | string[]> = {};
33-
for (const [key, value] of searchParams.entries()) {
34-
if (key in query) {
35-
if (Array.isArray(query[key])) {
36-
query[key].push(value);
37-
} else {
38-
query[key] = [query[key], value];
39-
}
40-
} else {
41-
query[key] = value;
42-
}
43-
}
44-
return query;
34+
return getQueryFromIterator(searchParams.entries());
4535
}

0 commit comments

Comments
 (0)