@ts-pf/stream 0.1.0-beta.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/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # @ts-pf/stream
2
+
3
+ Opt-in `StreamCodec` for root `AsyncIterable` input and output. JSON calls stay JSON. Streams are `application/jsonl` — one existing RPC envelope per line.
4
+
5
+ Agent skill: [`skills/ts-pf-stream/`](skills/ts-pf-stream/). Sync with `npx skills experimental_sync -y`.
6
+
7
+ ```ts
8
+ import { stream, StreamCodec } from '@ts-pf/stream'
9
+
10
+ import { FetchHandler } from '@ts-pf/server-http'
11
+ import { FetchLink } from '@ts-pf/client-http'
12
+
13
+ const codec = new StreamCodec()
14
+ new FetchHandler(app, { codec })
15
+ new FetchLink({ url: '/rpc', codec })
16
+
17
+ procedure.output(stream(z.object({ token: z.string() })))
18
+ ```
19
+
20
+ Wire spec: [`packages/protocol/PROTOCOL.md`](../protocol/PROTOCOL.md) (Message streams). SSE output framing of the same envelopes is [`@ts-pf/sse`](../sse) (`SseCodec`); `stream()` is not re-exported there.
@@ -0,0 +1,21 @@
1
+ import { type RpcBodySource, type RpcCodec, type RpcEncodedBody } from '@ts-pf/http';
2
+ import { type RpcRequest, type RpcResponse } from '@ts-pf/protocol';
3
+ export declare class StreamCodec implements RpcCodec {
4
+ private readonly inner;
5
+ constructor(options?: {
6
+ inner?: RpcCodec;
7
+ });
8
+ encodeRequest(req: RpcRequest): Promise<RpcEncodedBody>;
9
+ decodeRequest(source: RpcBodySource): Promise<RpcRequest>;
10
+ encodeSuccess<T>(output: T): Promise<RpcEncodedBody>;
11
+ encodeFailure(error: {
12
+ code: string;
13
+ message: string;
14
+ data?: unknown;
15
+ }): RpcEncodedBody | Promise<RpcEncodedBody>;
16
+ decodeResponse<T = unknown>(source: RpcBodySource): Promise<RpcResponse<T>>;
17
+ private readInputLines;
18
+ private readOutputLines;
19
+ private failureLine;
20
+ }
21
+ //# sourceMappingURL=codec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,aAAa,EAClB,KAAK,QAAQ,EACb,KAAK,cAAc,EACpB,MAAM,aAAa,CAAA;AACpB,OAAO,EAGL,KAAK,UAAU,EACf,KAAK,WAAW,EACjB,MAAM,iBAAiB,CAAA;AA2ExB,qBAAa,WAAY,YAAW,QAAQ;IAC1C,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAU;gBAEpB,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,QAAQ,CAAA;KAAE;IAIpC,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;IAmB9B,OAAO,CAAC,WAAW;CAgBpB"}
package/dist/codec.js ADDED
@@ -0,0 +1,165 @@
1
+ import { JSONCodec, } from '@ts-pf/http';
2
+ import { isPFError, PFError, } from '@ts-pf/protocol';
3
+ import { isAsyncIterable } from './is-async-iterable.js';
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
+ function isJsonlContentType(contentType) {
16
+ return mime(contentType) === JSONL_CONTENT_TYPE;
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');
31
+ }
32
+ function assertItem(value) {
33
+ if (isAsyncIterable(value)) {
34
+ throw badRequest('Nested streams are not supported');
35
+ }
36
+ if (typeof Blob !== 'undefined' && value instanceof Blob) {
37
+ throw badRequest('File values are not supported in streams');
38
+ }
39
+ }
40
+ function assertNoNestedStreams(value, atRoot) {
41
+ if (isAsyncIterable(value)) {
42
+ if (!atRoot) {
43
+ throw badRequest('Nested streams are not supported');
44
+ }
45
+ return;
46
+ }
47
+ if (value === null || typeof value !== 'object') {
48
+ return;
49
+ }
50
+ if (Array.isArray(value)) {
51
+ for (const child of value) {
52
+ assertNoNestedStreams(child, false);
53
+ }
54
+ return;
55
+ }
56
+ const proto = Object.getPrototypeOf(value);
57
+ if (proto !== Object.prototype && proto !== null) {
58
+ return;
59
+ }
60
+ for (const child of Object.values(value)) {
61
+ assertNoNestedStreams(child, false);
62
+ }
63
+ }
64
+ export class StreamCodec {
65
+ inner;
66
+ constructor(options) {
67
+ this.inner = options?.inner ?? new JSONCodec();
68
+ }
69
+ async encodeRequest(req) {
70
+ if (isAsyncIterable(req.input)) {
71
+ return {
72
+ contentType: JSONL_CONTENT_TYPE,
73
+ body: encodeJsonl(req.input, async (item) => {
74
+ assertItem(item);
75
+ return jsonText(await this.inner.encodeRequest({ input: item }));
76
+ }, (error) => this.failureLine(error)),
77
+ };
78
+ }
79
+ assertNoNestedStreams(req.input, true);
80
+ return this.inner.encodeRequest(req);
81
+ }
82
+ async decodeRequest(source) {
83
+ if (isJsonlContentType(source.contentType)) {
84
+ const stream = source.body();
85
+ if (!stream) {
86
+ throw badRequest('Missing stream body');
87
+ }
88
+ return {
89
+ input: this.readInputLines(stream),
90
+ };
91
+ }
92
+ if (!isJsonContentType(source.contentType)) {
93
+ throw badRequest('Unsupported content type');
94
+ }
95
+ return this.inner.decodeRequest(source);
96
+ }
97
+ async encodeSuccess(output) {
98
+ if (isAsyncIterable(output)) {
99
+ return {
100
+ contentType: JSONL_CONTENT_TYPE,
101
+ body: encodeJsonl(output, async (item) => {
102
+ assertItem(item);
103
+ return jsonText(await this.inner.encodeSuccess(item));
104
+ }, (error) => this.failureLine(error)),
105
+ };
106
+ }
107
+ assertNoNestedStreams(output, true);
108
+ return this.inner.encodeSuccess(output);
109
+ }
110
+ encodeFailure(error) {
111
+ return this.inner.encodeFailure(error);
112
+ }
113
+ async decodeResponse(source) {
114
+ if (isJsonlContentType(source.contentType)) {
115
+ const stream = source.body();
116
+ if (!stream) {
117
+ throw badRequest('Missing stream body');
118
+ }
119
+ return { ok: true, output: this.readOutputLines(stream) };
120
+ }
121
+ if (!isJsonContentType(source.contentType)) {
122
+ throw badRequest('Unsupported content type');
123
+ }
124
+ return this.inner.decodeResponse(source);
125
+ }
126
+ async *readInputLines(stream) {
127
+ for await (const line of readJsonlLines(stream)) {
128
+ const decoded = await this.inner.decodeRequest(jsonSource(line));
129
+ yield decoded.input;
130
+ }
131
+ }
132
+ async *readOutputLines(stream) {
133
+ for await (const line of readJsonlLines(stream)) {
134
+ const decoded = await this.inner.decodeResponse(jsonSource(line));
135
+ if (!decoded.ok) {
136
+ throw new PFError({
137
+ code: decoded.error.code,
138
+ status: 200,
139
+ message: decoded.error.message,
140
+ ...(decoded.error.data !== undefined
141
+ ? { data: decoded.error.data }
142
+ : {}),
143
+ });
144
+ }
145
+ yield decoded.output;
146
+ }
147
+ }
148
+ failureLine(error) {
149
+ if (isPFError(error)) {
150
+ const payload = {
151
+ code: error.code,
152
+ message: error.message,
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
+ }
164
+ }
165
+ //# sourceMappingURL=codec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codec.js","sourceRoot":"","sources":["../src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,GAIV,MAAM,aAAa,CAAA;AACpB,OAAO,EACL,SAAS,EACT,OAAO,GAGR,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAA;AACxD,OAAO,EACL,UAAU,EACV,WAAW,EACX,kBAAkB,EAClB,cAAc,GACf,MAAM,YAAY,CAAA;AAEnB,SAAS,IAAI,CAAC,WAA0B;IACtC,IAAI,WAAW,IAAI,IAAI,IAAI,WAAW,KAAK,EAAE,EAAE,CAAC;QAC9C,OAAO,EAAE,CAAA;IACX,CAAC;IACD,OAAO,WAAW,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,WAAW,EAAE,IAAI,EAAE,CAAA;AAC9D,CAAC;AAED,SAAS,iBAAiB,CAAC,WAA0B;IACnD,MAAM,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,CAAA;IAC9B,OAAO,IAAI,KAAK,EAAE,IAAI,IAAI,KAAK,kBAAkB,CAAA;AACnD,CAAC;AAED,SAAS,kBAAkB,CAAC,WAA0B;IACpD,OAAO,IAAI,CAAC,WAAW,CAAC,KAAK,kBAAkB,CAAA;AACjD,CAAC;AAED,SAAS,UAAU,CAAC,IAAY;IAC9B,OAAO;QACL,WAAW,EAAE,kBAAkB;QAC/B,IAAI,EAAE,KAAK,IAAI,EAAE,CAAC,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AAC;IACH,CAAC;IAEO,WAAW,CAAC,KAAc;QAChC,IAAI,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;YACrB,MAAM,OAAO,GAAsD;gBACjE,IAAI,EAAE,KAAK,CAAC,IAAI;gBAChB,OAAO,EAAE,KAAK,CAAC,OAAO;aACvB,CAAA;YACD,IAAI,KAAK,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;gBAC7B,OAAO,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAA;YAC3B,CAAC;YACD,OAAO,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAA;QACtD,CAAC;QACD,OAAO,IAAI,CAAC,SAAS,CAAC;YACpB,EAAE,EAAE,KAAK;YACT,KAAK,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,uBAAuB,EAAE;SAC9D,CAAC,CAAA;IACJ,CAAC;CACF"}
@@ -0,0 +1,3 @@
1
+ export { StreamCodec } from './codec.js';
2
+ export { stream } from './schema.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AACxC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { StreamCodec } from './codec.js';
2
+ export { stream } from './schema.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AACxC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA"}
@@ -0,0 +1,2 @@
1
+ export declare function isAsyncIterable(value: unknown): value is AsyncIterable<unknown>;
2
+ //# sourceMappingURL=is-async-iterable.d.ts.map
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,12 @@
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
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,6 @@
1
+ import { PFError } from '@ts-pf/protocol';
2
+ export declare const JSONL_CONTENT_TYPE = "application/jsonl";
3
+ export declare function readJsonlLines(stream: ReadableStream<Uint8Array>): AsyncGenerator<string>;
4
+ export declare function encodeJsonl(items: AsyncIterable<unknown>, writeLine: (item: unknown) => string | Promise<string>, onError: (error: unknown) => string): ReadableStream<Uint8Array>;
5
+ export declare function badRequest(message: string): PFError;
6
+ //# sourceMappingURL=jsonl.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsonl.d.ts","sourceRoot":"","sources":["../src/jsonl.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAA;AAEzC,eAAO,MAAM,kBAAkB,sBAAsB,CAAA;AAErD,wBAAuB,cAAc,CACnC,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,GACjC,cAAc,CAAC,MAAM,CAAC,CA4BxB;AAED,wBAAgB,WAAW,CACzB,KAAK,EAAE,aAAa,CAAC,OAAO,CAAC,EAC7B,SAAS,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,EACtD,OAAO,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,MAAM,GAClC,cAAc,CAAC,UAAU,CAAC,CAsB5B;AAED,wBAAgB,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAEnD"}
package/dist/jsonl.js ADDED
@@ -0,0 +1,60 @@
1
+ import { PFError } from '@ts-pf/protocol';
2
+ export const JSONL_CONTENT_TYPE = 'application/jsonl';
3
+ export async function* readJsonlLines(stream) {
4
+ const reader = stream.getReader();
5
+ const decoder = new TextDecoder();
6
+ let buffer = '';
7
+ try {
8
+ while (true) {
9
+ const { value, done } = await reader.read();
10
+ if (done) {
11
+ const last = buffer.trim();
12
+ if (last.length > 0) {
13
+ yield last;
14
+ }
15
+ return;
16
+ }
17
+ buffer += decoder.decode(value, { stream: true });
18
+ let newline = buffer.indexOf('\n');
19
+ while (newline !== -1) {
20
+ const line = buffer.slice(0, newline).trim();
21
+ buffer = buffer.slice(newline + 1);
22
+ if (line.length > 0) {
23
+ yield line;
24
+ }
25
+ newline = buffer.indexOf('\n');
26
+ }
27
+ }
28
+ }
29
+ finally {
30
+ reader.releaseLock();
31
+ }
32
+ }
33
+ export function encodeJsonl(items, writeLine, onError) {
34
+ const encoder = new TextEncoder();
35
+ const iterator = items[Symbol.asyncIterator]();
36
+ return new ReadableStream({
37
+ async pull(controller) {
38
+ try {
39
+ const next = await iterator.next();
40
+ if (next.done) {
41
+ controller.close();
42
+ return;
43
+ }
44
+ const line = await writeLine(next.value);
45
+ controller.enqueue(encoder.encode(`${line}\n`));
46
+ }
47
+ catch (error) {
48
+ controller.enqueue(encoder.encode(`${onError(error)}\n`));
49
+ controller.close();
50
+ }
51
+ },
52
+ cancel() {
53
+ void iterator.return?.();
54
+ },
55
+ });
56
+ }
57
+ export function badRequest(message) {
58
+ return new PFError({ code: 'BAD_REQUEST', status: 400, message });
59
+ }
60
+ //# sourceMappingURL=jsonl.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsonl.js","sourceRoot":"","sources":["../src/jsonl.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAA;AAEzC,MAAM,CAAC,MAAM,kBAAkB,GAAG,mBAAmB,CAAA;AAErD,MAAM,CAAC,KAAK,SAAS,CAAC,CAAC,cAAc,CACnC,MAAkC;IAElC,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,EAAE,CAAA;IACjC,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAA;IACjC,IAAI,MAAM,GAAG,EAAE,CAAA;IACf,IAAI,CAAC;QACH,OAAO,IAAI,EAAE,CAAC;YACZ,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAA;YAC3C,IAAI,IAAI,EAAE,CAAC;gBACT,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,EAAE,CAAA;gBAC1B,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACpB,MAAM,IAAI,CAAA;gBACZ,CAAC;gBACD,OAAM;YACR,CAAC;YACD,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAA;YACjD,IAAI,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC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AAA;gBACzD,UAAU,CAAC,KAAK,EAAE,CAAA;YACpB,CAAC;QACH,CAAC;QACD,MAAM;YACJ,KAAK,QAAQ,CAAC,MAAM,EAAE,EAAE,CAAA;QAC1B,CAAC;KACF,CAAC,CAAA;AACJ,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,OAAe;IACxC,OAAO,IAAI,OAAO,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,CAAC,CAAA;AACnE,CAAC"}
@@ -0,0 +1,4 @@
1
+ import type { StandardSchemaV1 } from '@standard-schema/spec';
2
+ import { type InferSchemaOutput } from '@ts-pf/contract';
3
+ export declare function stream<S>(item: S): StandardSchemaV1<AsyncIterable<InferSchemaOutput<S>>, AsyncIterable<InferSchemaOutput<S>>>;
4
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAC7D,OAAO,EAAE,KAAK,iBAAiB,EAAkB,MAAM,iBAAiB,CAAA;AAIxE,wBAAgB,MAAM,CAAC,CAAC,EACtB,IAAI,EAAE,CAAC,GACN,gBAAgB,CACjB,aAAa,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,EACnC,aAAa,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,CACpC,CAsBA"}
package/dist/schema.js ADDED
@@ -0,0 +1,38 @@
1
+ import { validateSchema } from '@ts-pf/contract';
2
+ import { PFError } from '@ts-pf/protocol';
3
+ import { isAsyncIterable } from './is-async-iterable.js';
4
+ export function stream(item) {
5
+ const schema = {
6
+ '~standard': {
7
+ version: 1,
8
+ vendor: 'ts-pf',
9
+ types: {
10
+ input: 0,
11
+ output: 0,
12
+ },
13
+ validate: async (value) => {
14
+ if (!isAsyncIterable(value)) {
15
+ return { issues: [{ message: 'Expected an async iterable' }] };
16
+ }
17
+ return { value: wrap(value, item) };
18
+ },
19
+ },
20
+ '~pfStream': { item },
21
+ };
22
+ return schema;
23
+ }
24
+ async function* wrap(value, item) {
25
+ for await (const entry of value) {
26
+ const result = await validateSchema(item, entry);
27
+ if (!result.success) {
28
+ throw new PFError({
29
+ code: 'VALIDATION',
30
+ status: 422,
31
+ message: 'Validation failed',
32
+ data: { issues: result.issues },
33
+ });
34
+ }
35
+ yield result.value;
36
+ }
37
+ }
38
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +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,eAAe,EAAE,MAAM,wBAAwB,CAAA;AAExD,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,eAAe,CAAC,KAAK,CAAC,EAAE,CAAC;oBAC5B,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 ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@ts-pf/stream",
3
+ "version": "0.1.0-beta.0",
4
+ "description": "Opt-in StreamCodec and stream() for root AsyncIterable (JSONL)",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/crobinson42/ts-pf.git",
9
+ "directory": "packages/stream"
10
+ },
11
+ "homepage": "https://github.com/crobinson42/ts-pf/tree/main/packages/stream#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/crobinson42/ts-pf/issues"
14
+ },
15
+ "type": "module",
16
+ "sideEffects": false,
17
+ "exports": {
18
+ ".": {
19
+ "types": "./src/index.ts",
20
+ "import": "./src/index.ts"
21
+ }
22
+ },
23
+ "publishConfig": {
24
+ "access": "public",
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.ts",
28
+ "import": "./dist/index.js"
29
+ }
30
+ }
31
+ },
32
+ "files": [
33
+ "dist",
34
+ "skills"
35
+ ],
36
+ "scripts": {
37
+ "build": "tsc -p tsconfig.build.json",
38
+ "test": "vitest run",
39
+ "type-check": "tsc --noEmit",
40
+ "lint": "biome check src tests"
41
+ },
42
+ "dependencies": {
43
+ "@standard-schema/spec": "^1.0.0",
44
+ "@ts-pf/contract": "0.1.0-beta.0",
45
+ "@ts-pf/http": "0.1.0-beta.0",
46
+ "@ts-pf/protocol": "0.1.0-beta.0"
47
+ },
48
+ "devDependencies": {
49
+ "@ts-pf/client": "0.1.0-beta.0",
50
+ "@ts-pf/client-http": "0.1.0-beta.0",
51
+ "@ts-pf/server": "0.1.0-beta.0",
52
+ "@ts-pf/server-http": "0.1.0-beta.0",
53
+ "zod": "^4.0.0"
54
+ }
55
+ }
@@ -0,0 +1,43 @@
1
+ ---
2
+ name: ts-pf-stream
3
+ description: Use when streaming root AsyncIterable over ts-pf with stream() and StreamCodec (JSONL). Triggers: @ts-pf/stream, StreamCodec, stream(), application/jsonl.
4
+ ---
5
+
6
+ # @ts-pf/stream
7
+
8
+ Opt-in `StreamCodec` for root `AsyncIterable` input and output. JSON calls stay JSON. Streams are `application/jsonl`.
9
+
10
+ Install: `npm i @ts-pf/stream@beta`
11
+
12
+ Link for agents: `npx skills experimental_sync -y`
13
+
14
+ ## Do
15
+
16
+ ```ts
17
+ import { stream, StreamCodec } from '@ts-pf/stream'
18
+ import { FetchHandler } from '@ts-pf/server-http'
19
+ import { FetchLink } from '@ts-pf/client-http'
20
+
21
+ const codec = new StreamCodec()
22
+ new FetchHandler(app, { codec })
23
+ new FetchLink({ url: '/rpc', codec })
24
+
25
+ procedure.output(stream(z.object({ token: z.string() })))
26
+ ```
27
+
28
+ Root only. Same codec on handler and link.
29
+
30
+ ## API
31
+
32
+ - `StreamCodec`
33
+ - `stream()`
34
+
35
+ ## Pair with
36
+
37
+ - `ts-pf-server-http` / `ts-pf-client-http`
38
+ - SSE output framing: `ts-pf-sse` (`SseCodec`; this package still owns `stream()`)
39
+
40
+ ## Don't
41
+
42
+ - Nested streams or File/Blob in items (`BAD_REQUEST`).
43
+ - Import `stream()` from `@ts-pf/sse` — it is not re-exported there.