@starkeep/protocol-primitives 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Aaron Koller, Pedro Pinto, Craig Schroeder
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,107 @@
1
+ # @starkeep/protocol-primitives
2
+
3
+ Protocol core for Starkeep: ULID-based identifiers, hybrid logical clocks (HLC), record builders, schema validation (valibot), and a type registry.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @starkeep/protocol-primitives
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import {
15
+ generateId,
16
+ createHLCClock,
17
+ createDataRecord,
18
+ createTypeRegistry,
19
+ validateDataRecord,
20
+ } from "@starkeep/protocol-primitives";
21
+
22
+ // Generate a ULID-based identifier
23
+ const identifier = generateId();
24
+
25
+ // Create an HLC clock for ordering events
26
+ const clock = createHLCClock({ nodeId: "node-1" });
27
+ const timestamp = clock.now();
28
+
29
+ // Build a data record
30
+ const record = createDataRecord({
31
+ type: "photo",
32
+ ownerId: "user-123",
33
+ clock,
34
+ payload: { title: "Sunset" },
35
+ mimeType: "image/jpeg",
36
+ sizeBytes: 204800,
37
+ });
38
+
39
+ // Validate a record against the schema
40
+ const validationResult = validateDataRecord(record);
41
+
42
+ // Register custom types
43
+ const registry = createTypeRegistry();
44
+ registry.register({ name: "photo", kind: "data" });
45
+ ```
46
+
47
+ ## API
48
+
49
+ ### Identifiers
50
+
51
+ | Export | Description |
52
+ |---|---|
53
+ | `generateId()` | Generate a new ULID-based `StarkeepId` |
54
+ | `generateIdAt(timestamp)` | Generate a ULID seeded at a specific timestamp |
55
+ | `createStarkeepId(value)` | Wrap a raw string as a `StarkeepId` |
56
+ | `isStarkeepId(value)` | Type guard for `StarkeepId` |
57
+
58
+ ### Hybrid Logical Clock
59
+
60
+ | Export | Description |
61
+ |---|---|
62
+ | `createHLCClock(options)` | Create an HLC clock instance |
63
+ | `compareHLC(a, b)` | Compare two HLC timestamps (-1, 0, 1) |
64
+ | `maxHLC(a, b)` | Return the greater of two HLC timestamps |
65
+ | `serializeHLC(timestamp)` | Serialize an HLC timestamp to a string |
66
+ | `deserializeHLC(value)` | Deserialize a string back to an HLC timestamp |
67
+
68
+ ### Records
69
+
70
+ | Export | Description |
71
+ |---|---|
72
+ | `createDataRecord(input)` | Build a `DataRecord` with generated id and timestamps |
73
+ | `createMetadataRecord(input)` | Build a `MetadataRecord` linked to a data record |
74
+ | `SyncStatus` | Enum: `local`, `syncing`, `synced`, `conflict` |
75
+
76
+ ### Schema & Validation
77
+
78
+ | Export | Description |
79
+ |---|---|
80
+ | `validateDataRecord(record)` | Validate a data record against the valibot schema |
81
+ | `validateMetadataRecord(record)` | Validate a metadata record |
82
+ | `validateAnyRecord(record)` | Validate any record (data or metadata) |
83
+ | `dataRecordSchema` | Valibot schema for data records |
84
+ | `metadataRecordSchema` | Valibot schema for metadata records |
85
+ | `createTypeRegistry()` | Create a registry for custom type definitions |
86
+
87
+ ### Error Types
88
+
89
+ | Export | Description |
90
+ |---|---|
91
+ | `StarkeepError` | Base error class |
92
+ | `ValidationError` | Schema validation failure |
93
+ | `NotFoundError` | Record not found |
94
+ | `ConflictError` | Conflicting record state |
95
+
96
+ ### Utilities
97
+
98
+ | Export | Description |
99
+ |---|---|
100
+ | `ok(value)` | Wrap a value in a success `Result` |
101
+ | `err(error)` | Wrap an error in a failure `Result` |
102
+
103
+ ## Testing
104
+
105
+ ```bash
106
+ pnpm --filter @starkeep/protocol-primitives test
107
+ ```