Skip to content

Commit 2152af4

Browse files
committed
Remove implementations of ES5 functions not provided in ie8
From now on, IE8 is only supported if polyfills for `Object.keys`, `Array.prototype.forEach`, and `Array.prototype.indexOf` are provided. The suggested polyfills from MDN are provided in ie8-polyfill.js, or you can use https://github.com/es-shims/es5-shim for a more complete set of polyfills.
1 parent a10d19b commit 2152af4

File tree

8 files changed

+183
-14
lines changed

8 files changed

+183
-14
lines changed

.zuul.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ browsers:
1616
version: 4.0..latest
1717
server: ./test/server/index.js
1818
scripts:
19-
- "/polyfill.js"
19+
- "/ie8-polyfill.js"
20+
- "/test-polyfill.js"

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ All browsers newer than IE8 support binary responses. All of the above browsers
2929
support true streaming or pseudo-streaming support that for binary data as well
3030
except for IE10. Old (presto-based) Opera also does not support binary streaming either.
3131

32+
### IE8 note:
33+
As of version 2.0.0, IE8 support requires the user to supply polyfills for
34+
`Object.keys`, `Array.prototype.forEach`, and `Array.prototype.indexOf`. Example
35+
implementations are provided in [ie8-polyfill.js](ie8-polyfill.js); alternately,
36+
you may want to consider using [es5-shim](https://github.com/es-shims/es5-shim).
37+
All browsers with full ES5 support shouldn't require any polyfills.
38+
3239
## How do you use it?
3340

3441
The intent is to have the same api as the client part of the

ie8-polyfill.js

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
// These polyfills taken from MDN (developer.mozilla.org)
2+
3+
// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
4+
if (!Object.keys) {
5+
Object.keys = (function() {
6+
'use strict';
7+
var hasOwnProperty = Object.prototype.hasOwnProperty,
8+
hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'),
9+
dontEnums = [
10+
'toString',
11+
'toLocaleString',
12+
'valueOf',
13+
'hasOwnProperty',
14+
'isPrototypeOf',
15+
'propertyIsEnumerable',
16+
'constructor'
17+
],
18+
dontEnumsLength = dontEnums.length;
19+
20+
return function(obj) {
21+
if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
22+
throw new TypeError('Object.keys called on non-object');
23+
}
24+
25+
var result = [], prop, i;
26+
27+
for (prop in obj) {
28+
if (hasOwnProperty.call(obj, prop)) {
29+
result.push(prop);
30+
}
31+
}
32+
33+
if (hasDontEnumBug) {
34+
for (i = 0; i < dontEnumsLength; i++) {
35+
if (hasOwnProperty.call(obj, dontEnums[i])) {
36+
result.push(dontEnums[i]);
37+
}
38+
}
39+
}
40+
return result;
41+
};
42+
}());
43+
}
44+
45+
// Production steps of ECMA-262, Edition 5, 15.4.4.18
46+
// Reference: http://es5.github.io/#x15.4.4.18
47+
if (!Array.prototype.forEach) {
48+
49+
Array.prototype.forEach = function(callback, thisArg) {
50+
51+
var T, k;
52+
53+
if (this == null) {
54+
throw new TypeError(' this is null or not defined');
55+
}
56+
57+
// 1. Let O be the result of calling ToObject passing the |this| value as the argument.
58+
var O = Object(this);
59+
60+
// 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
61+
// 3. Let len be ToUint32(lenValue).
62+
var len = O.length >>> 0;
63+
64+
// 4. If IsCallable(callback) is false, throw a TypeError exception.
65+
// See: http://es5.github.com/#x9.11
66+
if (typeof callback !== "function") {
67+
throw new TypeError(callback + ' is not a function');
68+
}
69+
70+
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
71+
if (arguments.length > 1) {
72+
T = thisArg;
73+
}
74+
75+
// 6. Let k be 0
76+
k = 0;
77+
78+
// 7. Repeat, while k < len
79+
while (k < len) {
80+
81+
var kValue;
82+
83+
// a. Let Pk be ToString(k).
84+
// This is implicit for LHS operands of the in operator
85+
// b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
86+
// This step can be combined with c
87+
// c. If kPresent is true, then
88+
if (k in O) {
89+
90+
// i. Let kValue be the result of calling the Get internal method of O with argument Pk.
91+
kValue = O[k];
92+
93+
// ii. Call the Call internal method of callback with T as the this value and
94+
// argument list containing kValue, k, and O.
95+
callback.call(T, kValue, k, O);
96+
}
97+
// d. Increase k by 1.
98+
k++;
99+
}
100+
// 8. return undefined
101+
};
102+
}
103+
104+
// Production steps of ECMA-262, Edition 5, 15.4.4.14
105+
// Reference: http://es5.github.io/#x15.4.4.14
106+
if (!Array.prototype.indexOf) {
107+
Array.prototype.indexOf = function(searchElement, fromIndex) {
108+
109+
var k;
110+
111+
// 1. Let O be the result of calling ToObject passing
112+
// the this value as the argument.
113+
if (this == null) {
114+
throw new TypeError('"this" is null or not defined');
115+
}
116+
117+
var O = Object(this);
118+
119+
// 2. Let lenValue be the result of calling the Get
120+
// internal method of O with the argument "length".
121+
// 3. Let len be ToUint32(lenValue).
122+
var len = O.length >>> 0;
123+
124+
// 4. If len is 0, return -1.
125+
if (len === 0) {
126+
return -1;
127+
}
128+
129+
// 5. If argument fromIndex was passed let n be
130+
// ToInteger(fromIndex); else let n be 0.
131+
var n = +fromIndex || 0;
132+
133+
if (Math.abs(n) === Infinity) {
134+
n = 0;
135+
}
136+
137+
// 6. If n >= len, return -1.
138+
if (n >= len) {
139+
return -1;
140+
}
141+
142+
// 7. If n >= 0, then Let k be n.
143+
// 8. Else, n<0, Let k be len - abs(n).
144+
// If k is less than 0, then let k be 0.
145+
k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
146+
147+
// 9. Repeat, while k < len
148+
while (k < len) {
149+
// a. Let Pk be ToString(k).
150+
// This is implicit for LHS operands of the in operator
151+
// b. Let kPresent be the result of calling the
152+
// HasProperty internal method of O with argument Pk.
153+
// This step can be combined with c
154+
// c. If kPresent is true, then
155+
// i. Let elementK be the result of calling the Get
156+
// internal method of O with the argument ToString(k).
157+
// ii. Let same be the result of applying the
158+
// Strict Equality Comparison Algorithm to
159+
// searchElement and elementK.
160+
// iii. If same is true, return k.
161+
if (k in O && O[k] === searchElement) {
162+
return k;
163+
}
164+
k++;
165+
}
166+
return -1;
167+
};
168+
}

