hx-stream 0.0.8 → 0.0.10
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 +10 -1
- package/dist/client.min.js +1 -1
- package/dist/server.js +13 -8
- package/package.json +1 -1
package/dist/client.js
CHANGED
|
@@ -70,7 +70,16 @@
|
|
|
70
70
|
const retarget = buffer.slice(a, b).trim();
|
|
71
71
|
const swap = buffer.slice(b + 1, c).trim();
|
|
72
72
|
const html = buffer.slice(c + 1, idx);
|
|
73
|
-
|
|
73
|
+
let target;
|
|
74
|
+
switch (retarget) {
|
|
75
|
+
case "this":
|
|
76
|
+
target = source;
|
|
77
|
+
break;
|
|
78
|
+
case "body":
|
|
79
|
+
target = document.body;
|
|
80
|
+
break;
|
|
81
|
+
default: target = htmx.find(source, retarget);
|
|
82
|
+
}
|
|
74
83
|
if (target)
|
|
75
84
|
binding.swap(target, html, { swapStyle: swap });
|
|
76
85
|
else
|
package/dist/client.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";!function(){let e;const t=globalThis.htmx;t.defineExtension("hx-stream",{init:t=>{e=t},onEvent:(s,n)=>{if("htmx:beforeRequest"!==s);else{const s=n.detail.elt;if("on"!==e.getAttributeValue(s,"hx-stream"))return;n.preventDefault();const o=n.detail.requestConfig.formData,i=n.detail.requestConfig.headers,a=n.detail.requestConfig.path;(async function(s,n,o,i,a){a.classList.add("htmx-request");const c=await fetch(n,{method:s,headers:o,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
|
|
1
|
+
"use strict";!function(){let e;const t=globalThis.htmx;t.defineExtension("hx-stream",{init:t=>{e=t},onEvent:(s,n)=>{if("htmx:beforeRequest"!==s);else{const s=n.detail.elt;if("on"!==e.getAttributeValue(s,"hx-stream"))return;n.preventDefault();const o=n.detail.requestConfig.formData,i=n.detail.requestConfig.headers,a=n.detail.requestConfig.path;(async function(s,n,o,i,a){a.classList.add("htmx-request");const c=await fetch(n,{method:s,headers:o,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 d=c.headers.get("X-Chunk-Boundary");if(!d)return console.error("hx-stream response is chunk boundary header"),void a.classList.remove("htmx-request");const l=`<${d}>`,h=`</${d}>`,u=c.body.getReader();let f="";for(;;){const{done:s,value:n}=await u.read();if(s)break;const o=r.decode(n),i=Math.max(0,f.length-h.length);f+=o;let c=f.indexOf(h,i);for(;-1!==c;){let r=f.lastIndexOf(l,c);if(-1===r)return console.error("hx-stream received invalid chunk");r+=l.length;const s=f.indexOf("|",r);if(-1===s)return console.error("hx-stream received invalid chunk");const n=f.indexOf("|",s+1);if(-1===n)return console.error("hx-stream received invalid chunk");const o=f.slice(r,s).trim(),i=f.slice(s+1,n).trim(),d=f.slice(n+1,c);let u;switch(o){case"this":u=a;break;case"body":u=document.body;break;default:u=t.find(a,o)}u?e.swap(u,d,{swapStyle:i}):console.warn(`hx-stream unable to find target ${o}`),f=f.slice(c+h.length),c=f.indexOf(h)}}a.classList.remove("htmx-request")})(n.detail.requestConfig.verb,a,i,o,n.detail.target).catch(console.error)}}});const r=new TextDecoder("utf8")}();
|
package/dist/server.js
CHANGED
|
@@ -14,6 +14,7 @@ function MakeBoundary() {
|
|
|
14
14
|
}
|
|
15
15
|
export class StreamResponse {
|
|
16
16
|
#controller;
|
|
17
|
+
#signal;
|
|
17
18
|
#timer;
|
|
18
19
|
#state;
|
|
19
20
|
#boundary;
|
|
@@ -36,6 +37,7 @@ export class StreamResponse {
|
|
|
36
37
|
// immediate prepare for abortion
|
|
37
38
|
const cancel = () => { this.close(); };
|
|
38
39
|
request.signal.addEventListener('abort', cancel);
|
|
40
|
+
this.#signal = request.signal;
|
|
39
41
|
const start = (c) => { this.#controller = c; this.#state = StreamResponse.OPEN; };
|
|
40
42
|
const stream = new ReadableStream({ start, cancel }, { highWaterMark: options.highWaterMark || 0 });
|
|
41
43
|
this.response = new Response(stream, { headers });
|
|
@@ -46,6 +48,14 @@ export class StreamResponse {
|
|
|
46
48
|
this.#controller = controller;
|
|
47
49
|
}
|
|
48
50
|
sendBytes(chunk) {
|
|
51
|
+
if (this.#state === StreamResponse.CLOSED) {
|
|
52
|
+
const err = new Error(`Warn: Attempted to send data on closed stream for: ${this.url}`);
|
|
53
|
+
console.warn(err);
|
|
54
|
+
}
|
|
55
|
+
if (this.#signal.aborted) {
|
|
56
|
+
this.close();
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
49
59
|
if (!this.#controller)
|
|
50
60
|
return false;
|
|
51
61
|
try {
|
|
@@ -63,10 +73,6 @@ export class StreamResponse {
|
|
|
63
73
|
}
|
|
64
74
|
keepAlive() { return this.sendText(" "); }
|
|
65
75
|
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
76
|
if (typeof html !== "string") {
|
|
71
77
|
if (!this.#render)
|
|
72
78
|
throw new Error(`Cannot render to JSX when no renderer provided during class initialization`);
|
|
@@ -75,10 +81,6 @@ export class StreamResponse {
|
|
|
75
81
|
return this.sendText(`<${this.#boundary}>${target}|${swap}|${html}</${this.#boundary}>\n`);
|
|
76
82
|
}
|
|
77
83
|
close() {
|
|
78
|
-
if (this.#state === StreamResponse.CLOSED) {
|
|
79
|
-
this.#controller = null;
|
|
80
|
-
return false;
|
|
81
|
-
}
|
|
82
84
|
if (this.#controller) {
|
|
83
85
|
try {
|
|
84
86
|
this.#controller.close();
|
|
@@ -91,6 +93,9 @@ export class StreamResponse {
|
|
|
91
93
|
// Cleanup
|
|
92
94
|
if (this.#timer)
|
|
93
95
|
clearInterval(this.#timer);
|
|
96
|
+
// was already closed
|
|
97
|
+
if (this.#state === EventSource.CLOSED)
|
|
98
|
+
return false;
|
|
94
99
|
// Mark closed
|
|
95
100
|
this.#state = StreamResponse.CLOSED;
|
|
96
101
|
return true;
|