-
Notifications
You must be signed in to change notification settings - Fork 674
feat(http/unstable): accept HeadersInit for serveDir and serveFile headers
#7190
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,14 @@ import { | |
| serveFile as stableServeFile, | ||
| type ServeFileOptions as StableServeFileOptions, | ||
| } from "./file_server.ts"; | ||
| import { isRedirectStatus } from "./status.ts"; | ||
|
|
||
| function appendHeaders(target: Headers, headers: HeadersInit): void { | ||
| const normalized = new Headers(headers); | ||
| for (const [name, value] of normalized) { | ||
| target.append(name, value); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Serves the files under the given directory root (opts.fsRoot). | ||
|
|
@@ -45,31 +53,42 @@ import { | |
| * @param opts Additional options. | ||
| * @returns A response for the request. | ||
| */ | ||
| export function serveDir( | ||
| export async function serveDir( | ||
| req: Request, | ||
| opts: ServeDirOptions = {}, | ||
| ): Promise<Response> { | ||
| return stableServeDir(req, opts); | ||
| const { headers, ...rest } = opts; | ||
| const response = await stableServeDir(req, rest); | ||
| if (headers && !isRedirectStatus(response.status)) { | ||
| appendHeaders(response.headers, headers); | ||
| } | ||
| return response; | ||
| } | ||
|
|
||
| /** Interface for serveDir options. */ | ||
| export interface ServeDirOptions extends StableServeDirOptions { | ||
| export interface ServeDirOptions | ||
| extends Omit<StableServeDirOptions, "headers"> { | ||
| /** | ||
| * Also serves `.html` files without the need for specifying the extension. | ||
| * For example `foo.html` could be accessed through both `/foo` and `/foo.html`. | ||
| * | ||
| * @default {false} | ||
| */ | ||
| cleanUrls?: boolean; | ||
| /** Headers to add to each response. | ||
| * | ||
| * @default {[]} | ||
| */ | ||
| headers?: HeadersInit; | ||
| } | ||
|
|
||
| /** Interface for serveFile options. */ | ||
| export interface ServeFileOptions extends StableServeFileOptions { | ||
| /** Headers to add to each response | ||
| /** Headers to add to each response. | ||
| * | ||
| * @default {[]} | ||
| */ | ||
| headers?: string[]; | ||
| headers?: HeadersInit; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -96,15 +115,11 @@ export async function serveFile( | |
| filePath: string, | ||
| options?: ServeFileOptions, | ||
| ): Promise<Response> { | ||
| const response = await stableServeFile(req, filePath, options); | ||
| const { headers, ...rest } = options ?? {}; | ||
| const response = await stableServeFile(req, filePath, rest); | ||
|
|
||
| if (options?.headers) { | ||
| for (const header of options.headers) { | ||
| const headerSplit = header.split(":"); | ||
| const name = headerSplit[0]!; | ||
| const value = headerSplit.slice(1).join(":"); | ||
| response.headers.append(name, value); | ||
| } | ||
| if (headers) { | ||
| appendHeaders(response.headers, headers); | ||
| } | ||
|
|
||
| return response; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
as UnstableServeDirOptionscast (repeated at 5 call sites) is forced byOmit<StableServeDirOptions, "headers">+ redefine: the spread carries stable'sheaders?: string[], which isn't assignable toHeadersInit. You could avoid all the casts by typing the sharedserveDirOptionsfixture asUnstableServeDirOptions(or usingsatisfies). Minor, test-only.