hx-stream 0.2.3 → 0.2.5

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 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, formData, source) {
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: formData, duplex: "half" });
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:(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")}();
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",
3
+ "version": "0.2.5",
4
4
  "description": "keep htmx client state across page refreshes",
5
5
  "keywords": [ "htmx", "streaming" ],
6
6
  "homepage": "https://hx-stream.ajanibilby.com",
package/server.d.ts CHANGED
@@ -12,6 +12,7 @@ type HasRender<O> = O extends {
12
12
  } ? true : false;
13
13
  export declare class StreamResponse<JsxEnabled extends boolean> {
14
14
  #private;
15
+ readonly render?: (jsx: JSX.Element) => string;
15
16
  readonly response: Response;
16
17
  get readyState(): number;
17
18
  static CONNECTING: number;
package/server.js CHANGED
@@ -19,7 +19,7 @@ export class StreamResponse {
19
19
  #timer;
20
20
  #state;
21
21
  #boundary;
22
- #render;
22
+ render;
23
23
  response;
24
24
  get readyState() { return this.#state; }
25
25
  static CONNECTING = 0;
@@ -28,7 +28,7 @@ export class StreamResponse {
28
28
  constructor(options) {
29
29
  this.#controller = null;
30
30
  this.#state = StreamResponse.CONNECTING;
31
- this.#render = options.render;
31
+ this.render = options.render;
32
32
  this.#boundary = MakeBoundary();
33
33
  // immediate prepare for abortion
34
34
  this.#signal = options.abortSignal;
@@ -70,10 +70,10 @@ export class StreamResponse {
70
70
  }
71
71
  keepAlive() { return this.sendText(" "); }
72
72
  send(target, swap, html) {
73
- if (typeof html !== "string") {
74
- if (!this.#render)
73
+ if (typeof html !== 'string') {
74
+ if (!this.render)
75
75
  throw new Error(`Cannot render to JSX when no renderer provided during class initialization`);
76
- html = this.#render(html);
76
+ html = this.render(html);
77
77
  }
78
78
  return this.sendText(`<${this.#boundary}>${target}|${swap}|${html}</${this.#boundary}>\n`);
79
79
  }