Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[doc] clarify the edge cases where the input buffer is empty #209

Merged
merged 1 commit into from
May 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
});