lib/request.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// var Base64 = require('Base64')
22
var capability = require('./capability')
3-
var foreach = require('foreach')
4-
var indexOf = require('indexof')
53
var inherits = require('inherits')
6-
var keys = require('object-keys')
74
var response = require('./response')
85
var stream = require('stream')
96

@@ -35,7 +32,7 @@ var ClientRequest = module.exports = function (opts) {
3532
self._headers = {}
3633
if (opts.auth)
3734
self.setHeader('Authorization', 'Basic ' + new Buffer(opts.auth).toString('base64'))
38-
foreach(keys(opts.headers), function (name) {
35+
Object.keys(opts.headers).forEach(function (name) {
3936
self.setHeader(name, opts.headers[name])
4037
})
4138

@@ -68,7 +65,7 @@ ClientRequest.prototype.setHeader = function (name, value) {
6865
// This check is not necessary, but it prevents warnings from browsers about setting unsafe
6966
// headers. To be honest I'm not entirely sure hiding these warnings is a good thing, but
7067
// http-browserify did it, so I will too.
71-
if (indexOf(unsafeHeaders, lowerName) !== -1)
68+
if (unsafeHeaders.indexOf(lowerName) !== -1)
7269
return
7370

7471
self._headers[lowerName] = {
@@ -110,7 +107,7 @@ ClientRequest.prototype._onFinish = function () {
110107
}
111108

112109
if (self._mode === 'fetch') {
113-
var headers = keys(headersObj).map(function (name) {
110+
var headers = Object.keys(headersObj).map(function (name) {
114111
return [headersObj[name].name, headersObj[name].value]
115112
})
116113

@@ -147,7 +144,7 @@ ClientRequest.prototype._onFinish = function () {
147144
if (self._mode === 'text' && 'overrideMimeType' in xhr)
148145
xhr.overrideMimeType('text/plain; charset=x-user-defined')
149146

150-
foreach(keys(headersObj), function (name) {
147+
Object.keys(headersObj).forEach(function (name) {
151148
xhr.setRequestHeader(headersObj[name].name, headersObj[name].value)
152149
})
153150

lib/response.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var capability = require('./capability')
2-
var foreach = require('foreach')
32
var inherits = require('inherits')
43
var stream = require('stream')
54

@@ -64,7 +63,7 @@ var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
6463
self.statusCode = xhr.status
6564
self.statusMessage = xhr.statusText
6665
var headers = xhr.getAllResponseHeaders().split(/\r?\n/)
67-
foreach(headers, function (header) {
66+
headers.forEach(function (header) {
6867
var matches = header.match(/^([^:]+):\s*(.*)/)
6968
if (matches) {
7069
var key = matches[1].toLowerCase()

package.json

-3
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@
2424
],
2525
"dependencies": {
2626
"builtin-status-codes": "^1.0.0",
27-
"foreach": "^2.0.5",
28-
"indexof": "0.0.1",
2927
"inherits": "^2.0.1",
30-
"object-keys": "^1.0.4",
3128
"xtend": "^4.0.0"
3229
},
3330
"devDependencies": {

test/server/static/ie8-polyfill.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../ie8-polyfill.js

test/server/static/polyfill.js renamed to test/server/static/test-polyfill.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// TODO: send a PR to url to remove .trim() so this isn't necessary
21
if (!String.prototype.trim) {
32
(function() {
43
// Make sure we trim BOM and NBSP

0 commit comments

Comments
 (0)