@openlfcp/core 0.1.0 → 0.1.2

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 CHANGED
@@ -3,7 +3,7 @@
3
3
  Identifiers, shared protocol types, errors and byte helpers used by every other `@openlfcp/*` package: Resource, Principal, Control Record and Data Unit IDs, uint64 sequences and epochs, `LfcpError` with the LFCP wire codes, hex and base64url helpers.
4
4
 
5
5
  ```sh
6
- npm install @openlfcp/core@next
6
+ npm install @openlfcp/core
7
7
  ```
8
8
 
9
9
  ESM only; Node.js 24 or later (browsers and Electron for every package except `@openlfcp/storage-node`).
@@ -12,9 +12,9 @@ ESM only; Node.js 24 or later (browsers and Electron for every package except `@
12
12
 
13
13
  Part of the OpenLFCP TypeScript SDK, which implements the OpenLFCP MVP 0.1
14
14
  subset of [LFCP-WIRE-01](https://github.com/openlfcp/spec/blob/main/wire/LFCP-WIRE-01.md)
15
- at `mvp-0.1-baseline.8`, not every deferred WIRE-01 feature. It does not
15
+ at `mvp-0.1-baseline.9`, not every deferred WIRE-01 feature. It does not
16
16
  claim full LFCP-WIRE-01 conformance. This is the MVP 0.1 release
17
- (`0.1.0`, npm dist-tag `latest`). Until 1.0, minor versions may change APIs.
17
+ (`0.1.2`, npm dist-tag `latest`). Until 1.0, minor versions may change APIs.
18
18
 
19
19
  ## Links
20
20
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openlfcp/core",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Identifiers, shared protocol types, errors and byte helpers for LFCP.",
5
5
  "keywords": [
6
6
  "openlfcp",
package/dist/bytes.d.ts DELETED
@@ -1,18 +0,0 @@
1
- /** Lowercase hexadecimal, two digits per byte. */
2
- export declare function toHex(bytes: Uint8Array): string;
3
- /**
4
- * Parses lowercase, even-length hexadecimal. Upper case, a `0x` prefix and
5
- * odd lengths are rejected with `INVALID_HEX`.
6
- */
7
- export declare function fromHex(hex: string): Uint8Array;
8
- /** Unpadded base64url (RFC 4648 §5), the encoding LFCP uses in URIs and references. */
9
- export declare function toBase64url(bytes: Uint8Array): string;
10
- /**
11
- * Parses canonical unpadded base64url. Rejects padding, characters outside
12
- * the URL-safe alphabet, a length of 1 modulo 4 and non-zero trailing bits,
13
- * so every byte string has exactly one accepted encoding.
14
- */
15
- export declare function fromBase64url(text: string): Uint8Array;
16
- /** Byte-for-byte equality. Not constant-time: do not use it to compare secrets. */
17
- export declare function bytesEqual(a: Uint8Array, b: Uint8Array): boolean;
18
- //# sourceMappingURL=bytes.d.ts.map
package/dist/bytes.js DELETED
@@ -1,99 +0,0 @@
1
- import { LfcpError } from "./errors.js";
2
- const HEX = "0123456789abcdef";
3
- const B64URL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
4
- const B64URL_VALUE = new Map([...B64URL].map((c, i) => [c, i]));
5
- /** Lowercase hexadecimal, two digits per byte. */
6
- export function toHex(bytes) {
7
- let out = "";
8
- for (const b of bytes)
9
- out += HEX.charAt(b >> 4) + HEX.charAt(b & 0x0f);
10
- return out;
11
- }
12
- /**
13
- * Parses lowercase, even-length hexadecimal. Upper case, a `0x` prefix and
14
- * odd lengths are rejected with `INVALID_HEX`.
15
- */
16
- export function fromHex(hex) {
17
- if (!/^(?:[0-9a-f]{2})*$/.test(hex)) {
18
- throw new LfcpError("INVALID_HEX", "expected lowercase hexadecimal with an even number of digits");
19
- }
20
- const out = new Uint8Array(hex.length / 2);
21
- for (let i = 0; i < out.length; i += 1)
22
- out[i] = Number.parseInt(hex.slice(2 * i, 2 * i + 2), 16);
23
- return out;
24
- }
25
- /** Unpadded base64url (RFC 4648 §5), the encoding LFCP uses in URIs and references. */
26
- export function toBase64url(bytes) {
27
- let out = "";
28
- let i = 0;
29
- for (; i + 3 <= bytes.length; i += 3) {
30
- const n = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
31
- out +=
32
- B64URL.charAt(n >> 18) +
33
- B64URL.charAt((n >> 12) & 63) +
34
- B64URL.charAt((n >> 6) & 63) +
35
- B64URL.charAt(n & 63);
36
- }
37
- const rest = bytes.length - i;
38
- if (rest === 1) {
39
- const n = bytes[i] << 16;
40
- out += B64URL.charAt(n >> 18) + B64URL.charAt((n >> 12) & 63);
41
- }
42
- else if (rest === 2) {
43
- const n = (bytes[i] << 16) | (bytes[i + 1] << 8);
44
- out += B64URL.charAt(n >> 18) + B64URL.charAt((n >> 12) & 63) + B64URL.charAt((n >> 6) & 63);
45
- }
46
- return out;
47
- }
48
- /**
49
- * Parses canonical unpadded base64url. Rejects padding, characters outside
50
- * the URL-safe alphabet, a length of 1 modulo 4 and non-zero trailing bits,
51
- * so every byte string has exactly one accepted encoding.
52
- */
53
- export function fromBase64url(text) {
54
- const fail = (why) => {
55
- throw new LfcpError("INVALID_BASE64URL", `not canonical unpadded base64url: ${why}`);
56
- };
57
- const rem = text.length % 4;
58
- if (rem === 1)
59
- fail("length is 1 modulo 4");
60
- const values = [];
61
- // The input may be a secret (a stored key, an invitation fragment): an
62
- // error names the position of a bad character, never the character.
63
- let i = 0;
64
- for (const c of text) {
65
- const v = B64URL_VALUE.get(c);
66
- if (v === undefined)
67
- fail(c === "=" ? "padding is not allowed" : `a character outside the alphabet at ${i}`);
68
- values.push(v);
69
- i += 1;
70
- }
71
- // The last character of a 2- or 3-character group carries 4 or 2 bits that
72
- // are not part of any byte; they must be zero (RFC 4648 §3.5).
73
- const last = values[values.length - 1] ?? 0;
74
- if ((rem === 2 && (last & 0x0f) !== 0) || (rem === 3 && (last & 0x03) !== 0))
75
- fail("non-zero trailing bits");
76
- const out = new Uint8Array((values.length * 6) >> 3);
77
- let acc = 0;
78
- let bits = 0;
79
- let o = 0;
80
- for (const v of values) {
81
- acc = ((acc << 6) | v) & 0xffff;
82
- bits += 6;
83
- if (bits >= 8) {
84
- bits -= 8;
85
- out[o++] = (acc >> bits) & 0xff;
86
- }
87
- }
88
- return out;
89
- }
90
- /** Byte-for-byte equality. Not constant-time: do not use it to compare secrets. */
91
- export function bytesEqual(a, b) {
92
- if (a.length !== b.length)
93
- return false;
94
- for (let i = 0; i < a.length; i += 1)
95
- if (a[i] !== b[i])
96
- return false;
97
- return true;
98
- }
99
- //# sourceMappingURL=bytes.js.map
package/dist/errors.d.ts DELETED
@@ -1,152 +0,0 @@
1
- /**
2
- * Validation codes raised by the SDK while checking local input.
3
- *
4
- * These are SDK-internal codes, not the LFCP-WIRE-01 §62 wire error codes
5
- * that are sent in NACK and ERROR messages. At the wire boundary, every
6
- * `CBOR_*` failure of a received persistent object surfaces as
7
- * `MALFORMED_MESSAGE` (LFCP-WIRE-01 §5.2).
8
- */
9
- export type LfcpErrorCode = "INVALID_LENGTH" | "INVALID_HEX" | "INVALID_BASE64URL" | "INVALID_UUIDV7" | "NO_SECURE_RANDOM"
10
- /** Valid CBOR, but not the deterministic encoding (non-shortest head, unsorted map keys). */
11
- | "CBOR_NON_CANONICAL"
12
- /** A map contains the same key twice. */
13
- | "CBOR_DUPLICATE_KEY"
14
- /** An indefinite-length item or a break code. */
15
- | "CBOR_INDEFINITE_LENGTH"
16
- /** A type LFCP does not use: tags, floats, undefined, other simple values, reserved encodings, unsupported JS values. */
17
- | "CBOR_UNSUPPORTED_TYPE"
18
- /** An integer outside the CBOR range -2^64 .. 2^64-1, or an unsafe JS number. */
19
- | "CBOR_OUT_OF_RANGE"
20
- /** The input ends inside an item. */
21
- | "CBOR_TRUNCATED"
22
- /** Bytes remain after the single top-level item. */
23
- | "CBOR_TRAILING_BYTES"
24
- /** A text string that is not well-formed UTF-8 (or a JS string with lone surrogates). */
25
- | "CBOR_INVALID_UTF8"
26
- /** Nesting deeper than the SDK's safety limit. */
27
- | "CBOR_TOO_DEEP"
28
- /** A cryptographic operation failed (e.g. a low-order X25519 public key). Never carries key bytes. */
29
- | "CRYPTO_FAILURE"
30
- /** A Principal Descriptor has the wrong shape, field set, key lengths or encoding (LFCP-WIRE-01 §7). */
31
- | "INVALID_PRINCIPAL_DESCRIPTOR"
32
- /** A Principal Descriptor's ID is not the §7 hash of its public keys. */
33
- | "PRINCIPAL_ID_MISMATCH"
34
- /**
35
- * A signed object is not the canonical LFCP COSE_Sign1 shape (LFCP-WIRE-01 §10):
36
- * tagged, wrong arity, extra or missing protected parameters, non-empty
37
- * unprotected header, detached payload or a non-64-byte signature. Surfaces
38
- * on the wire as MALFORMED_MESSAGE.
39
- */
40
- | "COSE_MALFORMED"
41
- /** The signing key does not belong to the Principal Descriptor it is used with. */
42
- | "COSE_SIGNER_MISMATCH"
43
- /**
44
- * Deterministic CBOR that does not have the structure a Wire rule requires:
45
- * wrong field set, field type or byte-string length, or a broken structural
46
- * rule (actor sequence 0 §8, a non-canonical actor-have or frontier §28.1,
47
- * §28.2, Genesis not at control_seq 0 with a null link §13.1). Surfaces on
48
- * the wire as MALFORMED_MESSAGE.
49
- */
50
- | "INVALID_STRUCTURE"
51
- /**
52
- * A well-formed value that LFCP reserves but this version does not define:
53
- * a core Control Record type 9–31. §14: "Unknown core Control Record types
54
- * MUST cause validation failure, with INVALID_CONTROL_CHAIN."
55
- */
56
- | "UNSUPPORTED_VALUE"
57
- /**
58
- * An integer outside the range a construction requires: a Data Epoch or
59
- * uint64 value outside 0..2^64-1, an actor sequence outside 1..2^64-1, or
60
- * a number that is not a safe integer. Values are never truncated.
61
- * SDK-local; decoded wire values are range-checked by the decoders.
62
- */
63
- | "OUT_OF_RANGE"
64
- /**
65
- * A local attempt to use an actor sequence twice for one (Resource,
66
- * Principal) (LFCP-WIRE-01 §8, §12: nonce reuse). SDK-local; never sent.
67
- */
68
- | "SEQUENCE_REUSE"
69
- /**
70
- * A local transaction that would produce a Shared Objects change over the
71
- * change expansion limits (SHARED-OBJECTS-PROFILE-01 §11.1). Nothing was
72
- * written; the application splits the transaction (§12). SDK-local.
73
- */
74
- | "CHANGE_TOO_LARGE"
75
- /**
76
- * A Control Chain that is not one valid linear chain (LFCP-WIRE-01 §13,
77
- * §13.1): no Genesis, a sequence that is not previous + 1, a link that is
78
- * not the previous record ID, a different Resource, or a record that
79
- * links to nothing. INVALID_CONTROL_CHAIN on the wire (§13.1).
80
- */
81
- | "INVALID_CONTROL_CHAIN"
82
- /**
83
- * A record whose issuer cannot be resolved to a Principal Descriptor, or
84
- * an object that references a Control Head the receiver does not have.
85
- * MISSING_DEPENDENCY on the wire (§13.1); the receiver may fetch and retry.
86
- */
87
- | "MISSING_DEPENDENCY"
88
- /**
89
- * Something this implementation does not support by scope: a Control
90
- * Chain containing a Coordinator Recovery (7) or Resource Tombstone (8)
91
- * record (MVP-0.1-PROTOCOL-SCOPE §4, DV1). PROTOCOL_UNSUPPORTED on the wire.
92
- */
93
- | "PROTOCOL_UNSUPPORTED"
94
- /** A signed object whose kid or signature is wrong for its expected signer (G1/N2). INVALID_SIGNATURE on the wire. */
95
- | "INVALID_SIGNATURE"
96
- /** A record the authorization policy (LFCP-021) refuses. AUTHORIZATION_FAILED on the wire. */
97
- | "AUTHORIZATION_FAILED"
98
- /**
99
- * Shared Objects state that violates SHARED-OBJECTS-PROFILE-01 (§74.1):
100
- * reported with one §74.1 diagnostic per failure. A profile-level code,
101
- * never an LFCP Wire error code.
102
- */
103
- | "PROFILE_INVALID"
104
- /**
105
- * A Resource whose Data Profile this client does not implement (§27):
106
- * its plaintext is never applied. PROFILE_UNSUPPORTED on the wire (§62).
107
- */
108
- | "PROFILE_UNSUPPORTED"
109
- /**
110
- * Two concurrent Shared Objects under one Object ID (SHARED-OBJECTS-
111
- * PROFILE-01 §21). A profile error of its own, not PROFILE_INVALID.
112
- */
113
- | "OBJECT_ID_COLLISION"
114
- /**
115
- * A received second, different change or unit for one (resource, actor,
116
- * sequence) (§26.2). ACTOR_EQUIVOCATION on the wire (§62). A local attempt
117
- * to reuse a sequence is SEQUENCE_REUSE instead.
118
- */
119
- | "ACTOR_EQUIVOCATION"
120
- /**
121
- * The keys given to open a Key Package are not those of the Principal it
122
- * names (§25: recipient binding). Client-local, no wire code (ADR 0001 N5).
123
- */
124
- | "KEY_PACKAGE_RECIPIENT_MISMATCH"
125
- /** An LFCP message above the size limit in force (§31). MESSAGE_TOO_LARGE on the wire. */
126
- | "MESSAGE_TOO_LARGE"
127
- /** A Data Profile codec used with a Resource of another data_profile (§15, §27). SDK-local misuse. */
128
- | "DATA_PROFILE_MISMATCH"
129
- /**
130
- * A Data Unit that fails ChaCha20-Poly1305 authentication (§26.3). Only a
131
- * client holding the DEK detects it; client-local, no wire code (ADR 0001 N3).
132
- */
133
- | "AEAD_AUTHENTICATION_FAILED"
134
- /** A Key Package does not open (HPKE failure). Client-local, no wire code (N5). */
135
- | "KEY_PACKAGE_OPEN_FAILED"
136
- /**
137
- * An opened DEK does not match the epoch's DEK commitment, or the
138
- * plaintext is not 32 bytes (§25.2). Client-local, no wire code (N5).
139
- */
140
- | "DEK_COMMITMENT_MISMATCH"
141
- /**
142
- * An invitation URI or invitation secret that is not the §18.2 form, or
143
- * a secret whose recomputed Invitation Principal is not the subject of
144
- * the referenced invitation grant. Client-local, never sent.
145
- */
146
- | "INVALID_INVITATION";
147
- /** Error with a stable machine-readable `code`; the message is for humans only. */
148
- export declare class LfcpError extends Error {
149
- readonly code: LfcpErrorCode;
150
- constructor(code: LfcpErrorCode, message: string);
151
- }
152
- //# sourceMappingURL=errors.d.ts.map
package/dist/errors.js DELETED
@@ -1,10 +0,0 @@
1
- /** Error with a stable machine-readable `code`; the message is for humans only. */
2
- export class LfcpError extends Error {
3
- code;
4
- constructor(code, message) {
5
- super(message);
6
- this.name = "LfcpError";
7
- this.code = code;
8
- }
9
- }
10
- //# sourceMappingURL=errors.js.map
package/dist/ids.d.ts DELETED
@@ -1,46 +0,0 @@
1
- /**
2
- * 32-byte LFCP identifiers, as raw bytes.
3
- *
4
- * Each kind is a distinct branded Uint8Array, so one kind cannot be passed
5
- * where another is expected. The constructors check the length and copy the
6
- * input, so later writes to the caller's buffer cannot change a validated
7
- * identifier. The returned array is still a Uint8Array: treat it as
8
- * read-only.
9
- */
10
- declare const brand: unique symbol;
11
- type Id32<Kind extends string> = Uint8Array & {
12
- readonly [brand]: Kind;
13
- };
14
- /** LFCP-WIRE-01 §6: 32 random bytes. */
15
- export type ResourceId = Id32<"ResourceId">;
16
- /** LFCP-WIRE-01 §7: SHA-256 over the Principal's public keys (derivation is LFCP-014). */
17
- export type PrincipalId = Id32<"PrincipalId">;
18
- /** LFCP-WIRE-01 §5.3: a SHA-256 value encoded as 32 bytes. */
19
- export type Hash32 = Id32<"Hash32">;
20
- /** LFCP-WIRE-01 §13, §10.6: SHA-256 of a Control Record's exact COSE_Sign1 bytes. */
21
- export type ControlRecordId = Id32<"ControlRecordId">;
22
- /** LFCP-WIRE-01 §26, §10.6: SHA-256 of a Data Unit's exact COSE_Sign1 bytes. */
23
- export type DataUnitId = Id32<"DataUnitId">;
24
- export declare const ID32_LENGTH = 32;
25
- export declare const resourceId: (bytes: Uint8Array) => ResourceId;
26
- /**
27
- * A new Resource ID: 32 bytes from the platform CSPRNG (LFCP-WIRE-01 §6).
28
- * It never encodes a hostname, owner, account, path or time, and nothing
29
- * derives it from Genesis content.
30
- */
31
- export declare const generateResourceId: () => ResourceId;
32
- export declare const principalId: (bytes: Uint8Array) => PrincipalId;
33
- export declare const hash32: (bytes: Uint8Array) => Hash32;
34
- export declare const controlRecordId: (bytes: Uint8Array) => ControlRecordId;
35
- export declare const dataUnitId: (bytes: Uint8Array) => DataUnitId;
36
- /** Equality of two identifiers of the same kind. */
37
- export declare function idEquals<T extends Id32<string>>(a: T, b: T): boolean;
38
- /**
39
- * Orders Principal IDs by their raw bytes, compared as unsigned bytes, as
40
- * LFCP-WIRE-01 §28.2 requires for canonical frontier entries. This is byte
41
- * order only; it carries no meaning of time, causality or precedence.
42
- * Returns a negative number, zero or a positive number like Array.sort expects.
43
- */
44
- export declare function compareCanonicalFrontierOrder(a: PrincipalId, b: PrincipalId): number;
45
- export {};
46
- //# sourceMappingURL=ids.d.ts.map
package/dist/ids.js DELETED
@@ -1,41 +0,0 @@
1
- import { bytesEqual } from "./bytes.js";
2
- import { LfcpError } from "./errors.js";
3
- import { secureRandom } from "./random.js";
4
- export const ID32_LENGTH = 32;
5
- function id32(kind, bytes) {
6
- if (!(bytes instanceof Uint8Array) || bytes.length !== ID32_LENGTH) {
7
- const got = bytes instanceof Uint8Array ? `${bytes.length} bytes` : typeof bytes;
8
- throw new LfcpError("INVALID_LENGTH", `${kind} must be exactly ${ID32_LENGTH} bytes, got ${got}`);
9
- }
10
- return Uint8Array.from(bytes);
11
- }
12
- export const resourceId = (bytes) => id32("ResourceId", bytes);
13
- /**
14
- * A new Resource ID: 32 bytes from the platform CSPRNG (LFCP-WIRE-01 §6).
15
- * It never encodes a hostname, owner, account, path or time, and nothing
16
- * derives it from Genesis content.
17
- */
18
- export const generateResourceId = () => resourceId(secureRandom(ID32_LENGTH));
19
- export const principalId = (bytes) => id32("PrincipalId", bytes);
20
- export const hash32 = (bytes) => id32("Hash32", bytes);
21
- export const controlRecordId = (bytes) => id32("ControlRecordId", bytes);
22
- export const dataUnitId = (bytes) => id32("DataUnitId", bytes);
23
- /** Equality of two identifiers of the same kind. */
24
- export function idEquals(a, b) {
25
- return bytesEqual(a, b);
26
- }
27
- /**
28
- * Orders Principal IDs by their raw bytes, compared as unsigned bytes, as
29
- * LFCP-WIRE-01 §28.2 requires for canonical frontier entries. This is byte
30
- * order only; it carries no meaning of time, causality or precedence.
31
- * Returns a negative number, zero or a positive number like Array.sort expects.
32
- */
33
- export function compareCanonicalFrontierOrder(a, b) {
34
- for (let i = 0; i < ID32_LENGTH; i += 1) {
35
- const d = a[i] - b[i];
36
- if (d !== 0)
37
- return d;
38
- }
39
- return 0;
40
- }
41
- //# sourceMappingURL=ids.js.map
package/dist/index.d.ts DELETED
@@ -1,9 +0,0 @@
1
- /** Name of this package. */
2
- export declare const PACKAGE = "@openlfcp/core";
3
- export { bytesEqual, fromBase64url, fromHex, toBase64url, toHex } from "./bytes.js";
4
- export { LfcpError, type LfcpErrorCode } from "./errors.js";
5
- export { type ControlRecordId, compareCanonicalFrontierOrder, controlRecordId, type DataUnitId, dataUnitId, generateResourceId, type Hash32, hash32, ID32_LENGTH, idEquals, type PrincipalId, principalId, type ResourceId, resourceId, } from "./ids.js";
6
- export { formatObjectId, type GenerateObjectIdOptions, generateObjectId, isObjectId, type ObjectId, parseObjectId, } from "./object-id.js";
7
- export { secureRandom } from "./random.js";
8
- export { type ActorSequence, actorSequence, type DataEpoch, dataEpoch, UINT64_MAX, uint64BE, } from "./uint64.js";
9
- //# sourceMappingURL=index.d.ts.map
package/dist/index.js DELETED
@@ -1,9 +0,0 @@
1
- /** Name of this package. */
2
- export const PACKAGE = "@openlfcp/core";
3
- export { bytesEqual, fromBase64url, fromHex, toBase64url, toHex } from "./bytes.js";
4
- export { LfcpError } from "./errors.js";
5
- export { compareCanonicalFrontierOrder, controlRecordId, dataUnitId, generateResourceId, hash32, ID32_LENGTH, idEquals, principalId, resourceId, } from "./ids.js";
6
- export { formatObjectId, generateObjectId, isObjectId, parseObjectId, } from "./object-id.js";
7
- export { secureRandom } from "./random.js";
8
- export { actorSequence, dataEpoch, UINT64_MAX, uint64BE, } from "./uint64.js";
9
- //# sourceMappingURL=index.js.map
@@ -1,33 +0,0 @@
1
- /**
2
- * Shared Object ID (SHARED-OBJECTS-PROFILE-01 §19): a UUIDv7 (RFC 9562) in its
3
- * canonical lowercase hyphenated string form.
4
- *
5
- * Object IDs identify objects; they do not order them. The UUIDv7 timestamp
6
- * and lexical order are NOT LFCP causality, conflict precedence or
7
- * authorization order, so this module deliberately offers no comparison,
8
- * sorting or timestamp extraction.
9
- */
10
- declare const brand: unique symbol;
11
- export type ObjectId = string & {
12
- readonly [brand]: "ObjectId";
13
- };
14
- /** True when `text` is a canonical UUIDv7 Object ID. */
15
- export declare function isObjectId(text: string): text is ObjectId;
16
- /** Returns `text` as an ObjectId, or throws `INVALID_UUIDV7`. Non-canonical forms (e.g. upper case) are rejected, not normalized. */
17
- export declare function parseObjectId(text: string): ObjectId;
18
- /** The canonical string form (Object IDs are stored canonically, so this is the value itself). */
19
- export declare function formatObjectId(id: ObjectId): string;
20
- export interface GenerateObjectIdOptions {
21
- /** Unix time in milliseconds for the 48-bit timestamp; defaults to Date.now(). */
22
- readonly now?: number;
23
- /** Source of 10 random bytes; defaults to globalThis.crypto.getRandomValues. */
24
- readonly random?: (length: number) => Uint8Array;
25
- }
26
- /**
27
- * Generates a new UUIDv7 Object ID (RFC 9562 §5.7): 48-bit Unix millisecond
28
- * timestamp, version 7, 12 random bits, variant 10, 62 random bits. No
29
- * within-millisecond counter is kept.
30
- */
31
- export declare function generateObjectId(options?: GenerateObjectIdOptions): ObjectId;
32
- export {};
33
- //# sourceMappingURL=object-id.d.ts.map
package/dist/object-id.js DELETED
@@ -1,44 +0,0 @@
1
- import { LfcpError } from "./errors.js";
2
- import { secureRandom } from "./random.js";
3
- // Lowercase, standard hyphen positions, version 7, variant bits 10.
4
- const UUIDV7 = /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
5
- /** True when `text` is a canonical UUIDv7 Object ID. */
6
- export function isObjectId(text) {
7
- return UUIDV7.test(text);
8
- }
9
- /** Returns `text` as an ObjectId, or throws `INVALID_UUIDV7`. Non-canonical forms (e.g. upper case) are rejected, not normalized. */
10
- export function parseObjectId(text) {
11
- if (!isObjectId(text))
12
- throw new LfcpError("INVALID_UUIDV7", "not a canonical lowercase UUIDv7");
13
- return text;
14
- }
15
- /** The canonical string form (Object IDs are stored canonically, so this is the value itself). */
16
- export function formatObjectId(id) {
17
- return id;
18
- }
19
- /**
20
- * Generates a new UUIDv7 Object ID (RFC 9562 §5.7): 48-bit Unix millisecond
21
- * timestamp, version 7, 12 random bits, variant 10, 62 random bits. No
22
- * within-millisecond counter is kept.
23
- */
24
- export function generateObjectId(options = {}) {
25
- const now = options.now ?? Date.now();
26
- if (!Number.isSafeInteger(now) || now < 0 || now > 0xffff_ffff_ffff) {
27
- throw new LfcpError("INVALID_UUIDV7", "timestamp must be an integer in [0, 2^48)");
28
- }
29
- const rnd = (options.random ?? secureRandom)(10);
30
- if (rnd.length !== 10)
31
- throw new LfcpError("INVALID_LENGTH", "random source must return 10 bytes");
32
- const b = new Uint8Array(16);
33
- let t = now;
34
- for (let i = 5; i >= 0; i -= 1) {
35
- b[i] = t % 256;
36
- t = Math.floor(t / 256);
37
- }
38
- b.set(rnd, 6);
39
- b[6] = 0x70 | (b[6] & 0x0f);
40
- b[8] = 0x80 | (b[8] & 0x3f);
41
- const hex = [...b].map((x) => x.toString(16).padStart(2, "0")).join("");
42
- return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
43
- }
44
- //# sourceMappingURL=object-id.js.map
package/dist/random.d.ts DELETED
@@ -1,3 +0,0 @@
1
- /** `length` bytes from the platform CSPRNG (globalThis.crypto.getRandomValues), or NO_SECURE_RANDOM. */
2
- export declare function secureRandom(length: number): Uint8Array;
3
- //# sourceMappingURL=random.d.ts.map
package/dist/random.js DELETED
@@ -1,10 +0,0 @@
1
- import { LfcpError } from "./errors.js";
2
- /** `length` bytes from the platform CSPRNG (globalThis.crypto.getRandomValues), or NO_SECURE_RANDOM. */
3
- export function secureRandom(length) {
4
- const c = globalThis.crypto;
5
- if (typeof c?.getRandomValues !== "function") {
6
- throw new LfcpError("NO_SECURE_RANDOM", "globalThis.crypto.getRandomValues is not available");
7
- }
8
- return c.getRandomValues(new Uint8Array(length));
9
- }
10
- //# sourceMappingURL=random.js.map
package/dist/uint64.d.ts DELETED
@@ -1,24 +0,0 @@
1
- /**
2
- * Unsigned 64-bit values of LFCP-WIRE-01 as branded bigints.
3
- *
4
- * Constructions such as `uint64_be(epoch)` (§11, §12) need the full uint64
5
- * range, which a JavaScript number cannot hold exactly above 2^53. The
6
- * constructors accept a bigint or a safe-integer number and reject anything
7
- * out of range with OUT_OF_RANGE; a value is never truncated or wrapped.
8
- */
9
- declare const brand: unique symbol;
10
- /** A Data Epoch (§11, §19): 0 <= epoch <= 2^64 - 1. */
11
- export type DataEpoch = bigint & {
12
- readonly [brand]: "DataEpoch";
13
- };
14
- /** A per-(Resource, Principal) actor sequence (§8): 1 <= seq <= 2^64 - 1. */
15
- export type ActorSequence = bigint & {
16
- readonly [brand]: "ActorSequence";
17
- };
18
- export declare const UINT64_MAX: bigint;
19
- export declare const dataEpoch: (value: bigint | number) => DataEpoch;
20
- export declare const actorSequence: (value: bigint | number) => ActorSequence;
21
- /** `uint64_be(value)`: 8 big-endian bytes; OUT_OF_RANGE outside 0..2^64-1. */
22
- export declare function uint64BE(value: bigint | number): Uint8Array;
23
- export {};
24
- //# sourceMappingURL=uint64.d.ts.map
package/dist/uint64.js DELETED
@@ -1,27 +0,0 @@
1
- import { LfcpError } from "./errors.js";
2
- export const UINT64_MAX = 2n ** 64n - 1n;
3
- function uint(what, value, min) {
4
- let n;
5
- if (typeof value === "bigint")
6
- n = value;
7
- else if (typeof value === "number" && Number.isSafeInteger(value))
8
- n = BigInt(value);
9
- else
10
- throw new LfcpError("OUT_OF_RANGE", `${what} must be a bigint or a safe integer, got ${typeof value === "number" ? String(value) : typeof value}`);
11
- if (n < min || n > UINT64_MAX)
12
- throw new LfcpError("OUT_OF_RANGE", `${what} must be in ${min}..2^64-1, got ${n}`);
13
- return n;
14
- }
15
- export const dataEpoch = (value) => uint("a Data Epoch", value, 0n);
16
- export const actorSequence = (value) => uint("an actor sequence", value, 1n);
17
- /** `uint64_be(value)`: 8 big-endian bytes; OUT_OF_RANGE outside 0..2^64-1. */
18
- export function uint64BE(value) {
19
- let n = uint("a uint64 value", value, 0n);
20
- const out = new Uint8Array(8);
21
- for (let i = 7; i >= 0; i -= 1) {
22
- out[i] = Number(n & 0xffn);
23
- n >>= 8n;
24
- }
25
- return out;
26
- }
27
- //# sourceMappingURL=uint64.js.map