hx-stream 0.0.6 → 0.0.7
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/client.js +38 -10
- package/dist/client.min.js +1 -1
- package/dist/server.js +8 -1
- package/package.json +1 -1
package/dist/client.js
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
(function () {
|
|
3
3
|
let binding;
|
|
4
4
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
|
-
globalThis.htmx
|
|
5
|
+
const htmx = globalThis.htmx;
|
|
6
|
+
htmx.defineExtension("hx-stream", {
|
|
6
7
|
init: (config) => { binding = config; },
|
|
7
8
|
onEvent: (name, event) => {
|
|
8
9
|
switch (name) {
|
|
@@ -23,30 +24,57 @@
|
|
|
23
24
|
}
|
|
24
25
|
});
|
|
25
26
|
const decoder = new TextDecoder("utf8");
|
|
26
|
-
async function Process(method, url, headers, formData,
|
|
27
|
-
|
|
27
|
+
async function Process(method, url, headers, formData, source) {
|
|
28
|
+
source.classList.add("htmx-request");
|
|
28
29
|
const req = await fetch(url, { method, headers, body: formData });
|
|
29
30
|
if (!req.ok) {
|
|
30
31
|
console.error(await req.text());
|
|
31
|
-
|
|
32
|
+
source.classList.remove("htmx-request");
|
|
32
33
|
return;
|
|
33
34
|
}
|
|
34
35
|
if (!req.body) {
|
|
35
36
|
console.error("hx-stream response is missing body");
|
|
36
|
-
|
|
37
|
+
source.classList.remove("htmx-request");
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const boundary = req.headers.get("X-Chunk-Boundary");
|
|
41
|
+
if (!boundary) {
|
|
42
|
+
console.error("hx-stream response is chunk boundary header");
|
|
43
|
+
source.classList.remove("htmx-request");
|
|
37
44
|
return;
|
|
38
45
|
}
|
|
39
46
|
const reader = req.body.getReader();
|
|
47
|
+
let buffer = "";
|
|
40
48
|
// eslint-disable-next-line no-constant-condition
|
|
41
49
|
while (true) {
|
|
42
50
|
const { done, value } = await reader.read();
|
|
43
51
|
if (done)
|
|
44
52
|
break;
|
|
45
|
-
const html = decoder.decode(value)
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
53
|
+
const html = decoder.decode(value);
|
|
54
|
+
const search = Math.max(0, buffer.length - boundary.length);
|
|
55
|
+
buffer += html;
|
|
56
|
+
let idx = buffer.indexOf(boundary, search);
|
|
57
|
+
while (idx !== -1) {
|
|
58
|
+
const chunk = buffer.slice(0, idx);
|
|
59
|
+
const a = chunk.indexOf("|");
|
|
60
|
+
if (a === -1)
|
|
61
|
+
return console.error("hx-stream received invalid chunk");
|
|
62
|
+
const b = chunk.indexOf("|", a + 1);
|
|
63
|
+
if (b === -1)
|
|
64
|
+
return console.error("hx-stream received invalid chunk");
|
|
65
|
+
const retarget = chunk.slice(0, a).trim(); // trim to remove any keep alive spaces
|
|
66
|
+
const swap = chunk.slice(a + 1, b);
|
|
67
|
+
const html = chunk.slice(b + 1);
|
|
68
|
+
const target = htmx.find(source, retarget);
|
|
69
|
+
if (target)
|
|
70
|
+
binding.swap(target, html, { swapStyle: swap });
|
|
71
|
+
else
|
|
72
|
+
console.warn(`hx-stream unable to find target ${retarget}`);
|
|
73
|
+
buffer = buffer.slice(idx + boundary.length);
|
|
74
|
+
idx = buffer.indexOf(boundary);
|
|
75
|
+
}
|
|
76
|
+
// binding.swap(target, html, { swapStyle: "beforeend" });
|
|
49
77
|
}
|
|
50
|
-
|
|
78
|
+
source.classList.remove("htmx-request");
|
|
51
79
|
}
|
|
52
80
|
})();
|
package/dist/client.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";!function(){let e;globalThis.htmx.defineExtension("hx-stream",{init:t=>{e=t},onEvent:(s,
|
|
1
|
+
"use strict";!function(){let e;const t=globalThis.htmx;t.defineExtension("hx-stream",{init:t=>{e=t},onEvent:(s,o)=>{if("htmx:beforeRequest"!==s);else{const s=o.detail.elt;if("on"!==e.getAttributeValue(s,"hx-stream"))return;o.preventDefault();const n=o.detail.requestConfig.formData,i=o.detail.requestConfig.headers,a=o.detail.requestConfig.path;(async function(s,o,n,i,a){a.classList.add("htmx-request");const c=await fetch(o,{method:s,headers:n,body:i});if(!c.ok)return console.error(await c.text()),void a.classList.remove("htmx-request");if(!c.body)return console.error("hx-stream response is missing body"),void a.classList.remove("htmx-request");const l=c.headers.get("X-Chunk-Boundary");if(!l)return console.error("hx-stream response is chunk boundary header"),void a.classList.remove("htmx-request");const d=c.body.getReader();let h="";for(;;){const{done:s,value:o}=await d.read();if(s)break;const n=r.decode(o),i=Math.max(0,h.length-l.length);h+=n;let c=h.indexOf(l,i);for(;-1!==c;){const r=h.slice(0,c),s=r.indexOf("|");if(-1===s)return console.error("hx-stream received invalid chunk");const o=r.indexOf("|",s+1);if(-1===o)return console.error("hx-stream received invalid chunk");const n=r.slice(0,s).trim(),i=r.slice(s+1,o),d=r.slice(o+1),u=t.find(a,n);u?e.swap(u,d,{swapStyle:i}):console.warn(`hx-stream unable to find target ${n}`),h=h.slice(c+l.length),c=h.indexOf(l)}}a.classList.remove("htmx-request")})(o.detail.requestConfig.verb,a,i,n,o.detail.target).catch(console.error)}}});const r=new TextDecoder("utf8")}();
|
package/dist/server.js
CHANGED
|
@@ -8,10 +8,15 @@ const headers = {
|
|
|
8
8
|
"Keep-Alive": "timeout=60",
|
|
9
9
|
"Connection": "keep-alive",
|
|
10
10
|
};
|
|
11
|
+
const SCALE = 36 ** 10;
|
|
12
|
+
function MakeBoundary() {
|
|
13
|
+
return "<" + (Math.random() * SCALE).toString(36) + ">";
|
|
14
|
+
}
|
|
11
15
|
export class StreamResponse {
|
|
12
16
|
#controller;
|
|
13
17
|
#timer;
|
|
14
18
|
#state;
|
|
19
|
+
#boundary;
|
|
15
20
|
#render;
|
|
16
21
|
response;
|
|
17
22
|
// just to make it polyfill
|
|
@@ -25,6 +30,7 @@ export class StreamResponse {
|
|
|
25
30
|
this.#controller = null;
|
|
26
31
|
this.#state = StreamResponse.CONNECTING;
|
|
27
32
|
this.#render = options.render;
|
|
33
|
+
this.#boundary = MakeBoundary();
|
|
28
34
|
this.withCredentials = request.mode === "cors";
|
|
29
35
|
this.url = request.url;
|
|
30
36
|
// immediate prepare for abortion
|
|
@@ -33,6 +39,7 @@ export class StreamResponse {
|
|
|
33
39
|
const start = (c) => { this.#controller = c; this.#state = StreamResponse.OPEN; };
|
|
34
40
|
const stream = new ReadableStream({ start, cancel }, { highWaterMark: 0 });
|
|
35
41
|
this.response = new Response(stream, { headers });
|
|
42
|
+
this.response.headers.set("X-Chunk-Boundary", this.#boundary);
|
|
36
43
|
this.#timer = setInterval(() => this.keepAlive(), options.keepAlive || 30_000);
|
|
37
44
|
}
|
|
38
45
|
bind(controller) {
|
|
@@ -65,7 +72,7 @@ export class StreamResponse {
|
|
|
65
72
|
throw new Error(`Cannot render to JSX when no renderer provided during class initialization`);
|
|
66
73
|
html = this.#render(html);
|
|
67
74
|
}
|
|
68
|
-
return this.sendText(
|
|
75
|
+
return this.sendText(`${target}|${swap}|${html}${this.#boundary}`);
|
|
69
76
|
}
|
|
70
77
|
close() {
|
|
71
78
|
if (this.#state === StreamResponse.CLOSED) {
|