hx-stream 0.0.10 → 0.1.1
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/dist/server.d.ts +2 -1
- package/dist/server.js +13 -3
- package/package.json +1 -1
package/dist/server.d.ts
CHANGED
|
@@ -26,5 +26,6 @@ export declare class StreamResponse<JsxEnabled extends boolean> {
|
|
|
26
26
|
send(target: string, swap: string, html: JsxEnabled extends true ? (JSX.Element | string) : string): boolean;
|
|
27
27
|
close(): boolean;
|
|
28
28
|
}
|
|
29
|
-
export declare function
|
|
29
|
+
export declare function MakeRawStream<O extends (DefaultOptions | RenderOptions)>(request: Request, options: O): StreamResponse<HasRender<O>>;
|
|
30
|
+
export declare function MakeStream<T extends Partial<RenderOptions>>(request: Request, props: T, cb: (stream: StreamResponse<HasRender<T>>, props: T) => Promise<void> | void): Response;
|
|
30
31
|
export {};
|
package/dist/server.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const encoder = new TextEncoder();
|
|
2
2
|
const headers = {
|
|
3
3
|
// Chunked encoding with immediate forwarding by proxies (i.e. nginx)
|
|
4
|
+
"X-Content-Type-Options": "nosniff",
|
|
4
5
|
"X-Accel-Buffering": "no",
|
|
5
6
|
"Transfer-Encoding": "chunked",
|
|
6
7
|
"Content-Type": "text/html",
|
|
@@ -35,11 +36,11 @@ export class StreamResponse {
|
|
|
35
36
|
this.withCredentials = request.mode === "cors";
|
|
36
37
|
this.url = request.url;
|
|
37
38
|
// immediate prepare for abortion
|
|
38
|
-
const cancel = () => { this.close(); };
|
|
39
|
+
const cancel = () => { this.close(); request.signal.removeEventListener("abort", cancel); };
|
|
39
40
|
request.signal.addEventListener('abort', cancel);
|
|
40
41
|
this.#signal = request.signal;
|
|
41
42
|
const start = (c) => { this.#controller = c; this.#state = StreamResponse.OPEN; };
|
|
42
|
-
const stream = new ReadableStream({ start, cancel }, { highWaterMark: options.highWaterMark
|
|
43
|
+
const stream = new ReadableStream({ start, cancel }, { highWaterMark: options.highWaterMark });
|
|
43
44
|
this.response = new Response(stream, { headers });
|
|
44
45
|
this.response.headers.set("X-Chunk-Boundary", this.#boundary);
|
|
45
46
|
this.#timer = setInterval(() => this.keepAlive(), options.keepAlive || 30_000);
|
|
@@ -101,6 +102,15 @@ export class StreamResponse {
|
|
|
101
102
|
return true;
|
|
102
103
|
}
|
|
103
104
|
}
|
|
104
|
-
export function
|
|
105
|
+
export function MakeRawStream(request, options) {
|
|
105
106
|
return new StreamResponse(request, options);
|
|
106
107
|
}
|
|
108
|
+
export function MakeStream(request, props, cb) {
|
|
109
|
+
const stream = MakeRawStream(request, props);
|
|
110
|
+
queueMicrotask(() => {
|
|
111
|
+
const p = cb(stream, props);
|
|
112
|
+
if (p instanceof Promise)
|
|
113
|
+
p.catch(console.error);
|
|
114
|
+
});
|
|
115
|
+
return stream.response;
|
|
116
|
+
}
|