Files
bare-operating-system/kernel/lib/bare/bundles/protomux.js
T
Raven Scott 9bdecc4170
Release rolling / release (push) Successful in 11m16s
Sync Holepunch modules to current clone/npm latest
Bump published pins (compact-encoding 3, bare-fetch/tls/https/ws 3,
bare-subprocess 6, bare-signals 5, corestore 7.12, protomux 3.11,
hypercore-crypto 3.7, bare-runtime 1.31) and regenerate catalogs,
manifests, and kernel/seeder bundles.

Adapt call sites to the new APIs:
- Corestore: explicit session flush before suspend(); treeCache ctor opts
- bare-crypto: KeyObject.export() instead of removed ._key
- Protomux 3.11: wait for fullyOpened()/fullyClosed() on chat channels
- bare-fetch: surface response.type and Headers.getSetCookie
- host snapshots: bare-os 3.9 / bare-posix / bare-fs.statfs frsize
- bare-subprocess 6: optional IPC channel + json serialization

Keep catalog sync from wiping curated pearEntries. Teach the Node test
shim to stub bare-thread/bare-worker (ESM absolute paths) and chain
Bare.on so bare-timers can load. Booter 479, protocol 34, seeder 14.
2026-08-12 20:56:28 -04:00

2119 lines
72 KiB
JavaScript

