hx-stream 0.0.0 → 0.0.2
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/LICENSE +20 -20
- package/dist/client.js +2 -4
- package/dist/client.min.js +1 -1
- package/dist/server.d.ts +14 -3
- package/package.json +5 -3
- package/dist/server.js +0 -94
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 Ajani Bilby
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Ajani Bilby
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
package/dist/client.js
CHANGED
|
@@ -35,7 +35,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
35
35
|
function Process(method, url, headers, formData, target) {
|
|
36
36
|
return __awaiter(this, void 0, void 0, function* () {
|
|
37
37
|
target.classList.add("htmx-request");
|
|
38
|
-
console.log(formData === null || formData === void 0 ? void 0 : formData.getAll("shipmentID"));
|
|
39
38
|
const req = yield fetch(url, { method, headers, body: formData });
|
|
40
39
|
if (!req.ok) {
|
|
41
40
|
console.error(yield req.text());
|
|
@@ -53,10 +52,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
53
52
|
const { done, value: buffer } = yield reader.read();
|
|
54
53
|
if (done)
|
|
55
54
|
break;
|
|
56
|
-
const html = decoder.decode(buffer);
|
|
57
|
-
if (html === "
|
|
55
|
+
const html = decoder.decode(buffer).trim();
|
|
56
|
+
if (html === "")
|
|
58
57
|
continue; // ignore keepalive byte
|
|
59
|
-
console.log(html);
|
|
60
58
|
binding.swap(target, html, { swapStyle: "beforeend" });
|
|
61
59
|
}
|
|
62
60
|
target.classList.remove("htmx-request");
|
package/dist/client.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var __awaiter=this&&this.__awaiter||function(e,t,
|
|
1
|
+
"use strict";var __awaiter=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function s(e){try{c(n.next(e))}catch(e){i(e)}}function a(e){try{c(n.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(s,a)}c((n=n.apply(e,t||[])).next())}))};!function(){let e;globalThis.htmx.defineExtension("hx-stream",{init:t=>{e=t},onEvent:(r,n)=>{if("htmx:beforeRequest"!==r);else{const r=n.detail.elt;if("on"!==e.getAttributeValue(r,"hx-stream"))return;n.preventDefault();const o=n.detail.requestConfig.formData,i=n.detail.requestConfig.headers,s=n.detail.requestConfig.path;(function(r,n,o,i,s){return __awaiter(this,void 0,void 0,(function*(){s.classList.add("htmx-request");const a=yield fetch(n,{method:r,headers:o,body:i});if(!a.ok)return console.error(yield a.text()),void s.classList.remove("htmx-request");if(!a.body)return console.error("hx-stream response is missing body"),void s.classList.remove("htmx-request");const c=a.body.getReader();for(;;){const{done:r,value:n}=yield c.read();if(r)break;const o=t.decode(n).trim();""!==o&&e.swap(s,o,{swapStyle:"beforeend"})}s.classList.remove("htmx-request")}))})(n.detail.requestConfig.verb,s,i,o,n.detail.target).catch(console.error)}}});const t=new TextDecoder("utf8")}();
|
package/dist/server.d.ts
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
type Options<T extends boolean> = T extends true ? RenderOptions : DefaultOptions;
|
|
2
|
+
type DefaultOptions = {
|
|
3
|
+
keepAlive?: number;
|
|
4
|
+
};
|
|
5
|
+
type RenderOptions = {
|
|
6
|
+
render: (jsx: JSX.Element) => string;
|
|
7
|
+
} & DefaultOptions;
|
|
8
|
+
type HasRender<O> = O extends {
|
|
9
|
+
render: (jsx: JSX.Element) => string;
|
|
10
|
+
} ? true : false;
|
|
11
|
+
export declare class StreamResponse<O extends Options<boolean>, T extends boolean = HasRender<O>> {
|
|
2
12
|
#private;
|
|
3
13
|
readonly response: Response;
|
|
4
14
|
readonly withCredentials: boolean;
|
|
@@ -7,10 +17,11 @@ export declare class StreamResponse {
|
|
|
7
17
|
static CONNECTING: number;
|
|
8
18
|
static OPEN: number;
|
|
9
19
|
static CLOSED: number;
|
|
10
|
-
constructor(request: Request,
|
|
20
|
+
constructor(request: Request, options: O);
|
|
11
21
|
private sendBytes;
|
|
12
22
|
private sendText;
|
|
13
23
|
private keepAlive;
|
|
14
|
-
send(target: string, swap: string, html: string): boolean;
|
|
24
|
+
send(target: string, swap: string, html: T extends true ? (JSX.Element | string) : string): boolean;
|
|
15
25
|
close(): boolean;
|
|
16
26
|
}
|
|
27
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hx-stream",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "keep htmx client state across page refreshes",
|
|
5
5
|
"keywords": [ "htmx", "streaming" ],
|
|
6
6
|
"homepage": "https://hx-stream.ajanibilby.com",
|
|
@@ -13,8 +13,10 @@
|
|
|
13
13
|
},
|
|
14
14
|
"scripts": {
|
|
15
15
|
"build": "npm run build:client & npm run build:server",
|
|
16
|
-
"build:client": "tsc --project tsconfig.client.json
|
|
17
|
-
"build:server": "tsc --project tsconfig.server.json"
|
|
16
|
+
"build:client": "tsc --project tsconfig.client.json",
|
|
17
|
+
"build:server": "tsc --project tsconfig.server.json",
|
|
18
|
+
"minify": "npm run minify:client",
|
|
19
|
+
"minify:client": "terser dist/client.js --compress --mangle --output dist/client.min.js"
|
|
18
20
|
},
|
|
19
21
|
"license": "MIT",
|
|
20
22
|
"author": "Ajani Bilby",
|
package/dist/server.js
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
8
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
9
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
10
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
11
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
12
|
-
};
|
|
13
|
-
var _StreamResponse_controller, _StreamResponse_timer, _StreamResponse_state;
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.StreamResponse = void 0;
|
|
16
|
-
const encoder = new TextEncoder();
|
|
17
|
-
const headers = {
|
|
18
|
-
// Chunked encoding with immediate forwarding by proxies (i.e. nginx)
|
|
19
|
-
"X-Accel-Buffering": "no",
|
|
20
|
-
"Transfer-Encoding": "chunked",
|
|
21
|
-
"Content-Type": "text/html",
|
|
22
|
-
// the maximum keep alive chrome shouldn't ignore
|
|
23
|
-
"Keep-Alive": "timeout=60",
|
|
24
|
-
"Connection": "keep-alive",
|
|
25
|
-
};
|
|
26
|
-
class StreamResponse {
|
|
27
|
-
get readyState() { return __classPrivateFieldGet(this, _StreamResponse_state, "f"); }
|
|
28
|
-
constructor(request, keepAlive = 30000) {
|
|
29
|
-
_StreamResponse_controller.set(this, void 0);
|
|
30
|
-
_StreamResponse_timer.set(this, void 0);
|
|
31
|
-
_StreamResponse_state.set(this, void 0);
|
|
32
|
-
__classPrivateFieldSet(this, _StreamResponse_controller, null, "f");
|
|
33
|
-
__classPrivateFieldSet(this, _StreamResponse_state, StreamResponse.CONNECTING, "f");
|
|
34
|
-
this.withCredentials = request.mode === "cors";
|
|
35
|
-
this.url = request.url;
|
|
36
|
-
// immediate prepare for abortion
|
|
37
|
-
const cancel = () => { this.close(); };
|
|
38
|
-
request.signal.addEventListener('abort', cancel);
|
|
39
|
-
const start = (c) => { __classPrivateFieldSet(this, _StreamResponse_controller, c, "f"); __classPrivateFieldSet(this, _StreamResponse_state, StreamResponse.OPEN, "f"); };
|
|
40
|
-
const stream = new ReadableStream({ start, cancel }, { highWaterMark: 0 });
|
|
41
|
-
this.response = new Response(stream, { headers });
|
|
42
|
-
__classPrivateFieldSet(this, _StreamResponse_timer, setInterval(() => this.keepAlive(), keepAlive), "f");
|
|
43
|
-
}
|
|
44
|
-
sendBytes(chunk) {
|
|
45
|
-
if (!__classPrivateFieldGet(this, _StreamResponse_controller, "f"))
|
|
46
|
-
return false;
|
|
47
|
-
try {
|
|
48
|
-
__classPrivateFieldGet(this, _StreamResponse_controller, "f").enqueue(chunk);
|
|
49
|
-
return true;
|
|
50
|
-
}
|
|
51
|
-
catch (e) {
|
|
52
|
-
console.error(e);
|
|
53
|
-
this.close(); // unbind on failure
|
|
54
|
-
return false;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
sendText(chunk) {
|
|
58
|
-
return this.sendBytes(encoder.encode(chunk));
|
|
59
|
-
}
|
|
60
|
-
keepAlive() { return this.sendText(" "); }
|
|
61
|
-
send(target, swap, html) {
|
|
62
|
-
if (__classPrivateFieldGet(this, _StreamResponse_state, "f") === StreamResponse.CLOSED) {
|
|
63
|
-
const err = new Error(`Warn: Attempted to send data on closed stream for: ${this.url}`);
|
|
64
|
-
console.warn(err);
|
|
65
|
-
}
|
|
66
|
-
return this.sendText(`<div hx-swap-oob="${swap}:${target}">${html}</div>`);
|
|
67
|
-
}
|
|
68
|
-
close() {
|
|
69
|
-
if (__classPrivateFieldGet(this, _StreamResponse_state, "f") === StreamResponse.CLOSED) {
|
|
70
|
-
__classPrivateFieldSet(this, _StreamResponse_controller, null, "f");
|
|
71
|
-
return false;
|
|
72
|
-
}
|
|
73
|
-
if (__classPrivateFieldGet(this, _StreamResponse_controller, "f")) {
|
|
74
|
-
try {
|
|
75
|
-
__classPrivateFieldGet(this, _StreamResponse_controller, "f").close();
|
|
76
|
-
}
|
|
77
|
-
catch (e) {
|
|
78
|
-
console.error(e);
|
|
79
|
-
}
|
|
80
|
-
__classPrivateFieldSet(this, _StreamResponse_controller, null, "f");
|
|
81
|
-
}
|
|
82
|
-
// Cleanup
|
|
83
|
-
if (__classPrivateFieldGet(this, _StreamResponse_timer, "f"))
|
|
84
|
-
clearInterval(__classPrivateFieldGet(this, _StreamResponse_timer, "f"));
|
|
85
|
-
// Mark closed
|
|
86
|
-
__classPrivateFieldSet(this, _StreamResponse_state, StreamResponse.CLOSED, "f");
|
|
87
|
-
return true;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
exports.StreamResponse = StreamResponse;
|
|
91
|
-
_StreamResponse_controller = new WeakMap(), _StreamResponse_timer = new WeakMap(), _StreamResponse_state = new WeakMap();
|
|
92
|
-
StreamResponse.CONNECTING = 0;
|
|
93
|
-
StreamResponse.OPEN = 1;
|
|
94
|
-
StreamResponse.CLOSED = 2;
|