Skip to content

Commit 361b2c6

Browse files
committed
[Fix] node v0.11.4 - 3 have an own slice method that works incorrectly
1 parent bcf30f9 commit 361b2c6

File tree

2 files changed

+6
-74
lines changed

2 files changed

+6
-74
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ assert.deepEqual(arr3, new Uint8Array([2, 3, 3]));
4040
assert.notEqual(arr.buffer, arr3.buffer);
4141
```
4242

43+
## Engines where this is needed
44+
45+
- node v0.11.4 - v4: no prototype or own `slice` method
46+
- node < v0.11.3: own `slice` method that fails to clone the underlying buffer
47+
4348
## Tests
4449
Simply clone the repo, `npm install`, and run `npm test`
4550

polyfill.js

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,7 @@
11
'use strict';
22

3-
var callBind = require('call-bind');
4-
var callBound = require('call-bind/callBound');
5-
6-
var $toString = callBound('Object.prototype.toString');
7-
var $strSlice = callBound('String.prototype.slice');
8-
93
var implementation = require('./implementation');
104

11-
var ownSlice = null;
12-
if (typeof Uint8Array === 'function' && !Uint8Array.prototype.slice && new Uint8Array(0).slice) {
13-
// node < 0.12 has typed arrays but only an own slice, that's specific for each type
14-
var map = {
15-
$Float32Array: callBind(new Float32Array(0).slice),
16-
$Float64Array: callBind(new Float64Array(0).slice),
17-
$Int8Array: callBind(new Int8Array(0).slice),
18-
$Int16Array: callBind(new Int16Array(0).slice),
19-
$Int32Array: callBind(new Int32Array(0).slice),
20-
$Uint8Array: callBind(new Uint8Array(0).slice),
21-
$Uint8ClampedArray: typeof Uint8ClampedArray !== 'undefined' && callBind(new Uint8ClampedArray(0).slice), // node 0.6 lacks Uint8ClampedArray
22-
$Uint16Array: callBind(new Uint16Array(0).slice),
23-
$Uint32Array: callBind(new Uint32Array(0).slice),
24-
__proto__: null
25-
};
26-
var dispatch = function dispatchSlice(fn, arr, args) {
27-
if (args.length < 2) {
28-
return fn(arr, args.length > 0 ? args[0] : 0);
29-
}
30-
return fn(arr, args[0], args[1]);
31-
};
32-
// eslint-disable-next-line no-unused-vars
33-
ownSlice = function slice(start, end) {
34-
var fn = map['$' + $strSlice($toString(this), 8, -1)];
35-
if (!fn) {
36-
/* eslint max-depth: 0 */
37-
try {
38-
return dispatch(map.$Float32Array, this, arguments);
39-
} catch (e) {
40-
try {
41-
return dispatch(map.$Float64Array, this, arguments);
42-
} catch (e1) {
43-
try {
44-
return dispatch(map.$Int8Array, this, arguments);
45-
} catch (e2) {
46-
try {
47-
return dispatch(map.$Uint8Array, this, arguments);
48-
} catch (e3) {
49-
try {
50-
return dispatch(map.$Uint8ClampedArray, this, arguments);
51-
} catch (e4) {
52-
try {
53-
return dispatch(map.$Int16Array, this, arguments);
54-
} catch (e5) {
55-
try {
56-
return dispatch(map.$Uint16Array, this, arguments);
57-
} catch (e6) {
58-
try {
59-
return dispatch(map.$Int32Array, this, arguments);
60-
} catch (e7) {
61-
return dispatch(map.$Uint32Array, this, arguments);
62-
}
63-
}
64-
}
65-
}
66-
}
67-
}
68-
}
69-
}
70-
}
71-
/* eslint no-invalid-this: 0 */
72-
return dispatch(fn, this, arguments);
73-
};
74-
}
75-
765
module.exports = function getPolyfill() {
77-
return (typeof Uint8Array === 'function' && Uint8Array.prototype.slice)
78-
|| ownSlice
79-
|| implementation;
6+
return (typeof Uint8Array === 'function' && Uint8Array.prototype.slice) || implementation;
807
};

0 commit comments

Comments
 (0)