UNPKG

mastercache

Version:

Multi-tier cache module for Node.js. Redis, Upstash, CloudfareKV, File, in-memory and others drivers

103 lines (102 loc) 3.75 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/bus/encoders/binary-encoder.ts var binary_encoder_exports = {}; __export(binary_encoder_exports, { BinaryEncoder: () => BinaryEncoder }); module.exports = __toCommonJS(binary_encoder_exports); var BinaryEncoder = class { #busIdLength; /** * We assume the bus ID is a string of length 24 by default. * Because this is the default length of a cuid */ constructor(busIdLength = 24) { this.#busIdLength = busIdLength; } busMessageTypeToNum(type) { if (type === "set" /* Set */) return 1; if (type === "clear" /* Clear */) return 2; return 3; } numToBusMessageType(num) { if (num === 1) return "set" /* Set */; if (num === 2) return "clear" /* Clear */; return "delete" /* Delete */; } /** * Encode the given message into a Buffer */ encode(message) { const payload = message.payload; const totalKeysLength = payload.keys.reduce( (sum, key) => sum + 4 + Buffer.byteLength(key, "utf8"), 0 ); const namespaceKeyLength = payload.namespace ? Buffer.byteLength(payload.namespace, "utf8") : 0; const totalLength = this.#busIdLength + 1 + 4 + namespaceKeyLength + totalKeysLength; const buffer = Buffer.alloc(totalLength); buffer.write(message.busId, 0, this.#busIdLength, "utf8"); buffer.writeUInt8(this.busMessageTypeToNum(payload.type), this.#busIdLength); let offset = this.#busIdLength + 1; buffer.writeUInt32BE(namespaceKeyLength, offset); offset += 4; if (payload.namespace) { buffer.write(payload.namespace, offset, namespaceKeyLength, "utf8"); offset += namespaceKeyLength; } for (const key of payload.keys) { const keyLength = Buffer.byteLength(key, "utf8"); buffer.writeUInt32BE(keyLength, offset); offset += 4; buffer.write(key, offset, keyLength, "utf8"); offset += keyLength; } return buffer; } /** * Decode the given Buffer into a CacheBusMessage */ decode(data) { let offset = 0; const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data, "binary"); const busId = buffer.toString("utf8", offset, this.#busIdLength); offset += this.#busIdLength; const typeValue = buffer.readUInt8(offset++); const type = this.numToBusMessageType(typeValue); const namespaceKeyLength = buffer.readUInt32BE(offset); offset += 4; const namespace = namespaceKeyLength ? buffer.toString("utf8", offset, offset + namespaceKeyLength) : ""; offset += namespaceKeyLength; const keys = []; while (offset < buffer.length) { const keyLength = buffer.readUInt32BE(offset); offset += 4; const key = buffer.toString("utf8", offset, offset + keyLength); offset += keyLength; keys.push(key); } return { busId, payload: { keys, type, namespace } }; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { BinaryEncoder }); //# sourceMappingURL=binary-encoder.cjs.map