@dxos/keys 0.8.3 → 0.8.4-main.05e74ebcff

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.
Files changed (38) hide show
  1. package/LICENSE +102 -5
  2. package/dist/lib/browser/index.mjs +236 -266
  3. package/dist/lib/browser/index.mjs.map +4 -4
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/node-esm/index.mjs +233 -264
  6. package/dist/lib/node-esm/index.mjs.map +4 -4
  7. package/dist/lib/node-esm/meta.json +1 -1
  8. package/dist/types/src/dxn.d.ts +40 -22
  9. package/dist/types/src/dxn.d.ts.map +1 -1
  10. package/dist/types/src/index.d.ts +2 -1
  11. package/dist/types/src/index.d.ts.map +1 -1
  12. package/dist/types/src/object-id.d.ts +67 -1
  13. package/dist/types/src/object-id.d.ts.map +1 -1
  14. package/dist/types/src/parse-id.d.ts +10 -0
  15. package/dist/types/src/parse-id.d.ts.map +1 -0
  16. package/dist/types/src/parse-id.test.d.ts +2 -0
  17. package/dist/types/src/parse-id.test.d.ts.map +1 -0
  18. package/dist/types/src/prng.d.ts +32 -0
  19. package/dist/types/src/prng.d.ts.map +1 -0
  20. package/dist/types/src/public-key.d.ts +3 -3
  21. package/dist/types/src/public-key.d.ts.map +1 -1
  22. package/dist/types/src/random-bytes.d.ts.map +1 -1
  23. package/dist/types/src/space-id.d.ts +2 -2
  24. package/dist/types/src/space-id.d.ts.map +1 -1
  25. package/dist/types/tsconfig.tsbuildinfo +1 -1
  26. package/package.json +17 -15
  27. package/src/dxn.ts +108 -83
  28. package/src/index.ts +2 -1
  29. package/src/object-id.ts +117 -5
  30. package/src/parse-id.test.ts +32 -0
  31. package/src/parse-id.ts +32 -0
  32. package/src/prng.ts +54 -0
  33. package/src/public-key.ts +4 -4
  34. package/src/random-bytes.ts +1 -1
  35. package/src/space-id.ts +3 -3
  36. package/dist/lib/node/index.cjs +0 -866
  37. package/dist/lib/node/index.cjs.map +0 -7
  38. package/dist/lib/node/meta.json +0 -1
@@ -31,9 +31,9 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
31
31
  mod
32
32
  ));
33
33
 
