[79a0317] | 1 | "use strict";
|
---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 3 | exports.updateV1State = updateV1State;
|
---|
| 4 | const rng_js_1 = require("./rng.js");
|
---|
| 5 | const stringify_js_1 = require("./stringify.js");
|
---|
| 6 | const _state = {};
|
---|
| 7 | function v1(options, buf, offset) {
|
---|
| 8 | let bytes;
|
---|
| 9 | const isV6 = options?._v6 ?? false;
|
---|
| 10 | if (options) {
|
---|
| 11 | const optionsKeys = Object.keys(options);
|
---|
| 12 | if (optionsKeys.length === 1 && optionsKeys[0] === '_v6') {
|
---|
| 13 | options = undefined;
|
---|
| 14 | }
|
---|
| 15 | }
|
---|
| 16 | if (options) {
|
---|
| 17 | bytes = v1Bytes(options.random ?? options.rng?.() ?? (0, rng_js_1.default)(), options.msecs, options.nsecs, options.clockseq, options.node, buf, offset);
|
---|
| 18 | }
|
---|
| 19 | else {
|
---|
| 20 | const now = Date.now();
|
---|
| 21 | const rnds = (0, rng_js_1.default)();
|
---|
| 22 | updateV1State(_state, now, rnds);
|
---|
| 23 | bytes = v1Bytes(rnds, _state.msecs, _state.nsecs, isV6 ? undefined : _state.clockseq, isV6 ? undefined : _state.node, buf, offset);
|
---|
| 24 | }
|
---|
| 25 | return buf ? bytes : (0, stringify_js_1.unsafeStringify)(bytes);
|
---|
| 26 | }
|
---|
| 27 | function updateV1State(state, now, rnds) {
|
---|
| 28 | state.msecs ??= -Infinity;
|
---|
| 29 | state.nsecs ??= 0;
|
---|
| 30 | if (now === state.msecs) {
|
---|
| 31 | state.nsecs++;
|
---|
| 32 | if (state.nsecs >= 10000) {
|
---|
| 33 | state.node = undefined;
|
---|
| 34 | state.nsecs = 0;
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
| 37 | else if (now > state.msecs) {
|
---|
| 38 | state.nsecs = 0;
|
---|
| 39 | }
|
---|
| 40 | else if (now < state.msecs) {
|
---|
| 41 | state.node = undefined;
|
---|
| 42 | }
|
---|
| 43 | if (!state.node) {
|
---|
| 44 | state.node = rnds.slice(10, 16);
|
---|
| 45 | state.node[0] |= 0x01;
|
---|
| 46 | state.clockseq = ((rnds[8] << 8) | rnds[9]) & 0x3fff;
|
---|
| 47 | }
|
---|
| 48 | state.msecs = now;
|
---|
| 49 | return state;
|
---|
| 50 | }
|
---|
| 51 | function v1Bytes(rnds, msecs, nsecs, clockseq, node, buf, offset = 0) {
|
---|
| 52 | if (!buf) {
|
---|
| 53 | buf = new Uint8Array(16);
|
---|
| 54 | offset = 0;
|
---|
| 55 | }
|
---|
| 56 | msecs ??= Date.now();
|
---|
| 57 | nsecs ??= 0;
|
---|
| 58 | clockseq ??= ((rnds[8] << 8) | rnds[9]) & 0x3fff;
|
---|
| 59 | node ??= rnds.slice(10, 16);
|
---|
| 60 | msecs += 12219292800000;
|
---|
| 61 | const tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
|
---|
| 62 | buf[offset++] = (tl >>> 24) & 0xff;
|
---|
| 63 | buf[offset++] = (tl >>> 16) & 0xff;
|
---|
| 64 | buf[offset++] = (tl >>> 8) & 0xff;
|
---|
| 65 | buf[offset++] = tl & 0xff;
|
---|
| 66 | const tmh = ((msecs / 0x100000000) * 10000) & 0xfffffff;
|
---|
| 67 | buf[offset++] = (tmh >>> 8) & 0xff;
|
---|
| 68 | buf[offset++] = tmh & 0xff;
|
---|
| 69 | buf[offset++] = ((tmh >>> 24) & 0xf) | 0x10;
|
---|
| 70 | buf[offset++] = (tmh >>> 16) & 0xff;
|
---|
| 71 | buf[offset++] = (clockseq >>> 8) | 0x80;
|
---|
| 72 | buf[offset++] = clockseq & 0xff;
|
---|
| 73 | for (let n = 0; n < 6; ++n) {
|
---|
| 74 | buf[offset++] = node[n];
|
---|
| 75 | }
|
---|
| 76 | return buf;
|
---|
| 77 | }
|
---|
| 78 | exports.default = v1;
|
---|