@dxos/keys 0.8.4-main.3f58842 → 0.8.4-main.5ea62a8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/keys",
3
- "version": "0.8.4-main.3f58842",
3
+ "version": "0.8.4-main.5ea62a8",
4
4
  "description": "Key utils and definitions.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -10,12 +10,10 @@
10
10
  "type": "module",
11
11
  "exports": {
12
12
  ".": {
13
+ "source": "./src/index.ts",
14
+ "types": "./dist/types/src/index.d.ts",
13
15
  "browser": "./dist/lib/browser/index.mjs",
14
- "node": {
15
- "require": "./dist/lib/node/index.cjs",
16
- "default": "./dist/lib/node-esm/index.mjs"
17
- },
18
- "types": "./dist/types/src/index.d.ts"
16
+ "node": "./dist/lib/node-esm/index.mjs"
19
17
  }
20
18
  },
21
19
  "types": "dist/types/src/index.d.ts",
@@ -27,11 +25,11 @@
27
25
  "src"
28
26
  ],
29
27
  "dependencies": {
30
- "effect": "3.17.0",
28
+ "effect": "3.17.7",
31
29
  "ulidx": "^2.3.0",
32
- "@dxos/debug": "0.8.4-main.3f58842",
33
- "@dxos/invariant": "0.8.4-main.3f58842",
34
- "@dxos/node-std": "0.8.4-main.3f58842"
30
+ "@dxos/debug": "0.8.4-main.5ea62a8",
31
+ "@dxos/node-std": "0.8.4-main.5ea62a8",
32
+ "@dxos/invariant": "0.8.4-main.5ea62a8"
35
33
  },
