UNPKG

robert-util

Version:
45 lines (44 loc) 1.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toJSON = exports.toString = exports.toBlob = exports.toArrayBuffer = exports.toBuffer = exports.toBuffers = void 0; async function toBuffers(res, maxSize) { return new Promise((resolve, reject) => { let buffers = []; let size = 0; res.on("data", data => { size += Buffer.byteLength(data); if (size > maxSize) reject(new Error("Body over maximum size: " + size)); else buffers.push(data); }); res.on("error", error => reject(error)); res.on("end", () => resolve(buffers)); }); } exports.toBuffers = toBuffers; async function toBuffer(res, maxSize) { const buffers = await toBuffers(res, maxSize); return Buffer.concat(buffers); } exports.toBuffer = toBuffer; async function toArrayBuffer(res, maxSize) { const buffer = await toBuffer(res, maxSize); return buffer.slice(buffer.byteLength, buffer.byteOffset + buffer.byteLength); } exports.toArrayBuffer = toArrayBuffer; async function toBlob(res, maxSize) { const buffers = await toBuffers(res, maxSize); return new Blob(buffers, { type: res.headers["content-type"] }); } exports.toBlob = toBlob; async function toString(res, maxSize) { const buffer = await toBuffer(res, maxSize); return buffer.toString(); } exports.toString = toString; async function toJSON(res, maxSize) { const string = await toString(res, maxSize); return JSON.parse(string); } exports.toJSON = toJSON;