UNPKG

ziggurat.js

Version:

a tiny modular js library for templates and other shorthands

72 lines (66 loc) 1.62 kB
// Generated by CoffeeScript 2.7.0 zg.stream = {}; zg.HttpException = class HttpException extends Error { constructor(status) { super(`Recieved code ${status} from server`); this.status = status; } }; // chunked fetch // yields all data available zg.stream.text = async function*(url, options) { var data, decoder, reader, res, result; res = (await fetch(url, options)); if (!res.ok) { throw new zg.HttpException(res.statusCode); } reader = res.body.getReader(); decoder = new TextDecoder(); while (true) { result = (await reader.read()); data = decoder.decode(result.value); if (data.length > 0) { yield data; } if (result.done) { return; } } }; // yields all lines as raw text zg.stream.lines = async function*(url, options) { var buffer, data, i, len, line, lines, ref, results; buffer = ""; ref = zg.stream.text(url, options); for await (data of ref) { buffer += data; lines = buffer.split("\n"); if (lines.length > 1) { yield lines[0]; lines = lines.slice(1); buffer = lines.join("\n"); } } results = []; for (i = 0, len = lines.length; i < len; i++) { line = lines[i]; if (line.length > 0) { results.push((yield line)); } else { results.push(void 0); } } return results; }; // yields all lines as JSON zg.stream.jsonl = async function*(url, options) { var line, ref, results; ref = zg.stream.lines(url, options); results = []; for await (line of ref) { try { results.push((yield JSON.parse(line))); } catch (error) {} } return results; };