36
34
  "devDependencies": {
37
35
  "base32-decode": "^1.0.0",
package/src/dxn.ts CHANGED
@@ -2,10 +2,11 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
+ import type { InspectOptionsStylized, inspect } from 'node:util';
6
+
5
7
  import { Schema } from 'effect';
6
- import type { inspect, InspectOptionsStylized } from 'node:util';
7
8
 
8
- import { devtoolsFormatter, type DevtoolsFormatter, inspectCustom } from '@dxos/debug';
9
+ import { type DevtoolsFormatter, devtoolsFormatter, inspectCustom } from '@dxos/debug';
9
10
  import { assertArgument, invariant } from '@dxos/invariant';
10
11
 
11
12
  import { ObjectId } from './object-id';
@@ -18,6 +19,8 @@ import { SpaceId } from './space-id';
18
19
  // TODO(dmaretskyi): "@" is a separator character in the URI spec.
19
20
  export const LOCAL_SPACE_TAG = '@';
20
21
 
22
+ export const DXN_ECHO_REGEXP = /@(dxn:[a-zA-Z0-p:@]+)/;
23
+
21
24
  // TODO(burdon): Namespace for.
22
25
  export const QueueSubspaceTags = Object.freeze({
23
26
  DATA: 'data',
@@ -26,6 +29,12 @@ export const QueueSubspaceTags = Object.freeze({
26
29
 
27
30
  export type QueueSubspaceTag = (typeof QueueSubspaceTags)[keyof typeof QueueSubspaceTags];
28
31
 
32
+ // TODO(burdon): Refactor.
33
+ // Consider: https://github.com/multiformats/multiaddr
34
+ // dxn:echo:[<space-id>:[<queue-id>:]]<object-id>
35
+ // dxn:echo:[S/<space-id>:[Q/<queue-id>:]]<object-id>
36
+ // dxn:type:dxos.org/markdown/Contact
37
+
29
38
  /**
30
39
  * DXN unambiguously names a resource like an ECHO object, schema definition, plugin, etc.
31
40
  * Each DXN starts with a dxn prefix, followed by a resource kind.
@@ -43,6 +52,7 @@ export type QueueSubspaceTag = (typeof QueueSubspaceTags)[keyof typeof QueueSubs
43
52
  * ```
44
53
  */
45
54
  export class DXN {
55
+ // TODO(burdon): Rename to DXN (i.e., DXN.DXN).
46
56
  // TODO(dmaretskyi): Should this be a transformation into the DXN type?
47
57
  static Schema = Schema.NonEmptyString.pipe(
48
58
  Schema.pattern(/^dxn:([^:]+):(?:[^:]+:?)+[^:]$/),
@@ -64,16 +74,16 @@ export class DXN {
64
74
  */
65
75
  static kind = Object.freeze({
66
76
  /**
67
- * dxn:type:<type name>[:<version>]
77
+ * dxn:type:<type_name>[:<version>]
68
78
  */
69
79
  TYPE: 'type',
70
80
 
71
81
  /**
72
- * dxn:echo:<space id>:<echo id>
73
- * dxn:echo:@:<echo id>
82
+ * dxn:echo:<space_id>:<echo_id>
83
+ * dxn:echo:@:<echo_id>
74
84
  */
75
- // TODO(burdon): Rename to OBJECT? (BREAKING CHANGE).
76
- // TODO(burdon): Add separate Kind for space.
85
+ // TODO(burdon): Rename to OBJECT? (BREAKING CHANGE to update "echo").
86
+ // TODO(burdon): Add separate Kind for space?
77
87
  ECHO: 'echo',
78
88
 
79
89
  /**
@@ -85,14 +95,19 @@ export class DXN {
85
95
  QUEUE: 'queue',
86
96
  });
87
97
 
88
- get kind() {
89
- return this.#kind;
90
- }
91
-
98
+ /**
99
+ * Exactly equals.
100
+ */
92
101
  static equals(a: DXN, b: DXN): boolean {
93
102
  return a.kind === b.kind && a.parts.length === b.parts.length && a.parts.every((part, i) => part === b.parts[i]);
94
103
  }
95
104
 
105
+ static equalsEchoId(a: DXN, b: DXN): boolean {
106
+ const a1 = a.asEchoDXN();
107
+ const b1 = b.asEchoDXN();
108
+ return !!a1 && !!b1 && a1.echoId === b1.echoId;
109
+ }
110
+
96
111
  // TODO(burdon): Rename isValid.
97
112
  static isDXNString(dxn: string): boolean {
98
113
  return dxn.startsWith('dxn:');
@@ -119,7 +134,7 @@ export class DXN {
119
134
  static tryParse(dxn: string): DXN | undefined {
120
135
  try {
121
136
  return DXN.parse(dxn);
122
- } catch (error) {
137
+ } catch {
123
138
  return undefined;
124
139
  }
125
140
  }
@@ -139,6 +154,15 @@ export class DXN {
139
154
  return new DXN(DXN.kind.TYPE, [typename, version]);
140
155
  }
141
156
 
157
+ /**
158
+ * @example `dxn:echo:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6`
159
+ */
160
+ static fromSpaceAndObjectId(spaceId: SpaceId, objectId: ObjectId): DXN {
161
+ assertArgument(SpaceId.isValid(spaceId), `Invalid space ID: ${spaceId}`);
162
+ assertArgument(ObjectId.isValid(objectId), `Invalid object ID: ${objectId}`);
163
+ return new DXN(DXN.kind.ECHO, [spaceId, objectId]);
164
+ }
165
+
142
166
  /**
143
167
  * @example `dxn:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6`
144
168
  */
@@ -148,9 +172,9 @@ export class DXN {
148
172
  }
149
173
 
150
174
  static fromQueue(subspaceTag: QueueSubspaceTag, spaceId: SpaceId, queueId: ObjectId, objectId?: ObjectId) {
151
- invariant(SpaceId.isValid(spaceId));
152
- invariant(ObjectId.isValid(queueId));
153
- invariant(!objectId || ObjectId.isValid(objectId));
175
+ assertArgument(SpaceId.isValid(spaceId), `Invalid space ID: ${spaceId}`);
176
+ assertArgument(ObjectId.isValid(queueId), `Invalid queue ID: ${queueId}`);
177
+ assertArgument(!objectId || ObjectId.isValid(objectId), `Invalid object ID: ${objectId}`);
154
178
 
155
179
  return new DXN(DXN.kind.QUEUE, [subspaceTag, spaceId, queueId, ...(objectId ? [objectId] : [])]);
156
180
  }
@@ -159,19 +183,22 @@ export class DXN {
159
183
  #parts: string[];
160
184
 
161
185
  constructor(kind: string, parts: string[]) {
162
- invariant(parts.length > 0);
163
- invariant(parts.every((part) => typeof part === 'string' && part.length > 0 && part.indexOf(':') === -1));
186
+ assertArgument(parts.length > 0, `Invalid DXN: ${parts}`);
187
+ assertArgument(
188
+ parts.every((part) => typeof part === 'string' && part.length > 0 && part.indexOf(':') === -1),
189
+ `Invalid DXN: ${parts}`,
190
+ );
164
191
 
165
192
  // Per-type validation.
166
193
  switch (kind) {
167
194
  case DXN.kind.TYPE:
168
195
  if (parts.length > 2) {
169
- throw new Error('Invalid "type" DXN');
196
+ throw new Error('Invalid DXN.kind.TYPE');
170
197
  }
171
198
  break;
172
199
  case DXN.kind.ECHO:
173
200
  if (parts.length !== 2) {
174
- throw new Error('Invalid "echo" DXN');
201
+ throw new Error('Invalid DXN.kind.ECHO');
175
202
  }
176
203
  break;
177
204
  }
@@ -209,6 +236,10 @@ export class DXN {
209
236
  };
210
237
  }
211
238
 
239
+ get kind() {
240
+ return this.#kind;
241
+ }
242
+
212
243
  get parts() {
213
244
  return this.#parts;
214
245
  }
@@ -219,6 +250,10 @@ export class DXN {
219
250
  return this.#parts[0];
220
251
  }
221
252
 
253
+ equals(other: DXN): boolean {
254
+ return DXN.equals(this, other);
255
+ }
256
+
222
257
  hasTypenameOf(typename: string): boolean {
223
258
  return this.#kind === DXN.kind.TYPE && this.#parts.length === 1 && this.#parts[0] === typename;
224
259
  }
@@ -248,6 +283,7 @@ export class DXN {
248
283
  const [spaceId, echoId] = this.#parts;
249
284
  return {
250
285
  spaceId: spaceId === LOCAL_SPACE_TAG ? undefined : (spaceId as SpaceId | undefined),
286
+ // TODO(burdon): objectId.
251
287
  echoId,
252
288
  };
253
289
  }
@@ -269,35 +305,27 @@ export class DXN {
269
305
  objectId: objectId as string | undefined,
270
306
  };
271
307
  }
272
- }
273
308
 
274
- // TODO(dmaretskyi): Fluent API:
275
- /*
276
- class DXN {
277
- ...
278
- isEchoDXN(): this is EchoDXN {
279
- return this.#kind === DXN.kind.ECHO;
280
- }
281
- ...
282
- }
283
-
284
- interface EchoDXN extends DXN {
285
- objectId: ObjectId;
286
- }
287
-
288
- declare const dxn: DXN;
289
-
290
- dxn.objectId
291
-
292
- if(dxn.isEchoDXN()) {
293
- dxn.objectId
309
+ /**
310
+ * Produces a new DXN with the given parts appended.
311
+ */
312
+ extend(parts: string[]): DXN {
313
+ return new DXN(this.#kind, [...this.#parts, ...parts]);
314
+ }
294
315
  }
295
- ```
296
316
 
297
317
  /**
298
318
  * API namespace.
299
319
  */
300
320
  export declare namespace DXN {
321
+ /**
322
+ * DXN represented as a javascript string.
323
+ */
324
+ // TODO(burdon): Use Effect branded string?
325
+ // export const String = S.String.pipe(S.brand('DXN'));
326
+ // export type String = S.To(typoeof String);
327
+ export type String = string & { __DXNString: never };
328
+
301
329
  export type TypeDXN = {
302
330
  type: string;
303
331
  version?: string;
@@ -305,8 +333,7 @@ export declare namespace DXN {
305
333
 
306
334
  export type EchoDXN = {
307
335
  spaceId?: SpaceId;
308
- // TODO(burdon): Rename objectId.
309
- echoId: string; // TODO(dmaretskyi): ObjectId.
336
+ echoId: string; // TODO(dmaretskyi): Rename to `objectId` and use `ObjectId` for the type.
310
337
  };
311
338
 
312
339
  export type QueueDXN = {
@@ -315,12 +342,4 @@ export declare namespace DXN {
315
342
  queueId: string; // TODO(dmaretskyi): ObjectId.
316
343
  objectId?: string; // TODO(dmaretskyi): ObjectId.
317
344
  };
318
-
319
- /**
320
- * DXN represented as a javascript string.
321
- */
322
- export type String = string & { __DXNString: never };
323
- // TODO(burdon): Make brand.
324
- // export const String = S.String.pipe(S.brand('DXN'));
325
- // export type String = S.To(typoeof String);
326
345
  }
package/src/object-id.ts CHANGED
@@ -9,7 +9,7 @@ import { ulid } from 'ulidx';
9
9
  // export const ObjectIdBrand: unique symbol = Symbol('@dxos/echo/ObjectId');
10
10
  // export const ObjectIdSchema = Schema.ULID.pipe(S.brand(ObjectIdBrand));
11
11
  const ObjectIdSchema = Schema.String.pipe(Schema.pattern(/^[0-7][0-9A-HJKMNP-TV-Z]{25}$/i)).annotations({
12
- description: 'a Universally Unique Lexicographically Sortable Identifier',
12
+ description: 'A Universally Unique Lexicographically Sortable Identifier',
13
13
  pattern: '^[0-7][0-9A-HJKMNP-TV-Z]{25}$',
14
14
  });
15
15
 
@@ -31,7 +31,7 @@ export const ObjectId: ObjectIdClass = class extends ObjectIdSchema {
31
31
  try {
32
32
  Schema.decodeSync(ObjectId)(id);
33
33
  return true;
34
- } catch (err) {
34
+ } catch {
35
35
  return false;
36
36
  }
37
37
  }
package/src/public-key.ts CHANGED
@@ -2,15 +2,16 @@
2
2
  // Copyright 2020 DXOS.org
3
3
  //
4
4
 
5
+ import { type InspectOptionsStylized, type inspect } from 'node:util';
6
+
5
7
  import base32Decode from 'base32-decode';
6
8
  import base32Encode from 'base32-encode';
7
- import { type inspect, type InspectOptionsStylized } from 'node:util';
8
9
 
9
10
  import {
10
- devtoolsFormatter,
11
11
  type DevtoolsFormatter,
12
- equalsSymbol,
13
12
  type Equatable,
13
+ devtoolsFormatter,
14
+ equalsSymbol,
14
15
  inspectCustom,
15
16
  truncateKey,
16
17
  } from '@dxos/debug';