@celljs/ta-share 4.27.0 → 4.28.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 +289 -13
  2. package/package.json +4 -4
package/host/index.mjs CHANGED
@@ -196,7 +196,7 @@ var RELAY_FEEDBACK_LIMITS = {
196
196
  };
197
197
  var RELAY_FEEDBACK_IMAGE = /^data:(image\/(?:jpeg|png|webp));base64,([A-Za-z0-9+/]+={0,2})$/;
198
198
  var RELAY_FEEDBACK_MODES = ["confirm", "auto", "human"];
199
- var RELAY_FEEDBACK_POLICY = { on: true, cooldownMinutes: 10, daily: 5, open: 10, newDays: 7, imageDays: 30 };
199
+ var RELAY_FEEDBACK_POLICY = { on: true, cooldownMinutes: 10, burst: 10, daily: 20, open: 20, newDays: 7, imageDays: 30 };
200
200
  var RELAY_FEEDBACK_PACE = { mode: "confirm", daily: 3, parallel: 2, tokens: 2000000, from: 9, to: 21, rush: ["security"] };
201
201
  var RelayFeedbackPolicyUtil;
202
202
  (function(RelayFeedbackPolicyUtil2) {
@@ -325,6 +325,7 @@ var RelayFeedbackPolicyUtil;
325
325
  return {
326
326
  on: typeof (given === null || given === undefined ? undefined : given.on) === "boolean" ? given.on : base.on,
327
327
  cooldownMinutes: count(given === null || given === undefined ? undefined : given.cooldownMinutes, base.cooldownMinutes, 0, 24 * 60),
328
+ burst: count(given === null || given === undefined ? undefined : given.burst, base.burst, 1, 50),
328
329
  daily: count(given === null || given === undefined ? undefined : given.daily, base.daily, 1, 100),
329
330
  open: count(given === null || given === undefined ? undefined : given.open, base.open, 1, 100),
330
331
  newDays: count(given === null || given === undefined ? undefined : given.newDays, base.newDays, 0, 90),
@@ -558,7 +559,7 @@ var RelayCloseCode;
558
559
  })(RelayCloseCode || (RelayCloseCode = {}));
559
560
  var RELAY_PING = "ping";
560
561
  var RELAY_PONG = "pong";
561
- var RELAY_PING_MS = 25000;
562
+ var RELAY_WAIT = '{"t":"wait"}';
562
563
  var RELAY_PROOF = "ta-relay ready";
563
564
  var RELAY_HUB_MAX_VALUE = 96 * 1024;
564
565
  var RELAY_HUB_MAX_STATE = 3 * 1024 * 1024;
@@ -790,14 +791,205 @@ var RelayDeflate;
790
791
  }
791
792
  })(RelayDeflate || (RelayDeflate = {}));
792
793
 
