@celljs/ta-sync 4.18.0 → 4.20.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 (2) hide show
  1. package/host/index.mjs +137 -8
  2. package/package.json +4 -4
package/host/index.mjs CHANGED
@@ -2381,6 +2381,87 @@ function writeStream(stream, bytes) {
2381
2381
  });
2382
2382
  }
2383
2383
 
2384
+ // web-src/sync-account.ts
2385
+ class SyncAccount {
2386
+ options;
2387
+ identities = new Set;
2388
+ told = new Map;
2389
+ tried = new Set;
2390
+ timer;
2391
+ stopHearing;
2392
+ running;
2393
+ constructor(options) {
2394
+ this.options = options;
2395
+ }
2396
+ start() {
2397
+ this.stopHearing = this.options.account.onTold((_from, value) => void this.heard(value));
2398
+ this.timer = setInterval(() => void this.round(), this.options.every ?? 30000);
2399
+ this.round();
2400
+ }
2401
+ dispose() {
2402
+ clearInterval(this.timer);
2403
+ this.stopHearing?.();
2404
+ }
2405
+ round() {
2406
+ this.running ??= this.look().catch(() => {
2407
+ return;
2408
+ }).finally(() => {
2409
+ this.running = undefined;
2410
+ });
2411
+ return this.running;
2412
+ }
2413
+ async look() {
2414
+ const { account, sync } = this.options;
2415
+ const computers = await account.computers();
2416
+ if (computers.length === 0 || await this.options.off()) {
2417
+ return;
2418
+ }
2419
+ let status = await sync.status();
2420
+ if (status.state === "off") {
2421
+ const self = await account.self();
2422
+ const first = self && computers.filter((one) => one.online).every((one) => self.id < one.id);
2423
+ if (!first) {
2424
+ return;
2425
+ }
2426
+ status = await sync.enable();
2427
+ }
2428
+ const word = { identity: await this.options.identity() };
2429
+ if (status.state === "on" && (status.owner || status.where !== "relay")) {
2430
+ word.link = (await sync.invite()).link;
2431
+ }
2432
+ const text = JSON.stringify(word);
2433
+ for (const computer of computers) {
2434
+ if (this.told.get(computer.id) !== text) {
2435
+ await account.tell(computer.id, word);
2436
+ this.told.set(computer.id, text);
2437
+ }
2438
+ }
2439
+ if (status.owner) {
2440
+ for (const request of await this.options.requests()) {
2441
+ if (this.identities.has(request.id) && !request.guest) {
2442
+ await sync.answer(request.ask, true);
2443
+ }
2444
+ }
2445
+ }
2446
+ }
2447
+ async heard(word) {
2448
+ if (typeof word?.identity === "string") {
2449
+ this.identities.add(word.identity);
2450
+ }
2451
+ if (typeof word?.link !== "string" || this.tried.has(word.link) || await this.options.off()) {
2452
+ return;
2453
+ }
2454
+ const status = await this.options.sync.status();
2455
+ if (status.state === "off" || status.state === "gone" || status.state === "refused") {
2456
+ this.tried.add(word.link);
2457
+ await this.options.sync.join(word.link).catch(() => {
2458
+ return;
2459
+ });
2460
+ }
2461
+ this.round();
2462
+ }
2463
+ }
2464
+
2384
2465
  // web-src/sync-protocol.ts
2385
2466
  var SYNC_KIND = "ta-sync";
2386
2467
  var SYNC_PACKAGE = "@celljs/ta-sync";
@@ -2479,6 +2560,7 @@ var RELAY_HUB_MAX_MEMBERS = 101;
2479
2560
  var RELAY_HUB_MAX_VALUE = 96 * 1024;
2480
2561
  var RELAY_HUB_MAX_KEYS = 4096;
2481
2562
  var RELAY_HUB_MAX_STATE = 3 * 1024 * 1024;
2563
+ var RELAY_PICTURE_MAX_CHARS = 16 * 1024;
2482
2564
  var RELAY_HUB_IDLE_DAYS = 90;
2483
2565
 
2484
2566
  // ../ta-relay/esm/common/relay-info.js
@@ -3482,12 +3564,14 @@ class SyncIdentity {
3482
3564
  }
3483
3565
  await this.side.setIdentityShared(true);
3484
3566
  base.name = local.identity.name;
3567
+ base.picture = SyncIdentityUtil.picture(local.identity);
3485
3568
  report.sent++;
