@hyperttp/core 3.0.5 → 3.0.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperttp/core",
3
- "version": "3.0.5",
3
+ "version": "3.0.6",
4
4
  "description": "Protocol-agnostic client/server execution core with pluggable protocols, transports and client policies",
5
5
  "keywords": [
6
6
  "browser",
@@ -1,2 +1,2 @@
1
- "use strict";import{TimeoutError as b}from"../../utils/errors.js";import{applyTimeout as m}from"../../utils/abort.js";import{DEFAULT_STATUS_TEXTS as g,getHeaderValue as w}from"./utils.js";const f="rest:stream",p=new TextEncoder,A=new TextDecoder,h=new WeakMap;function S(l){if(l===null||typeof l!="object")return!1;const t=Object.getPrototypeOf(l);return t===null||t===Object.prototype}async function E(l){if(l instanceof Uint8Array)return l;if(l instanceof ArrayBuffer)return new Uint8Array(l);if(ArrayBuffer.isView(l))return new Uint8Array(l.buffer,l.byteOffset,l.byteLength).slice();if(l&&typeof l=="object"&&"arrayBuffer"in l){const t=l.arrayBuffer;if(typeof t=="function")return new Uint8Array(await t.call(l))}if(typeof ReadableStream<"u"&&l instanceof ReadableStream){const t=l.getReader(),o=[];let e=0;try{for(;;){const{done:i,value:s}=await t.read();if(i)break;const a=s instanceof Uint8Array?s:new Uint8Array(s);o.push(a),e+=a.byteLength}}finally{t.releaseLock()}const r=new Uint8Array(e);let n=0;for(const i of o)r.set(i,n),n+=i.byteLength;return r}return l}function T(l,t,o){const e=l.getReader();let r=!1,n;const i=()=>{r||(r=!0,t?.removeEventListener("abort",a),e.releaseLock(),o())},s=async c=>{try{await e.cancel(c)}finally{i()}},a=()=>{n.error(t?.reason),s(t?.reason).catch(()=>{})};return new ReadableStream({start(c){n=c,t?.addEventListener("abort",a,{once:!0}),t?.aborted&&a()},async pull(){try{const c=await e.read();if(r)return;c.done?(n.close(),i()):n.enqueue(c.value)}catch(c){r||(n.error(c),i())}},cancel:s})}function U(l,t,o){const e=l[Symbol.asyncIterator]();let r=!1,n=!1;const i=()=>{r||(r=!0,t?.removeEventListener("abort",s),o())},s=()=>{n=!0,i(),(async()=>{try{l.destroy?.()}finally{await e.return?.()}})().catch(()=>{})};return t?.addEventListener("abort",s,{once:!0}),t?.aborted&&s(),{[Symbol.asyncIterator](){return this},async next(){if(n)throw t?.reason;if(r)return{done:!0,value:void 0};let a;const c=new Promise((d,u)=>{a=()=>u(t?.reason),t?.addEventListener("abort",a,{once:!0})});try{const d=await Promise.race([e.next(),c]);if(n)throw t?.reason;return d.done&&i(),d}catch(d){throw i(),d}finally{t?.removeEventListener("abort",a)}},async return(a){try{return await e.return?.(a)??{done:!0,value:a}}finally{i()}},async throw(a){try{if(e.throw)return await e.throw(a);throw await e.return?.(),a}finally{i()}}}}export class RestSender{protocol="rest";methods={get:(t,o,e,r)=>this.dispatch(t,{method:"GET",url:o,...e,signal:r??e?.signal}),post:(t,o,e,r,n)=>this.dispatch(t,{method:"POST",url:o,...r,body:e,signal:n??r?.signal}),put:(t,o,e,r,n)=>this.dispatch(t,{method:"PUT",url:o,...r,body:e,signal:n??r?.signal}),patch:(t,o,e,r,n)=>this.dispatch(t,{method:"PATCH",url:o,...r,body:e,signal:n??r?.signal}),delete:(t,o,e,r)=>this.dispatch(t,{method:"DELETE",url:o,...e,signal:r??e?.signal}),head:(t,o,e,r)=>this.dispatch(t,{method:"HEAD",url:o,...e,signal:r??e?.signal}),options:(t,o,e,r)=>this.dispatch(t,{method:"OPTIONS",url:o,...e,signal:r??e?.signal}),query:(t,o,e,r,n)=>this.dispatch(t,{method:"QUERY",url:o,...r,body:e,signal:n??r?.signal}),request:(t,o,e,r,n)=>this.dispatch(t,{method:o,url:e,...r,signal:n??r?.signal}),stream:(t,o,e,r)=>this.dispatch(t,{method:"GET",url:o,...e,stream:!0,signal:r??e?.signal})};prepare(t,o){const{input:e}=t,r=t.signal??e.signal??o.signal,n=e.timeout;let i,s=r;if(n!=null&&n>0){const a={};s=m(r,n,a,()=>new b(e.url,n)),i=a.cleanupSignal}try{const a={};if(e.headers)if(e.headers instanceof Headers)e.headers.forEach((u,y)=>{a[y.toLowerCase()]=u});else if(Array.isArray(e.headers))for(const[u,y]of e.headers)a[u.toLowerCase()]=y;else for(const[u,y]of Object.entries(e.headers))y!=null&&(a[u.toLowerCase()]=String(y));let c=e.body;c!=null&&(S(c)||Array.isArray(c))?(c=p.encode(JSON.stringify(c)),a["content-type"]||(a["content-type"]="application/json; charset=utf-8")):typeof c=="string"&&(c=p.encode(c));const d={method:e.method.toUpperCase(),url:this.buildUrl(e.url,e.query),headers:a,body:c,signal:s,protocol:this.protocol,stream:e.stream,stealth:e.stealth,followRedirects:e.followRedirects,maxRedirects:e.maxRedirects};return i&&h.set(d,i),d}catch(a){throw i?.(),a}}async send(t,o,e){const r=t,n=()=>{const s=h.get(t);h.delete(t),s?.()};let i=!1;try{const s=await o.execute(t);if(r.stream){if(typeof ReadableStream<"u"&&s.body instanceof ReadableStream){const c=T(s.body,t.signal,n);return i=!0,{...s,body:c,[f]:!0}}if(s.body!=null&&typeof s.body=="object"&&typeof s.body[Symbol.asyncIterator]=="function"){const c=U(s.body,t.signal,n);return i=!0,{...s,body:c,[f]:!0}}return{...s,[f]:!0}}const a=await E(s.body);return a===s.body?s:{...s,body:a}}finally{i||n()}}parse(t,o){const e=t.status>=200&&t.status<300;let r=t.body;if(!t[f]&&t.body instanceof Uint8Array){const n=w(t.headers,"content-type")??"",i=A.decode(t.body);if(n.includes("application/json"))try{r=JSON.parse(i)}catch{r=i}else(n.includes("text/")||n.includes("application/xml"))&&(r=i)}return{protocol:this.protocol,ok:e,status:t.status,statusText:t.statusText||g[t.status]||"",headers:t.headers,url:t.url,data:r,raw:t}}dispatch(t,o){return t.send({protocol:"rest",input:o,signal:o.signal})}buildUrl(t,o){if(!o)return t;const e=[];for(const[s,a]of Object.entries(o)){if(a==null)continue;const c=Array.isArray(a)?a:[a];for(const d of c)e.push(`${encodeURIComponent(s)}=${encodeURIComponent(String(d))}`)}if(e.length===0)return t;const r=e.join("&"),n=t.indexOf("#");if(n!==-1){const s=t.slice(0,n),a=t.slice(n),c=s.includes("?")?"&":"?";return`${s}${c}${r}${a}`}const i=t.includes("?")?"&":"?";return`${t}${i}${r}`}}
1
+ "use strict";import{TimeoutError as b}from"../../utils/errors.js";import{applyTimeout as m}from"../../utils/abort.js";import{DEFAULT_STATUS_TEXTS as g,getHeaderValue as w}from"./utils.js";const f="rest:stream",p=new TextEncoder,A=new TextDecoder,h=new WeakMap;function S(l){if(l===null||typeof l!="object")return!1;const e=Object.getPrototypeOf(l);return e===null||e===Object.prototype}async function E(l){if(l instanceof Uint8Array)return l;if(l instanceof ArrayBuffer)return new Uint8Array(l);if(ArrayBuffer.isView(l))return new Uint8Array(l.buffer,l.byteOffset,l.byteLength).slice();if(l&&typeof l=="object"&&"arrayBuffer"in l){const e=l.arrayBuffer;if(typeof e=="function")return new Uint8Array(await e.call(l))}if(typeof ReadableStream<"u"&&l instanceof ReadableStream){const e=l.getReader(),o=[];let t=0;try{for(;;){const{done:i,value:s}=await e.read();if(i)break;const a=s instanceof Uint8Array?s:new Uint8Array(s);o.push(a),t+=a.byteLength}}finally{e.releaseLock()}const r=new Uint8Array(t);let n=0;for(const i of o)r.set(i,n),n+=i.byteLength;return r}return l}function T(l,e,o){const t=l.getReader();let r=!1,n;const i=()=>{r||(r=!0,e?.removeEventListener("abort",a),t.releaseLock(),o())},s=async c=>{try{await t.cancel(c)}finally{i()}},a=()=>{n.error(e?.reason),s(e?.reason).catch(()=>{})};return new ReadableStream({start(c){n=c,e?.addEventListener("abort",a,{once:!0}),e?.aborted&&a()},async pull(){try{const c=await t.read();if(r)return;c.done?(n.close(),i()):n.enqueue(c.value)}catch(c){r||(n.error(c),i())}},cancel:s})}function U(l,e,o){const t=l[Symbol.asyncIterator]();let r=!1,n=!1;const i=()=>{r||(r=!0,e?.removeEventListener("abort",s),o())},s=()=>{n=!0,i(),(async()=>{try{l.destroy?.()}finally{await t.return?.()}})().catch(()=>{})};return e?.addEventListener("abort",s,{once:!0}),e?.aborted&&s(),{[Symbol.asyncIterator](){return this},async next(){if(n)throw e?.reason;if(r)return{done:!0,value:void 0};let a;const c=new Promise((d,u)=>{a=()=>u(e?.reason),e?.addEventListener("abort",a,{once:!0})});try{const d=await Promise.race([t.next(),c]);if(n)throw e?.reason;return d.done&&i(),d}catch(d){throw i(),d}finally{e?.removeEventListener("abort",a)}},async return(a){try{return await t.return?.(a)??{done:!0,value:a}}finally{i()}},async throw(a){try{if(t.throw)return await t.throw(a);throw await t.return?.(),a}finally{i()}}}}export class RestSender{protocol="rest";methods={get:(e,o,t,r)=>this.dispatch(e,{method:"GET",url:o,...t,signal:r??t?.signal}),post:(e,o,t,r,n)=>this.dispatch(e,{method:"POST",url:o,...r,body:t,signal:n??r?.signal}),put:(e,o,t,r,n)=>this.dispatch(e,{method:"PUT",url:o,...r,body:t,signal:n??r?.signal}),patch:(e,o,t,r,n)=>this.dispatch(e,{method:"PATCH",url:o,...r,body:t,signal:n??r?.signal}),delete:(e,o,t,r)=>this.dispatch(e,{method:"DELETE",url:o,...t,signal:r??t?.signal}),head:(e,o,t,r)=>this.dispatch(e,{method:"HEAD",url:o,...t,signal:r??t?.signal}),options:(e,o,t,r)=>this.dispatch(e,{method:"OPTIONS",url:o,...t,signal:r??t?.signal}),query:(e,o,t,r,n)=>this.dispatch(e,{method:"QUERY",url:o,...r,body:t,signal:n??r?.signal}),request:(e,o,t,r,n)=>this.dispatch(e,{method:o,url:t,...r,signal:n??r?.signal}),stream:(e,o,t,r)=>this.dispatch(e,{method:"GET",url:o,...t,stream:!0,signal:r??t?.signal})};prepare(e,o){const{input:t}=e,r=e.signal??t.signal??o.signal,n=t.timeout;let i,s=r;if(n!=null&&n>0){const a={};s=m(r,n,a,()=>new b(t.url,n)),i=a.cleanupSignal}try{const a={};if(t.headers)if(t.headers instanceof Headers)t.headers.forEach((u,y)=>{a[y.toLowerCase()]=u});else if(Array.isArray(t.headers))for(const[u,y]of t.headers)a[u.toLowerCase()]=y;else for(const[u,y]of Object.entries(t.headers))y!=null&&(a[u.toLowerCase()]=String(y));let c=t.body;c!=null&&(S(c)||Array.isArray(c))?(c=p.encode(JSON.stringify(c)),a["content-type"]||(a["content-type"]="application/json; charset=utf-8")):typeof c=="string"&&(c=p.encode(c));const d={method:t.method.toUpperCase(),url:this.buildUrl(t.url,t.query),headers:a,body:c,signal:s,protocol:this.protocol,stream:t.stream,stealth:t.stealth,followRedirects:t.followRedirects,maxRedirects:t.maxRedirects};return i&&h.set(d,i),d}catch(a){throw i?.(),a}}async send(e,o,t){const r=e,n=()=>{const s=h.get(e);h.delete(e),s?.()};let i=!1;try{const s=await o.execute(e);if(r.stream){if(typeof ReadableStream<"u"&&s.body instanceof ReadableStream){const c=T(s.body,e.signal,n);return i=!0,{...s,body:c,[f]:!0}}if(s.body!=null&&typeof s.body=="object"&&typeof s.body[Symbol.asyncIterator]=="function"){const c=U(s.body,e.signal,n);return i=!0,{...s,body:c,[f]:!0}}return{...s,[f]:!0}}const a=await E(s.body);return a===s.body?s:{...s,body:a}}finally{i||n()}}parse(e,o){const t=e.status>=200&&e.status<300;let r=e.body;if(!e[f]&&e.body instanceof Uint8Array){const n=w(e.headers,"content-type")??"",i=A.decode(e.body);if(n.includes("application/json"))try{r=JSON.parse(i)}catch{r=i}else(n.includes("text/")||n.includes("application/xml"))&&(r=i)}return{protocol:this.protocol,ok:t,status:e.status,statusText:e.statusText||g[e.status]||"",headers:e.headers,url:e.url,data:r,raw:e}}dispatch(e,o){return e.send({protocol:"rest",input:o,signal:o.signal})}buildUrl(e,o){if(!o)return e;const t=[];for(const[s,a]of Object.entries(o)){if(a==null)continue;const c=Array.isArray(a)?a:[a];for(const d of c)t.push(`${encodeURIComponent(s)}=${encodeURIComponent(String(d))}`)}if(t.length===0)return e;const r=t.join("&"),n=e.indexOf("#");if(n!==-1){const s=e.slice(0,n),a=e.slice(n),c=s.includes("?")?"&":"?";return`${s}${c}${r}${a}`}const i=e.includes("?")?"&":"?";return`${e}${i}${r}`}}
2
2
  //# sourceMappingURL=sender.js.map