UNPKG

@azure/cosmos

Version:
84 lines (83 loc) 3.36 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 number_exports = {}; __export(number_exports, { doubleToByteArrayBigInt: () => doubleToByteArrayBigInt, writeNumberForBinaryEncodingBigInt: () => writeNumberForBinaryEncodingBigInt }); module.exports = __toCommonJS(number_exports); var import_uint8 = require("../../uint8.js"); var import_prefix = require("./prefix.js"); function writeNumberForBinaryEncodingBigInt(hash) { let payload = encodeNumberAsUInt64BigInt(hash); let outputStream = (0, import_uint8.hexStringToUint8Array)(import_prefix.BytePrefix.Number); const firstChunk = payload >> BigInt(56) & BigInt(255); outputStream = (0, import_uint8.concatUint8Arrays)([ outputStream, (0, import_uint8.hexStringToUint8Array)(firstChunk.toString(16).padStart(2, "0")) ]); payload = payload << BigInt(8) & BigInt("0xffffffffffffffff"); let byteToWrite = BigInt(0); let firstIteration = true; do { if (!firstIteration) { const padded2 = byteToWrite.toString(16).padStart(2, "0"); if (padded2 !== "00") { outputStream = (0, import_uint8.concatUint8Arrays)([outputStream, (0, import_uint8.hexStringToUint8Array)(padded2)]); } } else { firstIteration = false; } const shifted = payload >> BigInt(56) & BigInt(255); byteToWrite = shifted | BigInt(1); payload = payload << BigInt(7) & BigInt("0xffffffffffffffff"); } while (payload !== BigInt(0)); const lastChunk = byteToWrite & BigInt(254); const padded = lastChunk.toString(16).padStart(2, "0"); if (padded !== "00") { outputStream = (0, import_uint8.concatUint8Arrays)([outputStream, (0, import_uint8.hexStringToUint8Array)(padded)]); } return outputStream; } function encodeNumberAsUInt64BigInt(value) { const rawValueBits = getRawBitsBigInt(value); const mask = BigInt("0x8000000000000000"); return mask > rawValueBits ? rawValueBits ^ mask : ~rawValueBits + BigInt(1); } function getRawBitsBigInt(value) { const buffer = new ArrayBuffer(8); const view = new DataView(buffer); view.setFloat64(0, value); return BigInt("0x" + buf2hex(new Uint8Array(buffer))); } function buf2hex(arr) { return Array.from(arr).map((x) => x.toString(16).padStart(2, "0")).join(""); } function doubleToByteArrayBigInt(double) { const output = new Uint8Array(8); const lng = getRawBitsBigInt(double); for (let i = 0; i < 8; i++) { output[i] = Number(lng >> BigInt(i * 8) & BigInt(255)); } return output; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { doubleToByteArrayBigInt, writeNumberForBinaryEncodingBigInt });