@dxos/echo-protocol 0.8.4-staging.ac66bdf99f → 0.9.1-main.c7dcc2e112

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/src/reference.ts CHANGED
@@ -3,126 +3,7 @@
3
3
  //
4
4
 
5
5
  import { assertArgument } from '@dxos/invariant';
6
- import { DXN, LOCAL_SPACE_TAG, type PublicKey } from '@dxos/keys';
7
- import { type ObjectId } from '@dxos/protocols';
8
- import { type Reference as ReferenceProto } from '@dxos/protocols/proto/dxos/echo/model/document';
9
-
10
- /**
11
- * Runtime representation of an reference in ECHO.
12
- * Implemented as a DXN, but we might extend it to other URIs in the future.
13
- * @deprecated Use `EncodedReference` instead.
14
- */
15
- export class Reference {
16
- /**
17
- * Protocol references to runtime registered types.
18
- * @deprecated
19
- */
20
- static TYPE_PROTOCOL = 'protobuf';
21
-
22
- static fromDXN(dxn: DXN): Reference {
23
- switch (dxn.kind) {
24
- case DXN.kind.TYPE:
25
- return new Reference(dxn.parts[0], Reference.TYPE_PROTOCOL, 'dxos.org', dxn);
26
- case DXN.kind.ECHO:
27
- if (dxn.parts[0] === LOCAL_SPACE_TAG) {
28
- return new Reference(dxn.parts[1], undefined, undefined, dxn);
29
- } else {
30
- return new Reference(dxn.parts[1], undefined, dxn.parts[0], dxn);
31
- }
32
- default:
33
- return new Reference(dxn.parts[0], undefined, dxn.parts[0], dxn);
34
- }
35
- }
36
-
37
- static fromValue(value: ReferenceProto): Reference {
38
- return new Reference(value.objectId, value.protocol, value.host);
39
- }
40
-
41
- /**
42
- * Reference an object in the local space.
43
- */
44
- static localObjectReference(objectId: ObjectId): Reference {
45
- return new Reference(objectId);
46
- }
47
-
48
- /**
49
- * @deprecated
50
- */
51
- // TODO(dmaretskyi): Remove.
52
- static fromLegacyTypename(type: string): Reference {
53
- return new Reference(type, Reference.TYPE_PROTOCOL, 'dxos.org');
54
- }
55
-
56
- /**
57
- * @deprecated
58
- */
59
- // TODO(dmaretskyi): Remove
60
- static fromObjectIdAndSpaceKey(objectId: ObjectId, spaceKey: PublicKey): Reference {
61
- // TODO(dmaretskyi): FIX ME! This should be a space ID not a space key.
62
- return new Reference(objectId, undefined, spaceKey.toHex());
63
- }
64
-
65
- // prettier-ignore
66
- private constructor(
67
- // TODO(dmaretskyi): Remove and just leave DXN.
68
- private readonly _objectId: ObjectId,
69
- private readonly _protocol?: string,
70
- private readonly _host?: string,
71
- private readonly _dxn?: DXN,
72
- ) {}
73
-
74
- get dxn(): DXN | undefined {
75
- return this._dxn;
76
- }
77
-
78
- /**
79
- * @deprecated
80
- */
81
- // TODO(dmaretskyi): Remove.
82
- get objectId(): ObjectId {
83
- return this._objectId;
84
- }
85
-
86
- /**
87
- * @deprecated
88
- */
89
- // TODO(dmaretskyi): Remove.
90
- get protocol(): string | undefined {
91
- return this._protocol;
92
- }
93
-
94
- /**
95
- * @deprecated
96
- */
97
- // TODO(dmaretskyi): Remove.
98
- get host(): string | undefined {
99
- return this._host;
100
- }
101
-
102
- encode(): ReferenceProto {
103
- return { objectId: this.objectId, host: this.host, protocol: this.protocol };
104
- }
105
-
106
- // TODO(dmaretskyi): Remove in favor of `reference.dxn`.
107
- toDXN(): DXN {
108
- if (this._dxn) {
109
- return this._dxn;
110
- }
111
-
112
- if (this.protocol === Reference.TYPE_PROTOCOL) {
113
- return new DXN(DXN.kind.TYPE, [this.objectId]);
114
- } else {
115
- if (this.host) {
116
- // Host is assumed to be the space key.
117
- // The DXN should actually have the space ID.
118
- // TODO(dmaretskyi): Migrate to space id.
119
- return new DXN(DXN.kind.ECHO, [this.host, this.objectId]);
120
- } else {
121
- return new DXN(DXN.kind.ECHO, [LOCAL_SPACE_TAG, this.objectId]);
122
- }
123
- }
124
- }
125
- }
6
+ import { URI } from '@dxos/keys';
126
7
 
