@abraca/dabra 1.0.20 → 1.0.22
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/dist/abracadabra-provider.cjs +595 -5
- package/dist/abracadabra-provider.cjs.map +1 -1
- package/dist/abracadabra-provider.esm.js +593 -6
- package/dist/abracadabra-provider.esm.js.map +1 -1
- package/dist/index.d.ts +251 -1
- package/package.json +1 -1
- package/src/AbracadabraClient.ts +27 -0
- package/src/AbracadabraProvider.ts +10 -1
- package/src/IdentityDoc.ts +497 -0
- package/src/index.ts +7 -0
- package/src/types.ts +3 -0
- package/src/webrtc/AbracadabraWebRTC.ts +8 -4
- package/src/webrtc/DevicePairingChannel.ts +384 -0
- package/src/webrtc/index.ts +2 -0
|
@@ -2792,7 +2792,7 @@ var AbracadabraProvider = class AbracadabraProvider extends AbracadabraBaseProvi
|
|
|
2792
2792
|
this._client = client;
|
|
2793
2793
|
this.abracadabraConfig = configuration;
|
|
2794
2794
|
this.subdocLoading = configuration.subdocLoading ?? "lazy";
|
|
2795
|
-
const serverOrigin = AbracadabraProvider.deriveServerOrigin(configuration, client);
|
|
2795
|
+
const serverOrigin = configuration.serverAgnostic ? void 0 : AbracadabraProvider.deriveServerOrigin(configuration, client);
|
|
2796
2796
|
this.offlineStore = configuration.disableOfflineStore ? null : new OfflineStore(configuration.name, serverOrigin);
|
|
2797
2797
|
this.on("subdocRegistered", configuration.onSubdocRegistered ?? (() => null));
|
|
2798
2798
|
this.on("subdocLoaded", configuration.onSubdocLoaded ?? (() => null));
|
|
@@ -3198,10 +3198,30 @@ var AbracadabraClient = class {
|
|
|
3198
3198
|
async listKeys() {
|
|
3199
3199
|
return (await this.request("GET", "/auth/keys")).keys;
|
|
3200
3200
|
}
|
|
3201
|
+
/** Rename a registered device key. */
|
|
3202
|
+
async renameKey(keyId, deviceName) {
|
|
3203
|
+
await this.request("PATCH", `/auth/keys/${encodeURIComponent(keyId)}`, { body: { deviceName } });
|
|
3204
|
+
}
|
|
3201
3205
|
/** Revoke a public key by its ID. */
|
|
3202
3206
|
async revokeKey(keyId) {
|
|
3203
3207
|
await this.request("DELETE", `/auth/keys/${encodeURIComponent(keyId)}`);
|
|
3204
3208
|
}
|
|
3209
|
+
/** Create a single-use device invite code for pairing a new device to this account. */
|
|
3210
|
+
async createDeviceInvite(opts) {
|
|
3211
|
+
return this.request("POST", "/auth/device-invite", { body: opts?.expiresIn != null ? { expiresIn: opts.expiresIn } : {} });
|
|
3212
|
+
}
|
|
3213
|
+
/** Redeem a device invite code to register a new device key. Returns a JWT token. */
|
|
3214
|
+
async redeemDeviceInvite(opts) {
|
|
3215
|
+
return this.request("POST", "/auth/device-redeem", {
|
|
3216
|
+
body: {
|
|
3217
|
+
code: opts.code,
|
|
3218
|
+
publicKey: opts.publicKey,
|
|
3219
|
+
x25519Key: opts.x25519Key,
|
|
3220
|
+
deviceName: opts.deviceName
|
|
3221
|
+
},
|
|
3222
|
+
auth: false
|
|
3223
|
+
});
|
|
3224
|
+
}
|
|
3205
3225
|
/** Get encryption info for a document. */
|
|
3206
3226
|
async getDocEncryption(docId) {
|
|
3207
3227
|
return this.request("GET", `/docs/${encodeURIComponent(docId)}/encryption`);
|
|
@@ -9897,19 +9917,23 @@ var AbracadabraWebRTC = class AbracadabraWebRTC extends EventEmitter {
|
|
|
9897
9917
|
}
|
|
9898
9918
|
/**
|
|
9899
9919
|
* Send a custom string message to a specific peer via a data channel.
|
|
9920
|
+
* When E2EE is active, the message is encrypted through the router.
|
|
9900
9921
|
*/
|
|
9901
9922
|
sendCustomMessage(peerId, payload) {
|
|
9902
9923
|
const pc = this.peerConnections.get(peerId);
|
|
9903
9924
|
if (!pc) return;
|
|
9904
|
-
|
|
9925
|
+
const channelName = "custom";
|
|
9926
|
+
let channel = pc.router.getChannel(channelName);
|
|
9905
9927
|
if (!channel || channel.readyState !== "open") {
|
|
9906
|
-
channel = pc.router.createChannel(
|
|
9928
|
+
channel = pc.router.createChannel(channelName, { ordered: true });
|
|
9907
9929
|
channel.onopen = () => {
|
|
9908
|
-
|
|
9930
|
+
const data = new TextEncoder().encode(payload);
|
|
9931
|
+
pc.router.send(channelName, data);
|
|
9909
9932
|
};
|
|
9910
9933
|
return;
|
|
9911
9934
|
}
|
|
9912
|
-
|
|
9935
|
+
const data = new TextEncoder().encode(payload);
|
|
9936
|
+
pc.router.send(channelName, data);
|
|
9913
9937
|
}
|
|
9914
9938
|
/**
|
|
9915
9939
|
* Send a custom string message to all connected peers.
|
|
@@ -10256,6 +10280,270 @@ var ManualSignaling = class extends EventEmitter {
|
|
|
10256
10280
|
}
|
|
10257
10281
|
};
|
|
10258
10282
|
|
|
10283
|
+
//#endregion
|
|
10284
|
+
//#region packages/provider/src/webrtc/DevicePairingChannel.ts
|
|
10285
|
+
/**
|
|
10286
|
+
* DevicePairingChannel
|
|
10287
|
+
*
|
|
10288
|
+
* Enables cross-device identity pairing over an E2EE WebRTC data channel.
|
|
10289
|
+
* Device A (approver) creates a pairing session with a short code. Device B
|
|
10290
|
+
* (requester) joins with the code. After E2EE is established, Device B sends
|
|
10291
|
+
* its public key and Device A registers it via `addKey()`.
|
|
10292
|
+
*
|
|
10293
|
+
* Reuses the existing WebRTC stack: SignalingSocket, PeerConnection,
|
|
10294
|
+
* DataChannelRouter, and E2EEChannel (X25519 ECDH + AES-256-GCM).
|
|
10295
|
+
*/
|
|
10296
|
+
/** Ambiguity-free charset (no 0/O/1/I). */
|
|
10297
|
+
const CODE_CHARSET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
|
10298
|
+
const CODE_LENGTH = 6;
|
|
10299
|
+
const PAIRING_TIMEOUT_MS = 300 * 1e3;
|
|
10300
|
+
function generatePairingCode() {
|
|
10301
|
+
const bytes = crypto.getRandomValues(new Uint8Array(CODE_LENGTH));
|
|
10302
|
+
return Array.from(bytes).map((b) => CODE_CHARSET[b % 32]).join("");
|
|
10303
|
+
}
|
|
10304
|
+
function codeToRoomId(code) {
|
|
10305
|
+
const hash = sha256(new TextEncoder().encode(code.toUpperCase()));
|
|
10306
|
+
return `__pairing_${Array.from(hash.slice(0, 8)).map((b) => b.toString(16).padStart(2, "0")).join("")}`;
|
|
10307
|
+
}
|
|
10308
|
+
var DevicePairingChannel = class DevicePairingChannel extends EventEmitter {
|
|
10309
|
+
constructor(role, pairingCode, config) {
|
|
10310
|
+
super();
|
|
10311
|
+
this.config = config;
|
|
10312
|
+
this.webrtc = null;
|
|
10313
|
+
this.timeoutHandle = null;
|
|
10314
|
+
this._destroyed = false;
|
|
10315
|
+
this._pendingRequest = null;
|
|
10316
|
+
this._connectedPeerId = null;
|
|
10317
|
+
this.role = role;
|
|
10318
|
+
this.pairingCode = pairingCode;
|
|
10319
|
+
}
|
|
10320
|
+
/**
|
|
10321
|
+
* Create an approver session (Device A). Returns the channel and a
|
|
10322
|
+
* 6-character pairing code to share with Device B.
|
|
10323
|
+
*/
|
|
10324
|
+
static createApprover(config) {
|
|
10325
|
+
const code = generatePairingCode();
|
|
10326
|
+
const channel = new DevicePairingChannel("approver", code, config);
|
|
10327
|
+
channel.start();
|
|
10328
|
+
return {
|
|
10329
|
+
channel,
|
|
10330
|
+
pairingCode: code
|
|
10331
|
+
};
|
|
10332
|
+
}
|
|
10333
|
+
/**
|
|
10334
|
+
* Create a requester session (Device B). Joins with a pairing code
|
|
10335
|
+
* obtained from Device A.
|
|
10336
|
+
*/
|
|
10337
|
+
static createRequester(config, pairingCode) {
|
|
10338
|
+
const channel = new DevicePairingChannel("requester", pairingCode.toUpperCase().replace(/[^A-Z2-9]/g, ""), config);
|
|
10339
|
+
channel.start();
|
|
10340
|
+
return channel;
|
|
10341
|
+
}
|
|
10342
|
+
/**
|
|
10343
|
+
* Approve the pending pairing request. Calls `client.addKey()` to
|
|
10344
|
+
* register Device B's public key, then notifies Device B.
|
|
10345
|
+
*/
|
|
10346
|
+
async approve(client) {
|
|
10347
|
+
if (this.role !== "approver") return {
|
|
10348
|
+
success: false,
|
|
10349
|
+
error: "Only the approver can approve"
|
|
10350
|
+
};
|
|
10351
|
+
if (!this._pendingRequest) return {
|
|
10352
|
+
success: false,
|
|
10353
|
+
error: "No pending pairing request"
|
|
10354
|
+
};
|
|
10355
|
+
if (!this._connectedPeerId) return {
|
|
10356
|
+
success: false,
|
|
10357
|
+
error: "Peer not connected"
|
|
10358
|
+
};
|
|
10359
|
+
const req = this._pendingRequest;
|
|
10360
|
+
try {
|
|
10361
|
+
await client.addKey({
|
|
10362
|
+
publicKey: req.publicKey,
|
|
10363
|
+
deviceName: req.deviceName,
|
|
10364
|
+
x25519Key: req.x25519Key
|
|
10365
|
+
});
|
|
10366
|
+
this.sendMessage({ type: "pair-approved" });
|
|
10367
|
+
this._pendingRequest = null;
|
|
10368
|
+
this.emit("pairingComplete", { success: true });
|
|
10369
|
+
return { success: true };
|
|
10370
|
+
} catch (err) {
|
|
10371
|
+
const error = err?.message ?? "Failed to register device key";
|
|
10372
|
+
this.sendMessage({
|
|
10373
|
+
type: "pair-rejected",
|
|
10374
|
+
reason: error
|
|
10375
|
+
});
|
|
10376
|
+
this._pendingRequest = null;
|
|
10377
|
+
const result = {
|
|
10378
|
+
success: false,
|
|
10379
|
+
error
|
|
10380
|
+
};
|
|
10381
|
+
this.emit("pairingComplete", result);
|
|
10382
|
+
return result;
|
|
10383
|
+
}
|
|
10384
|
+
}
|
|
10385
|
+
/**
|
|
10386
|
+
* Approve via server-side device invite. Creates a single-use invite code
|
|
10387
|
+
* and sends it to Device B over the E2EE channel. Device B redeems it
|
|
10388
|
+
* independently via HTTP — Device A can go offline after this.
|
|
10389
|
+
*/
|
|
10390
|
+
async approveWithInvite(client) {
|
|
10391
|
+
if (this.role !== "approver") return {
|
|
10392
|
+
success: false,
|
|
10393
|
+
error: "Only the approver can approve"
|
|
10394
|
+
};
|
|
10395
|
+
if (!this._pendingRequest) return {
|
|
10396
|
+
success: false,
|
|
10397
|
+
error: "No pending pairing request"
|
|
10398
|
+
};
|
|
10399
|
+
if (!this._connectedPeerId) return {
|
|
10400
|
+
success: false,
|
|
10401
|
+
error: "Peer not connected"
|
|
10402
|
+
};
|
|
10403
|
+
try {
|
|
10404
|
+
const { code } = await client.createDeviceInvite();
|
|
10405
|
+
this.sendMessage({
|
|
10406
|
+
type: "pair-invite-code",
|
|
10407
|
+
code
|
|
10408
|
+
});
|
|
10409
|
+
this._pendingRequest = null;
|
|
10410
|
+
this.emit("pairingComplete", { success: true });
|
|
10411
|
+
return { success: true };
|
|
10412
|
+
} catch (err) {
|
|
10413
|
+
const error = err?.message ?? "Failed to create device invite";
|
|
10414
|
+
this.sendMessage({
|
|
10415
|
+
type: "pair-rejected",
|
|
10416
|
+
reason: error
|
|
10417
|
+
});
|
|
10418
|
+
this._pendingRequest = null;
|
|
10419
|
+
const result = {
|
|
10420
|
+
success: false,
|
|
10421
|
+
error
|
|
10422
|
+
};
|
|
10423
|
+
this.emit("pairingComplete", result);
|
|
10424
|
+
return result;
|
|
10425
|
+
}
|
|
10426
|
+
}
|
|
10427
|
+
/**
|
|
10428
|
+
* Reject the pending pairing request.
|
|
10429
|
+
*/
|
|
10430
|
+
reject(reason = "Rejected by user") {
|
|
10431
|
+
if (this.role !== "approver" || !this._pendingRequest) return;
|
|
10432
|
+
this.sendMessage({
|
|
10433
|
+
type: "pair-rejected",
|
|
10434
|
+
reason
|
|
10435
|
+
});
|
|
10436
|
+
this._pendingRequest = null;
|
|
10437
|
+
this.emit("pairingComplete", {
|
|
10438
|
+
success: false,
|
|
10439
|
+
error: reason
|
|
10440
|
+
});
|
|
10441
|
+
}
|
|
10442
|
+
/**
|
|
10443
|
+
* Send a pairing request to Device A. Call this after the "connected"
|
|
10444
|
+
* event fires.
|
|
10445
|
+
*/
|
|
10446
|
+
requestPairing(request) {
|
|
10447
|
+
if (this.role !== "requester") return;
|
|
10448
|
+
this.sendMessage({
|
|
10449
|
+
type: "pair-request",
|
|
10450
|
+
publicKey: request.publicKey,
|
|
10451
|
+
x25519Key: request.x25519Key,
|
|
10452
|
+
deviceName: request.deviceName
|
|
10453
|
+
});
|
|
10454
|
+
}
|
|
10455
|
+
get isDestroyed() {
|
|
10456
|
+
return this._destroyed;
|
|
10457
|
+
}
|
|
10458
|
+
destroy() {
|
|
10459
|
+
if (this._destroyed) return;
|
|
10460
|
+
this._destroyed = true;
|
|
10461
|
+
if (this.timeoutHandle) {
|
|
10462
|
+
clearTimeout(this.timeoutHandle);
|
|
10463
|
+
this.timeoutHandle = null;
|
|
10464
|
+
}
|
|
10465
|
+
if (this.webrtc) {
|
|
10466
|
+
this.webrtc.destroy();
|
|
10467
|
+
this.webrtc = null;
|
|
10468
|
+
}
|
|
10469
|
+
this.removeAllListeners();
|
|
10470
|
+
}
|
|
10471
|
+
start() {
|
|
10472
|
+
this.webrtc = new AbracadabraWebRTC({
|
|
10473
|
+
docId: codeToRoomId(this.pairingCode),
|
|
10474
|
+
url: this.config.serverUrl,
|
|
10475
|
+
token: this.config.token,
|
|
10476
|
+
iceServers: this.config.iceServers,
|
|
10477
|
+
e2ee: this.config.e2ee,
|
|
10478
|
+
enableDocSync: false,
|
|
10479
|
+
enableAwarenessSync: false,
|
|
10480
|
+
enableFileTransfer: false,
|
|
10481
|
+
autoConnect: false,
|
|
10482
|
+
WebSocketPolyfill: this.config.WebSocketPolyfill
|
|
10483
|
+
});
|
|
10484
|
+
this.webrtc.on("e2eeEstablished", ({ peerId }) => {
|
|
10485
|
+
this._connectedPeerId = peerId;
|
|
10486
|
+
this.emit("connected");
|
|
10487
|
+
});
|
|
10488
|
+
this.webrtc.on("customMessage", ({ peerId, payload }) => {
|
|
10489
|
+
this.handleMessage(peerId, payload);
|
|
10490
|
+
});
|
|
10491
|
+
this.webrtc.on("peerLeft", () => {
|
|
10492
|
+
if (!this._destroyed) this.emit("error", /* @__PURE__ */ new Error("Peer disconnected"));
|
|
10493
|
+
});
|
|
10494
|
+
this.webrtc.on("signalingError", (err) => {
|
|
10495
|
+
this.emit("error", /* @__PURE__ */ new Error(`Signaling: ${err.message}`));
|
|
10496
|
+
});
|
|
10497
|
+
this.timeoutHandle = setTimeout(() => {
|
|
10498
|
+
if (!this._destroyed) {
|
|
10499
|
+
this.emit("error", /* @__PURE__ */ new Error("Pairing timed out"));
|
|
10500
|
+
this.destroy();
|
|
10501
|
+
}
|
|
10502
|
+
}, PAIRING_TIMEOUT_MS);
|
|
10503
|
+
this.webrtc.connect();
|
|
10504
|
+
}
|
|
10505
|
+
sendMessage(msg) {
|
|
10506
|
+
if (!this.webrtc || !this._connectedPeerId) return;
|
|
10507
|
+
this.webrtc.sendCustomMessage(this._connectedPeerId, JSON.stringify(msg));
|
|
10508
|
+
}
|
|
10509
|
+
handleMessage(peerId, payload) {
|
|
10510
|
+
let msg;
|
|
10511
|
+
try {
|
|
10512
|
+
msg = JSON.parse(payload);
|
|
10513
|
+
} catch {
|
|
10514
|
+
return;
|
|
10515
|
+
}
|
|
10516
|
+
switch (msg.type) {
|
|
10517
|
+
case "pair-request":
|
|
10518
|
+
if (this.role !== "approver") return;
|
|
10519
|
+
this._pendingRequest = {
|
|
10520
|
+
publicKey: msg.publicKey,
|
|
10521
|
+
x25519Key: msg.x25519Key,
|
|
10522
|
+
deviceName: msg.deviceName
|
|
10523
|
+
};
|
|
10524
|
+
this.emit("pairingRequest", this._pendingRequest);
|
|
10525
|
+
break;
|
|
10526
|
+
case "pair-approved":
|
|
10527
|
+
if (this.role !== "requester") return;
|
|
10528
|
+
this.emit("approved");
|
|
10529
|
+
this.emit("pairingComplete", { success: true });
|
|
10530
|
+
break;
|
|
10531
|
+
case "pair-rejected":
|
|
10532
|
+
if (this.role !== "requester") return;
|
|
10533
|
+
this.emit("rejected", msg.reason);
|
|
10534
|
+
this.emit("pairingComplete", {
|
|
10535
|
+
success: false,
|
|
10536
|
+
error: msg.reason
|
|
10537
|
+
});
|
|
10538
|
+
break;
|
|
10539
|
+
case "pair-invite-code":
|
|
10540
|
+
if (this.role !== "requester") return;
|
|
10541
|
+
this.emit("inviteCode", msg.code);
|
|
10542
|
+
break;
|
|
10543
|
+
}
|
|
10544
|
+
}
|
|
10545
|
+
};
|
|
10546
|
+
|
|
10259
10547
|
//#endregion
|
|
10260
10548
|
//#region packages/provider/src/sync/BroadcastChannelSync.ts
|
|
10261
10549
|
/**
|
|
@@ -10408,5 +10696,304 @@ var BroadcastChannelSync = class BroadcastChannelSync extends EventEmitter {
|
|
|
10408
10696
|
};
|
|
10409
10697
|
|
|
10410
10698
|
//#endregion
|
|
10411
|
-
|
|
10699
|
+
//#region packages/provider/src/IdentityDoc.ts
|
|
10700
|
+
/**
|
|
10701
|
+
* Derives a deterministic UUID from an Ed25519 account-level public key.
|
|
10702
|
+
*
|
|
10703
|
+
* The result is a valid UUID v5-style string that any device sharing the
|
|
10704
|
+
* same identity can independently compute. The `abracadabra:identity:`
|
|
10705
|
+
* prefix prevents collisions with randomly generated doc UUIDs.
|
|
10706
|
+
*
|
|
10707
|
+
* @param publicKeyB64 Base64url-encoded Ed25519 public key (32 bytes).
|
|
10708
|
+
*/
|
|
10709
|
+
function deriveIdentityDocId(publicKeyB64) {
|
|
10710
|
+
const hash = sha256(new TextEncoder().encode(`abracadabra:identity:${publicKeyB64}`));
|
|
10711
|
+
const bytes = new Uint8Array(hash.buffer, hash.byteOffset, 16);
|
|
10712
|
+
bytes[6] = bytes[6] & 15 | 80;
|
|
10713
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
10714
|
+
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
10715
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20, 32)}`;
|
|
10716
|
+
}
|
|
10717
|
+
/**
|
|
10718
|
+
* Manages a Y.Doc dedicated to user identity that syncs across trusted
|
|
10719
|
+
* targets only: a designated sync server, a local Tauri server, and/or
|
|
10720
|
+
* WebRTC P2P.
|
|
10721
|
+
*
|
|
10722
|
+
* The Y.Doc contains cross-device settings (profile, servers, spaces,
|
|
10723
|
+
* plugins, preferences). Each sync target gets its own
|
|
10724
|
+
* AbracadabraProvider sharing the same Y.Doc, with `serverAgnostic: true`
|
|
10725
|
+
* so they all use one IndexedDB store.
|
|
10726
|
+
*/
|
|
10727
|
+
var IdentityDocProvider = class extends EventEmitter {
|
|
10728
|
+
constructor(configuration) {
|
|
10729
|
+
super();
|
|
10730
|
+
this.providers = /* @__PURE__ */ new Map();
|
|
10731
|
+
this.websockets = /* @__PURE__ */ new Map();
|
|
10732
|
+
this.webrtc = null;
|
|
10733
|
+
this._destroyed = false;
|
|
10734
|
+
this.config = configuration;
|
|
10735
|
+
this.docId = deriveIdentityDocId(configuration.publicKey);
|
|
10736
|
+
this.document = new Y.Doc({ guid: this.docId });
|
|
10737
|
+
if (configuration.autoConnect !== false) this.connect();
|
|
10738
|
+
}
|
|
10739
|
+
connect() {
|
|
10740
|
+
if (this._destroyed) return;
|
|
10741
|
+
const targets = [];
|
|
10742
|
+
if (this.config.localServerUrl) targets.push({
|
|
10743
|
+
url: this.config.localServerUrl,
|
|
10744
|
+
key: "local"
|
|
10745
|
+
});
|
|
10746
|
+
if (this.config.syncServerUrl) targets.push({
|
|
10747
|
+
url: this.config.syncServerUrl,
|
|
10748
|
+
key: "sync"
|
|
10749
|
+
});
|
|
10750
|
+
for (const { url, key } of targets) {
|
|
10751
|
+
if (this.providers.has(key)) continue;
|
|
10752
|
+
this._connectToServer(key, url);
|
|
10753
|
+
}
|
|
10754
|
+
if (this.config.webrtc && !this.webrtc) this._connectWebRTC();
|
|
10755
|
+
}
|
|
10756
|
+
_connectToServer(key, serverUrl) {
|
|
10757
|
+
const token = this.config.tokens?.[serverUrl] ?? this.config.token ?? "";
|
|
10758
|
+
const ws = new AbracadabraWS({
|
|
10759
|
+
url: serverUrl.replace(/^http/, "ws").replace(/\/$/, "").concat("/ws"),
|
|
10760
|
+
WebSocketPolyfill: void 0
|
|
10761
|
+
});
|
|
10762
|
+
this.websockets.set(key, ws);
|
|
10763
|
+
const provider = new AbracadabraProvider({
|
|
10764
|
+
name: this.docId,
|
|
10765
|
+
document: this.document,
|
|
10766
|
+
websocketProvider: ws,
|
|
10767
|
+
token,
|
|
10768
|
+
serverAgnostic: true,
|
|
10769
|
+
disableOfflineStore: key !== "local" && key !== "sync" ? true : this.config.disableOfflineStore ?? false,
|
|
10770
|
+
...this.config.providerDefaults
|
|
10771
|
+
});
|
|
10772
|
+
provider.on("synced", () => this.emit("synced", { server: key }));
|
|
10773
|
+
provider.on("status", (data) => this.emit("status", {
|
|
10774
|
+
server: key,
|
|
10775
|
+
...data
|
|
10776
|
+
}));
|
|
10777
|
+
this.providers.set(key, provider);
|
|
10778
|
+
}
|
|
10779
|
+
_connectWebRTC() {
|
|
10780
|
+
const rtcConfig = this.config.webrtc;
|
|
10781
|
+
if (!rtcConfig) return;
|
|
10782
|
+
this.webrtc = new AbracadabraWebRTC({
|
|
10783
|
+
docId: this.docId,
|
|
10784
|
+
url: rtcConfig.signalingServerUrl,
|
|
10785
|
+
token: rtcConfig.token,
|
|
10786
|
+
document: this.document,
|
|
10787
|
+
enableDocSync: true,
|
|
10788
|
+
enableAwarenessSync: false,
|
|
10789
|
+
enableFileTransfer: false,
|
|
10790
|
+
e2ee: rtcConfig.e2ee,
|
|
10791
|
+
iceServers: rtcConfig.iceServers
|
|
10792
|
+
});
|
|
10793
|
+
}
|
|
10794
|
+
get profileMap() {
|
|
10795
|
+
return this.document.getMap("profile");
|
|
10796
|
+
}
|
|
10797
|
+
get serversMap() {
|
|
10798
|
+
return this.document.getMap("servers");
|
|
10799
|
+
}
|
|
10800
|
+
get spacesArray() {
|
|
10801
|
+
return this.document.getArray("spaces");
|
|
10802
|
+
}
|
|
10803
|
+
get pluginsMap() {
|
|
10804
|
+
return this.document.getMap("plugins");
|
|
10805
|
+
}
|
|
10806
|
+
get preferencesMap() {
|
|
10807
|
+
return this.document.getMap("preferences");
|
|
10808
|
+
}
|
|
10809
|
+
getProfile() {
|
|
10810
|
+
const m = this.profileMap;
|
|
10811
|
+
return {
|
|
10812
|
+
username: m.get("username"),
|
|
10813
|
+
displayName: m.get("displayName"),
|
|
10814
|
+
colorName: m.get("colorName"),
|
|
10815
|
+
neutralColorName: m.get("neutralColorName"),
|
|
10816
|
+
locale: m.get("locale"),
|
|
10817
|
+
avatarUrl: m.get("avatarUrl")
|
|
10818
|
+
};
|
|
10819
|
+
}
|
|
10820
|
+
setProfile(profile) {
|
|
10821
|
+
const m = this.profileMap;
|
|
10822
|
+
this.document.transact(() => {
|
|
10823
|
+
for (const [key, value] of Object.entries(profile)) if (value !== void 0) m.set(key, value);
|
|
10824
|
+
});
|
|
10825
|
+
}
|
|
10826
|
+
getServer(url) {
|
|
10827
|
+
const entry = this.serversMap.get(url);
|
|
10828
|
+
if (!entry) return void 0;
|
|
10829
|
+
return {
|
|
10830
|
+
label: entry.get("label") ?? url,
|
|
10831
|
+
hubDocId: entry.get("hubDocId"),
|
|
10832
|
+
entryDocId: entry.get("entryDocId"),
|
|
10833
|
+
defaultRole: entry.get("defaultRole"),
|
|
10834
|
+
spacesEnabled: entry.get("spacesEnabled"),
|
|
10835
|
+
addedAt: entry.get("addedAt") ?? 0
|
|
10836
|
+
};
|
|
10837
|
+
}
|
|
10838
|
+
setServer(url, entry) {
|
|
10839
|
+
const m = this.serversMap;
|
|
10840
|
+
this.document.transact(() => {
|
|
10841
|
+
let yEntry = m.get(url);
|
|
10842
|
+
if (!yEntry) {
|
|
10843
|
+
yEntry = new Y.Map();
|
|
10844
|
+
m.set(url, yEntry);
|
|
10845
|
+
}
|
|
10846
|
+
for (const [key, value] of Object.entries(entry)) if (value !== void 0) yEntry.set(key, value);
|
|
10847
|
+
});
|
|
10848
|
+
}
|
|
10849
|
+
removeServer(url) {
|
|
10850
|
+
this.serversMap.delete(url);
|
|
10851
|
+
}
|
|
10852
|
+
getServers() {
|
|
10853
|
+
const result = /* @__PURE__ */ new Map();
|
|
10854
|
+
this.serversMap.forEach((entry, url) => {
|
|
10855
|
+
result.set(url, {
|
|
10856
|
+
label: entry.get("label") ?? url,
|
|
10857
|
+
hubDocId: entry.get("hubDocId"),
|
|
10858
|
+
entryDocId: entry.get("entryDocId"),
|
|
10859
|
+
defaultRole: entry.get("defaultRole"),
|
|
10860
|
+
spacesEnabled: entry.get("spacesEnabled"),
|
|
10861
|
+
addedAt: entry.get("addedAt") ?? 0
|
|
10862
|
+
});
|
|
10863
|
+
});
|
|
10864
|
+
return result;
|
|
10865
|
+
}
|
|
10866
|
+
getSpaces() {
|
|
10867
|
+
const result = [];
|
|
10868
|
+
this.spacesArray.forEach((yMap) => {
|
|
10869
|
+
result.push({
|
|
10870
|
+
id: yMap.get("id"),
|
|
10871
|
+
name: yMap.get("name"),
|
|
10872
|
+
type: yMap.get("type") ?? "remote",
|
|
10873
|
+
serverUrl: yMap.get("serverUrl") ?? null,
|
|
10874
|
+
docId: yMap.get("docId"),
|
|
10875
|
+
remoteSpaceId: yMap.get("remoteSpaceId"),
|
|
10876
|
+
visibility: yMap.get("visibility") ?? "private",
|
|
10877
|
+
isHub: yMap.get("isHub") ?? false,
|
|
10878
|
+
order: yMap.get("order") ?? 0,
|
|
10879
|
+
lastSyncedAt: yMap.get("lastSyncedAt"),
|
|
10880
|
+
createdAt: yMap.get("createdAt") ?? 0
|
|
10881
|
+
});
|
|
10882
|
+
});
|
|
10883
|
+
return result;
|
|
10884
|
+
}
|
|
10885
|
+
addSpace(space) {
|
|
10886
|
+
const yMap = new Y.Map();
|
|
10887
|
+
this.document.transact(() => {
|
|
10888
|
+
for (const [key, value] of Object.entries(space)) if (value !== void 0) yMap.set(key, value);
|
|
10889
|
+
this.spacesArray.push([yMap]);
|
|
10890
|
+
});
|
|
10891
|
+
}
|
|
10892
|
+
removeSpace(spaceId) {
|
|
10893
|
+
const arr = this.spacesArray;
|
|
10894
|
+
for (let i = 0; i < arr.length; i++) if (arr.get(i).get("id") === spaceId) {
|
|
10895
|
+
arr.delete(i, 1);
|
|
10896
|
+
return;
|
|
10897
|
+
}
|
|
10898
|
+
}
|
|
10899
|
+
updateSpace(spaceId, updates) {
|
|
10900
|
+
const arr = this.spacesArray;
|
|
10901
|
+
for (let i = 0; i < arr.length; i++) {
|
|
10902
|
+
const yMap = arr.get(i);
|
|
10903
|
+
if (yMap.get("id") === spaceId) {
|
|
10904
|
+
this.document.transact(() => {
|
|
10905
|
+
for (const [key, value] of Object.entries(updates)) if (value !== void 0) yMap.set(key, value);
|
|
10906
|
+
});
|
|
10907
|
+
return;
|
|
10908
|
+
}
|
|
10909
|
+
}
|
|
10910
|
+
}
|
|
10911
|
+
getExternalPlugins() {
|
|
10912
|
+
let arr = this.pluginsMap.get("external");
|
|
10913
|
+
if (!arr) {
|
|
10914
|
+
arr = new Y.Array();
|
|
10915
|
+
this.pluginsMap.set("external", arr);
|
|
10916
|
+
}
|
|
10917
|
+
return arr;
|
|
10918
|
+
}
|
|
10919
|
+
getDisabledBuiltins() {
|
|
10920
|
+
let arr = this.pluginsMap.get("disabledBuiltins");
|
|
10921
|
+
if (!arr) {
|
|
10922
|
+
arr = new Y.Array();
|
|
10923
|
+
this.pluginsMap.set("disabledBuiltins", arr);
|
|
10924
|
+
}
|
|
10925
|
+
return arr;
|
|
10926
|
+
}
|
|
10927
|
+
getPreference(key) {
|
|
10928
|
+
return this.preferencesMap.get(key);
|
|
10929
|
+
}
|
|
10930
|
+
setPreference(key, value) {
|
|
10931
|
+
this.preferencesMap.set(key, value);
|
|
10932
|
+
}
|
|
10933
|
+
/**
|
|
10934
|
+
* Observe deep changes on a specific top-level map.
|
|
10935
|
+
* Returns an unsubscribe function.
|
|
10936
|
+
*/
|
|
10937
|
+
observe(mapName, callback) {
|
|
10938
|
+
const map = this.document.getMap(mapName);
|
|
10939
|
+
map.observeDeep(callback);
|
|
10940
|
+
return () => map.unobserveDeep(callback);
|
|
10941
|
+
}
|
|
10942
|
+
/**
|
|
10943
|
+
* Observe changes to the spaces array.
|
|
10944
|
+
* Returns an unsubscribe function.
|
|
10945
|
+
*/
|
|
10946
|
+
observeSpaces(callback) {
|
|
10947
|
+
const arr = this.spacesArray;
|
|
10948
|
+
arr.observe(callback);
|
|
10949
|
+
return () => arr.unobserve(callback);
|
|
10950
|
+
}
|
|
10951
|
+
/**
|
|
10952
|
+
* Returns true if the identity doc has no profile data yet (first use).
|
|
10953
|
+
* Call this to decide whether to run migration from localStorage.
|
|
10954
|
+
*/
|
|
10955
|
+
isEmpty() {
|
|
10956
|
+
return this.profileMap.size === 0 && this.serversMap.size === 0;
|
|
10957
|
+
}
|
|
10958
|
+
/**
|
|
10959
|
+
* Update the sync server URL at runtime (e.g. when user changes their
|
|
10960
|
+
* designated sync server in settings).
|
|
10961
|
+
*/
|
|
10962
|
+
setSyncServer(url) {
|
|
10963
|
+
const existing = this.providers.get("sync");
|
|
10964
|
+
if (existing) {
|
|
10965
|
+
existing.destroy();
|
|
10966
|
+
this.providers.delete("sync");
|
|
10967
|
+
}
|
|
10968
|
+
const existingWs = this.websockets.get("sync");
|
|
10969
|
+
if (existingWs) {
|
|
10970
|
+
existingWs.destroy();
|
|
10971
|
+
this.websockets.delete("sync");
|
|
10972
|
+
}
|
|
10973
|
+
if (url) {
|
|
10974
|
+
this.config = {
|
|
10975
|
+
...this.config,
|
|
10976
|
+
syncServerUrl: url
|
|
10977
|
+
};
|
|
10978
|
+
this._connectToServer("sync", url);
|
|
10979
|
+
}
|
|
10980
|
+
}
|
|
10981
|
+
getProvider(key) {
|
|
10982
|
+
return this.providers.get(key);
|
|
10983
|
+
}
|
|
10984
|
+
destroy() {
|
|
10985
|
+
if (this._destroyed) return;
|
|
10986
|
+
this._destroyed = true;
|
|
10987
|
+
for (const [, provider] of this.providers) provider.destroy();
|
|
10988
|
+
for (const [, ws] of this.websockets) ws.destroy();
|
|
10989
|
+
if (this.webrtc) this.webrtc.disconnect?.();
|
|
10990
|
+
this.providers.clear();
|
|
10991
|
+
this.websockets.clear();
|
|
10992
|
+
this.webrtc = null;
|
|
10993
|
+
this.document.destroy();
|
|
10994
|
+
}
|
|
10995
|
+
};
|
|
10996
|
+
|
|
10997
|
+
//#endregion
|
|
10998
|
+
export { AbracadabraBaseProvider, AbracadabraClient, AbracadabraProvider, AbracadabraWS, AbracadabraWebRTC, AuthMessageType, AwarenessError, BackgroundSyncManager, BackgroundSyncPersistence, BroadcastChannelSync, CHANNEL_NAMES, ConnectionTimeout, CryptoIdentityKeystore, DEFAULT_FILE_CHUNK_SIZE, DEFAULT_ICE_SERVERS, DataChannelRouter, DevicePairingChannel, DocKeyManager, DocumentCache, E2EAbracadabraProvider, E2EEChannel, E2EOfflineStore, EncryptedYMap, EncryptedYText, FileBlobStore, FileTransferChannel, FileTransferHandle, Forbidden, HocuspocusProvider, HocuspocusProviderWebsocket, IdentityDocProvider, KEY_EXCHANGE_CHANNEL, ManualSignaling, MessageTooBig, MessageType, OfflineStore, PeerConnection, ResetConnection, SearchIndex, SignalingSocket, SubdocMessage, Unauthorized, WebSocketStatus, WsReadyStates, YjsDataChannel, attachUpdatedAtObserver, awarenessStatesToArray, decryptField, deriveIdentityDocId, encryptField, makeEncryptedYMap, makeEncryptedYText, readAuthMessage, writeAuthenticated, writeAuthentication, writePermissionDenied, writeTokenSyncRequest };
|
|
10412
10999
|
//# sourceMappingURL=abracadabra-provider.esm.js.map
|