swc/bundler/tests/.cache/deno/ca43e799c5ad63b2fd7b34f3ea6ab92c8365e006.ts
강동윤 f8aa0509ce
fix(bundler): Fix stack overflow (#2080)
swc_bundler:
 - Prevent infinite recursions. (#1963)
2021-08-15 02:37:31 +00:00

50 lines
1.5 KiB
TypeScript

// Loaded from https://deno.land/x/tinyhttp@0.1.18/extensions/res/download.ts
import { contentDisposition } from 'https://esm.sh/@tinyhttp/content-disposition'
import { SendFileOptions, sendFile } from './send/sendFile.ts'
import { extname } from 'https://deno.land/std@0.101.0/path/mod.ts'
import { setContentType, setHeader } from './headers.ts'
import { Req, Res } from '../../deps.ts'
export type DownloadOptions = SendFileOptions &
Partial<{
headers: Record<string, unknown>
}>
export const download = <Request extends Req = Req, Response extends Res = Res>(req: Request, res: Response) => (
path: string,
filename?: string,
options: DownloadOptions = {}
): Response => {
const name: string | null = filename as string
let opts: DownloadOptions = options
// set Content-Disposition when file is sent
const headers: Record<string, string> = {
'Content-Disposition': contentDisposition(name || path)
}
// merge user-provided headers
if (opts.headers) {
for (const key of Object.keys(opts.headers)) {
if (key.toLowerCase() !== 'content-disposition') headers[key] = opts.headers[key]
}
}
// merge user-provided options
opts = { ...opts, headers }
// send file
return sendFile<Request, Response>(req, res)(path, opts) as Response
}
export const attachment = <Response extends Res = Res>(res: Response) => (filename?: string): Response => {
if (filename) setContentType(res)(extname(filename))
setHeader(res)('Content-Disposition', contentDisposition(filename))
return res
}