127
8
  // TODO(dmaretskyi): Is this used anywhere?
128
9
  export const REFERENCE_TYPE_TAG = 'dxos.echo.model.document.Reference';
@@ -131,55 +12,26 @@ export const REFERENCE_TYPE_TAG = 'dxos.echo.model.document.Reference';
131
12
  * Reference as it is stored in Automerge document.
132
13
  */
133
14
  export type EncodedReference = {
134
- '/': string;
135
- };
136
-
137
- /**
138
- * @deprecated Use `EncodedReference.fromDXN` instead.
139
- */
140
- export const encodeReference = (reference: Reference): EncodedReference => ({
141
- '/': reference.toDXN().toString(),
142
- });
143
-
144
- /**
145
- * @deprecated Use `EncodedReference.toDXN` instead.
146
- */
147
- export const decodeReference = (value: any) => {
148
- if (typeof value !== 'object' || value === null || typeof value['/'] !== 'string') {
149
- throw new Error('Invalid reference');
150
- }
151
- const dxnString = value['/'];
152
-
153
- if (
154
- dxnString.length % 2 === 0 &&
155
- dxnString.slice(0, dxnString.length / 2) === dxnString.slice(dxnString.length / 2) &&
156
- dxnString.includes('dxn:echo')
157
- ) {
158
- throw new Error('Automerge bug detected!');
159
- }
160
-
161
- return Reference.fromDXN(DXN.parse(dxnString));
15
+ '/': URI.URI;
162
16
  };
163
17
 
164
- /**
165
- * @deprecated Use `EncodedReference.isEncodedReference` instead.
166
- */
167
18
  export const isEncodedReference = (value: any): value is EncodedReference =>
168
19
  typeof value === 'object' && value !== null && Object.keys(value).length === 1 && typeof value['/'] === 'string';
169
20
 
170
21
  export const EncodedReference = Object.freeze({
171
22
  isEncodedReference,
172
- getReferenceString: (value: EncodedReference): string => {
23
+ /**
24
+ * Returns the opaque URI stored in the encoded reference (any scheme: `echo:` or `dxn:`).
25
+ * Consumers can narrow with `EID.isEID(uri)` / `DXN.isDXN(uri)`.
26
+ */
27
+ toURI: (value: EncodedReference): URI.URI => {
173
28
  assertArgument(isEncodedReference(value), 'value', 'invalid reference');
174
29
  return value['/'];
175
30
  },
176
- toDXN: (value: EncodedReference): DXN => {
177
- return DXN.parse(EncodedReference.getReferenceString(value));
178
- },
179
- fromDXN: (dxn: DXN): EncodedReference => {
180
- return { '/': dxn.toString() };
181
- },
182
- fromLegacyTypename: (typename: string): EncodedReference => {
183
- return { '/': DXN.fromTypename(typename).toString() };
31
+ /**
32
+ * Creates an encoded reference from an opaque URI.
33
+ */
34
+ fromURI: (uri: URI.URI): EncodedReference => {
35
+ return { '/': uri };
184
36
  },
185
37
  });