Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
make sure Decoder raises DecodeError or DataViewIndexOutOfBoundsError
  • Loading branch information
gfx committed May 4, 2021
commit a14e756db54e03faf5439ff21c1db8bcfb223423
16 changes: 16 additions & 0 deletions src/DecodeError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

export class DecodeError extends Error {
constructor(message: string) {
super(message);

// fix the prototype chain in a cross-platform way
const proto: typeof DecodeError.prototype = Object.create(DecodeError.prototype);
Object.setPrototypeOf(this, proto);

Object.defineProperty(this, "name", {
configurable: true,
enumerable: false,
value: DecodeError.name,
});
}
}
21 changes: 5 additions & 16 deletions src/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getInt64, getUint64 } from "./utils/int";
import { utf8DecodeJs, TEXT_DECODER_THRESHOLD, utf8DecodeTD } from "./utils/utf8";
import { createDataView, ensureUint8Array } from "./utils/typedArrays";
import { CachedKeyDecoder, KeyDecoder } from "./CachedKeyDecoder";
import { DecodeError } from "./DecodeError";

const enum State {
ARRAY,
Expand Down Expand Up @@ -60,22 +61,6 @@ const DEFAULT_MAX_LENGTH = 0xffff_ffff; // uint32_max

const sharedCachedKeyDecoder = new CachedKeyDecoder();

export class DecodeError extends Error {
constructor(message: string) {
super(message);

// fix the prototype chain in a cross-platform way
const proto: typeof DecodeError.prototype = Object.create(DecodeError.prototype);
Object.setPrototypeOf(this, proto);

Object.defineProperty(this, "name", {
configurable: true,
enumerable: false,
value: DecodeError.name,
});
}
}

export class Decoder<ContextType = undefined> {
private totalPos = 0;
private pos = 0;
Expand Down Expand Up @@ -133,6 +118,10 @@ export class Decoder<ContextType = undefined> {
return new RangeError(`Extra ${view.byteLength - pos} of ${view.byteLength} byte(s) found at buffer[${posToShow}]`);
}

/**
* @throws {DecodeError}
* @throws {RangeError}
*/
public decode(buffer: ArrayLike<number> | BufferSource): unknown {
this.reinitializeState();
this.setBuffer(buffer);
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ export { DecodeOptions };
import { decodeAsync, decodeArrayStream, decodeMultiStream, decodeStream } from "./decodeAsync";
export { decodeAsync, decodeArrayStream, decodeMultiStream, decodeStream };

import { Decoder, DecodeError } from "./Decoder";
export { Decoder, DecodeError };
import { Decoder, DataViewIndexOutOfBoundsError } from "./Decoder";
import { DecodeError } from "./DecodeError";
export { Decoder, DecodeError, DataViewIndexOutOfBoundsError };

import { Encoder } from "./Encoder";
export { Encoder };
Expand Down
3 changes: 2 additions & 1 deletion src/timestamp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type
import { DecodeError } from "./DecodeError";
import { getInt64, setInt64 } from "./utils/int";

export const EXT_TIMESTAMP = -1;
Expand Down Expand Up @@ -91,7 +92,7 @@ export function decodeTimestampToTimeSpec(data: Uint8Array): TimeSpec {
return { sec, nsec };
}
default:
throw new Error(`Unrecognized data size for timestamp: ${data.length}`);
throw new DecodeError(`Unrecognized data size for timestamp (expected 4, 8, or 12): ${data.length}`);
}
}

Expand Down