3486
3569
  } else if (theirs.id !== local.identity.id) {
3487
3570
  await this.side.importIdentity({ identity: theirs, rooms: [], friends: [] }, { shared: true });
3488
3571
  report.switched = true;
3489
3572
  report.received++;
3490
3573
  base.name = theirs.name;
3574
+ base.picture = SyncIdentityUtil.picture(theirs);
3491
3575
  base.rooms = [];
3492
3576
  base.friends = [];
3493
3577
  local = await this.side.exportIdentity();
@@ -3495,18 +3579,37 @@ class SyncIdentity {
3495
3579
  if (!local.identity.shared) {
3496
3580
  await this.side.setIdentityShared(true);
3497
3581
  }
3582
+ const sent = {};
3498
3583
  if (local.identity.name !== theirs.name) {
3499
3584
  if (base.name === theirs.name) {
3500
- const named = { ...theirs, name: local.identity.name };
3501
- if ((await this.room.setState(SyncIdentityKeys.IDENTITY, named, there?.version)).ok) {
3502
- report.sent++;
3503
- }
3585
+ sent.name = local.identity.name;
3504
3586
  } else {
3505
3587
  await this.side.setName(theirs.name);
3506
3588
  report.received++;
3507
3589
  }
3508
3590
  }
3509
- base.name = (await this.side.exportIdentity()).identity.name;
3591
+ const mine = SyncIdentityUtil.picture(local.identity);
3592
+ const shared = SyncIdentityUtil.picture(theirs);
3593
+ if (mine !== shared) {
3594
+ if ((base.picture ?? "") === shared) {
3595
+ sent.picture = mine;
3596
+ } else {
3597
+ await this.side.setPicture(shared || null);
3598
+ report.received++;
3599
+ }
3600
+ }
3601
+ if (Object.keys(sent).length > 0) {
3602
+ const next = { ...theirs, ...sent };
3603
+ if (!next.picture) {
3604
+ delete next.picture;
3605
+ }
3606
+ if ((await this.room.setState(SyncIdentityKeys.IDENTITY, next, there?.version)).ok) {
3607
+ report.sent++;
3608
+ }
3609
+ }
3610
+ const now = (await this.side.exportIdentity()).identity;
3611
+ base.name = now.name;
3612
+ base.picture = SyncIdentityUtil.picture(now);
3510
3613
  }
3511
3614
  await this.rooms(space, base, report);
3512
3615
  await this.friends(space, base, report);
@@ -3611,6 +3714,10 @@ var SyncIdentityUtil;
3611
3714
  return SyncMerge.isObject(value) && typeof friend.id === "string" && typeof friend.seenAt === "number";
3612
3715
  }
3613
3716
  SyncIdentityUtil.isFriend = isFriend;
3717
+ function picture(identity) {
3718
+ return typeof identity.picture === "string" ? identity.picture : "";
3719
+ }
3720
+ SyncIdentityUtil.picture = picture;
3614
3721
  function portable(identity) {
3615
3722
  const { shared: _shared, ...rest } = identity;
3616
3723
  return rest;
@@ -4280,12 +4387,33 @@ function activate(host) {
4280
4387
  });
4281
4388
  const stopData = host.onExtensionData((name, key, cwd) => cwd === undefined && service.dataChanged(name, key));
4282
4389
  service.start();
4390
+ const OFF = "account.off";
4391
+ const off = (value) => host.setExtensionData(SYNC_PACKAGE, OFF, value || null).catch(() => {
4392
+ return;
4393
+ });
4394
+ const account = host.account ? new SyncAccount({
4395
+ account: host.account,
4396
+ sync: service,
4397
+ identity: async () => (await host.rooms.getIdentity()).id,
4398
+ requests: () => host.rooms.listRequests(),
4399
+ off: async () => await host.getExtensionData(SYNC_PACKAGE, OFF) === true
4400
+ }) : undefined;
4401
+ account?.start();
4283
4402
  return {
4284
4403
  status: () => service.status(),
4285
- enable: () => service.enable(),
4404
+ enable: async () => {
4405
+ await off(false);
4406
+ return service.enable();
4407
+ },
4286
4408
  invite: (renew) => service.invite(renew === true),
4287
- join: (link) => service.join(String(link ?? "")),
4288
- leave: () => service.leave(),
4409
+ join: async (link) => {
4410
+ await off(false);
4411
+ return service.join(String(link ?? ""));
4412
+ },
4413
+ leave: async () => {
4414
+ await off(true);
4415
+ return service.leave();
4416
+ },
4289
4417
  answer: (ask, accept) => service.answer(String(ask ?? ""), accept === true),
4290
4418
  removeDevice: (member) => service.removeDevice(String(member ?? "")),
4291
4419
  setResources: (change) => service.setResources(change ?? {}),
@@ -4306,6 +4434,7 @@ function activate(host) {
4306
4434
  migrateRelocate: (from, to) => migrate.relocate(String(from ?? ""), String(to ?? "")),
4307
4435
  dispose: () => {
4308
4436
  stopData();
4437
+ account?.dispose();
4309
4438
  service.dispose();
4310
4439
  migrate.dispose();
4311
4440
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-sync",
3
- "version": "4.18.0",
3
+ "version": "4.20.0",
4
4
  "description": "A pi package for Ta: the user's settings, instructions, skills, extensions and the data extensions declare, the same on each of their computers, through a room of the relay sealed end to end, without an account",
5
5
  "keywords": [
6
6
  "pi-package"
@@ -26,9 +26,9 @@
26
26
  "watch": "tsc -b -w --preserveWatchOutput"
27
27
  },
28
28
  "devDependencies": {
29
- "@celljs/ta-protocol": "4.18.0",
30
- "@celljs/ta-relay": "4.18.0",
31
- "@celljs/ta-web": "4.18.0",
29
+ "@celljs/ta-protocol": "4.20.0",
30
+ "@celljs/ta-relay": "4.20.0",
31
+ "@celljs/ta-web": "4.20.0",
32
32
  "@types/react": "^19.3.0"
33
33
  },
34
34
  "publishConfig": {