@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
110 lines (109 loc) • 4.62 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 v1_exports = {};
__export(v1_exports, {
hashV1PartitionKey: () => hashV1PartitionKey
});
module.exports = __toCommonJS(v1_exports);
var import_number = require("./encoding/number.js");
var import_string = require("./encoding/string.js");
var import_prefix = require("./encoding/prefix.js");
var import_murmurHash = __toESM(require("./murmurHash.js"));
var import_uint8 = require("../uint8.js");
const MAX_STRING_CHARS = 100;
function hashV1PartitionKey(partitionKey) {
const key = partitionKey[0];
const toHash = prefixKeyByType(key);
const hash = import_murmurHash.default.x86.hash32(toHash);
const encodedJSBI = (0, import_number.writeNumberForBinaryEncodingBigInt)(hash);
const encodedValue = encodeByType(key);
const finalHash = (0, import_uint8.uint8ArrayToHex)((0, import_uint8.concatUint8Arrays)([encodedJSBI, encodedValue])).toUpperCase();
return finalHash;
}
function prefixKeyByType(key) {
let bytes;
switch (typeof key) {
case "string": {
const truncated = key.substr(0, MAX_STRING_CHARS);
bytes = (0, import_uint8.concatUint8Arrays)([
(0, import_uint8.hexStringToUint8Array)(import_prefix.BytePrefix.String),
new TextEncoder().encode(truncated),
(0, import_uint8.hexStringToUint8Array)(import_prefix.BytePrefix.Undefined)
]);
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 encodeByType(key) {
switch (typeof key) {
case "string": {
const truncated = key.substring(0, MAX_STRING_CHARS);
return (0, import_string.writeStringForBinaryEncoding)(truncated);
}
case "number": {
return (0, import_number.writeNumberForBinaryEncodingBigInt)(key);
}
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}`);
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
hashV1PartitionKey
});