2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-04-25 02:03:15 +00:00
|
|
|
// This module is browser compatible.
|
|
|
|
|
|
|
|
import { bytesToUuid, uuidToBytes } from "./_common.ts";
|
2024-04-29 02:57:30 +00:00
|
|
|
import { concat } from "@std/bytes/concat";
|
|
|
|
import { crypto } from "@std/crypto/crypto";
|
2024-05-29 09:09:30 +00:00
|
|
|
import { validate as validateCommon } from "./common.ts";
|
2023-04-25 02:03:15 +00:00
|
|
|
|
|
|
|
const UUID_RE =
|
|
|
|
/^[0-9a-f]{8}-[0-9a-f]{4}-[3][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
|
|
|
|
|
|
/**
|
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.3 | UUIDv3}.
|
2023-04-25 02:03:15 +00:00
|
|
|
*
|
2024-05-29 05:47:42 +00:00
|
|
|
* @param id UUID value.
|
|
|
|
*
|
|
|
|
* @returns `true` if the string is a valid UUIDv3, otherwise `false`.
|
|
|
|
*
|
|
|
|
* @example Usage
|
2023-04-25 02:03:15 +00:00
|
|
|
* ```ts
|
2024-05-29 05:47:42 +00:00
|
|
|
* import { validate } from "@std/uuid/v3";
|
|
|
|
* import { assert, assertFalse } from "@std/assert";
|
2023-04-25 02:03:15 +00:00
|
|
|
*
|
2024-05-29 05:47:42 +00:00
|
|
|
* assert(validate("22fe6191-c161-3d86-a432-a81f343eda08"));
|
|
|
|
* assertFalse(validate("this-is-not-a-uuid"));
|
2023-04-25 02:03:15 +00:00
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export function validate(id: string): boolean {
|
|
|
|
return UUID_RE.test(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-05-29 05:47:42 +00:00
|
|
|
* Generates a
|
|
|
|
* {@link https://www.rfc-editor.org/rfc/rfc9562.html#section-5.3 | UUIDv3}.
|
2023-04-25 02:03:15 +00:00
|
|
|
*
|
2024-05-29 05:47:42 +00:00
|
|
|
* @param namespace The namespace to use, encoded as a UUID.
|
|
|
|
* @param data The data to hash to calculate the MD5 digest for the UUID.
|
2023-04-25 02:03:15 +00:00
|
|
|
*
|
2024-05-29 05:47:42 +00:00
|
|
|
* @returns A UUIDv3 string.
|
2023-04-25 02:03:15 +00:00
|
|
|
*
|
2024-05-29 09:09:30 +00:00
|
|
|
* @throws {TypeError} If the namespace is not a valid UUID.
|
|
|
|
*
|
2024-05-29 05:47:42 +00:00
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { NAMESPACE_URL } from "@std/uuid/constants";
|
|
|
|
* import { generate, validate } from "@std/uuid/v3";
|
|
|
|
* import { assert } from "@std/assert";
|
2023-04-25 02:03:15 +00:00
|
|
|
*
|
2024-05-29 05:47:42 +00:00
|
|
|
* const data = new TextEncoder().encode("python.org");
|
|
|
|
* const uuid = await generate(NAMESPACE_URL, data);
|
|
|
|
*
|
|
|
|
* assert(validate(uuid));
|
|
|
|
* ```
|
2023-04-25 02:03:15 +00:00
|
|
|
*/
|
|
|
|
export async function generate(
|
|
|
|
namespace: string,
|
|
|
|
data: Uint8Array,
|
|
|
|
): Promise<string> {
|
2024-05-29 09:09:30 +00:00
|
|
|
if (!validateCommon(namespace)) {
|
2024-08-26 04:36:01 +00:00
|
|
|
throw new TypeError(`Cannot generate UUID: invalid namespace ${namespace}`);
|
2024-05-29 09:09:30 +00:00
|
|
|
}
|
2024-05-31 11:10:04 +00:00
|
|
|
const namespaceBytes = uuidToBytes(namespace);
|
|
|
|
const toHash = concat([namespaceBytes, data]);
|
2023-04-25 02:03:15 +00:00
|
|
|
const buffer = await crypto.subtle.digest("MD5", toHash);
|
|
|
|
const bytes = new Uint8Array(buffer);
|
|
|
|
|
2024-03-10 21:59:30 +00:00
|
|
|
bytes[6] = (bytes[6]! & 0x0f) | 0x30;
|
|
|
|
bytes[8] = (bytes[8]! & 0x3f) | 0x80;
|
2023-04-25 02:03:15 +00:00
|
|
|
|
|
|
|
return bytesToUuid(bytes);
|
|
|
|
}
|