var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.js var src_exports = {}; __export(src_exports, { Merge: () => Merge, Split: () => Split }); module.exports = __toCommonJS(src_exports); // src/split.js var import_varint = __toESM(require("varint"), 1); var import_streamx = require("streamx"); function idGenerator() { const limit = 1e4; let timestamp = 0; let offset = 0; function reset() { timestamp = Date.now(); offset = 0; } reset(); return function generate() { if (offset > limit) { reset(); } return { timestamp, offset: offset++ }; }; } var idGenerate = idGenerator(); function encodePacket(data, id, index, size) { let offsetEncoder = 0; const buf = new Uint8Array( import_varint.default.encodingLength(id.timestamp) + import_varint.default.encodingLength(id.offset) + import_varint.default.encodingLength(size) + import_varint.default.encodingLength(index) + data.byteLength ); import_varint.default.encode(id.timestamp, buf, offsetEncoder); offsetEncoder += import_varint.default.encode.bytes; import_varint.default.encode(id.offset, buf, offsetEncoder); offsetEncoder += import_varint.default.encode.bytes; import_varint.default.encode(size, buf, offsetEncoder); offsetEncoder += import_varint.default.encode.bytes; import_varint.default.encode(index, buf, offsetEncoder); offsetEncoder += import_varint.default.encode.bytes; buf.set(data, offsetEncoder); return buf; } var toBuffer = typeof Buffer !== "undefined" ? (data) => Buffer.from(data) : null; var map; if (toBuffer) { map = (data) => data ? toBuffer(data) : data; } var Split = class extends import_streamx.Transform { constructor(opts = {}) { const { chunkSize = 1024, ...streamOpts } = opts; super({ map, ...streamOpts }); this._chunkSize = chunkSize; } _transform(data, cb) { let buf; const id = idGenerate(); if (data.length <= this._chunkSize) { buf = encodePacket(data, id, 0, 1); this.push(buf); } else { let offset = 0; let end = 0; let index = 0; const size = Math.ceil(data.length / this._chunkSize); while (offset < data.length) { end = offset + this._chunkSize; if (end > data.length) { end = data.length; } buf = encodePacket(data.slice(offset, end), id, index++, size); offset = end; this.push(buf); } } cb(null); } }; // src/merge.js var import_varint2 = __toESM(require("varint"), 1); var import_streamx2 = require("streamx"); function concat(packet) { const buf = new Uint8Array(packet.byteLength); let offset = 0; packet.chunks.forEach((chunk) => { buf.set(chunk.data, offset); offset += chunk.data.length; }); return buf; } var sort = (a, b) => a.index - b.index; var toBuffer2 = typeof Buffer !== "undefined" ? (data) => Buffer.from(data) : null; var map2; if (toBuffer2) { map2 = (data) => data ? toBuffer2(data) : data; } var Merge = class extends import_streamx2.Transform { constructor(opts = {}) { const { timeout = 5e3, ...streamOpts } = opts; super({ map: map2, ...streamOpts }); this._timeout = timeout; this._packets = /* @__PURE__ */ new Map(); } _open(cb) { if (this._timeout) { this._timestamp = Date.now(); this._interval = setInterval(() => { this._packets.forEach((packet) => { if (Math.abs(packet.timestamp - this._timestamp) > this._timeout) { this._packets.delete(packet.id); } }); this._timestamp = Date.now(); }, this._timeout); } cb(null); } _destroy(cb) { this._interval && clearInterval(this._interval); cb(null); } _transform(data, cb) { try { const buf = this._decodePacket(data); if (buf) this.push(buf); cb(null); } catch (err) { cb(err); } } _decodePacket(data) { let offsetDecoder = 0; const timestamp = import_varint2.default.decode(data, offsetDecoder); offsetDecoder += import_varint2.default.decode.bytes; const offset = import_varint2.default.decode(data, offsetDecoder); offsetDecoder += import_varint2.default.decode.bytes; const size = import_varint2.default.decode(data, offsetDecoder); offsetDecoder += import_varint2.default.decode.bytes; const index = import_varint2.default.decode(data, offsetDecoder); offsetDecoder += import_varint2.default.decode.bytes; data = data.slice(offsetDecoder); const id = timestamp + "/" + offset; let packet = this._packets.get(id); if (!packet) { packet = { id, chunks: [], size, byteLength: 0, timestamp: Date.now() }; this._packets.set(id, packet); } const chunk = { index, data }; packet.chunks.push(chunk); packet.byteLength += chunk.data.byteLength; if (packet.chunks.length === packet.size) { packet.chunks.sort(sort); this._packets.delete(id); return concat(packet); } return null; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Merge, Split });