794
+ // ../ta-relay/esm/common/relay-direct.js
795
+ var RELAY_DIRECT_MARK = "\x01ta-direct ";
796
+ var RELAY_DIRECT_TIMEOUT_MS = 15000;
797
+ var RELAY_DIRECT_GATHER_MS = 3000;
798
+ var RELAY_DIRECT_PIECE = 16 * 1024;
799
+ var RelayDirectUtil;
800
+ (function(RelayDirectUtil2) {
801
+ function control(text2) {
802
+ if (!text2.startsWith(RELAY_DIRECT_MARK)) {
803
+ return;
804
+ }
805
+ try {
806
+ const message = JSON.parse(text2.slice(RELAY_DIRECT_MARK.length));
807
+ return message && typeof message === "object" && typeof message.t === "string" ? message : undefined;
808
+ } catch (_a) {
809
+ return;
810
+ }
811
+ }
812
+ RelayDirectUtil2.control = control;
813
+ function text(message) {
814
+ return RELAY_DIRECT_MARK + JSON.stringify(message);
815
+ }
816
+ RelayDirectUtil2.text = text;
817
+ function stun(list) {
818
+ const found = (Array.isArray(list) ? list : []).filter((each) => typeof each === "string" && /^stuns?:[^\s]+$/.test(each));
819
+ return [...new Set(found)].slice(0, 4);
820
+ }
821
+ RelayDirectUtil2.stun = stun;
822
+ function pieces(frame) {
823
+ const out = [];
824
+ for (let at = 0;at < frame.length || at === 0; at += RELAY_DIRECT_PIECE) {
825
+ const end = at + RELAY_DIRECT_PIECE >= frame.length;
826
+ out.push((end ? "." : "+") + frame.slice(at, at + RELAY_DIRECT_PIECE));
827
+ if (end) {
828
+ break;
829
+ }
830
+ }
831
+ return out;
832
+ }
833
+ RelayDirectUtil2.pieces = pieces;
834
+
835
+ class Joiner {
836
+ constructor(max = 32 * 1024 * 1024) {
837
+ this.max = max;
838
+ this.parts = [];
839
+ this.size = 0;
840
+ }
841
+ take(piece) {
842
+ const kind = piece[0];
843
+ if (kind !== "+" && kind !== ".") {
844
+ throw new Error("Not a piece of a frame.");
845
+ }
846
+ this.size += piece.length - 1;
847
+ if (this.size > this.max) {
848
+ throw new Error("A frame too large.");
849
+ }
850
+ this.parts.push(piece.slice(1));
851
+ if (kind === "+") {
852
+ return;
853
+ }
854
+ const frame = this.parts.join("");
855
+ this.parts = [];
856
+ this.size = 0;
857
+ return frame;
858
+ }
859
+ }
860
+ RelayDirectUtil2.Joiner = Joiner;
861
+ })(RelayDirectUtil || (RelayDirectUtil = {}));
862
+
863
+ // ../ta-relay/esm/common/relay-direct-link.js
864
+ class RelayDirectLink {
865
+ constructor(options, hooks, rtc) {
866
+ this.hooks = hooks;
867
+ this.joiner = new RelayDirectUtil.Joiner;
868
+ this.early = [];
869
+ this.closed = false;
870
+ this.sending = false;
871
+ this.taking = false;
872
+ this.pc = new rtc({ iceServers: options.stun.map((urls) => ({ urls })) });
873
+ this.channel = this.pc.createDataChannel("ta", { ordered: true });
874
+ this.channel.onopen = () => {
875
+ if (!this.closed) {
876
+ clearTimeout(this.timer);
877
+ this.hooks.moved();
878
+ }
879
+ };
880
+ this.channel.onmessage = (event) => this.piece(String(event.data));
881
+ this.channel.onclose = () => this.gone();
882
+ this.pc.onconnectionstatechange = () => {
883
+ if (this.pc.connectionState === "failed" || this.pc.connectionState === "closed") {
884
+ this.gone();
885
+ }
886
+ };
887
+ this.timer = setTimeout(() => this.close(), RELAY_DIRECT_TIMEOUT_MS);
888
+ }
889
+ static available(rtc = globalThis.RTCPeerConnection) {
890
+ return typeof rtc === "function";
891
+ }
892
+ async offer() {
893
+ var _a;
894
+ try {
895
+ await this.pc.setLocalDescription(await this.pc.createOffer());
896
+ await this.gathered();
897
+ const sdp = (_a = this.pc.localDescription) === null || _a === undefined ? undefined : _a.sdp;
898
+ if (sdp && !this.closed) {
899
+ this.hooks.control({ t: "offer", sdp });
900
+ }
901
+ } catch (_b) {
902
+ this.close();
903
+ }
904
+ }
905
+ async control(message) {
906
+ if (this.closed) {
907
+ return;
908
+ }
909
+ if (message.t === "answer") {
910
+ await this.pc.setRemoteDescription({ type: "answer", sdp: message.sdp }).catch(() => this.close());
911
+ } else if (message.t === "switch") {
912
+ this.taking = true;
913
+ for (const frame of this.early.splice(0)) {
914
+ this.hooks.frame(frame);
915
+ }
916
+ } else if (message.t === "off") {
917
+ this.close();
918
+ }
919
+ }
920
+ send(frame) {
921
+ for (const piece of RelayDirectUtil.pieces(frame)) {
922
+ this.channel.send(piece);
923
+ }
924
+ }
925
+ close() {
926
+ if (this.closed) {
927
+ return;
928
+ }
929
+ this.closed = true;
930
+ clearTimeout(this.timer);
931
+ this.channel.onclose = null;
932
+ try {
933
+ this.channel.close();
934
+ this.pc.close();
935
+ } catch (_a) {}
936
+ }
937
+ piece(piece) {
938
+ let frame;
939
+ try {
940
+ frame = this.joiner.take(piece);
941
+ } catch (_a) {
942
+ this.gone();
943
+ return;
944
+ }
945
+ if (frame === undefined || this.closed) {
946
+ return;
947
+ }
948
+ if (this.taking) {
949
+ this.hooks.frame(frame);
950
+ } else {
951
+ this.early.push(frame);
952
+ }
953
+ }
954
+ gone() {
955
+ if (this.closed) {
956
+ return;
957
+ }
958
+ const moved = this.sending || this.taking;
959
+ this.close();
960
+ if (moved) {
961
+ this.hooks.lost();
962
+ }
963
+ }
964
+ gathered() {
965
+ if (this.pc.iceGatheringState === "complete") {
966
+ return Promise.resolve();
967
+ }
968
+ return new Promise((resolve) => {
969
+ const timer = setTimeout(resolve, RELAY_DIRECT_GATHER_MS);
970
+ this.pc.onicegatheringstatechange = () => {
971
+ if (this.pc.iceGatheringState === "complete") {
972
+ clearTimeout(timer);
973
+ resolve();
974
+ }
975
+ };
976
+ });
977
+ }
978
+ }
979
+
793
980
  // ../ta-relay/esm/common/relay-pairing.js
