@needle-tools/addressing 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Readme.md +69 -89
- package/package.json +39 -35
- package/src/address.ts +106 -106
- package/src/anchor.ts +1 -16
- package/src/gltf.ts +89 -71
- package/src/hash.ts +58 -113
- package/src/id.ts +45 -32
- package/src/index.ts +2 -2
- package/src/resolve.ts +172 -172
- package/src/uuid.ts +48 -48
package/src/uuid.ts
CHANGED
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
import { sha1, toHex } from "./hash.js";
|
|
2
|
-
|
|
3
|
-
/** RFC 4122 URL namespace — the namespace needle-cloud's `ensureUUID` uses (`uuidv5.URL`). */
|
|
4
|
-
export const UUID_NAMESPACE_URL = "6ba7b811-9dad-11d1-80b4-00c04fd430c8";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* RFC 4122 version-5 (SHA-1, name-based) UUID. Byte-identical to the `uuid` npm
|
|
8
|
-
* package's `v5(name, namespace)`.
|
|
9
|
-
*/
|
|
10
|
-
export function uuidv5(name: string, namespace: string = UUID_NAMESPACE_URL): string {
|
|
11
|
-
const ns = parseUuidBytes(namespace);
|
|
12
|
-
const nameBytes = new TextEncoder().encode(name);
|
|
13
|
-
const input = new Uint8Array(ns.length + nameBytes.length);
|
|
14
|
-
input.set(ns);
|
|
15
|
-
input.set(nameBytes, ns.length);
|
|
16
|
-
const hash = sha1(input).slice(0, 16);
|
|
17
|
-
hash[6] = (hash[6] & 0x0f) | 0x50; // version 5
|
|
18
|
-
hash[8] = (hash[8] & 0x3f) | 0x80; // RFC 4122 variant
|
|
19
|
-
return formatUuid(hash);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Random UUID for content that has no source file to be deterministic against
|
|
24
|
-
* (scratch content created in an editor). Uses the platform's crypto when available.
|
|
25
|
-
*/
|
|
26
|
-
export function randomUuid(): string {
|
|
27
|
-
const cryptoObj = globalThis.crypto;
|
|
28
|
-
if (cryptoObj?.randomUUID) return cryptoObj.randomUUID();
|
|
29
|
-
const bytes = new Uint8Array(16);
|
|
30
|
-
if (cryptoObj?.getRandomValues) cryptoObj.getRandomValues(bytes);
|
|
31
|
-
else for (let i = 0; i < 16; i++) bytes[i] = Math.floor(Math.random() * 256);
|
|
32
|
-
bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
|
33
|
-
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
34
|
-
return formatUuid(bytes);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function parseUuidBytes(uuid: string): Uint8Array {
|
|
38
|
-
const hex = uuid.replace(/-/g, "");
|
|
39
|
-
if (hex.length !== 32) throw new Error(`Invalid UUID: ${uuid}`);
|
|
40
|
-
const bytes = new Uint8Array(16);
|
|
41
|
-
for (let i = 0; i < 16; i++) bytes[i] = parseInt(hex.substring(i * 2, i * 2 + 2), 16);
|
|
42
|
-
return bytes;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function formatUuid(bytes: Uint8Array): string {
|
|
46
|
-
const hex = toHex(bytes);
|
|
47
|
-
return `${hex.substring(0, 8)}-${hex.substring(8, 12)}-${hex.substring(12, 16)}-${hex.substring(16, 20)}-${hex.substring(20)}`;
|
|
48
|
-
}
|
|
1
|
+
import { sha1, toHex } from "./hash.js";
|
|
2
|
+
|
|
3
|
+
/** RFC 4122 URL namespace — the namespace needle-cloud's `ensureUUID` uses (`uuidv5.URL`). */
|
|
4
|
+
export const UUID_NAMESPACE_URL = "6ba7b811-9dad-11d1-80b4-00c04fd430c8";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* RFC 4122 version-5 (SHA-1, name-based) UUID. Byte-identical to the `uuid` npm
|
|
8
|
+
* package's `v5(name, namespace)`.
|
|
9
|
+
*/
|
|
10
|
+
export function uuidv5(name: string, namespace: string = UUID_NAMESPACE_URL): string {
|
|
11
|
+
const ns = parseUuidBytes(namespace);
|
|
12
|
+
const nameBytes = new TextEncoder().encode(name);
|
|
13
|
+
const input = new Uint8Array(ns.length + nameBytes.length);
|
|
14
|
+
input.set(ns);
|
|
15
|
+
input.set(nameBytes, ns.length);
|
|
16
|
+
const hash = sha1(input).slice(0, 16);
|
|
17
|
+
hash[6] = (hash[6] & 0x0f) | 0x50; // version 5
|
|
18
|
+
hash[8] = (hash[8] & 0x3f) | 0x80; // RFC 4122 variant
|
|
19
|
+
return formatUuid(hash);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Random UUID for content that has no source file to be deterministic against
|
|
24
|
+
* (scratch content created in an editor). Uses the platform's crypto when available.
|
|
25
|
+
*/
|
|
26
|
+
export function randomUuid(): string {
|
|
27
|
+
const cryptoObj = globalThis.crypto;
|
|
28
|
+
if (cryptoObj?.randomUUID) return cryptoObj.randomUUID();
|
|
29
|
+
const bytes = new Uint8Array(16);
|
|
30
|
+
if (cryptoObj?.getRandomValues) cryptoObj.getRandomValues(bytes);
|
|
31
|
+
else for (let i = 0; i < 16; i++) bytes[i] = Math.floor(Math.random() * 256);
|
|
32
|
+
bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
|
33
|
+
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
34
|
+
return formatUuid(bytes);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function parseUuidBytes(uuid: string): Uint8Array {
|
|
38
|
+
const hex = uuid.replace(/-/g, "");
|
|
39
|
+
if (hex.length !== 32) throw new Error(`Invalid UUID: ${uuid}`);
|
|
40
|
+
const bytes = new Uint8Array(16);
|
|
41
|
+
for (let i = 0; i < 16; i++) bytes[i] = parseInt(hex.substring(i * 2, i * 2 + 2), 16);
|
|
42
|
+
return bytes;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function formatUuid(bytes: Uint8Array): string {
|
|
46
|
+
const hex = toHex(bytes);
|
|
47
|
+
return `${hex.substring(0, 8)}-${hex.substring(8, 12)}-${hex.substring(12, 16)}-${hex.substring(16, 20)}-${hex.substring(20)}`;
|
|
48
|
+
}
|