UNPKG

diffusion

Version:

Diffusion JavaScript client

272 lines (271 loc) 9.13 kB
"use strict"; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from) { for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) to[j] = from[i]; return to; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isUint8Array = exports.isEqualUint8Arrays = exports.uint8toUtf8 = exports.uint8ToBase64 = exports.uint8ToAscii = exports.uint8FromDoubleBE = exports.uint8FromBase64 = exports.uint8FromUtf8 = exports.uint8FromBinary = exports.writeInt32BE = exports.writeInt8 = exports.readDoubleBE = exports.readFloatBE = exports.readInt32BE = exports.readUInt32BE = exports.readUInt16BE = exports.readInt8 = exports.concatUint8Arrays = void 0; /** * Concatenates multiple Uint8Arrays into a single Uint8Array. * * @param arrays The arrays to concatenate * @returns The concatenated array */ function concatUint8Arrays(arrays) { var e_1, _a; var totalLength = arrays.reduce(function (acc, a) { return acc + a.length; }, 0); var result = new Uint8Array(totalLength); var offset = 0; try { for (var arrays_1 = __values(arrays), arrays_1_1 = arrays_1.next(); !arrays_1_1.done; arrays_1_1 = arrays_1.next()) { var a = arrays_1_1.value; result.set(a, offset); offset += a.length; } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (arrays_1_1 && !arrays_1_1.done && (_a = arrays_1.return)) _a.call(arrays_1); } finally { if (e_1) throw e_1.error; } } return result; } exports.concatUint8Arrays = concatUint8Arrays; /** * Reads an 8-bit integer from a Uint8Array. * * @param buffer The buffer to read from * @param offset The offset in the buffer to read from * @returns The read integer */ function readInt8(buffer, offset) { var view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); return view.getInt8(offset); } exports.readInt8 = readInt8; /** * Reads an unsigned 16-bit integer from a Uint8Array. * * @param buffer The buffer to read from * @param offset The offset in the buffer to read from * @returns The read integer */ function readUInt16BE(buffer, offset) { var view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); return view.getUint16(offset, false); } exports.readUInt16BE = readUInt16BE; /** * Reads an unsigned 32-bit integer from a Uint8Array. * * @param buffer The buffer to read from * @param offset The offset in the buffer to read from * @returns The read integer */ function readUInt32BE(buffer, offset) { var view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); return view.getUint32(offset, false); } exports.readUInt32BE = readUInt32BE; /** * Reads a signed 32-bit integer from a Uint8Array. * * @param buffer The buffer to read from * @param offset The offset in the buffer to read from * @returns The read integer */ function readInt32BE(buffer, offset) { var view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); return view.getInt32(offset, false); } exports.readInt32BE = readInt32BE; /** * Reads a 32-bit float from a Uint8Array. * * @param buffer The buffer to read from * @param offset The offset in the buffer to read from * @returns The read number */ function readFloatBE(buffer, offset) { var view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); return view.getFloat32(offset, false); } exports.readFloatBE = readFloatBE; /** * Reads a 64-bit double from a Uint8Array. * * @param buffer The buffer to read from * @param offset The offset in the buffer to read from * @returns The read number */ function readDoubleBE(buffer, offset) { var view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); return view.getFloat64(offset, false); } exports.readDoubleBE = readDoubleBE; /** * Writes an unsigned 8-bit integer to a Uint8Array. * * @param buffer The buffer to write to * @param offset The offset in the buffer to write to * @param value The value to write */ function writeInt8(buffer, offset, value) { buffer[offset] = value; } exports.writeInt8 = writeInt8; /** * Writes a signed 32-bit integer to a Uint8Array. * * @param buffer The buffer to write to * @param offset The offset in the buffer to write to * @param value The value to write */ function writeInt32BE(buffer, offset, value) { var view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); view.setInt32(offset, value, false); } exports.writeInt32BE = writeInt32BE; /** * Creates a Uint8Array from a binary string. * * @param str The binary string * @returns A new Uint8Array */ function uint8FromBinary(str) { var buffer = new Uint8Array(str.length); for (var i = 0; i < str.length; i++) { buffer[i] = str.charCodeAt(i); } return buffer; } exports.uint8FromBinary = uint8FromBinary; /** * Creates a Uint8Array from a utf-8 string. * * @param str The utf-8 string * @returns A new Uint8Array */ function uint8FromUtf8(str) { return new TextEncoder().encode(str); } exports.uint8FromUtf8 = uint8FromUtf8; /** * Creates a Uint8Array from a base64 string. * * @param str The base64 string * @returns A new Uint8Array */ // eslint-disable-next-line deprecation/deprecation exports.uint8FromBase64 = (typeof window === 'undefined' || window.atob === undefined) ? function (str) { return new Uint8Array(Buffer.from(str, 'base64')); } : // eslint-disable-next-line deprecation/deprecation function (str) { return new Uint8Array(window.atob(str).split('').map(function (c) { return c.charCodeAt(0); })); }; /** * Creates a Uint8Array from a 64-bit double. * * @param val The double value * @returns A new Uint8Array */ function uint8FromDoubleBE(val) { var buffer = new ArrayBuffer(8); new DataView(buffer).setFloat64(0, val, false); return new Uint8Array(buffer); } exports.uint8FromDoubleBE = uint8FromDoubleBE; /** * Converts a Uint8Array to a binary string. * * @param val The Uint8Array to convert * @returns A binary string */ function uint8ToAscii(val) { // split val into chunks of 128 bytes to avoid stack overflow var chunks = []; for (var i = 0; i < val.length; i += 128) { chunks.push(val.subarray(i, Math.min(i + 128, val.length))); } return chunks.map(function (chunk) { return String.fromCharCode.apply(String, __spreadArray([], __read(chunk))); }).join(''); } exports.uint8ToAscii = uint8ToAscii; /** * Converts a Uint8Array to a base64 string. * * @param val The Uint8Array to convert * @returns A base64 string */ // eslint-disable-next-line deprecation/deprecation exports.uint8ToBase64 = (typeof window === 'undefined' || window.btoa === undefined) ? function (val) { return Buffer.from(val).toString('base64'); } : // eslint-disable-next-line deprecation/deprecation function (val) { return window.btoa(uint8ToAscii(val)); }; /** * Converts a Uint8Array to a utf-8 string. * * @param val The Uint8Array to convert * @returns A utf-8 string */ function uint8toUtf8(val, startPos, endPos) { if (startPos === void 0) { startPos = 0; } if (endPos === void 0) { endPos = val.length; } return new TextDecoder().decode(val.subarray(startPos, endPos)); } exports.uint8toUtf8 = uint8toUtf8; /** * Compares two Uint8Arrays for equality. * * @param a The first Uint8Array * @param b The second Uint8Array * @returns True if the arrays are equal, false otherwise */ function isEqualUint8Arrays(a, b) { if (a.length !== b.length) { return false; } for (var i = 0; i < a.length; ++i) { if (a[i] !== b[i]) { return false; } } return true; } exports.isEqualUint8Arrays = isEqualUint8Arrays; /** * Tests if a value is a Uint8Array. * * @param value - value to test * @returns whether value is a Uint8Array */ function isUint8Array(value) { return (typeof Uint8Array === 'function' && value instanceof Uint8Array) || Object.prototype.toString.call(value) === '[object Uint8Array]'; } exports.isUint8Array = isUint8Array;