Skip to content

Commit a14e756

Browse files
committed
make sure Decoder raises DecodeError or DataViewIndexOutOfBoundsError
1 parent 8b1e562 commit a14e756

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

src/DecodeError.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
export class DecodeError extends Error {
3+
constructor(message: string) {
4+
super(message);
5+
6+
// fix the prototype chain in a cross-platform way
7+
const proto: typeof DecodeError.prototype = Object.create(DecodeError.prototype);
8+
Object.setPrototypeOf(this, proto);
9+
10+
Object.defineProperty(this, "name", {
11+
configurable: true,
12+
enumerable: false,
13+
value: DecodeError.name,
14+
});
15+
}
16+
}

src/Decoder.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getInt64, getUint64 } from "./utils/int";
44
import { utf8DecodeJs, TEXT_DECODER_THRESHOLD, utf8DecodeTD } from "./utils/utf8";
55
import { createDataView, ensureUint8Array } from "./utils/typedArrays";
66
import { CachedKeyDecoder, KeyDecoder } from "./CachedKeyDecoder";
7+
import { DecodeError } from "./DecodeError";
78

89
const enum State {
910
ARRAY,
@@ -60,22 +61,6 @@ const DEFAULT_MAX_LENGTH = 0xffff_ffff; // uint32_max
6061

6162
const sharedCachedKeyDecoder = new CachedKeyDecoder();
6263

63-
export class DecodeError extends Error {
64-
constructor(message: string) {
65-
super(message);
66-
67-
// fix the prototype chain in a cross-platform way
68-
const proto: typeof DecodeError.prototype = Object.create(DecodeError.prototype);
69-
Object.setPrototypeOf(this, proto);
70-
71-
Object.defineProperty(this, "name", {
72-
configurable: true,
73-
enumerable: false,
74-
value: DecodeError.name,
75-
});
76-
}
77-
}
78-
7964
export class Decoder<ContextType = undefined> {
8065
private totalPos = 0;
8166
private pos = 0;
@@ -133,6 +118,10 @@ export class Decoder<ContextType = undefined> {
133118
return new RangeError(`Extra ${view.byteLength - pos} of ${view.byteLength} byte(s) found at buffer[${posToShow}]`);
134119
}
135120

121+
/**
122+
* @throws {DecodeError}
123+
* @throws {RangeError}
124+
*/
136125
public decode(buffer: ArrayLike<number> | BufferSource): unknown {
137126
this.reinitializeState();
138127
this.setBuffer(buffer);

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ export { DecodeOptions };
1313
import { decodeAsync, decodeArrayStream, decodeMultiStream, decodeStream } from "./decodeAsync";
1414
export { decodeAsync, decodeArrayStream, decodeMultiStream, decodeStream };
1515

16-
import { Decoder, DecodeError } from "./Decoder";
17-
export { Decoder, DecodeError };
16+
import { Decoder, DataViewIndexOutOfBoundsError } from "./Decoder";
17+
import { DecodeError } from "./DecodeError";
18+
export { Decoder, DecodeError, DataViewIndexOutOfBoundsError };
1819

1920
import { Encoder } from "./Encoder";
2021
export { Encoder };

src/timestamp.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type
2+
import { DecodeError } from "./DecodeError";
23
import { getInt64, setInt64 } from "./utils/int";
34

45
export const EXT_TIMESTAMP = -1;
@@ -91,7 +92,7 @@ export function decodeTimestampToTimeSpec(data: Uint8Array): TimeSpec {
9192
return { sec, nsec };
9293
}
9394
default:
94-
throw new Error(`Unrecognized data size for timestamp: ${data.length}`);
95+
throw new DecodeError(`Unrecognized data size for timestamp (expected 4, 8, or 12): ${data.length}`);
9596
}
9697
}
9798

0 commit comments

Comments
 (0)