|
|
import type { OutgoingHttpHeaders } from 'http' |
|
|
import { |
|
|
NEXT_INTERCEPTION_MARKER_PREFIX, |
|
|
NEXT_QUERY_PARAM_PREFIX, |
|
|
} from '../../lib/constants' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function fromNodeOutgoingHttpHeaders( |
|
|
nodeHeaders: OutgoingHttpHeaders |
|
|
): Headers { |
|
|
const headers = new Headers() |
|
|
for (let [key, value] of Object.entries(nodeHeaders)) { |
|
|
const values = Array.isArray(value) ? value : [value] |
|
|
for (let v of values) { |
|
|
if (typeof v === 'undefined') continue |
|
|
if (typeof v === 'number') { |
|
|
v = v.toString() |
|
|
} |
|
|
|
|
|
headers.append(key, v) |
|
|
} |
|
|
} |
|
|
return headers |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function splitCookiesString(cookiesString: string) { |
|
|
var cookiesStrings = [] |
|
|
var pos = 0 |
|
|
var start |
|
|
var ch |
|
|
var lastComma |
|
|
var nextStart |
|
|
var cookiesSeparatorFound |
|
|
|
|
|
function skipWhitespace() { |
|
|
while (pos < cookiesString.length && /\s/.test(cookiesString.charAt(pos))) { |
|
|
pos += 1 |
|
|
} |
|
|
return pos < cookiesString.length |
|
|
} |
|
|
|
|
|
function notSpecialChar() { |
|
|
ch = cookiesString.charAt(pos) |
|
|
|
|
|
return ch !== '=' && ch !== ';' && ch !== ',' |
|
|
} |
|
|
|
|
|
while (pos < cookiesString.length) { |
|
|
start = pos |
|
|
cookiesSeparatorFound = false |
|
|
|
|
|
while (skipWhitespace()) { |
|
|
ch = cookiesString.charAt(pos) |
|
|
if (ch === ',') { |
|
|
|
|
|
lastComma = pos |
|
|
pos += 1 |
|
|
|
|
|
skipWhitespace() |
|
|
nextStart = pos |
|
|
|
|
|
while (pos < cookiesString.length && notSpecialChar()) { |
|
|
pos += 1 |
|
|
} |
|
|
|
|
|
|
|
|
if (pos < cookiesString.length && cookiesString.charAt(pos) === '=') { |
|
|
|
|
|
cookiesSeparatorFound = true |
|
|
|
|
|
pos = nextStart |
|
|
cookiesStrings.push(cookiesString.substring(start, lastComma)) |
|
|
start = pos |
|
|
} else { |
|
|
|
|
|
|
|
|
pos = lastComma + 1 |
|
|
} |
|
|
} else { |
|
|
pos += 1 |
|
|
} |
|
|
} |
|
|
|
|
|
if (!cookiesSeparatorFound || pos >= cookiesString.length) { |
|
|
cookiesStrings.push(cookiesString.substring(start, cookiesString.length)) |
|
|
} |
|
|
} |
|
|
|
|
|
return cookiesStrings |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function toNodeOutgoingHttpHeaders( |
|
|
headers: Headers |
|
|
): OutgoingHttpHeaders { |
|
|
const nodeHeaders: OutgoingHttpHeaders = {} |
|
|
const cookies: string[] = [] |
|
|
if (headers) { |
|
|
for (const [key, value] of headers.entries()) { |
|
|
if (key.toLowerCase() === 'set-cookie') { |
|
|
|
|
|
|
|
|
|
|
|
cookies.push(...splitCookiesString(value)) |
|
|
nodeHeaders[key] = cookies.length === 1 ? cookies[0] : cookies |
|
|
} else { |
|
|
nodeHeaders[key] = value |
|
|
} |
|
|
} |
|
|
} |
|
|
return nodeHeaders |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function validateURL(url: string | URL): string { |
|
|
try { |
|
|
return String(new URL(String(url))) |
|
|
} catch (error: any) { |
|
|
throw new Error( |
|
|
`URL is malformed "${String( |
|
|
url |
|
|
)}". Please use only absolute URLs - https://nextjs.org/docs/messages/middleware-relative-urls`, |
|
|
{ cause: error } |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function normalizeNextQueryParam(key: string): null | string { |
|
|
const prefixes = [NEXT_QUERY_PARAM_PREFIX, NEXT_INTERCEPTION_MARKER_PREFIX] |
|
|
for (const prefix of prefixes) { |
|
|
if (key !== prefix && key.startsWith(prefix)) { |
|
|
return key.substring(prefix.length) |
|
|
} |
|
|
} |
|
|
return null |
|
|
} |
|
|
|