headers.js
6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
'use strict'
/**
* headers.js
*
* Headers class offers convenient helpers
*/
const common = require('./common.js')
const checkInvalidHeaderChar = common.checkInvalidHeaderChar
const checkIsHttpToken = common.checkIsHttpToken
function sanitizeName (name) {
name += ''
if (!checkIsHttpToken(name)) {
throw new TypeError(`${name} is not a legal HTTP header name`)
}
return name.toLowerCase()
}
function sanitizeValue (value) {
value += ''
if (checkInvalidHeaderChar(value)) {
throw new TypeError(`${value} is not a legal HTTP header value`)
}
return value
}
const MAP = Symbol('map')
class Headers {
/**
* Headers class
*
* @param Object headers Response headers
* @return Void
*/
constructor (init) {
this[MAP] = Object.create(null)
if (init instanceof Headers) {
const rawHeaders = init.raw()
const headerNames = Object.keys(rawHeaders)
for (const headerName of headerNames) {
for (const value of rawHeaders[headerName]) {
this.append(headerName, value)
}
}
return
}
// We don't worry about converting prop to ByteString here as append()
// will handle it.
if (init == null) {
// no op
} else if (typeof init === 'object') {
const method = init[Symbol.iterator]
if (method != null) {
if (typeof method !== 'function') {
throw new TypeError('Header pairs must be iterable')
}
// sequence<sequence<ByteString>>
// Note: per spec we have to first exhaust the lists then process them
const pairs = []
for (const pair of init) {
if (typeof pair !== 'object' || typeof pair[Symbol.iterator] !== 'function') {
throw new TypeError('Each header pair must be iterable')
}
pairs.push(Array.from(pair))
}
for (const pair of pairs) {
if (pair.length !== 2) {
throw new TypeError('Each header pair must be a name/value tuple')
}
this.append(pair[0], pair[1])
}
} else {
// record<ByteString, ByteString>
for (const key of Object.keys(init)) {
const value = init[key]
this.append(key, value)
}
}
} else {
throw new TypeError('Provided initializer must be an object')
}
Object.defineProperty(this, Symbol.toStringTag, {
value: 'Headers',
writable: false,
enumerable: false,
configurable: true
})
}
/**
* Return first header value given name
*
* @param String name Header name
* @return Mixed
*/
get (name) {
const list = this[MAP][sanitizeName(name)]
if (!list) {
return null
}
return list.join(', ')
}
/**
* Iterate over all headers
*
* @param Function callback Executed for each item with parameters (value, name, thisArg)
* @param Boolean thisArg `this` context for callback function
* @return Void
*/
forEach (callback, thisArg) {
let pairs = getHeaderPairs(this)
let i = 0
while (i < pairs.length) {
const name = pairs[i][0]
const value = pairs[i][1]
callback.call(thisArg, value, name, this)
pairs = getHeaderPairs(this)
i++
}
}
/**
* Overwrite header values given name
*
* @param String name Header name
* @param String value Header value
* @return Void
*/
set (name, value) {
this[MAP][sanitizeName(name)] = [sanitizeValue(value)]
}
/**
* Append a value onto existing header
*
* @param String name Header name
* @param String value Header value
* @return Void
*/
append (name, value) {
if (!this.has(name)) {
this.set(name, value)
return
}
this[MAP][sanitizeName(name)].push(sanitizeValue(value))
}
/**
* Check for header name existence
*
* @param String name Header name
* @return Boolean
*/
has (name) {
return !!this[MAP][sanitizeName(name)]
}
/**
* Delete all header values given name
*
* @param String name Header name
* @return Void
*/
delete (name) {
delete this[MAP][sanitizeName(name)]
};
/**
* Return raw headers (non-spec api)
*
* @return Object
*/
raw () {
return this[MAP]
}
/**
* Get an iterator on keys.
*
* @return Iterator
*/
keys () {
return createHeadersIterator(this, 'key')
}
/**
* Get an iterator on values.
*
* @return Iterator
*/
values () {
return createHeadersIterator(this, 'value')
}
/**
* Get an iterator on entries.
*
* This is the default iterator of the Headers object.
*
* @return Iterator
*/
[Symbol.iterator] () {
return createHeadersIterator(this, 'key+value')
}
}
Headers.prototype.entries = Headers.prototype[Symbol.iterator]
Object.defineProperty(Headers.prototype, Symbol.toStringTag, {
value: 'HeadersPrototype',
writable: false,
enumerable: false,
configurable: true
})
function getHeaderPairs (headers, kind) {
const keys = Object.keys(headers[MAP]).sort()
return keys.map(
kind === 'key'
? k => [k]
: k => [k, headers.get(k)]
)
}
const INTERNAL = Symbol('internal')
function createHeadersIterator (target, kind) {
const iterator = Object.create(HeadersIteratorPrototype)
iterator[INTERNAL] = {
target,
kind,
index: 0
}
return iterator
}
const HeadersIteratorPrototype = Object.setPrototypeOf({
next () {
// istanbul ignore if
if (!this ||
Object.getPrototypeOf(this) !== HeadersIteratorPrototype) {
throw new TypeError('Value of `this` is not a HeadersIterator')
}
const target = this[INTERNAL].target
const kind = this[INTERNAL].kind
const index = this[INTERNAL].index
const values = getHeaderPairs(target, kind)
const len = values.length
if (index >= len) {
return {
value: undefined,
done: true
}
}
const pair = values[index]
this[INTERNAL].index = index + 1
let result
if (kind === 'key') {
result = pair[0]
} else if (kind === 'value') {
result = pair[1]
} else {
result = pair
}
return {
value: result,
done: false
}
}
}, Object.getPrototypeOf(
Object.getPrototypeOf([][Symbol.iterator]())
))
Object.defineProperty(HeadersIteratorPrototype, Symbol.toStringTag, {
value: 'HeadersIterator',
writable: false,
enumerable: false,
configurable: true
})
module.exports = Headers