@open-e2ee/signal-protocol-sdk 6.0.0 → 7.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/CHANGELOG.md +75 -0
  2. package/README.md +2 -2
  3. package/SECURITY.md +2 -2
  4. package/client/README.md +59 -0
  5. package/dist/client/client.d.ts.map +1 -1
  6. package/dist/client/client.js +12 -1
  7. package/dist/client/client.js.map +1 -1
  8. package/dist/client/content-adapter.d.ts +4 -0
  9. package/dist/client/content-adapter.d.ts.map +1 -1
  10. package/dist/client/content-adapter.js +7 -0
  11. package/dist/client/content-adapter.js.map +1 -1
  12. package/dist/client/hosted-mailbox-subscription.d.ts +23 -0
  13. package/dist/client/hosted-mailbox-subscription.d.ts.map +1 -1
  14. package/dist/client/hosted-mailbox-subscription.js +83 -4
  15. package/dist/client/hosted-mailbox-subscription.js.map +1 -1
  16. package/dist/client/hosted-presence-frames.d.ts +140 -0
  17. package/dist/client/hosted-presence-frames.d.ts.map +1 -0
  18. package/dist/client/hosted-presence-frames.js +291 -0
  19. package/dist/client/hosted-presence-frames.js.map +1 -0
  20. package/dist/client/hosted-presence.d.ts +83 -0
  21. package/dist/client/hosted-presence.d.ts.map +1 -0
  22. package/dist/client/hosted-presence.js +448 -0
  23. package/dist/client/hosted-presence.js.map +1 -0
  24. package/dist/client/hosted-profile-keys.d.ts +14 -0
  25. package/dist/client/hosted-profile-keys.d.ts.map +1 -0
  26. package/dist/client/hosted-profile-keys.js +139 -0
  27. package/dist/client/hosted-profile-keys.js.map +1 -0
  28. package/dist/client/hosted-transport.d.ts +13 -1
  29. package/dist/client/hosted-transport.d.ts.map +1 -1
  30. package/dist/client/hosted-transport.js +38 -0
  31. package/dist/client/hosted-transport.js.map +1 -1
  32. package/dist/client/hosted.d.ts +10 -0
  33. package/dist/client/hosted.d.ts.map +1 -1
  34. package/dist/client/hosted.js +12 -3
  35. package/dist/client/hosted.js.map +1 -1
  36. package/dist/client/index.d.ts +1 -1
  37. package/dist/client/index.d.ts.map +1 -1
  38. package/dist/client/index.js +1 -1
  39. package/dist/client/index.js.map +1 -1
  40. package/dist/client/profile-key-exchange.d.ts +13 -0
  41. package/dist/client/profile-key-exchange.d.ts.map +1 -0
  42. package/dist/client/profile-key-exchange.js +22 -0
  43. package/dist/client/profile-key-exchange.js.map +1 -0
  44. package/dist/client/relay-subscription.js +10 -0
  45. package/dist/client/relay-subscription.js.map +1 -1
  46. package/dist/client/types.d.ts +3 -0
  47. package/dist/client/types.d.ts.map +1 -1
  48. package/dist/client/types.js.map +1 -1
  49. package/dist/index.d.ts +2 -2
  50. package/dist/index.d.ts.map +1 -1
  51. package/dist/index.js +1 -1
  52. package/dist/index.js.map +1 -1
  53. package/docs/ASSURANCE.md +5 -5
  54. package/examples/browser/index.html +1 -1
  55. package/package.json +1 -1
