|
2 | 2 |
|
3 | 3 | // Partially implements Headers from the Fetch API |
4 | 4 | // https://developer.mozilla.org/en-US/docs/Web/API/Headers |
5 | | -function FastBootHeaders(headers) { |
6 | | - headers = headers || {}; |
7 | | - this.headers = {}; |
8 | | - |
9 | | - for (var header in headers) { |
10 | | - let value = headers[header]; |
11 | | - |
12 | | - // Express gives us either a string |
13 | | - // or an array of strings if there are multiple values. |
14 | | - // We want to support the Header spec |
15 | | - // so we will coerce to an array always. |
16 | | - if (typeof value === 'string') { |
17 | | - value = [value]; |
| 5 | +class FastBootHeaders { |
| 6 | + constructor(headers) { |
| 7 | + headers = headers || {}; |
| 8 | + this.headers = {}; |
| 9 | + |
| 10 | + for (var header in headers) { |
| 11 | + let value = headers[header]; |
| 12 | + |
| 13 | + // Express gives us either a string |
| 14 | + // or an array of strings if there are multiple values. |
| 15 | + // We want to support the Header spec |
| 16 | + // so we will coerce to an array always. |
| 17 | + if (typeof value === 'string') { |
| 18 | + value = [value]; |
| 19 | + } |
| 20 | + |
| 21 | + this.headers[header] = value; |
18 | 22 | } |
19 | | - |
20 | | - this.headers[header] = value; |
21 | 23 | } |
22 | | -} |
23 | 24 |
|
24 | | -FastBootHeaders.prototype.append = function(header, value) { |
25 | | - header = header.toLowerCase(); |
26 | | - if (!this.has(header)) { |
27 | | - this.headers[header] = []; |
28 | | - } |
| 25 | + append(header, value) { |
| 26 | + let _header = header.toLowerCase(); |
29 | 27 |
|
30 | | - this.headers[header].push(value); |
31 | | -}; |
| 28 | + if (!this.has(_header)) { |
| 29 | + this.headers[_header] = []; |
| 30 | + } |
32 | 31 |
|
33 | | -FastBootHeaders.prototype.delete = function(header) { |
34 | | - delete this.headers[header.toLowerCase()]; |
35 | | -}; |
| 32 | + this.headers[_header].push(value); |
| 33 | + } |
36 | 34 |
|
37 | | -FastBootHeaders.prototype.entries = function() { |
38 | | - var entries = []; |
| 35 | + delete(header) { |
| 36 | + delete this.headers[header.toLowerCase()]; |
| 37 | + } |
| 38 | + |
| 39 | + entries() { |
| 40 | + var entries = []; |
39 | 41 |
|
40 | | - for(var key in this.headers) { |
41 | | - var values = this.headers[key]; |
42 | | - for(var index = 0; index < values.length; ++index ) { |
43 | | - entries.push([key, values[index]]); |
| 42 | + for(var key in this.headers) { |
| 43 | + var values = this.headers[key]; |
| 44 | + for(var index = 0; index < values.length; ++index ) { |
| 45 | + entries.push([key, values[index]]); |
| 46 | + } |
44 | 47 | } |
45 | | - } |
46 | 48 |
|
47 | | - return entries[Symbol.iterator](); |
48 | | -}; |
| 49 | + return entries[Symbol.iterator](); |
| 50 | + } |
49 | 51 |
|
50 | | -FastBootHeaders.prototype.get = function(header) { |
51 | | - return this.getAll(header)[0] || null; |
52 | | -}; |
| 52 | + get(header) { |
| 53 | + return this.getAll(header)[0] || null; |
| 54 | + } |
53 | 55 |
|
54 | | -FastBootHeaders.prototype.getAll = function(header) { |
55 | | - return this.headers[header.toLowerCase()] || []; |
56 | | -}; |
| 56 | + getAll(header) { |
| 57 | + return this.headers[header.toLowerCase()] || []; |
| 58 | + } |
57 | 59 |
|
58 | | -FastBootHeaders.prototype.has = function(header) { |
59 | | - return this.headers[header.toLowerCase()] !== undefined; |
60 | | -}; |
| 60 | + has(header) { |
| 61 | + return this.headers[header.toLowerCase()] !== undefined; |
| 62 | + } |
61 | 63 |
|
62 | | -FastBootHeaders.prototype.keys = function() { |
63 | | - var entries = []; |
| 64 | + keys() { |
| 65 | + var entries = []; |
64 | 66 |
|
65 | | - for(var key in this.headers) { |
66 | | - var values = this.headers[key]; |
67 | | - for(var index = 0; index < values.length; ++index ) { |
68 | | - entries.push(key); |
| 67 | + for(var key in this.headers) { |
| 68 | + var values = this.headers[key]; |
| 69 | + for(var index = 0; index < values.length; ++index ) { |
| 70 | + entries.push(key); |
| 71 | + } |
69 | 72 | } |
70 | | - } |
71 | 73 |
|
72 | | - return entries[Symbol.iterator](); |
73 | | -}; |
| 74 | + return entries[Symbol.iterator](); |
| 75 | + } |
74 | 76 |
|
75 | | -FastBootHeaders.prototype.set = function(header, value) { |
76 | | - header = header.toLowerCase(); |
77 | | - this.headers[header] = [value]; |
78 | | -}; |
| 77 | + set(header, value) { |
| 78 | + header = header.toLowerCase(); |
| 79 | + this.headers[header] = [value]; |
| 80 | + } |
79 | 81 |
|
80 | | -FastBootHeaders.prototype.values = function() { |
81 | | - var entries = []; |
| 82 | + values() { |
| 83 | + var entries = []; |
82 | 84 |
|
83 | | - for(var key in this.headers) { |
84 | | - var values = this.headers[key]; |
85 | | - for(var index = 0; index < values.length; ++index ) { |
86 | | - entries.push(values[index]); |
| 85 | + for(var key in this.headers) { |
| 86 | + var values = this.headers[key]; |
| 87 | + for(var index = 0; index < values.length; ++index ) { |
| 88 | + entries.push(values[index]); |
| 89 | + } |
87 | 90 | } |
88 | | - } |
89 | 91 |
|
90 | | - return entries[Symbol.iterator](); |
91 | | -}; |
| 92 | + return entries[Symbol.iterator](); |
| 93 | + } |
| 94 | +} |
92 | 95 |
|
93 | 96 | module.exports = FastBootHeaders; |
0 commit comments