@tinyhttp/res
Advanced tools
Comparing version 0.4.0 to 0.4.1
# @tinyhttp/res | ||
## 0.4.1 | ||
### Patch Changes | ||
- Small bugfixes to @tinyhttp/send | ||
- Updated dependencies [undefined] | ||
- @tinyhttp/send@0.4.1 | ||
## 0.4.0 | ||
@@ -4,0 +12,0 @@ |
@@ -7,1 +7,2 @@ export * from '@tinyhttp/send'; | ||
export * from './redirect'; | ||
export * from './download'; |
@@ -0,1 +1,2 @@ | ||
import { sendFile } from '@tinyhttp/send'; | ||
export * from '@tinyhttp/send'; | ||
@@ -9,2 +10,4 @@ import { serialize } from '@tinyhttp/cookie'; | ||
import escapeHtml from 'escape-html'; | ||
import { contentDisposition } from '@tinyhttp/content-disposition'; | ||
import { resolve, extname } from 'path'; | ||
@@ -169,2 +172,47 @@ const setCookie = (req, res) => (name, value, options = {}) => { | ||
export { clearCookie, formatResponse, getResponseHeader, redirect, setContentType, setCookie, setHeader, setLinksHeader, setLocationHeader, setVaryHeader }; | ||
const download = (req, res) => (path, filename, options, cb) => { | ||
let done = cb; | ||
let name = filename; | ||
let opts = (options || null); | ||
// support function as second or third arg | ||
if (typeof filename === 'function') { | ||
done = filename; | ||
name = null; | ||
opts = null; | ||
} | ||
else if (typeof options === 'function') { | ||
done = options; | ||
opts = null; | ||
} | ||
// set Content-Disposition when file is sent | ||
const headers = { | ||
'Content-Disposition': contentDisposition(name || path), | ||
}; | ||
// merge user-provided headers | ||
if (opts && 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 = Object.create(opts); | ||
opts.headers = headers; | ||
// Resolve the full path for sendFile | ||
const fullPath = resolve(path); | ||
const noop = () => { | ||
return; | ||
}; | ||
// send file | ||
return sendFile(req, res)(fullPath, opts, done || noop); | ||
}; | ||
const attachment = (req, res) => (filename) => { | ||
if (filename) { | ||
setContentType(req, res)(extname(filename)); | ||
} | ||
setHeader(req, res)('Content-Disposition', contentDisposition(filename)); | ||
return res; | ||
}; | ||
export { attachment, clearCookie, download, formatResponse, getResponseHeader, redirect, setContentType, setCookie, setHeader, setLinksHeader, setLocationHeader, setVaryHeader }; |
{ | ||
"name": "@tinyhttp/res", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"type": "module", | ||
@@ -40,6 +40,7 @@ "description": "response extensions for tinyhttp", | ||
"dependencies": { | ||
"@tinyhttp/content-disposition": "^0.4.0", | ||
"@tinyhttp/cookie": "0.4.0", | ||
"@tinyhttp/cookie-signature": "0.4.0", | ||
"@tinyhttp/req": "0.4.0", | ||
"@tinyhttp/send": "0.4.0", | ||
"@tinyhttp/send": "0.4.1", | ||
"es-mime-types": "^0.0.16", | ||
@@ -46,0 +47,0 @@ "es-vary": "^0.0.7", |
@@ -7,1 +7,2 @@ export * from '@tinyhttp/send' | ||
export * from './redirect' | ||
export * from './download' |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33740
21
764
8
+ Added@tinyhttp/content-disposition@0.4.1(transitive)
+ Added@tinyhttp/send@0.4.1(transitive)
- Removed@tinyhttp/send@0.4.0(transitive)
Updated@tinyhttp/send@0.4.1