@dxos/keys 0.8.3 → 0.8.4-main.28f8d3d

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.3",
3
+ "version": "0.8.4-main.28f8d3d",
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.14.21",
28
+ "effect": "3.17.7",
31
29
  "ulidx": "^2.3.0",
32
- "@dxos/invariant": "0.8.3",
33
- "@dxos/debug": "0.8.3",
34
- "@dxos/node-std": "0.8.3"
30
+ "@dxos/debug": "0.8.4-main.28f8d3d",
31
+ "@dxos/invariant": "0.8.4-main.28f8d3d",
32
+ "@dxos/node-std": "0.8.4-main.28f8d3d"
35
33
  },
36
34
  "devDependencies": {
37
35
  "base32-decode": "^1.0.0",
package/src/dxn.ts CHANGED
@@ -2,11 +2,12 @@
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 { invariant } from '@dxos/invariant';
9
+ import { type DevtoolsFormatter, devtoolsFormatter, inspectCustom } from '@dxos/debug';
10
+ import { assertArgument, invariant } from '@dxos/invariant';
10
11
 
11
12
  import { ObjectId } from './object-id';
12
13
  import { SpaceId } from './space-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',
@@ -43,6 +46,7 @@ export type QueueSubspaceTag = (typeof QueueSubspaceTags)[keyof typeof QueueSubs
43
46
  * ```
44
47
  */
45
48
  export class DXN {
49
+ // TODO(burdon): Rename to DXN (i.e., DXN.DXN).
46
50
  // TODO(dmaretskyi): Should this be a transformation into the DXN type?
47
51
  static Schema = Schema.NonEmptyString.pipe(
48
52
  Schema.pattern(/^dxn:([^:]+):(?:[^:]+:?)+[^:]$/),
@@ -119,7 +123,7 @@ export class DXN {
119
123
  static tryParse(dxn: string): DXN | undefined {
120
124
  try {
121
125
  return DXN.parse(dxn);
122
- } catch (error) {
126
+ } catch {
123
127
  return undefined;
124
128
  }
125
129
  }
@@ -139,17 +143,27 @@ export class DXN {
139
143
  return new DXN(DXN.kind.TYPE, [typename, version]);
140
144
  }
141
145
 
146
+ /**
147
+ * @example `dxn:echo:BA25QRC2FEWCSAMRP4RZL65LWJ7352CKE:01J00J9B45YHYSGZQTQMSKMGJ6`
148
+ */
149
+ static fromSpaceAndObjectId(spaceId: SpaceId, objectId: ObjectId): DXN {
150
+ assertArgument(SpaceId.isValid(spaceId), `Invalid space ID: ${spaceId}`);
151
+ assertArgument(ObjectId.isValid(objectId), `Invalid object ID: ${objectId}`);
152
+ return new DXN(DXN.kind.ECHO, [spaceId, objectId]);
153
+ }
154
+
142
155
  /**
143
156
  * @example `dxn:echo:@:01J00J9B45YHYSGZQTQMSKMGJ6`
144
157
  */
145
158
  static fromLocalObjectId(id: string): DXN {
159
+ assertArgument(ObjectId.isValid(id), `Invalid object ID: ${id}`);
146
160
  return new DXN(DXN.kind.ECHO, [LOCAL_SPACE_TAG, id]);
147
161
  }
148
162
 
149
163
  static fromQueue(subspaceTag: QueueSubspaceTag, spaceId: SpaceId, queueId: ObjectId, objectId?: ObjectId) {
150
- invariant(SpaceId.isValid(spaceId));
151
- invariant(ObjectId.isValid(queueId));
152
- invariant(!objectId || ObjectId.isValid(objectId));
164
+ assertArgument(SpaceId.isValid(spaceId), `Invalid space ID: ${spaceId}`);
165
+ assertArgument(ObjectId.isValid(queueId), `Invalid queue ID: ${queueId}`);
166
+ assertArgument(!objectId || ObjectId.isValid(objectId), `Invalid object ID: ${objectId}`);
153
167
 
154
168
  return new DXN(DXN.kind.QUEUE, [subspaceTag, spaceId, queueId, ...(objectId ? [objectId] : [])]);
155
169
  }
@@ -158,8 +172,11 @@ export class DXN {
158
172
  #parts: string[];
159
173
 
160
174
  constructor(kind: string, parts: string[]) {
161
- invariant(parts.length > 0);
162
- invariant(parts.every((part) => typeof part === 'string' && part.length > 0 && part.indexOf(':') === -1));
175
+ assertArgument(parts.length > 0, `Invalid DXN: ${parts}`);
176
+ assertArgument(
177
+ parts.every((part) => typeof part === 'string' && part.length > 0 && part.indexOf(':') === -1),
178
+ `Invalid DXN: ${parts}`,
179
+ );
163
180
 
164
181
  // Per-type validation.
165
182
  switch (kind) {
@@ -179,6 +196,35 @@ export class DXN {
179
196
  this.#parts = parts;
180
197
  }
181
198
 
199
+ toString(): DXN.String {
200
+ return `dxn:${this.#kind}:${this.#parts.join(':')}` as DXN.String;
201
+ }
202
+
203
+ toJSON(): string {
204
+ return this.toString();
205
+ }
206
+
207
+ /**
208
+ * Used by Node.js to get textual representation of this object when it's printed with a `console.log` statement.
209
+ */
210
+ [inspectCustom](depth: number, options: InspectOptionsStylized, inspectFn: typeof inspect): string {
211
+ const printControlCode = (code: number) => {
212
+ return `\x1b[${code}m`;
213
+ };
214
+
215
+ return (
216
+ printControlCode(inspectFn.colors.blueBright![0]) + this.toString() + printControlCode(inspectFn.colors.reset![0])
217
+ );
218
+ }
219
+
220
+ get [devtoolsFormatter](): DevtoolsFormatter {
221
+ return {
222
+ header: () => {
223
+ return ['span', { style: 'font-weight: bold;' }, this.toString()];
224
+ },
225
+ };
226
+ }
227
+
182
228
  get parts() {
183
229
  return this.#parts;
184
230
  }
@@ -204,6 +250,7 @@ export class DXN {
204
250
 
205
251
  const [type, version] = this.#parts;
206
252
  return {
253
+ // TODO(wittjosiah): Should be `typename` for consistency.
207
254
  type,
208
255
  version: version as string | undefined,
209
256
  };
@@ -239,29 +286,11 @@ export class DXN {
239
286
  };
240
287
  }
241
288
 
242
- toString(): DXN.String {
243
- return `dxn:${this.#kind}:${this.#parts.join(':')}` as DXN.String;
244
- }
245
-
246
289
  /**
247
- * Used by Node.js to get textual representation of this object when it's printed with a `console.log` statement.
290
+ * Produces a new DXN with the given parts appended.
248
291
  */
249
- [inspectCustom](depth: number, options: InspectOptionsStylized, inspectFn: typeof inspect): string {
250
- const printControlCode = (code: number) => {
251
- return `\x1b[${code}m`;
252
- };
253
-
254
- return (
255
- printControlCode(inspectFn.colors.blueBright![0]) + this.toString() + printControlCode(inspectFn.colors.reset![0])
256
- );
257
- }
258
-
259
- get [devtoolsFormatter](): DevtoolsFormatter {
260
- return {
261
- header: () => {
262
- return ['span', { style: 'font-weight: bold;' }, this.toString()];
263
- },
264
- };
292
+ extend(parts: string[]): DXN {
293
+ return new DXN(this.#kind, [...this.#parts, ...parts]);
265
294
  }
266
295
  }
267
296
 
@@ -299,8 +328,7 @@ export declare namespace DXN {
299
328
 
300
329
  export type EchoDXN = {
301
330
  spaceId?: SpaceId;
302
- // TODO(burdon): Rename objectId.
303
- echoId: string; // TODO(dmaretskyi): ObjectId.
331
+ echoId: string; // TODO(dmaretskyi): Rename to `objectId` and use `ObjectId` for the type.
304
332
  };
305
333
 
306
334
  export type QueueDXN = {
package/src/index.ts CHANGED
@@ -7,4 +7,4 @@ export * from './identity-did';
7
7
  export * from './object-id';
8
8
  export * from './public-key';
9
9
  export * from './space-id';
10
- export * from './types';
10
+ export type * from './types';
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';
@@ -4,7 +4,7 @@
4
4
 
5
5
  export const randomBytes = (length: number) => {
6
6
  // globalThis.crypto is not available in Node.js when running in vitest even though the documentation says it should be.
7
- // eslint-disable-next-line @typescript-eslint/no-var-requires
7
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
8
8
  const webCrypto = globalThis.crypto ?? require('node:crypto').webcrypto;
9
9
 
10
10
  const bytes = new Uint8Array(length);