Skip to content

Commit fb2ef45

Browse files
authored
Improves URL polyfill to work without TypedArray.prototype.slice/indexOf (#73)
This PR updates the `URL` polyfill to not depend on `TypedArray.prototype.slice` or `TypedArray.prototype.indexOf`, which may not be available in all engines. It also reverts changes to the `ArrayBuffer` polyfill from #62 to avoid loading the entire `ArrayBuffer` polyfill in engines that don't need it.
1 parent 923817d commit fb2ef45

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

polyfills/ArrayBuffer/config.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ docs = "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global
1717
[browsers]
1818
android = "*"
1919
bb = "*"
20-
chrome = "<45"
20+
chrome = "<32"
2121
edge = "<14"
2222
edge_mob = "<14"
2323
firefox = "<44"
@@ -27,8 +27,8 @@ ie_mob = "*"
2727
opera = "<19"
2828
op_mob = "<19"
2929
op_mini = "*"
30-
safari = "<10.0"
31-
ios_saf = "<10.0"
30+
safari = "<7.0"
31+
ios_saf = "<7.0"
3232
samsung_mob = "<2.0"
3333

3434
[install]

polyfills/ArrayBuffer/detect.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// use "Int8Array" as a proxy for support of "TypedArray" subclasses
22
// confirm that the prototype of "Int8Array" is NOT the "Object" prototype, which is a bug in IE11 and maybe other old browsers
33
'ArrayBuffer' in self && 'DataView' in self && 'Int8Array' in self
4-
// IE11 has an incomplete implementation that's missing `slice` and maybe others
5-
&& 'slice' in self.Int8Array.prototype
64
// TODO: add back this check once we remove support for ie10 and below
75
// && Object.getPrototypeOf(self.Int8Array) !== Object.getPrototypeOf(Object)

polyfills/URL/polyfill.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2498,7 +2498,7 @@ function percentDecodeBytes(input) {
24982498
i += 2;
24992499
}
25002500
}
2501-
return output.slice(0, outputIndex);
2501+
return output.subarray(0, outputIndex);
25022502
}
25032503

25042504
// https://url.spec.whatwg.org/#string-percent-decode
@@ -3739,10 +3739,10 @@ function parseUrlencoded(input) {
37393739
}
37403740
var name = void 0,
37413741
value = void 0;
3742-
var indexOfEqual = bytes.indexOf(p("="));
3742+
var indexOfEqual = Array.prototype.indexOf.call(bytes, p("="));
37433743
if (indexOfEqual >= 0) {
3744-
name = bytes.slice(0, indexOfEqual);
3745-
value = bytes.slice(indexOfEqual + 1);
3744+
name = bytes.subarray(0, indexOfEqual);
3745+
value = bytes.subarray(indexOfEqual + 1);
37463746
} else {
37473747
name = bytes;
37483748
value = new Uint8Array(0);
@@ -3810,22 +3810,22 @@ function serializeUrlencoded(tuples) {
38103810
function strictlySplitByteSequence(buf, cp) {
38113811
var list = [];
38123812
var last = 0;
3813-
var i = buf.indexOf(cp);
3813+
var i = Array.prototype.indexOf.call(buf, cp);
38143814
while (i >= 0) {
3815-
list.push(buf.slice(last, i));
3815+
list.push(buf.subarray(last, i));
38163816
last = i + 1;
3817-
i = buf.indexOf(cp, last);
3817+
i = Array.prototype.indexOf.call(buf, cp, last);
38183818
}
38193819
if (last !== buf.length) {
3820-
list.push(buf.slice(last));
3820+
list.push(buf.subarray(last));
38213821
}
38223822
return list;
38233823
}
38243824
function replaceByteInByteSequence(buf, from, to) {
3825-
var i = buf.indexOf(from);
3825+
var i = Array.prototype.indexOf.call(buf, from);
38263826
while (i >= 0) {
38273827
buf[i] = to;
3828-
i = buf.indexOf(from, i + 1);
3828+
i = Array.prototype.indexOf.call(buf, from, i + 1);
38293829
}
38303830
return buf;
38313831
}

polyfills/URL/update.task.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,26 @@ const codeProcessors = [
4646
'Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "byteLength")?.get ?? Object.getOwnPropertyDescriptor(new Uint8Array(), "byteLength")?.get;'
4747
)
4848
},
49+
{
50+
filename: "node_modules/whatwg-url/lib/percent-encoding.js",
51+
description: "replace `typedArray.slice` with `typedArray.subarray`",
52+
processor: (code) =>
53+
code.replace(
54+
/return output\.slice\(0, outputIndex\);/,
55+
"return output.subarray(0, outputIndex);"
56+
)
57+
},
58+
{
59+
filename: "node_modules/whatwg-url/lib/urlencoded.js",
60+
description:
61+
"replace `typedArray.slice` with `typedArray.subarray` and `typedArray.indexOf` with `Array.prototype.indexOf`",
62+
processor: (code) =>
63+
code
64+
.replace(/bytes\.slice\(/g, "bytes.subarray(")
65+
.replace(/buf\.slice\(/g, "buf.subarray(")
66+
.replace(/bytes\.indexOf\(/g, "Array.prototype.indexOf.call(bytes, ")
67+
.replace(/buf\.indexOf\(/g, "Array.prototype.indexOf.call(buf, ")
68+
},
4969
{
5070
filename: "node_modules/whatwg-url/lib/URL.js",
5171
description:

0 commit comments

Comments
 (0)