@ts-pf/stream 0.1.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +2 -0
- package/dist/codec.d.ts +2 -3
- package/dist/codec.d.ts.map +1 -1
- package/dist/codec.js +19 -55
- package/dist/codec.js.map +1 -1
- package/dist/item-stream.d.ts +7 -0
- package/dist/item-stream.d.ts.map +1 -0
- package/dist/item-stream.js +11 -0
- package/dist/item-stream.js.map +1 -0
- package/dist/schema.js +2 -2
- package/dist/schema.js.map +1 -1
- package/package.json +11 -8
- package/skills/ts-pf-stream/SKILL.md +4 -2
- package/dist/is-async-iterable.d.ts +0 -2
- package/dist/is-async-iterable.d.ts.map +0 -1
- package/dist/is-async-iterable.js +0 -12
- package/dist/is-async-iterable.js.map +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ts-pf contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -5,6 +5,8 @@ Opt-in `StreamCodec` for root `AsyncIterable` input and output. JSON calls stay
|
|
|
5
5
|
Agent skill: [`skills/ts-pf-stream/`](skills/ts-pf-stream/). Sync with `npx skills experimental_sync -y`.
|
|
6
6
|
|
|
7
7
|
```ts
|
|
8
|
+
import { procedure } from '@ts-pf/contract'
|
|
9
|
+
import { z } from 'zod'
|
|
8
10
|
import { stream, StreamCodec } from '@ts-pf/stream'
|
|
9
11
|
|
|
10
12
|
import { FetchHandler } from '@ts-pf/server-http'
|
package/dist/codec.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { type RpcBodySource, type RpcCodec, type RpcEncodedBody } from '@ts-pf/http';
|
|
1
|
+
import { type EnvelopeTextCodec, type RpcBodySource, type RpcCodec, type RpcEncodedBody } from '@ts-pf/http';
|
|
2
2
|
import { type RpcRequest, type RpcResponse } from '@ts-pf/protocol';
|
|
3
3
|
export declare class StreamCodec implements RpcCodec {
|
|
4
4
|
private readonly inner;
|
|
5
5
|
constructor(options?: {
|
|
6
|
-
inner?:
|
|
6
|
+
inner?: EnvelopeTextCodec;
|
|
7
7
|
});
|
|
8
8
|
encodeRequest(req: RpcRequest): Promise<RpcEncodedBody>;
|
|
9
9
|
decodeRequest(source: RpcBodySource): Promise<RpcRequest>;
|
|
@@ -16,6 +16,5 @@ export declare class StreamCodec implements RpcCodec {
|
|
|
16
16
|
decodeResponse<T = unknown>(source: RpcBodySource): Promise<RpcResponse<T>>;
|
|
17
17
|
private readInputLines;
|
|
18
18
|
private readOutputLines;
|
|
19
|
-
private failureLine;
|
|
20
19
|
}
|
|
21
20
|
//# sourceMappingURL=codec.d.ts.map
|
package/dist/codec.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,iBAAiB,EAKtB,KAAK,aAAa,EAClB,KAAK,QAAQ,EACb,KAAK,cAAc,EACpB,MAAM,aAAa,CAAA;AACpB,OAAO,EAGL,KAAK,UAAU,EACf,KAAK,WAAW,EAEjB,MAAM,iBAAiB,CAAA;AA+CxB,qBAAa,WAAY,YAAW,QAAQ;IAC1C,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAmB;gBAE7B,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,iBAAiB,CAAA;KAAE;IAI7C,aAAa,CAAC,GAAG,EAAE,UAAU,GAAG,OAAO,CAAC,cAAc,CAAC;IAkBvD,aAAa,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,UAAU,CAAC;IAgBzD,aAAa,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG,OAAO,CAAC,cAAc,CAAC;IAkB1D,aAAa,CAAC,KAAK,EAAE;QACnB,IAAI,EAAE,MAAM,CAAA;QACZ,OAAO,EAAE,MAAM,CAAA;QACf,IAAI,CAAC,EAAE,OAAO,CAAA;KACf,GAAG,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC;IAItC,cAAc,CAAC,CAAC,GAAG,OAAO,EAC9B,MAAM,EAAE,aAAa,GACpB,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;YAcX,cAAc;YASd,eAAe;CAiB/B"}
|
package/dist/codec.js
CHANGED
|
@@ -1,36 +1,12 @@
|
|
|
1
|
-
import { JSONCodec, } from '@ts-pf/http';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { isJsonContentType, JSONCodec, jsonBodySource, mimeType, } from '@ts-pf/http';
|
|
2
|
+
import { PFError, toWireError, } from '@ts-pf/protocol';
|
|
3
|
+
import { isItemStream } from './item-stream.js';
|
|
4
4
|
import { badRequest, encodeJsonl, JSONL_CONTENT_TYPE, readJsonlLines, } from './jsonl.js';
|
|
5
|
-
function mime(contentType) {
|
|
6
|
-
if (contentType == null || contentType === '') {
|
|
7
|
-
return '';
|
|
8
|
-
}
|
|
9
|
-
return contentType.split(';')[0]?.trim().toLowerCase() ?? '';
|
|
10
|
-
}
|
|
11
|
-
function isJsonContentType(contentType) {
|
|
12
|
-
const type = mime(contentType);
|
|
13
|
-
return type === '' || type === 'application/json';
|
|
14
|
-
}
|
|
15
5
|
function isJsonlContentType(contentType) {
|
|
16
|
-
return
|
|
17
|
-
}
|
|
18
|
-
function jsonSource(text) {
|
|
19
|
-
return {
|
|
20
|
-
contentType: 'application/json',
|
|
21
|
-
text: async () => text,
|
|
22
|
-
formData: async () => new FormData(),
|
|
23
|
-
body: () => null,
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
async function jsonText(encoded) {
|
|
27
|
-
if (typeof encoded.body === 'string') {
|
|
28
|
-
return encoded.body;
|
|
29
|
-
}
|
|
30
|
-
throw new Error('StreamCodec inner codec must produce a JSON string');
|
|
6
|
+
return mimeType(contentType) === JSONL_CONTENT_TYPE;
|
|
31
7
|
}
|
|
32
8
|
function assertItem(value) {
|
|
33
|
-
if (
|
|
9
|
+
if (isItemStream(value)) {
|
|
34
10
|
throw badRequest('Nested streams are not supported');
|
|
35
11
|
}
|
|
36
12
|
if (typeof Blob !== 'undefined' && value instanceof Blob) {
|
|
@@ -38,7 +14,7 @@ function assertItem(value) {
|
|
|
38
14
|
}
|
|
39
15
|
}
|
|
40
16
|
function assertNoNestedStreams(value, atRoot) {
|
|
41
|
-
if (
|
|
17
|
+
if (isItemStream(value)) {
|
|
42
18
|
if (!atRoot) {
|
|
43
19
|
throw badRequest('Nested streams are not supported');
|
|
44
20
|
}
|
|
@@ -67,13 +43,13 @@ export class StreamCodec {
|
|
|
67
43
|
this.inner = options?.inner ?? new JSONCodec();
|
|
68
44
|
}
|
|
69
45
|
async encodeRequest(req) {
|
|
70
|
-
if (
|
|
46
|
+
if (isItemStream(req.input)) {
|
|
71
47
|
return {
|
|
72
48
|
contentType: JSONL_CONTENT_TYPE,
|
|
73
49
|
body: encodeJsonl(req.input, async (item) => {
|
|
74
50
|
assertItem(item);
|
|
75
|
-
return
|
|
76
|
-
},
|
|
51
|
+
return (await this.inner.encodeRequest({ input: item })).body;
|
|
52
|
+
}, failureLine),
|
|
77
53
|
};
|
|
78
54
|
}
|
|
79
55
|
assertNoNestedStreams(req.input, true);
|
|
@@ -95,13 +71,13 @@ export class StreamCodec {
|
|
|
95
71
|
return this.inner.decodeRequest(source);
|
|
96
72
|
}
|
|
97
73
|
async encodeSuccess(output) {
|
|
98
|
-
if (
|
|
74
|
+
if (isItemStream(output)) {
|
|
99
75
|
return {
|
|
100
76
|
contentType: JSONL_CONTENT_TYPE,
|
|
101
77
|
body: encodeJsonl(output, async (item) => {
|
|
102
78
|
assertItem(item);
|
|
103
|
-
return
|
|
104
|
-
},
|
|
79
|
+
return (await this.inner.encodeSuccess(item)).body;
|
|
80
|
+
}, failureLine),
|
|
105
81
|
};
|
|
106
82
|
}
|
|
107
83
|
assertNoNestedStreams(output, true);
|
|
@@ -125,17 +101,16 @@ export class StreamCodec {
|
|
|
125
101
|
}
|
|
126
102
|
async *readInputLines(stream) {
|
|
127
103
|
for await (const line of readJsonlLines(stream)) {
|
|
128
|
-
const decoded = await this.inner.decodeRequest(
|
|
104
|
+
const decoded = await this.inner.decodeRequest(jsonBodySource(line));
|
|
129
105
|
yield decoded.input;
|
|
130
106
|
}
|
|
131
107
|
}
|
|
132
108
|
async *readOutputLines(stream) {
|
|
133
109
|
for await (const line of readJsonlLines(stream)) {
|
|
134
|
-
const decoded = await this.inner.decodeResponse(
|
|
110
|
+
const decoded = await this.inner.decodeResponse(jsonBodySource(line));
|
|
135
111
|
if (!decoded.ok) {
|
|
136
112
|
throw new PFError({
|
|
137
113
|
code: decoded.error.code,
|
|
138
|
-
status: 200,
|
|
139
114
|
message: decoded.error.message,
|
|
140
115
|
...(decoded.error.data !== undefined
|
|
141
116
|
? { data: decoded.error.data }
|
|
@@ -145,21 +120,10 @@ export class StreamCodec {
|
|
|
145
120
|
yield decoded.output;
|
|
146
121
|
}
|
|
147
122
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
};
|
|
154
|
-
if (error.data !== undefined) {
|
|
155
|
-
payload.data = error.data;
|
|
156
|
-
}
|
|
157
|
-
return JSON.stringify({ ok: false, error: payload });
|
|
158
|
-
}
|
|
159
|
-
return JSON.stringify({
|
|
160
|
-
ok: false,
|
|
161
|
-
error: { code: 'INTERNAL', message: 'Internal server error' },
|
|
162
|
-
});
|
|
163
|
-
}
|
|
123
|
+
}
|
|
124
|
+
/** In-band failure after the stream started: an unknown throw is `INTERNAL`. */
|
|
125
|
+
function failureLine(error) {
|
|
126
|
+
const failure = { ok: false, error: toWireError(error) };
|
|
127
|
+
return JSON.stringify(failure);
|
|
164
128
|
}
|
|
165
129
|
//# sourceMappingURL=codec.js.map
|
package/dist/codec.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codec.js","sourceRoot":"","sources":["../src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"codec.js","sourceRoot":"","sources":["../src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,iBAAiB,EACjB,SAAS,EACT,cAAc,EACd,QAAQ,GAIT,MAAM,aAAa,CAAA;AACpB,OAAO,EACL,OAAO,EAIP,WAAW,GACZ,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAA;AAC/C,OAAO,EACL,UAAU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is async-iterable
|
|
3
|
+
* on Node but carries bytes, not items, so it is never framed as JSONL; it
|
|
4
|
+
* stays a plain value for the inner codec, as before.
|
|
5
|
+
*/
|
|
6
|
+
export declare function isItemStream(value: unknown): value is AsyncIterable<unknown>;
|
|
7
|
+
//# sourceMappingURL=item-stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"item-stream.d.ts","sourceRoot":"","sources":["../src/item-stream.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,aAAa,CAAC,OAAO,CAAC,CAK5E"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { isAsyncIterable } from '@ts-pf/protocol';
|
|
2
|
+
/**
|
|
3
|
+
* An async iterable of envelope items. A `ReadableStream` is async-iterable
|
|
4
|
+
* on Node but carries bytes, not items, so it is never framed as JSONL; it
|
|
5
|
+
* stays a plain value for the inner codec, as before.
|
|
6
|
+
*/
|
|
7
|
+
export function isItemStream(value) {
|
|
8
|
+
return (isAsyncIterable(value) &&
|
|
9
|
+
!(typeof ReadableStream !== 'undefined' && value instanceof ReadableStream));
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=item-stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"item-stream.js","sourceRoot":"","sources":["../src/item-stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAEjD;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,OAAO,CACL,eAAe,CAAC,KAAK,CAAC;QACtB,CAAC,CAAC,OAAO,cAAc,KAAK,WAAW,IAAI,KAAK,YAAY,cAAc,CAAC,CAC5E,CAAA;AACH,CAAC"}
|
package/dist/schema.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { validateSchema } from '@ts-pf/contract';
|
|
2
2
|
import { PFError } from '@ts-pf/protocol';
|
|
3
|
-
import {
|
|
3
|
+
import { isItemStream } from './item-stream.js';
|
|
4
4
|
export function stream(item) {
|
|
5
5
|
const schema = {
|
|
6
6
|
'~standard': {
|
|
@@ -11,7 +11,7 @@ export function stream(item) {
|
|
|
11
11
|
output: 0,
|
|
12
12
|
},
|
|
13
13
|
validate: async (value) => {
|
|
14
|
-
if (!
|
|
14
|
+
if (!isItemStream(value)) {
|
|
15
15
|
return { issues: [{ message: 'Expected an async iterable' }] };
|
|
16
16
|
}
|
|
17
17
|
return { value: wrap(value, item) };
|
package/dist/schema.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AACA,OAAO,EAA0B,cAAc,EAAE,MAAM,iBAAiB,CAAA;AACxE,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAA;AACzC,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AACA,OAAO,EAA0B,cAAc,EAAE,MAAM,iBAAiB,CAAA;AACxE,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAA;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAA;AAE/C,MAAM,UAAU,MAAM,CACpB,IAAO;IAKP,MAAM,MAAM,GAGuB;QACjC,WAAW,EAAE;YACX,OAAO,EAAE,CAAC;YACV,MAAM,EAAE,OAAO;YACf,KAAK,EAAE;gBACL,KAAK,EAAE,CAAmD;gBAC1D,MAAM,EAAE,CAAmD;aAC5D;YACD,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE;gBACxB,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,CAAC;oBACzB,OAAO,EAAE,MAAM,EAAE,CAAC,EAAE,OAAO,EAAE,4BAA4B,EAAE,CAAC,EAAE,CAAA;gBAChE,CAAC;gBACD,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,EAAE,CAAA;YACrC,CAAC;SACF;QACD,WAAW,EAAE,EAAE,IAAI,EAAE;KACtB,CAAA;IACD,OAAO,MAAM,CAAA;AACf,CAAC;AAED,KAAK,SAAS,CAAC,CAAC,IAAI,CAClB,KAA6B,EAC7B,IAAO;IAEP,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,KAAK,EAAE,CAAC;QAChC,MAAM,MAAM,GAAG,MAAM,cAAc,CAAuB,IAAI,EAAE,KAAK,CAAC,CAAA;QACtE,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;YACpB,MAAM,IAAI,OAAO,CAAC;gBAChB,IAAI,EAAE,YAAY;gBAClB,MAAM,EAAE,GAAG;gBACX,OAAO,EAAE,mBAAmB;gBAC5B,IAAI,EAAE,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE;aAChC,CAAC,CAAA;QACJ,CAAC;QACD,MAAM,MAAM,CAAC,KAAK,CAAA;IACpB,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ts-pf/stream",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Opt-in StreamCodec and stream() for root AsyncIterable (JSONL)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ts-pf",
|
|
@@ -15,6 +15,9 @@
|
|
|
15
15
|
"async-iterable"
|
|
16
16
|
],
|
|
17
17
|
"license": "MIT",
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=22"
|
|
20
|
+
},
|
|
18
21
|
"repository": {
|
|
19
22
|
"type": "git",
|
|
20
23
|
"url": "git+https://github.com/crobinson42/ts-pf.git",
|
|
@@ -47,15 +50,15 @@
|
|
|
47
50
|
},
|
|
48
51
|
"dependencies": {
|
|
49
52
|
"@standard-schema/spec": "^1.0.0",
|
|
50
|
-
"@ts-pf/contract": "0.
|
|
51
|
-
"@ts-pf/http": "0.
|
|
52
|
-
"@ts-pf/protocol": "0.
|
|
53
|
+
"@ts-pf/contract": "0.2.0",
|
|
54
|
+
"@ts-pf/http": "0.2.0",
|
|
55
|
+
"@ts-pf/protocol": "0.2.0"
|
|
53
56
|
},
|
|
54
57
|
"devDependencies": {
|
|
55
|
-
"@ts-pf/client": "0.
|
|
56
|
-
"@ts-pf/client-http": "0.
|
|
57
|
-
"@ts-pf/server": "0.
|
|
58
|
-
"@ts-pf/server-http": "0.
|
|
58
|
+
"@ts-pf/client": "0.2.0",
|
|
59
|
+
"@ts-pf/client-http": "0.2.0",
|
|
60
|
+
"@ts-pf/server": "0.4.0",
|
|
61
|
+
"@ts-pf/server-http": "0.3.0",
|
|
59
62
|
"zod": "^4.0.0"
|
|
60
63
|
}
|
|
61
64
|
}
|
|
@@ -14,6 +14,8 @@ Link for agents: `npx skills experimental_sync -y`
|
|
|
14
14
|
## Do
|
|
15
15
|
|
|
16
16
|
```ts
|
|
17
|
+
import { procedure } from '@ts-pf/contract'
|
|
18
|
+
import { z } from 'zod'
|
|
17
19
|
import { stream, StreamCodec } from '@ts-pf/stream'
|
|
18
20
|
import { FetchHandler } from '@ts-pf/server-http'
|
|
19
21
|
import { FetchLink } from '@ts-pf/client-http'
|
|
@@ -25,9 +27,9 @@ new FetchLink({ url: '/rpc', codec })
|
|
|
25
27
|
procedure.output(stream(z.object({ token: z.string() })))
|
|
26
28
|
```
|
|
27
29
|
|
|
28
|
-
Root only. Same codec on handler and link. Item checks follow the server validation flags. Stream input items are checked unless `validation.input` is `false`. Stream output items are checked only when `validation.output` is `true`:
|
|
30
|
+
Root only. Same codec on handler and link. A failure line after the stream started rejects the iterator with a `PFError` whose `status` is `undefined` (the response was already 200); switch on `code`. Item checks follow the server validation flags. Stream input items are checked unless `validation.input` is `false`. Stream output items are checked only when `validation.output` is `true`:
|
|
29
31
|
|
|
30
|
-
```ts
|
|
32
|
+
```ts no-check
|
|
31
33
|
new FetchHandler(app, { codec, validation: { output: true } })
|
|
32
34
|
```
|
|
33
35
|
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"is-async-iterable.d.ts","sourceRoot":"","sources":["../src/is-async-iterable.ts"],"names":[],"mappings":"AAAA,wBAAgB,eAAe,CAC7B,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,aAAa,CAAC,OAAO,CAAC,CAcjC"}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export function isAsyncIterable(value) {
|
|
2
|
-
if (typeof ReadableStream !== 'undefined' &&
|
|
3
|
-
value instanceof ReadableStream) {
|
|
4
|
-
return false;
|
|
5
|
-
}
|
|
6
|
-
return (typeof value === 'object' &&
|
|
7
|
-
value !== null &&
|
|
8
|
-
Symbol.asyncIterator in value &&
|
|
9
|
-
typeof value[Symbol.asyncIterator] ===
|
|
10
|
-
'function');
|
|
11
|
-
}
|
|
12
|
-
//# sourceMappingURL=is-async-iterable.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"is-async-iterable.js","sourceRoot":"","sources":["../src/is-async-iterable.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,eAAe,CAC7B,KAAc;IAEd,IACE,OAAO,cAAc,KAAK,WAAW;QACrC,KAAK,YAAY,cAAc,EAC/B,CAAC;QACD,OAAO,KAAK,CAAA;IACd,CAAC;IACD,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,MAAM,CAAC,aAAa,IAAI,KAAK;QAC7B,OAAQ,KAAgC,CAAC,MAAM,CAAC,aAAa,CAAC;YAC5D,UAAU,CACb,CAAA;AACH,CAAC"}
|