Skip to content

Commit 0017670

Browse files
committed
Commit
1 parent 987cc9c commit 0017670

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

json-parse-stream.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export class JSONParseNexus<T = any> extends TransformStream<string | Uint8Array
7979
promise<T = any>(jsonPath: string): JSONParseLazyPromise<T | undefined> {
8080
const stream = this.stream(jsonPath);
8181
return JSONParseLazyPromise.from(async () => {
82+
if (!this.writable.locked) throw TypeError('Cannot await result while there\'s no data source.')
8283
const x = await stream.getReader().read();
8384
return x.done ? undefined : x.value;
8485
})
@@ -91,6 +92,7 @@ export class JSONParseNexus<T = any> extends TransformStream<string | Uint8Array
9192
this.#queues.set(path, queue)
9293
},
9394
pull: async () => {
95+
if (!this.writable.locked) throw TypeError('Cannot await result while there\'s no data source.')
9496
while (true) {
9597
const { done, value } = await this.#reader.read();
9698
// FIXME: avoid duplicate match

test/json-parse-stream.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,21 @@ test('from file/fetch', async () => {
270270
assertEquals((await aCollect(fillerS)).length, 300)
271271
assertEquals((await aCollect(itemsS)).length, 300)
272272
})
273+
274+
test('promise throws without data source', () => {
275+
const nexus = new JSONParseNexus();
276+
const type = nexus.promise('$.type')
277+
assertRejects(() => type, TypeError)
278+
})
279+
280+
test('streams throws without data source', () => {
281+
const nexus = new JSONParseNexus();
282+
const items = nexus.stream('$.items.*')
283+
assertRejects(() => items.getReader().read(), TypeError)
284+
})
285+
286+
test('iterations throws without data source', () => {
287+
const nexus = new JSONParseNexus();
288+
const items = nexus.iterable('$.items.*')
289+
assertRejects(() => items.next(), TypeError)
290+
})

0 commit comments

Comments
 (0)