hx-stream 0.1.1 → 0.2.1
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.
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
const decoder = new TextDecoder("utf8");
|
|
27
27
|
async function Process(method, url, headers, formData, source) {
|
|
28
28
|
source.classList.add("htmx-request");
|
|
29
|
-
const req = await fetch(url, { method, headers, body: formData });
|
|
29
|
+
const req = await fetch(url, { method, headers, body: formData, duplex: "half" });
|
|
30
30
|
if (!req.ok) {
|
|
31
31
|
console.error(await req.text());
|
|
32
32
|
source.classList.remove("htmx-request");
|
package/global.d.ts
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hx-stream",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "keep htmx client state across page refreshes",
|
|
5
5
|
"keywords": [ "htmx", "streaming" ],
|
|
6
6
|
"homepage": "https://hx-stream.ajanibilby.com",
|
|
@@ -16,12 +16,12 @@
|
|
|
16
16
|
"build:client": "tsc --project tsconfig.client.json",
|
|
17
17
|
"build:server": "tsc --project tsconfig.json",
|
|
18
18
|
"minify": "npm run minify:client",
|
|
19
|
-
"minify:client": "terser
|
|
19
|
+
"minify:client": "terser client.js --compress --mangle --output client.min.js"
|
|
20
20
|
},
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"author": "Ajani Bilby",
|
|
23
23
|
"type": "module",
|
|
24
|
-
"main": "
|
|
24
|
+
"main": "client.min.js",
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"terser": "^5.39.0",
|
|
27
27
|
"typescript": "^5.8.3"
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
type Options<T extends boolean> = T extends true ? RenderOptions : DefaultOptions;
|
|
2
2
|
type DefaultOptions = {
|
|
3
|
-
|
|
3
|
+
abortSignal?: AbortSignal;
|
|
4
4
|
highWaterMark?: number;
|
|
5
|
+
keepAlive?: number;
|
|
5
6
|
};
|
|
6
7
|
type RenderOptions = {
|
|
7
8
|
render: (jsx: JSX.Element) => string;
|
|
@@ -12,13 +13,11 @@ type HasRender<O> = O extends {
|
|
|
12
13
|
export declare class StreamResponse<JsxEnabled extends boolean> {
|
|
13
14
|
#private;
|
|
14
15
|
readonly response: Response;
|
|
15
|
-
readonly withCredentials: boolean;
|
|
16
|
-
readonly url: string;
|
|
17
16
|
get readyState(): number;
|
|
18
17
|
static CONNECTING: number;
|
|
19
18
|
static OPEN: number;
|
|
20
19
|
static CLOSED: number;
|
|
21
|
-
constructor(
|
|
20
|
+
constructor(options: Options<JsxEnabled>);
|
|
22
21
|
bind(controller: ReadableByteStreamController): void;
|
|
23
22
|
private sendBytes;
|
|
24
23
|
private sendText;
|
|
@@ -26,6 +25,5 @@ export declare class StreamResponse<JsxEnabled extends boolean> {
|
|
|
26
25
|
send(target: string, swap: string, html: JsxEnabled extends true ? (JSX.Element | string) : string): boolean;
|
|
27
26
|
close(): boolean;
|
|
28
27
|
}
|
|
29
|
-
export declare function
|
|
30
|
-
export declare function MakeStream<T extends Partial<RenderOptions>>(request: Request, props: T, cb: (stream: StreamResponse<HasRender<T>>, props: T) => Promise<void> | void): Response;
|
|
28
|
+
export declare function MakeStream<T extends Partial<RenderOptions>>(props: T, cb: (stream: StreamResponse<HasRender<T>>, props: T) => Promise<void> | void): Response;
|
|
31
29
|
export {};
|
|
@@ -4,7 +4,7 @@ const headers = {
|
|
|
4
4
|
"X-Content-Type-Options": "nosniff",
|
|
5
5
|
"X-Accel-Buffering": "no",
|
|
6
6
|
"Transfer-Encoding": "chunked",
|
|
7
|
-
"Content-Type": "text/
|
|
7
|
+
"Content-Type": "text/event-stream",
|
|
8
8
|
// the maximum keep alive chrome shouldn't ignore
|
|
9
9
|
"Keep-Alive": "timeout=60",
|
|
10
10
|
"Connection": "keep-alive",
|
|
@@ -21,24 +21,19 @@ export class StreamResponse {
|
|
|
21
21
|
#boundary;
|
|
22
22
|
#render;
|
|
23
23
|
response;
|
|
24
|
-
// just to make it polyfill
|
|
25
|
-
withCredentials;
|
|
26
|
-
url;
|
|
27
24
|
get readyState() { return this.#state; }
|
|
28
25
|
static CONNECTING = 0;
|
|
29
26
|
static OPEN = 1;
|
|
30
27
|
static CLOSED = 2;
|
|
31
|
-
constructor(
|
|
28
|
+
constructor(options) {
|
|
32
29
|
this.#controller = null;
|
|
33
30
|
this.#state = StreamResponse.CONNECTING;
|
|
34
31
|
this.#render = options.render;
|
|
35
32
|
this.#boundary = MakeBoundary();
|
|
36
|
-
this.withCredentials = request.mode === "cors";
|
|
37
|
-
this.url = request.url;
|
|
38
33
|
// immediate prepare for abortion
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
this.#signal
|
|
34
|
+
this.#signal = options.abortSignal;
|
|
35
|
+
const cancel = () => { this.close(); this.#signal?.removeEventListener("abort", cancel); };
|
|
36
|
+
this.#signal?.addEventListener('abort', cancel);
|
|
42
37
|
const start = (c) => { this.#controller = c; this.#state = StreamResponse.OPEN; };
|
|
43
38
|
const stream = new ReadableStream({ start, cancel }, { highWaterMark: options.highWaterMark });
|
|
44
39
|
this.response = new Response(stream, { headers });
|
|
@@ -50,10 +45,11 @@ export class StreamResponse {
|
|
|
50
45
|
}
|
|
51
46
|
sendBytes(chunk) {
|
|
52
47
|
if (this.#state === StreamResponse.CLOSED) {
|
|
53
|
-
const err = new Error(`Warn: Attempted to send data on closed stream
|
|
48
|
+
const err = new Error(`Warn: Attempted to send data on closed hx-stream`);
|
|
54
49
|
console.warn(err);
|
|
50
|
+
return false;
|
|
55
51
|
}
|
|
56
|
-
if (this.#signal
|
|
52
|
+
if (this.#signal?.aborted) {
|
|
57
53
|
this.close();
|
|
58
54
|
return false;
|
|
59
55
|
}
|
|
@@ -61,13 +57,13 @@ export class StreamResponse {
|
|
|
61
57
|
return false;
|
|
62
58
|
try {
|
|
63
59
|
this.#controller.enqueue(chunk);
|
|
64
|
-
return true;
|
|
65
60
|
}
|
|
66
61
|
catch (e) {
|
|
67
62
|
console.error(e);
|
|
68
63
|
this.close(); // unbind on failure
|
|
69
64
|
return false;
|
|
70
65
|
}
|
|
66
|
+
return true;
|
|
71
67
|
}
|
|
72
68
|
sendText(chunk) {
|
|
73
69
|
return this.sendBytes(encoder.encode(chunk));
|
|
@@ -102,11 +98,8 @@ export class StreamResponse {
|
|
|
102
98
|
return true;
|
|
103
99
|
}
|
|
104
100
|
}
|
|
105
|
-
export function
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
export function MakeStream(request, props, cb) {
|
|
109
|
-
const stream = MakeRawStream(request, props);
|
|
101
|
+
export function MakeStream(props, cb) {
|
|
102
|
+
const stream = new StreamResponse(props);
|
|
110
103
|
queueMicrotask(() => {
|
|
111
104
|
const p = cb(stream, props);
|
|
112
105
|
if (p instanceof Promise)
|
package/dist/client.min.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
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")}();
|
|
File without changes
|