UNPKG

@azure/event-hubs

Version:
134 lines (133 loc) 3.55 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); var partitionKeyToIdMapper_exports = {}; __export(partitionKeyToIdMapper_exports, { hashPartitionKey: () => hashPartitionKey, mapPartitionKeyToId: () => mapPartitionKeyToId }); module.exports = __toCommonJS(partitionKeyToIdMapper_exports); function mapPartitionKeyToId(partitionKey, partitionCount) { const hashedParitionKey = hashPartitionKey(partitionKey); return Math.abs(hashedParitionKey % partitionCount); } function hashPartitionKey(partitionKey) { const hash = computeHash(Buffer.from(partitionKey, "utf8")); return castToInt16(hash.c ^ hash.b); } function readUInt32(data, offset) { return data.readUInt32LE(offset); } function castToInt16(n) { return new Int16Array([n])[0]; } function computeHash(data, seed1 = 0, seed2 = 0) { let a, b, c; a = b = c = 3735928559 + data.length + seed1; c += seed2; let index = 0, size = data.length; while (size > 12) { a += readUInt32(data, index); b += readUInt32(data, index + 4); c += readUInt32(data, index + 8); a -= c; a ^= c << 4 | c >>> 28; c += b; b -= a; b ^= a << 6 | a >>> 26; a += c; c -= b; c ^= b << 8 | b >>> 24; b += a; a -= c; a ^= c << 16 | c >>> 16; c += b; b -= a; b ^= a << 19 | a >>> 13; a += c; c -= b; c ^= b << 4 | b >>> 28; b += a; index += 12; size -= 12; } let curr = size; switch (curr) { case 12: a += readUInt32(data, index); b += readUInt32(data, index + 4); c += readUInt32(data, index + 8); break; case 11: c += data[index + 10] << 16; curr = 10; case 10: c += data[index + 9] << 8; curr = 9; case 9: c += data[index + 8]; curr = 8; case 8: b += readUInt32(data, index + 4); a += readUInt32(data, index); break; case 7: b += data[index + 6] << 16; curr = 6; case 6: b += data[index + 5] << 8; curr = 5; case 5: b += data[index + 4]; curr = 4; case 4: a += readUInt32(data, index); break; case 3: a += data[index + 2] << 16; curr = 2; case 2: a += data[index + 1] << 8; curr = 1; case 1: a += data[index]; break; case 0: return { b: b >>> 0, c: c >>> 0 }; } c ^= b; c -= b << 14 | b >>> 18; a ^= c; a -= c << 11 | c >>> 21; b ^= a; b -= a << 25 | a >>> 7; c ^= b; c -= b << 16 | b >>> 16; a ^= c; a -= c << 4 | c >>> 28; b ^= a; b -= a << 14 | a >>> 18; c ^= b; c -= b << 24 | b >>> 8; return { b: b >>> 0, c: c >>> 0 }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { hashPartitionKey, mapPartitionKeyToId }); //# sourceMappingURL=partitionKeyToIdMapper.js.map