MCPcopy
hub / github.com/tinyhttp/tinyhttp / download

Function download

packages/res/src/download.ts:19–56  ·  view source on GitHub ↗
(req: Request, res: Response)

Source from the content-addressed store, hash-verified

17type Res = Pick<S, 'setHeader' | 'statusCode' | 'writeHead'> & NodeJS.WritableStream
18
19export const download = <Request extends Req = Req, Response extends Res = Res>(req: Request, res: Response) => (
20 path: string,
21 filename?: string | Callback,
22 options?: DownloadOptions | Callback,
23 cb?: Callback
24): Response => {
25 let done = cb
26 let name = filename as string
27 let opts = (options || null) as DownloadOptions
28
29 // support function as second or third arg
30 if (typeof filename === 'function') {
31 done = filename
32 name = null
33 } else if (typeof options === 'function') {
34 done = options
35 opts = null
36 }
37
38 // set Content-Disposition when file is sent
39 const headers = {
40 'Content-Disposition': contentDisposition(name || path)
41 }
42
43 // merge user-provided headers
44 if (opts && opts.headers) {
45 for (const key of Object.keys(opts.headers)) {
46 if (key.toLowerCase() !== 'content-disposition') headers[key] = opts.headers[key]
47 }
48 }
49
50 // merge user-provided options
51 opts = { ...opts, headers }
52
53 // send file
54
55 return sendFile(req, res)(opts.root ? path : resolve(path), opts, done || (() => undefined))
56}
57
58export const attachment = <Response extends Pick<S, 'getHeader' | 'setHeader'> = Pick<S, 'getHeader' | 'setHeader'>>(
59 res: Response

Callers 2

extendMiddlewareFunction · 0.90
res.test.tsFile · 0.90

Calls 2

contentDispositionFunction · 0.90
sendFileFunction · 0.90

Tested by

no test coverage detected