var __bare_os_bundle_exports__ = (() => {
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 __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
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(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// ../../node_modules/b4a/index.js
var require_b4a = __commonJS({
"../../node_modules/b4a/index.js"(exports, module) {
function isBuffer(value) {
return Buffer.isBuffer(value) || value instanceof Uint8Array;
}
function isEncoding(encoding) {
return Buffer.isEncoding(encoding);
}
function alloc(size, fill2, encoding) {
return Buffer.alloc(size, fill2, encoding);
}
function allocUnsafe(size) {
return Buffer.allocUnsafe(size);
}
function allocUnsafeSlow(size) {
return Buffer.allocUnsafeSlow(size);
}
function byteLength(string, encoding) {
return Buffer.byteLength(string, encoding);
}
function compare(a, b) {
return Buffer.compare(a, b);
}
function concat(buffers, totalLength) {
return Buffer.concat(buffers, totalLength);
}
function copy(source, target, targetStart, start, end) {
return toBuffer(source).copy(target, targetStart, start, end);
}
function equals(a, b) {
return toBuffer(a).equals(b);
}
function fill(buffer, value, offset, end, encoding) {
return toBuffer(buffer).fill(value, offset, end, encoding);
}
function from(value, encodingOrOffset, length) {
return Buffer.from(value, encodingOrOffset, length);
}
function includes(buffer, value, byteOffset, encoding) {
return toBuffer(buffer).includes(value, byteOffset, encoding);
}
function indexOf(buffer, value, byfeOffset, encoding) {
return toBuffer(buffer).indexOf(value, byfeOffset, encoding);
}
function lastIndexOf(buffer, value, byteOffset, encoding) {
return toBuffer(buffer).lastIndexOf(value, byteOffset, encoding);
}
function swap16(buffer) {
return toBuffer(buffer).swap16();
}
function swap32(buffer) {
return toBuffer(buffer).swap32();
}
function swap64(buffer) {
return toBuffer(buffer).swap64();
}
function toBuffer(buffer) {
if (Buffer.isBuffer(buffer)) return buffer;
return Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength);
}
function toString(buffer, encoding, start, end) {
return toBuffer(buffer).toString(encoding, start, end);
}
function write(buffer, string, offset, length, encoding) {
return toBuffer(buffer).write(string, offset, length, encoding);
}
function readDoubleBE(buffer, offset) {
return toBuffer(buffer).readDoubleBE(offset);
}
function readDoubleLE(buffer, offset) {
return toBuffer(buffer).readDoubleLE(offset);
}
function readFloatBE(buffer, offset) {
return toBuffer(buffer).readFloatBE(offset);
}
function readFloatLE(buffer, offset) {
return toBuffer(buffer).readFloatLE(offset);
}
function readInt32BE(buffer, offset) {
return toBuffer(buffer).readInt32BE(offset);
}
function readInt32LE(buffer, offset) {
return toBuffer(buffer).readInt32LE(offset);
}
function readUInt32BE(buffer, offset) {
return toBuffer(buffer).readUInt32BE(offset);
}
function readUInt32LE(buffer, offset) {
return toBuffer(buffer).readUInt32LE(offset);
}
function writeDoubleBE(buffer, value, offset) {
return toBuffer(buffer).writeDoubleBE(value, offset);
}
function writeDoubleLE(buffer, value, offset) {
return toBuffer(buffer).writeDoubleLE(value, offset);
}
function writeFloatBE(buffer, value, offset) {
return toBuffer(buffer).writeFloatBE(value, offset);
}
function writeFloatLE(buffer, value, offset) {
return toBuffer(buffer).writeFloatLE(value, offset);
}
function writeInt32BE(buffer, value, offset) {
return toBuffer(buffer).writeInt32BE(value, offset);
}
function writeInt32LE(buffer, value, offset) {
return toBuffer(buffer).writeInt32LE(value, offset);
}
function writeUInt32BE(buffer, value, offset) {
return toBuffer(buffer).writeUInt32BE(value, offset);
}
function writeUInt32LE(buffer, value, offset) {
return toBuffer(buffer).writeUInt32LE(value, offset);
}
module.exports = {
isBuffer,
isEncoding,
alloc,
allocUnsafe,
allocUnsafeSlow,
byteLength,
compare,
concat,
copy,
equals,
fill,
from,
includes,
indexOf,
lastIndexOf,
swap16,
swap32,
swap64,
toBuffer,
toString,
write,
readDoubleBE,
readDoubleLE,
readFloatBE,
readFloatLE,
readInt32BE,
readInt32LE,
readUInt32BE,
readUInt32LE,
writeDoubleBE,
writeDoubleLE,
writeFloatBE,
writeFloatLE,
writeInt32BE,
writeInt32LE,
writeUInt32BE,
writeUInt32LE
};
}
});
// ../../node_modules/protomux/node_modules/compact-encoding/endian.js
var require_endian = __commonJS({
"../../node_modules/protomux/node_modules/compact-encoding/endian.js"(exports) {
var LE = exports.LE = new Uint8Array(new Uint16Array([255]).buffer)[0] === 255;
exports.BE = !LE;
}
});
// ../../node_modules/protomux/node_modules/compact-encoding/raw.js
var require_raw = __commonJS({
"../../node_modules/protomux/node_modules/compact-encoding/raw.js"(exports, module) {
var b4a = require_b4a();
var { BE } = require_endian();
exports = module.exports = {
preencode(state, b) {
state.end += b.byteLength;
},
encode(state, b) {
state.buffer.set(b, state.start);
state.start += b.byteLength;
},
decode(state) {
const b = state.buffer.subarray(state.start, state.end);
state.start = state.end;
return b;
}
};
var buffer = exports.buffer = {
preencode(state, b) {
uint8array.preencode(state, b);
},
encode(state, b) {
uint8array.encode(state, b);
},
decode(state) {
const b = state.buffer.subarray(state.start);
state.start = state.end;
return b;
}
};
exports.binary = {
...buffer,
preencode(state, b) {
if (typeof b === "string") utf8.preencode(state, b);
else buffer.preencode(state, b);
},
encode(state, b) {
if (typeof b === "string") utf8.encode(state, b);
else buffer.encode(state, b);
}
};
exports.arraybuffer = {
preencode(state, b) {
state.end += b.byteLength;
},
encode(state, b) {
const view = new Uint8Array(b);
state.buffer.set(view, state.start);
state.start += b.byteLength;
},
decode(state) {
const b = new ArrayBuffer(state.end - state.start);
const view = new Uint8Array(b);
view.set(state.buffer.subarray(state.start));
state.start = state.end;
return b;
}
};
function typedarray(TypedArray, swap) {
const n = TypedArray.BYTES_PER_ELEMENT;
return {
preencode(state, b) {
state.end += b.byteLength;
},
encode(state, b) {
const view = new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
if (BE && swap) swap(view);
state.buffer.set(view, state.start);
state.start += b.byteLength;
},
decode(state) {
let b = state.buffer.subarray(state.start);
if (b.byteOffset % n !== 0) b = new Uint8Array(b);
if (BE && swap) swap(b);
state.start = state.end;
return new TypedArray(b.buffer, b.byteOffset, b.byteLength / n);
}
};
}
var uint8array = exports.uint8array = typedarray(Uint8Array);
exports.uint16array = typedarray(Uint16Array, b4a.swap16);
exports.uint32array = typedarray(Uint32Array, b4a.swap32);
exports.int8array = typedarray(Int8Array);
exports.int16array = typedarray(Int16Array, b4a.swap16);
exports.int32array = typedarray(Int32Array, b4a.swap32);
exports.biguint64array = typedarray(BigUint64Array, b4a.swap64);
exports.bigint64array = typedarray(BigInt64Array, b4a.swap64);
exports.float32array = typedarray(Float32Array, b4a.swap32);
exports.float64array = typedarray(Float64Array, b4a.swap64);
function string(encoding) {
return {
preencode(state, s) {
state.end += b4a.byteLength(s, encoding);
},
encode(state, s) {
state.start += b4a.write(state.buffer, s, state.start, encoding);
},
decode(state) {
const s = b4a.toString(state.buffer, encoding, state.start);
state.start = state.end;
return s;
}
};
}
var utf8 = exports.string = exports.utf8 = string("utf-8");
exports.ascii = string("ascii");
exports.hex = string("hex");
exports.base64 = string("base64");
exports.ucs2 = exports.utf16le = string("utf16le");
exports.array = function array(enc) {
return {
preencode(state, list) {
for (const value of list) enc.preencode(state, value);
},
encode(state, list) {
for (const value of list) enc.encode(state, value);
},
decode(state) {
const arr = [];
while (state.start < state.end) arr.push(enc.decode(state));
return arr;
}
};
};
exports.json = {
preencode(state, v) {
utf8.preencode(state, JSON.stringify(v));
},
encode(state, v) {
utf8.encode(state, JSON.stringify(v));
},
decode(state) {
return JSON.parse(utf8.decode(state));
}
};
exports.ndjson = {
preencode(state, v) {
utf8.preencode(state, JSON.stringify(v) + "\n");
},
encode(state, v) {
utf8.encode(state, JSON.stringify(v) + "\n");
},
decode(state) {
return JSON.parse(utf8.decode(state));
}
};
}
});
// ../../node_modules/protomux/node_modules/compact-encoding/lexint.js
var require_lexint = __commonJS({
"../../node_modules/protomux/node_modules/compact-encoding/lexint.js"(exports, module) {
module.exports = {
preencode,
encode,
decode
};
function preencode(state, num) {
if (num < 251) {
state.end++;
} else if (num < 256) {
state.end += 2;
} else if (num < 65536) {
state.end += 3;
} else if (num < 16777216) {
state.end += 4;
} else if (num < 4294967296) {
state.end += 5;
} else {
state.end++;
const exp = Math.floor(Math.log(num) / Math.log(2)) - 32;
preencode(state, exp);
state.end += 6;
}
}
function encode(state, num) {
const max = 251;
const x = num - max;
if (num < max) {
state.buffer[state.start++] = num;
} else if (num < 256) {
state.buffer[state.start++] = max;
state.buffer[state.start++] = x;
} else if (num < 65536) {
state.buffer[state.start++] = max + 1;
state.buffer[state.start++] = x >> 8 & 255;
state.buffer[state.start++] = x & 255;
} else if (num < 16777216) {
state.buffer[state.start++] = max + 2;
state.buffer[state.start++] = x >> 16;
state.buffer[state.start++] = x >> 8 & 255;
state.buffer[state.start++] = x & 255;
} else if (num < 4294967296) {
state.buffer[state.start++] = max + 3;
state.buffer[state.start++] = x >> 24;
state.buffer[state.start++] = x >> 16 & 255;
state.buffer[state.start++] = x >> 8 & 255;
state.buffer[state.start++] = x & 255;
} else {
const exp = Math.floor(Math.log(x) / Math.log(2)) - 32;
state.buffer[state.start++] = 255;
encode(state, exp);
const rem = x / Math.pow(2, exp - 11);
for (let i = 5; i >= 0; i--) {
state.buffer[state.start++] = rem / Math.pow(2, 8 * i) & 255;
}
}
}
function decode(state) {
const max = 251;
if (state.end - state.start < 1) throw new Error("Out of bounds");
const flag = state.buffer[state.start++];
if (flag < max) return flag;
if (state.end - state.start < flag - max + 1) {
throw new Error("Out of bounds.");
}
if (flag < 252) {
return state.buffer[state.start++] + max;
}
if (flag < 253) {
return (state.buffer[state.start++] << 8) + state.buffer[state.start++] + max;
}
if (flag < 254) {
return (state.buffer[state.start++] << 16) + (state.buffer[state.start++] << 8) + state.buffer[state.start++] + max;
}
if (flag < 255) {
return state.buffer[state.start++] * 16777216 + (state.buffer[state.start++] << 16) + (state.buffer[state.start++] << 8) + state.buffer[state.start++] + max;
}
const exp = decode(state);
if (state.end - state.start < 6) throw new Error("Out of bounds");
let rem = 0;
for (let i = 5; i >= 0; i--) {
rem += state.buffer[state.start++] * Math.pow(2, 8 * i);
}
return rem * Math.pow(2, exp - 11) + max;
}
}
});
// ../../node_modules/protomux/node_modules/compact-encoding/index.js
var require_compact_encoding = __commonJS({
"../../node_modules/protomux/node_modules/compact-encoding/index.js"(exports) {
var b4a = require_b4a();
var { BE } = require_endian();
exports.state = function(start = 0, end = 0, buffer2 = null) {
return { start, end, buffer: buffer2 };
};
var raw = exports.raw = require_raw();
var uint = exports.uint = {
preencode(state, n) {
state.end += n <= 252 ? 1 : n <= 65535 ? 3 : n <= 4294967295 ? 5 : 9;
},
encode(state, n) {
if (n <= 252) uint8.encode(state, n);
else if (n <= 65535) {
state.buffer[state.start++] = 253;
uint16.encode(state, n);
} else if (n <= 4294967295) {
state.buffer[state.start++] = 254;
uint32.encode(state, n);
} else {
state.buffer[state.start++] = 255;
uint64.encode(state, n);
}
},
decode(state) {
const a = uint8.decode(state);
if (a <= 252) return a;
if (a === 253) return uint16.decode(state);
if (a === 254) return uint32.decode(state);
return uint64.decode(state);
}
};
var uint8 = exports.uint8 = {
preencode(state, n) {
state.end += 1;
},
encode(state, n) {
validateUint(n);
state.buffer[state.start++] = n;
},
decode(state) {
if (state.start >= state.end) throw new Error("Out of bounds");
return state.buffer[state.start++];
}
};
var uint16 = exports.uint16 = {
preencode(state, n) {
state.end += 2;
},
encode(state, n) {
validateUint(n);
state.buffer[state.start++] = n;
state.buffer[state.start++] = n >>> 8;
},
decode(state) {
if (state.end - state.start < 2) throw new Error("Out of bounds");
return state.buffer[state.start++] + state.buffer[state.start++] * 256;
}
};
var uint24 = exports.uint24 = {
preencode(state, n) {
state.end += 3;
},
encode(state, n) {
validateUint(n);
state.buffer[state.start++] = n;
state.buffer[state.start++] = n >>> 8;
state.buffer[state.start++] = n >>> 16;
},
decode(state) {
if (state.end - state.start < 3) throw new Error("Out of bounds");
return state.buffer[state.start++] + state.buffer[state.start++] * 256 + state.buffer[state.start++] * 65536;
}
};
var uint32 = exports.uint32 = {
preencode(state, n) {
state.end += 4;
},
encode(state, n) {
validateUint(n);
state.buffer[state.start++] = n;
state.buffer[state.start++] = n >>> 8;
state.buffer[state.start++] = n >>> 16;
state.buffer[state.start++] = n >>> 24;
},
decode(state) {
if (state.end - state.start < 4) throw new Error("Out of bounds");
return state.buffer[state.start++] + state.buffer[state.start++] * 256 + state.buffer[state.start++] * 65536 + state.buffer[state.start++] * 16777216;
}
};
var uint32be = exports.uint32be = {
preencode(state, n) {
state.end += 4;
},
encode(state, n) {
validateUint(n);
state.buffer[state.start++] = n >>> 24;
state.buffer[state.start++] = n >>> 16;
state.buffer[state.start++] = n >>> 8;
state.buffer[state.start++] = n;
},
decode(state) {
if (state.end - state.start < 4) throw new Error("Out of bounds");
return state.buffer[state.start++] * 16777216 + state.buffer[state.start++] * 65536 + state.buffer[state.start++] * 256 + state.buffer[state.start++];
}
};
var uint40 = exports.uint40 = {
preencode(state, n) {
state.end += 5;
},
encode(state, n) {
validateUint(n);
const r = Math.floor(n / 256);
uint8.encode(state, n);
uint32.encode(state, r);
},
decode(state) {
if (state.end - state.start < 5) throw new Error("Out of bounds");
return uint8.decode(state) + 256 * uint32.decode(state);
}
};
var uint48 = exports.uint48 = {
preencode(state, n) {
state.end += 6;
},
encode(state, n) {
validateUint(n);
const r = Math.floor(n / 65536);
uint16.encode(state, n);
uint32.encode(state, r);
},
decode(state) {
if (state.end - state.start < 6) throw new Error("Out of bounds");
return uint16.decode(state) + 65536 * uint32.decode(state);
}
};
var uint56 = exports.uint56 = {
preencode(state, n) {
state.end += 7;
},
encode(state, n) {
validateUint(n);
const r = Math.floor(n / 16777216);
uint24.encode(state, n);
uint32.encode(state, r);
},
decode(state) {
if (state.end - state.start < 7) throw new Error("Out of bounds");
return validateSafeUint(
uint24.decode(state) + 16777216 * uint32.decode(state)
);
}
};
var uint64 = exports.uint64 = {
preencode(state, n) {
state.end += 8;
},
encode(state, n) {
validateUint(n);
const r = Math.floor(n / 4294967296);
uint32.encode(state, n);
uint32.encode(state, r);
},
decode(state) {
if (state.end - state.start < 8) throw new Error("Out of bounds");
return validateSafeUint(
uint32.decode(state) + 4294967296 * uint32.decode(state)
);
}
};
exports.uint64be = {
preencode(state, n) {
state.end += 8;
},
encode(state, n) {
validateUint(n);
const r = Math.floor(n / 4294967296);
uint32be.encode(state, r);
uint32be.encode(state, n);
},
decode(state) {
if (state.end - state.start < 8) throw new Error("Out of bounds");
return validateSafeUint(
4294967296 * uint32be.decode(state) + uint32be.decode(state)
);
}
};
var int = exports.int = zigZagInt(uint);
exports.int8 = zigZagInt(uint8);
exports.int16 = zigZagInt(uint16);
exports.int24 = zigZagInt(uint24);
exports.int32 = zigZagInt(uint32);
exports.int40 = zigZagInt(uint40);
exports.int48 = zigZagInt(uint48);
exports.int56 = zigZagInt(uint56);
exports.int64 = zigZagInt(uint64);
var biguint64 = exports.biguint64 = {
preencode(state, n) {
state.end += 8;
},
encode(state, n) {
const view = new DataView(
state.buffer.buffer,
state.start + state.buffer.byteOffset,
8
);
view.setBigUint64(0, n, true);
state.start += 8;
},
decode(state) {
if (state.end - state.start < 8) throw new Error("Out of bounds");
const view = new DataView(
state.buffer.buffer,
state.start + state.buffer.byteOffset,
8
);
const n = view.getBigUint64(0, true);
state.start += 8;
return n;
}
};
exports.bigint64 = zigZagBigInt(biguint64);
var biguint = exports.biguint = {
preencode(state, n) {
let len = 0;
for (let m = n; m; m = m >> 64n) len++;
uint.preencode(state, len);
state.end += 8 * len;
},
encode(state, n) {
let len = 0;
for (let m = n; m; m = m >> 64n) len++;
uint.encode(state, len);
const view = new DataView(
state.buffer.buffer,
state.start + state.buffer.byteOffset,
8 * len
);
for (let m = n, i = 0; m; m = m >> 64n, i += 8) {
view.setBigUint64(i, BigInt.asUintN(64, m), true);
}
state.start += 8 * len;
},
decode(state) {
const len = uint.decode(state);
if (state.end - state.start < 8 * len) throw new Error("Out of bounds");
const view = new DataView(
state.buffer.buffer,
state.start + state.buffer.byteOffset,
8 * len
);
let n = 0n;
for (let i = len - 1; i >= 0; i--)
n = (n << 64n) + view.getBigUint64(i * 8, true);
state.start += 8 * len;
return n;
}
};
exports.bigint = zigZagBigInt(biguint);
exports.lexint = require_lexint();
exports.float32 = {
preencode(state, n) {
state.end += 4;
},
encode(state, n) {
const view = new DataView(
state.buffer.buffer,
state.start + state.buffer.byteOffset,
4
);
view.setFloat32(0, n, true);
state.start += 4;
},
decode(state) {
if (state.end - state.start < 4) throw new Error("Out of bounds");
const view = new DataView(
state.buffer.buffer,
state.start + state.buffer.byteOffset,
4
);
const float = view.getFloat32(0, true);
state.start += 4;
return float;
}
};
exports.float64 = {
preencode(state, n) {
state.end += 8;
},
encode(state, n) {
const view = new DataView(
state.buffer.buffer,
state.start + state.buffer.byteOffset,
8
);
view.setFloat64(0, n, true);
state.start += 8;
},
decode(state) {
if (state.end - state.start < 8) throw new Error("Out of bounds");
const view = new DataView(
state.buffer.buffer,
state.start + state.buffer.byteOffset,
8
);
const float = view.getFloat64(0, true);
state.start += 8;
return float;
}
};
var buffer = exports.buffer = {
preencode(state, b) {
uint8array.preencode(state, b);
},
encode(state, b) {
uint8array.encode(state, b);
},
decode(state) {
const len = uint.decode(state);
if (state.end - state.start < len) throw new Error("Out of bounds");
return state.buffer.subarray(state.start, state.start += len);
}
};
exports.optionalBuffer = {
preencode(state, b) {
if (b) uint8array.preencode(state, b);
else state.end++;
},
encode(state, b) {
if (b) uint8array.encode(state, b);
else state.buffer[state.start++] = 0;
},
decode(state) {
const len = uint.decode(state);
if (len === 0) return null;
if (state.end - state.start < len) throw new Error("Out of bounds");
return state.buffer.subarray(state.start, state.start += len);
}
};
exports.binary = {
...buffer,
preencode(state, b) {
if (typeof b === "string") utf8.preencode(state, b);
else buffer.preencode(state, b);
},
encode(state, b) {
if (typeof b === "string") utf8.encode(state, b);
else buffer.encode(state, b);
}
};
exports.arraybuffer = {
preencode(state, b) {
uint.preencode(state, b.byteLength);
state.end += b.byteLength;
},
encode(state, b) {
uint.encode(state, b.byteLength);
const view = new Uint8Array(b);
state.buffer.set(view, state.start);
state.start += b.byteLength;
},
decode(state) {
const len = uint.decode(state);
const b = new ArrayBuffer(len);
const view = new Uint8Array(b);
view.set(state.buffer.subarray(state.start, state.start += len));
return b;
}
};
function typedarray(TypedArray, swap) {
const n = TypedArray.BYTES_PER_ELEMENT;
return {
preencode(state, b) {
uint.preencode(state, b.length);
state.end += b.byteLength;
},
encode(state, b) {
uint.encode(state, b.length);
const view = new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
if (BE && swap) swap(view);
state.buffer.set(view, state.start);
state.start += b.byteLength;
},
decode(state) {
const len = uint.decode(state);
let b = state.buffer.subarray(state.start, state.start += len * n);
if (b.byteLength !== len * n) throw new Error("Out of bounds");
if (b.byteOffset % n !== 0) b = new Uint8Array(b);
if (BE && swap) swap(b);
return new TypedArray(b.buffer, b.byteOffset, b.byteLength / n);
}
};
}
var uint8array = exports.uint8array = typedarray(Uint8Array);
exports.uint16array = typedarray(Uint16Array, b4a.swap16);
exports.uint32array = typedarray(Uint32Array, b4a.swap32);
exports.int8array = typedarray(Int8Array);
exports.int16array = typedarray(Int16Array, b4a.swap16);
exports.int32array = typedarray(Int32Array, b4a.swap32);
exports.biguint64array = typedarray(BigUint64Array, b4a.swap64);
exports.bigint64array = typedarray(BigInt64Array, b4a.swap64);
exports.float32array = typedarray(Float32Array, b4a.swap32);
exports.float64array = typedarray(Float64Array, b4a.swap64);
function string(encoding) {
return {
preencode(state, s) {
const len = b4a.byteLength(s, encoding);
uint.preencode(state, len);
state.end += len;
},
encode(state, s) {
const len = b4a.byteLength(s, encoding);
uint.encode(state, len);
b4a.write(state.buffer, s, state.start, encoding);
state.start += len;
},
decode(state) {
const len = uint.decode(state);
if (state.end - state.start < len) throw new Error("Out of bounds");
return b4a.toString(
state.buffer,
encoding,
state.start,
state.start += len
);
},
fixed(n) {
return {
preencode(state) {
state.end += n;
},
encode(state, s) {
b4a.write(state.buffer, s, state.start, n, encoding);
state.start += n;
},
decode(state) {
if (state.end - state.start < n) throw new Error("Out of bounds");
return b4a.toString(
state.buffer,
encoding,
state.start,
state.start += n
);
}
};
}
};
}
var utf8 = exports.string = exports.utf8 = string("utf-8");
exports.ascii = string("ascii");
exports.hex = string("hex");
exports.base64 = string("base64");
exports.ucs2 = exports.utf16le = string("utf16le");
exports.bool = {
preencode(state, b) {
state.end++;
},
encode(state, b) {
state.buffer[state.start++] = b ? 1 : 0;
},
decode(state) {
if (state.start >= state.end) throw Error("Out of bounds");
return state.buffer[state.start++] === 1;
}
};
var fixed = exports.fixed = function fixed2(n) {
return {
preencode(state, s) {
if (s.byteLength !== n) throw new Error("Incorrect buffer size");
state.end += n;
},
encode(state, s) {
state.buffer.set(s, state.start);
state.start += n;
},
decode(state) {
if (state.end - state.start < n) throw new Error("Out of bounds");
return state.buffer.subarray(state.start, state.start += n);
}
};
};
exports.fixed32 = fixed(32);
exports.fixed64 = fixed(64);
exports.array = function array(enc) {
return {
preencode(state, list) {
uint.preencode(state, list.length);
for (let i = 0; i < list.length; i++) enc.preencode(state, list[i]);
},
encode(state, list) {
uint.encode(state, list.length);
for (let i = 0; i < list.length; i++) enc.encode(state, list[i]);
},
decode(state) {
const len = uint.decode(state);
if (len > 1048576) throw new Error("Array is too big");
const arr = new Array(len);
for (let i = 0; i < len; i++) arr[i] = enc.decode(state);
return arr;
}
};
};
exports.frame = function frame(enc) {
const dummy = exports.state();
return {
preencode(state, m) {
const end = state.end;
enc.preencode(state, m);
uint.preencode(state, state.end - end);
},
encode(state, m) {
dummy.end = 0;
enc.preencode(dummy, m);
uint.encode(state, dummy.end);
enc.encode(state, m);
},
decode(state) {
const end = state.end;
const len = uint.decode(state);
state.end = state.start + len;
const m = enc.decode(state);
state.start = state.end;
state.end = end;
return m;
}
};
};
exports.date = {
preencode(state, d) {
int.preencode(state, d.getTime());
},
encode(state, d) {
int.encode(state, d.getTime());
},
decode(state, d) {
return new Date(int.decode(state));
}
};
exports.json = {
preencode(state, v) {
utf8.preencode(state, JSON.stringify(v));
},
encode(state, v) {
utf8.encode(state, JSON.stringify(v));
},
decode(state) {
return JSON.parse(utf8.decode(state));
}
};
exports.ndjson = {
preencode(state, v) {
utf8.preencode(state, JSON.stringify(v) + "\n");
},
encode(state, v) {
utf8.encode(state, JSON.stringify(v) + "\n");
},
decode(state) {
return JSON.parse(utf8.decode(state));
}
};
exports.none = {
preencode(state, n) {
},
encode(state, n) {
},
decode(state) {
return null;
}
};
var anyArray = {
preencode(state, arr) {
uint.preencode(state, arr.length);
for (let i = 0; i < arr.length; i++) {
any.preencode(state, arr[i]);
}
},
encode(state, arr) {
uint.encode(state, arr.length);
for (let i = 0; i < arr.length; i++) {
any.encode(state, arr[i]);
}
},
decode(state) {
const arr = [];
let len = uint.decode(state);
while (len-- > 0) {
arr.push(any.decode(state));
}
return arr;
}
};
var anyObject = {
preencode(state, o) {
const keys = Object.keys(o);
uint.preencode(state, keys.length);
for (const key of keys) {
utf8.preencode(state, key);
any.preencode(state, o[key]);
}
},
encode(state, o) {
const keys = Object.keys(o);
uint.encode(state, keys.length);
for (const key of keys) {
utf8.encode(state, key);
any.encode(state, o[key]);
}
},
decode(state) {
let len = uint.decode(state);
const o = {};
while (len-- > 0) {
const key = utf8.decode(state);
o[key] = any.decode(state);
}
return o;
}
};
var anyTypes = [
exports.none,
exports.bool,
exports.string,
exports.buffer,
exports.uint,
exports.int,
exports.float64,
anyArray,
anyObject,
exports.date
];
var any = exports.any = {
preencode(state, o) {
const t = getType(o);
uint.preencode(state, t);
anyTypes[t].preencode(state, o);
},
encode(state, o) {
const t = getType(o);
uint.encode(state, t);
anyTypes[t].encode(state, o);
},
decode(state) {
const t = uint.decode(state);
if (t >= anyTypes.length) throw new Error("Unknown type: " + t);
return anyTypes[t].decode(state);
}
};
var port = exports.port = uint16;
var address = (host, family) => {
return {
preencode(state, m) {
host.preencode(state, m.host);
port.preencode(state, m.port);
},
encode(state, m) {
host.encode(state, m.host);
port.encode(state, m.port);
},
decode(state) {
return {
host: host.decode(state),
family,
port: port.decode(state)
};
}
};
};
var ipv4 = exports.ipv4 = {
preencode(state) {
state.end += 4;
},
encode(state, string2) {
const start = state.start;
const end = start + 4;
let i = 0;
while (i < string2.length) {
let n = 0;
let c;
while (i < string2.length && (c = string2.charCodeAt(i++)) !== /* . */
46) {
n = n * 10 + (c - /* 0 */
48);
}
state.buffer[state.start++] = n;
}
state.start = end;
},
decode(state) {
if (state.end - state.start < 4) throw new Error("Out of bounds");
return state.buffer[state.start++] + "." + state.buffer[state.start++] + "." + state.buffer[state.start++] + "." + state.buffer[state.start++];
}
};
exports.ipv4Address = address(ipv4, 4);
var ipv6 = exports.ipv6 = {
preencode(state) {
state.end += 16;
},
encode(state, string2) {
const start = state.start;
const end = start + 16;
let i = 0;
let split = null;
while (i < string2.length) {
let n = 0;
let c;
while (i < string2.length && (c = string2.charCodeAt(i++)) !== /* : */
58) {
if (c >= 48 && c <= 57) n = n * 16 + (c - /* 0 */
48);
else if (c >= 65 && c <= 70) n = n * 16 + (c - /* A */
65 + 10);
else if (c >= 97 && c <= 102) n = n * 16 + (c - /* a */
97 + 10);
}
state.buffer[state.start++] = n >>> 8;
state.buffer[state.start++] = n;
if (i < string2.length && string2.charCodeAt(i) === /* : */
58) {
i++;
split = state.start;
}
}
if (split !== null) {
const offset = end - state.start;
state.buffer.copyWithin(split + offset, split).fill(0, split, split + offset);
}
state.start = end;
},
decode(state) {
if (state.end - state.start < 16) throw new Error("Out of bounds");
return (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) + ":" + (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) + ":" + (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) + ":" + (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) + ":" + (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) + ":" + (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) + ":" + (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) + ":" + (state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16);
}
};
exports.ipv6Address = address(ipv6, 6);
var ip = exports.ip = {
preencode(state, string2) {
const family = string2.includes(":") ? 6 : 4;
uint8.preencode(state, family);
if (family === 4) ipv4.preencode(state);
else ipv6.preencode(state);
},
encode(state, string2) {
const family = string2.includes(":") ? 6 : 4;
uint8.encode(state, family);
if (family === 4) ipv4.encode(state, string2);
else ipv6.encode(state, string2);
},
decode(state) {
const family = uint8.decode(state);
if (family === 4) return ipv4.decode(state);
else return ipv6.decode(state);
}
};
exports.ipAddress = {
preencode(state, m) {
ip.preencode(state, m.host);
port.preencode(state, m.port);
},
encode(state, m) {
ip.encode(state, m.host);
port.encode(state, m.port);
},
decode(state) {
const family = uint8.decode(state);
return {
host: family === 4 ? ipv4.decode(state) : ipv6.decode(state),
family,
port: port.decode(state)
};
}
};
var record = exports.record = function(keyEncoding, valueEncoding) {
return {
preencode(state, v) {
const keys = Object.keys(v);
uint.preencode(state, keys.length);
for (const k of keys) {
keyEncoding.preencode(state, k);
valueEncoding.preencode(state, v[k]);
}
},
encode(state, v) {
const keys = Object.keys(v);
uint.encode(state, keys.length);
for (const k of keys) {
keyEncoding.encode(state, k);
valueEncoding.encode(state, v[k]);
}
},
decode(state) {
const out = /* @__PURE__ */ Object.create(null);
const keys = uint.decode(state);
for (let i = 0; i < keys; i++) {
out[keyEncoding.decode(state)] = valueEncoding.decode(state);
}
return out;
}
};
};
exports.stringRecord = record(utf8, utf8);
function getType(o) {
if (o === null || o === void 0) return 0;
if (typeof o === "boolean") return 1;
if (typeof o === "string") return 2;
if (b4a.isBuffer(o)) return 3;
if (typeof o === "number") {
if (Number.isInteger(o)) return o >= 0 ? 4 : 5;
return 6;
}
if (Array.isArray(o)) return 7;
if (o instanceof Date) return 9;
if (typeof o === "object") return 8;
throw new Error("Unsupported type for " + o);
}
exports.from = function from(enc) {
if (typeof enc === "string") return fromNamed(enc);
if (enc.preencode) return enc;
if (enc.encodingLength) return fromAbstractEncoder(enc);
return fromCodec(enc);
};
function fromNamed(enc) {
switch (enc) {
case "ascii":
return raw.ascii;
case "utf-8":
case "utf8":
return raw.utf8;
case "hex":
return raw.hex;
case "base64":
return raw.base64;
case "utf16-le":
case "utf16le":
case "ucs-2":
case "ucs2":
return raw.ucs2;
case "ndjson":
return raw.ndjson;
case "json":
return raw.json;
case "binary":
default:
return raw.binary;
}
}
function fromCodec(enc) {
let tmpM = null;
let tmpBuf = null;
return {
preencode(state, m) {
tmpM = m;
tmpBuf = enc.encode(m);
state.end += tmpBuf.byteLength;
},
encode(state, m) {
raw.encode(state, m === tmpM ? tmpBuf : enc.encode(m));
tmpM = tmpBuf = null;
},
decode(state) {
return enc.decode(raw.decode(state));
}
};
}
function fromAbstractEncoder(enc) {
return {
preencode(state, m) {
state.end += enc.encodingLength(m);
},
encode(state, m) {
enc.encode(m, state.buffer, state.start);
state.start += enc.encode.bytes;
},
decode(state) {
const m = enc.decode(state.buffer, state.start, state.end);
state.start += enc.decode.bytes;
return m;
}
};
}
exports.encode = function encode(enc, m) {
const state = exports.state();
enc.preencode(state, m);
state.buffer = b4a.allocUnsafe(state.end);
enc.encode(state, m);
return state.buffer;
};
exports.decode = function decode(enc, buffer2) {
return enc.decode(exports.state(0, buffer2.byteLength, buffer2));
};
function zigZagInt(enc) {
return {
preencode(state, n) {
enc.preencode(state, zigZagEncodeInt(n));
},
encode(state, n) {
enc.encode(state, zigZagEncodeInt(n));
},
decode(state) {
return zigZagDecodeInt(enc.decode(state));
}
};
}
function zigZagDecodeInt(n) {
return n === 0 ? n : (n & 1) === 0 ? n / 2 : -(n + 1) / 2;
}
function zigZagEncodeInt(n) {
return n < 0 ? 2 * -n - 1 : n === 0 ? 0 : 2 * n;
}
function zigZagBigInt(enc) {
return {
preencode(state, n) {
enc.preencode(state, zigZagEncodeBigInt(n));
},
encode(state, n) {
enc.encode(state, zigZagEncodeBigInt(n));
},
decode(state) {
return zigZagDecodeBigInt(enc.decode(state));
}
};
}
function zigZagDecodeBigInt(n) {
return n === 0n ? n : (n & 1n) === 0n ? n / 2n : -(n + 1n) / 2n;
}
function zigZagEncodeBigInt(n) {
return n < 0n ? 2n * -n - 1n : n === 0n ? 0n : 2n * n;
}
function validateSafeUint(n) {
if (n > Number.MAX_SAFE_INTEGER)
throw new Error(
"uint is greater than the maximum safe integer, use biguint/bigint"
);
return n;
}
function validateUint(n) {
if (n >= 0 === false)
throw new Error("uint must be positive");
if (n > Number.MAX_SAFE_INTEGER)
throw new Error(
"integer is greater than the maximum safe integer, use biguint/bigint"
);
}
}
});
// ../../node_modules/queue-tick/queue-microtask.js
var require_queue_microtask = __commonJS({
"../../node_modules/queue-tick/queue-microtask.js"(exports, module) {
module.exports = typeof queueMicrotask === "function" ? queueMicrotask : (fn) => Promise.resolve().then(fn);
}
});
// ../../node_modules/queue-tick/process-next-tick.js
var require_process_next_tick = __commonJS({
"../../node_modules/queue-tick/process-next-tick.js"(exports, module) {
module.exports = typeof process !== "undefined" && typeof process.nextTick === "function" ? process.nextTick.bind(process) : require_queue_microtask();
}
});
// ../../node_modules/safety-catch/index.js
var require_safety_catch = __commonJS({
"../../node_modules/safety-catch/index.js"(exports, module) {
module.exports = safetyCatch;
function isActuallyUncaught(err) {
if (!err) return false;
return err instanceof TypeError || err instanceof SyntaxError || err instanceof ReferenceError || err instanceof EvalError || err instanceof RangeError || err instanceof URIError || err.code === "ERR_ASSERTION" || err.name === "AssertionError";
}
function throwErrorNT(err) {
queueMicrotask(() => {
throw err;
});
}
function safetyCatch(err) {
if (isActuallyUncaught(err)) {
throwErrorNT(err);
throw err;
}
}
}
});
// ../../node_modules/unslab/index.js
var require_unslab = __commonJS({
"../../node_modules/unslab/index.js"(exports, module) {
var b4a = require_b4a();
unslab.all = all;
unslab.is = is;
module.exports = unslab;
function unslab(buf) {
if (buf === null || buf.buffer.byteLength === buf.byteLength) return buf;
const copy = b4a.allocUnsafeSlow(buf.byteLength);
copy.set(buf, 0);
return copy;
}
function is(buf) {
return buf.buffer.byteLength !== buf.byteLength;
}
function all(list) {
let size = 0;
for (let i = 0; i < list.length; i++) {
const buf = list[i];
size += buf === null || buf.buffer.byteLength === buf.byteLength ? 0 : buf.byteLength;
}
const copy = b4a.allocUnsafeSlow(size);
const result = new Array(list.length);
let offset = 0;
for (let i = 0; i < list.length; i++) {
let buf = list[i];
if (buf !== null && buf.buffer.byteLength !== buf.byteLength) {
copy.set(buf, offset);
buf = copy.subarray(offset, offset += buf.byteLength);
}
result[i] = buf;
}
return result;
}
}
});
// ../../node_modules/protomux/index.js
var require_protomux = __commonJS({
"../../node_modules/protomux/index.js"(exports, module) {
var b4a = require_b4a();
var c = require_compact_encoding();
var queueTick = require_process_next_tick();
var safetyCatch = require_safety_catch();
var unslab = require_unslab();
var MAX_BUFFERED = 32768;
var MAX_BACKLOG = Infinity;
var MAX_BATCH = 8 * 1024 * 1024;
var Channel = class {
constructor(mux, info, userData, protocol, aliases, id, handshake, messages, onopen, onclose, ondestroy, ondrain) {
this.userData = userData;
this.protocol = protocol;
this.aliases = aliases;
this.id = id;
this.handshake = null;
this.messages = [];
this.opened = false;
this.closed = false;
this.destroyed = false;
this.onopen = onopen;
this.onclose = onclose;
this.ondestroy = ondestroy;
this.ondrain = ondrain;
this._handshake = handshake;
this._mux = mux;
this._info = info;
this._localId = 0;
this._remoteId = 0;
this._active = 0;
this._extensions = null;
this._decBound = this._dec.bind(this);
this._decAndDestroyBound = this._decAndDestroy.bind(this);
this._openedPromise = null;
this._openedResolve = null;
this._destroyedPromise = null;
this._destroyedResolve = null;
for (const m of messages) this.addMessage(m);
}
get drained() {
return this._mux.drained;
}
fullyOpened() {
if (this.opened) return Promise.resolve(true);
if (this.closed) return Promise.resolve(false);
if (this._openedPromise) return this._openedPromise;
this._openedPromise = new Promise((resolve) => {
this._openedResolve = resolve;
});
return this._openedPromise;
}
fullyClosed() {
if (this.destroyed) return Promise.resolve();
if (this._destroyedPromise) return this._destroyedPromise;
this._destroyedPromise = new Promise((resolve) => {
this._destroyedResolve = resolve;
});
return this._destroyedPromise;
}
open(handshake) {
const id = this._mux._free.length > 0 ? this._mux._free.pop() : this._mux._local.push(null) - 1;
this._info.opened++;
this._info.lastChannel = this;
this._localId = id + 1;
this._mux._local[id] = this;
if (this._remoteId === 0) {
this._info.outgoing.push(this._localId);
}
const state = { buffer: null, start: 2, end: 2 };
c.uint.preencode(state, this._localId);
c.string.preencode(state, this.protocol);
c.optionalBuffer.preencode(state, this.id);
if (this._handshake) this._handshake.preencode(state, handshake);
state.buffer = this._mux._alloc(state.end);
state.buffer[0] = 0;
state.buffer[1] = 1;
c.uint.encode(state, this._localId);
c.string.encode(state, this.protocol);
c.optionalBuffer.encode(state, this.id);
if (this._handshake) this._handshake.encode(state, handshake);
this._mux._write0(state.buffer);
}
_dec() {
if (--this._active === 0 && this.closed === true) this._destroy();
}
_decAndDestroy(err) {
this._dec();
this._mux._safeDestroy(err);
}
_fullyOpenSoon() {
this._mux._remote[this._remoteId - 1].session = this;
queueTick(this._fullyOpenOrDestroy.bind(this));
}
_fullyOpenOrDestroy() {
try {
this._fullyOpen();
} catch (err) {
this._mux._safeDestroyBound(err);
}
}
_fullyOpen() {
if (this.opened === true || this.closed === true) return;
const remote = this._mux._remote[this._remoteId - 1];
this.handshake = this._handshake ? this._handshake.decode(remote.state) : null;
this._track(this.onopen(this.handshake, this));
remote.session = this;
remote.state = null;
if (remote.pending !== null) this._drain(remote);
if (this._mux._destroying === true) return;
this.opened = true;
this._resolveOpen(true);
}
_resolveOpen(opened) {
if (this._openedResolve !== null) {
this._openedResolve(opened);
this._openedResolve = this._openedPromise = null;
}
}
_resolveDestroyed() {
if (this._destroyedResolve !== null) {
this._destroyedResolve();
this._destroyedResolve = this._destroyedPromise = null;
}
}
_drain(remote) {
for (let i = 0; i < remote.pending.length; i++) {
const p = remote.pending[i];
this._mux._buffered -= byteSize(p.state);
this._recv(p.type, p.state);
if (this._mux._destroying === true) return;
}
remote.pending = null;
this._mux._resumeMaybe();
}
_track(p) {
if (isPromise(p) === true) {
this._active++;
return p.then(this._decBound, this._decAndDestroyBound);
}
return null;
}
_close(isRemote) {
if (this.closed === true) return;
this.closed = true;
this._info.opened--;
if (this._info.lastChannel === this) this._info.lastChannel = null;
if (this._remoteId > 0) {
this._mux._remote[this._remoteId - 1] = null;
this._remoteId = 0;
this._mux._free.push(this._localId - 1);
}
this._mux._local[this._localId - 1] = null;
this._localId = 0;
this._mux._gc(this._info);
this._track(this.onclose(isRemote, this));
if (this._active === 0) this._destroy();
this._resolveOpen(false);
}
_destroy() {
if (this.destroyed === true) return;
this.destroyed = true;
this._track(this.ondestroy(this));
this._resolveDestroyed();
}
_recv(type, state) {
if (type < this.messages.length) {
const m = this.messages[type];
const p = m.recv(state, this);
if (m.autoBatch === true) return p;
}
return null;
}
cork() {
this._mux.cork();
}
uncork() {
this._mux.uncork();
}
close() {
if (this.closed === true) return;
const state = { buffer: null, start: 2, end: 2 };
c.uint.preencode(state, this._localId);
state.buffer = this._mux._alloc(state.end);
state.buffer[0] = 0;
state.buffer[1] = 3;
c.uint.encode(state, this._localId);
this._close(false);
this._mux._write0(state.buffer);
}
addMessage(opts) {
if (!opts) return this._skipMessage();
const type = this.messages.length;
const autoBatch = opts.autoBatch !== false;
const encoding = opts.encoding || c.raw;
const onmessage = opts.onmessage || noop;
const s = this;
const typeLen = encodingLength(c.uint, type);
const m = {
type,
autoBatch,
encoding,
onmessage,
recv(state, session) {
return session._track(m.onmessage(encoding.decode(state), session));
},
send(m2, session = s) {
if (session.closed === true) return false;
const mux = session._mux;
const state = { buffer: null, start: 0, end: typeLen };
if (mux._batch !== null) {
encoding.preencode(state, m2);
state.buffer = mux._alloc(state.end);
c.uint.encode(state, type);
encoding.encode(state, m2);
mux._pushBatch(session._localId, state.buffer);
return true;
}
c.uint.preencode(state, session._localId);
encoding.preencode(state, m2);
state.buffer = mux._alloc(state.end);
c.uint.encode(state, session._localId);
c.uint.encode(state, type);
encoding.encode(state, m2);
mux.drained = mux.stream.write(state.buffer);
return mux.drained;
}
};
this.messages.push(m);
return m;
}
_skipMessage() {
const type = this.messages.length;
const m = {
type,
encoding: c.raw,
onmessage: noop,
recv(state, session) {
},
send(m2, session) {
}
};
this.messages.push(m);
return m;
}
};
module.exports = class Protomux {
constructor(stream, { alloc } = {}) {
if (stream.userData === null) stream.userData = this;
this.isProtomux = true;
this.stream = stream;
this.corked = 0;
this.drained = true;
this._alloc = alloc || (typeof stream.alloc === "function" ? stream.alloc.bind(stream) : b4a.allocUnsafe);
this._safeDestroyBound = this._safeDestroy.bind(this);
this._uncorkBound = this.uncork.bind(this);
this._remoteBacklog = 0;
this._buffered = 0;
this._paused = false;
this._remote = [];
this._local = [];
this._free = [];
this._batch = null;
this._batchState = null;
this._infos = /* @__PURE__ */ new Map();
this._notify = /* @__PURE__ */ new Map();
this._destroying = false;
this.stream.on("data", this._ondata.bind(this));
this.stream.on("drain", this._ondrain.bind(this));
this.stream.on("end", this._onend.bind(this));
this.stream.on("error", noop);
this.stream.on("close", this._shutdown.bind(this));
}
static from(stream, opts) {
if (stream.userData && stream.userData.isProtomux) return stream.userData;
if (stream.isProtomux) return stream;
return new this(stream, opts);
}
static isProtomux(mux) {
return typeof mux === "object" && mux.isProtomux === true;
}
*[Symbol.iterator]() {
for (const session of this._local) {
if (session !== null) yield session;
}
}
isIdle() {
return this._local.length === this._free.length;
}
cork() {
if (++this.corked === 1) {
this._batch = [];
this._batchState = { buffer: null, start: 0, end: 1 };
}
}
uncork() {
if (--this.corked === 0) {
this._sendBatch(this._batch, this._batchState);
this._batch = null;
this._batchState = null;
}
}
getLastChannel({ protocol, id = null }) {
const key = toKey(protocol, id);
const info = this._infos.get(key);
if (info) return info.lastChannel;
return null;
}
pair({ protocol, id = null }, notify) {
this._notify.set(toKey(protocol, id), notify);
}
unpair({ protocol, id = null }) {
this._notify.delete(toKey(protocol, id));
}
opened({ protocol, id = null }) {
const key = toKey(protocol, id);
const info = this._infos.get(key);
return info ? info.opened > 0 : false;
}
createChannel({
userData = null,
protocol,
aliases = [],
id = null,
unique = true,
handshake = null,
messages = [],
onopen = noop,
onclose = noop,
ondestroy = noop,
ondrain = noop
}) {
if (this.stream.destroyed) return null;
const info = this._get(protocol, id, aliases);
if (unique && info.opened > 0) return null;
if (info.incoming.length === 0) {
return new Channel(
this,
info,
userData,
protocol,
aliases,
id,
handshake,
messages,
onopen,
onclose,
ondestroy,
ondrain
);
}
this._remoteBacklog--;
const remoteId = info.incoming.shift();
const r = this._remote[remoteId - 1];
if (r === null) return null;
const session = new Channel(
this,
info,
userData,
protocol,
aliases,
id,
handshake,
messages,
onopen,
onclose,
ondestroy,
ondrain
);
session._remoteId = remoteId;
session._fullyOpenSoon();
return session;
}
_pushBatch(localId, buffer) {
if (this._batchState.end >= MAX_BATCH) {
this._sendBatch(this._batch, this._batchState);
this._batch = [];
this._batchState = { buffer: null, start: 0, end: 1 };
}
if (this._batch.length === 0 || this._batch[this._batch.length - 1].localId !== localId) {
this._batchState.end++;
c.uint.preencode(this._batchState, localId);
}
c.buffer.preencode(this._batchState, buffer);
this._batch.push({ localId, buffer });
}
_sendBatch(batch, state) {
if (batch.length === 0) return;
let prev = batch[0].localId;
state.buffer = this._alloc(state.end);
state.buffer[state.start++] = 0;
state.buffer[state.start++] = 0;
c.uint.encode(state, prev);
for (let i = 0; i < batch.length; i++) {
const b = batch[i];
if (prev !== b.localId) {
state.buffer[state.start++] = 0;
c.uint.encode(state, prev = b.localId);
}
c.buffer.encode(state, b.buffer);
}
this.drained = this.stream.write(state.buffer);
}
_get(protocol, id, aliases = []) {
const key = toKey(protocol, id);
let info = this._infos.get(key);
if (info) return info;
info = {
key,
protocol,
aliases: [],
id,
pairing: 0,
opened: 0,
incoming: [],
outgoing: [],
lastChannel: null
};
this._infos.set(key, info);
for (const alias of aliases) {
const key2 = toKey(alias, id);
info.aliases.push(key2);
this._infos.set(key2, info);
}
return info;
}
_gc(info) {
if (info.opened === 0 && info.outgoing.length === 0 && info.incoming.length === 0) {
this._infos.delete(info.key);
for (const alias of info.aliases) this._infos.delete(alias);
}
}
_ondata(buffer) {
if (buffer.byteLength === 0) return;
try {
const state = { buffer, start: 0, end: buffer.byteLength };
this._decode(c.uint.decode(state), state);
} catch (err) {
this._safeDestroy(err);
}
}
_ondrain() {
this.drained = true;
for (const s of this._local) {
if (s !== null) s._track(s.ondrain(s));
}
}
_onend() {
this.stream.end();
}
_decode(remoteId, state) {
const type = c.uint.decode(state);
if (remoteId === 0) {
return this._oncontrolsession(type, state);
}
const r = remoteId <= this._remote.length ? this._remote[remoteId - 1] : null;
if (r === null) return null;
if (r.pending !== null) {
this._bufferMessage(r, type, state);
return null;
}
return r.session._recv(type, state);
}
_oncontrolsession(type, state) {
switch (type) {
case 0:
this._onbatch(state);
break;
case 1:
return this._onopensession(state);
case 2:
this._onrejectsession(state);
break;
case 3:
this._onclosesession(state);
break;
}
return null;
}
_bufferMessage(r, type, { buffer, start, end }) {
const state = { buffer, start, end };
r.pending.push({ type, state });
this._buffered += byteSize(state);
this._pauseMaybe();
}
_pauseMaybe() {
if (this._paused === true || this._buffered <= MAX_BUFFERED) return;
this._paused = true;
this.stream.pause();
}
_resumeMaybe() {
if (this._paused === false || this._buffered > MAX_BUFFERED) return;
this._paused = false;
this.stream.resume();
}
_onbatch(state) {
const end = state.end;
let remoteId = c.uint.decode(state);
let waiting = null;
while (state.end > state.start) {
const len = c.uint.decode(state);
if (len === 0) {
remoteId = c.uint.decode(state);
continue;
}
state.end = state.start + len;
if (end !== state.end && waiting === null) {
waiting = [];
this.cork();
}
const p = this._decode(remoteId, state);
if (waiting !== null && p !== null) waiting.push(p);
state.start = state.end;
state.end = end;
}
if (waiting !== null) {
Promise.all(waiting).then(this._uncorkBound, this._safeDestroyBound);
}
}
_onopensession(state) {
const remoteId = c.uint.decode(state);
const protocol = c.string.decode(state);
const id = unslab(c.optionalBuffer.decode(state));
if (remoteId === 0) {
this._rejectSession(0);
return null;
}
const rid = remoteId - 1;
const info = this._get(protocol, id);
if (this._remote.length === rid) {
this._remote.push(null);
}
if (rid >= this._remote.length || this._remote[rid] !== null) {
throw new Error("Invalid open message");
}
if (info.outgoing.length > 0) {
const localId = info.outgoing.shift();
const session = this._local[localId - 1];
if (session === null) {
this._free.push(localId - 1);
return null;
}
this._remote[rid] = { state, pending: null, session: null };
session._remoteId = remoteId;
session._fullyOpen();
return null;
}
const copyState = { buffer: state.buffer, start: state.start, end: state.end };
this._remote[rid] = { state: copyState, pending: [], session: null };
if (++this._remoteBacklog > MAX_BACKLOG) {
throw new Error("Remote exceeded backlog");
}
info.pairing++;
info.incoming.push(remoteId);
return this._requestSession(protocol, id, info).catch(this._safeDestroyBound);
}
_onrejectsession(state) {
const localId = c.uint.decode(state);
for (const info of this._infos.values()) {
const i = info.outgoing.indexOf(localId);
if (i === -1) continue;
info.outgoing.splice(i, 1);
const session = this._local[localId - 1];
this._free.push(localId - 1);
if (session !== null) session._close(true);
this._gc(info);
return;
}
throw new Error("Invalid reject message");
}
_onclosesession(state) {
const remoteId = c.uint.decode(state);
if (remoteId === 0) return;
const rid = remoteId - 1;
const r = rid < this._remote.length ? this._remote[rid] : null;
if (r === null) return;
if (r.session !== null) r.session._close(true);
}
async _requestSession(protocol, id, info) {
const notify = this._notify.get(toKey(protocol, id)) || this._notify.get(toKey(protocol, null));
if (notify) await notify(id);
if (--info.pairing > 0) return;
while (info.incoming.length > 0) {
this._rejectSession(info, info.incoming.shift());
}
this._gc(info);
}
_rejectSession(info, remoteId) {
if (remoteId > 0) {
const r = this._remote[remoteId - 1];
if (r.pending !== null) {
for (let i = 0; i < r.pending.length; i++) {
this._buffered -= byteSize(r.pending[i].state);
}
}
this._remote[remoteId - 1] = null;
this._resumeMaybe();
}
const state = { buffer: null, start: 2, end: 2 };
c.uint.preencode(state, remoteId);
state.buffer = this._alloc(state.end);
state.buffer[0] = 0;
state.buffer[1] = 2;
c.uint.encode(state, remoteId);
this._write0(state.buffer);
}
_write0(buffer) {
if (this._batch !== null) {
this._pushBatch(0, buffer.subarray(1));
return;
}
this.drained = this.stream.write(buffer);
}
destroy(err) {
this._destroying = true;
this.stream.destroy(err);
}
_safeDestroy(err) {
safetyCatch(err);
this._destroying = true;
this.stream.destroy(err);
}
_shutdown() {
this._destroying = true;
for (const s of this._local) {
if (s !== null) s._close(true);
}
}
};
function noop() {
}
function toKey(protocol, id) {
return protocol + "##" + (id ? b4a.toString(id, "hex") : "");
}
function byteSize(state) {
return 512 + (state.end - state.start);
}
function isPromise(p) {
return !!(p && typeof p.then === "function");
}
function encodingLength(enc, val) {
const state = { buffer: null, start: 0, end: 0 };
enc.preencode(state, val);
return state.end;
}
}
});
// ../../bare-lib-entry-protomux.js
var bare_lib_entry_protomux_exports = {};
__export(bare_lib_entry_protomux_exports, {
default: () => bare_lib_entry_protomux_default
});
var import_protomux = __toESM(require_protomux());
var bare_lib_entry_protomux_default = import_protomux.default;
return __toCommonJS(bare_lib_entry_protomux_exports);
})();
;(function(){var g=globalThis;var s="__bare_os_stdlib__";g[s]=g[s]||{};var e=typeof __bare_os_bundle_exports__!=="undefined"?__bare_os_bundle_exports__:void 0;var v=e!=null&&typeof e==="object"&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e;g[s]["protomux"]=v;})();