@tinyhttp/res
Advanced tools
Comparing version 0.1.3 to 0.1.7
# @tinyhttp/res | ||
## 0.1.7 | ||
### Patch Changes | ||
- Fix broken release | ||
- Updated dependencies [undefined] | ||
- @tinyhttp/cookie@0.2.2 | ||
- @tinyhttp/cookie-signature@0.2.1 | ||
- @tinyhttp/req@0.1.4 | ||
- @tinyhttp/send@0.1.1 | ||
## 0.1.6 | ||
### Patch Changes | ||
- @tinyhttp/req@0.1.3 | ||
## 0.1.5 | ||
### Patch Changes | ||
- add res.redirect and res.format, transfer es-accepts to @tinyhttp/accepts | ||
- Updated dependencies [undefined] | ||
- @tinyhttp/req@0.1.2 | ||
## 0.1.4 | ||
### Patch Changes | ||
- Add res.vary() method and upgrade module deps | ||
## 0.1.3 | ||
@@ -4,0 +35,0 @@ |
@@ -9,1 +9,2 @@ /// <reference types="node" /> | ||
}) => Response_1; | ||
export declare const setVaryHeader: <Request_1 extends I = I, Response_1 extends S = S>(_req: Request_1, res: Response_1) => (field: string) => Response_1; |
export * from '@tinyhttp/send'; | ||
export * from './cookie'; | ||
export * from './headers'; | ||
export type { FormatProps } from './format'; | ||
export { formatResponse } from './format'; | ||
export * from './redirect'; |
@@ -5,3 +5,6 @@ export * from '@tinyhttp/send'; | ||
import { lookup } from 'es-mime-types'; | ||
import { getRequestHeader } from '@tinyhttp/req'; | ||
import { getRequestHeader, getAccepts } from '@tinyhttp/req'; | ||
import { vary } from 'es-vary'; | ||
import { STATUS_CODES } from 'http'; | ||
import escapeHtml from 'escape-html'; | ||
@@ -64,3 +67,3 @@ const setCookie = (req, res) => (name, value, options = {}) => { | ||
// set location | ||
res.setHeader('Location', encodeURIComponent(loc)); | ||
res.setHeader('Location', loc[0] + encodeURIComponent(loc.slice(1))); | ||
return res; | ||
@@ -81,3 +84,84 @@ }; | ||
}; | ||
const setVaryHeader = (_req, res) => (field) => { | ||
vary(res, field); | ||
return res; | ||
}; | ||
export { clearCookie, getResponseHeader, setCookie, setHeader, setLinksHeader, setLocationHeader }; | ||
const normalizeType = (type) => { | ||
return ~type.indexOf('/') ? acceptParams(type) : { value: lookup(type), params: {} }; | ||
}; | ||
function acceptParams(str, index) { | ||
const parts = str.split(/ *; */); | ||
const ret = { value: parts[0], quality: 1, params: {}, originalIndex: index }; | ||
for (const part of parts) { | ||
const pms = part.split(/ *= */); | ||
if ('q' === pms[0]) { | ||
ret.quality = parseFloat(pms[1]); | ||
} | ||
else { | ||
ret.params[pms[0]] = pms[1]; | ||
} | ||
} | ||
return ret; | ||
} | ||
function normalizeTypes(types) { | ||
const ret = []; | ||
for (const type of types) { | ||
ret.push(normalizeType(type)); | ||
} | ||
return ret; | ||
} | ||
const formatResponse = (req, res, next) => (obj) => { | ||
const fn = obj.default; | ||
if (fn) | ||
delete obj.default; | ||
const keys = Object.keys(obj); | ||
const key = keys.length > 0 ? getAccepts(req)(...keys) : false; | ||
setVaryHeader(req, res)('Accept'); | ||
if (key) { | ||
res.setHeader('Content-Type', normalizeType(key).value); | ||
obj[key](req, res, next); | ||
} | ||
else if (fn) { | ||
fn(); | ||
} | ||
else { | ||
const err = new Error('Not Acceptable'); | ||
err.status = err.statusCode = 406; | ||
err.types = normalizeTypes(keys).map((o) => o.value); | ||
next(err); | ||
} | ||
return res; | ||
}; | ||
const redirect = (req, res, next) => (url, status) => { | ||
let address = url; | ||
status = status || 302; | ||
let body; | ||
address = setLocationHeader(req, res)(address).getHeader('Location'); | ||
formatResponse(req, res, next)({ | ||
text: () => { | ||
body = STATUS_CODES[status] + '. Redirecting to ' + address; | ||
}, | ||
html: () => { | ||
const u = escapeHtml(address); | ||
console.log('here'); | ||
body = `<p>${STATUS_CODES[status]}. Redirecting to <a href="${u}">${u}</a></p>`; | ||
}, | ||
default: () => { | ||
body = ''; | ||
}, | ||
}); | ||
res.setHeader('Content-Length', Buffer.byteLength(body)); | ||
res.statusCode = status; | ||
if (req.method === 'HEAD') { | ||
res.end(); | ||
} | ||
else { | ||
res.end(body); | ||
} | ||
return res; | ||
}; | ||
export { clearCookie, formatResponse, getResponseHeader, redirect, setCookie, setHeader, setLinksHeader, setLocationHeader, setVaryHeader }; |
{ | ||
"name": "@tinyhttp/res", | ||
"version": "0.1.3", | ||
"version": "0.1.7", | ||
"type": "module", | ||
@@ -40,8 +40,13 @@ "description": "response extensions for tinyhttp", | ||
"dependencies": { | ||
"@tinyhttp/cookie": "0.2.1", | ||
"@tinyhttp/cookie-signature": "0.2.0", | ||
"@tinyhttp/req": "^0.1.1", | ||
"@tinyhttp/send": "0.1.0", | ||
"es-mime-types": "^0.0.15" | ||
"@tinyhttp/cookie": "0.2.2", | ||
"@tinyhttp/cookie-signature": "0.2.1", | ||
"@tinyhttp/req": "^0.1.4", | ||
"@tinyhttp/send": "0.1.1", | ||
"es-mime-types": "^0.0.15", | ||
"es-vary": "^0.0.7", | ||
"escape-html": "^1.0.3" | ||
}, | ||
"devDependencies": { | ||
"@types/escape-html": "^1.0.0" | ||
} | ||
} |
import { IncomingMessage as I, ServerResponse as S } from 'http' | ||
import * as mime from 'es-mime-types' | ||
import { getRequestHeader } from '@tinyhttp/req' | ||
import { vary } from 'es-vary' | ||
@@ -43,3 +44,3 @@ const charsetRegExp = /;\s*charset\s*=/ | ||
// set location | ||
res.setHeader('Location', encodeURIComponent(loc)) | ||
res.setHeader('Location', loc[0] + encodeURIComponent(loc.slice(1))) | ||
return res | ||
@@ -65,1 +66,7 @@ } | ||
} | ||
export const setVaryHeader = <Request extends I = I, Response extends S = S>(_req: Request, res: Response) => (field: string) => { | ||
vary(res, field) | ||
return res | ||
} |
export * from '@tinyhttp/send' | ||
export * from './cookie' | ||
export * from './headers' | ||
export type { FormatProps } from './format' | ||
export { formatResponse } from './format' | ||
export * from './redirect' |
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
Network access
Supply chain riskThis module accesses the network.
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
25815
19
584
7
1
2
+ Addedes-vary@^0.0.7
+ Addedescape-html@^1.0.3
+ Added@tinyhttp/cookie@0.2.2(transitive)
+ Added@tinyhttp/cookie-signature@0.2.1(transitive)
+ Added@tinyhttp/etag@0.3.1(transitive)
+ Added@tinyhttp/send@0.1.1(transitive)
+ Addedes-vary@0.0.7(transitive)
+ Addedescape-html@1.0.3(transitive)
- Removed@tinyhttp/cookie@0.2.1(transitive)
- Removed@tinyhttp/cookie-signature@0.2.0(transitive)
- Removed@tinyhttp/etag@0.3.0(transitive)
- Removed@tinyhttp/send@0.1.0(transitive)
Updated@tinyhttp/cookie@0.2.2
Updated@tinyhttp/req@^0.1.4
Updated@tinyhttp/send@0.1.1