@@ -0,0 +1,291 @@
1
+ /**
2
+ * The presence wire contract of the hosted Relay, version 1.
3
+ *
4
+ * A device with a mailbox socket sends each presence request as one text frame
5
+ * on that socket. The frame carries `type`, `version`, and an `id` that the
6
+ * Relay echoes in its one answer: a `presence-result` or a `presence-error`
7
+ * frame. The Relay closes the socket with code 1008 on a frame type or a
8
+ * version that it does not accept, as it does for every other unknown frame.
9
+ * The SDK has no fallback for that close. It rejects the pending requests
10
+ * with `FRAME_REJECTED`.
11
+ *
12
+ * On the socket only, a `presence-watch` request registers the device's
13
+ * whole watch set. After its answer, the Relay pushes one `presence-update`
14
+ * frame for each socket edge of a watched account. That frame has no `id`,
15
+ * because it answers no request.
16
+ *
17
+ * A wake client has no socket. It sends the same request body, without `id`
18
+ * and with `publishableKey`, as an authenticated `POST /presence`, and the
19
+ * response body is the `result` object of the same request type. The HTTP
20
+ * carrier has no watch.
21
+ */
22
+ import { base64ToBytes, bytesToUrlSafeBase64, urlSafeToBase64, } from "../internal/crypto/index.js";
23
+ /** The version that every presence request and answer carries. */
24
+ export const PRESENCE_FRAME_VERSION = 1;
25
+ /** The Relay refuses a presence read with more accounts. */
26
+ export const PRESENCE_READ_LIMIT = 100;
27
+ /** The Relay refuses a presence watch with more accounts. */
28
+ export const PRESENCE_WATCH_LIMIT = 16;
29
+ /** A presence key is 16 bytes, sent as 22 canonical base64url characters. */
30
+ const PRESENCE_KEY_BYTES = 16;
31
+ const PRESENCE_KEY_CHARACTERS = 22;
32
+ const MAXIMUM_ACCOUNT_CHARACTERS = 256;
33
+ const MAXIMUM_ERROR_CODE_CHARACTERS = 64;
34
+ const MAXIMUM_ERROR_MESSAGE_CHARACTERS = 1_024;
35
+ const REQUEST_ID = /^[A-Za-z0-9_-]{1,64}$/u;
36
+ const ERROR_CODE = /^[A-Z][A-Z0-9_]*$/u;
37
+ /** A presence request that the Relay refused or that got no answer. */
38
+ export class HostedRelayPresenceError extends Error {
39
+ code;
40
+ /** True when the same request can be sent again later. */
41
+ retryable;
42
+ constructor(code, message, retryable) {
43
+ super(message);
44
+ this.name = "HostedRelayPresenceError";
45
+ this.code = code;
46
+ this.retryable = retryable;
47
+ }
48
+ }
49
+ function record(value) {
50
+ return typeof value === "object" && value !== null && !Array.isArray(value);
51
+ }
52
+ function exactKeys(value, keys) {
53
+ const present = Object.keys(value);
54
+ return (present.length === keys.length && keys.every((key) => key in value));
55
+ }
56
+ function invalidResult() {
57
+ return new Error("Signal Protocol Relay returned an invalid presence result");
58
+ }
59
+ /** Throws unless the account is a Relay account address of 1-256 characters. */
60
+ export function assertPresenceAccount(account) {
61
+ if (typeof account !== "string" ||
62
+ account.length === 0 ||
63
+ account.length > MAXIMUM_ACCOUNT_CHARACTERS)
64
+ throw new Error("Signal Protocol Relay presence account is invalid");
65
+ return account;
66
+ }
67
+ /** Throws unless the key is exactly 16 bytes of canonical base64url. */
68
+ export function assertPresenceKey(presenceKey) {
69
+ if (typeof presenceKey !== "string" ||
70
+ presenceKey.length !== PRESENCE_KEY_CHARACTERS)
71
+ throw new Error("Signal Protocol Relay presence key is invalid");
72
+ let bytes;
73
+ try {
74
+ bytes = base64ToBytes(urlSafeToBase64(presenceKey));
75
+ }
76
+ catch {
77
+ throw new Error("Signal Protocol Relay presence key is invalid");
78
+ }
79
+ if (bytes.length !== PRESENCE_KEY_BYTES ||
80
+ bytesToUrlSafeBase64(bytes) !== presenceKey)
81
+ throw new Error("Signal Protocol Relay presence key is invalid");
82
+ return presenceKey;
83
+ }
84
+ function targetFields(targets) {
85
+ return targets.map((target) => target.presenceKey === undefined
86
+ ? { account: assertPresenceAccount(target.account) }
87
+ : {
88
+ account: assertPresenceAccount(target.account),
89
+ presenceKey: assertPresenceKey(target.presenceKey),
90
+ });
91
+ }
92
+ /** The request fields after `type` and `version`, in their wire order. */
93
+ function requestFields(request) {
94
+ switch (request.type) {
95
+ case "presence-policy":
96
+ case "presence-account":
97
+ return {};
98
+ case "presence-setting":
99
+ if (request.setting !== "visible" && request.setting !== "hidden")
100
+ throw new Error("Signal Protocol Relay presence setting is invalid");
101
+ return { setting: request.setting };
102
+ case "presence-key":
103
+ return { presenceKey: assertPresenceKey(request.presenceKey) };
104
+ case "presence-read":
105
+ if (!Array.isArray(request.accounts) ||
106
+ request.accounts.length === 0 ||
107
+ request.accounts.length > PRESENCE_READ_LIMIT)
108
+ throw new Error("Signal Protocol Relay presence read is invalid");
109
+ return { accounts: targetFields(request.accounts) };
110
+ case "presence-watch":
111
+ if (!Array.isArray(request.accounts) ||
112
+ request.accounts.length > PRESENCE_WATCH_LIMIT)
113
+ throw new Error("Signal Protocol Relay presence watch is invalid");
114
+ return { accounts: targetFields(request.accounts) };
115
+ default:
116
+ throw new Error("Signal Protocol Relay presence request is invalid");
117
+ }
118
+ }
119
+ /** The socket frame of one presence request. */
120
+ export function encodePresenceFrame(id, request) {
121
+ if (!REQUEST_ID.test(id))
122
+ throw new Error("Signal Protocol Relay presence request id is invalid");
123
+ return JSON.stringify({
124
+ type: request.type,
125
+ version: PRESENCE_FRAME_VERSION,
126
+ id,
127
+ ...requestFields(request),
128
+ });
129
+ }
130
+ /**
131
+ * The HTTP body of one presence request from a client with no socket. A watch
132
+ * exists only on the socket, so this carrier refuses it.
133
+ */
134
+ export function presenceHttpBody(publishableKey, request) {
135
+ if (request.type === "presence-watch")
136
+ throw new Error("Signal Protocol Relay presence watch needs the mailbox socket");
137
+ return {
138
+ type: request.type,
139
+ version: PRESENCE_FRAME_VERSION,
140
+ ...requestFields(request),
141
+ publishableKey,
142
+ };
143
+ }
144
+ /** True for the frame types that answer a presence request. */
145
+ export function isPresenceAnswerType(type) {
146
+ return type === "presence-result" || type === "presence-error";
147
+ }
148
+ /**
149
+ * Decodes a `presence-result` or `presence-error` frame. A malformed answer
150
+ * throws, and the subscription treats it as an invalid frame.
151
+ */
152
+ export function decodePresenceAnswer(frame) {
153
+ if (frame.version !== PRESENCE_FRAME_VERSION ||
154
+ typeof frame.id !== "string" ||
155
+ !REQUEST_ID.test(frame.id))
156
+ throw new Error("Invalid presence frame.");
157
+ if (frame.type === "presence-result") {
158
+ if (!exactKeys(frame, ["type", "version", "id", "result"]))
159
+ throw new Error("Invalid presence frame.");
160
+ return { id: frame.id, result: frame.result };
161
+ }
162
+ if (frame.type !== "presence-error" ||
163
+ !exactKeys(frame, ["type", "version", "id", "code", "message", "retryable"]) ||
164
+ typeof frame.code !== "string" ||
165
+ frame.code.length > MAXIMUM_ERROR_CODE_CHARACTERS ||
166
+ !ERROR_CODE.test(frame.code) ||
167
+ typeof frame.message !== "string" ||
168
+ frame.message.length > MAXIMUM_ERROR_MESSAGE_CHARACTERS ||
169
+ typeof frame.retryable !== "boolean")
170
+ throw new Error("Invalid presence frame.");
171
+ return {
172
+ id: frame.id,
173
+ error: new HostedRelayPresenceError(frame.code, frame.message, frame.retryable),
174
+ };
175
+ }
176
+ /** True for the frame type of a pushed presence edge. */
177
+ export function isPresenceUpdateType(type) {
178
+ return type === "presence-update";
179
+ }
180
+ /**
181
+ * Decodes a `presence-update` frame. A malformed frame throws, and the
182
+ * subscription treats it as an invalid frame. The Relay never pushes `null`.
183
+ */
184
+ export function decodePresenceUpdate(frame) {
185
+ if (frame.type !== "presence-update" ||
186
+ frame.version !== PRESENCE_FRAME_VERSION ||
187
+ !exactKeys(frame, ["type", "version", "account", "presence"]) ||
188
+ typeof frame.account !== "string" ||
189
+ frame.account.length === 0 ||
190
+ frame.account.length > MAXIMUM_ACCOUNT_CHARACTERS)
191
+ throw new Error("Invalid presence frame.");
192
+ let presence;
193
+ try {
194
+ presence = decodeStatus(frame.presence);
195
+ }
196
+ catch {
197
+ throw new Error("Invalid presence frame.");
198
+ }
199
+ if (presence === null)
200
+ throw new Error("Invalid presence frame.");
201
+ return { account: frame.account, presence };
202
+ }
203
+ const MODES = ["off", "default-on", "default-off", "forced"];
204
+ const AUDIENCES = ["contacts", "project"];
205
+ const LAST_SEEN_POLICIES = [
206
+ "exact",
207
+ "approximate",
208
+ "hidden",
209
+ ];
210
+ const SETTINGS = ["unset", "visible", "hidden"];
211
+ const VISIBILITIES = ["visible", "hidden"];
212
+ const APPROXIMATE_LAST_SEEN = [
213
+ "recently",
214
+ "within-week",
215
+ "within-month",
216
+ "long-ago",
217
+ ];
218
+ function decodePolicy(value) {
219
+ if (!record(value) ||
220
+ !exactKeys(value, ["audience", "lastSeen", "mode", "reciprocal"]) ||
221
+ !AUDIENCES.includes(value.audience) ||
222
+ !LAST_SEEN_POLICIES.includes(value.lastSeen) ||
223
+ !MODES.includes(value.mode) ||
224
+ typeof value.reciprocal !== "boolean")
225
+ throw invalidResult();
226
+ return {
227
+ audience: value.audience,
228
+ lastSeen: value.lastSeen,
229
+ mode: value.mode,
230
+ reciprocal: value.reciprocal,
231
+ };
232
+ }
233
+ function decodeAccount(value) {
234
+ if (!record(value) ||
235
+ !exactKeys(value, ["effective", "keyRegistered", "mode", "setting"]) ||
236
+ !VISIBILITIES.includes(value.effective) ||
237
+ typeof value.keyRegistered !== "boolean" ||
238
+ !MODES.includes(value.mode) ||
239
+ !SETTINGS.includes(value.setting))
240
+ throw invalidResult();
241
+ return {
242
+ effective: value.effective,
243
+ keyRegistered: value.keyRegistered,
244
+ mode: value.mode,
245
+ setting: value.setting,
246
+ };
247
+ }
248
+ function decodeStatus(value) {
249
+ if (value === null)
250
+ return null;
251
+ if (!record(value) ||
252
+ !exactKeys(value, ["lastSeen", "online"]) ||
253
+ typeof value.online !== "boolean" ||
254
+ !(value.lastSeen === null ||
255
+ APPROXIMATE_LAST_SEEN.includes(value.lastSeen) ||
256
+ (typeof value.lastSeen === "number" &&
257
+ Number.isSafeInteger(value.lastSeen) &&
258
+ value.lastSeen >= 0)))
259
+ throw invalidResult();
260
+ return {
261
+ lastSeen: value.lastSeen,
262
+ online: value.online,
263
+ };
264
+ }
265
+ /** Decodes the `result` of one request, and throws on any other shape. */
266
+ export function decodePresenceResult(request, value) {
267
+ switch (request.type) {
268
+ case "presence-policy":
269
+ return decodePolicy(value);
270
+ case "presence-account":
271
+ case "presence-setting":
272
+ return decodeAccount(value);
273
+ case "presence-key":
274
+ if (!record(value) || Object.keys(value).length !== 0)
275
+ throw invalidResult();
276
+ return undefined;
277
+ case "presence-read":
278
+ case "presence-watch": {
279
+ if (!record(value) ||
280
+ !exactKeys(value, ["results"]) ||
281
+ !Array.isArray(value.results) ||
282
+ value.results.length !== request.accounts.length)
283
+ throw invalidResult();
284
+ const results = value.results.map(decodeStatus);
285
+ return results;
286
+ }
287
+ default:
288
+ throw invalidResult();
289
+ }
290
+ }
291
+ //# sourceMappingURL=hosted-presence-frames.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hosted-presence-frames.js","sourceRoot":"","sources":["../../client/hosted-presence-frames.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,OAAO,EACL,aAAa,EACb,oBAAoB,EACpB,eAAe,GAChB,MAAM,oBAAoB,CAAC;AAG5B,kEAAkE;AAClE,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,CAAC;AAExC,4DAA4D;AAC5D,MAAM,CAAC,MAAM,mBAAmB,GAAG,GAAG,CAAC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AQ,IAAI,CAAC,CAAC,CACvB;QAED,MAAM,aAAa,EAAE,CAAC;IACxB,OAAO;QACL,QAAQ,EAAE,KAAK,CAAC,QAAiD;QACjE,MAAM,EAAE,KAAK,CAAC,MAAM;KACrB,CAAC;AACJ,CAAC;AAED,0EAA0E;AAC1E,MAAM,UAAU,oBAAoB,CAGlC,OAAgB,EAChB,KAAc;IAGd,QAAQ,OAAO,CAAC,IAAI,EAAE,CAAC;QACrB,KAAK,iBAAiB;YACpB,OAAO,YAAY,CAAC,KAAK,CAAW,CAAC;QACvC,KAAK,kBAAkB,CAAC;QACxB,KAAK,kBAAkB;YACrB,OAAO,aAAa,CAAC,KAAK,CAAW,CAAC;QACxC,KAAK,cAAc;YACjB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,KAAK,CAAC;gBACnD,MAAM,aAAa,EAAE,CAAC;YACxB,OAAO,SAAmB,CAAC;QAC7B,KAAK,eAAe,CAAC;QACrB,KAAK,gBAAgB,EAAE,CAAC;YACtB,IACE,CAAC,MAAM,CAAC,KAAK,CAAC;gBACd,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC,SAAS,CAAC,CAAC;gBAC9B,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC;gBAC7B,KAAK,CAAC,OAAO,CAAC,MAAM,KAAK,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAEhD,MAAM,aAAa,EAAE,CAAC;YACxB,MAAM,OAAO,GACX,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;YAClC,OAAO,OAA4B,CAAC;QACtC,CAAC;QACD;YACE,MAAM,aAAa,EAAE,CAAC;IAC1B,CAAC;AACH,CAAC"}
@@ -0,0 +1,83 @@
1
+ import type { Unsubscribe } from "../remote/relay/types.js";
2
+ import type { DefaultSignalProtocolClient } from "./client.js";
3
+ import type { HostedRelayWakeClient } from "./hosted-push.js";
4
+ import { type HostedRelayPresenceAccount, type HostedRelayPresencePolicy, type HostedRelayPresenceStatus, type HostedRelayPresenceVisibility } from "./hosted-presence-frames.js";
5
+ export { HostedRelayPresenceError, type HostedRelayPresenceAccount, type HostedRelayPresenceApproximateLastSeen, type HostedRelayPresenceAudience, type HostedRelayPresenceErrorCode, type HostedRelayPresenceLastSeenPolicy, type HostedRelayPresenceMode, type HostedRelayPresencePolicy, type HostedRelayPresenceSetting, type HostedRelayPresenceStatus, type HostedRelayPresenceVisibility, } from "./hosted-presence-frames.js";
6
+ /**
7
+ * The interval of the `watch()` poll. It reads only the accounts that no
8
+ * answered presence watch covers.
9
+ */
10
+ export declare const PRESENCE_WATCH_POLL_MILLISECONDS = 30000;
11
+ /**
12
+ * The interval of the `watch()` renewal. Each renewal sends the whole watch
13
+ * set again, and its answer is the read of the watched accounts. The Relay
14
+ * drops a watch that is not renewed in two intervals.
15
+ */
16
+ export declare const PRESENCE_WATCH_RENEWAL_MILLISECONDS = 120000;
17
+ /**
18
+ * The presence of a hosted client with a mailbox subscription. Each request
19
+ * needs a connected socket and otherwise fails with `NOT_CONNECTED`.
20
+ */
21
+ export interface HostedRelayPresence {
22
+ /** The project's presence policy, so the app can show or disable its toggle. */
23
+ policy(): Promise<HostedRelayPresencePolicy>;
24
+ /** The local account's setting and the visibility that applies. */
25
+ setting(): Promise<HostedRelayPresenceAccount>;
26
+ /**
27
+ * Writes the local account's setting. It fails with
28
+ * `PRESENCE_SETTING_FORCED` under the `forced` mode and with
29
+ * `INVALID_TRANSITION` under the `off` mode.
30
+ */
31
+ setVisibility(visibility: HostedRelayPresenceVisibility): Promise<HostedRelayPresenceAccount>;
32
+ /**
33
+ * Reads the presence of each account, in the order given. A null result is
34
+ * the same for a hidden account, an unknown account, a missing or wrong
35
+ * key, and a project with presence off. The read sends the key that
36
+ * `grant()` stored for each account.
37
+ */
38
+ read(accounts: readonly string[]): Promise<(HostedRelayPresenceStatus | null)[]>;
39
+ /**
40
+ * Calls `onChange` with the account's presence, then again after each
41
+ * change. While the mailbox socket is connected, the client registers one
42
+ * presence watch for the first 16 watched accounts. It registers at once,
43
+ * at each change of that set, at each new socket, and again every 120 s.
44
+ * The Relay then pushes each change. The client reads the other accounts
45
+ * every 30 s. After a failed watch, or when no answer arrives in 10 s, it
46
+ * reads every account every 30 s and tries the watch again 120 s later. A
47
+ * failed read keeps the last value. After a `FRAME_REJECTED` request, it
48
+ * waits 30 s after the reconnect to read and 120 s to watch.
49
+ */
50
+ watch(account: string, onChange: (status: HostedRelayPresenceStatus | null) => void): Unsubscribe;
51
+ /**
52
+ * Derives this account's presence key from its 32-byte profile key,
53
+ * registers the key with the Relay, and returns it. A new profile key
54
+ * revokes every old holder. With `hosted.profileKeys`, the SDK calls it
55
+ * when the profile key changes.
56
+ */
57
+ accessKey(profileKey: Uint8Array): Promise<string>;
58
+ /**
59
+ * Stores the presence key that a contact sent, for later reads. With
60
+ * `hosted.profileKeys`, the SDK calls it for each contact profile key that
61
+ * it receives.
62
+ */
63
+ grant(account: string, presenceKey: string): Promise<void>;
64
+ }
65
+ /**
66
+ * The presence of a wake client: a hosted client in a push handler, a
67
+ * notification service extension, or a background task, which pulls its
68
+ * mailbox without a subscription. Each request is one authenticated HTTP
69
+ * request. A wake client has no `watch()`.
70
+ */
71
+ export type HostedRelayWakePresence = Omit<HostedRelayPresence, "watch">;
72
+ /**
73
+ * The presence of a hosted client with a mailbox subscription. Every request
74
+ * goes over the socket, so start the subscription first. Each client has one
75
+ * presence object, and its watches share one presence watch and one poll.
76
+ */
77
+ export declare function hostedRelayPresence(client: DefaultSignalProtocolClient): HostedRelayPresence;
78
+ /**
79
+ * The presence of a wake client, over HTTP. Use it only where the client runs
80
+ * no mailbox subscription, for example beside `pullHostedRelayAfterWake()`.
81
+ */
82
+ export declare function hostedRelayWakePresence(client: HostedRelayWakeClient): HostedRelayWakePresence;
83
+ //# sourceMappingURL=hosted-presence.d.ts.map
@@ -0,0 +1 @@
1
+ {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