@dxos/echo-protocol 0.6.12 → 0.6.13-main.548ca8d

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.
@@ -1,5 +1,24 @@
1
1
  // packages/core/echo/echo-protocol/src/reference.ts
2
- import { DXN, LOCAL_SPACE_TAG } from "@dxos/keys";
2
+ import { DXN, LOCAL_SPACE_TAG, PublicKey as PublicKey2 } from "@dxos/keys";
3
+
4
+ // packages/core/echo/echo-protocol/src/space-id.ts
5
+ import { subtleCrypto } from "@dxos/crypto";
6
+ import { PublicKey, SpaceId } from "@dxos/keys";
7
+ import { ComplexMap } from "@dxos/util";
8
+ var SPACE_IDS_CACHE = new ComplexMap(PublicKey.hash);
9
+ var createIdFromSpaceKey = async (spaceKey) => {
10
+ const cachedValue = SPACE_IDS_CACHE.get(spaceKey);
11
+ if (cachedValue !== void 0) {
12
+ return cachedValue;
13
+ }
14
+ const digest = await subtleCrypto.digest("SHA-256", spaceKey.asUint8Array());
15
+ const bytes = new Uint8Array(digest).slice(0, SpaceId.byteLength);
16
+ const spaceId = SpaceId.encode(bytes);
17
+ SPACE_IDS_CACHE.set(spaceKey, spaceId);
18
+ return spaceId;
19
+ };
20
+
21
+ // packages/core/echo/echo-protocol/src/reference.ts
3
22
  var Reference = class _Reference {
4
23
  static {
5
24
  /**
@@ -70,6 +89,17 @@ var encodeReference = (reference) => ({
70
89
  });
71
90
  var decodeReference = (value) => Reference.fromDXN(DXN.parse(value["/"]));
72
91
  var isEncodedReference = (value) => typeof value === "object" && value !== null && Object.keys(value).length === 1 && typeof value["/"] === "string";
92
+ var convertLegacyReference = async (reference) => {
93
+ if (reference.protocol === Reference.TYPE_PROTOCOL) {
94
+ return encodeReference(Reference.fromLegacyTypename(reference.itemId));
95
+ }
96
+ if (!reference.itemId) {
97
+ throw new Error("Invalid reference");
98
+ }
99
+ const spaceKey = reference.host;
100
+ const spaceId = spaceKey != null ? await createIdFromSpaceKey(PublicKey2.fromHex(spaceKey)) : void 0;
101
+ return encodeReference(new Reference(reference.itemId, reference.protocol ?? void 0, spaceId));
102
+ };
73
103
 
74
104
  // packages/core/echo/echo-protocol/src/space-doc-version.ts
75
105
  var SpaceDocVersion = Object.freeze({
@@ -93,6 +123,8 @@ export {
93
123
  REFERENCE_TYPE_TAG,
94
124
  Reference,
95
125
  SpaceDocVersion,
126
+ convertLegacyReference,
127
+ createIdFromSpaceKey,
96
128
  decodeReference,
97
129
  encodeReference,
98
130
  isEncodedReference,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../../../src/reference.ts", "../../../src/space-doc-version.ts", "../../../src/legacy.ts"],
4
- "sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nimport { DXN, LOCAL_SPACE_TAG } from '@dxos/keys';\nimport { type ObjectId } from '@dxos/protocols';\nimport { type Reference as ReferenceProto } from '@dxos/protocols/proto/dxos/echo/model/document';\n\n/**\n * Runtime representation of object reference.\n */\nexport class Reference {\n /**\n * Protocol references to runtime registered types.\n */\n static TYPE_PROTOCOL = 'protobuf';\n\n static fromValue(value: ReferenceProto): Reference {\n return new Reference(value.objectId, value.protocol, value.host);\n }\n\n /**\n * @deprecated\n */\n // TODO(burdon): Document/remove?\n static fromLegacyTypename(type: string): Reference {\n return new Reference(type, Reference.TYPE_PROTOCOL, 'dxos.org');\n }\n\n static fromDXN(dxn: DXN): Reference {\n switch (dxn.kind) {\n case DXN.kind.TYPE:\n return Reference.fromLegacyTypename(dxn.parts[0]);\n case DXN.kind.ECHO:\n if (dxn.parts[0] === LOCAL_SPACE_TAG) {\n return new Reference(dxn.parts[1]);\n } else {\n return new Reference(dxn.parts[1], undefined, dxn.parts[0]);\n }\n default:\n throw new Error(`Unsupported DXN kind: ${dxn.kind}`);\n }\n }\n\n // prettier-ignore\n constructor(\n public readonly objectId: ObjectId,\n public readonly protocol?: string,\n public readonly host?: string\n ) {}\n\n encode(): ReferenceProto {\n return { objectId: this.objectId, host: this.host, protocol: this.protocol };\n }\n\n toDXN(): DXN {\n if (this.protocol === Reference.TYPE_PROTOCOL) {\n return new DXN(DXN.kind.TYPE, [this.objectId]);\n } else {\n if (this.host) {\n // Host is assumed to be the space key.\n // The DXN should actually have the space ID.\n // TODO(dmaretskyi): Migrate to space id.\n return new DXN(DXN.kind.ECHO, [this.host, this.objectId]);\n } else {\n return new DXN(DXN.kind.ECHO, [LOCAL_SPACE_TAG, this.objectId]);\n }\n }\n }\n}\n\nexport const REFERENCE_TYPE_TAG = 'dxos.echo.model.document.Reference';\n\n/**\n * Reference as it is stored in Automerge document.\n */\nexport type EncodedReference = {\n '/': string;\n};\n\nexport const encodeReference = (reference: Reference): EncodedReference => ({\n '/': reference.toDXN().toString(),\n});\n\nexport const decodeReference = (value: any) => Reference.fromDXN(DXN.parse(value['/']));\n\nexport const isEncodedReference = (value: any): value is EncodedReference =>\n typeof value === 'object' && value !== null && Object.keys(value).length === 1 && typeof value['/'] === 'string';\n", "//\n// Copyright 2024 DXOS.org\n//\n\n/**\n * Denotes the data version of the space automerge document as well as the leaf documents for each individual ECHO object.\n */\nexport type SpaceDocVersion = number & { __type: 'SpaceDocVersion' };\n\nexport const SpaceDocVersion = Object.freeze({\n /**\n * For the documents created before the versioning was introduced.\n */\n LEGACY: 0 as SpaceDocVersion,\n\n /**\n * Current version.\n */\n CURRENT: 1 as SpaceDocVersion,\n});\n", "//\n// Copyright 2024 DXOS.org\n//\n\nexport const LEGACY_REFERENCE_TYPE_TAG = 'dxos.echo.model.document.Reference';\n\n/**\n * Reference as it is stored in Automerge document.\n */\nexport type LegacyEncodedReferenceObject = {\n '@type': typeof LEGACY_REFERENCE_TYPE_TAG;\n itemId: string;\n protocol?: string;\n host?: string;\n};\n\nexport const isLegacyReference = (value: any): boolean =>\n typeof value === 'object' && value !== null && value['@type'] === LEGACY_REFERENCE_TYPE_TAG;\n\nexport const LEGACY_TYPE_PROPERTIES = 'dxos.sdk.client.Properties';\n"],
5
- "mappings": ";AAI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F;AACF;AAEO,IAAMkB,qBAAqB;AAS3B,IAAMC,kBAAkB,CAACC,eAA4C;EAC1E,KAAKA,UAAUH,MAAK,EAAGI,SAAQ;AACjC;AAEO,IAAMC,kBAAkB,CAACvB,UAAeH,UAAUS,QAAQG,IAAIe,MAAMxB,MAAM,GAAA,CAAI,CAAA;AAE9E,IAAMyB,qBAAqB,CAACzB,UACjC,OAAOA,UAAU,YAAYA,UAAU,QAAQ0B,OAAOC,KAAK3B,KAAAA,EAAO4B,WAAW,KAAK,OAAO5B,MAAM,GAAA,MAAS;;;AC9EnG,IAAM6B,kBAAkBC,OAAOC,OAAO;;;;EAI3CC,QAAQ;;;;EAKRC,SAAS;AACX,CAAA;;;ACfO,IAAMC,4BAA4B;AAYlC,IAAMC,oBAAoB,CAACC,UAChC,OAAOA,UAAU,YAAYA,UAAU,QAAQA,MAAM,OAAA,MAAaF;AAE7D,IAAMG,yBAAyB;",
6
- "names": ["DXN", "LOCAL_SPACE_TAG", "Reference", "TYPE_PROTOCOL", "fromValue", "value", "objectId", "protocol", "host", "fromLegacyTypename", "type", "fromDXN", "dxn", "kind", "DXN", "TYPE", "parts", "ECHO", "LOCAL_SPACE_TAG", "undefined", "Error", "constructor", "encode", "toDXN", "REFERENCE_TYPE_TAG", "encodeReference", "reference", "toString", "decodeReference", "parse", "isEncodedReference", "Object", "keys", "length", "SpaceDocVersion", "Object", "freeze", "LEGACY", "CURRENT", "LEGACY_REFERENCE_TYPE_TAG", "isLegacyReference", "value", "LEGACY_TYPE_PROPERTIES"]
3
+ "sources": ["../../../src/reference.ts", "../../../src/space-id.ts", "../../../src/space-doc-version.ts", "../../../src/legacy.ts"],
4
+ "sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nimport { DXN, LOCAL_SPACE_TAG, PublicKey } from '@dxos/keys';\nimport { type ObjectId } from '@dxos/protocols';\nimport { type Reference as ReferenceProto } from '@dxos/protocols/proto/dxos/echo/model/document';\n\nimport type { LegacyEncodedReferenceObject } from './legacy';\nimport { createIdFromSpaceKey } from './space-id';\n\n/**\n * Runtime representation of object reference.\n */\nexport class Reference {\n /**\n * Protocol references to runtime registered types.\n */\n static TYPE_PROTOCOL = 'protobuf';\n\n static fromValue(value: ReferenceProto): Reference {\n return new Reference(value.objectId, value.protocol, value.host);\n }\n\n /**\n * @deprecated\n */\n // TODO(burdon): Document/remove?\n static fromLegacyTypename(type: string): Reference {\n return new Reference(type, Reference.TYPE_PROTOCOL, 'dxos.org');\n }\n\n static fromDXN(dxn: DXN): Reference {\n switch (dxn.kind) {\n case DXN.kind.TYPE:\n return Reference.fromLegacyTypename(dxn.parts[0]);\n case DXN.kind.ECHO:\n if (dxn.parts[0] === LOCAL_SPACE_TAG) {\n return new Reference(dxn.parts[1]);\n } else {\n return new Reference(dxn.parts[1], undefined, dxn.parts[0]);\n }\n default:\n throw new Error(`Unsupported DXN kind: ${dxn.kind}`);\n }\n }\n\n // prettier-ignore\n constructor(\n public readonly objectId: ObjectId,\n public readonly protocol?: string,\n public readonly host?: string\n ) {}\n\n encode(): ReferenceProto {\n return { objectId: this.objectId, host: this.host, protocol: this.protocol };\n }\n\n toDXN(): DXN {\n if (this.protocol === Reference.TYPE_PROTOCOL) {\n return new DXN(DXN.kind.TYPE, [this.objectId]);\n } else {\n if (this.host) {\n // Host is assumed to be the space key.\n // The DXN should actually have the space ID.\n // TODO(dmaretskyi): Migrate to space id.\n return new DXN(DXN.kind.ECHO, [this.host, this.objectId]);\n } else {\n return new DXN(DXN.kind.ECHO, [LOCAL_SPACE_TAG, this.objectId]);\n }\n }\n }\n}\n\nexport const REFERENCE_TYPE_TAG = 'dxos.echo.model.document.Reference';\n\n/**\n * Reference as it is stored in Automerge document.\n */\nexport type EncodedReference = {\n '/': string;\n};\n\nexport const encodeReference = (reference: Reference): EncodedReference => ({\n '/': reference.toDXN().toString(),\n});\n\nexport const decodeReference = (value: any) => Reference.fromDXN(DXN.parse(value['/']));\n\nexport const isEncodedReference = (value: any): value is EncodedReference =>\n typeof value === 'object' && value !== null && Object.keys(value).length === 1 && typeof value['/'] === 'string';\n\nexport const convertLegacyReference = async (reference: LegacyEncodedReferenceObject): Promise<EncodedReference> => {\n if (reference.protocol === Reference.TYPE_PROTOCOL) {\n return encodeReference(Reference.fromLegacyTypename(reference.itemId));\n }\n if (!reference.itemId) {\n throw new Error('Invalid reference');\n }\n\n const spaceKey = reference.host;\n const spaceId = spaceKey != null ? await createIdFromSpaceKey(PublicKey.fromHex(spaceKey)) : undefined;\n return encodeReference(new Reference(reference.itemId, reference.protocol ?? undefined, spaceId));\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { subtleCrypto } from '@dxos/crypto';\nimport { PublicKey, SpaceId } from '@dxos/keys';\nimport { ComplexMap } from '@dxos/util';\n\nconst SPACE_IDS_CACHE = new ComplexMap<PublicKey, SpaceId>(PublicKey.hash);\n\n/**\n * Space keys are generated by creating a keypair, and then taking the first 20 bytes of the SHA-256 hash of the public key and encoding them to multibase RFC4648 base-32 format (prefixed with B, see Multibase Table).\n * Inspired by how ethereum addresses are derived.\n */\nexport const createIdFromSpaceKey = async (spaceKey: PublicKey): Promise<SpaceId> => {\n const cachedValue = SPACE_IDS_CACHE.get(spaceKey);\n if (cachedValue !== undefined) {\n return cachedValue;\n }\n\n const digest = await subtleCrypto.digest('SHA-256', spaceKey.asUint8Array());\n\n const bytes = new Uint8Array(digest).slice(0, SpaceId.byteLength);\n const spaceId = SpaceId.encode(bytes);\n SPACE_IDS_CACHE.set(spaceKey, spaceId);\n return spaceId;\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\n/**\n * Denotes the data version of the space automerge document as well as the leaf documents for each individual ECHO object.\n */\nexport type SpaceDocVersion = number & { __type: 'SpaceDocVersion' };\n\nexport const SpaceDocVersion = Object.freeze({\n /**\n * For the documents created before the versioning was introduced.\n */\n LEGACY: 0 as SpaceDocVersion,\n\n /**\n * Current version.\n */\n CURRENT: 1 as SpaceDocVersion,\n});\n", "//\n// Copyright 2024 DXOS.org\n//\n\nexport const LEGACY_REFERENCE_TYPE_TAG = 'dxos.echo.model.document.Reference';\n\n/**\n * Reference as it is stored in Automerge document.\n */\nexport type LegacyEncodedReferenceObject = {\n '@type': typeof LEGACY_REFERENCE_TYPE_TAG;\n itemId: string;\n protocol?: string;\n host?: string;\n};\n\nexport const isLegacyReference = (value: any): boolean =>\n typeof value === 'object' && value !== null && value['@type'] === LEGACY_REFERENCE_TYPE_TAG;\n\nexport const LEGACY_TYPE_PROPERTIES = 'dxos.sdk.client.Properties';\n"],
5
+ "mappings": ";AAIA,SAASA,KAAKC,iBAAiBC,aAAAA,kBAAiB;;;ACAhD,SAASC,oBAAoB;AAC7B,SAASC,WAAWC,eAAe;AACnC,SAASC,kBAAkB;AAE3B,IAAMC,kBAAkB,IAAIC,WAA+BC,UAAUC,IAAI;AAMlE,IAAMC,uBAAuB,OAAOC,aAAAA;AACzC,QAAMC,cAAcN,gBAAgBO,IAAIF,QAAAA;AACxC,MAAIC,gBAAgBE,QAAW;AAC7B,WAAOF;EACT;AAEA,QAAMG,SAAS,MAAMC,aAAaD,OAAO,WAAWJ,SAASM,aAAY,CAAA;AAEzE,QAAMC,QAAQ,IAAIC,WAAWJ,MAAAA,EAAQK,MAAM,GAAGC,QAAQC,UAAU;AAChE,QAAMC,UAAUF,QAAQG,OAAON,KAAAA;AAC/BZ,kBAAgBmB,IAAId,UAAUY,OAAAA;AAC9B,SAAOA;AACT;;;ADZO,IAAMG,YAAN,MAAMA,WAAAA;EAIX;;;;SAAOC,gBAAgB;;EAEvB,OAAOC,UAAUC,OAAkC;AACjD,WAAO,IAAIH,WAAUG,MAAMC,UAAUD,MAAME,UAAUF,MAAMG,IAAI;EACjE;;;;;EAMA,OAAOC,mBAAmBC,MAAyB;AACjD,WAAO,IAAIR,WAAUQ,MAAMR,WAAUC,eAAe,UAAA;EACtD;EAEA,OAAOQ,QAAQC,KAAqB;AAClC,YAAQA,IAAIC,MAAI;MACd,KAAKC,IAAID,KAAKE;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yBAAyB,OAAOR,cAAAA;AAC3C,MAAIA,UAAUnB,aAAaL,UAAUC,eAAe;AAClD,WAAOsB,gBAAgBvB,UAAUO,mBAAmBiB,UAAUS,MAAM,CAAA;EACtE;AACA,MAAI,CAACT,UAAUS,QAAQ;AACrB,UAAM,IAAIf,MAAM,mBAAA;EAClB;AAEA,QAAMgB,WAAWV,UAAUlB;AAC3B,QAAM6B,UAAUD,YAAY,OAAO,MAAME,qBAAqBC,WAAUC,QAAQJ,QAAAA,CAAAA,IAAajB;AAC7F,SAAOM,gBAAgB,IAAIvB,UAAUwB,UAAUS,QAAQT,UAAUnB,YAAYY,QAAWkB,OAAAA,CAAAA;AAC1F;;;AE9FO,IAAMI,kBAAkBC,OAAOC,OAAO;;;;EAI3CC,QAAQ;;;;EAKRC,SAAS;AACX,CAAA;;;ACfO,IAAMC,4BAA4B;AAYlC,IAAMC,oBAAoB,CAACC,UAChC,OAAOA,UAAU,YAAYA,UAAU,QAAQA,MAAM,OAAA,MAAaF;AAE7D,IAAMG,yBAAyB;",
6
+ "names": ["DXN", "LOCAL_SPACE_TAG", "PublicKey", "subtleCrypto", "PublicKey", "SpaceId", "ComplexMap", "SPACE_IDS_CACHE", "ComplexMap", "PublicKey", "hash", "createIdFromSpaceKey", "spaceKey", "cachedValue", "get", "undefined", "digest", "subtleCrypto", "asUint8Array", "bytes", "Uint8Array", "slice", "SpaceId", "byteLength", "spaceId", "encode", "set", "Reference", "TYPE_PROTOCOL", "fromValue", "value", "objectId", "protocol", "host", "fromLegacyTypename", "type", "fromDXN", "dxn", "kind", "DXN", "TYPE", "parts", "ECHO", "LOCAL_SPACE_TAG", "undefined", "Error", "constructor", "encode", "toDXN", "REFERENCE_TYPE_TAG", "encodeReference", "reference", "toString", "decodeReference", "parse", "isEncodedReference", "Object", "keys", "length", "convertLegacyReference", "itemId", "spaceKey", "spaceId", "createIdFromSpaceKey", "PublicKey", "fromHex", "SpaceDocVersion", "Object", "freeze", "LEGACY", "CURRENT", "LEGACY_REFERENCE_TYPE_TAG", "isLegacyReference", "value", "LEGACY_TYPE_PROPERTIES"]
7
7
  }
@@ -1 +1 @@
1
- {"inputs":{"packages/core/echo/echo-protocol/src/document-structure.ts":{"bytes":2528,"imports":[],"format":"esm"},"packages/core/echo/echo-protocol/src/reference.ts":{"bytes":9351,"imports":[{"path":"@dxos/keys","kind":"import-statement","external":true}],"format":"esm"},"packages/core/echo/echo-protocol/src/space-doc-version.ts":{"bytes":1591,"imports":[],"format":"esm"},"packages/core/echo/echo-protocol/src/legacy.ts":{"bytes":1719,"imports":[],"format":"esm"},"packages/core/echo/echo-protocol/src/index.ts":{"bytes":818,"imports":[{"path":"packages/core/echo/echo-protocol/src/document-structure.ts","kind":"import-statement","original":"./document-structure"},{"path":"packages/core/echo/echo-protocol/src/reference.ts","kind":"import-statement","original":"./reference"},{"path":"packages/core/echo/echo-protocol/src/space-doc-version.ts","kind":"import-statement","original":"./space-doc-version"},{"path":"packages/core/echo/echo-protocol/src/legacy.ts","kind":"import-statement","original":"./legacy"}],"format":"esm"}},"outputs":{"packages/core/echo/echo-protocol/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":5988},"packages/core/echo/echo-protocol/dist/lib/browser/index.mjs":{"imports":[{"path":"@dxos/keys","kind":"import-statement","external":true}],"exports":["LEGACY_REFERENCE_TYPE_TAG","LEGACY_TYPE_PROPERTIES","REFERENCE_TYPE_TAG","Reference","SpaceDocVersion","decodeReference","encodeReference","isEncodedReference","isLegacyReference"],"entryPoint":"packages/core/echo/echo-protocol/src/index.ts","inputs":{"packages/core/echo/echo-protocol/src/index.ts":{"bytesInOutput":0},"packages/core/echo/echo-protocol/src/reference.ts":{"bytesInOutput":1969},"packages/core/echo/echo-protocol/src/space-doc-version.ts":{"bytesInOutput":179},"packages/core/echo/echo-protocol/src/legacy.ts":{"bytesInOutput":257}},"bytes":2807}}}
1
+ {"inputs":{"packages/core/echo/echo-protocol/src/document-structure.ts":{"bytes":2528,"imports":[],"format":"esm"},"packages/core/echo/echo-protocol/src/space-id.ts":{"bytes":3580,"imports":[{"path":"@dxos/crypto","kind":"import-statement","external":true},{"path":"@dxos/keys","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"format":"esm"},"packages/core/echo/echo-protocol/src/reference.ts":{"bytes":11612,"imports":[{"path":"@dxos/keys","kind":"import-statement","external":true},{"path":"packages/core/echo/echo-protocol/src/space-id.ts","kind":"import-statement","original":"./space-id"}],"format":"esm"},"packages/core/echo/echo-protocol/src/space-doc-version.ts":{"bytes":1591,"imports":[],"format":"esm"},"packages/core/echo/echo-protocol/src/legacy.ts":{"bytes":1719,"imports":[],"format":"esm"},"packages/core/echo/echo-protocol/src/collection-sync.ts":{"bytes":550,"imports":[],"format":"esm"},"packages/core/echo/echo-protocol/src/index.ts":{"bytes":1017,"imports":[{"path":"packages/core/echo/echo-protocol/src/document-structure.ts","kind":"import-statement","original":"./document-structure"},{"path":"packages/core/echo/echo-protocol/src/reference.ts","kind":"import-statement","original":"./reference"},{"path":"packages/core/echo/echo-protocol/src/space-doc-version.ts","kind":"import-statement","original":"./space-doc-version"},{"path":"packages/core/echo/echo-protocol/src/legacy.ts","kind":"import-statement","original":"./legacy"},{"path":"packages/core/echo/echo-protocol/src/collection-sync.ts","kind":"import-statement","original":"./collection-sync"},{"path":"packages/core/echo/echo-protocol/src/space-id.ts","kind":"import-statement","original":"./space-id"}],"format":"esm"}},"outputs":{"packages/core/echo/echo-protocol/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":8975},"packages/core/echo/echo-protocol/dist/lib/browser/index.mjs":{"imports":[{"path":"@dxos/keys","kind":"import-statement","external":true},{"path":"@dxos/crypto","kind":"import-statement","external":true},{"path":"@dxos/keys","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"exports":["LEGACY_REFERENCE_TYPE_TAG","LEGACY_TYPE_PROPERTIES","REFERENCE_TYPE_TAG","Reference","SpaceDocVersion","convertLegacyReference","createIdFromSpaceKey","decodeReference","encodeReference","isEncodedReference","isLegacyReference"],"entryPoint":"packages/core/echo/echo-protocol/src/index.ts","inputs":{"packages/core/echo/echo-protocol/src/index.ts":{"bytesInOutput":0},"packages/core/echo/echo-protocol/src/reference.ts":{"bytesInOutput":2495},"packages/core/echo/echo-protocol/src/space-id.ts":{"bytesInOutput":604},"packages/core/echo/echo-protocol/src/space-doc-version.ts":{"bytesInOutput":179},"packages/core/echo/echo-protocol/src/legacy.ts":{"bytesInOutput":257}},"bytes":4094}}}
@@ -23,6 +23,8 @@ __export(node_exports, {
23
23
  REFERENCE_TYPE_TAG: () => REFERENCE_TYPE_TAG,
24
24
  Reference: () => Reference,
25
25
  SpaceDocVersion: () => SpaceDocVersion,
26
+ convertLegacyReference: () => convertLegacyReference,
27
+ createIdFromSpaceKey: () => createIdFromSpaceKey,
26
28
  decodeReference: () => decodeReference,
27
29
  encodeReference: () => encodeReference,
28
30
  isEncodedReference: () => isEncodedReference,
@@ -30,6 +32,21 @@ __export(node_exports, {
30
32
  });
31
33
  module.exports = __toCommonJS(node_exports);
32
34
  var import_keys = require("@dxos/keys");
35
+ var import_crypto = require("@dxos/crypto");
36
+ var import_keys2 = require("@dxos/keys");
37
+ var import_util = require("@dxos/util");
38
+ var SPACE_IDS_CACHE = new import_util.ComplexMap(import_keys2.PublicKey.hash);
39
+ var createIdFromSpaceKey = async (spaceKey) => {
40
+ const cachedValue = SPACE_IDS_CACHE.get(spaceKey);
41
+ if (cachedValue !== void 0) {
42
+ return cachedValue;
43
+ }
44
+ const digest = await import_crypto.subtleCrypto.digest("SHA-256", spaceKey.asUint8Array());
45
+ const bytes = new Uint8Array(digest).slice(0, import_keys2.SpaceId.byteLength);
46
+ const spaceId = import_keys2.SpaceId.encode(bytes);
47
+ SPACE_IDS_CACHE.set(spaceKey, spaceId);
48
+ return spaceId;
49
+ };
33
50
  var Reference = class _Reference {
34
51
  static {
35
52
  this.TYPE_PROTOCOL = "protobuf";
@@ -97,6 +114,17 @@ var encodeReference = (reference) => ({
97
114
  });
98
115
  var decodeReference = (value) => Reference.fromDXN(import_keys.DXN.parse(value["/"]));
99
116
  var isEncodedReference = (value) => typeof value === "object" && value !== null && Object.keys(value).length === 1 && typeof value["/"] === "string";
117
+ var convertLegacyReference = async (reference) => {
118
+ if (reference.protocol === Reference.TYPE_PROTOCOL) {
119
+ return encodeReference(Reference.fromLegacyTypename(reference.itemId));
120
+ }
121
+ if (!reference.itemId) {
122
+ throw new Error("Invalid reference");
123
+ }
124
+ const spaceKey = reference.host;
125
+ const spaceId = spaceKey != null ? await createIdFromSpaceKey(import_keys.PublicKey.fromHex(spaceKey)) : void 0;
126
+ return encodeReference(new Reference(reference.itemId, reference.protocol ?? void 0, spaceId));
127
+ };
100
128
  var SpaceDocVersion = Object.freeze({
101
129
  /**
102
130
  * For the documents created before the versioning was introduced.
@@ -117,6 +145,8 @@ var LEGACY_TYPE_PROPERTIES = "dxos.sdk.client.Properties";
117
145
  REFERENCE_TYPE_TAG,
118
146
  Reference,
119
147
  SpaceDocVersion,
148
+ convertLegacyReference,
149
+ createIdFromSpaceKey,
120
150
  decodeReference,
121
151
  encodeReference,
122
152
  isEncodedReference,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../../../src/reference.ts", "../../../src/space-doc-version.ts", "../../../src/legacy.ts"],
4
- "sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nimport { DXN, LOCAL_SPACE_TAG } from '@dxos/keys';\nimport { type ObjectId } from '@dxos/protocols';\nimport { type Reference as ReferenceProto } from '@dxos/protocols/proto/dxos/echo/model/document';\n\n/**\n * Runtime representation of object reference.\n */\nexport class Reference {\n /**\n * Protocol references to runtime registered types.\n */\n static TYPE_PROTOCOL = 'protobuf';\n\n static fromValue(value: ReferenceProto): Reference {\n return new Reference(value.objectId, value.protocol, value.host);\n }\n\n /**\n * @deprecated\n */\n // TODO(burdon): Document/remove?\n static fromLegacyTypename(type: string): Reference {\n return new Reference(type, Reference.TYPE_PROTOCOL, 'dxos.org');\n }\n\n static fromDXN(dxn: DXN): Reference {\n switch (dxn.kind) {\n case DXN.kind.TYPE:\n return Reference.fromLegacyTypename(dxn.parts[0]);\n case DXN.kind.ECHO:\n if (dxn.parts[0] === LOCAL_SPACE_TAG) {\n return new Reference(dxn.parts[1]);\n } else {\n return new Reference(dxn.parts[1], undefined, dxn.parts[0]);\n }\n default:\n throw new Error(`Unsupported DXN kind: ${dxn.kind}`);\n }\n }\n\n // prettier-ignore\n constructor(\n public readonly objectId: ObjectId,\n public readonly protocol?: string,\n public readonly host?: string\n ) {}\n\n encode(): ReferenceProto {\n return { objectId: this.objectId, host: this.host, protocol: this.protocol };\n }\n\n toDXN(): DXN {\n if (this.protocol === Reference.TYPE_PROTOCOL) {\n return new DXN(DXN.kind.TYPE, [this.objectId]);\n } else {\n if (this.host) {\n // Host is assumed to be the space key.\n // The DXN should actually have the space ID.\n // TODO(dmaretskyi): Migrate to space id.\n return new DXN(DXN.kind.ECHO, [this.host, this.objectId]);\n } else {\n return new DXN(DXN.kind.ECHO, [LOCAL_SPACE_TAG, this.objectId]);\n }\n }\n }\n}\n\nexport const REFERENCE_TYPE_TAG = 'dxos.echo.model.document.Reference';\n\n/**\n * Reference as it is stored in Automerge document.\n */\nexport type EncodedReference = {\n '/': string;\n};\n\nexport const encodeReference = (reference: Reference): EncodedReference => ({\n '/': reference.toDXN().toString(),\n});\n\nexport const decodeReference = (value: any) => Reference.fromDXN(DXN.parse(value['/']));\n\nexport const isEncodedReference = (value: any): value is EncodedReference =>\n typeof value === 'object' && value !== null && Object.keys(value).length === 1 && typeof value['/'] === 'string';\n", "//\n// Copyright 2024 DXOS.org\n//\n\n/**\n * Denotes the data version of the space automerge document as well as the leaf documents for each individual ECHO object.\n */\nexport type SpaceDocVersion = number & { __type: 'SpaceDocVersion' };\n\nexport const SpaceDocVersion = Object.freeze({\n /**\n * For the documents created before the versioning was introduced.\n */\n LEGACY: 0 as SpaceDocVersion,\n\n /**\n * Current version.\n */\n CURRENT: 1 as SpaceDocVersion,\n});\n", "//\n// Copyright 2024 DXOS.org\n//\n\nexport const LEGACY_REFERENCE_TYPE_TAG = 'dxos.echo.model.document.Reference';\n\n/**\n * Reference as it is stored in Automerge document.\n */\nexport type LegacyEncodedReferenceObject = {\n '@type': typeof LEGACY_REFERENCE_TYPE_TAG;\n itemId: string;\n protocol?: string;\n host?: string;\n};\n\nexport const isLegacyReference = (value: any): boolean =>\n typeof value === 'object' && value !== null && value['@type'] === LEGACY_REFERENCE_TYPE_TAG;\n\nexport const LEGACY_TYPE_PROPERTIES = 'dxos.sdk.client.Properties';\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,kBAAqC;AAO9B,IAAMA,YAAN,MAAMA,WAAAA;EAIX,OAAA;SAAOC,gBAAgB;;EAEvB,OAAOC,UAAUC,OAAkC;AACjD,WAAO,IAAIH,WAAUG,MAAMC,UAAUD,MAAME,UAAUF,MAAMG,IAAI;EACjE;;;;;EAMA,OAAOC,mBAAmBC,MAAyB;AACjD,WAAO,IAAIR,WAAUQ,MAAMR,WAAUC,eAAe,UAAA;EACtD;EAEA,OAAOQ,QAAQC,KAAqB;AAClC,YAAQA,IAAIC,MAAI;MACd,KAAKC,gBAAID,KAAKE;AACZ,eAAOb,WAAUO,mBAAmBG,IAAII,MAAM,CAAA,CAAE;MAClD,KAAKF,gBAAID,KAAKI;AACZ,YAAIL,IAAII,MAAM,CAAA,MAAOE,6BAAiB;AACpC,iBAAO,IAAIhB,WAAUU,IAAII,MAAM,CAAA,CAAE;QACnC,OAAO;AACL,iBAAO,IAAId,WAAUU,IAAII,MAAM,CAAA,GAAIG,QAAWP,IAAII,MAAM,CAAA,CAAE;QAC5D;MACF;AACE,cAAM,IAAII,MAAM,yBAAyBR,IAAIC,IAAI,EAAE;IACvD;EACF;;EAGAQ,YACkBf,UACAC,UACAC,MAChB;SAHgBF,WAAAA;SACAC,WAAAA;SACAC,OAAAA;EACf;EAEHc,SAAyB;AACvB,WAAO;MAAEhB,UAAU,KAAKA;MAAUE,MAAM,KAAKA;MAAMD,UAAU,KAAKA;IAAS;EAC7E;EAEAgB,QAAa;AACX,QAAI,KAAKhB,aAAaL,WAAUC,eAAe;AAC7C,aAAO,IAAIW,gBAAIA,gBAAID,KAAKE,MAAM;QAAC,KAAKT;OAAS;IAC/C,OAAO;AACL,UAAI,KAAKE,MAAM;AAIb,eAAO,IAAIM,gBAAIA,gBAAID,KAAKI,MAAM;UAAC,KAAKT;UAAM,KAAKF;SAAS;MAC1D,OAAO;AACL,eAAO,IAAIQ,gBAAIA,gBAAID,KAAKI,MAAM;UAACC;UAAiB,KAAKZ;SAAS;MAChE;IACF;EACF;AACF;AAEO,IAAMkB,qBAAqB;AAS3B,IAAMC,kBAAkB,CAACC,eAA4C;EAC1E,KAAKA,UAAUH,MAAK,EAAGI,SAAQ;AACjC;AAEO,IAAMC,kBAAkB,CAACvB,UAAeH,UAAUS,QAAQG,gBAAIe,MAAMxB,MAAM,GAAA,CAAI,CAAA;AAE9E,IAAMyB,qBAAqB,CAACzB,UACjC,OAAOA,UAAU,YAAYA,UAAU,QAAQ0B,OAAOC,KAAK3B,KAAAA,EAAO4B,WAAW,KAAK,OAAO5B,MAAM,GAAA,MAAS;AC9EnG,IAAM6B,kBAAkBH,OAAOI,OAAO;;;;EAI3CC,QAAQ;;;;EAKRC,SAAS;AACX,CAAA;ACfO,IAAMC,4BAA4B;AAYlC,IAAMC,oBAAoB,CAAClC,UAChC,OAAOA,UAAU,YAAYA,UAAU,QAAQA,MAAM,OAAA,MAAaiC;AAE7D,IAAME,yBAAyB;",
6
- "names": ["Reference", "TYPE_PROTOCOL", "fromValue", "value", "objectId", "protocol", "host", "fromLegacyTypename", "type", "fromDXN", "dxn", "kind", "DXN", "TYPE", "parts", "ECHO", "LOCAL_SPACE_TAG", "undefined", "Error", "constructor", "encode", "toDXN", "REFERENCE_TYPE_TAG", "encodeReference", "reference", "toString", "decodeReference", "parse", "isEncodedReference", "Object", "keys", "length", "SpaceDocVersion", "freeze", "LEGACY", "CURRENT", "LEGACY_REFERENCE_TYPE_TAG", "isLegacyReference", "LEGACY_TYPE_PROPERTIES"]
3
+ "sources": ["../../../src/reference.ts", "../../../src/space-id.ts", "../../../src/space-doc-version.ts", "../../../src/legacy.ts"],
4
+ "sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nimport { DXN, LOCAL_SPACE_TAG, PublicKey } from '@dxos/keys';\nimport { type ObjectId } from '@dxos/protocols';\nimport { type Reference as ReferenceProto } from '@dxos/protocols/proto/dxos/echo/model/document';\n\nimport type { LegacyEncodedReferenceObject } from './legacy';\nimport { createIdFromSpaceKey } from './space-id';\n\n/**\n * Runtime representation of object reference.\n */\nexport class Reference {\n /**\n * Protocol references to runtime registered types.\n */\n static TYPE_PROTOCOL = 'protobuf';\n\n static fromValue(value: ReferenceProto): Reference {\n return new Reference(value.objectId, value.protocol, value.host);\n }\n\n /**\n * @deprecated\n */\n // TODO(burdon): Document/remove?\n static fromLegacyTypename(type: string): Reference {\n return new Reference(type, Reference.TYPE_PROTOCOL, 'dxos.org');\n }\n\n static fromDXN(dxn: DXN): Reference {\n switch (dxn.kind) {\n case DXN.kind.TYPE:\n return Reference.fromLegacyTypename(dxn.parts[0]);\n case DXN.kind.ECHO:\n if (dxn.parts[0] === LOCAL_SPACE_TAG) {\n return new Reference(dxn.parts[1]);\n } else {\n return new Reference(dxn.parts[1], undefined, dxn.parts[0]);\n }\n default:\n throw new Error(`Unsupported DXN kind: ${dxn.kind}`);\n }\n }\n\n // prettier-ignore\n constructor(\n public readonly objectId: ObjectId,\n public readonly protocol?: string,\n public readonly host?: string\n ) {}\n\n encode(): ReferenceProto {\n return { objectId: this.objectId, host: this.host, protocol: this.protocol };\n }\n\n toDXN(): DXN {\n if (this.protocol === Reference.TYPE_PROTOCOL) {\n return new DXN(DXN.kind.TYPE, [this.objectId]);\n } else {\n if (this.host) {\n // Host is assumed to be the space key.\n // The DXN should actually have the space ID.\n // TODO(dmaretskyi): Migrate to space id.\n return new DXN(DXN.kind.ECHO, [this.host, this.objectId]);\n } else {\n return new DXN(DXN.kind.ECHO, [LOCAL_SPACE_TAG, this.objectId]);\n }\n }\n }\n}\n\nexport const REFERENCE_TYPE_TAG = 'dxos.echo.model.document.Reference';\n\n/**\n * Reference as it is stored in Automerge document.\n */\nexport type EncodedReference = {\n '/': string;\n};\n\nexport const encodeReference = (reference: Reference): EncodedReference => ({\n '/': reference.toDXN().toString(),\n});\n\nexport const decodeReference = (value: any) => Reference.fromDXN(DXN.parse(value['/']));\n\nexport const isEncodedReference = (value: any): value is EncodedReference =>\n typeof value === 'object' && value !== null && Object.keys(value).length === 1 && typeof value['/'] === 'string';\n\nexport const convertLegacyReference = async (reference: LegacyEncodedReferenceObject): Promise<EncodedReference> => {\n if (reference.protocol === Reference.TYPE_PROTOCOL) {\n return encodeReference(Reference.fromLegacyTypename(reference.itemId));\n }\n if (!reference.itemId) {\n throw new Error('Invalid reference');\n }\n\n const spaceKey = reference.host;\n const spaceId = spaceKey != null ? await createIdFromSpaceKey(PublicKey.fromHex(spaceKey)) : undefined;\n return encodeReference(new Reference(reference.itemId, reference.protocol ?? undefined, spaceId));\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { subtleCrypto } from '@dxos/crypto';\nimport { PublicKey, SpaceId } from '@dxos/keys';\nimport { ComplexMap } from '@dxos/util';\n\nconst SPACE_IDS_CACHE = new ComplexMap<PublicKey, SpaceId>(PublicKey.hash);\n\n/**\n * Space keys are generated by creating a keypair, and then taking the first 20 bytes of the SHA-256 hash of the public key and encoding them to multibase RFC4648 base-32 format (prefixed with B, see Multibase Table).\n * Inspired by how ethereum addresses are derived.\n */\nexport const createIdFromSpaceKey = async (spaceKey: PublicKey): Promise<SpaceId> => {\n const cachedValue = SPACE_IDS_CACHE.get(spaceKey);\n if (cachedValue !== undefined) {\n return cachedValue;\n }\n\n const digest = await subtleCrypto.digest('SHA-256', spaceKey.asUint8Array());\n\n const bytes = new Uint8Array(digest).slice(0, SpaceId.byteLength);\n const spaceId = SpaceId.encode(bytes);\n SPACE_IDS_CACHE.set(spaceKey, spaceId);\n return spaceId;\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\n/**\n * Denotes the data version of the space automerge document as well as the leaf documents for each individual ECHO object.\n */\nexport type SpaceDocVersion = number & { __type: 'SpaceDocVersion' };\n\nexport const SpaceDocVersion = Object.freeze({\n /**\n * For the documents created before the versioning was introduced.\n */\n LEGACY: 0 as SpaceDocVersion,\n\n /**\n * Current version.\n */\n CURRENT: 1 as SpaceDocVersion,\n});\n", "//\n// Copyright 2024 DXOS.org\n//\n\nexport const LEGACY_REFERENCE_TYPE_TAG = 'dxos.echo.model.document.Reference';\n\n/**\n * Reference as it is stored in Automerge document.\n */\nexport type LegacyEncodedReferenceObject = {\n '@type': typeof LEGACY_REFERENCE_TYPE_TAG;\n itemId: string;\n protocol?: string;\n host?: string;\n};\n\nexport const isLegacyReference = (value: any): boolean =>\n typeof value === 'object' && value !== null && value['@type'] === LEGACY_REFERENCE_TYPE_TAG;\n\nexport const LEGACY_TYPE_PROPERTIES = 'dxos.sdk.client.Properties';\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,kBAAgD;ACAhD,oBAA6B;AAC7B,IAAAA,eAAmC;AACnC,kBAA2B;AAE3B,IAAMC,kBAAkB,IAAIC,uBAA+BC,uBAAUC,IAAI;AAMlE,IAAMC,uBAAuB,OAAOC,aAAAA;AACzC,QAAMC,cAAcN,gBAAgBO,IAAIF,QAAAA;AACxC,MAAIC,gBAAgBE,QAAW;AAC7B,WAAOF;EACT;AAEA,QAAMG,SAAS,MAAMC,2BAAaD,OAAO,WAAWJ,SAASM,aAAY,CAAA;AAEzE,QAAMC,QAAQ,IAAIC,WAAWJ,MAAAA,EAAQK,MAAM,GAAGC,qBAAQC,UAAU;AAChE,QA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,IAAIQ,gBAAIA,gBAAID,KAAKI,MAAM;UAACC;UAAiB,KAAKZ;SAAS;MAChE;IACF;EACF;AACF;AAEO,IAAMgB,qBAAqB;AAS3B,IAAMC,kBAAkB,CAACC,eAA4C;EAC1E,KAAKA,UAAUH,MAAK,EAAGI,SAAQ;AACjC;AAEO,IAAMC,kBAAkB,CAACrB,UAAeH,UAAUS,QAAQG,gBAAIa,MAAMtB,MAAM,GAAA,CAAI,CAAA;AAE9E,IAAMuB,qBAAqB,CAACvB,UACjC,OAAOA,UAAU,YAAYA,UAAU,QAAQwB,OAAOC,KAAKzB,KAAAA,EAAO0B,WAAW,KAAK,OAAO1B,MAAM,GAAA,MAAS;AAEnG,IAAM2B,yBAAyB,OAAOR,cAAAA;AAC3C,MAAIA,UAAUjB,aAAaL,UAAUC,eAAe;AAClD,WAAOoB,gBAAgBrB,UAAUO,mBAAmBe,UAAUS,MAAM,CAAA;EACtE;AACA,MAAI,CAACT,UAAUS,QAAQ;AACrB,UAAM,IAAId,MAAM,mBAAA;EAClB;AAEA,QAAMhC,WAAWqC,UAAUhB;AAC3B,QAAMT,UAAUZ,YAAY,OAAO,MAAMD,qBAAqBF,YAAAA,UAAUkD,QAAQ/C,QAAAA,CAAAA,IAAaG;AAC7F,SAAOiC,gBAAgB,IAAIrB,UAAUsB,UAAUS,QAAQT,UAAUjB,YAAYjB,QAAWS,OAAAA,CAAAA;AAC1F;AE9FO,IAAMoC,kBAAkBN,OAAOO,OAAO;;;;EAI3CC,QAAQ;;;;EAKRC,SAAS;AACX,CAAA;ACfO,IAAMC,4BAA4B;AAYlC,IAAMC,oBAAoB,CAACnC,UAChC,OAAOA,UAAU,YAAYA,UAAU,QAAQA,MAAM,OAAA,MAAakC;AAE7D,IAAME,yBAAyB;",
6
+ "names": ["import_keys", "SPACE_IDS_CACHE", "ComplexMap", "PublicKey", "hash", "createIdFromSpaceKey", "spaceKey", "cachedValue", "get", "undefined", "digest", "subtleCrypto", "asUint8Array", "bytes", "Uint8Array", "slice", "SpaceId", "byteLength", "spaceId", "encode", "set", "Reference", "TYPE_PROTOCOL", "fromValue", "value", "objectId", "protocol", "host", "fromLegacyTypename", "type", "fromDXN", "dxn", "kind", "DXN", "TYPE", "parts", "ECHO", "LOCAL_SPACE_TAG", "Error", "constructor", "toDXN", "REFERENCE_TYPE_TAG", "encodeReference", "reference", "toString", "decodeReference", "parse", "isEncodedReference", "Object", "keys", "length", "convertLegacyReference", "itemId", "fromHex", "SpaceDocVersion", "freeze", "LEGACY", "CURRENT", "LEGACY_REFERENCE_TYPE_TAG", "isLegacyReference", "LEGACY_TYPE_PROPERTIES"]
7
7
  }
@@ -1 +1 @@
1
- {"inputs":{"packages/core/echo/echo-protocol/src/document-structure.ts":{"bytes":2528,"imports":[],"format":"esm"},"packages/core/echo/echo-protocol/src/reference.ts":{"bytes":9351,"imports":[{"path":"@dxos/keys","kind":"import-statement","external":true}],"format":"esm"},"packages/core/echo/echo-protocol/src/space-doc-version.ts":{"bytes":1591,"imports":[],"format":"esm"},"packages/core/echo/echo-protocol/src/legacy.ts":{"bytes":1719,"imports":[],"format":"esm"},"packages/core/echo/echo-protocol/src/index.ts":{"bytes":818,"imports":[{"path":"packages/core/echo/echo-protocol/src/document-structure.ts","kind":"import-statement","original":"./document-structure"},{"path":"packages/core/echo/echo-protocol/src/reference.ts","kind":"import-statement","original":"./reference"},{"path":"packages/core/echo/echo-protocol/src/space-doc-version.ts","kind":"import-statement","original":"./space-doc-version"},{"path":"packages/core/echo/echo-protocol/src/legacy.ts","kind":"import-statement","original":"./legacy"}],"format":"esm"}},"outputs":{"packages/core/echo/echo-protocol/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":5988},"packages/core/echo/echo-protocol/dist/lib/node/index.cjs":{"imports":[{"path":"@dxos/keys","kind":"import-statement","external":true}],"exports":["LEGACY_REFERENCE_TYPE_TAG","LEGACY_TYPE_PROPERTIES","REFERENCE_TYPE_TAG","Reference","SpaceDocVersion","decodeReference","encodeReference","isEncodedReference","isLegacyReference"],"entryPoint":"packages/core/echo/echo-protocol/src/index.ts","inputs":{"packages/core/echo/echo-protocol/src/index.ts":{"bytesInOutput":0},"packages/core/echo/echo-protocol/src/reference.ts":{"bytesInOutput":1969},"packages/core/echo/echo-protocol/src/space-doc-version.ts":{"bytesInOutput":179},"packages/core/echo/echo-protocol/src/legacy.ts":{"bytesInOutput":257}},"bytes":2807}}}
1
+ {"inputs":{"packages/core/echo/echo-protocol/src/document-structure.ts":{"bytes":2528,"imports":[],"format":"esm"},"packages/core/echo/echo-protocol/src/space-id.ts":{"bytes":3580,"imports":[{"path":"@dxos/crypto","kind":"import-statement","external":true},{"path":"@dxos/keys","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"format":"esm"},"packages/core/echo/echo-protocol/src/reference.ts":{"bytes":11612,"imports":[{"path":"@dxos/keys","kind":"import-statement","external":true},{"path":"packages/core/echo/echo-protocol/src/space-id.ts","kind":"import-statement","original":"./space-id"}],"format":"esm"},"packages/core/echo/echo-protocol/src/space-doc-version.ts":{"bytes":1591,"imports":[],"format":"esm"},"packages/core/echo/echo-protocol/src/legacy.ts":{"bytes":1719,"imports":[],"format":"esm"},"packages/core/echo/echo-protocol/src/collection-sync.ts":{"bytes":550,"imports":[],"format":"esm"},"packages/core/echo/echo-protocol/src/index.ts":{"bytes":1017,"imports":[{"path":"packages/core/echo/echo-protocol/src/document-structure.ts","kind":"import-statement","original":"./document-structure"},{"path":"packages/core/echo/echo-protocol/src/reference.ts","kind":"import-statement","original":"./reference"},{"path":"packages/core/echo/echo-protocol/src/space-doc-version.ts","kind":"import-statement","original":"./space-doc-version"},{"path":"packages/core/echo/echo-protocol/src/legacy.ts","kind":"import-statement","original":"./legacy"},{"path":"packages/core/echo/echo-protocol/src/collection-sync.ts","kind":"import-statement","original":"./collection-sync"},{"path":"packages/core/echo/echo-protocol/src/space-id.ts","kind":"import-statement","original":"./space-id"}],"format":"esm"}},"outputs":{"packages/core/echo/echo-protocol/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":8975},"packages/core/echo/echo-protocol/dist/lib/node/index.cjs":{"imports":[{"path":"@dxos/keys","kind":"import-statement","external":true},{"path":"@dxos/crypto","kind":"import-statement","external":true},{"path":"@dxos/keys","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"exports":["LEGACY_REFERENCE_TYPE_TAG","LEGACY_TYPE_PROPERTIES","REFERENCE_TYPE_TAG","Reference","SpaceDocVersion","convertLegacyReference","createIdFromSpaceKey","decodeReference","encodeReference","isEncodedReference","isLegacyReference"],"entryPoint":"packages/core/echo/echo-protocol/src/index.ts","inputs":{"packages/core/echo/echo-protocol/src/index.ts":{"bytesInOutput":0},"packages/core/echo/echo-protocol/src/reference.ts":{"bytesInOutput":2495},"packages/core/echo/echo-protocol/src/space-id.ts":{"bytesInOutput":604},"packages/core/echo/echo-protocol/src/space-doc-version.ts":{"bytesInOutput":179},"packages/core/echo/echo-protocol/src/legacy.ts":{"bytesInOutput":257}},"bytes":4094}}}
@@ -0,0 +1,135 @@
1
+ import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
+
3
+ // packages/core/echo/echo-protocol/src/reference.ts
4
+ import { DXN, LOCAL_SPACE_TAG, PublicKey as PublicKey2 } from "@dxos/keys";
5
+
6
+ // packages/core/echo/echo-protocol/src/space-id.ts
7
+ import { subtleCrypto } from "@dxos/crypto";
8
+ import { PublicKey, SpaceId } from "@dxos/keys";
9
+ import { ComplexMap } from "@dxos/util";
10
+ var SPACE_IDS_CACHE = new ComplexMap(PublicKey.hash);
11
+ var createIdFromSpaceKey = async (spaceKey) => {
12
+ const cachedValue = SPACE_IDS_CACHE.get(spaceKey);
13
+ if (cachedValue !== void 0) {
14
+ return cachedValue;
15
+ }
16
+ const digest = await subtleCrypto.digest("SHA-256", spaceKey.asUint8Array());
17
+ const bytes = new Uint8Array(digest).slice(0, SpaceId.byteLength);
18
+ const spaceId = SpaceId.encode(bytes);
19
+ SPACE_IDS_CACHE.set(spaceKey, spaceId);
20
+ return spaceId;
21
+ };
22
+
23
+ // packages/core/echo/echo-protocol/src/reference.ts
24
+ var Reference = class _Reference {
25
+ static {
26
+ /**
27
+ * Protocol references to runtime registered types.
28
+ */
29
+ this.TYPE_PROTOCOL = "protobuf";
30
+ }
31
+ static fromValue(value) {
32
+ return new _Reference(value.objectId, value.protocol, value.host);
33
+ }
34
+ /**
35
+ * @deprecated
36
+ */
37
+ // TODO(burdon): Document/remove?
38
+ static fromLegacyTypename(type) {
39
+ return new _Reference(type, _Reference.TYPE_PROTOCOL, "dxos.org");
40
+ }
41
+ static fromDXN(dxn) {
42
+ switch (dxn.kind) {
43
+ case DXN.kind.TYPE:
44
+ return _Reference.fromLegacyTypename(dxn.parts[0]);
45
+ case DXN.kind.ECHO:
46
+ if (dxn.parts[0] === LOCAL_SPACE_TAG) {
47
+ return new _Reference(dxn.parts[1]);
48
+ } else {
49
+ return new _Reference(dxn.parts[1], void 0, dxn.parts[0]);
50
+ }
51
+ default:
52
+ throw new Error(`Unsupported DXN kind: ${dxn.kind}`);
53
+ }
54
+ }
55
+ // prettier-ignore
56
+ constructor(objectId, protocol, host) {
57
+ this.objectId = objectId;
58
+ this.protocol = protocol;
59
+ this.host = host;
60
+ }
61
+ encode() {
62
+ return {
63
+ objectId: this.objectId,
64
+ host: this.host,
65
+ protocol: this.protocol
66
+ };
67
+ }
68
+ toDXN() {
69
+ if (this.protocol === _Reference.TYPE_PROTOCOL) {
70
+ return new DXN(DXN.kind.TYPE, [
71
+ this.objectId
72
+ ]);
73
+ } else {
74
+ if (this.host) {
75
+ return new DXN(DXN.kind.ECHO, [
76
+ this.host,
77
+ this.objectId
78
+ ]);
79
+ } else {
80
+ return new DXN(DXN.kind.ECHO, [
81
+ LOCAL_SPACE_TAG,
82
+ this.objectId
83
+ ]);
84
+ }
85
+ }
86
+ }
87
+ };
88
+ var REFERENCE_TYPE_TAG = "dxos.echo.model.document.Reference";
89
+ var encodeReference = (reference) => ({
90
+ "/": reference.toDXN().toString()
91
+ });
92
+ var decodeReference = (value) => Reference.fromDXN(DXN.parse(value["/"]));
93
+ var isEncodedReference = (value) => typeof value === "object" && value !== null && Object.keys(value).length === 1 && typeof value["/"] === "string";
94
+ var convertLegacyReference = async (reference) => {
95
+ if (reference.protocol === Reference.TYPE_PROTOCOL) {
96
+ return encodeReference(Reference.fromLegacyTypename(reference.itemId));
97
+ }
98
+ if (!reference.itemId) {
99
+ throw new Error("Invalid reference");
100
+ }
101
+ const spaceKey = reference.host;
102
+ const spaceId = spaceKey != null ? await createIdFromSpaceKey(PublicKey2.fromHex(spaceKey)) : void 0;
103
+ return encodeReference(new Reference(reference.itemId, reference.protocol ?? void 0, spaceId));
104
+ };
105
+
106
+ // packages/core/echo/echo-protocol/src/space-doc-version.ts
107
+ var SpaceDocVersion = Object.freeze({
108
+ /**
109
+ * For the documents created before the versioning was introduced.
110
+ */
111
+ LEGACY: 0,
112
+ /**
113
+ * Current version.
114
+ */
115
+ CURRENT: 1
116
+ });
117
+
118
+ // packages/core/echo/echo-protocol/src/legacy.ts
119
+ var LEGACY_REFERENCE_TYPE_TAG = "dxos.echo.model.document.Reference";
120
+ var isLegacyReference = (value) => typeof value === "object" && value !== null && value["@type"] === LEGACY_REFERENCE_TYPE_TAG;
121
+ var LEGACY_TYPE_PROPERTIES = "dxos.sdk.client.Properties";
122
+ export {
123
+ LEGACY_REFERENCE_TYPE_TAG,
124
+ LEGACY_TYPE_PROPERTIES,
125
+ REFERENCE_TYPE_TAG,
126
+ Reference,
127
+ SpaceDocVersion,
128
+ convertLegacyReference,
129
+ createIdFromSpaceKey,
130
+ decodeReference,
131
+ encodeReference,
132
+ isEncodedReference,
133
+ isLegacyReference
134
+ };
135
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/reference.ts", "../../../src/space-id.ts", "../../../src/space-doc-version.ts", "../../../src/legacy.ts"],
4
+ "sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nimport { DXN, LOCAL_SPACE_TAG, PublicKey } from '@dxos/keys';\nimport { type ObjectId } from '@dxos/protocols';\nimport { type Reference as ReferenceProto } from '@dxos/protocols/proto/dxos/echo/model/document';\n\nimport type { LegacyEncodedReferenceObject } from './legacy';\nimport { createIdFromSpaceKey } from './space-id';\n\n/**\n * Runtime representation of object reference.\n */\nexport class Reference {\n /**\n * Protocol references to runtime registered types.\n */\n static TYPE_PROTOCOL = 'protobuf';\n\n static fromValue(value: ReferenceProto): Reference {\n return new Reference(value.objectId, value.protocol, value.host);\n }\n\n /**\n * @deprecated\n */\n // TODO(burdon): Document/remove?\n static fromLegacyTypename(type: string): Reference {\n return new Reference(type, Reference.TYPE_PROTOCOL, 'dxos.org');\n }\n\n static fromDXN(dxn: DXN): Reference {\n switch (dxn.kind) {\n case DXN.kind.TYPE:\n return Reference.fromLegacyTypename(dxn.parts[0]);\n case DXN.kind.ECHO:\n if (dxn.parts[0] === LOCAL_SPACE_TAG) {\n return new Reference(dxn.parts[1]);\n } else {\n return new Reference(dxn.parts[1], undefined, dxn.parts[0]);\n }\n default:\n throw new Error(`Unsupported DXN kind: ${dxn.kind}`);\n }\n }\n\n // prettier-ignore\n constructor(\n public readonly objectId: ObjectId,\n public readonly protocol?: string,\n public readonly host?: string\n ) {}\n\n encode(): ReferenceProto {\n return { objectId: this.objectId, host: this.host, protocol: this.protocol };\n }\n\n toDXN(): DXN {\n if (this.protocol === Reference.TYPE_PROTOCOL) {\n return new DXN(DXN.kind.TYPE, [this.objectId]);\n } else {\n if (this.host) {\n // Host is assumed to be the space key.\n // The DXN should actually have the space ID.\n // TODO(dmaretskyi): Migrate to space id.\n return new DXN(DXN.kind.ECHO, [this.host, this.objectId]);\n } else {\n return new DXN(DXN.kind.ECHO, [LOCAL_SPACE_TAG, this.objectId]);\n }\n }\n }\n}\n\nexport const REFERENCE_TYPE_TAG = 'dxos.echo.model.document.Reference';\n\n/**\n * Reference as it is stored in Automerge document.\n */\nexport type EncodedReference = {\n '/': string;\n};\n\nexport const encodeReference = (reference: Reference): EncodedReference => ({\n '/': reference.toDXN().toString(),\n});\n\nexport const decodeReference = (value: any) => Reference.fromDXN(DXN.parse(value['/']));\n\nexport const isEncodedReference = (value: any): value is EncodedReference =>\n typeof value === 'object' && value !== null && Object.keys(value).length === 1 && typeof value['/'] === 'string';\n\nexport const convertLegacyReference = async (reference: LegacyEncodedReferenceObject): Promise<EncodedReference> => {\n if (reference.protocol === Reference.TYPE_PROTOCOL) {\n return encodeReference(Reference.fromLegacyTypename(reference.itemId));\n }\n if (!reference.itemId) {\n throw new Error('Invalid reference');\n }\n\n const spaceKey = reference.host;\n const spaceId = spaceKey != null ? await createIdFromSpaceKey(PublicKey.fromHex(spaceKey)) : undefined;\n return encodeReference(new Reference(reference.itemId, reference.protocol ?? undefined, spaceId));\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { subtleCrypto } from '@dxos/crypto';\nimport { PublicKey, SpaceId } from '@dxos/keys';\nimport { ComplexMap } from '@dxos/util';\n\nconst SPACE_IDS_CACHE = new ComplexMap<PublicKey, SpaceId>(PublicKey.hash);\n\n/**\n * Space keys are generated by creating a keypair, and then taking the first 20 bytes of the SHA-256 hash of the public key and encoding them to multibase RFC4648 base-32 format (prefixed with B, see Multibase Table).\n * Inspired by how ethereum addresses are derived.\n */\nexport const createIdFromSpaceKey = async (spaceKey: PublicKey): Promise<SpaceId> => {\n const cachedValue = SPACE_IDS_CACHE.get(spaceKey);\n if (cachedValue !== undefined) {\n return cachedValue;\n }\n\n const digest = await subtleCrypto.digest('SHA-256', spaceKey.asUint8Array());\n\n const bytes = new Uint8Array(digest).slice(0, SpaceId.byteLength);\n const spaceId = SpaceId.encode(bytes);\n SPACE_IDS_CACHE.set(spaceKey, spaceId);\n return spaceId;\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\n/**\n * Denotes the data version of the space automerge document as well as the leaf documents for each individual ECHO object.\n */\nexport type SpaceDocVersion = number & { __type: 'SpaceDocVersion' };\n\nexport const SpaceDocVersion = Object.freeze({\n /**\n * For the documents created before the versioning was introduced.\n */\n LEGACY: 0 as SpaceDocVersion,\n\n /**\n * Current version.\n */\n CURRENT: 1 as SpaceDocVersion,\n});\n", "//\n// Copyright 2024 DXOS.org\n//\n\nexport const LEGACY_REFERENCE_TYPE_TAG = 'dxos.echo.model.document.Reference';\n\n/**\n * Reference as it is stored in Automerge document.\n */\nexport type LegacyEncodedReferenceObject = {\n '@type': typeof LEGACY_REFERENCE_TYPE_TAG;\n itemId: string;\n protocol?: string;\n host?: string;\n};\n\nexport const isLegacyReference = (value: any): boolean =>\n typeof value === 'object' && value !== null && value['@type'] === LEGACY_REFERENCE_TYPE_TAG;\n\nexport const LEGACY_TYPE_PROPERTIES = 'dxos.sdk.client.Properties';\n"],
5
+ "mappings": ";;;AAIA,SAASA,KAAKC,iBAAiBC,aAAAA,kBAAiB;;;ACAhD,SAASC,oBAAoB;AAC7B,SAASC,WAAWC,eAAe;AACnC,SAASC,kBAAkB;AAE3B,IAAMC,kBAAkB,IAAIC,WAA+BC,UAAUC,IAAI;AAMlE,IAAMC,uBAAuB,OAAOC,aAAAA;AACzC,QAAMC,cAAcN,gBAAgBO,IAAIF,QAAAA;AACxC,MAAIC,gBAAgBE,QAAW;AAC7B,WAAOF;EACT;AAEA,QAAMG,SAAS,MAAMC,aAAaD,OAAO,WAAWJ,SAASM,aAAY,CAAA;AAEzE,QAAMC,QAAQ,IAAIC,WAAWJ,MAAAA,EAAQK,MAAM,GAAGC,QAAQC,UAAU;AAChE,QAAMC,UAAUF,QAAQG,OAAON,KAAAA;AAC/BZ,kBAAgBmB,IAAId,UAAUY,OAAAA;AAC9B,SAAOA;AACT;;;ADZO,IAAMG,YAAN,MAAMA,WAAAA;EAIX;;;;SAAOC,gBAAgB;;EAEvB,OAAOC,UAAUC,OAAkC;AACjD,WAAO,IAAIH,WAAUG,MAA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AAeH,UAAUS,QAAQG,IAAIe,MAAMxB,MAAM,GAAA,CAAI,CAAA;AAE9E,IAAMyB,qBAAqB,CAACzB,UACjC,OAAOA,UAAU,YAAYA,UAAU,QAAQ0B,OAAOC,KAAK3B,KAAAA,EAAO4B,WAAW,KAAK,OAAO5B,MAAM,GAAA,MAAS;AAEnG,IAAM6B,yBAAyB,OAAOR,cAAAA;AAC3C,MAAIA,UAAUnB,aAAaL,UAAUC,eAAe;AAClD,WAAOsB,gBAAgBvB,UAAUO,mBAAmBiB,UAAUS,MAAM,CAAA;EACtE;AACA,MAAI,CAACT,UAAUS,QAAQ;AACrB,UAAM,IAAIf,MAAM,mBAAA;EAClB;AAEA,QAAMgB,WAAWV,UAAUlB;AAC3B,QAAM6B,UAAUD,YAAY,OAAO,MAAME,qBAAqBC,WAAUC,QAAQJ,QAAAA,CAAAA,IAAajB;AAC7F,SAAOM,gBAAgB,IAAIvB,UAAUwB,UAAUS,QAAQT,UAAUnB,YAAYY,QAAWkB,OAAAA,CAAAA;AAC1F;;;AE9FO,IAAMI,kBAAkBC,OAAOC,OAAO;;;;EAI3CC,QAAQ;;;;EAKRC,SAAS;AACX,CAAA;;;ACfO,IAAMC,4BAA4B;AAYlC,IAAMC,oBAAoB,CAACC,UAChC,OAAOA,UAAU,YAAYA,UAAU,QAAQA,MAAM,OAAA,MAAaF;AAE7D,IAAMG,yBAAyB;",
6
+ "names": ["DXN", "LOCAL_SPACE_TAG", "PublicKey", "subtleCrypto", "PublicKey", "SpaceId", "ComplexMap", "SPACE_IDS_CACHE", "ComplexMap", "PublicKey", "hash", "createIdFromSpaceKey", "spaceKey", "cachedValue", "get", "undefined", "digest", "subtleCrypto", "asUint8Array", "bytes", "Uint8Array", "slice", "SpaceId", "byteLength", "spaceId", "encode", "set", "Reference", "TYPE_PROTOCOL", "fromValue", "value", "objectId", "protocol", "host", "fromLegacyTypename", "type", "fromDXN", "dxn", "kind", "DXN", "TYPE", "parts", "ECHO", "LOCAL_SPACE_TAG", "undefined", "Error", "constructor", "encode", "toDXN", "REFERENCE_TYPE_TAG", "encodeReference", "reference", "toString", "decodeReference", "parse", "isEncodedReference", "Object", "keys", "length", "convertLegacyReference", "itemId", "spaceKey", "spaceId", "createIdFromSpaceKey", "PublicKey", "fromHex", "SpaceDocVersion", "Object", "freeze", "LEGACY", "CURRENT", "LEGACY_REFERENCE_TYPE_TAG", "isLegacyReference", "value", "LEGACY_TYPE_PROPERTIES"]
7
+ }
@@ -0,0 +1 @@
1
+ {"inputs":{"packages/core/echo/echo-protocol/src/document-structure.ts":{"bytes":2528,"imports":[],"format":"esm"},"packages/core/echo/echo-protocol/src/space-id.ts":{"bytes":3580,"imports":[{"path":"@dxos/crypto","kind":"import-statement","external":true},{"path":"@dxos/keys","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"format":"esm"},"packages/core/echo/echo-protocol/src/reference.ts":{"bytes":11612,"imports":[{"path":"@dxos/keys","kind":"import-statement","external":true},{"path":"packages/core/echo/echo-protocol/src/space-id.ts","kind":"import-statement","original":"./space-id"}],"format":"esm"},"packages/core/echo/echo-protocol/src/space-doc-version.ts":{"bytes":1591,"imports":[],"format":"esm"},"packages/core/echo/echo-protocol/src/legacy.ts":{"bytes":1719,"imports":[],"format":"esm"},"packages/core/echo/echo-protocol/src/collection-sync.ts":{"bytes":550,"imports":[],"format":"esm"},"packages/core/echo/echo-protocol/src/index.ts":{"bytes":1017,"imports":[{"path":"packages/core/echo/echo-protocol/src/document-structure.ts","kind":"import-statement","original":"./document-structure"},{"path":"packages/core/echo/echo-protocol/src/reference.ts","kind":"import-statement","original":"./reference"},{"path":"packages/core/echo/echo-protocol/src/space-doc-version.ts","kind":"import-statement","original":"./space-doc-version"},{"path":"packages/core/echo/echo-protocol/src/legacy.ts","kind":"import-statement","original":"./legacy"},{"path":"packages/core/echo/echo-protocol/src/collection-sync.ts","kind":"import-statement","original":"./collection-sync"},{"path":"packages/core/echo/echo-protocol/src/space-id.ts","kind":"import-statement","original":"./space-id"}],"format":"esm"}},"outputs":{"packages/core/echo/echo-protocol/dist/lib/node-esm/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":8977},"packages/core/echo/echo-protocol/dist/lib/node-esm/index.mjs":{"imports":[{"path":"@dxos/keys","kind":"import-statement","external":true},{"path":"@dxos/crypto","kind":"import-statement","external":true},{"path":"@dxos/keys","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"exports":["LEGACY_REFERENCE_TYPE_TAG","LEGACY_TYPE_PROPERTIES","REFERENCE_TYPE_TAG","Reference","SpaceDocVersion","convertLegacyReference","createIdFromSpaceKey","decodeReference","encodeReference","isEncodedReference","isLegacyReference"],"entryPoint":"packages/core/echo/echo-protocol/src/index.ts","inputs":{"packages/core/echo/echo-protocol/src/index.ts":{"bytesInOutput":0},"packages/core/echo/echo-protocol/src/reference.ts":{"bytesInOutput":2495},"packages/core/echo/echo-protocol/src/space-id.ts":{"bytesInOutput":604},"packages/core/echo/echo-protocol/src/space-doc-version.ts":{"bytesInOutput":179},"packages/core/echo/echo-protocol/src/legacy.ts":{"bytesInOutput":257}},"bytes":4187}}}
@@ -0,0 +1,4 @@
1
+ export type CollectionId = string & {
2
+ __CollectionId: true;
3
+ };
4
+ //# sourceMappingURL=collection-sync.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"collection-sync.d.ts","sourceRoot":"","sources":["../../../src/collection-sync.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG;IAAE,cAAc,EAAE,IAAI,CAAA;CAAE,CAAC"}
@@ -2,4 +2,6 @@ export * from './document-structure';
2
2
  export * from './reference';
3
3
  export * from './space-doc-version';
4
4
  export * from './legacy';
5
+ export * from './collection-sync';
6
+ export * from './space-id';
5
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,sBAAsB,CAAC;AACrC,cAAc,aAAa,CAAC;AAC5B,cAAc,qBAAqB,CAAC;AACpC,cAAc,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,sBAAsB,CAAC;AACrC,cAAc,aAAa,CAAC;AAC5B,cAAc,qBAAqB,CAAC;AACpC,cAAc,UAAU,CAAC;AACzB,cAAc,mBAAmB,CAAC;AAClC,cAAc,YAAY,CAAC"}
@@ -1,6 +1,7 @@
1
1
  import { DXN } from '@dxos/keys';
2
2
  import { type ObjectId } from '@dxos/protocols';
3
3
  import { type Reference as ReferenceProto } from '@dxos/protocols/proto/dxos/echo/model/document';
4
+ import type { LegacyEncodedReferenceObject } from './legacy';
4
5
  /**
5
6
  * Runtime representation of object reference.
6
7
  */
@@ -32,4 +33,5 @@ export type EncodedReference = {
32
33
  export declare const encodeReference: (reference: Reference) => EncodedReference;
33
34
  export declare const decodeReference: (value: any) => Reference;
34
35
  export declare const isEncodedReference: (value: any) => value is EncodedReference;
36
+ export declare const convertLegacyReference: (reference: LegacyEncodedReferenceObject) => Promise<EncodedReference>;
35
37
  //# sourceMappingURL=reference.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"reference.d.ts","sourceRoot":"","sources":["../../../src/reference.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,GAAG,EAAmB,MAAM,YAAY,CAAC;AAClD,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,KAAK,SAAS,IAAI,cAAc,EAAE,MAAM,gDAAgD,CAAC;AAElG;;GAEG;AACH,qBAAa,SAAS;aAmCF,QAAQ,EAAE,QAAQ;aAClB,QAAQ,CAAC,EAAE,MAAM;aACjB,IAAI,CAAC,EAAE,MAAM;IApC/B;;OAEG;IACH,MAAM,CAAC,aAAa,SAAc;IAElC,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,cAAc,GAA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AC,OAAO,CAAC,GAAG,EAAE,GAAG,GAAG,SAAS;gBAiBjB,QAAQ,EAAE,QAAQ,EAClB,QAAQ,CAAC,EAAE,MAAM,YAAA,EACjB,IAAI,CAAC,EAAE,MAAM,YAAA;IAG/B,MAAM,IAAI,cAAc;IAIxB,KAAK,IAAI,GAAG;CAcb;AAED,eAAO,MAAM,kBAAkB,uCAAuC,CAAC;AAEvE;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,eAAO,MAAM,eAAe,cAAe,SAAS,KAAG,gBAErD,CAAC;AAEH,eAAO,MAAM,eAAe,UAAW,GAAG,cAA6C,CAAC;AAExF,eAAO,MAAM,kBAAkB,UAAW,GAAG,KAAG,KAAK,IAAI,gBACyD,CAAC;AAEnH,eAAO,MAAM,sBAAsB,cAAqB,4BAA4B,KAAG,OAAO,CAAC,gBAAgB,CAW9G,CAAC"}
@@ -0,0 +1,7 @@
1
+ import { PublicKey, SpaceId } from '@dxos/keys';
2
+ /**
3
+ * Space keys are generated by creating a keypair, and then taking the first 20 bytes of the SHA-256 hash of the public key and encoding them to multibase RFC4648 base-32 format (prefixed with B, see Multibase Table).
4
+ * Inspired by how ethereum addresses are derived.
5
+ */
6
+ export declare const createIdFromSpaceKey: (spaceKey: PublicKey) => Promise<SpaceId>;
7
+ //# sourceMappingURL=space-id.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"space-id.d.ts","sourceRoot":"","sources":["../../../src/space-id.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAKhD;;;GAGG;AACH,eAAO,MAAM,oBAAoB,aAAoB,SAAS,KAAG,OAAO,CAAC,OAAO,CAY/E,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/echo-protocol",
3
- "version": "0.6.12",
3
+ "version": "0.6.13-main.548ca8d",
4
4
  "description": "Core ECHO APIs.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -11,7 +11,8 @@
11
11
  ".": {
12
12
  "browser": "./dist/lib/browser/index.mjs",
13
13
  "node": {
14
- "default": "./dist/lib/node/index.cjs"
14
+ "require": "./dist/lib/node/index.cjs",
15
+ "default": "./dist/lib/node-esm/index.mjs"
15
16
  },
16
17
  "types": "./dist/types/src/index.d.ts"
17
18
  }
@@ -25,8 +26,10 @@
25
26
  "src"
26
27
  ],
27
28
  "dependencies": {
28
- "@dxos/keys": "0.6.12",
29
- "@dxos/protocols": "0.6.12"
29
+ "@dxos/crypto": "0.6.13-main.548ca8d",
30
+ "@dxos/keys": "0.6.13-main.548ca8d",
31
+ "@dxos/util": "0.6.13-main.548ca8d",
32
+ "@dxos/protocols": "0.6.13-main.548ca8d"
30
33
  },
31
34
  "publishConfig": {
32
35
  "access": "public"
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ export type CollectionId = string & { __CollectionId: true };
package/src/index.ts CHANGED
@@ -6,3 +6,5 @@ export * from './document-structure';
6
6
  export * from './reference';
7
7
  export * from './space-doc-version';
8
8
  export * from './legacy';
9
+ export * from './collection-sync';
10
+ export * from './space-id';
package/src/reference.ts CHANGED
@@ -2,10 +2,13 @@
2
2
  // Copyright 2022 DXOS.org
3
3
  //
4
4
 
5
- import { DXN, LOCAL_SPACE_TAG } from '@dxos/keys';
5
+ import { DXN, LOCAL_SPACE_TAG, PublicKey } from '@dxos/keys';
6
6
  import { type ObjectId } from '@dxos/protocols';
7
7
  import { type Reference as ReferenceProto } from '@dxos/protocols/proto/dxos/echo/model/document';
8
8
 
9
+ import type { LegacyEncodedReferenceObject } from './legacy';
10
+ import { createIdFromSpaceKey } from './space-id';
11
+
9
12
  /**
10
13
  * Runtime representation of object reference.
11
14
  */
@@ -86,3 +89,16 @@ export const decodeReference = (value: any) => Reference.fromDXN(DXN.parse(value
86
89
 
87
90
  export const isEncodedReference = (value: any): value is EncodedReference =>
88
91
  typeof value === 'object' && value !== null && Object.keys(value).length === 1 && typeof value['/'] === 'string';
92
+
93
+ export const convertLegacyReference = async (reference: LegacyEncodedReferenceObject): Promise<EncodedReference> => {
94
+ if (reference.protocol === Reference.TYPE_PROTOCOL) {
95
+ return encodeReference(Reference.fromLegacyTypename(reference.itemId));
96
+ }
97
+ if (!reference.itemId) {
98
+ throw new Error('Invalid reference');
99
+ }
100
+
101
+ const spaceKey = reference.host;
102
+ const spaceId = spaceKey != null ? await createIdFromSpaceKey(PublicKey.fromHex(spaceKey)) : undefined;
103
+ return encodeReference(new Reference(reference.itemId, reference.protocol ?? undefined, spaceId));
104
+ };
@@ -0,0 +1,27 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { subtleCrypto } from '@dxos/crypto';
6
+ import { PublicKey, SpaceId } from '@dxos/keys';
7
+ import { ComplexMap } from '@dxos/util';
8
+
9
+ const SPACE_IDS_CACHE = new ComplexMap<PublicKey, SpaceId>(PublicKey.hash);
10
+
11
+ /**
12
+ * Space keys are generated by creating a keypair, and then taking the first 20 bytes of the SHA-256 hash of the public key and encoding them to multibase RFC4648 base-32 format (prefixed with B, see Multibase Table).
13
+ * Inspired by how ethereum addresses are derived.
14
+ */
15
+ export const createIdFromSpaceKey = async (spaceKey: PublicKey): Promise<SpaceId> => {
16
+ const cachedValue = SPACE_IDS_CACHE.get(spaceKey);
17
+ if (cachedValue !== undefined) {
18
+ return cachedValue;
19
+ }
20
+
21
+ const digest = await subtleCrypto.digest('SHA-256', spaceKey.asUint8Array());
22
+
23
+ const bytes = new Uint8Array(digest).slice(0, SpaceId.byteLength);
24
+ const spaceId = SpaceId.encode(bytes);
25
+ SPACE_IDS_CACHE.set(spaceKey, spaceId);
26
+ return spaceId;
27
+ };