hx-stream 0.0.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/LICENSE +21 -0
- package/dist/client.d.ts +0 -0
- package/dist/client.js +65 -0
- package/dist/client.min.js +1 -0
- package/dist/server.d.ts +16 -0
- package/dist/server.js +94 -0
- package/package.json +27 -0
package/LICENSE
ADDED
|
@@ -0,0 +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
|
|
21
|
+
SOFTWARE.
|
package/dist/client.d.ts
ADDED
|
File without changes
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
(function () {
|
|
12
|
+
let binding;
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
+
globalThis.htmx.defineExtension("hx-stream", {
|
|
15
|
+
init: (config) => { binding = config; },
|
|
16
|
+
onEvent: (name, event) => {
|
|
17
|
+
switch (name) {
|
|
18
|
+
case "htmx:beforeRequest": {
|
|
19
|
+
const source = event.detail.elt;
|
|
20
|
+
if (binding.getAttributeValue(source, "hx-stream") !== "on")
|
|
21
|
+
return;
|
|
22
|
+
event.preventDefault();
|
|
23
|
+
const formData = event.detail.requestConfig.formData;
|
|
24
|
+
const headers = event.detail.requestConfig.headers;
|
|
25
|
+
const url = event.detail.requestConfig.path;
|
|
26
|
+
const method = event.detail.requestConfig.verb;
|
|
27
|
+
const target = event.detail.target;
|
|
28
|
+
Process(method, url, headers, formData, target).catch(console.error);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
const decoder = new TextDecoder("utf8");
|
|
35
|
+
function Process(method, url, headers, formData, target) {
|
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
target.classList.add("htmx-request");
|
|
38
|
+
console.log(formData === null || formData === void 0 ? void 0 : formData.getAll("shipmentID"));
|
|
39
|
+
const req = yield fetch(url, { method, headers, body: formData });
|
|
40
|
+
if (!req.ok) {
|
|
41
|
+
console.error(yield req.text());
|
|
42
|
+
target.classList.remove("htmx-request");
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (!req.body) {
|
|
46
|
+
console.error("hx-stream response is missing body");
|
|
47
|
+
target.classList.remove("htmx-request");
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const reader = req.body.getReader();
|
|
51
|
+
// eslint-disable-next-line no-constant-condition
|
|
52
|
+
while (true) {
|
|
53
|
+
const { done, value: buffer } = yield reader.read();
|
|
54
|
+
if (done)
|
|
55
|
+
break;
|
|
56
|
+
const html = decoder.decode(buffer);
|
|
57
|
+
if (html === " ")
|
|
58
|
+
continue; // ignore keepalive byte
|
|
59
|
+
console.log(html);
|
|
60
|
+
binding.swap(target, html, { swapStyle: "beforeend" });
|
|
61
|
+
}
|
|
62
|
+
target.classList.remove("htmx-request");
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
})();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var __awaiter=this&&this.__awaiter||function(e,t,o,n){return new(o||(o=Promise))((function(r,s){function i(e){try{c(n.next(e))}catch(e){s(e)}}function a(e){try{c(n.throw(e))}catch(e){s(e)}}function c(e){var t;e.done?r(e.value):(t=e.value,t instanceof o?t:new o((function(e){e(t)}))).then(i,a)}c((n=n.apply(e,t||[])).next())}))};!function(){let e;globalThis.htmx.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 r=n.detail.requestConfig.formData,s=n.detail.requestConfig.headers,i=n.detail.requestConfig.path;(function(o,n,r,s,i){return __awaiter(this,void 0,void 0,(function*(){i.classList.add("htmx-request"),console.log(null==s?void 0:s.getAll("shipmentID"));const a=yield fetch(n,{method:o,headers:r,body:s});if(!a.ok)return console.error(yield a.text()),void i.classList.remove("htmx-request");if(!a.body)return console.error("hx-stream response is missing body"),void i.classList.remove("htmx-request");const c=a.body.getReader();for(;;){const{done:o,value:n}=yield c.read();if(o)break;const r=t.decode(n);" "!==r&&(console.log(r),e.swap(i,r,{swapStyle:"beforeend"}))}i.classList.remove("htmx-request")}))})(n.detail.requestConfig.verb,i,s,r,n.detail.target).catch(console.error)}}});const t=new TextDecoder("utf8")}();
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare class StreamResponse {
|
|
2
|
+
#private;
|
|
3
|
+
readonly response: Response;
|
|
4
|
+
readonly withCredentials: boolean;
|
|
5
|
+
readonly url: string;
|
|
6
|
+
get readyState(): number;
|
|
7
|
+
static CONNECTING: number;
|
|
8
|
+
static OPEN: number;
|
|
9
|
+
static CLOSED: number;
|
|
10
|
+
constructor(request: Request, keepAlive?: number);
|
|
11
|
+
private sendBytes;
|
|
12
|
+
private sendText;
|
|
13
|
+
private keepAlive;
|
|
14
|
+
send(target: string, swap: string, html: string): boolean;
|
|
15
|
+
close(): boolean;
|
|
16
|
+
}
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
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;
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "hx-stream",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "keep htmx client state across page refreshes",
|
|
5
|
+
"keywords": [ "htmx", "streaming" ],
|
|
6
|
+
"homepage": "https://hx-stream.ajanibilby.com",
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/AjaniBilby/hx-stream/issues"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/AjaniBilby/hx-stream.git"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "npm run build:client & npm run build:server",
|
|
16
|
+
"build:client": "tsc --project tsconfig.client.json & terser dist/client.js --compress --mangle --output dist/client.min.js",
|
|
17
|
+
"build:server": "tsc --project tsconfig.server.json"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"author": "Ajani Bilby",
|
|
21
|
+
"type": "commonjs",
|
|
22
|
+
"main": "dist/client.min.js",
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"terser": "^5.39.0",
|
|
25
|
+
"typescript": "^5.8.3"
|
|
26
|
+
}
|
|
27
|
+
}
|