Skip to content

Commit 8d59417

Browse files
Add decodeStream (#46)
* Add decodeStream * Fix code review comments * Fix codereview
1 parent 9fd6111 commit 8d59417

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

src/Decoder.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,25 @@ export class Decoder {
140140
);
141141
}
142142

143+
async *decodeStream(stream: AsyncIterable<ArrayLike<number> | Uint8Array>) {
144+
for await (const buffer of stream) {
145+
this.appendBuffer(buffer);
146+
147+
try {
148+
while (true) {
149+
const result = this.decodeSync();
150+
151+
yield result;
152+
}
153+
} catch (e) {
154+
if (!(e instanceof DataViewIndexOutOfBoundsError)) {
155+
throw e; // rethrow
156+
}
157+
// fallthrough
158+
}
159+
}
160+
}
161+
143162
async *decodeArrayStream(stream: AsyncIterable<ArrayLike<number> | Uint8Array>) {
144163
let headerParsed = false;
145164
let decoded = false;
@@ -160,7 +179,7 @@ export class Decoder {
160179

161180
try {
162181
while (true) {
163-
let result = this.decodeSync();
182+
const result = this.decodeSync();
164183

165184
yield result;
166185

src/decodeAsync.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export async function decodeAsync(
2222
return decoder.decodeOneAsync(stream);
2323
}
2424

25-
export async function* decodeArrayStream(
25+
export function decodeArrayStream(
2626
streamLike: ReadableStreamLike<Uint8Array | ArrayLike<number>>,
2727
options: DecodeAsyncOptions = defaultDecodeOptions,
2828
) {
@@ -37,7 +37,23 @@ export async function* decodeArrayStream(
3737
options.maxExtLength,
3838
);
3939

40-
for await (let item of decoder.decodeArrayStream(stream)) {
41-
yield item;
42-
}
40+
return decoder.decodeArrayStream(stream);
41+
}
42+
43+
export function decodeStream(
44+
streamLike: ReadableStreamLike<Uint8Array | ArrayLike<number>>,
45+
options: DecodeAsyncOptions = defaultDecodeOptions,
46+
) {
47+
const stream = ensureAsyncIterabe(streamLike);
48+
49+
const decoder = new Decoder(
50+
options.extensionCodec,
51+
options.maxStrLength,
52+
options.maxBinLength,
53+
options.maxArrayLength,
54+
options.maxMapLength,
55+
options.maxExtLength,
56+
);
57+
58+
return decoder.decodeStream(stream);
4359
}

test/decodeStream.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import assert from "assert";
2+
import { encode } from "../src";
3+
import { decodeStream } from "../src/decodeAsync";
4+
5+
describe("decodeStream", () => {
6+
it("decodes stream", async () => {
7+
const items = [
8+
"foo",
9+
10,
10+
{
11+
name: "bar",
12+
},
13+
[1, 2, 3],
14+
];
15+
16+
const createStream = async function*() {
17+
for (const item of items) {
18+
yield encode(item);
19+
}
20+
};
21+
22+
const result = [];
23+
24+
for await (const item of decodeStream(createStream())) {
25+
result.push(item);
26+
}
27+
28+
assert.deepStrictEqual(result, items);
29+
});
30+
});

0 commit comments

Comments
 (0)