Skip to content

Commit

Permalink
Merge pull request #209 from msgpack/clarify_empty_input
Browse files Browse the repository at this point in the history
[doc] clarify the edge cases where the input buffer is empty
  • Loading branch information
gfx authored May 1, 2022
2 parents 81bf88b + bebb16b commit 32654ee
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 7 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ module.exports = {
"prettier",
],
plugins: [
"@typescript-eslint",
"@typescript-eslint/eslint-plugin",
"eslint-plugin-tsdoc"
],
parser: "@typescript-eslint/parser",
parserOptions: {
Expand All @@ -38,6 +39,8 @@ module.exports = {
"import/no-cycle": "error",
"import/no-default-export": "warn",

"tsdoc/syntax": "warn",

"@typescript-eslint/await-thenable": "warn",
"@typescript-eslint/array-type": ["warn", { default: "generic" }],
"@typescript-eslint/naming-convention": [
Expand Down
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
"editor.tabSize": 2,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
},
"cSpell.words": [
"tsdoc"
]
}
94 changes: 94 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"eslint": "latest",
"eslint-config-prettier": "latest",
"eslint-plugin-import": "latest",
"eslint-plugin-tsdoc": "latest",
"ieee754": "latest",
"karma": "latest",
"karma-chrome-launcher": "latest",
Expand Down
4 changes: 2 additions & 2 deletions src/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ export class Decoder<ContextType = undefined> {
}

/**
* @throws {DecodeError}
* @throws {RangeError}
* @throws {@link DecodeError}
* @throws {@link RangeError}
*/
public decode(buffer: ArrayLike<number> | BufferSource): unknown {
this.reinitializeState();
Expand Down
6 changes: 6 additions & 0 deletions src/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export const defaultDecodeOptions: DecodeOptions = {};
*
* This is a synchronous decoding function.
* See other variants for asynchronous decoding: {@link decodeAsync()}, {@link decodeStream()}, or {@link decodeArrayStream()}.
*
* @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty.
* @throws {@link DecodeError} if the buffer contains invalid data.
*/
export function decode<ContextType = undefined>(
buffer: ArrayLike<number> | BufferSource,
Expand All @@ -67,6 +70,9 @@ export function decode<ContextType = undefined>(
/**
* It decodes multiple MessagePack objects in a buffer.
* This is corresponding to {@link decodeMultiStream()}.
*
* @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty.
* @throws {@link DecodeError} if the buffer contains invalid data.
*/
export function decodeMulti<ContextType = undefined>(
buffer: ArrayLike<number> | BufferSource,
Expand Down
16 changes: 14 additions & 2 deletions src/decodeAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import type { ReadableStreamLike } from "./utils/stream";
import type { DecodeOptions } from "./decode";
import type { SplitUndefined } from "./context";

export async function decodeAsync<ContextType>(
/**
* @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty.
* @throws {@link DecodeError} if the buffer contains invalid data.
*/
export async function decodeAsync<ContextType>(
streamLike: ReadableStreamLike<ArrayLike<number> | BufferSource>,
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any,
): Promise<unknown> {
Expand All @@ -23,7 +27,11 @@ export async function decodeAsync<ContextType>(
return decoder.decodeAsync(stream);
}

export function decodeArrayStream<ContextType>(
/**
* @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty.
* @throws {@link DecodeError} if the buffer contains invalid data.
*/
export function decodeArrayStream<ContextType>(
streamLike: ReadableStreamLike<ArrayLike<number> | BufferSource>,
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any,
): AsyncGenerator<unknown, void, unknown> {
Expand All @@ -42,6 +50,10 @@ export function decodeArrayStream<ContextType>(
return decoder.decodeArrayStream(stream);
}

/**
* @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty.
* @throws {@link DecodeError} if the buffer contains invalid data.
*/
export function decodeMultiStream<ContextType>(
streamLike: ReadableStreamLike<ArrayLike<number> | BufferSource>,
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any,
Expand Down
20 changes: 19 additions & 1 deletion test/edge-cases.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// kind of hand-written fuzzing data
// any errors should not break Encoder/Decoder instance states
import assert from "assert";
import { encode, decode, Encoder, Decoder } from "../src";
import { encode, decodeAsync, decode, Encoder, Decoder } from "../src";
import { DataViewIndexOutOfBoundsError } from "../src/Decoder";

function testEncoder(encoder: Encoder): void {
Expand Down Expand Up @@ -147,4 +147,22 @@ describe("edge cases", () => {
testDecoder(decoder);
});
});

context("try to decode an empty input", () => {
it("throws RangeError (synchronous)", () => {
assert.throws(() => {
decode([]);
}, RangeError);
});

it("throws RangeError (asynchronous)", async () => {
const createStream = async function* () {
yield [];
};

assert.rejects(async () => {
await decodeAsync(createStream());
}, RangeError);
});
});
});

0 comments on commit 32654ee

Please sign in to comment.