UNPKG

ziggurat.js

Version:

a tiny modular js library for templates and other shorthands

4 lines (3 loc) 771 B
zg.stream={},zg.HttpException=class HttpException extends Error{constructor(e){super(`Recieved code ${e} from server`),this.status=e}},zg.stream.text=async function*(e,t){if(n=await fetch(e,t),!n.ok)throw new zg.HttpException(n.statusCode);for(var n,s,o,a=n.body.getReader(),i=new TextDecoder;!0;)if(o=await a.read(),s=i.decode(o.value),s.length>0&&(yield s),o.done)return},zg.stream.lines=async function*(e,t){var n,s,o,a,r,c,i="",l=zg.stream.text(e,t);for await(r of l)i+=r,n=i.split(` `),n.length>1&&(yield n[0],n=n.slice(1),i=n.join(` `));o=[];for(s=0,c=n.length;s<c;s++)a=n[s],a.length>0?o.push(yield a):o.push(void 0);return o},zg.stream.jsonl=async function*(e,t){var n,o=zg.stream.lines(e,t),s=[];for await(n of o)try{s.push(yield JSON.parse(n))}catch{}return s}