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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(writeEarlyHints): handle object-format early hints #202

Merged
merged 7 commits into from Oct 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 32 additions & 9 deletions src/utils/response.ts
@@ -1,4 +1,4 @@
import type { OutgoingMessage } from 'http'
import type { OutgoingMessage, ServerResponse } from 'http'
import { createError } from '../error'
import type { H3Event } from '../event'
import { MIMES } from './consts'
Expand Down Expand Up @@ -86,19 +86,42 @@ export function sendStream (event: H3Event, data: any): Promise<void> {
})
}

export function writeEarlyHints (event: H3Event, links: string | string[], callback?: () => void) {
const noop = () => {}
export function writeEarlyHints (event: H3Event, hints: string | string[] | Record<string, string | string[]>, cb: () => void = noop) {
if (!event.res.socket && !('writeEarlyHints' in event.res)) {
if (callback) {
callback()
}
cb()
return
}

// Normalize if string or string[] is provided
if (typeof hints === 'string' || Array.isArray(hints)) {
hints = { link: hints }
}

if (hints.link) {
hints.link = Array.isArray(hints.link) ? hints.link : hints.link.split(',')
// TODO: remove when https://github.com/nodejs/node/pull/44874 is released
hints.link = hints.link.map(l => l.trim().replace(/; crossorigin/g, ''))
}

if ('writeEarlyHints' in event.res) {
// @ts-expect-error native node 18 implementation
return event.res.writeEarlyHints(links, callback)
return event.res.writeEarlyHints(hints, cb)
}

const headers: [string, string | string[]][] = Object.entries(hints).map(e => [e[0].toLowerCase(), e[1]])
if (!headers.length) {
cb()
return
}

const _links = Array.isArray(links) ? links : [links]
event.res.socket!.write(`HTTP/1.1 103 Early Hints\r\nLink: ${_links.join('\r\n')}\r\n\r\n`, 'utf-8', callback)
let hint = 'HTTP/1.1 103 Early Hints'
if (hints.link) {
pi0 marked this conversation as resolved.
Show resolved Hide resolved
hint += `\r\nLink: ${(hints.link as string[]).join('\r\n')}`
}

for (const [header, value] of headers) {
if (header === 'link') { continue }
hint += `\r\n${header}: ${value}`
}
(event.res as ServerResponse).socket!.write(`${hint}\r\n\r\n`, 'utf-8', cb)
}