@deepkit/bson
Version:
Deepkit BSON parser
58 lines • 2.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObjectId = exports.hexTable = exports.BSONError = void 0;
/*
* 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.
*/
const core_1 = require("@deepkit/core");
let PROCESS_UNIQUE = undefined;
class BSONError extends core_1.CustomError {
}
exports.BSONError = BSONError;
BSONError.__type = [() => core_1.CustomError, 'BSONError', 'P7!5w"'];
function getUnique() {
if (PROCESS_UNIQUE)
return PROCESS_UNIQUE;
PROCESS_UNIQUE = crypto.getRandomValues(new Uint8Array(5));
return PROCESS_UNIQUE;
}
getUnique.__type = ['getUnique', 'PW/!'];
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);
ObjectId.__type = ['index', function () { return Math.ceil(Math.random() * 0xffffff); }, 'time', 'generate', 'ObjectId', '\'3!s>"P\'2#8&0$s5w%'];
//# sourceMappingURL=model.js.map