981
+ var MAX_ROUTES = 8;
794
982
  var RelayPairingUtil;
795
983
  (function(RelayPairingUtil2) {
796
984
  function link(pairing) {
985
+ var _a;
797
986
  const fragment = new URLSearchParams({ v: "1", h: pairing.host, d: pairing.device, k: pairing.key });
798
987
  if (pairing.name) {
799
988
  fragment.set("n", pairing.name);
800
989
  }
990
+ for (const relay of (_a = pairing.relays) !== null && _a !== undefined ? _a : []) {
991
+ fragment.append("r", relay);
992
+ }
801
993
  return `${pairing.relay}${RELAY_PAIR_PATH}#${fragment}`;
802
994
  }
803
995
  RelayPairingUtil2.link = link;
@@ -812,9 +1004,28 @@ var RelayPairingUtil;
812
1004
  if (!/^https?:$/.test(url.protocol) || url.pathname !== RELAY_PAIR_PATH || fragment.get("v") !== "1" || !RELAY_ID.test(host) || !RELAY_ID.test(device) || !/^[A-Za-z0-9_-]{43}$/.test(key)) {
813
1005
  throw new Error("Not a pairing link of Ta.");
814
1006
  }
815
- return { relay: url.origin, host, device, key, name: (_a = fragment.get("n")) !== null && _a !== undefined ? _a : undefined };
1007
+ const relays = routes(fragment.getAll("r"));
1008
+ return { relay: url.origin, host, device, key, name: (_a = fragment.get("n")) !== null && _a !== undefined ? _a : undefined, ...relays.length ? { relays } : {} };
816
1009
  }
817
1010
  RelayPairingUtil2.parse = parse;
1011
+ function routes(list) {
1012
+ const found = [];
1013
+ for (const each of Array.isArray(list) ? list : []) {
1014
+ try {
1015
+ const url = new URL(String(each));
1016
+ if (/^https?:$/.test(url.protocol) && !found.includes(url.origin)) {
1017
+ found.push(url.origin);
1018
+ }
1019
+ } catch (_a) {}
1020
+ }
1021
+ return found.slice(0, MAX_ROUTES);
1022
+ }
1023
+ RelayPairingUtil2.routes = routes;
1024
+ function ways(pairing) {
1025
+ var _a;
1026
+ return routes([...(_a = pairing.relays) !== null && _a !== undefined ? _a : [], pairing.relay]);
1027
+ }
1028
+ RelayPairingUtil2.ways = ways;
818
1029
  function websocketUrl(relay, path) {
819
1030
  return relay.replace(/^http/, "ws") + path;
820
1031
  }
@@ -827,6 +1038,8 @@ var OPEN = 1;
827
1038
  var CLOSING = 2;
828
1039
  var CLOSED = 3;
829
1040
  var HELLO_MS = 15000;
1041
+ var WAIT_MS = 60000;
1042
+ var DEVICE_PING_MS = 1e4;
830
1043
 
831
1044
  class RelaySocket {
832
1045
  constructor(pairing, options = {}) {
@@ -837,6 +1050,7 @@ class RelaySocket {
837
1050
  this.onmessage = null;
838
1051
  this.onclose = null;
839
1052
  this.onerror = null;
1053
+ this.onwait = null;
840
1054
  this.state = CONNECTING;
841
1055
  this.nonce = RelayBytes.random(16);
842
1056
  this.ponged = false;
@@ -851,10 +1065,11 @@ class RelaySocket {
851
1065
  v: 1,
852
1066
  device: pairing.device,
853
1067
  nonce: RelayBytes.toBase64(this.nonce),
854
- ...RelayDeflate.supported() ? { z: 1 } : {}
1068
+ ...RelayDeflate.supported() ? { z: 1 } : {},
1069
+ ...options.wait ? { w: 1 } : {}
855
1070
  };
856
1071
  this.socket.send(JSON.stringify(hello));
857
- this.pingTimer = setInterval(() => this.ping(), (_a2 = options.pingMs) !== null && _a2 !== undefined ? _a2 : RELAY_PING_MS);
1072
+ this.pingTimer = setInterval(() => this.ping(), (_a2 = options.pingMs) !== null && _a2 !== undefined ? _a2 : DEVICE_PING_MS);
858
1073
  };
859
1074
  this.socket.onmessage = (message) => {
860
1075
  const text = String(message.data);
@@ -877,11 +1092,55 @@ class RelaySocket {
877
1092
  get closeCode() {
878
1093
  return this.code;
879
1094
  }
1095
+ get path() {
1096
+ var _a;
1097
+ return ((_a = this.link) === null || _a === undefined ? undefined : _a.sending) ? "direct" : "relay";
1098
+ }
880
1099
  send(text) {
881
1100
  if (this.state !== OPEN || !this.cipher) {
882
1101
  throw new Error("The connection is not open.");
883
1102
  }
884
- this.cipher.seal(text).then((frame) => this.socket.readyState === OPEN && this.socket.send(frame));
1103
+ this.cipher.seal(text).then((frame) => this.transmit(frame));
1104
+ }
1105
+ direct(options) {
1106
+ var _a;
1107
+ const rtc = (_a = this.options.rtc) !== null && _a !== undefined ? _a : globalThis.RTCPeerConnection;
1108
+ if (this.link || this.state !== OPEN || !RelayDirectLink.available(rtc) || !options.stun) {
1109
+ return;
1110
+ }
1111
+ this.link = new RelayDirectLink(options, {
1112
+ control: (message) => this.control(message),
1113
+ moved: () => {
1114
+ var _a2;
1115
+ (_a2 = this.cipher) === null || _a2 === undefined || _a2.seal(RelayDirectUtil.text({ t: "switch" })).then((frame) => {
1116
+ this.transmit(frame);
1117
+ if (this.link) {
1118
+ this.link.sending = true;
1119
+ }
1120
+ });
1121
+ },
1122
+ frame: (frame) => {
1123
+ this.receiving = this.receiving.then(() => this.receive(frame));
1124
+ },
1125
+ lost: () => this.drop(RelayCloseCode.TIMEOUT, "The direct connection was lost.")
1126
+ }, rtc);
1127
+ this.link.offer();
1128
+ }
1129
+ transmit(frame) {
1130
+ var _a;
1131
+ if ((_a = this.link) === null || _a === undefined ? undefined : _a.sending) {
1132
+ try {
1133
+ this.link.send(frame);
1134
+ } catch (_b) {
1135
+ this.drop(RelayCloseCode.TIMEOUT, "The direct connection was lost.");
1136
+ }
1137
+ } else if (this.socket.readyState === OPEN) {
1138
+ this.socket.send(frame);
1139
+ }
1140
+ }
1141
+ control(message) {
1142
+ var _a;
1143
+ (_a = this.cipher) === null || _a === undefined || _a.seal(RelayDirectUtil.text(message)).then((frame) => this.transmit(frame));
885
1144
  }
886
1145
  close(code = 1000, reason = "") {
887
1146
  if (this.state === CLOSED) {
@@ -891,7 +1150,8 @@ class RelaySocket {
891
1150
  this.socket.close(code, reason);
892
1151
  }
893
1152
  async receive(text) {
894
- var _a, _b;
1153
+ var _a, _b, _c, _d, _e, _f;
1154
+ var _g;
895
1155
  if (this.cipher) {
896
1156
  if (this.options.proveFirst && text.startsWith("{")) {
897
1157
  this.drop(RelaySocket.REFUSED, RelaySocket.refusal(text));
@@ -899,12 +1159,23 @@ class RelaySocket {
899
1159
  }
900
1160
  try {
901
1161
  const data = await this.cipher.open(text);
902
- (_a = this.onmessage) === null || _a === undefined || _a.call(this, { data });
903
- } catch (_c) {
1162
+ const direct = RelayDirectUtil.control(data);
1163
+ if (direct) {
1164
+ await ((_a = this.link) === null || _a === undefined ? undefined : _a.control(direct));
1165
+ return;
1166
+ }
1167
+ (_b = this.onmessage) === null || _b === undefined || _b.call(this, { data });
1168
+ } catch (_h) {
904
1169
  this.close(4400, "A message did not open.");
905
1170
  }
906
1171
  return;
907
1172
  }
1173
+ if (text === RELAY_WAIT && this.options.wait) {
1174
+ clearTimeout(this.helloTimer);
1175
+ this.helloTimer = setTimeout(() => this.drop(RelayCloseCode.HOST_OFFLINE, "The host did not come back."), (_g = this.options.waitMs) !== null && _g !== undefined ? _g : WAIT_MS);
1176
+ (_c = this.onwait) === null || _c === undefined || _c.call(this);
1177
+ return;
1178
+ }
908
1179
  try {
909
1180
  const welcome = JSON.parse(text);
910
1181
  if (welcome.t !== "ready") {
@@ -921,8 +1192,12 @@ class RelaySocket {
921
1192
  this.cipher = cipher;
922
1193
  clearTimeout(this.helloTimer);
923
1194
  this.state = OPEN;
924
- (_b = this.onopen) === null || _b === undefined || _b.call(this);
925
- } catch (_d) {
1195
+ (_d = this.onopen) === null || _d === undefined || _d.call(this);
1196
+ const direct = this.state === OPEN ? (_f = (_e = this.options).direct) === null || _f === undefined ? undefined : _f.call(_e) : undefined;
1197
+ if (direct) {
1198
+ this.direct(direct);
1199
+ }
1200
+ } catch (_j) {
926
1201
  this.drop(RelaySocket.REFUSED, "The host refused this device.");
927
1202
  }
928
1203
  }
@@ -949,15 +1224,16 @@ class RelaySocket {
949
1224
  this.finish(code, reason);
950
1225
  }
951
1226
  finish(code, reason) {
952
- var _a;
1227
+ var _a, _b;
953
1228
  if (this.state === CLOSED) {
954
1229
  return;
955
1230
  }
956
1231
  clearTimeout(this.helloTimer);
957
1232
  clearInterval(this.pingTimer);
1233
+ (_a = this.link) === null || _a === undefined || _a.close();
958
1234
  this.state = CLOSED;
959
1235
  this.code = code;
960
- (_a = this.onclose) === null || _a === undefined || _a.call(this, { code, reason });
1236
+ (_b = this.onclose) === null || _b === undefined || _b.call(this, { code, reason });
961
1237
  }
962
1238
  }
963
1239
  RelaySocket.CONNECTING = CONNECTING;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-share",
3
- "version": "4.27.0",
3
+ "version": "4.28.0",
4
4
  "description": "A pi package for Ta: share a conversation, a page or a file by a link, public, with a password or for yourself, end to end through the relay; open and import what others share",
5
5
  "keywords": [
6
6
  "pi-package"
@@ -27,9 +27,9 @@
27
27
  "watch": "tsc -b -w --preserveWatchOutput"
28
28
  },
29
29
  "devDependencies": {
30
- "@celljs/ta-protocol": "4.27.0",
31
- "@celljs/ta-relay": "4.27.0",
32
- "@celljs/ta-web": "4.27.0",
30
+ "@celljs/ta-protocol": "4.28.0",
31
+ "@celljs/ta-relay": "4.28.0",
32
+ "@celljs/ta-web": "4.28.0",
33
33
  "@types/react": "^19.3.0"
34
34
  },
35
35
  "publishConfig": {