Skip to content

Commit c978f78

Browse files
committed
Commit
1 parent df04116 commit c978f78

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

json-parse-stream.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,21 @@ export class JSONParseStream<T = any> extends TransformStream<string | BufferSou
5555
});
5656
}
5757

58-
promise<T>(jsonPath: string): Promise<T> {
58+
promise<T = any>(jsonPath: string): Promise<T> {
5959
if (this.readable.locked) throw Error('Already locked')
6060
const p = new ResolvablePromise<T>()
6161
this.#promises.set(normalize(jsonPath), p);
62-
return p;
62+
return Promise.resolve(p);
6363
}
6464

65-
generator<T>(jsonPath: string): AsyncIterableIterator<T> {
65+
iterable<T = any>(jsonPath: string): AsyncIterableIterator<T> {
6666
if (this.readable.locked) throw Error('Already locked')
6767
const q = new AsyncQueue<T>()
6868
this.#queues.set(normalize(jsonPath), q)
69-
return q;
69+
return identity(q);
7070
}
7171

72-
stream<T>(jsonPath: string): ReadableStream<T> {
73-
return asyncIterToStream(this.generator(jsonPath));
72+
stream<T = any>(jsonPath: string): ReadableStream<T> {
73+
return asyncIterToStream(this.iterable(jsonPath));
7474
}
7575
}

test/json-parse-stream.test.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// deno-lint-ignore-file no-explicit-any no-unused-vars require-await
1+
// deno-lint-ignore-file no-explicit-any no-unused-vars require-await ban-unused-ignore
22
import 'https://gist.githubusercontent.com/qwtel/b14f0f81e3a96189f7771f83ee113f64/raw/TestRequest.ts'
33
import {
44
assert,
@@ -78,7 +78,7 @@ test('promise value', async () => {
7878
const parseStream = new JSONParseStream()
7979
const actual = {
8080
type: parseStream.promise('$.type'),
81-
data: parseStream.generator('$.data.*')
81+
data: parseStream.iterable('$.data.*')
8282
}
8383
const expected = JSON.stringify({ type: 'foo', data: [{ a: 1 }, { b: 2 }, { c: 3 }] })
8484
const done = new Response(expected).body!
@@ -88,3 +88,18 @@ test('promise value', async () => {
8888
assertEquals(await aJoin(jsonStringifyGenerator(actual)), expected)
8989
await done;
9090
})
91+
92+
test('promise value II', async () => {
93+
const parseStream = new JSONParseStream()
94+
const actual = {
95+
type: parseStream.promise('$.type'),
96+
data: parseStream.stream('$.data.*')
97+
}
98+
const expected = JSON.stringify({ type: 'foo', data: [{ a: 1 }, { b: 2 }, { c: 3 }] })
99+
const done = new Response(expected).body!
100+
.pipeThrough(parseStream)
101+
.pipeTo(new WritableStream())
102+
103+
assertEquals(await aJoin(jsonStringifyGenerator(actual)), expected)
104+
await done;
105+
})

0 commit comments

Comments
 (0)