34
- // node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js
34
+ // ../../../node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js
35
35
  var require_base32_decode = __commonJS({
36
- "node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js"(exports, module) {
36
+ "../../../node_modules/.pnpm/base32-decode@1.0.0/node_modules/base32-decode/index.js"(exports, module) {
37
37
  var RFC46482 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
38
38
  var RFC4648_HEX2 = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
39
39
  var CROCKFORD2 = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
@@ -81,36 +81,81 @@ var require_base32_decode = __commonJS({
81
81
  }
82
82
  });
83
83
 
84
- // packages/common/keys/src/dxn.ts
85
- import { Schema as Schema3 } from "effect";
84
+ // src/dxn.ts
85
+ import * as Schema3 from "effect/Schema";
86
86
  import { devtoolsFormatter, inspectCustom } from "@dxos/debug";
87
- import { invariant as invariant2 } from "@dxos/invariant";
87
+ import { assertArgument, invariant as invariant2 } from "@dxos/invariant";
88
88
 
89
- // packages/common/keys/src/object-id.ts
90
- import { Schema } from "effect";
91
- import { ulid } from "ulidx";
89
+ // src/object-id.ts
90
+ import * as Schema from "effect/Schema";
91
+ import { monotonicFactory } from "ulidx";
92
92
  var ObjectIdSchema = Schema.String.pipe(Schema.pattern(/^[0-7][0-9A-HJKMNP-TV-Z]{25}$/i)).annotations({
93
- description: "a Universally Unique Lexicographically Sortable Identifier",
93
+ description: "A Universally Unique Lexicographically Sortable Identifier",
94
94
  pattern: "^[0-7][0-9A-HJKMNP-TV-Z]{25}$"
95
95
  });
96
96
  var ObjectId = class extends ObjectIdSchema {
97
+ static #factory = monotonicFactory();
98
+ static #seedTime = void 0;
97
99
  static isValid(id) {
98
100
  try {
99
101
  Schema.decodeSync(ObjectId)(id);
100
102
  return true;
101
- } catch (err) {
103
+ } catch {
102
104
  return false;
103
105
  }
104
106
  }
105
107
  static random() {
106
- return ulid();
108
+ return this.#factory(this.#seedTime);
109
+ }
110
+ static dangerouslyDisableRandomness() {
111
+ this.#factory = monotonicFactory(makeTestPRNG());
112
+ this.#seedTime = (/* @__PURE__ */ new Date("2025-01-01")).getTime();
113
+ }
114
+ static dangerouslySetSeed(time, seed) {
115
+ this.#factory = monotonicFactory(makeTestPRNG(seed));
116
+ this.#seedTime = time;
117
+ }
118
+ };
119
+ var makeTestPRNG = (seed = 0) => {
120
+ const rng = new SimplePRNG(seed);
121
+ return () => {
122
+ return rng.next();
123
+ };
124
+ };
125
+ var SimplePRNG = class _SimplePRNG {
126
+ #seed;
127
+ // LCG parameters (from Numerical Recipes)
128
+ static #a = 1664525;
129
+ static #c = 1013904223;
130
+ static #m = Math.pow(2, 32);
131
+ /**
132
+ * Creates a new PRNG instance.
133
+ * @param seed - Initial seed value. If not provided, uses 0.
134
+ */
135
+ constructor(seed = 0) {
136
+ this.#seed = seed;
137
+ }
138
+ /**
139
+ * Generates the next pseudo-random number in the range [0, 1).
140
+ * @returns A pseudo-random number between 0 (inclusive) and 1 (exclusive).
141
+ */
142
+ next() {
143
+ this.#seed = (_SimplePRNG.#a * this.#seed + _SimplePRNG.#c) % _SimplePRNG.#m;
144
+ return this.#seed / _SimplePRNG.#m;
145
+ }
146
+ /**
147
+ * Resets the generator with a new seed.
148
+ * @param seed - New seed value.
149
+ */
150
+ reset(seed) {
151
+ this.#seed = seed;
107
152
  }
108
153
  };
109
154
 
110
- // packages/common/keys/src/space-id.ts
155
+ // src/space-id.ts
111
156
  var import_base32_decode = __toESM(require_base32_decode(), 1);
112
157
 
113
- // node_modules/.pnpm/to-data-view@2.0.0/node_modules/to-data-view/index.js
158
+ // ../../../node_modules/.pnpm/to-data-view@2.0.0/node_modules/to-data-view/index.js
114
159
  function toDataView(data) {
115
160
  if (data instanceof Int8Array || data instanceof Uint8Array || data instanceof Uint8ClampedArray) {
116
161
  return new DataView(data.buffer, data.byteOffset, data.byteLength);
@@ -121,7 +166,7 @@ function toDataView(data) {
121
166
  throw new TypeError("Expected `data` to be an ArrayBuffer, Buffer, Int8Array, Uint8Array or Uint8ClampedArray");
122
167
  }
123
168
 
124
- // node_modules/.pnpm/base32-encode@2.0.0/node_modules/base32-encode/index.js
169
+ // ../../../node_modules/.pnpm/base32-encode@2.0.0/node_modules/base32-encode/index.js
125
170
  var RFC4648 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
126
171
  var RFC4648_HEX = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
127
172
  var CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
@@ -169,11 +214,11 @@ function base32Encode(data, variant, options) {
169
214
  return output;
170
215
  }
171
216
 
172
- // packages/common/keys/src/space-id.ts
173
- import { Schema as Schema2 } from "effect";
217
+ // src/space-id.ts
218
+ import * as Schema2 from "effect/Schema";
174
219
  import { invariant } from "@dxos/invariant";
175
220
 
176
- // packages/common/keys/src/random-bytes.ts
221
+ // src/random-bytes.ts
177
222
  var randomBytes = (length) => {
178
223
  const webCrypto = globalThis.crypto ?? __require("node:crypto").webcrypto;
179
224
  const bytes = new Uint8Array(length);
@@ -181,122 +226,89 @@ var randomBytes = (length) => {
181
226
  return bytes;
182
227
  };
183
228
 
184
- // packages/common/keys/src/space-id.ts
185
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/keys/src/space-id.ts";
229
+ // src/space-id.ts
186
230
  var MULTIBASE_PREFIX = "B";
187
231
  var ENCODED_LENGTH = 33;
188
232
  var isValid = (value) => {
189
233
  return typeof value === "string" && value.startsWith(MULTIBASE_PREFIX) && value.length === ENCODED_LENGTH;
190
234
  };
191
235
  var SpaceId = class extends Schema2.String.pipe(Schema2.filter(isValid)) {
192
- static {
193
- this.byteLength = 20;
194
- }
195
- static {
196
- this.encode = (value) => {
197
- invariant(value instanceof Uint8Array, "Invalid type", {
198
- F: __dxlog_file,
199
- L: 43,
200
- S: this,
201
- A: [
202
- "value instanceof Uint8Array",
203
- "'Invalid type'"
204
- ]
205
- });
206
- invariant(value.length === SpaceId.byteLength, "Invalid length", {
207
- F: __dxlog_file,
208
- L: 44,
209
- S: this,
210
- A: [
211
- "value.length === SpaceId.byteLength",
212
- "'Invalid length'"
213
- ]
214
- });
215
- return MULTIBASE_PREFIX + base32Encode(value, "RFC4648");
216
- };
217
- }
218
- static {
219
- this.decode = (value) => {
220
- invariant(value.startsWith(MULTIBASE_PREFIX), "Invalid multibase32 encoding", {
221
- F: __dxlog_file,
222
- L: 49,
223
- S: this,
224
- A: [
225
- "value.startsWith(MULTIBASE_PREFIX)",
226
- "'Invalid multibase32 encoding'"
227
- ]
228
- });
229
- return new Uint8Array((0, import_base32_decode.default)(value.slice(1), "RFC4648"));
230
- };
231
- }
232
- static {
233
- this.isValid = isValid;
234
- }
235
- static {
236
- this.random = () => {
237
- return SpaceId.encode(randomBytes(SpaceId.byteLength));
238
- };
239
- }
236
+ static byteLength = 20;
237
+ static encode = (value) => {
238
+ invariant(value instanceof Uint8Array, "Invalid type");
239
+ invariant(value.length === SpaceId.byteLength, "Invalid length");
240
+ return MULTIBASE_PREFIX + base32Encode(value, "RFC4648");
241
+ };
242
+ static decode = (value) => {
243
+ invariant(value.startsWith(MULTIBASE_PREFIX), "Invalid multibase32 encoding");
244
+ return new Uint8Array((0, import_base32_decode.default)(value.slice(1), "RFC4648"));
245
+ };
246
+ static isValid = isValid;
247
+ static random = () => {
248
+ return SpaceId.encode(randomBytes(SpaceId.byteLength));
249
+ };
240
250
  };
241
251
 
242
- // packages/common/keys/src/dxn.ts
243
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/common/keys/src/dxn.ts";
252
+ // src/dxn.ts
244
253
  var LOCAL_SPACE_TAG = "@";
254
+ var DXN_ECHO_REGEXP = /@(dxn:[a-zA-Z0-p:@]+)/;
245
255
  var QueueSubspaceTags = Object.freeze({
246
256
  DATA: "data",
247
257
  TRACE: "trace"
248
258
  });
249
259
  var DXN = class _DXN {
250
- static {
251
- // TODO(dmaretskyi): Should this be a transformation into the DXN type?
252
- this.Schema = Schema3.NonEmptyString.pipe(
253
- Schema3.pattern(/^dxn:([^:]+):(?:[^:]+:?)+[^:]$/),
254
- // TODO(dmaretskyi): To set the format we need to move the annotation IDs out of the echo-schema package.
255
- // FormatAnnotation.set(FormatEnum.DXN),
256
- Schema3.annotations({
257
- title: "DXN",
258
- description: "DXN URI",
259
- examples: [
260
- "dxn:type:example.com/type/MyType",
261
- "dxn:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6"
262
- ]
263
- })
264
- );
265
- }
260
+ // TODO(burdon): Rename to DXN (i.e., DXN.DXN).
261
+ // TODO(dmaretskyi): Should this be a transformation into the DXN type?
262
+ static Schema = Schema3.NonEmptyString.pipe(
263
+ Schema3.pattern(/^dxn:([^:]+):(?:[^:]+:?)+[^:]$/),
264
+ // TODO(dmaretskyi): To set the format we need to move the annotation IDs out of the echo-schema package.
265
+ // FormatAnnotation.set(TypeFormat.DXN),
266
+ Schema3.annotations({
267
+ title: "DXN",
268
+ description: "DXN URI",
269
+ examples: [
270
+ "dxn:type:com.example.type.my-type",
271
+ "dxn:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6"
272
+ ]
273
+ })
274
+ );
266
275
  static hash(dxn) {
267
276
  return dxn.toString();
268
277
  }
269
- static {
278
+ /**
279
+ * Kind constants.
280
+ */
281
+ static kind = Object.freeze({
270
282
  /**
271
- * Kind constants.
283
+ * dxn:type:<type_name>[:<version>]
272
284
  */
273
- this.kind = Object.freeze({
274
- /**
275
- * dxn:type:<type name>[:<version>]
276
- */
277
- TYPE: "type",
278
- /**
279
- * dxn:echo:<space id>:<echo id>
280
- * dxn:echo:@:<echo id>
281
- */
282
- // TODO(burdon): Rename to OBJECT? (BREAKING CHANGE).
283
- // TODO(burdon): Add separate Kind for space.
284
- ECHO: "echo",
285
- /**
286
- * The subspace tag enables us to partition queues by usage within the context of a space.
287
- * dxn:queue:<subspace_tag>:<space_id>:<queue_id>[:object_id]
288
- * dxn:queue:data:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6
289
- * dxn:queue:trace:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6
290
- */
291
- QUEUE: "queue"
292
- });
293
- }
294
- get kind() {
295
- return this.#kind;
296
- }
285
+ TYPE: "type",
286
+ /**
287
+ * dxn:echo:<space_id>:<echo_id>
288
+ * dxn:echo:@:<echo_id>
289
+ */
290
+ // TODO(burdon): Rename to OBJECT? (BREAKING CHANGE to update "echo").
291
+ // TODO(burdon): Add separate Kind for space?
292
+ ECHO: "echo",
293
+ /**
294
+ * The subspace tag enables us to partition queues by usage within the context of a space.
295
+ * dxn:queue:<subspace_tag>:<space_id>:<queue_id>[:object_id]
296
+ * dxn:queue:data:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6
297
+ * dxn:queue:trace:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6
298
+ */
299
+ QUEUE: "queue"
300
+ });
301
+ /**
302
+ * Exactly equals.
303
+ */
297
304
  static equals(a, b) {
298
305
  return a.kind === b.kind && a.parts.length === b.parts.length && a.parts.every((part, i) => part === b.parts[i]);
299
306
  }
307
+ static equalsEchoId(a, b) {
308
+ const a1 = a.asEchoDXN();
309
+ const b1 = b.asEchoDXN();
310
+ return !!a1 && !!b1 && a1.echoId === b1.echoId;
311
+ }
300
312
  // TODO(burdon): Rename isValid.
301
313
  static isDXNString(dxn) {
302
314
  return dxn.startsWith("dxn:");
@@ -320,12 +332,12 @@ var DXN = class _DXN {
320
332
  static tryParse(dxn) {
321
333
  try {
322
334
  return _DXN.parse(dxn);
323
- } catch (error) {
335
+ } catch {
324
336
  return void 0;
325
337
  }
326
338
  }
327
339
  /**
328
- * @example `dxn:type:example.com/type/Contact`
340
+ * @example `dxn:type:com.example.type.person`
329
341
  */
330
342
  static fromTypename(typename) {
331
343
  return new _DXN(_DXN.kind.TYPE, [
@@ -333,7 +345,7 @@ var DXN = class _DXN {
333
345
  ]);
334
346
  }
335
347
  /**
336
- * @example `dxn:type:example.com/type/Contact:0.1.0`
348
+ * @example `dxn:type:com.example.type.person:0.1.0`
337
349
  */
338
350
  // TODO(dmaretskyi): Consider using @ as the version separator.
339
351
  static fromTypenameAndVersion(typename, version) {
@@ -343,42 +355,30 @@ var DXN = class _DXN {
343
355
  ]);
344
356
  }
345
357
  /**
358
+ * @example `dxn:echo:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6`
359
+ */
360
+ static fromSpaceAndObjectId(spaceId, objectId) {
361
+ assertArgument(SpaceId.isValid(spaceId), `Invalid space ID: ${spaceId}`);
362
+ assertArgument(ObjectId.isValid(objectId), "objectId", `Invalid object ID: ${objectId}`);
363
+ return new _DXN(_DXN.kind.ECHO, [
364
+ spaceId,
365
+ objectId
366
+ ]);
367
+ }
368
+ /**
346
369
  * @example `dxn:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6`
347
370
  */
348
371
  static fromLocalObjectId(id) {
372
+ assertArgument(ObjectId.isValid(id), "id", `Invalid object ID: ${id}`);
349
373
  return new _DXN(_DXN.kind.ECHO, [
350
374
  LOCAL_SPACE_TAG,
351
375
  id
352
376
  ]);
353
377
  }
354
378
  static fromQueue(subspaceTag, spaceId, queueId, objectId) {
355
- invariant2(SpaceId.isValid(spaceId), void 0, {
356
- F: __dxlog_file2,
357
- L: 150,
358
- S: this,
359
- A: [
360
- "SpaceId.isValid(spaceId)",
361
- ""
362
- ]
363
- });
364
- invariant2(ObjectId.isValid(queueId), void 0, {
365
- F: __dxlog_file2,
366
- L: 151,
367
- S: this,
368
- A: [
369
- "ObjectId.isValid(queueId)",
370
- ""
371
- ]
372
- });
373
- invariant2(!objectId || ObjectId.isValid(objectId), void 0, {
374
- F: __dxlog_file2,
375
- L: 152,
376
- S: this,
377
- A: [
378
- "!objectId || ObjectId.isValid(objectId)",
379
- ""
380
- ]
381
- });
379
+ assertArgument(SpaceId.isValid(spaceId), `Invalid space ID: ${spaceId}`);
380
+ assertArgument(ObjectId.isValid(queueId), "queueId", `Invalid queue ID: ${queueId}`);
381
+ assertArgument(!objectId || ObjectId.isValid(objectId), "objectId", `Invalid object ID: ${objectId}`);
382
382
  return new _DXN(_DXN.kind.QUEUE, [
383
383
  subspaceTag,
384
384
  spaceId,
@@ -391,55 +391,65 @@ var DXN = class _DXN {
391
391
  #kind;
392
392
  #parts;
393
393
  constructor(kind, parts) {
394
- invariant2(parts.length > 0, void 0, {
395
- F: __dxlog_file2,
396
- L: 161,
397
- S: this,
398
- A: [
399
- "parts.length > 0",
400
- ""
401
- ]
402
- });
403
- invariant2(parts.every((part) => typeof part === "string" && part.length > 0 && part.indexOf(":") === -1), void 0, {
404
- F: __dxlog_file2,
405
- L: 162,
406
- S: this,
407
- A: [
408
- "parts.every((part) => typeof part === 'string' && part.length > 0 && part.indexOf(':') === -1)",
409
- ""
410
- ]
411
- });
394
+ assertArgument(parts.length > 0, "parts", `Invalid DXN: ${parts}`);
395
+ assertArgument(parts.every((part) => typeof part === "string" && part.length > 0 && part.indexOf(":") === -1), "parts", `Invalid DXN: ${parts}`);
412
396
  switch (kind) {
413
397
  case _DXN.kind.TYPE:
414
398
  if (parts.length > 2) {
415
- throw new Error('Invalid "type" DXN');
399
+ throw new Error("Invalid DXN.kind.TYPE");
416
400
  }
417
401
  break;
418
402
  case _DXN.kind.ECHO:
419
403
  if (parts.length !== 2) {
420
- throw new Error('Invalid "echo" DXN');
404
+ throw new Error("Invalid DXN.kind.ECHO");
421
405
  }
422
406
  break;
423
407
  }
424
408
  this.#kind = kind;
425
409
  this.#parts = parts;
426
410
  }
411
+ toString() {
412
+ return `dxn:${this.#kind}:${this.#parts.join(":")}`;
413
+ }
414
+ toJSON() {
415
+ return this.toString();
416
+ }
417
+ /**
418
+ * Used by Node.js to get textual representation of this object when it's printed with a `console.log` statement.
419
+ */
420
+ [inspectCustom](depth, options, inspectFn) {
421
+ const printControlCode = (code) => {
422
+ return `\x1B[${code}m`;
423
+ };
424
+ return printControlCode(inspectFn.colors.blueBright[0]) + this.toString() + printControlCode(inspectFn.colors.reset[0]);
425
+ }
426
+ get [devtoolsFormatter]() {
427
+ return {
428
+ header: () => {
429
+ return [
430
+ "span",
431
+ {
432
+ style: "font-weight: bold;"
433
+ },
434
+ this.toString()
435
+ ];
436
+ }
437
+ };
438
+ }
439
+ get kind() {
440
+ return this.#kind;
441
+ }
427
442
  get parts() {
428
443
  return this.#parts;
429
444
  }
430
445
  // TODO(burdon): Should getters fail?
431
446
  get typename() {
432
- invariant2(this.#kind === _DXN.kind.TYPE, void 0, {
433
- F: __dxlog_file2,
434
- L: 188,
435
- S: this,
436
- A: [
437
- "this.#kind === DXN.kind.TYPE",
438
- ""
439
- ]
440
- });
447
+ invariant2(this.#kind === _DXN.kind.TYPE);
441
448
  return this.#parts[0];
442
449
  }
450
+ equals(other) {
451
+ return _DXN.equals(this, other);
452
+ }
443
453
  hasTypenameOf(typename) {
444
454
  return this.#kind === _DXN.kind.TYPE && this.#parts.length === 1 && this.#parts[0] === typename;
445
455
  }
@@ -452,6 +462,7 @@ var DXN = class _DXN {
452
462
  }
453
463
  const [type, version] = this.#parts;
454
464
  return {
465
+ // TODO(wittjosiah): Should be `typename` for consistency.
455
466
  type,
456
467
  version
457
468
  };
@@ -463,6 +474,7 @@ var DXN = class _DXN {
463
474
  const [spaceId, echoId] = this.#parts;
464
475
  return {
465
476
  spaceId: spaceId === LOCAL_SPACE_TAG ? void 0 : spaceId,
477
+ // TODO(burdon): objectId.
466
478
  echoId
467
479
  };
468
480
  }
@@ -481,70 +493,29 @@ var DXN = class _DXN {
481
493
  objectId
482
494
  };
483
495
  }
484
- toString() {
485
- return `dxn:${this.#kind}:${this.#parts.join(":")}`;
486
- }
487
496
  /**
488
- * Used by Node.js to get textual representation of this object when it's printed with a `console.log` statement.
497
+ * Produces a new DXN with the given parts appended.
489
498
  */
490
- [inspectCustom](depth, options, inspectFn) {
491
- const printControlCode = (code) => {
492
- return `\x1B[${code}m`;
493
- };
494
- return printControlCode(inspectFn.colors.blueBright[0]) + this.toString() + printControlCode(inspectFn.colors.reset[0]);
495
- }
496
- get [devtoolsFormatter]() {
497
- return {
498
- header: () => {
499
- return [
500
- "span",
501
- {
502
- style: "font-weight: bold;"
503
- },
504
- this.toString()
505
- ];
506
- }
507
- };
499
+ extend(parts) {
500
+ return new _DXN(this.#kind, [
501
+ ...this.#parts,
502
+ ...parts
503
+ ]);
508
504
  }
509
505
  };
510
506
 
511
- // packages/common/keys/src/identity-did.ts
507
+ // src/identity-did.ts
512
508
  var import_base32_decode2 = __toESM(require_base32_decode(), 1);
513
509
  import { invariant as invariant3 } from "@dxos/invariant";
514
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/common/keys/src/identity-did.ts";
515
510
  var IdentityDid = Object.freeze({
516
511
  byteLength: 20,
517
512
  encode: (value) => {
518
- invariant3(value instanceof Uint8Array, "Invalid type", {
519
- F: __dxlog_file3,
520
- L: 22,
521
- S: void 0,
522
- A: [
523
- "value instanceof Uint8Array",
524
- "'Invalid type'"
525
- ]
526
- });
527
- invariant3(value.length === IdentityDid.byteLength, "Invalid length", {
528
- F: __dxlog_file3,
529
- L: 23,
530
- S: void 0,
531
- A: [
532
- "value.length === IdentityDid.byteLength",
533
- "'Invalid length'"
534
- ]
535
- });
513
+ invariant3(value instanceof Uint8Array, "Invalid type");
514
+ invariant3(value.length === IdentityDid.byteLength, "Invalid length");
536
515
  return DID_PREFIX + MULTIBASE_PREFIX2 + base32Encode(value, "RFC4648");
537
516
  },
538
517
  decode: (value) => {
539
- invariant3(value.startsWith(DID_PREFIX + MULTIBASE_PREFIX2), "Invalid multibase32 encoding", {
540
- F: __dxlog_file3,
541
- L: 28,
542
- S: void 0,
543
- A: [
544
- "value.startsWith(DID_PREFIX + MULTIBASE_PREFIX)",
545
- "'Invalid multibase32 encoding'"
546
- ]
547
- });
518
+ invariant3(value.startsWith(DID_PREFIX + MULTIBASE_PREFIX2), "Invalid multibase32 encoding");
548
519
  return new Uint8Array((0, import_base32_decode2.default)(value.slice(10), "RFC4648"));
549
520
  },
550
521
  isValid: (value) => {
@@ -558,33 +529,49 @@ var MULTIBASE_PREFIX2 = "B";
558
529
  var DID_PREFIX = "did:halo:";
559
530
  var ENCODED_LENGTH2 = 42;
560
531
 
561
- // packages/common/keys/src/public-key.ts
532
+ // src/parse-id.ts
533
+ var SPACE_ID_LENGTH = 33;
534
+ var OBJECT_ID_LENGTH = 26;
535
+ var FQ_ID_LENGTH = SPACE_ID_LENGTH + OBJECT_ID_LENGTH + 1;
536
+ var parseId = (id) => {
537
+ if (!id) {
538
+ return {};
539
+ } else if (id.length === SPACE_ID_LENGTH) {
540
+ return {
541
+ spaceId: id
542
+ };
543
+ } else if (id.length === OBJECT_ID_LENGTH) {
544
+ return {
545
+ objectId: id
546
+ };
547
+ } else if (id.length === FQ_ID_LENGTH && id.indexOf(":") === SPACE_ID_LENGTH) {
548
+ const [spaceId, objectId] = id.split(":");
549
+ return {
550
+ spaceId,
551
+ objectId
552
+ };
553
+ } else {
554
+ return {};
555
+ }
556
+ };
557
+
558
+ // src/public-key.ts
562
559
  var import_base32_decode3 = __toESM(require_base32_decode(), 1);
563
560
  import { devtoolsFormatter as devtoolsFormatter2, equalsSymbol, inspectCustom as inspectCustom2, truncateKey } from "@dxos/debug";
564
561
  import { invariant as invariant4 } from "@dxos/invariant";
565
- var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/common/keys/src/public-key.ts";
566
562
  var PUBLIC_KEY_LENGTH = 32;
567
563
  var SECRET_KEY_LENGTH = 64;
568
564
  var isLikeArrayBuffer = (value) => typeof value === "object" && value !== null && Object.getPrototypeOf(value).constructor.name === "ArrayBuffer";
569
565
  var PublicKey = class _PublicKey {
570
- static {
571
- this.ZERO = _PublicKey.from("00".repeat(PUBLIC_KEY_LENGTH));
572
- }
566
+ _value;
567
+ static ZERO = _PublicKey.from("00".repeat(PUBLIC_KEY_LENGTH));
573
568
  /**
574
569
  * Creates new instance of PublicKey automatically determining the input format.
575
570
  * @param source A Buffer, or Uint8Array, or hex encoded string, or something with an `asUint8Array` method on it
576
571
  * @returns PublicKey
577
572
  */
578
573
  static from(source) {
579
- invariant4(source, void 0, {
580
- F: __dxlog_file4,
581
- L: 49,
582
- S: this,
583
- A: [
584
- "source",
585
- ""
586
- ]
587
- });
574
+ invariant4(source);
588
575
  if (source instanceof _PublicKey) {
589
576
  return source;
590
577
  } else if (source instanceof Buffer) {
@@ -668,15 +655,7 @@ var PublicKey = class _PublicKey {
668
655
  * @deprecated All keys should be represented as instances of PublicKey.
669
656
  */
670
657
  static bufferize(str) {
671
- invariant4(typeof str === "string", "Invalid type", {
672
- F: __dxlog_file4,
673
- L: 152,
674
- S: this,
675
- A: [
676
- "typeof str === 'string'",
677
- "'Invalid type'"
678
- ]
679
- });
658
+ invariant4(typeof str === "string", "Invalid type");
680
659
  const buffer = Buffer.from(str, "hex");
681
660
  return buffer;
682
661
  }
@@ -691,15 +670,7 @@ var PublicKey = class _PublicKey {
691
670
  } else if (key instanceof Uint8Array) {
692
671
  key = Buffer.from(key.buffer, key.byteOffset, key.byteLength);
693
672
  }
694
- invariant4(key instanceof Buffer, "Invalid type", {
695
- F: __dxlog_file4,
696
- L: 171,
697
- S: this,
698
- A: [
699
- "key instanceof Buffer",
700
- "'Invalid type'"
701
- ]
702
- });
673
+ invariant4(key instanceof Buffer, "Invalid type");
703
674
  return key.toString("hex");
704
675
  }
705
676
  /**
@@ -710,15 +681,7 @@ var PublicKey = class _PublicKey {
710
681
  return key.toHex();
711
682
  }
712
683
  static fromMultibase32(encoded) {
713
- invariant4(encoded.startsWith("B"), "Invalid multibase32 encoding", {
714
- F: __dxlog_file4,
715
- L: 184,
716
- S: this,
717
- A: [
718
- "encoded.startsWith('B')",
719
- "'Invalid multibase32 encoding'"
720
- ]
721
- });
684
+ invariant4(encoded.startsWith("B"), "Invalid multibase32 encoding");
722
685
  return new _PublicKey(new Uint8Array((0, import_base32_decode3.default)(encoded.slice(1), "RFC4648")));
723
686
  }
724
687
  constructor(_value) {
@@ -745,7 +708,7 @@ var PublicKey = class _PublicKey {
745
708
  toMultibase32() {
746
709
  return "B" + base32Encode(this._value, "RFC4648");
747
710
  }
748
- truncate(length = void 0) {
711
+ truncate(length) {
749
712
  return truncateKey(this, length);
750
713
  }
751
714
  asBuffer() {
@@ -851,13 +814,19 @@ var PublicKey = class _PublicKey {
851
814
  };
852
815
  export {
853
816
  DXN,
817
+ DXN_ECHO_REGEXP,
818
+ FQ_ID_LENGTH,
854
819
  IdentityDid,
855
820
  LOCAL_SPACE_TAG,
821
+ OBJECT_ID_LENGTH,
856
822
  ObjectId,
857
823
  PUBLIC_KEY_LENGTH,
858
824
  PublicKey,
859
825
  QueueSubspaceTags,
860
826
  SECRET_KEY_LENGTH,
861
- SpaceId
827
+ SPACE_ID_LENGTH,
828
+ SimplePRNG,
829
+ SpaceId,
830
+ parseId
862
831
  };
863
832
  //# sourceMappingURL=index.mjs.map