@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
91 lines (90 loc) • 3.73 kB
JavaScript
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(
// 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);
var v2_exports = {};
__export(v2_exports, {
hashV2PartitionKey: () => hashV2PartitionKey,
reverse: () => reverse
});
module.exports = __toCommonJS(v2_exports);
var import_number = require("./encoding/number.js");
var import_prefix = require("./encoding/prefix.js");
var import_murmurHash = __toESM(require("./murmurHash.js"));
var import_uint8 = require("../uint8.js");
function hashV2PartitionKey(partitionKey) {
const toHash = (0, import_uint8.concatUint8Arrays)(partitionKey.map(prefixKeyByType));
const hash = import_murmurHash.default.x64.hash128(toHash);
const reverseBuff = reverse((0, import_uint8.hexStringToUint8Array)(hash));
reverseBuff[0] &= 63;
return (0, import_uint8.uint8ArrayToHex)(reverseBuff).toUpperCase();
}
function prefixKeyByType(key) {
let bytes;
switch (typeof key) {
case "string": {
bytes = (0, import_uint8.concatUint8Arrays)([
(0, import_uint8.hexStringToUint8Array)(import_prefix.BytePrefix.String),
new TextEncoder().encode(key),
(0, import_uint8.hexStringToUint8Array)(import_prefix.BytePrefix.Infinity)
]);
return bytes;
}
case "number": {
const numberBytes = (0, import_number.doubleToByteArrayBigInt)(key);
bytes = (0, import_uint8.concatUint8Arrays)([(0, import_uint8.hexStringToUint8Array)(import_prefix.BytePrefix.Number), numberBytes]);
return bytes;
}
case "boolean": {
const prefix = key ? import_prefix.BytePrefix.True : import_prefix.BytePrefix.False;
return (0, import_uint8.hexStringToUint8Array)(prefix);
}
case "object": {
if (key === null) {
return (0, import_uint8.hexStringToUint8Array)(import_prefix.BytePrefix.Null);
}
return (0, import_uint8.hexStringToUint8Array)(import_prefix.BytePrefix.Undefined);
}
case "undefined": {
return (0, import_uint8.hexStringToUint8Array)(import_prefix.BytePrefix.Undefined);
}
default:
throw new Error(`Unexpected type: ${typeof key}`);
}
}
function reverse(buff) {
const uint8array = new Uint8Array(buff.length);
for (let i = 0, j = buff.length - 1; i <= j; ++i, --j) {
uint8array[i] = buff[j];
uint8array[j] = buff[i];
}
return uint8array;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
hashV2PartitionKey,
reverse
});