UNPKG

@deepkit/bson

Version:
80 lines 3 kB
"use strict"; /* * Deepkit Framework * Copyright (C) 2021 Deepkit UG, Marc J. Schmidt * * This program is free software: you can redistribute it and/or modify * it under the terms of the MIT License. * * You should have received a copy of the MIT License along with this program. */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ObjectId = exports.hexTable = exports.BSONError = void 0; const crypto = __importStar(require("@deepkit/crypto")); const core_1 = require("@deepkit/core"); const { randomBytes } = crypto; let PROCESS_UNIQUE = undefined; class BSONError extends core_1.CustomError { } exports.BSONError = BSONError; function getUnique() { if (PROCESS_UNIQUE) return PROCESS_UNIQUE; PROCESS_UNIQUE = randomBytes(5); return PROCESS_UNIQUE; } exports.hexTable = []; for (let i = 0; i < 256; i++) { exports.hexTable[i] = (i <= 15 ? '0' : '') + i.toString(16); } /** * Thin wrapper around the native type to allow to serialize it correctly * in types like t.any. */ class ObjectId { static generate(time) { if (!time) time = Math.ceil(Date.now() / 1000); const inc = (++ObjectId.index) % 0xffffff; const processUnique = getUnique(); return '' + exports.hexTable[(time >> 24) & 0xff] + exports.hexTable[(time >> 16) & 0xff] + exports.hexTable[(time >> 8) & 0xff] + exports.hexTable[time & 0xff] + exports.hexTable[processUnique[0]] + exports.hexTable[processUnique[1]] + exports.hexTable[processUnique[2]] + exports.hexTable[processUnique[3]] + exports.hexTable[processUnique[4]] + exports.hexTable[(inc >> 16) & 0xff] + exports.hexTable[(inc >> 8) & 0xff] + exports.hexTable[inc & 0xff]; } } exports.ObjectId = ObjectId; ObjectId.index = Math.ceil(Math.random() & 0xffffff); //# sourceMappingURL=model.js.map