UNPKG

@spider-cloud/spider-client

Version:

Isomorphic Javascript SDK for Spider Cloud services

26 lines (25 loc) 924 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.streamReader = void 0; const process_chunk_1 = require("./process-chunk"); // Stream the response via callbacks. const streamReader = async (res, cb) => { var _a; if (res.ok) { const reader = (_a = res.body) === null || _a === void 0 ? void 0 : _a.getReader(); const decoder = new TextDecoder(); const processChunk = (0, process_chunk_1.createJsonLineProcessor)(cb); if (reader) { while (true) { const { done, value } = await reader.read(); if (done) { break; } const chunk = decoder.decode(value, { stream: true }); processChunk(chunk); } processChunk(decoder.decode(new Uint8Array(), { stream: false })); } } }; exports.streamReader = streamReader;