hx-stream 0.0.3 → 0.0.4
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.js +93 -0
- package/package.json +1 -1
package/dist/server.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
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.withCredentials = request.mode === "cors";
|
|
28
|
+
this.url = request.url;
|
|
29
|
+
// immediate prepare for abortion
|
|
30
|
+
const cancel = () => { this.close(); };
|
|
31
|
+
request.signal.addEventListener('abort', cancel);
|
|
32
|
+
const start = (c) => { this.#controller = c; this.#state = StreamResponse.OPEN; };
|
|
33
|
+
const stream = new ReadableStream({ start, cancel }, { highWaterMark: 0 });
|
|
34
|
+
this.response = new Response(stream, { headers });
|
|
35
|
+
this.#timer = setInterval(() => this.keepAlive(), options.keepAlive || 30_000);
|
|
36
|
+
}
|
|
37
|
+
bind(controller) {
|
|
38
|
+
this.#controller = controller;
|
|
39
|
+
}
|
|
40
|
+
sendBytes(chunk) {
|
|
41
|
+
if (!this.#controller)
|
|
42
|
+
return false;
|
|
43
|
+
try {
|
|
44
|
+
this.#controller.enqueue(chunk);
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
catch (e) {
|
|
48
|
+
console.error(e);
|
|
49
|
+
this.close(); // unbind on failure
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
sendText(chunk) {
|
|
54
|
+
return this.sendBytes(encoder.encode(chunk));
|
|
55
|
+
}
|
|
56
|
+
keepAlive() { return this.sendText(" "); }
|
|
57
|
+
send(target, swap, html) {
|
|
58
|
+
if (this.#state === StreamResponse.CLOSED) {
|
|
59
|
+
const err = new Error(`Warn: Attempted to send data on closed stream for: ${this.url}`);
|
|
60
|
+
console.warn(err);
|
|
61
|
+
}
|
|
62
|
+
if (typeof html !== "string") {
|
|
63
|
+
if (!this.#render)
|
|
64
|
+
throw new Error(`Cannot render to JSX when no renderer provided during class initialization`);
|
|
65
|
+
html = this.#render(html);
|
|
66
|
+
}
|
|
67
|
+
return this.sendText(`<div hx-swap-oob="${swap}:${target}">${html}</div>`);
|
|
68
|
+
}
|
|
69
|
+
close() {
|
|
70
|
+
if (this.#state === StreamResponse.CLOSED) {
|
|
71
|
+
this.#controller = null;
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
if (this.#controller) {
|
|
75
|
+
try {
|
|
76
|
+
this.#controller.close();
|
|
77
|
+
}
|
|
78
|
+
catch (e) {
|
|
79
|
+
console.error(e);
|
|
80
|
+
}
|
|
81
|
+
this.#controller = null;
|
|
82
|
+
}
|
|
83
|
+
// Cleanup
|
|
84
|
+
if (this.#timer)
|
|
85
|
+
clearInterval(this.#timer);
|
|
86
|
+
// Mark closed
|
|
87
|
+
this.#state = StreamResponse.CLOSED;
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
export function MakeStream(request, options) {
|
|
92
|
+
return new StreamResponse(request, options);
|
|
93
|
+
}
|