hx-stream 0.0.9 → 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 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 MakeStream<O extends (DefaultOptions | RenderOptions)>(request: Request, options: O): StreamResponse<HasRender<O>>;
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",
@@ -14,6 +15,7 @@ function MakeBoundary() {
14
15
  }
15
16
  export class StreamResponse {
16
17
  #controller;
18
+ #signal;
17
19
  #timer;
18
20
  #state;
19
21
  #boundary;
@@ -34,10 +36,11 @@ export class StreamResponse {
34
36
  this.withCredentials = request.mode === "cors";
35
37
  this.url = request.url;
36
38
  // immediate prepare for abortion
37
- const cancel = () => { this.close(); };
39
+ const cancel = () => { this.close(); request.signal.removeEventListener("abort", cancel); };
38
40
  request.signal.addEventListener('abort', cancel);
41
+ this.#signal = request.signal;
39
42
  const start = (c) => { this.#controller = c; this.#state = StreamResponse.OPEN; };
40
- const stream = new ReadableStream({ start, cancel }, { highWaterMark: options.highWaterMark || 0 });
43
+ const stream = new ReadableStream({ start, cancel }, { highWaterMark: options.highWaterMark });
41
44
  this.response = new Response(stream, { headers });
42
45
  this.response.headers.set("X-Chunk-Boundary", this.#boundary);
43
46
  this.#timer = setInterval(() => this.keepAlive(), options.keepAlive || 30_000);
@@ -46,6 +49,14 @@ export class StreamResponse {
46
49
  this.#controller = controller;
47
50
  }
48
51
  sendBytes(chunk) {
52
+ if (this.#state === StreamResponse.CLOSED) {
53
+ const err = new Error(`Warn: Attempted to send data on closed stream for: ${this.url}`);
54
+ console.warn(err);
55
+ }
56
+ if (this.#signal.aborted) {
57
+ this.close();
58
+ return false;
59
+ }
49
60
  if (!this.#controller)
50
61
  return false;
51
62
  try {
@@ -63,10 +74,6 @@ export class StreamResponse {
63
74
  }
64
75
  keepAlive() { return this.sendText(" "); }
65
76
  send(target, swap, html) {
66
- if (this.#state === StreamResponse.CLOSED) {
67
- const err = new Error(`Warn: Attempted to send data on closed stream for: ${this.url}`);
68
- console.warn(err);
69
- }
70
77
  if (typeof html !== "string") {
71
78
  if (!this.#render)
72
79
  throw new Error(`Cannot render to JSX when no renderer provided during class initialization`);
@@ -75,10 +82,6 @@ export class StreamResponse {
75
82
  return this.sendText(`<${this.#boundary}>${target}|${swap}|${html}</${this.#boundary}>\n`);
76
83
  }
77
84
  close() {
78
- if (this.#state === StreamResponse.CLOSED) {
79
- this.#controller = null;
80
- return false;
81
- }
82
85
  if (this.#controller) {
83
86
  try {
84
87
  this.#controller.close();
@@ -91,11 +94,23 @@ export class StreamResponse {
91
94
  // Cleanup
92
95
  if (this.#timer)
93
96
  clearInterval(this.#timer);
97
+ // was already closed
98
+ if (this.#state === EventSource.CLOSED)
99
+ return false;
94
100
  // Mark closed
95
101
  this.#state = StreamResponse.CLOSED;
96
102
  return true;
97
103
  }
98
104
  }
99
- export function MakeStream(request, options) {
105
+ export function MakeRawStream(request, options) {
100
106
  return new StreamResponse(request, options);
101
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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hx-stream",
3
- "version": "0.0.9",
3
+ "version": "0.1.1",
4
4
  "description": "keep htmx client state across page refreshes",
5
5
  "keywords": [ "htmx", "streaming" ],
6
6
  "homepage": "https://hx-stream.ajanibilby.com",