hx-stream 0.0.3 → 0.0.6

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.
Files changed (2) hide show
  1. package/dist/server.js +94 -0
  2. package/package.json +2 -2
package/dist/server.js ADDED
@@ -0,0 +1,94 @@
1
+ const encoder = new TextEncoder();
2
+ const headers = {
3
+ // Chunked encoding with immediate forwarding by proxies (i.e. nginx)
4
+ "X-Accel-Buffering": "no",
5
+ "Transfer-Encoding": "chunked",
6
+ "Content-Type": "text/html",
7
+ // the maximum keep alive chrome shouldn't ignore
8
+ "Keep-Alive": "timeout=60",
9
+ "Connection": "keep-alive",
10
+ };
11
+ export class StreamResponse {
12
+ #controller;
13
+ #timer;
14
+ #state;
15
+ #render;
16
+ response;
17
+ // just to make it polyfill
18
+ withCredentials;
19
+ url;
20
+ get readyState() { return this.#state; }
21
+ static CONNECTING = 0;
22
+ static OPEN = 1;
23
+ static CLOSED = 2;
24
+ constructor(request, options) {
25
+ this.#controller = null;
26
+ this.#state = StreamResponse.CONNECTING;
27
+ this.#render = options.render;
28
+ this.withCredentials = request.mode === "cors";
29
+ this.url = request.url;
30
+ // immediate prepare for abortion
31
+ const cancel = () => { this.close(); };
32
+ request.signal.addEventListener('abort', cancel);
33
+ const start = (c) => { this.#controller = c; this.#state = StreamResponse.OPEN; };
34
+ const stream = new ReadableStream({ start, cancel }, { highWaterMark: 0 });
35
+ this.response = new Response(stream, { headers });
36
+ this.#timer = setInterval(() => this.keepAlive(), options.keepAlive || 30_000);
37
+ }
38
+ bind(controller) {
39
+ this.#controller = controller;
40
+ }
41
+ sendBytes(chunk) {
42
+ if (!this.#controller)
43
+ return false;
44
+ try {
45
+ this.#controller.enqueue(chunk);
46
+ return true;
47
+ }
48
+ catch (e) {
49
+ console.error(e);
50
+ this.close(); // unbind on failure
51
+ return false;
52
+ }
53
+ }
54
+ sendText(chunk) {
55
+ return this.sendBytes(encoder.encode(chunk));
56
+ }
57
+ keepAlive() { return this.sendText(" "); }
58
+ send(target, swap, html) {
59
+ if (this.#state === StreamResponse.CLOSED) {
60
+ const err = new Error(`Warn: Attempted to send data on closed stream for: ${this.url}`);
61
+ console.warn(err);
62
+ }
63
+ if (typeof html !== "string") {
64
+ if (!this.#render)
65
+ throw new Error(`Cannot render to JSX when no renderer provided during class initialization`);
66
+ html = this.#render(html);
67
+ }
68
+ return this.sendText(`<div hx-swap-oob="${swap}:${target}">${html}</div>`);
69
+ }
70
+ close() {
71
+ if (this.#state === StreamResponse.CLOSED) {
72
+ this.#controller = null;
73
+ return false;
74
+ }
75
+ if (this.#controller) {
76
+ try {
77
+ this.#controller.close();
78
+ }
79
+ catch (e) {
80
+ console.error(e);
81
+ }
82
+ this.#controller = null;
83
+ }
84
+ // Cleanup
85
+ if (this.#timer)
86
+ clearInterval(this.#timer);
87
+ // Mark closed
88
+ this.#state = StreamResponse.CLOSED;
89
+ return true;
90
+ }
91
+ }
92
+ export function MakeStream(request, options) {
93
+ return new StreamResponse(request, options);
94
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hx-stream",
3
- "version": "0.0.3",
3
+ "version": "0.0.6",
4
4
  "description": "keep htmx client state across page refreshes",
5
5
  "keywords": [ "htmx", "streaming" ],
6
6
  "homepage": "https://hx-stream.ajanibilby.com",
@@ -20,7 +20,7 @@
20
20
  },
21
21
  "license": "MIT",
22
22
  "author": "Ajani Bilby",
23
- "type": "commonjs",
23
+ "type": "module",
24
24
  "main": "dist/client.min.js",
25
25
  "devDependencies": {
26
26
  "terser": "^5.39.0",