@daloyjs/core 0.2.0 → 0.3.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 CHANGED
@@ -27,7 +27,7 @@ DaloyJS exists to be the framework you'd build if you took the best ideas from e
27
27
  | **Supply-chain-hardened installs and publishing** | [pnpm](https://pnpm.io/motivation) + hardened CI/CD | `ignore-scripts`, release-age cooldown, explicit build allowlist, SHA-pinned actions, isolated OIDC publish with provenance. |
28
28
 
29
29
  ```
30
- 119/119 framework tests passing · 100% line + function coverage · clean strict TypeScript 6
30
+ 159/159 framework tests passing · 100% line + function coverage · clean strict TypeScript 6
31
31
  runs on Node, Bun, Deno, Cloudflare, Vercel
32
32
  ~12.3M static-route ops/sec · ~1.5M dynamic-route ops/sec on M-class CPU
33
33
  ```
@@ -177,7 +177,7 @@ import { swaggerUiHtml, htmlResponse } from "@daloyjs/core/docs";
177
177
  ```
178
178
 
179
179
  Mount at `/docs` and the UI is always contract-accurate — never stale.
180
- `create-daloy@0.1.10` mounts Swagger UI at `/docs` and the live spec at `/openapi.json` by default.
180
+ `create-daloy@0.1.11` mounts Swagger UI at `/docs` and the live spec at `/openapi.json` by default.
181
181
 
182
182
  ---
183
183
 
@@ -310,11 +310,12 @@ Full, versioned plan: [ROADMAP.md](./ROADMAP.md).
310
310
  - [x] `SECURITY.md` and vulnerability disclosure process
311
311
  - [x] `pnpm create daloy` project scaffolder (Node + Vercel Edge + Cloudflare templates)
312
312
  - [x] Docs site discoverability pass: page metadata, sitemap, robots, OpenGraph image, ORM guides
313
+ - [x] Streaming response helpers: SSE + NDJSON with backpressure-safe writers (`sseStream` / `sseResponse` / `ndjsonStream` / `ndjsonResponse`)
313
314
  - [ ] Branch coverage push to `>= 98%`
314
315
  - [ ] Release checklist and publishing docs cleanup
315
316
 
316
- **On deck (`0.3.0` and beyond):** SSE/NDJSON streaming, OpenTelemetry,
317
- multipart/form-data ergonomics, CSRF helper, scaffolder, Redis rate-limit store,
317
+ **On deck (`0.3.0` and beyond):** OpenTelemetry,
318
+ multipart/form-data ergonomics, CSRF helper, Redis rate-limit store,
318
319
  CLI route and schema inspector, WebSockets, and HTTP/2 + HTTP/3 adapters.
319
320
 
320
321
  ## License
package/dist/index.d.ts CHANGED
@@ -10,4 +10,6 @@ export { requestId, secureHeaders, cors, rateLimit, timing, bearerAuth, } from "
10
10
  export type { RequestIdOptions, SecureHeadersOptions, CorsOptions, RateLimitOptions, RateLimitStore, } from "./middleware.js";
11
11
  export { createLogger, noopLogger } from "./logger.js";
12
12
  export type { Logger, LogLevel, ConsoleLoggerOptions } from "./logger.js";
13
+ export { sseStream, sseResponse, ndjsonStream, ndjsonResponse, } from "./streaming.js";
14
+ export type { SSEMessage, StreamOptions, SSEStreamOptions, SSEResponseOptions, NDJSONResponseOptions, } from "./streaming.js";
13
15
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,YAAY,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAE9D,YAAY,EACV,eAAe,EACf,UAAU,EACV,UAAU,EACV,cAAc,EACd,YAAY,EACZ,YAAY,EACZ,QAAQ,EACR,KAAK,EACL,WAAW,EACX,QAAQ,EACR,aAAa,EACb,YAAY,EACZ,QAAQ,EACR,UAAU,GACX,MAAM,YAAY,CAAC;AAEpB,OAAO,EACL,SAAS,EACT,eAAe,EACf,eAAe,EACf,aAAa,EACb,iBAAiB,EACjB,cAAc,EACd,qBAAqB,EACrB,oBAAoB,EACpB,yBAAyB,EACzB,oBAAoB,EACpB,mBAAmB,EACnB,aAAa,GACd,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAExE,YAAY,EAAE,gBAAgB,EAAE,MAAM,aA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,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEzD,OAAO,EACL,eAAe,EACf,aAAa,EACb,kBAAkB,EAClB,mBAAmB,EACnB,eAAe,EACf,QAAQ,GACT,MAAM,eAAe,CAAC;AAEvB,OAAO,EACL,SAAS,EACT,aAAa,EACb,IAAI,EACJ,SAAS,EACT,MAAM,EACN,UAAU,GACX,MAAM,iBAAiB,CAAC;AACzB,YAAY,EACV,gBAAgB,EAChB,oBAAoB,EACpB,WAAW,EACX,gBAAgB,EAChB,cAAc,GACf,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACvD,YAAY,EAAE,MAAM,EAAE,QAAQ,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAE1E,OAAO,EACL,SAAS,EACT,WAAW,EACX,YAAY,EACZ,cAAc,GACf,MAAM,gBAAgB,CAAC;AACxB,YAAY,EACV,UAAU,EACV,aAAa,EACb,gBAAgB,EAChB,kBAAkB,EAClB,qBAAqB,GACtB,MAAM,gBAAgB,CAAC"}
package/dist/index.js CHANGED
@@ -4,4 +4,5 @@ export { validate, isStandardSchema } from "./schema.js";
4
4
  export { readBodyLimited, safeJsonParse, sanitizeHeaderName, sanitizeHeaderValue, timingSafeEqual, randomId, } from "./security.js";
5
5
  export { requestId, secureHeaders, cors, rateLimit, timing, bearerAuth, } from "./middleware.js";
6
6
  export { createLogger, noopLogger } from "./logger.js";
7
+ export { sseStream, sseResponse, ndjsonStream, ndjsonResponse, } from "./streaming.js";
7
8
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAoB/B,OAAO,EACL,SAAS,EACT,eAAe,EACf,eAAe,EACf,aAAa,EACb,iBAAiB,EACjB,cAAc,EACd,qBAAqB,EACrB,oBAAoB,EACpB,yBAAyB,EACzB,oBAAoB,EACpB,mBAAmB,EACnB,aAAa,GACd,MAAM,aAAa,CAAC;AAIrB,OAAO,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEzD,OAAO,EACL,eAAe,EACf,aAAa,EACb,kBAAkB,EAClB,mBAAmB,EACnB,eAAe,EACf,QAAQ,GACT,MAAM,eAAe,CAAC;AAEvB,OAAO,EACL,SAAS,EACT,aAAa,EACb,IAAI,EACJ,SAAS,EACT,MAAM,EACN,UAAU,GACX,MAAM,iBAAiB,CAAC;AASzB,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAoB/B,OAAO,EACL,SAAS,EACT,eAAe,EACf,eAAe,EACf,aAAa,EACb,iBAAiB,EACjB,cAAc,EACd,qBAAqB,EACrB,oBAAoB,EACpB,yBAAyB,EACzB,oBAAoB,EACpB,mBAAmB,EACnB,aAAa,GACd,MAAM,aAAa,CAAC;AAIrB,OAAO,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEzD,OAAO,EACL,eAAe,EACf,aAAa,EACb,kBAAkB,EAClB,mBAAmB,EACnB,eAAe,EACf,QAAQ,GACT,MAAM,eAAe,CAAC;AAEvB,OAAO,EACL,SAAS,EACT,aAAa,EACb,IAAI,EACJ,SAAS,EACT,MAAM,EACN,UAAU,GACX,MAAM,iBAAiB,CAAC;AASzB,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGvD,OAAO,EACL,SAAS,EACT,WAAW,EACX,YAAY,EACZ,cAAc,GACf,MAAM,gBAAgB,CAAC"}
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Streaming response helpers — Server-Sent Events (SSE) and NDJSON.
3
+ *
4
+ * Both helpers return a `ReadableStream<Uint8Array>` (or, in the `*Response`
5
+ * forms, a `Response`) that is **backpressure-safe**: data is only pulled
6
+ * from the underlying `AsyncIterable`/generator when the consumer asks for
7
+ * the next chunk. They also honor an optional `AbortSignal` and call
8
+ * `iterator.return()` so caller-owned resources (DB cursors, fetches, queues)
9
+ * are released when the client disconnects.
10
+ *
11
+ * ```ts
12
+ * import { sseStream } from "@daloyjs/core";
13
+ *
14
+ * app.route({
15
+ * method: "GET",
16
+ * path: "/events",
17
+ * operationId: "events",
18
+ * responses: { 200: { description: "SSE stream" } },
19
+ * handler: ({ request }) => ({
20
+ * status: 200,
21
+ * headers: { "content-type": "text/event-stream" },
22
+ * body: sseStream(async function* () {
23
+ * yield { event: "ping", data: { now: Date.now() } };
24
+ * }, { signal: request.signal }),
25
+ * }),
26
+ * });
27
+ * ```
28
+ *
29
+ * For NDJSON, each yielded value is JSON-encoded and terminated with `\n`.
30
+ */
31
+ /** A single SSE event. `data` may be a string or any JSON-serializable value. */
32
+ export interface SSEMessage {
33
+ data: unknown;
34
+ event?: string;
35
+ id?: string;
36
+ /** Reconnection delay in milliseconds. */
37
+ retry?: number;
38
+ /** Comment line (sent as `: <comment>`). Useful for keep-alive pings. */
39
+ comment?: string;
40
+ }
41
+ export interface StreamOptions {
42
+ /** Abort the stream when this signal fires. */
43
+ signal?: AbortSignal;
44
+ }
45
+ export interface SSEStreamOptions extends StreamOptions {
46
+ /**
47
+ * Send a comment (`:keep-alive`) every N milliseconds to keep the
48
+ * connection open through proxies. Set to `0`/`undefined` to disable.
49
+ */
50
+ keepAliveMs?: number;
51
+ }
52
+ export interface SSEResponseOptions extends SSEStreamOptions {
53
+ status?: number;
54
+ headers?: HeadersInit;
55
+ }
56
+ export interface NDJSONResponseOptions extends StreamOptions {
57
+ status?: number;
58
+ headers?: HeadersInit;
59
+ }
60
+ type IterableSource<T> = AsyncIterable<T> | Iterable<T> | (() => AsyncIterable<T> | Iterable<T>);
61
+ /**
62
+ * Build a backpressure-safe `ReadableStream` from an async iterable of SSE
63
+ * messages. The iterator is only advanced when the consumer pulls the next
64
+ * chunk, so a slow client cannot cause unbounded buffering.
65
+ */
66
+ export declare function sseStream(source: IterableSource<SSEMessage | string>, opts?: SSEStreamOptions): ReadableStream<Uint8Array>;
67
+ /**
68
+ * Wrap `sseStream` in a `Response` with the proper SSE headers
69
+ * (`text/event-stream`, no caching, keep-alive). Caller-supplied headers win.
70
+ */
71
+ export declare function sseResponse(source: IterableSource<SSEMessage | string>, opts?: SSEResponseOptions): Response;
72
+ /**
73
+ * Build a backpressure-safe `ReadableStream` of NDJSON (newline-delimited
74
+ * JSON) records from an async iterable. Each yielded value is encoded with
75
+ * `JSON.stringify` and terminated with `\n`. Values that stringify to
76
+ * `undefined` throw because they cannot be represented as valid NDJSON.
77
+ */
78
+ export declare function ndjsonStream<T>(source: IterableSource<T>, opts?: StreamOptions): ReadableStream<Uint8Array>;
79
+ /**
80
+ * Wrap `ndjsonStream` in a `Response` with `application/x-ndjson` and
81
+ * cache-busting headers. Caller-supplied headers win.
82
+ */
83
+ export declare function ndjsonResponse<T>(source: IterableSource<T>, opts?: NDJSONResponseOptions): Response;
84
+ export {};
85
+ //# sourceMappingURL=streaming.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"streaming.d.ts","sourceRoot":"","sources":["../src/streaming.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAIH,iFAAiF;AACjF,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,OAAO,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,0CAA0C;IAC1C,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,yEAAyE;IACzE,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,aAAa;IAC5B,+CAA+C;IAC/C,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,MAAM,WAAW,gBAAiB,SAAQ,aAAa;IACrD;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,kBAAmB,SAAQ,gBAAgB;IAC1D,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,WAAW,CAAC;CACvB;AAED,MAAM,WAAW,qBAAsB,SAAQ,aAAa;IAC1D,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,WAAW,CAAC;CACvB;AAED,KAAK,cAAc,CAAC,CAAC,IAAI,aAAa,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,aAAa,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;AA2DjG;;;;GAIG;AACH,wBAAgB,SAAS,CACvB,MAAM,EAAE,cAAc,CAAC,UAAU,GAAG,MAAM,CAAC,EAC3C,IAAI,GAAE,gBAAqB,GAC1B,cAAc,CAAC,UAAU,CAAC,CAuE5B;AAED;;;GAGG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,cAAc,CAAC,UAAU,GAAG,MAAM,CAAC,EAC3C,IAAI,GAAE,kBAAuB,GAC5B,QAAQ,CASV;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAC5B,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC,EACzB,IAAI,GAAE,aAAkB,GACvB,cAAc,CAAC,UAAU,CAAC,CAuD5B;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAC9B,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC,EACzB,IAAI,GAAE,qBAA0B,GAC/B,QAAQ,CAOV"}
@@ -0,0 +1,261 @@
1
+ /**
2
+ * Streaming response helpers — Server-Sent Events (SSE) and NDJSON.
3
+ *
4
+ * Both helpers return a `ReadableStream<Uint8Array>` (or, in the `*Response`
5
+ * forms, a `Response`) that is **backpressure-safe**: data is only pulled
6
+ * from the underlying `AsyncIterable`/generator when the consumer asks for
7
+ * the next chunk. They also honor an optional `AbortSignal` and call
8
+ * `iterator.return()` so caller-owned resources (DB cursors, fetches, queues)
9
+ * are released when the client disconnects.
10
+ *
11
+ * ```ts
12
+ * import { sseStream } from "@daloyjs/core";
13
+ *
14
+ * app.route({
15
+ * method: "GET",
16
+ * path: "/events",
17
+ * operationId: "events",
18
+ * responses: { 200: { description: "SSE stream" } },
19
+ * handler: ({ request }) => ({
20
+ * status: 200,
21
+ * headers: { "content-type": "text/event-stream" },
22
+ * body: sseStream(async function* () {
23
+ * yield { event: "ping", data: { now: Date.now() } };
24
+ * }, { signal: request.signal }),
25
+ * }),
26
+ * });
27
+ * ```
28
+ *
29
+ * For NDJSON, each yielded value is JSON-encoded and terminated with `\n`.
30
+ */
31
+ const TEXT_ENCODER = new TextEncoder();
32
+ function getAsyncIterator(src) {
33
+ const it = typeof src === "function" ? src() : src;
34
+ if (it == null) {
35
+ throw new TypeError("Streaming source is null/undefined");
36
+ }
37
+ if (typeof it[Symbol.asyncIterator] === "function") {
38
+ return it[Symbol.asyncIterator]();
39
+ }
40
+ if (typeof it[Symbol.iterator] === "function") {
41
+ const sync = it[Symbol.iterator]();
42
+ const wrapped = {
43
+ next: async () => sync.next(),
44
+ };
45
+ if (sync.return) {
46
+ wrapped.return = async (value) => sync.return(value);
47
+ }
48
+ return wrapped;
49
+ }
50
+ throw new TypeError("Streaming source is not iterable");
51
+ }
52
+ function encodeSSE(msg) {
53
+ const message = typeof msg === "string" ? { data: msg } : msg;
54
+ let out = "";
55
+ if (message.comment) {
56
+ for (const line of String(message.comment).split(/\r?\n/)) {
57
+ out += `: ${line}\n`;
58
+ }
59
+ }
60
+ if (message.event !== undefined) {
61
+ // Event names cannot contain newlines per the spec.
62
+ out += `event: ${String(message.event).replace(/[\r\n]+/g, " ")}\n`;
63
+ }
64
+ if (message.id !== undefined) {
65
+ out += `id: ${String(message.id).replace(/[\r\n]+/g, " ")}\n`;
66
+ }
67
+ if (message.retry !== undefined && Number.isFinite(message.retry)) {
68
+ out += `retry: ${Math.max(0, Math.floor(message.retry))}\n`;
69
+ }
70
+ if (message.data !== undefined) {
71
+ const raw = typeof message.data === "string" ? message.data : JSON.stringify(message.data);
72
+ for (const line of raw.split(/\r?\n/)) {
73
+ out += `data: ${line}\n`;
74
+ }
75
+ }
76
+ out += "\n";
77
+ return TEXT_ENCODER.encode(out);
78
+ }
79
+ function encodeNDJSON(value) {
80
+ const line = JSON.stringify(value);
81
+ if (line === undefined) {
82
+ throw new TypeError("NDJSON values must be JSON-serializable");
83
+ }
84
+ return TEXT_ENCODER.encode(line + "\n");
85
+ }
86
+ /**
87
+ * Build a backpressure-safe `ReadableStream` from an async iterable of SSE
88
+ * messages. The iterator is only advanced when the consumer pulls the next
89
+ * chunk, so a slow client cannot cause unbounded buffering.
90
+ */
91
+ export function sseStream(source, opts = {}) {
92
+ const iterator = getAsyncIterator(source);
93
+ let keepAliveTimer;
94
+ let abortHandler;
95
+ const cleanup = async (cancelValue) => {
96
+ if (keepAliveTimer !== undefined) {
97
+ clearInterval(keepAliveTimer);
98
+ keepAliveTimer = undefined;
99
+ }
100
+ if (opts.signal && abortHandler) {
101
+ opts.signal.removeEventListener("abort", abortHandler);
102
+ abortHandler = undefined;
103
+ }
104
+ if (typeof iterator.return === "function") {
105
+ try {
106
+ await iterator.return(cancelValue);
107
+ }
108
+ catch {
109
+ /* swallow — the consumer is already gone */
110
+ }
111
+ }
112
+ };
113
+ return new ReadableStream({
114
+ start(controller) {
115
+ if (opts.signal?.aborted) {
116
+ controller.close();
117
+ void cleanup();
118
+ return;
119
+ }
120
+ if (opts.signal) {
121
+ abortHandler = () => {
122
+ try {
123
+ controller.close();
124
+ }
125
+ catch {
126
+ /* already closed */
127
+ }
128
+ void cleanup();
129
+ };
130
+ opts.signal.addEventListener("abort", abortHandler, { once: true });
131
+ }
132
+ if (opts.keepAliveMs && opts.keepAliveMs > 0) {
133
+ keepAliveTimer = setInterval(() => {
134
+ try {
135
+ if ((controller.desiredSize ?? 0) > 0) {
136
+ controller.enqueue(TEXT_ENCODER.encode(": keep-alive\n\n"));
137
+ }
138
+ }
139
+ catch {
140
+ /* stream closed — interval will be cleared on cancel */
141
+ }
142
+ }, opts.keepAliveMs);
143
+ }
144
+ },
145
+ async pull(controller) {
146
+ try {
147
+ const { value, done } = await iterator.next();
148
+ if (done) {
149
+ controller.close();
150
+ await cleanup();
151
+ return;
152
+ }
153
+ controller.enqueue(encodeSSE(value));
154
+ }
155
+ catch (err) {
156
+ controller.error(err);
157
+ await cleanup(err);
158
+ }
159
+ },
160
+ async cancel(reason) {
161
+ await cleanup(reason);
162
+ },
163
+ });
164
+ }
165
+ /**
166
+ * Wrap `sseStream` in a `Response` with the proper SSE headers
167
+ * (`text/event-stream`, no caching, keep-alive). Caller-supplied headers win.
168
+ */
169
+ export function sseResponse(source, opts = {}) {
170
+ const stream = sseStream(source, opts);
171
+ const headers = new Headers(opts.headers);
172
+ if (!headers.has("content-type"))
173
+ headers.set("content-type", "text/event-stream; charset=utf-8");
174
+ if (!headers.has("cache-control"))
175
+ headers.set("cache-control", "no-cache, no-transform");
176
+ if (!headers.has("connection"))
177
+ headers.set("connection", "keep-alive");
178
+ // Disable proxy buffering (nginx).
179
+ if (!headers.has("x-accel-buffering"))
180
+ headers.set("x-accel-buffering", "no");
181
+ return new Response(stream, { status: opts.status ?? 200, headers });
182
+ }
183
+ /**
184
+ * Build a backpressure-safe `ReadableStream` of NDJSON (newline-delimited
185
+ * JSON) records from an async iterable. Each yielded value is encoded with
186
+ * `JSON.stringify` and terminated with `\n`. Values that stringify to
187
+ * `undefined` throw because they cannot be represented as valid NDJSON.
188
+ */
189
+ export function ndjsonStream(source, opts = {}) {
190
+ const iterator = getAsyncIterator(source);
191
+ let abortHandler;
192
+ const cleanup = async (cancelValue) => {
193
+ if (opts.signal && abortHandler) {
194
+ opts.signal.removeEventListener("abort", abortHandler);
195
+ abortHandler = undefined;
196
+ }
197
+ if (typeof iterator.return === "function") {
198
+ try {
199
+ await iterator.return(cancelValue);
200
+ }
201
+ catch {
202
+ /* swallow */
203
+ }
204
+ }
205
+ };
206
+ return new ReadableStream({
207
+ start(controller) {
208
+ if (opts.signal?.aborted) {
209
+ controller.close();
210
+ void cleanup();
211
+ return;
212
+ }
213
+ if (opts.signal) {
214
+ abortHandler = () => {
215
+ try {
216
+ controller.close();
217
+ }
218
+ catch {
219
+ /* already closed */
220
+ }
221
+ void cleanup();
222
+ };
223
+ opts.signal.addEventListener("abort", abortHandler, { once: true });
224
+ }
225
+ },
226
+ async pull(controller) {
227
+ try {
228
+ const { value, done } = await iterator.next();
229
+ if (done) {
230
+ controller.close();
231
+ await cleanup();
232
+ return;
233
+ }
234
+ controller.enqueue(encodeNDJSON(value));
235
+ }
236
+ catch (err) {
237
+ controller.error(err);
238
+ await cleanup(err);
239
+ }
240
+ },
241
+ async cancel(reason) {
242
+ await cleanup(reason);
243
+ },
244
+ });
245
+ }
246
+ /**
247
+ * Wrap `ndjsonStream` in a `Response` with `application/x-ndjson` and
248
+ * cache-busting headers. Caller-supplied headers win.
249
+ */
250
+ export function ndjsonResponse(source, opts = {}) {
251
+ const stream = ndjsonStream(source, opts);
252
+ const headers = new Headers(opts.headers);
253
+ if (!headers.has("content-type"))
254
+ headers.set("content-type", "application/x-ndjson; charset=utf-8");
255
+ if (!headers.has("cache-control"))
256
+ headers.set("cache-control", "no-cache, no-transform");
257
+ if (!headers.has("x-accel-buffering"))
258
+ headers.set("x-accel-buffering", "no");
259
+ return new Response(stream, { status: opts.status ?? 200, headers });
260
+ }
261
+ //# sourceMappingURL=streaming.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"streaming.js","sourceRoot":"","sources":["../src/streaming.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,MAAM,YAAY,GAAG,IAAI,WAAW,EAAE,CAAC;AAsCvC,SAA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AAC,OAAO,GAAG,EAAE,CAAC;gBACb,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;gBACtB,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC;YACrB,CAAC;QACH,CAAC;QACD,KAAK,CAAC,MAAM,CAAC,MAAM;YACjB,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC;QACxB,CAAC;KACF,CAAC,CAAC;AACL,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,cAAc,CAC5B,MAAyB,EACzB,OAA8B,EAAE;IAEhC,MAAM,MAAM,GAAG,YAAY,CAAI,MAAM,EAAE,IAAI,CAAC,CAAC;IAC7C,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC1C,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC;QAAE,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,qCAAqC,CAAC,CAAC;IACrG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC;QAAE,OAAO,CAAC,GAAG,CAAC,eAAe,EAAE,wBAAwB,CAAC,CAAC;IAC1F,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAC;QAAE,OAAO,CAAC,GAAG,CAAC,mBAAmB,EAAE,IAAI,CAAC,CAAC;IAC9E,OAAO,IAAI,QAAQ,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,IAAI,GAAG,EAAE,OAAO,EAAE,CAAC,CAAC;AACvE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@daloyjs/core",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "DaloyJS is a runtime-portable, contract-first TypeScript web framework with built-in OpenAPI (Hey API), typed client generation, large-scale maintainability, and security-first defaults. Hono-grade portability, Elysia-grade DX, FastAPI-grade docs, Fastify-grade ops — distributed via pnpm.",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -61,6 +61,10 @@
61
61
  "./contract": {
62
62
  "types": "./dist/contract.d.ts",
63
63
  "import": "./dist/contract.js"
64
+ },
65
+ "./streaming": {
66
+ "types": "./dist/streaming.d.ts",
67
+ "import": "./dist/streaming.js"
64
68
  }
65
69
  },
66
70
  "files": [