hx-stream 0.1.1 → 0.2.0
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 +1 -1
- package/dist/client.min.js +1 -1
- package/package.json +3 -3
- package/dist/server.d.ts +0 -31
- package/dist/server.js +0 -116
package/dist/client.js
CHANGED
|
@@ -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/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,duplex:"half"});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=`<${l}>`,h=`</${l}>`,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(d,c);if(-1===r)return console.error("hx-stream received invalid chunk");r+=d.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(),l=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,l,{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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hx-stream",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
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"
|
package/dist/server.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
type Options<T extends boolean> = T extends true ? RenderOptions : DefaultOptions;
|
|
2
|
-
type DefaultOptions = {
|
|
3
|
-
keepAlive?: number;
|
|
4
|
-
highWaterMark?: number;
|
|
5
|
-
};
|
|
6
|
-
type RenderOptions = {
|
|
7
|
-
render: (jsx: JSX.Element) => string;
|
|
8
|
-
} & DefaultOptions;
|
|
9
|
-
type HasRender<O> = O extends {
|
|
10
|
-
render: (jsx: JSX.Element) => string;
|
|
11
|
-
} ? true : false;
|
|
12
|
-
export declare class StreamResponse<JsxEnabled extends boolean> {
|
|
13
|
-
#private;
|
|
14
|
-
readonly response: Response;
|
|
15
|
-
readonly withCredentials: boolean;
|
|
16
|
-
readonly url: string;
|
|
17
|
-
get readyState(): number;
|
|
18
|
-
static CONNECTING: number;
|
|
19
|
-
static OPEN: number;
|
|
20
|
-
static CLOSED: number;
|
|
21
|
-
constructor(request: Request, options: Options<JsxEnabled>);
|
|
22
|
-
bind(controller: ReadableByteStreamController): void;
|
|
23
|
-
private sendBytes;
|
|
24
|
-
private sendText;
|
|
25
|
-
private keepAlive;
|
|
26
|
-
send(target: string, swap: string, html: JsxEnabled extends true ? (JSX.Element | string) : string): boolean;
|
|
27
|
-
close(): boolean;
|
|
28
|
-
}
|
|
29
|
-
export declare function MakeRawStream<O extends (DefaultOptions | RenderOptions)>(request: Request, options: O): StreamResponse<HasRender<O>>;
|
|
30
|
-
export declare function MakeStream<T extends Partial<RenderOptions>>(request: Request, props: T, cb: (stream: StreamResponse<HasRender<T>>, props: T) => Promise<void> | void): Response;
|
|
31
|
-
export {};
|
package/dist/server.js
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
const encoder = new TextEncoder();
|
|
2
|
-
const headers = {
|
|
3
|
-
// Chunked encoding with immediate forwarding by proxies (i.e. nginx)
|
|
4
|
-
"X-Content-Type-Options": "nosniff",
|
|
5
|
-
"X-Accel-Buffering": "no",
|
|
6
|
-
"Transfer-Encoding": "chunked",
|
|
7
|
-
"Content-Type": "text/html",
|
|
8
|
-
// the maximum keep alive chrome shouldn't ignore
|
|
9
|
-
"Keep-Alive": "timeout=60",
|
|
10
|
-
"Connection": "keep-alive",
|
|
11
|
-
};
|
|
12
|
-
const SCALE = 36 ** 6;
|
|
13
|
-
function MakeBoundary() {
|
|
14
|
-
return "hx-" + Math.floor(Math.random() * SCALE).toString(36);
|
|
15
|
-
}
|
|
16
|
-
export class StreamResponse {
|
|
17
|
-
#controller;
|
|
18
|
-
#signal;
|
|
19
|
-
#timer;
|
|
20
|
-
#state;
|
|
21
|
-
#boundary;
|
|
22
|
-
#render;
|
|
23
|
-
response;
|
|
24
|
-
// just to make it polyfill
|
|
25
|
-
withCredentials;
|
|
26
|
-
url;
|
|
27
|
-
get readyState() { return this.#state; }
|
|
28
|
-
static CONNECTING = 0;
|
|
29
|
-
static OPEN = 1;
|
|
30
|
-
static CLOSED = 2;
|
|
31
|
-
constructor(request, options) {
|
|
32
|
-
this.#controller = null;
|
|
33
|
-
this.#state = StreamResponse.CONNECTING;
|
|
34
|
-
this.#render = options.render;
|
|
35
|
-
this.#boundary = MakeBoundary();
|
|
36
|
-
this.withCredentials = request.mode === "cors";
|
|
37
|
-
this.url = request.url;
|
|
38
|
-
// immediate prepare for abortion
|
|
39
|
-
const cancel = () => { this.close(); request.signal.removeEventListener("abort", cancel); };
|
|
40
|
-
request.signal.addEventListener('abort', cancel);
|
|
41
|
-
this.#signal = request.signal;
|
|
42
|
-
const start = (c) => { this.#controller = c; this.#state = StreamResponse.OPEN; };
|
|
43
|
-
const stream = new ReadableStream({ start, cancel }, { highWaterMark: options.highWaterMark });
|
|
44
|
-
this.response = new Response(stream, { headers });
|
|
45
|
-
this.response.headers.set("X-Chunk-Boundary", this.#boundary);
|
|
46
|
-
this.#timer = setInterval(() => this.keepAlive(), options.keepAlive || 30_000);
|
|
47
|
-
}
|
|
48
|
-
bind(controller) {
|
|
49
|
-
this.#controller = controller;
|
|
50
|
-
}
|
|
51
|
-
sendBytes(chunk) {
|
|
52
|
-
if (this.#state === StreamResponse.CLOSED) {
|
|
53
|
-
const err = new Error(`Warn: Attempted to send data on closed stream for: ${this.url}`);
|
|
54
|
-
console.warn(err);
|
|
55
|
-
}
|
|
56
|
-
if (this.#signal.aborted) {
|
|
57
|
-
this.close();
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
60
|
-
if (!this.#controller)
|
|
61
|
-
return false;
|
|
62
|
-
try {
|
|
63
|
-
this.#controller.enqueue(chunk);
|
|
64
|
-
return true;
|
|
65
|
-
}
|
|
66
|
-
catch (e) {
|
|
67
|
-
console.error(e);
|
|
68
|
-
this.close(); // unbind on failure
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
sendText(chunk) {
|
|
73
|
-
return this.sendBytes(encoder.encode(chunk));
|
|
74
|
-
}
|
|
75
|
-
keepAlive() { return this.sendText(" "); }
|
|
76
|
-
send(target, swap, html) {
|
|
77
|
-
if (typeof html !== "string") {
|
|
78
|
-
if (!this.#render)
|
|
79
|
-
throw new Error(`Cannot render to JSX when no renderer provided during class initialization`);
|
|
80
|
-
html = this.#render(html);
|
|
81
|
-
}
|
|
82
|
-
return this.sendText(`<${this.#boundary}>${target}|${swap}|${html}</${this.#boundary}>\n`);
|
|
83
|
-
}
|
|
84
|
-
close() {
|
|
85
|
-
if (this.#controller) {
|
|
86
|
-
try {
|
|
87
|
-
this.#controller.close();
|
|
88
|
-
}
|
|
89
|
-
catch (e) {
|
|
90
|
-
console.error(e);
|
|
91
|
-
}
|
|
92
|
-
this.#controller = null;
|
|
93
|
-
}
|
|
94
|
-
// Cleanup
|
|
95
|
-
if (this.#timer)
|
|
96
|
-
clearInterval(this.#timer);
|
|
97
|
-
// was already closed
|
|
98
|
-
if (this.#state === EventSource.CLOSED)
|
|
99
|
-
return false;
|
|
100
|
-
// Mark closed
|
|
101
|
-
this.#state = StreamResponse.CLOSED;
|
|
102
|
-
return true;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
export function MakeRawStream(request, options) {
|
|
106
|
-
return new StreamResponse(request, options);
|
|
107
|
-
}
|
|
108
|
-
export function MakeStream(request, props, cb) {
|
|
109
|
-
const stream = MakeRawStream(request, props);
|
|
110
|
-
queueMicrotask(() => {
|
|
111
|
-
const p = cb(stream, props);
|
|
112
|
-
if (p instanceof Promise)
|
|
113
|
-
p.catch(console.error);
|
|
114
|
-
});
|
|
115
|
-
return stream.response;
|
|
116
|
-
}
|