hx-stream 0.2.2 → 0.2.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/client.js +5 -2
- package/client.min.js +1 -1
- package/package.json +6 -2
- package/server.d.ts +1 -1
- package/server.js +5 -6
package/client.js
CHANGED
|
@@ -23,10 +23,13 @@
|
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
});
|
|
26
|
+
const METHODS_WITHOUT_BODY = new Set(['GET', 'HEAD', 'DELETE', 'OPTIONS', 'TRACE', 'CONNECT']);
|
|
26
27
|
const decoder = new TextDecoder("utf8");
|
|
27
|
-
async function Process(method, url, headers,
|
|
28
|
+
async function Process(method, url, headers, body, source) {
|
|
29
|
+
if (METHODS_WITHOUT_BODY.has(method.toUpperCase()))
|
|
30
|
+
body = undefined;
|
|
28
31
|
source.classList.add("htmx-request");
|
|
29
|
-
const req = await fetch(url, { method, headers, body
|
|
32
|
+
const req = await fetch(url, { method, headers, body, duplex: "half" });
|
|
30
33
|
if (!req.ok) {
|
|
31
34
|
console.error(await req.text());
|
|
32
35
|
source.classList.remove("htmx-request");
|
package/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:(
|
|
1
|
+
"use strict";!function(){let e;const t=globalThis.htmx;t.defineExtension("hx-stream",{init:t=>{e=t},onEvent:(o,n)=>{if("htmx:beforeRequest"!==o);else{const o=n.detail.elt;if("on"!==e.getAttributeValue(o,"hx-stream"))return;n.preventDefault();const i=n.detail.requestConfig.formData,a=n.detail.requestConfig.headers,c=n.detail.requestConfig.path;(async function(o,n,i,a,c){r.has(o.toUpperCase())&&(a=void 0);c.classList.add("htmx-request");const d=await fetch(n,{method:o,headers:i,body:a,duplex:"half"});if(!d.ok)return console.error(await d.text()),void c.classList.remove("htmx-request");if(!d.body)return console.error("hx-stream response is missing body"),void c.classList.remove("htmx-request");const l=d.headers.get("X-Chunk-Boundary");if(!l)return console.error("hx-stream response is chunk boundary header"),void c.classList.remove("htmx-request");const h=`<${l}>`,u=`</${l}>`,f=d.body.getReader();let m="";for(;;){const{done:r,value:o}=await f.read();if(r)break;const n=s.decode(o),i=Math.max(0,m.length-u.length);m+=n;let a=m.indexOf(u,i);for(;-1!==a;){let r=m.lastIndexOf(h,a);if(-1===r)return console.error("hx-stream received invalid chunk");r+=h.length;const s=m.indexOf("|",r);if(-1===s)return console.error("hx-stream received invalid chunk");const o=m.indexOf("|",s+1);if(-1===o)return console.error("hx-stream received invalid chunk");const n=m.slice(r,s).trim(),i=m.slice(s+1,o).trim(),d=m.slice(o+1,a);let l;switch(n){case"this":l=c;break;case"body":l=document.body;break;default:l=t.find(c,n)}l?e.swap(l,d,{swapStyle:i}):console.warn(`hx-stream unable to find target ${n}`),m=m.slice(a+u.length),a=m.indexOf(u)}}c.classList.remove("htmx-request")})(n.detail.requestConfig.verb,c,a,i,n.detail.target).catch(console.error)}}});const r=new Set(["GET","HEAD","DELETE","OPTIONS","TRACE","CONNECT"]),s=new TextDecoder("utf8")}();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hx-stream",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"description": "keep htmx client state across page refreshes",
|
|
5
5
|
"keywords": [ "htmx", "streaming" ],
|
|
6
6
|
"homepage": "https://hx-stream.ajanibilby.com",
|
|
@@ -18,10 +18,14 @@
|
|
|
18
18
|
"minify": "npm run minify:client",
|
|
19
19
|
"minify:client": "terser client.js --compress --mangle --output client.min.js"
|
|
20
20
|
},
|
|
21
|
+
"main": "client.min.js",
|
|
22
|
+
"exports": {
|
|
23
|
+
"./client": "./client.js",
|
|
24
|
+
"./server": "./server.js"
|
|
25
|
+
},
|
|
21
26
|
"license": "MIT",
|
|
22
27
|
"author": "Ajani Bilby",
|
|
23
28
|
"type": "module",
|
|
24
|
-
"main": "client.min.js",
|
|
25
29
|
"devDependencies": {
|
|
26
30
|
"terser": "^5.39.0",
|
|
27
31
|
"typescript": "^5.8.3"
|
package/server.d.ts
CHANGED
|
@@ -22,7 +22,7 @@ export declare class StreamResponse<JsxEnabled extends boolean> {
|
|
|
22
22
|
private sendBytes;
|
|
23
23
|
private sendText;
|
|
24
24
|
private keepAlive;
|
|
25
|
-
send(target: string, swap: string, html: JsxEnabled extends true ? (JSX.Element | string) : string): boolean;
|
|
25
|
+
send(target: string, swap: string, html: JsxEnabled extends true ? (JSX.Element | string) : string): boolean | undefined;
|
|
26
26
|
close(): boolean;
|
|
27
27
|
}
|
|
28
28
|
export declare function MakeStream<T extends Partial<RenderOptions>>(props: T, cb: (stream: StreamResponse<HasRender<T>>, props: T) => Promise<void> | void): Response;
|
package/server.js
CHANGED
|
@@ -70,12 +70,11 @@ export class StreamResponse {
|
|
|
70
70
|
}
|
|
71
71
|
keepAlive() { return this.sendText(" "); }
|
|
72
72
|
send(target, swap, html) {
|
|
73
|
-
if (typeof html
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return this.sendText(`<${this.#boundary}>${target}|${swap}|${html}</${this.#boundary}>\n`);
|
|
73
|
+
if (typeof html === "string")
|
|
74
|
+
return this.sendText(`<${this.#boundary}>${target}|${swap}|${html}</${this.#boundary}>\n`);
|
|
75
|
+
if (!this.#render)
|
|
76
|
+
throw new Error(`Cannot render to JSX when no renderer provided during class initialization`);
|
|
77
|
+
html = this.#render(html);
|
|
79
78
|
}
|
|
80
79
|
close() {
|
|
81
80
|
if (this.#controller) {
|