2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-03-01 04:25:50 +00:00
|
|
|
// This module is browser compatible.
|
|
|
|
|
2020-04-15 14:38:05 +00:00
|
|
|
import { bytesToUuid } from "./_common.ts";
|
|
|
|
|
2021-02-02 03:16:27 +00:00
|
|
|
const UUID_RE =
|
|
|
|
/^[0-9a-f]{8}-[0-9a-f]{4}-1[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
2020-04-15 14:38:05 +00:00
|
|
|
|
2020-10-01 09:40:40 +00:00
|
|
|
/**
|
2024-05-29 05:47:42 +00:00
|
|
|
* Determines whether a string is a valid
|
|
|
|
* {@link https://www.rfc-editor.org/rfc/rfc9562.html#section-5.1 | UUIDv1}.
|
2023-12-30 22:40:48 +00:00
|
|
|
*
|
2024-05-29 05:47:42 +00:00
|
|
|
* @param id UUID value.
|
|
|
|
*
|
|
|
|
* @returns `true` if the string is a valid UUIDv1, otherwise `false`.
|
|
|
|
*
|
|
|
|
* @example Usage
|
2023-12-30 22:40:48 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { validate } from "@std/uuid/v1";
|
2024-05-29 05:47:42 +00:00
|
|
|
* import { assert, assertFalse } from "@std/assert";
|
2023-12-30 22:40:48 +00:00
|
|
|
*
|
2024-05-29 05:47:42 +00:00
|
|
|
* assert(validate("ea71fc60-a713-11ee-af61-8349da24f689"));
|
|
|
|
* assertFalse(validate("fac8c1e0-ad1a-4204-a0d0-8126ae84495d"));
|
2023-12-30 22:40:48 +00:00
|
|
|
* ```
|
2020-10-01 09:40:40 +00:00
|
|
|
*/
|
2020-04-15 14:38:05 +00:00
|
|
|
export function validate(id: string): boolean {
|
|
|
|
return UUID_RE.test(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
let _nodeId: number[];
|
|
|
|
let _clockseq: number;
|
|
|
|
|
|
|
|
let _lastMSecs = 0;
|
|
|
|
let _lastNSecs = 0;
|
|
|
|
|
2024-05-29 07:47:06 +00:00
|
|
|
/**
|
|
|
|
* Options for {@linkcode generate}.
|
|
|
|
*
|
|
|
|
* @deprecated This will be removed in 1.0.0. Use {@linkcode GenerateOptions}
|
|
|
|
* instead.
|
|
|
|
*/
|
|
|
|
export interface V1Options extends GenerateOptions {}
|
|
|
|
|
2024-05-29 05:47:42 +00:00
|
|
|
/** Options for {@linkcode generate}. */
|
2024-05-29 07:47:06 +00:00
|
|
|
export interface GenerateOptions {
|
2023-12-30 22:40:48 +00:00
|
|
|
/**
|
|
|
|
* An array of 6 bytes that represents a 48-bit IEEE 802 MAC address.
|
|
|
|
*
|
|
|
|
* @see {@link https://www.rfc-editor.org/rfc/rfc4122#section-4.1.6}
|
|
|
|
*/
|
2020-04-15 14:38:05 +00:00
|
|
|
node?: number[];
|
2023-12-30 22:40:48 +00:00
|
|
|
/**
|
|
|
|
* A 14-bit value used to avoid duplicates that could arise when the clock is
|
|
|
|
* set backwards in time or if the node ID changes (0 - 16383).
|
|
|
|
*
|
|
|
|
* @see {@link https://www.rfc-editor.org/rfc/rfc4122#section-4.1.5}
|
|
|
|
*/
|
2020-04-15 14:38:05 +00:00
|
|
|
clockseq?: number;
|
2023-12-30 22:40:48 +00:00
|
|
|
/**
|
|
|
|
* The number of milliseconds since the Unix epoch (January 1, 1970).
|
|
|
|
*
|
|
|
|
* @see {@link https://www.rfc-editor.org/rfc/rfc4122#section-4.1.4}
|
|
|
|
*/
|
2020-04-15 14:38:05 +00:00
|
|
|
msecs?: number;
|
2023-12-30 22:40:48 +00:00
|
|
|
/**
|
|
|
|
* The number of nanoseconds to add to {@linkcode V1Options.msecs}
|
|
|
|
* (0 - 10,000).
|
|
|
|
*
|
|
|
|
* @see {@link https://www.rfc-editor.org/rfc/rfc4122#section-4.1.4}
|
|
|
|
*/
|
2020-04-15 14:38:05 +00:00
|
|
|
nsecs?: number;
|
2023-12-30 22:40:48 +00:00
|
|
|
/** An array of 16 random bytes (0 - 255). */
|
2020-04-15 14:38:05 +00:00
|
|
|
random?: number[];
|
2023-12-30 22:40:48 +00:00
|
|
|
/**
|
|
|
|
* A function that returns an array of 16 random bytes (0 - 255).
|
|
|
|
* Alternative to {@linkcode V1Options.random}.
|
|
|
|
*/
|
2020-04-15 14:38:05 +00:00
|
|
|
rng?: () => number[];
|
2021-02-02 03:16:27 +00:00
|
|
|
}
|
2020-04-15 14:38:05 +00:00
|
|
|
|
2024-05-29 09:07:20 +00:00
|
|
|
/**
|
|
|
|
* Generates a
|
|
|
|
* {@link https://www.rfc-editor.org/rfc/rfc9562.html#section-5.1 | UUIDv1}.
|
|
|
|
*
|
|
|
|
* @param options Can use RFC time sequence values as overwrites.
|
|
|
|
* @param buf Can allow the UUID to be written in byte-form starting at the offset.
|
|
|
|
* @param offset Index to start writing on the UUID bytes in buffer.
|
|
|
|
*
|
|
|
|
* @returns Returns a UUIDv1 string or an array of 16 bytes.
|
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { generate, validate } from "@std/uuid/v1";
|
|
|
|
* import { assert } from "@std/assert/assert";
|
|
|
|
*
|
|
|
|
* const options = {
|
|
|
|
* node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
|
|
|
|
* clockseq: 0x1234,
|
|
|
|
* msecs: new Date("2011-11-01").getTime(),
|
|
|
|
* nsecs: 5678,
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* const uuid = generate(options) as string;
|
|
|
|
* assert(validate(uuid));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @deprecated This will be removed in 1.0.0. Use the other overload instead.
|
|
|
|
*/
|
|
|
|
export function generate(
|
|
|
|
options?: GenerateOptions,
|
|
|
|
buf?: number[],
|
|
|
|
offset?: number,
|
|
|
|
): string | number[];
|
2020-10-01 09:40:40 +00:00
|
|
|
/**
|
2024-05-29 05:47:42 +00:00
|
|
|
* Generates a
|
|
|
|
* {@link https://www.rfc-editor.org/rfc/rfc9562.html#section-5.1 | UUIDv1}.
|
|
|
|
*
|
|
|
|
* @param options Can use RFC time sequence values as overwrites.
|
|
|
|
* @param buf Can allow the UUID to be written in byte-form starting at the offset.
|
|
|
|
* @param offset Index to start writing on the UUID bytes in buffer.
|
2023-12-30 22:40:48 +00:00
|
|
|
*
|
2024-05-29 05:47:42 +00:00
|
|
|
* @returns Returns a UUIDv1 string or an array of 16 bytes.
|
|
|
|
*
|
|
|
|
* @example Usage
|
2023-12-30 22:40:48 +00:00
|
|
|
* ```ts
|
2024-05-29 05:47:42 +00:00
|
|
|
* import { generate, validate } from "@std/uuid/v1";
|
|
|
|
* import { assert } from "@std/assert/assert";
|
2023-12-30 22:40:48 +00:00
|
|
|
*
|
|
|
|
* const options = {
|
|
|
|
* node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
|
|
|
|
* clockseq: 0x1234,
|
|
|
|
* msecs: new Date("2011-11-01").getTime(),
|
|
|
|
* nsecs: 5678,
|
|
|
|
* };
|
|
|
|
*
|
2024-05-29 05:47:42 +00:00
|
|
|
* const uuid = generate(options) as string;
|
|
|
|
* assert(validate(uuid));
|
2023-12-30 22:40:48 +00:00
|
|
|
* ```
|
2020-10-01 09:40:40 +00:00
|
|
|
*/
|
2024-05-29 09:07:20 +00:00
|
|
|
export function generate(options?: GenerateOptions): string;
|
2020-04-15 14:38:05 +00:00
|
|
|
export function generate(
|
2024-05-29 07:47:06 +00:00
|
|
|
options: GenerateOptions = {},
|
2020-04-15 14:38:05 +00:00
|
|
|
buf?: number[],
|
2020-07-14 19:24:17 +00:00
|
|
|
offset?: number,
|
2020-04-15 14:38:05 +00:00
|
|
|
): string | number[] {
|
|
|
|
let i = (buf && offset) || 0;
|
2021-02-02 03:16:27 +00:00
|
|
|
const b = buf ?? [];
|
2020-04-15 14:38:05 +00:00
|
|
|
|
2021-02-02 03:16:27 +00:00
|
|
|
let { node = _nodeId, clockseq = _clockseq } = options;
|
2020-04-15 14:38:05 +00:00
|
|
|
|
2021-02-02 03:16:27 +00:00
|
|
|
if (node === undefined || clockseq === undefined) {
|
2020-11-03 15:19:29 +00:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2021-02-02 03:16:27 +00:00
|
|
|
const seedBytes: any = options.random ??
|
|
|
|
options.rng ??
|
2020-04-15 14:38:05 +00:00
|
|
|
crypto.getRandomValues(new Uint8Array(16));
|
2021-02-02 03:16:27 +00:00
|
|
|
|
|
|
|
if (node === undefined) {
|
2020-04-15 14:38:05 +00:00
|
|
|
node = _nodeId = [
|
|
|
|
seedBytes[0] | 0x01,
|
|
|
|
seedBytes[1],
|
|
|
|
seedBytes[2],
|
|
|
|
seedBytes[3],
|
|
|
|
seedBytes[4],
|
|
|
|
seedBytes[5],
|
|
|
|
];
|
|
|
|
}
|
2021-02-02 03:16:27 +00:00
|
|
|
|
|
|
|
if (clockseq === undefined) {
|
2020-04-15 14:38:05 +00:00
|
|
|
clockseq = _clockseq = ((seedBytes[6] << 8) | seedBytes[7]) & 0x3fff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 03:16:27 +00:00
|
|
|
let { msecs = new Date().getTime(), nsecs = _lastNSecs + 1 } = options;
|
2020-04-15 14:38:05 +00:00
|
|
|
|
|
|
|
const dt = msecs - _lastMSecs + (nsecs - _lastNSecs) / 10000;
|
|
|
|
|
|
|
|
if (dt < 0 && options.clockseq === undefined) {
|
|
|
|
clockseq = (clockseq + 1) & 0x3fff;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === undefined) {
|
|
|
|
nsecs = 0;
|
|
|
|
}
|
|
|
|
|
2021-02-02 03:16:27 +00:00
|
|
|
if (nsecs > 10000) {
|
2020-04-15 14:38:05 +00:00
|
|
|
throw new Error("Can't create more than 10M uuids/sec");
|
|
|
|
}
|
|
|
|
|
2024-03-10 21:59:30 +00:00
|
|
|
if (node.length !== 6) {
|
|
|
|
throw new Error(
|
|
|
|
"Cannot create UUID. The node option must be an array of 6 bytes",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:38:05 +00:00
|
|
|
_lastMSecs = msecs;
|
|
|
|
_lastNSecs = nsecs;
|
|
|
|
_clockseq = clockseq;
|
|
|
|
|
2023-12-30 22:40:48 +00:00
|
|
|
// We have to add this value because "msecs" here is the number of
|
|
|
|
// milliseconds since January 1, 1970, not since October 15, 1582.
|
|
|
|
// This is also the milliseconds from October 15, 1582 to January 1, 1970.
|
2020-04-15 14:38:05 +00:00
|
|
|
msecs += 12219292800000;
|
|
|
|
|
|
|
|
const tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
|
|
|
|
b[i++] = (tl >>> 24) & 0xff;
|
|
|
|
b[i++] = (tl >>> 16) & 0xff;
|
|
|
|
b[i++] = (tl >>> 8) & 0xff;
|
|
|
|
b[i++] = tl & 0xff;
|
|
|
|
|
|
|
|
const tmh = ((msecs / 0x100000000) * 10000) & 0xfffffff;
|
|
|
|
b[i++] = (tmh >>> 8) & 0xff;
|
|
|
|
b[i++] = tmh & 0xff;
|
|
|
|
|
|
|
|
b[i++] = ((tmh >>> 24) & 0xf) | 0x10;
|
|
|
|
b[i++] = (tmh >>> 16) & 0xff;
|
|
|
|
|
|
|
|
b[i++] = (clockseq >>> 8) | 0x80;
|
|
|
|
|
|
|
|
b[i++] = clockseq & 0xff;
|
|
|
|
|
|
|
|
for (let n = 0; n < 6; ++n) {
|
2024-03-10 21:59:30 +00:00
|
|
|
b[i + n] = node[n]!;
|
2020-04-15 14:38:05 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 03:16:27 +00:00
|
|
|
return buf ?? bytesToUuid(b);
|
2020-04-15 14:38:05 +00:00
|
|
|
}
|