Skip to content

Commit 46671b4

Browse files
committed
workround IE11 issues
1 parent 50b613f commit 46671b4

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/Decoder.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,20 @@ type StackState = StackArrayState | StackMapState;
2929
const HEAD_BYTE_REQUIRED = -1;
3030

3131
const EMPTY_VIEW = new DataView(new ArrayBuffer(0));
32-
const MORE_DATA = new RangeError("Insufficient data");
32+
33+
// IE11: Just use RangeError when IE11 is obsolete.
34+
export const DataViewIndexOutOfBoundsError: typeof Error = (() => {
35+
try {
36+
// IE11: The spec says it should throw RangeError,
37+
// IE11: but in IE11 it throws TypeError.
38+
EMPTY_VIEW.getInt8(0);
39+
} catch (e) {
40+
return e.constructor;
41+
}
42+
throw new Error("never reached");
43+
})();
44+
45+
const MORE_DATA = new DataViewIndexOutOfBoundsError("Insufficient data");
3346

3447
export class Decoder {
3548
totalPos = 0;
@@ -92,7 +105,7 @@ export class Decoder {
92105
object = this.decodeSync();
93106
decoded = true;
94107
} catch (e) {
95-
if (!(e instanceof RangeError)) {
108+
if (!(e instanceof DataViewIndexOutOfBoundsError)) {
96109
throw e; // rethrow
97110
}
98111
// fallthrough

test/edge-cases.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import assert from "assert";
22
import { encode, decode, decodeAsync } from "../src";
3+
import { DataViewIndexOutOfBoundsError } from "../src/Decoder";
34

45
describe("edge cases", () => {
56
context("try to encode trycyclic refs", () => {
@@ -15,7 +16,7 @@ describe("edge cases", () => {
1516
context("try to encode non-encodable objects", () => {
1617
it("throws errors", () => {
1718
assert.throws(() => {
18-
encode(Symbol("this is a symbol!"));
19+
encode(() => {});
1920
}, /unrecognized object/i);
2021
});
2122
});
@@ -51,7 +52,8 @@ describe("edge cases", () => {
5152
0x92, // fixarray size=2
5253
0xc0, // nil
5354
]);
54-
}, RangeError);
55+
// [IE11] A raw error thrown by DataView
56+
}, DataViewIndexOutOfBoundsError);
5557
});
5658

5759
it("throws errors (asynchronous)", async () => {

0 commit comments

Comments
 (0)