UNPKG

@vulppi/toolbelt

Version:

A collection of tools and utilities.

2 lines (1 loc) 776 B
import{deflate as l,inflate as f}from"pako";function g(r){return l(JSON.stringify(r))}function h(r){return JSON.parse(f(r,{to:"string"}).toString())}async function*p(r){if(!r.body)return;let a=parseInt(r.headers.get("content-length")||"0",10),c=new TextDecoder,s=0,n=r.body;async function*d(e){let t=e.getReader();try{for(;;){let{done:o,value:i}=await t.read();if(o)break;i&&(yield i)}}finally{t.releaseLock()}}let y=n[Symbol.asyncIterator]?n[Symbol.asyncIterator]():d(n);try{for await(let e of y){let t=c.decode(e,{stream:!0});s+=e.byteLength;let o=a?s/a:-1;yield{chunk:t,progress:o}}}catch(e){throw console.error("Error reading stream:",e),e}finally{let e=c.decode();e&&(yield{chunk:e,progress:1})}}export{h as deserializeObject,p as progressResponse,g as serializeObject};