-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Polyfill the fetch Headers object if needed (#1014)
* test: improve test description * fix: polyfill the fetch Headers object if needed
- Loading branch information
Showing
3 changed files
with
165 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* headers - Detect when fetch Headers are undefined and use a partial polyfill. | ||
* | ||
* A full polyfill is not used in order to keep package size as small as possible. | ||
* Since this is only used internally and is not added to the window object, | ||
* the full interface doesn't need to be supported. | ||
* | ||
* This implementation is modified from whatwg-fetch: | ||
* https://github.com/github/fetch | ||
*/ | ||
function headers(headers) { | ||
if (typeof Headers === 'undefined') { | ||
return new FetchHeaders(headers); | ||
} | ||
|
||
return new Headers(headers); | ||
} | ||
|
||
function normalizeName(name) { | ||
if (typeof name !== 'string') { | ||
name = String(name) | ||
} | ||
return name.toLowerCase() | ||
} | ||
|
||
function normalizeValue(value) { | ||
if (typeof value !== 'string') { | ||
value = String(value) | ||
} | ||
return value | ||
} | ||
|
||
function iteratorFor(items) { | ||
var iterator = { | ||
next: function() { | ||
var value = items.shift() | ||
return {done: value === undefined, value: value} | ||
} | ||
} | ||
|
||
return iterator | ||
} | ||
|
||
function FetchHeaders(headers) { | ||
this.map = {} | ||
|
||
if (headers instanceof FetchHeaders) { | ||
headers.forEach(function(value, name) { | ||
this.append(name, value) | ||
}, this) | ||
} else if (Array.isArray(headers)) { | ||
headers.forEach(function(header) { | ||
this.append(header[0], header[1]) | ||
}, this) | ||
} else if (headers) { | ||
Object.getOwnPropertyNames(headers).forEach(function(name) { | ||
this.append(name, headers[name]) | ||
}, this) | ||
} | ||
} | ||
|
||
FetchHeaders.prototype.append = function(name, value) { | ||
name = normalizeName(name) | ||
value = normalizeValue(value) | ||
var oldValue = this.map[name] | ||
this.map[name] = oldValue ? oldValue + ', ' + value : value | ||
} | ||
|
||
FetchHeaders.prototype.get = function(name) { | ||
name = normalizeName(name) | ||
return this.has(name) ? this.map[name] : null | ||
} | ||
|
||
FetchHeaders.prototype.has = function(name) { | ||
return this.map.hasOwnProperty(normalizeName(name)) | ||
} | ||
|
||
FetchHeaders.prototype.forEach = function(callback, thisArg) { | ||
for (var name in this.map) { | ||
if (this.map.hasOwnProperty(name)) { | ||
callback.call(thisArg, this.map[name], name, this) | ||
} | ||
} | ||
} | ||
|
||
FetchHeaders.prototype.entries = function() { | ||
var items = [] | ||
this.forEach(function(value, name) { | ||
items.push([name, value]) | ||
}) | ||
return iteratorFor(items) | ||
} | ||
|
||
module.exports = headers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters