@johntalton/http-util 5.1.5 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -7
- package/package.json +11 -5
- package/src/body.js +8 -8
- package/src/{response/defs.js → defs.js} +2 -1
- package/src/{accept-encoding.js → headers/accept-encoding.js} +2 -2
- package/src/{accept-language.js → headers/accept-language.js} +4 -5
- package/src/{accept.js → headers/accept.js} +18 -26
- package/src/headers/client-hints.js +81 -0
- package/src/{conditional.js → headers/conditional.js} +21 -12
- package/src/{content-disposition.js → headers/content-disposition.js} +26 -25
- package/src/{content-range.js → headers/content-range.js} +1 -1
- package/src/headers/content-type.js +101 -0
- package/src/{fetch-metadata.js → headers/fetch-metadata.js} +0 -1
- package/src/{forwarded.js → headers/forwarded.js} +6 -23
- package/src/{index.js → headers/index.js} +4 -1
- package/src/{multipart.js → headers/multipart.js} +6 -5
- package/src/{preference.js → headers/preference.js} +3 -15
- package/src/{range.js → headers/range.js} +1 -1
- package/src/{server-timing.js → headers/server-timing.js} +2 -2
- package/src/headers/strict-transport-security.js +38 -0
- package/src/{accept-util.js → headers/util/accept-util.js} +8 -14
- package/src/headers/util/index.js +7 -0
- package/src/headers/util/kvp.js +79 -0
- package/src/headers/util/mime.js +77 -0
- package/src/headers/util/whitespace.js +6 -0
- package/src/{www-authenticate.js → headers/www-authenticate.js} +1 -1
- package/src/response/{accepted.js → 2xx/accepted.js} +2 -2
- package/src/response/{bytes.js → 2xx/bytes.js} +5 -5
- package/src/response/{created.js → 2xx/created.js} +4 -4
- package/src/response/{json.js → 2xx/json.js} +5 -5
- package/src/response/{no-content.js → 2xx/no-content.js} +4 -4
- package/src/response/{partial-content.js → 2xx/partial-content.js} +9 -9
- package/src/response/{preflight.js → 2xx/preflight.js} +4 -4
- package/src/response/{sse.js → 2xx/sse.js} +2 -2
- package/src/response/{trace.js → 2xx/trace.js} +3 -3
- package/src/response/{moved-permanently.js → 3xx/moved-permanently.js} +2 -2
- package/src/response/{multiple-choices.js → 3xx/multiple-choices.js} +2 -3
- package/src/response/{not-modified.js → 3xx/not-modified.js} +7 -7
- package/src/response/{permanent-redirect.js → 3xx/permanent-redirect.js} +2 -2
- package/src/response/{see-other.js → 3xx/see-other.js} +2 -2
- package/src/response/{temporary-redirect.js → 3xx/temporary-redirect.js} +2 -2
- package/src/response/{conflict.js → 4xx/conflict.js} +2 -2
- package/src/response/{content-too-large.js → 4xx/content-too-large.js} +2 -2
- package/src/response/{forbidden.js → 4xx/forbidden.js} +3 -2
- package/src/response/{gone.js → 4xx/gone.js} +2 -2
- package/src/response/{im-a-teapot.js → 4xx/im-a-teapot.js} +2 -2
- package/src/response/{not-acceptable.js → 4xx/not-acceptable.js} +3 -3
- package/src/response/{not-allowed.js → 4xx/not-allowed.js} +2 -2
- package/src/response/{not-found.js → 4xx/not-found.js} +3 -3
- package/src/response/{precondition-failed.js → 4xx/precondition-failed.js} +2 -2
- package/src/response/{range-not-satisfiable.js → 4xx/range-not-satisfiable.js} +4 -4
- package/src/response/{timeout.js → 4xx/timeout.js} +2 -2
- package/src/response/{too-many-requests.js → 4xx/too-many-requests.js} +5 -5
- package/src/response/{unauthorized.js → 4xx/unauthorized.js} +4 -4
- package/src/response/{unprocessable.js → 4xx/unprocessable.js} +2 -2
- package/src/response/{unsupported-media.js → 4xx/unsupported-media.js} +3 -3
- package/src/response/{error.js → 5xx/error.js} +3 -3
- package/src/response/{insufficient-storage.js → 5xx/insufficient-storage.js} +2 -2
- package/src/response/{not-implemented.js → 5xx/not-implemented.js} +4 -4
- package/src/response/{unavailable.js → 5xx/unavailable.js} +4 -4
- package/src/response/header-util.js +2 -2
- package/src/response/index.js +35 -35
- package/src/response/response.js +34 -34
- package/src/response/send-util.js +11 -13
- package/src/content-type.js +0 -148
- /package/src/{cache-control.js → headers/cache-control.js} +0 -0
- /package/src/{clear-site-data.js → headers/clear-site-data.js} +0 -0
- /package/src/{link.js → headers/link.js} +0 -0
- /package/src/{rate-limit.js → headers/rate-limit.js} +0 -0
- /package/src/{quote.js → headers/util/quote.js} +0 -0
package/src/content-type.js
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
export const MIME_TYPE_JSON = 'application/json'
|
|
3
|
-
export const MIME_TYPE_TEXT = 'text/plain'
|
|
4
|
-
export const MIME_TYPE_EVENT_STREAM = 'text/event-stream'
|
|
5
|
-
export const MIME_TYPE_XML = 'application/xml'
|
|
6
|
-
export const MIME_TYPE_URL_FORM_DATA = 'application/x-www-form-urlencoded'
|
|
7
|
-
export const MIME_TYPE_MULTIPART_FORM_DATA = 'multipart/form-data'
|
|
8
|
-
export const MIME_TYPE_MULTIPART_RANGE = 'multipart/byteranges'
|
|
9
|
-
export const MIME_TYPE_OCTET_STREAM = 'application/octet-stream'
|
|
10
|
-
export const MIME_TYPE_MESSAGE_HTTP = 'message/http'
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
export const KNOWN_CONTENT_TYPES = [
|
|
14
|
-
'application', 'audio', 'image', 'message',
|
|
15
|
-
'multipart','text', 'video'
|
|
16
|
-
]
|
|
17
|
-
|
|
18
|
-
export const TYPE_X_TOKEN_PREFIX = 'X-'
|
|
19
|
-
|
|
20
|
-
export const SPECIAL_CHARS = [
|
|
21
|
-
// special
|
|
22
|
-
'(', ')', '<', '>',
|
|
23
|
-
'@', ',', ';', ':',
|
|
24
|
-
'\\', '"', '/', '[',
|
|
25
|
-
']', '?', '.', '=',
|
|
26
|
-
// space
|
|
27
|
-
' ', '\u000B', '\u000C',
|
|
28
|
-
// control
|
|
29
|
-
'\n', '\r', '\t'
|
|
30
|
-
]
|
|
31
|
-
|
|
32
|
-
export const WHITESPACE_REGEX = /\s/
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* @param {string} c
|
|
36
|
-
*/
|
|
37
|
-
export function isWhitespace(c){ return WHITESPACE_REGEX.test(c) }
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* @param {string|undefined} value
|
|
41
|
-
*/
|
|
42
|
-
export function hasSpecialChar(value) {
|
|
43
|
-
if(value === undefined) { return false }
|
|
44
|
-
for(const special of SPECIAL_CHARS) {
|
|
45
|
-
if(value.includes(special)) { return true}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return false
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* @typedef {Object} ContentType
|
|
53
|
-
* @property {string} mimetype
|
|
54
|
-
* @property {string} mimetypeRaw
|
|
55
|
-
* @property {string} type
|
|
56
|
-
* @property {string} subtype
|
|
57
|
-
* @property {string} [charset]
|
|
58
|
-
* @property {Map<string, string>} parameters
|
|
59
|
-
*/
|
|
60
|
-
|
|
61
|
-
export const CONTENT_TYPE_SEPARATOR = {
|
|
62
|
-
SUBTYPE: '/',
|
|
63
|
-
PARAMETER: ';',
|
|
64
|
-
KVP: '='
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export const CHARSET_UTF8 = 'utf8'
|
|
68
|
-
export const CHARSET = 'charset'
|
|
69
|
-
export const PARAMETER_CHARSET_UTF8 = `${CHARSET}${CONTENT_TYPE_SEPARATOR.KVP}${CHARSET_UTF8}`
|
|
70
|
-
export const CONTENT_TYPE_JSON = `${MIME_TYPE_JSON}${CONTENT_TYPE_SEPARATOR.PARAMETER}${PARAMETER_CHARSET_UTF8}`
|
|
71
|
-
export const CONTENT_TYPE_TEXT = `${MIME_TYPE_TEXT}${CONTENT_TYPE_SEPARATOR.PARAMETER}${PARAMETER_CHARSET_UTF8}`
|
|
72
|
-
export const CONTENT_TYPE_MESSAGE_HTTP = `${MIME_TYPE_MESSAGE_HTTP}${CONTENT_TYPE_SEPARATOR.PARAMETER}${PARAMETER_CHARSET_UTF8}`
|
|
73
|
-
|
|
74
|
-
/** @type {ContentType} */
|
|
75
|
-
export const WELL_KNOWN_JSON = {
|
|
76
|
-
mimetype: 'application/json',
|
|
77
|
-
mimetypeRaw: 'application/json',
|
|
78
|
-
type: 'application',
|
|
79
|
-
subtype: 'json',
|
|
80
|
-
charset: 'utf8',
|
|
81
|
-
parameters: new Map()
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export const WELL_KNOWN_CONTENT_TYPES = new Map([
|
|
85
|
-
[ 'application/json', WELL_KNOWN_JSON ],
|
|
86
|
-
[ 'application/json;charset=utf8', WELL_KNOWN_JSON ]
|
|
87
|
-
])
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* @param {string|undefined} contentTypeHeader
|
|
92
|
-
* @returns {ContentType|undefined}
|
|
93
|
-
*/
|
|
94
|
-
export function parseContentType(contentTypeHeader) {
|
|
95
|
-
if(contentTypeHeader === undefined) { return undefined }
|
|
96
|
-
if(contentTypeHeader === null) { return undefined }
|
|
97
|
-
|
|
98
|
-
const wellKnown = WELL_KNOWN_CONTENT_TYPES.get(contentTypeHeader)
|
|
99
|
-
if(wellKnown !== undefined) { return wellKnown }
|
|
100
|
-
|
|
101
|
-
const [ mimetypeRaw, ...parameterSet ] = contentTypeHeader.split(CONTENT_TYPE_SEPARATOR.PARAMETER)
|
|
102
|
-
if(mimetypeRaw === undefined) { return undefined }
|
|
103
|
-
if(mimetypeRaw === '') { return undefined }
|
|
104
|
-
|
|
105
|
-
const [ typeRaw, subtypeRaw ] = mimetypeRaw
|
|
106
|
-
.split(CONTENT_TYPE_SEPARATOR.SUBTYPE)
|
|
107
|
-
.map(t => t.toLowerCase())
|
|
108
|
-
|
|
109
|
-
if(typeRaw === undefined) { return undefined }
|
|
110
|
-
if(typeRaw === '') { return undefined }
|
|
111
|
-
if(hasSpecialChar(typeRaw)) { return undefined }
|
|
112
|
-
if(subtypeRaw === undefined) { return undefined }
|
|
113
|
-
if(subtypeRaw === '') { return undefined }
|
|
114
|
-
if(hasSpecialChar(subtypeRaw)) { return undefined }
|
|
115
|
-
|
|
116
|
-
const type = typeRaw.trim()
|
|
117
|
-
const subtype = subtypeRaw.trim()
|
|
118
|
-
|
|
119
|
-
const parameters = new Map()
|
|
120
|
-
|
|
121
|
-
for(const parameter of parameterSet) {
|
|
122
|
-
const [ key, value ] = parameter.split(CONTENT_TYPE_SEPARATOR.KVP)
|
|
123
|
-
if(key === undefined || key === '') { continue }
|
|
124
|
-
if(value === undefined || value === '') { continue }
|
|
125
|
-
|
|
126
|
-
const actualKey = key?.trim().toLowerCase()
|
|
127
|
-
if(hasSpecialChar(actualKey)) { continue }
|
|
128
|
-
|
|
129
|
-
const quoted = (value.at(0) === '"' && value.at(-1) === '"')
|
|
130
|
-
const actualValue = quoted ? value.substring(1, value.length - 1) : value
|
|
131
|
-
|
|
132
|
-
if(!parameters.has(actualKey)) {
|
|
133
|
-
parameters.set(actualKey, actualValue)
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const charset = parameters.get(CHARSET)
|
|
138
|
-
|
|
139
|
-
return {
|
|
140
|
-
mimetype: `${type}${CONTENT_TYPE_SEPARATOR.SUBTYPE}${subtype}`,
|
|
141
|
-
mimetypeRaw, type, subtype,
|
|
142
|
-
charset,
|
|
143
|
-
parameters
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
//
|
|
148
|
-
// console.log(parseContentType('multipart/form-data; boundary=----WebKitFormBoundaryJZy5maoMBkBMoGjt'))
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|