Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add decodeStream #46

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

Merged
sergeyzenchenko merged 5 commits into master from decodeStream
May 30, 2019
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
21 changes: 20 additions & 1 deletion src/Decoder.ts
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ export class Decoder {
);
}

async *decodeStream(stream: AsyncIterable<ArrayLike<number> | Uint8Array>) {
for await (const buffer of stream) {
this.appendBuffer(buffer);

try {
while (true) {
const result = this.decodeSync();

yield result;
}
} catch (e) {
if (!(e instanceof DataViewIndexOutOfBoundsError)) {
throw e; // rethrow
}
// fallthrough
}
}
}

async *decodeArrayStream(stream: AsyncIterable<ArrayLike<number> | Uint8Array>) {
let headerParsed = false;
let decoded = false;
Expand All @@ -160,7 +179,7 @@ export class Decoder {

try {
while (true) {
let result = this.decodeSync();
const result = this.decodeSync();

yield result;

Expand Down
24 changes: 20 additions & 4 deletions src/decodeAsync.ts
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function decodeAsync(
return decoder.decodeOneAsync(stream);
}

export async function* decodeArrayStream(
export function decodeArrayStream(
streamLike: ReadableStreamLike<Uint8Array | ArrayLike<number>>,
options: DecodeAsyncOptions = defaultDecodeOptions,
) {
Expand All @@ -37,7 +37,23 @@ export async function* decodeArrayStream(
options.maxExtLength,
);

for await (let item of decoder.decodeArrayStream(stream)) {
yield item;
}
return decoder.decodeArrayStream(stream);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

export function decodeStream(
streamLike: ReadableStreamLike<Uint8Array | ArrayLike<number>>,
options: DecodeAsyncOptions = defaultDecodeOptions,
) {
const stream = ensureAsyncIterabe(streamLike);

const decoder = new Decoder(
options.extensionCodec,
options.maxStrLength,
options.maxBinLength,
options.maxArrayLength,
options.maxMapLength,
options.maxExtLength,
);

return decoder.decodeStream(stream);
}
30 changes: 30 additions & 0 deletions test/decodeStream.test.ts
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import assert from "assert";
import { encode } from "../src";
import { decodeStream } from "../src/decodeAsync";

describe("decodeStream", () => {
it("decodes stream", async () => {
const items = [
"foo",
10,
{
name: "bar",
},
[1, 2, 3],
];

const createStream = async function*() {
for (const item of items) {
yield encode(item);
}
};

const result = [];

for await (const item of decodeStream(createStream())) {
result.push(item);
}

assert.deepStrictEqual(result, items);
});
});

AltStyle によって変換されたページ (->オリジナル) /