@dxos/keys 0.8.4-main.fd6878d → 0.8.4-main.fdfb99ef29

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.
@@ -100,19 +100,21 @@ init_inject_globals();
100
100
 
101
101
  // src/dxn.ts
102
102
  init_inject_globals();
103
- import { Schema as Schema3 } from "effect";
103
+ import * as Schema3 from "effect/Schema";
104
104
  import { devtoolsFormatter, inspectCustom } from "@dxos/debug";
105
105
  import { assertArgument, invariant as invariant2 } from "@dxos/invariant";
106
106
 
107
107
  // src/object-id.ts
108
108
  init_inject_globals();
109
- import { Schema } from "effect";
110
- import { ulid } from "ulidx";
109
+ import * as Schema from "effect/Schema";
110
+ import { monotonicFactory } from "ulidx";
111
111
  var ObjectIdSchema = Schema.String.pipe(Schema.pattern(/^[0-7][0-9A-HJKMNP-TV-Z]{25}$/i)).annotations({
112
112
  description: "A Universally Unique Lexicographically Sortable Identifier",
113
113
  pattern: "^[0-7][0-9A-HJKMNP-TV-Z]{25}$"
114
114
  });
115
115
  var ObjectId = class extends ObjectIdSchema {
116
+ static #factory = monotonicFactory();
117
+ static #seedTime = void 0;
116
118
  static isValid(id) {
117
119
  try {
118
120
  Schema.decodeSync(ObjectId)(id);
@@ -122,7 +124,46 @@ var ObjectId = class extends ObjectIdSchema {
122
124
  }
123
125
  }
124
126
  static random() {
125
- return ulid();
127
+ return this.#factory(this.#seedTime);
128
+ }
129
+ static dangerouslyDisableRandomness() {
130
+ this.#factory = monotonicFactory(makeTestPRNG());
131
+ this.#seedTime = (/* @__PURE__ */ new Date("2025-01-01")).getTime();
132
+ }
133
+ };
134
+ var makeTestPRNG = () => {
135
+ const rng = new SimplePRNG();
136
+ return () => {
137
+ return rng.next();
138
+ };
139
+ };
140
+ var SimplePRNG = class _SimplePRNG {
141
+ #seed;
142
+ // LCG parameters (from Numerical Recipes)
143
+ static #a = 1664525;
144
+ static #c = 1013904223;
145
+ static #m = Math.pow(2, 32);
146
+ /**
147
+ * Creates a new PRNG instance.
148
+ * @param seed - Initial seed value. If not provided, uses 0.
149
+ */
150
+ constructor(seed = 0) {
151
+ this.#seed = seed;
152
+ }
153
+ /**
154
+ * Generates the next pseudo-random number in the range [0, 1).
155
+ * @returns A pseudo-random number between 0 (inclusive) and 1 (exclusive).
156
+ */
157
+ next() {
158
+ this.#seed = (_SimplePRNG.#a * this.#seed + _SimplePRNG.#c) % _SimplePRNG.#m;
159
+ return this.#seed / _SimplePRNG.#m;
160
+ }
161
+ /**
162
+ * Resets the generator with a new seed.
163
+ * @param seed - New seed value.
164
+ */
165
+ reset(seed) {
166
+ this.#seed = seed;
126
167
  }
127
168
  };
128
169
 
@@ -194,7 +235,7 @@ function base32Encode(data, variant, options) {
194
235
  }
195
236
 
196
237
  // src/space-id.ts
197
- import { Schema as Schema2 } from "effect";
238
+ import * as Schema2 from "effect/Schema";
198
239
  import { invariant } from "@dxos/invariant";
199
240
 
200
241
  // src/random-bytes.ts
@@ -207,7 +248,6 @@ var randomBytes = (length) => {
207
248
  };
208
249
 
209
250
  // src/space-id.ts
210
- var __dxlog_file = "/__w/dxos/dxos/packages/common/keys/src/space-id.ts";
211
251
  var MULTIBASE_PREFIX = "B";
212
252
  var ENCODED_LENGTH = 33;
213
253
  var isValid = (value) => {
@@ -216,36 +256,12 @@ var isValid = (value) => {
216
256
  var SpaceId = class extends Schema2.String.pipe(Schema2.filter(isValid)) {
217
257
  static byteLength = 20;
218
258
  static encode = (value) => {
219
- invariant(value instanceof Uint8Array, "Invalid type", {
220
- F: __dxlog_file,
221
- L: 43,
222
- S: this,
223
- A: [
224
- "value instanceof Uint8Array",
225
- "'Invalid type'"
226
- ]
227
- });
228
- invariant(value.length === SpaceId.byteLength, "Invalid length", {
229
- F: __dxlog_file,
230
- L: 44,
231
- S: this,
232
- A: [
233
- "value.length === SpaceId.byteLength",
234
- "'Invalid length'"
235
- ]
236
- });
259
+ invariant(value instanceof Uint8Array, "Invalid type");
260
+ invariant(value.length === SpaceId.byteLength, "Invalid length");
237
261
  return MULTIBASE_PREFIX + base32Encode(value, "RFC4648");
238
262
  };
239
263
  static decode = (value) => {
240
- invariant(value.startsWith(MULTIBASE_PREFIX), "Invalid multibase32 encoding", {
241
- F: __dxlog_file,
242
- L: 49,
243
- S: this,
244
- A: [
245
- "value.startsWith(MULTIBASE_PREFIX)",
246
- "'Invalid multibase32 encoding'"
247
- ]
248
- });
264
+ invariant(value.startsWith(MULTIBASE_PREFIX), "Invalid multibase32 encoding");
249
265
  return new Uint8Array((0, import_base32_decode.default)(value.slice(1), "RFC4648"));
250
266
  };
251
267
  static isValid = isValid;
@@ -255,7 +271,6 @@ var SpaceId = class extends Schema2.String.pipe(Schema2.filter(isValid)) {
255
271
  };
256
272
 
257
273
  // src/dxn.ts
258
- var __dxlog_file2 = "/__w/dxos/dxos/packages/common/keys/src/dxn.ts";
259
274
  var LOCAL_SPACE_TAG = "@";
260
275
  var DXN_ECHO_REGEXP = /@(dxn:[a-zA-Z0-p:@]+)/;
261
276
  var QueueSubspaceTags = Object.freeze({
@@ -268,12 +283,12 @@ var DXN = class _DXN {
268
283
  static Schema = Schema3.NonEmptyString.pipe(
269
284
  Schema3.pattern(/^dxn:([^:]+):(?:[^:]+:?)+[^:]$/),
270
285
  // TODO(dmaretskyi): To set the format we need to move the annotation IDs out of the echo-schema package.
271
- // FormatAnnotation.set(FormatEnum.DXN),
286
+ // FormatAnnotation.set(TypeFormat.DXN),
272
287
  Schema3.annotations({
273
288
  title: "DXN",
274
289
  description: "DXN URI",
275
290
  examples: [
276
- "dxn:type:example.com/type/MyType",
291
+ "dxn:type:com.example.type.my-type",
277
292
  "dxn:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6"
278
293
  ]
279
294
  })
@@ -286,15 +301,15 @@ var DXN = class _DXN {
286
301
  */
287
302
  static kind = Object.freeze({
288
303
  /**
289
- * dxn:type:<type name>[:<version>]
304
+ * dxn:type:<type_name>[:<version>]
290
305
  */
291
306
  TYPE: "type",
292
307
  /**
293
- * dxn:echo:<space id>:<echo id>
294
- * dxn:echo:@:<echo id>
308
+ * dxn:echo:<space_id>:<echo_id>
309
+ * dxn:echo:@:<echo_id>
295
310
  */
296
- // TODO(burdon): Rename to OBJECT? (BREAKING CHANGE).
297
- // TODO(burdon): Add separate Kind for space.
311
+ // TODO(burdon): Rename to OBJECT? (BREAKING CHANGE to update "echo").
312
+ // TODO(burdon): Add separate Kind for space?
298
313
  ECHO: "echo",
299
314
  /**
300
315
  * The subspace tag enables us to partition queues by usage within the context of a space.
@@ -304,12 +319,17 @@ var DXN = class _DXN {
304
319
  */
305
320
  QUEUE: "queue"
306
321
  });
307
- get kind() {
308
- return this.#kind;
309
- }
322
+ /**
323
+ * Exactly equals.
324
+ */
310
325
  static equals(a, b) {
311
326
  return a.kind === b.kind && a.parts.length === b.parts.length && a.parts.every((part, i) => part === b.parts[i]);
312
327
  }
328
+ static equalsEchoId(a, b) {
329
+ const a1 = a.asEchoDXN();
330
+ const b1 = b.asEchoDXN();
331
+ return !!a1 && !!b1 && a1.echoId === b1.echoId;
332
+ }
313
333
  // TODO(burdon): Rename isValid.
314
334
  static isDXNString(dxn) {
315
335
  return dxn.startsWith("dxn:");
@@ -338,7 +358,7 @@ var DXN = class _DXN {
338
358
  }
339
359
  }
340
360
  /**
341
- * @example `dxn:type:example.com/type/Contact`
361
+ * @example `dxn:type:com.example.type.person`
342
362
  */
343
363
  static fromTypename(typename) {
344
364
  return new _DXN(_DXN.kind.TYPE, [
@@ -346,7 +366,7 @@ var DXN = class _DXN {
346
366
  ]);
347
367
  }
348
368
  /**
349
- * @example `dxn:type:example.com/type/Contact:0.1.0`
369
+ * @example `dxn:type:com.example.type.person:0.1.0`
350
370
  */
351
371
  // TODO(dmaretskyi): Consider using @ as the version separator.
352
372
  static fromTypenameAndVersion(typename, version) {
@@ -360,7 +380,7 @@ var DXN = class _DXN {
360
380
  */
361
381
  static fromSpaceAndObjectId(spaceId, objectId) {
362
382
  assertArgument(SpaceId.isValid(spaceId), `Invalid space ID: ${spaceId}`);
363
- assertArgument(ObjectId.isValid(objectId), `Invalid object ID: ${objectId}`);
383
+ assertArgument(ObjectId.isValid(objectId), "objectId", `Invalid object ID: ${objectId}`);
364
384
  return new _DXN(_DXN.kind.ECHO, [
365
385
  spaceId,
366
386
  objectId
@@ -370,7 +390,7 @@ var DXN = class _DXN {
370
390
  * @example `dxn:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6`
371
391
  */
372
392
  static fromLocalObjectId(id) {
373
- assertArgument(ObjectId.isValid(id), `Invalid object ID: ${id}`);
393
+ assertArgument(ObjectId.isValid(id), "id", `Invalid object ID: ${id}`);
374
394
  return new _DXN(_DXN.kind.ECHO, [
375
395
  LOCAL_SPACE_TAG,
376
396
  id
@@ -378,8 +398,8 @@ var DXN = class _DXN {
378
398
  }
379
399
  static fromQueue(subspaceTag, spaceId, queueId, objectId) {
380
400
  assertArgument(SpaceId.isValid(spaceId), `Invalid space ID: ${spaceId}`);
381
- assertArgument(ObjectId.isValid(queueId), `Invalid queue ID: ${queueId}`);
382
- assertArgument(!objectId || ObjectId.isValid(objectId), `Invalid object ID: ${objectId}`);
401
+ assertArgument(ObjectId.isValid(queueId), "queueId", `Invalid queue ID: ${queueId}`);
402
+ assertArgument(!objectId || ObjectId.isValid(objectId), "objectId", `Invalid object ID: ${objectId}`);
383
403
  return new _DXN(_DXN.kind.QUEUE, [
384
404
  subspaceTag,
385
405
  spaceId,
@@ -392,17 +412,17 @@ var DXN = class _DXN {
392
412
  #kind;
393
413
  #parts;
394
414
  constructor(kind, parts) {
395
- assertArgument(parts.length > 0, `Invalid DXN: ${parts}`);
396
- assertArgument(parts.every((part) => typeof part === "string" && part.length > 0 && part.indexOf(":") === -1), `Invalid DXN: ${parts}`);
415
+ assertArgument(parts.length > 0, "parts", `Invalid DXN: ${parts}`);
416
+ assertArgument(parts.every((part) => typeof part === "string" && part.length > 0 && part.indexOf(":") === -1), "parts", `Invalid DXN: ${parts}`);
397
417
  switch (kind) {
398
418
  case _DXN.kind.TYPE:
399
419
  if (parts.length > 2) {
400
- throw new Error('Invalid "type" DXN');
420
+ throw new Error("Invalid DXN.kind.TYPE");
401
421
  }
402
422
  break;
403
423
  case _DXN.kind.ECHO:
404
424
  if (parts.length !== 2) {
405
- throw new Error('Invalid "echo" DXN');
425
+ throw new Error("Invalid DXN.kind.ECHO");
406
426
  }
407
427
  break;
408
428
  }
@@ -437,22 +457,20 @@ var DXN = class _DXN {
437
457
  }
438
458
  };
439
459
  }
460
+ get kind() {
461
+ return this.#kind;
462
+ }
440
463
  get parts() {
441
464
  return this.#parts;
442
465
  }
443
466
  // TODO(burdon): Should getters fail?
444
467
  get typename() {
445
- invariant2(this.#kind === _DXN.kind.TYPE, void 0, {
446
- F: __dxlog_file2,
447
- L: 234,
448
- S: this,
449
- A: [
450
- "this.#kind === DXN.kind.TYPE",
451
- ""
452
- ]
453
- });
468
+ invariant2(this.#kind === _DXN.kind.TYPE);
454
469
  return this.#parts[0];
455
470
  }
471
+ equals(other) {
472
+ return _DXN.equals(this, other);
473
+ }
456
474
  hasTypenameOf(typename) {
457
475
  return this.#kind === _DXN.kind.TYPE && this.#parts.length === 1 && this.#parts[0] === typename;
458
476
  }
@@ -477,6 +495,7 @@ var DXN = class _DXN {
477
495
  const [spaceId, echoId] = this.#parts;
478
496
  return {
479
497
  spaceId: spaceId === LOCAL_SPACE_TAG ? void 0 : spaceId,
498
+ // TODO(burdon): objectId.
480
499
  echoId
481
500
  };
482
501
  }
@@ -510,40 +529,15 @@ var DXN = class _DXN {
510
529
  init_inject_globals();
511
530
  var import_base32_decode2 = __toESM(require_base32_decode(), 1);
512
531
  import { invariant as invariant3 } from "@dxos/invariant";
513
- var __dxlog_file3 = "/__w/dxos/dxos/packages/common/keys/src/identity-did.ts";
514
532
  var IdentityDid = Object.freeze({
515
533
  byteLength: 20,
516
534
  encode: (value) => {
517
- invariant3(value instanceof Uint8Array, "Invalid type", {
518
- F: __dxlog_file3,
519
- L: 22,
520
- S: void 0,
521
- A: [
522
- "value instanceof Uint8Array",
523
- "'Invalid type'"
524
- ]
525
- });
526
- invariant3(value.length === IdentityDid.byteLength, "Invalid length", {
527
- F: __dxlog_file3,
528
- L: 23,
529
- S: void 0,
530
- A: [
531
- "value.length === IdentityDid.byteLength",
532
- "'Invalid length'"
533
- ]
534
- });
535
+ invariant3(value instanceof Uint8Array, "Invalid type");
536
+ invariant3(value.length === IdentityDid.byteLength, "Invalid length");
535
537
  return DID_PREFIX + MULTIBASE_PREFIX2 + base32Encode(value, "RFC4648");
536
538
  },
537
539
  decode: (value) => {
538
- invariant3(value.startsWith(DID_PREFIX + MULTIBASE_PREFIX2), "Invalid multibase32 encoding", {
539
- F: __dxlog_file3,
540
- L: 28,
541
- S: void 0,
542
- A: [
543
- "value.startsWith(DID_PREFIX + MULTIBASE_PREFIX)",
544
- "'Invalid multibase32 encoding'"
545
- ]
546
- });
540
+ invariant3(value.startsWith(DID_PREFIX + MULTIBASE_PREFIX2), "Invalid multibase32 encoding");
547
541
  return new Uint8Array((0, import_base32_decode2.default)(value.slice(10), "RFC4648"));
548
542
  },
549
543
  isValid: (value) => {
@@ -562,7 +556,6 @@ init_inject_globals();
562
556
  var import_base32_decode3 = __toESM(require_base32_decode(), 1);
563
557
  import { devtoolsFormatter as devtoolsFormatter2, equalsSymbol, inspectCustom as inspectCustom2, truncateKey } from "@dxos/debug";
564
558
  import { invariant as invariant4 } from "@dxos/invariant";
565
- var __dxlog_file4 = "/__w/dxos/dxos/packages/common/keys/src/public-key.ts";
566
559
  var PUBLIC_KEY_LENGTH = 32;
567
560
  var SECRET_KEY_LENGTH = 64;
568
561
  var isLikeArrayBuffer = (value) => typeof value === "object" && value !== null && Object.getPrototypeOf(value).constructor.name === "ArrayBuffer";
@@ -575,15 +568,7 @@ var PublicKey = class _PublicKey {
575
568
  * @returns PublicKey
576
569
  */
577
570
  static from(source) {
578
- invariant4(source, void 0, {
579
- F: __dxlog_file4,
580
- L: 50,
581
- S: this,
582
- A: [
583
- "source",
584
- ""
585
- ]
586
- });
571
+ invariant4(source);
587
572
  if (source instanceof _PublicKey) {
588
573
  return source;
589
574
  } else if (source instanceof Buffer2) {
@@ -667,15 +652,7 @@ var PublicKey = class _PublicKey {
667
652
  * @deprecated All keys should be represented as instances of PublicKey.
668
653
  */
669
654
  static bufferize(str) {
670
- invariant4(typeof str === "string", "Invalid type", {
671
- F: __dxlog_file4,
672
- L: 153,
673
- S: this,
674
- A: [
675
- "typeof str === 'string'",
676
- "'Invalid type'"
677
- ]
678
- });
655
+ invariant4(typeof str === "string", "Invalid type");
679
656
  const buffer = Buffer2.from(str, "hex");
680
657
  return buffer;
681
658
  }
@@ -690,15 +667,7 @@ var PublicKey = class _PublicKey {
690
667
  } else if (key instanceof Uint8Array) {
691
668
  key = Buffer2.from(key.buffer, key.byteOffset, key.byteLength);
692
669
  }
693
- invariant4(key instanceof Buffer2, "Invalid type", {
694
- F: __dxlog_file4,
695
- L: 172,
696
- S: this,
697
- A: [
698
- "key instanceof Buffer",
699
- "'Invalid type'"
700
- ]
701
- });
670
+ invariant4(key instanceof Buffer2, "Invalid type");
702
671
  return key.toString("hex");
703
672
  }
704
673
  /**
@@ -709,15 +678,7 @@ var PublicKey = class _PublicKey {
709
678
  return key.toHex();
710
679
  }
711
680
  static fromMultibase32(encoded) {
712
- invariant4(encoded.startsWith("B"), "Invalid multibase32 encoding", {
713
- F: __dxlog_file4,
714
- L: 185,
715
- S: this,
716
- A: [
717
- "encoded.startsWith('B')",
718
- "'Invalid multibase32 encoding'"
719
- ]
720
- });
681
+ invariant4(encoded.startsWith("B"), "Invalid multibase32 encoding");
721
682
  return new _PublicKey(new Uint8Array((0, import_base32_decode3.default)(encoded.slice(1), "RFC4648")));
722
683
  }
723
684
  constructor(_value) {
@@ -744,7 +705,7 @@ var PublicKey = class _PublicKey {
744
705
  toMultibase32() {
745
706
  return "B" + base32Encode(this._value, "RFC4648");
746
707
  }
747
- truncate(length = void 0) {
708
+ truncate(length) {
748
709
  return truncateKey(this, length);
749
710
  }
750
711
  asBuffer() {
@@ -858,6 +819,7 @@ export {
858
819
  PublicKey,
859
820
  QueueSubspaceTags,
860
821
  SECRET_KEY_LENGTH,
822
+ SimplePRNG,
861
823
  SpaceId
862
824
  };
863
825
  //# sourceMappingURL=index.mjs.map