Files
SnowLuma/.pnpm-store/v11/files/34/0aed51e2c83df0a69581cc7d69a405541147e72fe8a589b805ee21e39509ab916eba051977a7cdad8f7505754e11f9ff48578e70dcdad5827d2d52a432d520
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

41 lines
1.2 KiB
Plaintext

import { splitSetCookieString } from 'cookie-es'
import type { OutgoingHttpHeaders } from 'node:http2'
export type AnyHeaders =
| Headers
| HeadersInit
| Record<string, string>
| Array<[string, string]>
| OutgoingHttpHeaders
| undefined
// Helper function to convert various HeaderInit types to a Headers instance
function toHeadersInstance(init: AnyHeaders) {
if (init instanceof Headers) {
return init
} else if (Array.isArray(init)) {
return new Headers(init)
} else if (typeof init === 'object') {
return new Headers(init as HeadersInit)
} else {
return null
}
}
// Function to merge headers with proper overrides
export function mergeHeaders(...headers: Array<AnyHeaders>) {
return headers.reduce((acc: Headers, header) => {
const headersInstance = toHeadersInstance(header)
if (!headersInstance) return acc
for (const [key, value] of headersInstance.entries()) {
if (key === 'set-cookie') {
const splitCookies = splitSetCookieString(value)
splitCookies.forEach((cookie) => acc.append('set-cookie', cookie))
} else {
acc.set(key, value)
}
}
return acc
}, new Headers())
}