@celljs/ta-nearby 4.16.0 → 4.17.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.
- package/extension/index.mjs +9 -0
- package/host/index.mjs +1073 -1018
- package/package.json +4 -4
- package/web/index.js +370 -362
package/host/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// web-src/nearby-service.ts
|
|
2
|
-
import { randomBytes as
|
|
2
|
+
import { randomBytes as randomBytes4 } from "node:crypto";
|
|
3
3
|
|
|
4
4
|
// ../ta-protocol/esm/common/language.js
|
|
5
5
|
var TaLanguageUtil;
|
|
@@ -234,60 +234,6 @@ var FaceUtil;
|
|
|
234
234
|
FaceUtil2.art = art;
|
|
235
235
|
})(FaceUtil || (FaceUtil = {}));
|
|
236
236
|
|
|
237
|
-
// web-src/nearby-crypto.ts
|
|
238
|
-
import { createCipheriv, createDecipheriv, createECDH, createHmac, hkdfSync, randomBytes } from "node:crypto";
|
|
239
|
-
var SLOT_MS = 15 * 60000;
|
|
240
|
-
|
|
241
|
-
class NearbyKeys {
|
|
242
|
-
createdAt;
|
|
243
|
-
e;
|
|
244
|
-
x;
|
|
245
|
-
ecdh;
|
|
246
|
-
constructor(createdAt = Date.now()) {
|
|
247
|
-
this.createdAt = createdAt;
|
|
248
|
-
this.e = randomBytes(9).toString("base64url");
|
|
249
|
-
this.ecdh = createECDH("prime256v1");
|
|
250
|
-
this.x = this.ecdh.generateKeys().toString("base64url");
|
|
251
|
-
}
|
|
252
|
-
keyWith(e, x) {
|
|
253
|
-
const secret = this.ecdh.computeSecret(Buffer.from(x, "base64url"));
|
|
254
|
-
return Buffer.from(hkdfSync("sha256", secret, [this.e, e].sort().join("."), "ta-near/1 seal", 32));
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
var NearbyCrypto;
|
|
258
|
-
((NearbyCrypto) => {
|
|
259
|
-
function slot(now = Date.now()) {
|
|
260
|
-
return Math.floor(now / SLOT_MS);
|
|
261
|
-
}
|
|
262
|
-
NearbyCrypto.slot = slot;
|
|
263
|
-
function mark(identity, slot2) {
|
|
264
|
-
return createHmac("sha256", identity).update(`ta-near/1 ${slot2}`).digest().subarray(0, 12).toString("base64url");
|
|
265
|
-
}
|
|
266
|
-
NearbyCrypto.mark = mark;
|
|
267
|
-
function marks(identity, mark2, now = Date.now()) {
|
|
268
|
-
const at = slot(now);
|
|
269
|
-
return NearbyCrypto.mark(identity, at) === mark2 || NearbyCrypto.mark(identity, at - 1) === mark2;
|
|
270
|
-
}
|
|
271
|
-
NearbyCrypto.marks = marks;
|
|
272
|
-
function seal(key, value) {
|
|
273
|
-
const iv = randomBytes(12);
|
|
274
|
-
const cipher = createCipheriv("aes-256-gcm", key, iv);
|
|
275
|
-
const body = Buffer.concat([cipher.update(JSON.stringify(value), "utf8"), cipher.final(), cipher.getAuthTag()]);
|
|
276
|
-
return { i: iv.toString("base64url"), c: body.toString("base64url") };
|
|
277
|
-
}
|
|
278
|
-
NearbyCrypto.seal = seal;
|
|
279
|
-
function open(key, sealed) {
|
|
280
|
-
const body = Buffer.from(sealed.c, "base64url");
|
|
281
|
-
if (body.length < 16) {
|
|
282
|
-
throw new Error("Too short.");
|
|
283
|
-
}
|
|
284
|
-
const decipher = createDecipheriv("aes-256-gcm", key, Buffer.from(sealed.i, "base64url"));
|
|
285
|
-
decipher.setAuthTag(body.subarray(body.length - 16));
|
|
286
|
-
return JSON.parse(Buffer.concat([decipher.update(body.subarray(0, body.length - 16)), decipher.final()]).toString("utf8"));
|
|
287
|
-
}
|
|
288
|
-
NearbyCrypto.open = open;
|
|
289
|
-
})(NearbyCrypto ||= {});
|
|
290
|
-
|
|
291
237
|
// web-src/nearby-meet.ts
|
|
292
238
|
import { createHash } from "node:crypto";
|
|
293
239
|
var NearbyMeetUtil;
|
|
@@ -753,322 +699,478 @@ class NearbyChannel {
|
|
|
753
699
|
var NAME = "@celljs/ta-nearby";
|
|
754
700
|
var NEARBY_PORT = 47232;
|
|
755
701
|
var NEARBY_VIEW = `${NAME}/nearby`;
|
|
756
|
-
var
|
|
702
|
+
var NearbyKeys;
|
|
757
703
|
((NearbyKeys) => {
|
|
758
704
|
NearbyKeys.VISIBLE = "visible";
|
|
759
705
|
NearbyKeys.STATUS = "status";
|
|
760
706
|
NearbyKeys.SETTINGS = "settings";
|
|
761
707
|
NearbyKeys.CARDS = "cards";
|
|
762
708
|
NearbyKeys.MET = "met";
|
|
763
|
-
})(
|
|
709
|
+
})(NearbyKeys ||= {});
|
|
764
710
|
var SHOW_MAX = 600 * 1024;
|
|
765
711
|
|
|
766
|
-
// web-src/nearby-
|
|
767
|
-
import {
|
|
768
|
-
|
|
769
|
-
// ../ta-relay/esm/common/relay-protocol.js
|
|
770
|
-
var RELAY_FRAME_CSP = "default-src 'none'; script-src 'unsafe-inline' 'unsafe-eval' https: data: blob:; style-src 'unsafe-inline' https: data:; img-src https: data: blob:; font-src https: data:; media-src https: data: blob:; connect-src 'none'; form-action 'none'";
|
|
771
|
-
var RELAY_FRAME_HEADER_CSP = `${RELAY_FRAME_CSP}; frame-ancestors 'self'; sandbox allow-scripts allow-popups allow-popups-to-escape-sandbox`;
|
|
772
|
-
var RELAY_SHARE_MAX_BYTES = 8 * 1024 * 1024;
|
|
773
|
-
var RELAY_SHARE_CHUNK_BYTES = 192 * 1024;
|
|
774
|
-
var RELAY_CONTENT_MAX_BYTES = 8 * 1024 * 1024;
|
|
775
|
-
var RelayCloseCode;
|
|
776
|
-
(function(RelayCloseCode2) {
|
|
777
|
-
RelayCloseCode2[RelayCloseCode2["HOST_OFFLINE"] = 4404] = "HOST_OFFLINE";
|
|
778
|
-
RelayCloseCode2[RelayCloseCode2["REFUSED"] = 4401] = "REFUSED";
|
|
779
|
-
RelayCloseCode2[RelayCloseCode2["REPLACED"] = 4409] = "REPLACED";
|
|
780
|
-
RelayCloseCode2[RelayCloseCode2["CLOSED_BY_HOST"] = 4410] = "CLOSED_BY_HOST";
|
|
781
|
-
RelayCloseCode2[RelayCloseCode2["FULL"] = 4429] = "FULL";
|
|
782
|
-
RelayCloseCode2[RelayCloseCode2["TIMEOUT"] = 4408] = "TIMEOUT";
|
|
783
|
-
})(RelayCloseCode || (RelayCloseCode = {}));
|
|
784
|
-
var RELAY_PING = "ping";
|
|
785
|
-
var RELAY_HUB_MAX_VALUE = 96 * 1024;
|
|
786
|
-
var RELAY_HUB_MAX_STATE = 3 * 1024 * 1024;
|
|
712
|
+
// web-src/nearby-crypto.ts
|
|
713
|
+
import { createCipheriv, createDecipheriv, createECDH, createHmac, hkdfSync, randomBytes } from "node:crypto";
|
|
714
|
+
var SLOT_MS = 15 * 60000;
|
|
787
715
|
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
RelaySpotCloseCode2[RelaySpotCloseCode2["FULL"] = 4429] = "FULL";
|
|
799
|
-
RelaySpotCloseCode2[RelaySpotCloseCode2["TRIES"] = 4428] = "TRIES";
|
|
800
|
-
})(RelaySpotCloseCode || (RelaySpotCloseCode = {}));
|
|
801
|
-
var RelaySpotTries;
|
|
802
|
-
(function(RelaySpotTries2) {
|
|
803
|
-
function keyOf(address) {
|
|
804
|
-
const ip = (address !== null && address !== undefined ? address : "").trim().replace(/^::ffff:/i, "");
|
|
805
|
-
if (!ip.includes(":")) {
|
|
806
|
-
return ip || "unknown";
|
|
807
|
-
}
|
|
808
|
-
const [head, tail = ""] = ip.toLowerCase().split("::");
|
|
809
|
-
const groups = head ? head.split(":") : [];
|
|
810
|
-
const rest = tail ? tail.split(":") : [];
|
|
811
|
-
const full = [...groups, ...Array(Math.max(0, 8 - groups.length - rest.length)).fill("0"), ...rest];
|
|
812
|
-
return `${full.slice(0, 4).map((group) => group.replace(/^0+(?=.)/, "")).join(":")}::/64`;
|
|
813
|
-
}
|
|
814
|
-
RelaySpotTries2.keyOf = keyOf;
|
|
815
|
-
function recent(fails, now) {
|
|
816
|
-
return fails.filter((at) => now - at < RELAY_SPOT_TRIES_MS);
|
|
817
|
-
}
|
|
818
|
-
RelaySpotTries2.recent = recent;
|
|
819
|
-
function blocked(fails, now) {
|
|
820
|
-
return recent(fails, now).length >= RELAY_SPOT_TRIES;
|
|
716
|
+
class NearbyKeys2 {
|
|
717
|
+
createdAt;
|
|
718
|
+
e;
|
|
719
|
+
x;
|
|
720
|
+
ecdh;
|
|
721
|
+
constructor(createdAt = Date.now()) {
|
|
722
|
+
this.createdAt = createdAt;
|
|
723
|
+
this.e = randomBytes(9).toString("base64url");
|
|
724
|
+
this.ecdh = createECDH("prime256v1");
|
|
725
|
+
this.x = this.ecdh.generateKeys().toString("base64url");
|
|
821
726
|
}
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
return kept.length >= RELAY_SPOT_TRIES ? kept[kept.length - RELAY_SPOT_TRIES] + RELAY_SPOT_TRIES_MS : now;
|
|
727
|
+
keyWith(e, x) {
|
|
728
|
+
const secret = this.ecdh.computeSecret(Buffer.from(x, "base64url"));
|
|
729
|
+
return Buffer.from(hkdfSync("sha256", secret, [this.e, e].sort().join("."), "ta-near/1 seal", 32));
|
|
826
730
|
}
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
var PING_MS = 30000;
|
|
833
|
-
var NearbySpotUtil;
|
|
834
|
-
((NearbySpotUtil) => {
|
|
835
|
-
function code() {
|
|
836
|
-
return String(randomInt(0, 1e6)).padStart(6, "0");
|
|
731
|
+
}
|
|
732
|
+
var NearbyCrypto;
|
|
733
|
+
((NearbyCrypto) => {
|
|
734
|
+
function slot(now = Date.now()) {
|
|
735
|
+
return Math.floor(now / SLOT_MS);
|
|
837
736
|
}
|
|
838
|
-
|
|
839
|
-
function
|
|
840
|
-
return
|
|
737
|
+
NearbyCrypto.slot = slot;
|
|
738
|
+
function mark(identity, slot2) {
|
|
739
|
+
return createHmac("sha256", identity).update(`ta-near/1 ${slot2}`).digest().subarray(0, 12).toString("base64url");
|
|
841
740
|
}
|
|
842
|
-
|
|
843
|
-
function
|
|
844
|
-
|
|
741
|
+
NearbyCrypto.mark = mark;
|
|
742
|
+
function marks(identity, mark2, now = Date.now()) {
|
|
743
|
+
const at = slot(now);
|
|
744
|
+
return NearbyCrypto.mark(identity, at) === mark2 || NearbyCrypto.mark(identity, at - 1) === mark2;
|
|
845
745
|
}
|
|
846
|
-
|
|
847
|
-
function
|
|
848
|
-
|
|
746
|
+
NearbyCrypto.marks = marks;
|
|
747
|
+
function seal(key, value) {
|
|
748
|
+
const iv = randomBytes(12);
|
|
749
|
+
const cipher = createCipheriv("aes-256-gcm", key, iv);
|
|
750
|
+
const body = Buffer.concat([cipher.update(JSON.stringify(value), "utf8"), cipher.final(), cipher.getAuthTag()]);
|
|
751
|
+
return { i: iv.toString("base64url"), c: body.toString("base64url") };
|
|
849
752
|
}
|
|
850
|
-
|
|
851
|
-
function
|
|
852
|
-
|
|
753
|
+
NearbyCrypto.seal = seal;
|
|
754
|
+
function open(key, sealed) {
|
|
755
|
+
const body = Buffer.from(sealed.c, "base64url");
|
|
756
|
+
if (body.length < 16) {
|
|
757
|
+
throw new Error("Too short.");
|
|
758
|
+
}
|
|
759
|
+
const decipher = createDecipheriv("aes-256-gcm", key, Buffer.from(sealed.i, "base64url"));
|
|
760
|
+
decipher.setAuthTag(body.subarray(body.length - 16));
|
|
761
|
+
return JSON.parse(Buffer.concat([decipher.update(body.subarray(0, body.length - 16)), decipher.final()]).toString("utf8"));
|
|
853
762
|
}
|
|
854
|
-
|
|
855
|
-
})(
|
|
763
|
+
NearbyCrypto.open = open;
|
|
764
|
+
})(NearbyCrypto ||= {});
|
|
856
765
|
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
766
|
+
// web-src/nearby-service-base.ts
|
|
767
|
+
var GONE_MS = 1e4;
|
|
768
|
+
var INVITE_MS = 10 * 60000;
|
|
769
|
+
var GATHER_MS = 5 * 60000;
|
|
770
|
+
var PROPOSE_GAP_MS = 2 * 60 * 60000;
|
|
771
|
+
var SKEW_MS = 5 * 60000;
|
|
772
|
+
var TICK_MS = 2000;
|
|
773
|
+
var IDENTITY_MS = 1e4;
|
|
774
|
+
var INITIATIVE = "@celljs/ta-initiative";
|
|
775
|
+
var NOTIFY = "@celljs/ta-notify";
|
|
776
|
+
var ROOM_KIND = "nearby.room";
|
|
777
|
+
var MEET_KIND = "nearby.meet";
|
|
778
|
+
var GREET_KIND = "nearby.greet";
|
|
779
|
+
var MEET_A_DAY = 3;
|
|
780
|
+
var MET_MS = 30 * 24 * 60 * 60000;
|
|
781
|
+
var ROOMS_KIND = "ta-rooms";
|
|
782
|
+
|
|
783
|
+
class NearbyServiceBase {
|
|
784
|
+
host;
|
|
785
|
+
options;
|
|
786
|
+
visible;
|
|
787
|
+
settings = {};
|
|
788
|
+
keys;
|
|
789
|
+
previous;
|
|
790
|
+
beacon;
|
|
791
|
+
channel;
|
|
792
|
+
heard = new Map;
|
|
793
|
+
sent = new Map;
|
|
794
|
+
received = new Map;
|
|
795
|
+
identity;
|
|
796
|
+
identityAt = 0;
|
|
797
|
+
friends = [];
|
|
798
|
+
friendsAt = 0;
|
|
799
|
+
error;
|
|
800
|
+
timer;
|
|
801
|
+
published = "";
|
|
802
|
+
proposedAt = 0;
|
|
803
|
+
madeAt = 0;
|
|
804
|
+
defined = false;
|
|
805
|
+
stopRooms;
|
|
806
|
+
spot;
|
|
807
|
+
shown;
|
|
808
|
+
cards = {};
|
|
809
|
+
met = {};
|
|
810
|
+
greets = new Map;
|
|
811
|
+
hellos = new Map;
|
|
812
|
+
greeted = new Map;
|
|
813
|
+
kinds = new Set;
|
|
814
|
+
spotTimer;
|
|
815
|
+
now;
|
|
816
|
+
constructor(host, options = {}) {
|
|
817
|
+
this.host = host;
|
|
818
|
+
this.options = options;
|
|
819
|
+
this.now = options.now ?? Date.now;
|
|
869
820
|
}
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
this.
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
message = JSON.parse(event.data);
|
|
883
|
-
} catch {
|
|
884
|
-
return;
|
|
885
|
-
}
|
|
886
|
-
if (message.t === "ready") {
|
|
887
|
-
ready = true;
|
|
888
|
-
this.opened = message.opened;
|
|
889
|
-
this.ping = setInterval(() => this.send(RELAY_PING), PING_MS);
|
|
890
|
-
resolve(message.opened);
|
|
891
|
-
} else if (message.t === "here") {
|
|
892
|
-
this.handlers.here(message.b);
|
|
893
|
-
} else if (message.t === "gone") {
|
|
894
|
-
this.handlers.gone(message.e);
|
|
895
|
-
} else if (message.t === "miss") {
|
|
896
|
-
this.settle(message.id, undefined, new Error("No longer there."));
|
|
897
|
-
} else if (message.t === "frame") {
|
|
898
|
-
if (message.re) {
|
|
899
|
-
this.settle(message.id, message.f);
|
|
900
|
-
} else {
|
|
901
|
-
this.handlers.frame(message.from, message.f).then((answer) => {
|
|
902
|
-
if (answer) {
|
|
903
|
-
this.send(JSON.stringify({ t: "to", to: message.from, id: message.id, f: answer, re: true }));
|
|
904
|
-
}
|
|
905
|
-
});
|
|
906
|
-
}
|
|
907
|
-
}
|
|
908
|
-
};
|
|
909
|
-
socket.onclose = (event) => {
|
|
910
|
-
clearInterval(this.ping);
|
|
911
|
-
for (const id of [...this.pending.keys()]) {
|
|
912
|
-
this.settle(id, undefined, new Error("The spot closed."));
|
|
913
|
-
}
|
|
914
|
-
if (!ready) {
|
|
915
|
-
reject(new NearbySpotError(event.code));
|
|
916
|
-
} else if (this.socket === socket) {
|
|
917
|
-
this.socket = undefined;
|
|
918
|
-
this.handlers.closed(this);
|
|
919
|
-
}
|
|
920
|
-
};
|
|
921
|
-
socket.onerror = () => {
|
|
922
|
-
return;
|
|
923
|
-
};
|
|
924
|
-
});
|
|
925
|
-
}
|
|
926
|
-
here(beacon) {
|
|
927
|
-
this.send(JSON.stringify({ t: "here", b: beacon }));
|
|
928
|
-
}
|
|
929
|
-
request(to, frame) {
|
|
930
|
-
const id = randomBytes2(9).toString("base64url");
|
|
931
|
-
return new Promise((resolve, reject) => {
|
|
932
|
-
const timer = setTimeout(() => this.settle(id, undefined, new Error("No answer.")), ANSWER_MS);
|
|
933
|
-
this.pending.set(id, { resolve, reject, timer });
|
|
934
|
-
this.send(JSON.stringify({ t: "to", to, id, f: frame }));
|
|
935
|
-
});
|
|
821
|
+
async setVisible(on, minutes) {
|
|
822
|
+
if (!on) {
|
|
823
|
+
this.close();
|
|
824
|
+
await this.host.setExtensionData(NAME, NearbyKeys.VISIBLE, null);
|
|
825
|
+
return this.publish(true);
|
|
826
|
+
}
|
|
827
|
+
const now = this.now();
|
|
828
|
+
const visible = { since: now, ...typeof minutes === "number" && minutes > 0 ? { until: now + Math.round(minutes) * 60000 } : {} };
|
|
829
|
+
await this.host.setExtensionData(NAME, NearbyKeys.VISIBLE, visible);
|
|
830
|
+
this.close();
|
|
831
|
+
await this.open(visible);
|
|
832
|
+
return this.status();
|
|
936
833
|
}
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
this.
|
|
834
|
+
async open(visible) {
|
|
835
|
+
this.visible = visible;
|
|
836
|
+
this.error = undefined;
|
|
837
|
+
await this.loadIdentity();
|
|
838
|
+
this.keys = new NearbyKeys2(this.now());
|
|
839
|
+
const channel = new NearbyChannel((frame) => this.receive(frame));
|
|
840
|
+
const port = Number(process.env.TA_NEARBY_PORT) || this.options.port || NEARBY_PORT;
|
|
841
|
+
const beacon = new NearbyBeacon({ port, targets: this.options.targets, intervalMs: this.options.intervalMs }, (data, address) => this.hear(data, address));
|
|
941
842
|
try {
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
843
|
+
await channel.start();
|
|
844
|
+
await beacon.start(() => this.beaconData(channel.port));
|
|
845
|
+
this.channel = channel;
|
|
846
|
+
this.beacon = beacon;
|
|
847
|
+
} catch (error) {
|
|
848
|
+
channel.stop();
|
|
849
|
+
beacon.stop();
|
|
850
|
+
this.error = error instanceof Error ? error.message : String(error);
|
|
948
851
|
}
|
|
852
|
+
clearInterval(this.timer);
|
|
853
|
+
this.timer = setInterval(() => void this.tick(), TICK_MS);
|
|
854
|
+
this.timer.unref?.();
|
|
855
|
+
await this.publish(true);
|
|
949
856
|
}
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
857
|
+
close() {
|
|
858
|
+
this.leaveSpot();
|
|
859
|
+
clearInterval(this.timer);
|
|
860
|
+
this.timer = undefined;
|
|
861
|
+
this.beacon?.stop();
|
|
862
|
+
this.channel?.stop();
|
|
863
|
+
this.beacon = undefined;
|
|
864
|
+
this.channel = undefined;
|
|
865
|
+
this.visible = undefined;
|
|
866
|
+
this.keys = undefined;
|
|
867
|
+
this.previous = undefined;
|
|
868
|
+
this.heard.clear();
|
|
961
869
|
}
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
870
|
+
beaconData(port) {
|
|
871
|
+
const keys = this.keys;
|
|
872
|
+
return {
|
|
873
|
+
t: "ta-near",
|
|
874
|
+
v: 1,
|
|
875
|
+
e: keys.e,
|
|
876
|
+
n: this.selfName(),
|
|
877
|
+
f: this.identity ? FaceUtil.of(this.identity.id) : 0,
|
|
878
|
+
...this.identity?.look ? { l: this.identity.look } : {},
|
|
879
|
+
p: port,
|
|
880
|
+
a: NearbyNetUtil.addresses(),
|
|
881
|
+
x: keys.x,
|
|
882
|
+
h: NearbyCrypto.mark(this.identity?.id ?? "", NearbyCrypto.slot(this.now())),
|
|
883
|
+
...this.offers().length ? { o: this.offers() } : {},
|
|
884
|
+
...this.meets() ? { m: 1 } : {}
|
|
885
|
+
};
|
|
969
886
|
}
|
|
970
|
-
|
|
971
|
-
[
|
|
972
|
-
[RelaySpotCloseCode.LATE]: "late",
|
|
973
|
-
[RelaySpotCloseCode.FULL]: "full",
|
|
974
|
-
[RelaySpotCloseCode.TRIES]: "tries"
|
|
975
|
-
};
|
|
976
|
-
}
|
|
977
|
-
|
|
978
|
-
// web-src/server-texts.ts
|
|
979
|
-
var NearbyTexts;
|
|
980
|
-
((NearbyTexts) => {
|
|
981
|
-
function language(env = process.env) {
|
|
982
|
-
return TaLanguageUtil.of(env.TA_LANGUAGE || env.LC_ALL || env.LC_MESSAGES || env.LANG || "");
|
|
887
|
+
offers() {
|
|
888
|
+
return [...this.settings.show !== "off" ? ["show"] : [], ...this.settings.work ? ["work"] : []];
|
|
983
889
|
}
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
if (peer.friend) {
|
|
987
|
-
return peer.friend.name;
|
|
988
|
-
}
|
|
989
|
-
return peer.name || TaLanguageUtil.say({
|
|
990
|
-
"zh-CN": "一位 Ta 用户",
|
|
991
|
-
"zh-TW": "一位 Ta 使用者",
|
|
992
|
-
"en-US": "a user of Ta",
|
|
993
|
-
"ja-JP": "Ta のユーザー"
|
|
994
|
-
});
|
|
890
|
+
meets() {
|
|
891
|
+
return this.settings.meet === true && !!this.cards.card;
|
|
995
892
|
}
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
return TaLanguageUtil.say({
|
|
999
|
-
"zh-CN": names.join("、"),
|
|
1000
|
-
"zh-TW": names.join("、"),
|
|
1001
|
-
"en-US": names.join(", "),
|
|
1002
|
-
"ja-JP": names.join("、")
|
|
1003
|
-
});
|
|
893
|
+
selfName() {
|
|
894
|
+
return this.settings.hideName ? "" : (this.identity?.name ?? "").slice(0, 40);
|
|
1004
895
|
}
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
"ja-JP": "近くにいる"
|
|
1012
|
-
});
|
|
896
|
+
async loadIdentity() {
|
|
897
|
+
this.identityAt = this.now();
|
|
898
|
+
try {
|
|
899
|
+
const identity = await this.host.rooms.getIdentity();
|
|
900
|
+
this.identity = { id: identity.id, name: identity.name, ...identity.look ? { look: identity.look } : {} };
|
|
901
|
+
} catch {}
|
|
1013
902
|
}
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
return
|
|
903
|
+
async tick() {
|
|
904
|
+
const now = this.now();
|
|
905
|
+
if (this.visible?.until !== undefined && this.visible.until <= now) {
|
|
906
|
+
this.close();
|
|
907
|
+
await this.host.setExtensionData(NAME, NearbyKeys.VISIBLE, null).catch(() => {
|
|
908
|
+
return;
|
|
909
|
+
});
|
|
910
|
+
await this.publish(true);
|
|
911
|
+
return;
|
|
1023
912
|
}
|
|
1024
|
-
if (
|
|
1025
|
-
|
|
913
|
+
if (this.keys && now - this.identityAt >= IDENTITY_MS) {
|
|
914
|
+
await this.loadIdentity();
|
|
1026
915
|
}
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
function invited(name, title) {
|
|
1031
|
-
return TaLanguageUtil.say({
|
|
1032
|
-
"zh-CN": `${name}(在你附近)邀请你进房间「${title}」`,
|
|
1033
|
-
"zh-TW": `${name}(在你附近)邀請你進房間「${title}」`,
|
|
1034
|
-
"en-US": `${name}, nearby, invites you into the room ${title}`,
|
|
1035
|
-
"ja-JP": `${name}(近くにいる)があなたをルーム「${title}」に招待しています`
|
|
1036
|
-
});
|
|
1037
|
-
}
|
|
1038
|
-
NearbyTexts.invited = invited;
|
|
1039
|
-
function proposeTitle(count) {
|
|
1040
|
-
return TaLanguageUtil.say({
|
|
1041
|
-
"zh-CN": `和附近的 ${count} 个 Ta 建个房间,放纪要和文件`,
|
|
1042
|
-
"zh-TW": `和附近的 ${count} 個 Ta 建個房間,放紀要和檔案`,
|
|
1043
|
-
"en-US": `Make a room with the ${count} Ta nearby, for notes and files`,
|
|
1044
|
-
"ja-JP": `近くにいる ${count} 人の Ta とルームを作成して、ノートとファイルを保存`
|
|
1045
|
-
});
|
|
1046
|
-
}
|
|
1047
|
-
NearbyTexts.proposeTitle = proposeTitle;
|
|
1048
|
-
function proposeWhy(names) {
|
|
1049
|
-
return TaLanguageUtil.say({
|
|
1050
|
-
"zh-CN": `${list(names)} 在你附近已经 5 分钟以上,像是在一起开会或做事。`,
|
|
1051
|
-
"zh-TW": `${list(names)} 在你附近已經 5 分鐘以上,像是在一起開會或做事。`,
|
|
1052
|
-
"en-US": `${list(names)} have been near you for over 5 minutes, as if meeting or working together.`,
|
|
1053
|
-
"ja-JP": `${list(names)} が 5 分以上近くにいて、一緒に会議をしたり、何かをしているようです。`
|
|
1054
|
-
});
|
|
1055
|
-
}
|
|
1056
|
-
NearbyTexts.proposeWhy = proposeWhy;
|
|
1057
|
-
function confirmRoom(title, names) {
|
|
1058
|
-
const lang = language();
|
|
1059
|
-
if (lang === "zh-CN") {
|
|
1060
|
-
return [`和附近的人建房间「${title}」?`, `会邀请 ${list(names)}。他们在自己的 Ta 里点"加入"后就进来,不需要你再同意。`];
|
|
916
|
+
if (this.keys && now - this.keys.createdAt >= SLOT_MS) {
|
|
917
|
+
this.previous = this.keys;
|
|
918
|
+
this.keys = new NearbyKeys2(now);
|
|
1061
919
|
}
|
|
1062
|
-
if (
|
|
1063
|
-
|
|
920
|
+
if (this.previous && this.keys && now - this.keys.createdAt > 60000) {
|
|
921
|
+
this.previous = undefined;
|
|
1064
922
|
}
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
];
|
|
923
|
+
for (const [e, heard] of this.heard) {
|
|
924
|
+
if (now - heard.seen > GONE_MS) {
|
|
925
|
+
this.heard.delete(e);
|
|
926
|
+
}
|
|
1070
927
|
}
|
|
1071
|
-
|
|
928
|
+
for (const [k, invite] of this.received) {
|
|
929
|
+
if (now - invite.at >= INVITE_MS) {
|
|
930
|
+
this.received.delete(k);
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
for (const [k, greet] of this.greets) {
|
|
934
|
+
if (now - greet.at >= INVITE_MS) {
|
|
935
|
+
this.greets.delete(k);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
for (const [k, hello] of this.hellos) {
|
|
939
|
+
if (now - hello.at >= 24 * 60 * 60000) {
|
|
940
|
+
this.hellos.delete(k);
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
for (const [k, at] of this.greeted) {
|
|
944
|
+
if (now - at >= INVITE_MS) {
|
|
945
|
+
this.greeted.delete(k);
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
for (const [k, sent] of this.sent) {
|
|
949
|
+
if (now - sent.at >= 24 * 60 * 60000) {
|
|
950
|
+
this.sent.delete(k);
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
for (const heard of this.heard.values()) {
|
|
954
|
+
this.exchangeCard(heard);
|
|
955
|
+
}
|
|
956
|
+
await this.publish();
|
|
957
|
+
await this.propose().catch(() => {
|
|
958
|
+
return;
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
hear(data, address, via) {
|
|
962
|
+
if (!this.keys || data.e === this.keys.e || data.e === this.previous?.e) {
|
|
963
|
+
return;
|
|
964
|
+
}
|
|
965
|
+
const before = this.heard.get(data.e);
|
|
966
|
+
if (before && via && !before.via) {
|
|
967
|
+
before.seen = this.now();
|
|
968
|
+
return;
|
|
969
|
+
}
|
|
970
|
+
const now = this.now();
|
|
971
|
+
if (this.identity && NearbyCrypto.marks(this.identity.id, data.h, now)) {
|
|
972
|
+
return;
|
|
973
|
+
}
|
|
974
|
+
const known = this.heard.get(data.e);
|
|
975
|
+
if (known && known.data.x !== data.x) {
|
|
976
|
+
return;
|
|
977
|
+
}
|
|
978
|
+
const heard = known ? { ...known, data, address, seen: now, via } : { data, address, since: now, seen: now, ...via ? { via } : {} };
|
|
979
|
+
if (!via) {
|
|
980
|
+
delete heard.via;
|
|
981
|
+
}
|
|
982
|
+
if (!known) {
|
|
983
|
+
const friend = this.recognize(data.h, now);
|
|
984
|
+
if (friend) {
|
|
985
|
+
heard.friend = friend;
|
|
986
|
+
for (const [e, other] of this.heard) {
|
|
987
|
+
if (other.friend?.id === friend.id) {
|
|
988
|
+
heard.since = Math.min(heard.since, other.since);
|
|
989
|
+
this.heard.delete(e);
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
this.heard.set(data.e, heard);
|
|
995
|
+
if (!known) {
|
|
996
|
+
this.exchangeCard(heard);
|
|
997
|
+
if (via) {
|
|
998
|
+
this.announceSpot(via);
|
|
999
|
+
} else {
|
|
1000
|
+
this.beacon?.sendNow();
|
|
1001
|
+
}
|
|
1002
|
+
this.refreshFriends();
|
|
1003
|
+
this.publish();
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
recognize(mark, now) {
|
|
1007
|
+
const friend = this.friends.find((one) => NearbyCrypto.marks(one.id, mark, now));
|
|
1008
|
+
return friend ? { id: friend.id, name: friend.name } : undefined;
|
|
1009
|
+
}
|
|
1010
|
+
async refreshFriends() {
|
|
1011
|
+
if (this.now() - this.friendsAt < 60000) {
|
|
1012
|
+
return;
|
|
1013
|
+
}
|
|
1014
|
+
this.friendsAt = this.now();
|
|
1015
|
+
this.friends = await this.host.rooms.listFriends().catch(() => this.friends);
|
|
1016
|
+
let changed = false;
|
|
1017
|
+
for (const heard of this.heard.values()) {
|
|
1018
|
+
if (!heard.friend) {
|
|
1019
|
+
heard.friend = this.recognize(heard.data.h, this.now());
|
|
1020
|
+
changed ||= !!heard.friend;
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
if (changed) {
|
|
1024
|
+
await this.publish();
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
async send(e, message) {
|
|
1028
|
+
const heard = this.heard.get(e);
|
|
1029
|
+
const keys = this.keys;
|
|
1030
|
+
if (!heard || !keys) {
|
|
1031
|
+
throw new Error("No longer nearby.");
|
|
1032
|
+
}
|
|
1033
|
+
const key = keys.keyWith(heard.data.e, heard.data.x);
|
|
1034
|
+
const frame = { v: 1, e: keys.e, x: keys.x, to: heard.data.e, ...NearbyCrypto.seal(key, message) };
|
|
1035
|
+
const answer = heard.via ? NearbyServiceBase.frame(await heard.via.request(heard.data.e, frame)) : await NearbyNetUtil.send([...new Set([heard.address, ...heard.data.a])], heard.data.p, frame);
|
|
1036
|
+
if (!answer) {
|
|
1037
|
+
throw new Error("No answer.");
|
|
1038
|
+
}
|
|
1039
|
+
return NearbyCrypto.open(key, answer);
|
|
1040
|
+
}
|
|
1041
|
+
async receive(frame) {
|
|
1042
|
+
const keys = frame.to === this.keys?.e ? this.keys : frame.to === this.previous?.e ? this.previous : undefined;
|
|
1043
|
+
const heard = this.heard.get(frame.e);
|
|
1044
|
+
if (!keys || !heard || heard.data.x !== frame.x) {
|
|
1045
|
+
return;
|
|
1046
|
+
}
|
|
1047
|
+
const key = keys.keyWith(frame.e, frame.x);
|
|
1048
|
+
const message = NearbyCrypto.open(key, frame);
|
|
1049
|
+
if (!message || typeof message !== "object" || typeof message.at !== "number" || Math.abs(this.now() - message.at) > SKEW_MS) {
|
|
1050
|
+
return;
|
|
1051
|
+
}
|
|
1052
|
+
const answer = await this.handle(message, heard);
|
|
1053
|
+
return { v: 1, e: keys.e, x: keys.x, to: frame.e, ...NearbyCrypto.seal(key, answer) };
|
|
1054
|
+
}
|
|
1055
|
+
static frame(value) {
|
|
1056
|
+
const frame = value;
|
|
1057
|
+
return frame && frame.v === 1 && [frame.e, frame.x, frame.to, frame.i, frame.c].every((part) => typeof part === "string" && part.length > 0 && part.length < 2000000) ? frame : undefined;
|
|
1058
|
+
}
|
|
1059
|
+
async read(key) {
|
|
1060
|
+
return this.host.getExtensionData(NAME, key).catch(() => {
|
|
1061
|
+
return;
|
|
1062
|
+
});
|
|
1063
|
+
}
|
|
1064
|
+
async publish(force = false) {
|
|
1065
|
+
const status = this.status();
|
|
1066
|
+
const text = JSON.stringify(status);
|
|
1067
|
+
if (force || text !== this.published) {
|
|
1068
|
+
this.published = text;
|
|
1069
|
+
await this.host.setExtensionData(NAME, NearbyKeys.STATUS, status).catch(() => {
|
|
1070
|
+
return;
|
|
1071
|
+
});
|
|
1072
|
+
}
|
|
1073
|
+
return status;
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
// web-src/nearby-service-meet.ts
|
|
1078
|
+
import { randomBytes as randomBytes2 } from "node:crypto";
|
|
1079
|
+
|
|
1080
|
+
// web-src/server-texts.ts
|
|
1081
|
+
var NearbyTexts;
|
|
1082
|
+
((NearbyTexts) => {
|
|
1083
|
+
function language(env = process.env) {
|
|
1084
|
+
return TaLanguageUtil.of(env.TA_LANGUAGE || env.LC_ALL || env.LC_MESSAGES || env.LANG || "");
|
|
1085
|
+
}
|
|
1086
|
+
NearbyTexts.language = language;
|
|
1087
|
+
function who(peer) {
|
|
1088
|
+
if (peer.friend) {
|
|
1089
|
+
return peer.friend.name;
|
|
1090
|
+
}
|
|
1091
|
+
return peer.name || TaLanguageUtil.say({
|
|
1092
|
+
"zh-CN": "一位 Ta 用户",
|
|
1093
|
+
"zh-TW": "一位 Ta 使用者",
|
|
1094
|
+
"en-US": "a user of Ta",
|
|
1095
|
+
"ja-JP": "Ta のユーザー"
|
|
1096
|
+
});
|
|
1097
|
+
}
|
|
1098
|
+
NearbyTexts.who = who;
|
|
1099
|
+
function list(names) {
|
|
1100
|
+
return TaLanguageUtil.say({
|
|
1101
|
+
"zh-CN": names.join("、"),
|
|
1102
|
+
"zh-TW": names.join("、"),
|
|
1103
|
+
"en-US": names.join(", "),
|
|
1104
|
+
"ja-JP": names.join("、")
|
|
1105
|
+
});
|
|
1106
|
+
}
|
|
1107
|
+
NearbyTexts.list = list;
|
|
1108
|
+
function nearby() {
|
|
1109
|
+
return TaLanguageUtil.say({
|
|
1110
|
+
"zh-CN": "附近",
|
|
1111
|
+
"zh-TW": "附近",
|
|
1112
|
+
"en-US": "Nearby",
|
|
1113
|
+
"ja-JP": "近くにいる"
|
|
1114
|
+
});
|
|
1115
|
+
}
|
|
1116
|
+
NearbyTexts.nearby = nearby;
|
|
1117
|
+
function roomTitle(now = new Date) {
|
|
1118
|
+
const time = `${String(now.getHours()).padStart(2, "0")}:${String(now.getMinutes()).padStart(2, "0")}`;
|
|
1119
|
+
const lang = language();
|
|
1120
|
+
if (lang === "zh-CN") {
|
|
1121
|
+
return `附近 · ${now.getMonth() + 1}月${now.getDate()}日 ${time}`;
|
|
1122
|
+
}
|
|
1123
|
+
if (lang === "zh-TW") {
|
|
1124
|
+
return `附近 · ${now.getMonth() + 1}月${now.getDate()}日 ${time}`;
|
|
1125
|
+
}
|
|
1126
|
+
if (lang === "ja-JP") {
|
|
1127
|
+
return `近くにいる · ${now.toLocaleDateString("ja", { month: "short", day: "numeric" })} ${time}`;
|
|
1128
|
+
}
|
|
1129
|
+
return `Nearby · ${now.toLocaleDateString("en", { month: "short", day: "numeric" })} ${time}`;
|
|
1130
|
+
}
|
|
1131
|
+
NearbyTexts.roomTitle = roomTitle;
|
|
1132
|
+
function invited(name, title) {
|
|
1133
|
+
return TaLanguageUtil.say({
|
|
1134
|
+
"zh-CN": `${name}(在你附近)邀请你进房间「${title}」`,
|
|
1135
|
+
"zh-TW": `${name}(在你附近)邀請你進房間「${title}」`,
|
|
1136
|
+
"en-US": `${name}, nearby, invites you into the room ${title}`,
|
|
1137
|
+
"ja-JP": `${name}(近くにいる)があなたをルーム「${title}」に招待しています`
|
|
1138
|
+
});
|
|
1139
|
+
}
|
|
1140
|
+
NearbyTexts.invited = invited;
|
|
1141
|
+
function proposeTitle(count) {
|
|
1142
|
+
return TaLanguageUtil.say({
|
|
1143
|
+
"zh-CN": `和附近的 ${count} 个 Ta 建个房间,放纪要和文件`,
|
|
1144
|
+
"zh-TW": `和附近的 ${count} 個 Ta 建個房間,放紀要和檔案`,
|
|
1145
|
+
"en-US": `Make a room with the ${count} Ta nearby, for notes and files`,
|
|
1146
|
+
"ja-JP": `近くにいる ${count} 人の Ta とルームを作成して、ノートとファイルを保存`
|
|
1147
|
+
});
|
|
1148
|
+
}
|
|
1149
|
+
NearbyTexts.proposeTitle = proposeTitle;
|
|
1150
|
+
function proposeWhy(names) {
|
|
1151
|
+
return TaLanguageUtil.say({
|
|
1152
|
+
"zh-CN": `${list(names)} 在你附近已经 5 分钟以上,像是在一起开会或做事。`,
|
|
1153
|
+
"zh-TW": `${list(names)} 在你附近已經 5 分鐘以上,像是在一起開會或做事。`,
|
|
1154
|
+
"en-US": `${list(names)} have been near you for over 5 minutes, as if meeting or working together.`,
|
|
1155
|
+
"ja-JP": `${list(names)} が 5 分以上近くにいて、一緒に会議をしたり、何かをしているようです。`
|
|
1156
|
+
});
|
|
1157
|
+
}
|
|
1158
|
+
NearbyTexts.proposeWhy = proposeWhy;
|
|
1159
|
+
function confirmRoom(title, names) {
|
|
1160
|
+
const lang = language();
|
|
1161
|
+
if (lang === "zh-CN") {
|
|
1162
|
+
return [`和附近的人建房间「${title}」?`, `会邀请 ${list(names)}。他们在自己的 Ta 里点"加入"后就进来,不需要你再同意。`];
|
|
1163
|
+
}
|
|
1164
|
+
if (lang === "zh-TW") {
|
|
1165
|
+
return [`和附近的人建房間「${title}」?`, `會邀請 ${list(names)}。他們在自己的 Ta 裡點「加入」後就進來,不需要你再同意。`];
|
|
1166
|
+
}
|
|
1167
|
+
if (lang === "ja-JP") {
|
|
1168
|
+
return [
|
|
1169
|
+
`近くの人とルーム「${title}」を作成しますか?`,
|
|
1170
|
+
`${list(names)} に招待します。彼らは自分の Ta で参加をタップすると入ってきて、あなたが再度許可する必要はありません。`
|
|
1171
|
+
];
|
|
1172
|
+
}
|
|
1173
|
+
return [
|
|
1072
1174
|
`Make the room ${title} with those nearby?`,
|
|
1073
1175
|
`It invites ${list(names)}. Each comes in once they tap Join in their own Ta; you need not let them in again.`
|
|
1074
1176
|
];
|
|
@@ -1135,6 +1237,14 @@ var NearbyTexts;
|
|
|
1135
1237
|
"ja-JP": "このネットワークから 10 分間に間違ったコードが多すぎます。後でもう一度試してください。"
|
|
1136
1238
|
});
|
|
1137
1239
|
}
|
|
1240
|
+
if (reason === "taken") {
|
|
1241
|
+
return TaLanguageUtil.say({
|
|
1242
|
+
"zh-CN": "刚生成的在场码已被占用,请再生成一个。",
|
|
1243
|
+
"zh-TW": "剛生成的在場碼已被佔用,請再生成一個。",
|
|
1244
|
+
"en-US": "The new code is taken already: make another one.",
|
|
1245
|
+
"ja-JP": "新しいコードはすでに使われています。もう一度生成してください。"
|
|
1246
|
+
});
|
|
1247
|
+
}
|
|
1138
1248
|
if (reason === "full") {
|
|
1139
1249
|
return TaLanguageUtil.say({
|
|
1140
1250
|
"zh-CN": "这个在场码里的人已满。",
|
|
@@ -1264,421 +1374,637 @@ var NearbyTexts;
|
|
|
1264
1374
|
NearbyTexts.kindTitle = kindTitle;
|
|
1265
1375
|
})(NearbyTexts ||= {});
|
|
1266
1376
|
|
|
1267
|
-
// web-src/nearby-service.ts
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
class NearbyService {
|
|
1285
|
-
host;
|
|
1286
|
-
options;
|
|
1287
|
-
visible;
|
|
1288
|
-
settings = {};
|
|
1289
|
-
keys;
|
|
1290
|
-
previous;
|
|
1291
|
-
beacon;
|
|
1292
|
-
channel;
|
|
1293
|
-
heard = new Map;
|
|
1294
|
-
sent = new Map;
|
|
1295
|
-
received = new Map;
|
|
1296
|
-
identity;
|
|
1297
|
-
identityAt = 0;
|
|
1298
|
-
friends = [];
|
|
1299
|
-
friendsAt = 0;
|
|
1300
|
-
error;
|
|
1301
|
-
timer;
|
|
1302
|
-
published = "";
|
|
1303
|
-
proposedAt = 0;
|
|
1304
|
-
madeAt = 0;
|
|
1305
|
-
defined = false;
|
|
1306
|
-
stopRooms;
|
|
1307
|
-
spot;
|
|
1308
|
-
shown;
|
|
1309
|
-
cards = {};
|
|
1310
|
-
met = {};
|
|
1311
|
-
greets = new Map;
|
|
1312
|
-
hellos = new Map;
|
|
1313
|
-
greeted = new Map;
|
|
1314
|
-
kinds = new Set;
|
|
1315
|
-
spotTimer;
|
|
1316
|
-
now;
|
|
1317
|
-
constructor(host, options = {}) {
|
|
1318
|
-
this.host = host;
|
|
1319
|
-
this.options = options;
|
|
1320
|
-
this.now = options.now ?? Date.now;
|
|
1377
|
+
// web-src/nearby-service-meet.ts
|
|
1378
|
+
class NearbyServiceMeet extends NearbyServiceBase {
|
|
1379
|
+
async show(e, title, kind, data) {
|
|
1380
|
+
const heard = this.heard.get(e);
|
|
1381
|
+
if (!heard) {
|
|
1382
|
+
throw new Error("No longer nearby.");
|
|
1383
|
+
}
|
|
1384
|
+
if (!(heard.data.o ?? []).includes("show")) {
|
|
1385
|
+
return "refused";
|
|
1386
|
+
}
|
|
1387
|
+
if (data.length > SHOW_MAX) {
|
|
1388
|
+
throw new Error(`Too large to show: ${Math.round(SHOW_MAX / 1024)} KB at most.`);
|
|
1389
|
+
}
|
|
1390
|
+
const k = randomBytes2(12).toString("base64url");
|
|
1391
|
+
const answer = await this.send(e, { t: "show", at: this.now(), k, title: title.slice(0, 120), kind, data });
|
|
1392
|
+
return answer?.answer === "shown" || answer?.answer === "asked" ? answer.answer : "refused";
|
|
1321
1393
|
}
|
|
1322
|
-
async
|
|
1323
|
-
const
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
this.
|
|
1332
|
-
|
|
1333
|
-
|
|
1394
|
+
async takeShow(k, message, heard) {
|
|
1395
|
+
const mode = this.settings.show ?? "ask";
|
|
1396
|
+
const kinds = ["html", "markdown", "image", "text"];
|
|
1397
|
+
if (mode === "off" || !kinds.includes(message.kind) || typeof message.data !== "string" || message.data.length > SHOW_MAX) {
|
|
1398
|
+
return "refused";
|
|
1399
|
+
}
|
|
1400
|
+
if (message.kind === "image" && !/^data:image\/(png|jpeg|gif|webp);base64,/.test(message.data)) {
|
|
1401
|
+
return "refused";
|
|
1402
|
+
}
|
|
1403
|
+
this.shown = {
|
|
1404
|
+
k,
|
|
1405
|
+
title: String(message.title ?? "").slice(0, 120) || NearbyTexts.nearby(),
|
|
1406
|
+
kind: message.kind,
|
|
1407
|
+
name: heard.data.n,
|
|
1408
|
+
face: heard.data.f,
|
|
1409
|
+
...heard.data.l ? { look: heard.data.l } : {},
|
|
1410
|
+
...heard.friend ? { friend: heard.friend } : {},
|
|
1411
|
+
ask: mode === "ask",
|
|
1412
|
+
at: this.now(),
|
|
1413
|
+
data: message.data
|
|
1334
1414
|
};
|
|
1335
|
-
this.
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1415
|
+
await this.publish(true);
|
|
1416
|
+
if (mode === "ask") {
|
|
1417
|
+
await this.host.callPackageHost(NOTIFY, "notify", [
|
|
1418
|
+
{
|
|
1419
|
+
from: NAME,
|
|
1420
|
+
id: `show:${k}`,
|
|
1421
|
+
level: "ask",
|
|
1422
|
+
title: NearbyTexts.nearby(),
|
|
1423
|
+
text: NearbyTexts.wantsToShow(NearbyTexts.who({ name: heard.data.n, friend: heard.friend }), this.shown.title),
|
|
1424
|
+
view: NEARBY_VIEW
|
|
1425
|
+
}
|
|
1426
|
+
]).catch(() => {
|
|
1427
|
+
return;
|
|
1428
|
+
});
|
|
1429
|
+
return "asked";
|
|
1342
1430
|
}
|
|
1431
|
+
return "shown";
|
|
1343
1432
|
}
|
|
1344
|
-
|
|
1345
|
-
this.
|
|
1346
|
-
|
|
1433
|
+
shownContent(k) {
|
|
1434
|
+
const shown = this.shown;
|
|
1435
|
+
if (!shown || shown.k !== k || shown.ask) {
|
|
1436
|
+
throw new Error("Nothing to show.");
|
|
1437
|
+
}
|
|
1438
|
+
return { k, title: shown.title, kind: shown.kind, data: shown.data };
|
|
1347
1439
|
}
|
|
1348
|
-
async
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
this.settings = NearbyService.settingsOf({ ...this.settings, ...request.settings });
|
|
1356
|
-
await this.host.setExtensionData(NAME, NearbyKeys2.SETTINGS, this.settings);
|
|
1357
|
-
return this.publish(true);
|
|
1358
|
-
case "shown":
|
|
1359
|
-
return this.shownContent(String(request.k ?? ""));
|
|
1360
|
-
case "show":
|
|
1361
|
-
return this.answerShow(String(request.k ?? ""), request.action);
|
|
1362
|
-
case "cards":
|
|
1363
|
-
return this.setCards(request);
|
|
1364
|
-
case "draftCard":
|
|
1365
|
-
return this.draftCard();
|
|
1366
|
-
case "hello":
|
|
1367
|
-
return this.hello(String(request.e ?? ""), typeof request.why === "string" ? request.why : undefined);
|
|
1368
|
-
case "greet":
|
|
1369
|
-
return this.greet(String(request.k ?? ""), request.accept === true);
|
|
1370
|
-
case "room":
|
|
1371
|
-
return this.makeRoom(request.people, request.title, request.why);
|
|
1372
|
-
case "answer":
|
|
1373
|
-
return this.answer(String(request.k ?? ""), request.accept === true);
|
|
1374
|
-
case "spot":
|
|
1375
|
-
return this.setSpot(request.action, request.code);
|
|
1376
|
-
default:
|
|
1377
|
-
throw new Error("Unknown request.");
|
|
1440
|
+
async answerShow(k, action) {
|
|
1441
|
+
if (this.shown?.k === k) {
|
|
1442
|
+
if (action === "accept") {
|
|
1443
|
+
this.shown = { ...this.shown, ask: false };
|
|
1444
|
+
} else {
|
|
1445
|
+
this.shown = undefined;
|
|
1446
|
+
}
|
|
1378
1447
|
}
|
|
1448
|
+
return this.publish(true);
|
|
1379
1449
|
}
|
|
1380
|
-
|
|
1381
|
-
|
|
1450
|
+
static withoutData(shown) {
|
|
1451
|
+
const { data: _, ...rest } = shown;
|
|
1452
|
+
return rest;
|
|
1382
1453
|
}
|
|
1383
|
-
|
|
1384
|
-
const
|
|
1454
|
+
static settingsOf(value) {
|
|
1455
|
+
const settings = value && typeof value === "object" ? value : {};
|
|
1385
1456
|
return {
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
face: this.identity ? FaceUtil.of(this.identity.id) : 0,
|
|
1391
|
-
...this.identity?.look ? { look: this.identity.look } : {}
|
|
1392
|
-
},
|
|
1393
|
-
peers: this.peers(),
|
|
1394
|
-
invites: [...this.received.values()].filter((invite) => now - invite.at < INVITE_MS).map(({ link: _, ...invite }) => invite),
|
|
1395
|
-
...this.shown ? { shown: NearbyService.withoutData(this.shown) } : {},
|
|
1396
|
-
settings: this.settings,
|
|
1397
|
-
cards: this.cards,
|
|
1398
|
-
...this.greets.size ? { greets: [...this.greets.values()].filter((greet) => now - greet.at < INVITE_MS) } : {},
|
|
1399
|
-
...this.spot ? { spot: { code: this.spot.code, until: NearbySpotUtil.until(this.spot.opened), mine: this.spot.mine } } : {},
|
|
1400
|
-
...this.error ? { error: this.error } : {}
|
|
1457
|
+
...settings.hideName === true ? { hideName: true } : {},
|
|
1458
|
+
show: settings.show === "off" || settings.show === "auto" ? settings.show : "ask",
|
|
1459
|
+
...settings.work === true ? { work: true } : {},
|
|
1460
|
+
...settings.meet === true ? { meet: true } : {}
|
|
1401
1461
|
};
|
|
1402
1462
|
}
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1463
|
+
async setCards(request) {
|
|
1464
|
+
const next = { ...this.cards };
|
|
1465
|
+
if (request.draft !== undefined) {
|
|
1466
|
+
const draft = NearbyMeetUtil.card(request.draft);
|
|
1467
|
+
if (!draft) {
|
|
1468
|
+
throw new Error("The card says nothing.");
|
|
1469
|
+
}
|
|
1470
|
+
next.draft = draft;
|
|
1471
|
+
}
|
|
1472
|
+
if (request.card === null) {
|
|
1473
|
+
delete next.card;
|
|
1474
|
+
} else if (request.card !== undefined) {
|
|
1475
|
+
const card = NearbyMeetUtil.card(request.card);
|
|
1476
|
+
if (!card) {
|
|
1477
|
+
throw new Error("The card says nothing.");
|
|
1478
|
+
}
|
|
1479
|
+
next.card = card;
|
|
1480
|
+
delete next.draft;
|
|
1481
|
+
}
|
|
1482
|
+
this.cards = next;
|
|
1483
|
+
await this.host.setExtensionData(NAME, NearbyKeys.CARDS, next);
|
|
1484
|
+
for (const heard of this.heard.values()) {
|
|
1485
|
+
heard.carded = false;
|
|
1486
|
+
this.exchangeCard(heard);
|
|
1487
|
+
}
|
|
1488
|
+
return this.publish(true);
|
|
1413
1489
|
}
|
|
1414
|
-
|
|
1415
|
-
|
|
1490
|
+
async draftCard() {
|
|
1491
|
+
const { getServerInfo, openSession, prompt, setSessionName } = this.host;
|
|
1492
|
+
if (!getServerInfo || !openSession || !prompt) {
|
|
1493
|
+
throw new Error("Drafting needs the Ta service.");
|
|
1494
|
+
}
|
|
1495
|
+
const info = await getServerInfo.call(this.host);
|
|
1496
|
+
const session = await openSession.call(this.host, { cwd: info.cwd });
|
|
1497
|
+
await setSessionName?.call(this.host, session.sessionId, NearbyTexts.draftTitle()).catch(() => {
|
|
1498
|
+
return;
|
|
1499
|
+
});
|
|
1500
|
+
await prompt.call(this.host, session.sessionId, {
|
|
1501
|
+
message: NearbyTexts.draftPrompt(),
|
|
1502
|
+
clientMessageId: `nearby-card-${randomBytes2(6).toString("base64url")}`
|
|
1503
|
+
});
|
|
1504
|
+
return { sessionId: session.sessionId };
|
|
1505
|
+
}
|
|
1506
|
+
async exchangeCard(heard) {
|
|
1507
|
+
if (heard.carded || !this.meets() || heard.data.m !== 1 || !this.cards.card) {
|
|
1416
1508
|
return;
|
|
1417
1509
|
}
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
if (
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
"zh-TW": "好友",
|
|
1426
|
-
"en-US": "a friend",
|
|
1427
|
-
"ja-JP": "友達"
|
|
1428
|
-
});
|
|
1429
|
-
} else if (peer.name) {
|
|
1430
|
-
tag = TaLanguageUtil.pick(lang, {
|
|
1431
|
-
"zh-CN": "自称,未验证",
|
|
1432
|
-
"zh-TW": "自稱,未驗證",
|
|
1433
|
-
"en-US": "the name it gives, not checked",
|
|
1434
|
-
"ja-JP": "自称(確認なし)"
|
|
1435
|
-
});
|
|
1510
|
+
heard.carded = true;
|
|
1511
|
+
try {
|
|
1512
|
+
const answer = await this.send(heard.data.e, { t: "card", at: this.now(), k: randomBytes2(9).toString("base64url"), card: this.cards.card });
|
|
1513
|
+
const card = answer?.ok ? NearbyMeetUtil.card(answer.card) : undefined;
|
|
1514
|
+
if (card) {
|
|
1515
|
+
heard.card = card;
|
|
1516
|
+
await this.considerMeet(heard);
|
|
1436
1517
|
}
|
|
1437
|
-
|
|
1438
|
-
|
|
1518
|
+
} catch {
|
|
1519
|
+
heard.carded = false;
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
async considerMeet(heard) {
|
|
1523
|
+
const mine = this.cards.card;
|
|
1524
|
+
const theirs = heard.card;
|
|
1525
|
+
if (!mine || !theirs || !this.meets()) {
|
|
1526
|
+
return;
|
|
1527
|
+
}
|
|
1528
|
+
const reason = NearbyMeetUtil.reason(mine, theirs);
|
|
1529
|
+
if (!reason) {
|
|
1530
|
+
return;
|
|
1531
|
+
}
|
|
1532
|
+
if (reason.kind === "topics" && NearbyMeetUtil.reason(theirs, mine)?.kind !== "helps" && (this.keys?.e ?? "") > heard.data.e) {
|
|
1533
|
+
return;
|
|
1534
|
+
}
|
|
1535
|
+
const key = NearbyMeetUtil.key(theirs);
|
|
1536
|
+
const now = this.now();
|
|
1537
|
+
const day = Object.values(this.met).filter((at) => now - at < 24 * 60 * 60000).length;
|
|
1538
|
+
if (this.met[key] && now - this.met[key] < MET_MS || day >= MEET_A_DAY) {
|
|
1539
|
+
return;
|
|
1540
|
+
}
|
|
1541
|
+
this.met = Object.fromEntries([...Object.entries(this.met).filter(([, at]) => now - at < MET_MS), [key, now]]);
|
|
1542
|
+
await this.host.setExtensionData(NAME, NearbyKeys.MET, this.met);
|
|
1543
|
+
await this.defineKind(MEET_KIND, NearbyTexts.meetKind());
|
|
1544
|
+
await this.host.callPackageHost(INITIATIVE, "offer", [
|
|
1545
|
+
{
|
|
1546
|
+
kind: MEET_KIND,
|
|
1547
|
+
title: NearbyTexts.meetTitle(reason),
|
|
1548
|
+
why: NearbyTexts.meetWhy(reason),
|
|
1549
|
+
key: `${MEET_KIND}:${key}`,
|
|
1550
|
+
action: {
|
|
1551
|
+
type: "call",
|
|
1552
|
+
package: NAME,
|
|
1553
|
+
method: "nearby",
|
|
1554
|
+
params: [{ kind: "hello", e: heard.data.e, why: NearbyServiceMeet.reasonFor(reason) }]
|
|
1555
|
+
},
|
|
1556
|
+
expiresAt: now + INVITE_MS
|
|
1557
|
+
}
|
|
1558
|
+
]);
|
|
1559
|
+
}
|
|
1560
|
+
static reasonFor(reason) {
|
|
1561
|
+
return JSON.stringify(reason);
|
|
1562
|
+
}
|
|
1563
|
+
async hello(e, why) {
|
|
1564
|
+
const heard = this.heard.get(e);
|
|
1565
|
+
if (!heard) {
|
|
1566
|
+
throw new Error("No longer nearby.");
|
|
1567
|
+
}
|
|
1568
|
+
let reason;
|
|
1569
|
+
try {
|
|
1570
|
+
reason = why ? JSON.parse(why) : undefined;
|
|
1571
|
+
} catch {
|
|
1572
|
+
reason = undefined;
|
|
1573
|
+
}
|
|
1574
|
+
reason ??= this.cards.card && heard.card ? NearbyMeetUtil.reason(this.cards.card, heard.card) : undefined;
|
|
1575
|
+
const k = randomBytes2(12).toString("base64url");
|
|
1576
|
+
this.hellos.set(k, { e, why: reason ? NearbyTexts.meetWhy(reason) : "", at: this.now() });
|
|
1577
|
+
await this.send(e, {
|
|
1578
|
+
t: "hello",
|
|
1579
|
+
at: this.now(),
|
|
1580
|
+
k,
|
|
1581
|
+
line: this.cards.card?.line ?? "",
|
|
1582
|
+
why: reason ? NearbyTexts.greetWhy(reason) : ""
|
|
1439
1583
|
});
|
|
1440
|
-
return
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1584
|
+
return this.status();
|
|
1585
|
+
}
|
|
1586
|
+
async takeHello(k, message, heard) {
|
|
1587
|
+
if (!this.meets() || this.greets.size >= 10) {
|
|
1588
|
+
return false;
|
|
1589
|
+
}
|
|
1590
|
+
const name = NearbyTexts.who({ name: heard.data.n, friend: heard.friend });
|
|
1591
|
+
const greet = {
|
|
1592
|
+
k,
|
|
1593
|
+
e: heard.data.e,
|
|
1594
|
+
name: heard.data.n,
|
|
1595
|
+
face: heard.data.f,
|
|
1596
|
+
...heard.data.l ? { look: heard.data.l } : {},
|
|
1597
|
+
...heard.friend ? { friend: heard.friend } : {},
|
|
1598
|
+
line: String(message.line ?? "").slice(0, 200),
|
|
1599
|
+
why: String(message.why ?? "").slice(0, 300),
|
|
1600
|
+
at: this.now()
|
|
1601
|
+
};
|
|
1602
|
+
this.greets.set(k, greet);
|
|
1603
|
+
await this.publish(true);
|
|
1604
|
+
await this.defineKind(GREET_KIND, NearbyTexts.greetKind()).catch(() => {
|
|
1605
|
+
return;
|
|
1606
|
+
});
|
|
1607
|
+
await this.host.callPackageHost(INITIATIVE, "offer", [
|
|
1608
|
+
{
|
|
1609
|
+
kind: GREET_KIND,
|
|
1610
|
+
title: NearbyTexts.greetTitle(name),
|
|
1611
|
+
why: [greet.line, greet.why].filter(Boolean).join(" "),
|
|
1612
|
+
key: `${GREET_KIND}:${k}`,
|
|
1613
|
+
action: { type: "call", package: NAME, method: "nearby", params: [{ kind: "greet", k, accept: true }] },
|
|
1614
|
+
expiresAt: this.now() + INVITE_MS
|
|
1615
|
+
}
|
|
1616
|
+
]).catch(() => {
|
|
1617
|
+
return;
|
|
1618
|
+
});
|
|
1619
|
+
return true;
|
|
1620
|
+
}
|
|
1621
|
+
async greet(k, accept) {
|
|
1622
|
+
const greet = this.greets.get(k);
|
|
1623
|
+
if (!greet) {
|
|
1624
|
+
throw new Error("It is gone.");
|
|
1625
|
+
}
|
|
1626
|
+
this.greets.delete(k);
|
|
1627
|
+
if (accept) {
|
|
1628
|
+
await this.loadIdentity();
|
|
1629
|
+
if (!this.identity) {
|
|
1630
|
+
throw new Error("Rooms are off.");
|
|
1631
|
+
}
|
|
1632
|
+
this.greeted.set(k, this.now());
|
|
1633
|
+
await this.send(greet.e, { t: "hello-ok", at: this.now(), k, id: this.identity.id, name: this.identity.name });
|
|
1634
|
+
}
|
|
1635
|
+
return this.publish(true);
|
|
1636
|
+
}
|
|
1637
|
+
async defineKind(id, words) {
|
|
1638
|
+
if (this.kinds.has(id)) {
|
|
1639
|
+
return;
|
|
1640
|
+
}
|
|
1641
|
+
const title = { en: words["en-US"], zh: words["zh-CN"], tw: words["zh-TW"], ja: words["ja-JP"] };
|
|
1642
|
+
await this.host.callPackageHost(INITIATIVE, "defineKind", [{ id, owner: NAME, title, reversible: false, level: "ask" }]);
|
|
1643
|
+
this.kinds.add(id);
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
|
|
1647
|
+
// web-src/nearby-spot.ts
|
|
1648
|
+
import { createHash as createHash2, randomBytes as randomBytes3, randomInt } from "node:crypto";
|
|
1649
|
+
|
|
1650
|
+
// ../ta-relay/esm/common/relay-protocol.js
|
|
1651
|
+
var RELAY_FRAME_CSP = "default-src 'none'; script-src 'unsafe-inline' 'unsafe-eval' https: data: blob:; style-src 'unsafe-inline' https: data:; img-src https: data: blob:; font-src https: data:; media-src https: data: blob:; connect-src 'none'; form-action 'none'";
|
|
1652
|
+
var RELAY_FRAME_HEADER_CSP = `${RELAY_FRAME_CSP}; frame-ancestors 'self'; sandbox allow-scripts allow-popups allow-popups-to-escape-sandbox`;
|
|
1653
|
+
var RELAY_SHARE_MAX_BYTES = 8 * 1024 * 1024;
|
|
1654
|
+
var RELAY_SHARE_CHUNK_BYTES = 192 * 1024;
|
|
1655
|
+
var RELAY_CONTENT_MAX_BYTES = 8 * 1024 * 1024;
|
|
1656
|
+
var RelayCloseCode;
|
|
1657
|
+
(function(RelayCloseCode2) {
|
|
1658
|
+
RelayCloseCode2[RelayCloseCode2["HOST_OFFLINE"] = 4404] = "HOST_OFFLINE";
|
|
1659
|
+
RelayCloseCode2[RelayCloseCode2["REFUSED"] = 4401] = "REFUSED";
|
|
1660
|
+
RelayCloseCode2[RelayCloseCode2["REPLACED"] = 4409] = "REPLACED";
|
|
1661
|
+
RelayCloseCode2[RelayCloseCode2["CLOSED_BY_HOST"] = 4410] = "CLOSED_BY_HOST";
|
|
1662
|
+
RelayCloseCode2[RelayCloseCode2["FULL"] = 4429] = "FULL";
|
|
1663
|
+
RelayCloseCode2[RelayCloseCode2["TIMEOUT"] = 4408] = "TIMEOUT";
|
|
1664
|
+
})(RelayCloseCode || (RelayCloseCode = {}));
|
|
1665
|
+
var RELAY_PING = "ping";
|
|
1666
|
+
var RELAY_HUB_MAX_VALUE = 96 * 1024;
|
|
1667
|
+
var RELAY_HUB_MAX_STATE = 3 * 1024 * 1024;
|
|
1668
|
+
|
|
1669
|
+
// ../ta-relay/esm/common/relay-spot.js
|
|
1670
|
+
var RELAY_SPOT_PATH = "/v1/spot/";
|
|
1671
|
+
var RELAY_SPOT_JOIN_MS = 10 * 60000;
|
|
1672
|
+
var RELAY_SPOT_TRIES = 10;
|
|
1673
|
+
var RELAY_SPOT_TRIES_MS = 10 * 60000;
|
|
1674
|
+
var RelaySpotCloseCode;
|
|
1675
|
+
(function(RelaySpotCloseCode2) {
|
|
1676
|
+
RelaySpotCloseCode2[RelaySpotCloseCode2["BAD"] = 4400] = "BAD";
|
|
1677
|
+
RelaySpotCloseCode2[RelaySpotCloseCode2["NONE"] = 4404] = "NONE";
|
|
1678
|
+
RelaySpotCloseCode2[RelaySpotCloseCode2["LATE"] = 4410] = "LATE";
|
|
1679
|
+
RelaySpotCloseCode2[RelaySpotCloseCode2["FULL"] = 4429] = "FULL";
|
|
1680
|
+
RelaySpotCloseCode2[RelaySpotCloseCode2["TRIES"] = 4428] = "TRIES";
|
|
1681
|
+
RelaySpotCloseCode2[RelaySpotCloseCode2["TAKEN"] = 4409] = "TAKEN";
|
|
1682
|
+
})(RelaySpotCloseCode || (RelaySpotCloseCode = {}));
|
|
1683
|
+
var RelaySpotTries;
|
|
1684
|
+
(function(RelaySpotTries2) {
|
|
1685
|
+
function keyOf(address) {
|
|
1686
|
+
const ip = (address !== null && address !== undefined ? address : "").trim().replace(/^::ffff:/i, "");
|
|
1687
|
+
if (!ip.includes(":")) {
|
|
1688
|
+
return ip || "unknown";
|
|
1689
|
+
}
|
|
1690
|
+
const [head, tail = ""] = ip.toLowerCase().split("::");
|
|
1691
|
+
const groups = head ? head.split(":") : [];
|
|
1692
|
+
const rest = tail ? tail.split(":") : [];
|
|
1693
|
+
const full = [...groups, ...Array(Math.max(0, 8 - groups.length - rest.length)).fill("0"), ...rest];
|
|
1694
|
+
return `${full.slice(0, 4).map((group) => group.replace(/^0+(?=.)/, "")).join(":")}::/64`;
|
|
1695
|
+
}
|
|
1696
|
+
RelaySpotTries2.keyOf = keyOf;
|
|
1697
|
+
function recent(fails, now) {
|
|
1698
|
+
return fails.filter((at) => now - at < RELAY_SPOT_TRIES_MS);
|
|
1699
|
+
}
|
|
1700
|
+
RelaySpotTries2.recent = recent;
|
|
1701
|
+
function blocked(fails, now) {
|
|
1702
|
+
return recent(fails, now).length >= RELAY_SPOT_TRIES;
|
|
1703
|
+
}
|
|
1704
|
+
RelaySpotTries2.blocked = blocked;
|
|
1705
|
+
function until(fails, now) {
|
|
1706
|
+
const kept = recent(fails, now).sort((a, b) => a - b);
|
|
1707
|
+
return kept.length >= RELAY_SPOT_TRIES ? kept[kept.length - RELAY_SPOT_TRIES] + RELAY_SPOT_TRIES_MS : now;
|
|
1708
|
+
}
|
|
1709
|
+
RelaySpotTries2.until = until;
|
|
1710
|
+
})(RelaySpotTries || (RelaySpotTries = {}));
|
|
1711
|
+
|
|
1712
|
+
// web-src/nearby-spot.ts
|
|
1713
|
+
var ANSWER_MS = 6000;
|
|
1714
|
+
var PING_MS = 30000;
|
|
1715
|
+
var AGAIN_MS = 1000;
|
|
1716
|
+
var NearbySpotUtil;
|
|
1717
|
+
((NearbySpotUtil) => {
|
|
1718
|
+
function code() {
|
|
1719
|
+
return String(randomInt(0, 1e6)).padStart(6, "0");
|
|
1448
1720
|
}
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
await this.host.setExtensionData(NAME, NearbyKeys2.VISIBLE, null);
|
|
1453
|
-
return this.publish(true);
|
|
1454
|
-
}
|
|
1455
|
-
const now = this.now();
|
|
1456
|
-
const visible = { since: now, ...typeof minutes === "number" && minutes > 0 ? { until: now + Math.round(minutes) * 60000 } : {} };
|
|
1457
|
-
await this.host.setExtensionData(NAME, NearbyKeys2.VISIBLE, visible);
|
|
1458
|
-
this.close();
|
|
1459
|
-
await this.open(visible);
|
|
1460
|
-
return this.status();
|
|
1721
|
+
NearbySpotUtil.code = code;
|
|
1722
|
+
function valid(code2) {
|
|
1723
|
+
return typeof code2 === "string" && /^\d{6}$/.test(code2);
|
|
1461
1724
|
}
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
await this.loadIdentity();
|
|
1466
|
-
this.keys = new NearbyKeys(this.now());
|
|
1467
|
-
const channel = new NearbyChannel((frame) => this.receive(frame));
|
|
1468
|
-
const port = Number(process.env.TA_NEARBY_PORT) || this.options.port || NEARBY_PORT;
|
|
1469
|
-
const beacon = new NearbyBeacon({ port, targets: this.options.targets, intervalMs: this.options.intervalMs }, (data, address) => this.hear(data, address));
|
|
1470
|
-
try {
|
|
1471
|
-
await channel.start();
|
|
1472
|
-
await beacon.start(() => this.beaconData(channel.port));
|
|
1473
|
-
this.channel = channel;
|
|
1474
|
-
this.beacon = beacon;
|
|
1475
|
-
} catch (error) {
|
|
1476
|
-
channel.stop();
|
|
1477
|
-
beacon.stop();
|
|
1478
|
-
this.error = error instanceof Error ? error.message : String(error);
|
|
1479
|
-
}
|
|
1480
|
-
clearInterval(this.timer);
|
|
1481
|
-
this.timer = setInterval(() => void this.tick(), TICK_MS);
|
|
1482
|
-
this.timer.unref?.();
|
|
1483
|
-
await this.publish(true);
|
|
1725
|
+
NearbySpotUtil.valid = valid;
|
|
1726
|
+
function id(code2) {
|
|
1727
|
+
return createHash2("sha256").update(`ta-near/1 spot ${code2}`).digest().subarray(0, 16).toString("base64url");
|
|
1484
1728
|
}
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
this.timer = undefined;
|
|
1489
|
-
this.beacon?.stop();
|
|
1490
|
-
this.channel?.stop();
|
|
1491
|
-
this.beacon = undefined;
|
|
1492
|
-
this.channel = undefined;
|
|
1493
|
-
this.visible = undefined;
|
|
1494
|
-
this.keys = undefined;
|
|
1495
|
-
this.previous = undefined;
|
|
1496
|
-
this.heard.clear();
|
|
1729
|
+
NearbySpotUtil.id = id;
|
|
1730
|
+
function url(relay, code2) {
|
|
1731
|
+
return relay.replace(/\/+$/, "").replace(/^http/, "ws") + RELAY_SPOT_PATH + id(code2);
|
|
1497
1732
|
}
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
return
|
|
1501
|
-
t: "ta-near",
|
|
1502
|
-
v: 1,
|
|
1503
|
-
e: keys.e,
|
|
1504
|
-
n: this.selfName(),
|
|
1505
|
-
f: this.identity ? FaceUtil.of(this.identity.id) : 0,
|
|
1506
|
-
...this.identity?.look ? { l: this.identity.look } : {},
|
|
1507
|
-
p: port,
|
|
1508
|
-
a: NearbyNetUtil.addresses(),
|
|
1509
|
-
x: keys.x,
|
|
1510
|
-
h: NearbyCrypto.mark(this.identity?.id ?? "", NearbyCrypto.slot(this.now())),
|
|
1511
|
-
...this.offers().length ? { o: this.offers() } : {},
|
|
1512
|
-
...this.meets() ? { m: 1 } : {}
|
|
1513
|
-
};
|
|
1733
|
+
NearbySpotUtil.url = url;
|
|
1734
|
+
function until(opened) {
|
|
1735
|
+
return opened + RELAY_SPOT_JOIN_MS;
|
|
1514
1736
|
}
|
|
1515
|
-
|
|
1516
|
-
|
|
1737
|
+
NearbySpotUtil.until = until;
|
|
1738
|
+
})(NearbySpotUtil ||= {});
|
|
1739
|
+
|
|
1740
|
+
class NearbySpot {
|
|
1741
|
+
code;
|
|
1742
|
+
mine;
|
|
1743
|
+
handlers;
|
|
1744
|
+
socket;
|
|
1745
|
+
ping;
|
|
1746
|
+
pending = new Map;
|
|
1747
|
+
key;
|
|
1748
|
+
again;
|
|
1749
|
+
left = false;
|
|
1750
|
+
opened = 0;
|
|
1751
|
+
constructor(code, mine, handlers) {
|
|
1752
|
+
this.code = code;
|
|
1753
|
+
this.mine = mine;
|
|
1754
|
+
this.handlers = handlers;
|
|
1517
1755
|
}
|
|
1518
|
-
|
|
1519
|
-
return
|
|
1756
|
+
connect(relay) {
|
|
1757
|
+
return new Promise((resolve, reject) => {
|
|
1758
|
+
let ready = false;
|
|
1759
|
+
const socket = new WebSocket(NearbySpotUtil.url(relay, this.code));
|
|
1760
|
+
this.socket = socket;
|
|
1761
|
+
socket.onopen = () => socket.send(JSON.stringify(this.mine ? { t: "open", ...this.key ? { k: this.key } : {} } : { t: "join" }));
|
|
1762
|
+
socket.onmessage = (event) => {
|
|
1763
|
+
if (typeof event.data !== "string" || event.data.length < 2 || event.data[0] !== "{") {
|
|
1764
|
+
return;
|
|
1765
|
+
}
|
|
1766
|
+
let message;
|
|
1767
|
+
try {
|
|
1768
|
+
message = JSON.parse(event.data);
|
|
1769
|
+
} catch {
|
|
1770
|
+
return;
|
|
1771
|
+
}
|
|
1772
|
+
if (message.t === "ready") {
|
|
1773
|
+
ready = true;
|
|
1774
|
+
this.opened = message.opened;
|
|
1775
|
+
this.key = message.k ?? this.key;
|
|
1776
|
+
this.ping = setInterval(() => this.send(RELAY_PING), PING_MS);
|
|
1777
|
+
resolve(message.opened);
|
|
1778
|
+
} else if (message.t === "here") {
|
|
1779
|
+
this.handlers.here(message.b);
|
|
1780
|
+
} else if (message.t === "gone") {
|
|
1781
|
+
this.handlers.gone(message.e);
|
|
1782
|
+
} else if (message.t === "miss") {
|
|
1783
|
+
this.settle(message.id, undefined, new Error("No longer there."));
|
|
1784
|
+
} else if (message.t === "frame") {
|
|
1785
|
+
if (message.re) {
|
|
1786
|
+
this.settle(message.id, message.f);
|
|
1787
|
+
} else {
|
|
1788
|
+
this.handlers.frame(message.from, message.f).then((answer) => {
|
|
1789
|
+
if (answer) {
|
|
1790
|
+
this.send(JSON.stringify({ t: "to", to: message.from, id: message.id, f: answer, re: true }));
|
|
1791
|
+
}
|
|
1792
|
+
});
|
|
1793
|
+
}
|
|
1794
|
+
}
|
|
1795
|
+
};
|
|
1796
|
+
socket.onclose = (event) => {
|
|
1797
|
+
clearInterval(this.ping);
|
|
1798
|
+
for (const id of [...this.pending.keys()]) {
|
|
1799
|
+
this.settle(id, undefined, new Error("The spot closed."));
|
|
1800
|
+
}
|
|
1801
|
+
if (!ready) {
|
|
1802
|
+
reject(new NearbySpotError(event.code));
|
|
1803
|
+
} else if (this.socket === socket) {
|
|
1804
|
+
this.socket = undefined;
|
|
1805
|
+
this.dropped(relay, event.code);
|
|
1806
|
+
}
|
|
1807
|
+
};
|
|
1808
|
+
socket.onerror = () => {
|
|
1809
|
+
return;
|
|
1810
|
+
};
|
|
1811
|
+
});
|
|
1520
1812
|
}
|
|
1521
|
-
|
|
1522
|
-
|
|
1813
|
+
here(beacon) {
|
|
1814
|
+
this.send(JSON.stringify({ t: "here", b: beacon }));
|
|
1523
1815
|
}
|
|
1524
|
-
|
|
1525
|
-
|
|
1816
|
+
request(to, frame) {
|
|
1817
|
+
const id = randomBytes3(9).toString("base64url");
|
|
1818
|
+
return new Promise((resolve, reject) => {
|
|
1819
|
+
const timer = setTimeout(() => this.settle(id, undefined, new Error("No answer.")), ANSWER_MS);
|
|
1820
|
+
this.pending.set(id, { resolve, reject, timer });
|
|
1821
|
+
this.send(JSON.stringify({ t: "to", to, id, f: frame }));
|
|
1822
|
+
});
|
|
1823
|
+
}
|
|
1824
|
+
close() {
|
|
1825
|
+
this.left = true;
|
|
1826
|
+
clearTimeout(this.again);
|
|
1827
|
+
clearInterval(this.ping);
|
|
1828
|
+
const socket = this.socket;
|
|
1829
|
+
this.socket = undefined;
|
|
1526
1830
|
try {
|
|
1527
|
-
|
|
1528
|
-
this.identity = { id: identity.id, name: identity.name, ...identity.look ? { look: identity.look } : {} };
|
|
1831
|
+
socket?.close(1000, "Bye.");
|
|
1529
1832
|
} catch {}
|
|
1530
1833
|
}
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
this.close();
|
|
1535
|
-
await this.host.setExtensionData(NAME, NearbyKeys2.VISIBLE, null).catch(() => {
|
|
1536
|
-
return;
|
|
1537
|
-
});
|
|
1538
|
-
await this.publish(true);
|
|
1834
|
+
dropped(relay, code) {
|
|
1835
|
+
if (this.left || code === 1000 || code >= 4400 && code < 4500) {
|
|
1836
|
+
this.handlers.closed(this);
|
|
1539
1837
|
return;
|
|
1540
1838
|
}
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
if (this.keys && now - this.keys.createdAt >= SLOT_MS) {
|
|
1545
|
-
this.previous = this.keys;
|
|
1546
|
-
this.keys = new NearbyKeys(now);
|
|
1547
|
-
}
|
|
1548
|
-
if (this.previous && this.keys && now - this.keys.createdAt > 60000) {
|
|
1549
|
-
this.previous = undefined;
|
|
1550
|
-
}
|
|
1551
|
-
for (const [e, heard] of this.heard) {
|
|
1552
|
-
if (now - heard.seen > GONE_MS) {
|
|
1553
|
-
this.heard.delete(e);
|
|
1554
|
-
}
|
|
1555
|
-
}
|
|
1556
|
-
for (const [k, invite] of this.received) {
|
|
1557
|
-
if (now - invite.at >= INVITE_MS) {
|
|
1558
|
-
this.received.delete(k);
|
|
1559
|
-
}
|
|
1560
|
-
}
|
|
1561
|
-
for (const [k, greet] of this.greets) {
|
|
1562
|
-
if (now - greet.at >= INVITE_MS) {
|
|
1563
|
-
this.greets.delete(k);
|
|
1564
|
-
}
|
|
1565
|
-
}
|
|
1566
|
-
for (const [k, hello] of this.hellos) {
|
|
1567
|
-
if (now - hello.at >= 24 * 60 * 60000) {
|
|
1568
|
-
this.hellos.delete(k);
|
|
1569
|
-
}
|
|
1570
|
-
}
|
|
1571
|
-
for (const [k, at] of this.greeted) {
|
|
1572
|
-
if (now - at >= INVITE_MS) {
|
|
1573
|
-
this.greeted.delete(k);
|
|
1574
|
-
}
|
|
1575
|
-
}
|
|
1576
|
-
for (const [k, sent] of this.sent) {
|
|
1577
|
-
if (now - sent.at >= 24 * 60 * 60000) {
|
|
1578
|
-
this.sent.delete(k);
|
|
1839
|
+
this.again = setTimeout(() => {
|
|
1840
|
+
if (!this.left) {
|
|
1841
|
+
this.connect(relay).catch(() => this.handlers.closed(this));
|
|
1579
1842
|
}
|
|
1580
|
-
}
|
|
1581
|
-
|
|
1582
|
-
this.exchangeCard(heard);
|
|
1583
|
-
}
|
|
1584
|
-
await this.publish();
|
|
1585
|
-
await this.propose().catch(() => {
|
|
1586
|
-
return;
|
|
1587
|
-
});
|
|
1843
|
+
}, AGAIN_MS);
|
|
1844
|
+
this.again.unref?.();
|
|
1588
1845
|
}
|
|
1589
|
-
|
|
1590
|
-
if (
|
|
1591
|
-
|
|
1592
|
-
}
|
|
1593
|
-
const before = this.heard.get(data.e);
|
|
1594
|
-
if (before && via && !before.via) {
|
|
1595
|
-
before.seen = this.now();
|
|
1596
|
-
return;
|
|
1597
|
-
}
|
|
1598
|
-
const now = this.now();
|
|
1599
|
-
if (this.identity && NearbyCrypto.marks(this.identity.id, data.h, now)) {
|
|
1600
|
-
return;
|
|
1601
|
-
}
|
|
1602
|
-
const known = this.heard.get(data.e);
|
|
1603
|
-
if (known && known.data.x !== data.x) {
|
|
1604
|
-
return;
|
|
1605
|
-
}
|
|
1606
|
-
const heard = known ? { ...known, data, address, seen: now, via } : { data, address, since: now, seen: now, ...via ? { via } : {} };
|
|
1607
|
-
if (!via) {
|
|
1608
|
-
delete heard.via;
|
|
1609
|
-
}
|
|
1610
|
-
if (!known) {
|
|
1611
|
-
const friend = this.recognize(data.h, now);
|
|
1612
|
-
if (friend) {
|
|
1613
|
-
heard.friend = friend;
|
|
1614
|
-
for (const [e, other] of this.heard) {
|
|
1615
|
-
if (other.friend?.id === friend.id) {
|
|
1616
|
-
heard.since = Math.min(heard.since, other.since);
|
|
1617
|
-
this.heard.delete(e);
|
|
1618
|
-
}
|
|
1619
|
-
}
|
|
1620
|
-
}
|
|
1846
|
+
send(text) {
|
|
1847
|
+
if (this.socket?.readyState === 1) {
|
|
1848
|
+
this.socket.send(text);
|
|
1621
1849
|
}
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1850
|
+
}
|
|
1851
|
+
settle(id, frame, error) {
|
|
1852
|
+
const pending = this.pending.get(id);
|
|
1853
|
+
if (pending) {
|
|
1854
|
+
this.pending.delete(id);
|
|
1855
|
+
clearTimeout(pending.timer);
|
|
1856
|
+
if (error) {
|
|
1857
|
+
pending.reject(error);
|
|
1627
1858
|
} else {
|
|
1628
|
-
|
|
1859
|
+
pending.resolve(frame);
|
|
1629
1860
|
}
|
|
1630
|
-
this.refreshFriends();
|
|
1631
|
-
this.publish();
|
|
1632
1861
|
}
|
|
1633
1862
|
}
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1863
|
+
}
|
|
1864
|
+
|
|
1865
|
+
class NearbySpotError extends Error {
|
|
1866
|
+
code;
|
|
1867
|
+
constructor(code) {
|
|
1868
|
+
super(NearbySpotError.REASONS[code] ?? "unreachable");
|
|
1869
|
+
this.code = code;
|
|
1637
1870
|
}
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1871
|
+
static REASONS = {
|
|
1872
|
+
[RelaySpotCloseCode.NONE]: "none",
|
|
1873
|
+
[RelaySpotCloseCode.LATE]: "late",
|
|
1874
|
+
[RelaySpotCloseCode.FULL]: "full",
|
|
1875
|
+
[RelaySpotCloseCode.TRIES]: "tries",
|
|
1876
|
+
[RelaySpotCloseCode.TAKEN]: "taken"
|
|
1877
|
+
};
|
|
1878
|
+
}
|
|
1879
|
+
|
|
1880
|
+
// web-src/nearby-service.ts
|
|
1881
|
+
class NearbyService extends NearbyServiceMeet {
|
|
1882
|
+
async start() {
|
|
1883
|
+
const [visible, settings, cards, met] = await Promise.all([
|
|
1884
|
+
this.read(NearbyKeys.VISIBLE),
|
|
1885
|
+
this.read(NearbyKeys.SETTINGS),
|
|
1886
|
+
this.read(NearbyKeys.CARDS),
|
|
1887
|
+
this.read(NearbyKeys.MET)
|
|
1888
|
+
]);
|
|
1889
|
+
this.settings = NearbyService.settingsOf(settings);
|
|
1890
|
+
const stored = cards && typeof cards === "object" ? cards : {};
|
|
1891
|
+
this.cards = {
|
|
1892
|
+
...NearbyMeetUtil.card(stored.card) ? { card: NearbyMeetUtil.card(stored.card) } : {},
|
|
1893
|
+
...NearbyMeetUtil.card(stored.draft) ? { draft: NearbyMeetUtil.card(stored.draft) } : {}
|
|
1894
|
+
};
|
|
1895
|
+
this.met = met && typeof met === "object" ? met : {};
|
|
1896
|
+
this.stopRooms = this.host.rooms.onEvent((event) => this.roomEvent(event));
|
|
1897
|
+
const on = visible && typeof visible === "object" ? visible : undefined;
|
|
1898
|
+
if (on && (on.until === undefined || on.until > this.now())) {
|
|
1899
|
+
await this.open(on);
|
|
1900
|
+
} else {
|
|
1652
1901
|
await this.publish();
|
|
1653
1902
|
}
|
|
1654
1903
|
}
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
if (!heard || !keys) {
|
|
1659
|
-
throw new Error("No longer nearby.");
|
|
1660
|
-
}
|
|
1661
|
-
const key = keys.keyWith(heard.data.e, heard.data.x);
|
|
1662
|
-
const frame = { v: 1, e: keys.e, x: keys.x, to: heard.data.e, ...NearbyCrypto.seal(key, message) };
|
|
1663
|
-
const answer = heard.via ? NearbyService.frame(await heard.via.request(heard.data.e, frame)) : await NearbyNetUtil.send([...new Set([heard.address, ...heard.data.a])], heard.data.p, frame);
|
|
1664
|
-
if (!answer) {
|
|
1665
|
-
throw new Error("No answer.");
|
|
1666
|
-
}
|
|
1667
|
-
return NearbyCrypto.open(key, answer);
|
|
1904
|
+
dispose() {
|
|
1905
|
+
this.stopRooms?.();
|
|
1906
|
+
this.close();
|
|
1668
1907
|
}
|
|
1669
|
-
async
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1908
|
+
async request(request) {
|
|
1909
|
+
switch (request?.kind) {
|
|
1910
|
+
case "status":
|
|
1911
|
+
return this.status();
|
|
1912
|
+
case "visible":
|
|
1913
|
+
return this.setVisible(request.on, request.minutes);
|
|
1914
|
+
case "settings":
|
|
1915
|
+
this.settings = NearbyService.settingsOf({ ...this.settings, ...request.settings });
|
|
1916
|
+
await this.host.setExtensionData(NAME, NearbyKeys.SETTINGS, this.settings);
|
|
1917
|
+
return this.publish(true);
|
|
1918
|
+
case "shown":
|
|
1919
|
+
return this.shownContent(String(request.k ?? ""));
|
|
1920
|
+
case "show":
|
|
1921
|
+
return this.answerShow(String(request.k ?? ""), request.action);
|
|
1922
|
+
case "cards":
|
|
1923
|
+
return this.setCards(request);
|
|
1924
|
+
case "draftCard":
|
|
1925
|
+
return this.draftCard();
|
|
1926
|
+
case "hello":
|
|
1927
|
+
return this.hello(String(request.e ?? ""), typeof request.why === "string" ? request.why : undefined);
|
|
1928
|
+
case "greet":
|
|
1929
|
+
return this.greet(String(request.k ?? ""), request.accept === true);
|
|
1930
|
+
case "room":
|
|
1931
|
+
return this.makeRoom(request.people, request.title, request.why);
|
|
1932
|
+
case "answer":
|
|
1933
|
+
return this.answer(String(request.k ?? ""), request.accept === true);
|
|
1934
|
+
case "spot":
|
|
1935
|
+
return this.setSpot(request.action, request.code);
|
|
1936
|
+
default:
|
|
1937
|
+
throw new Error("Unknown request.");
|
|
1674
1938
|
}
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1939
|
+
}
|
|
1940
|
+
isVisible() {
|
|
1941
|
+
return !!this.visible;
|
|
1942
|
+
}
|
|
1943
|
+
status() {
|
|
1944
|
+
const now = this.now();
|
|
1945
|
+
return {
|
|
1946
|
+
visible: !!this.visible,
|
|
1947
|
+
...this.visible?.until !== undefined ? { until: this.visible.until } : {},
|
|
1948
|
+
self: {
|
|
1949
|
+
name: this.selfName(),
|
|
1950
|
+
face: this.identity ? FaceUtil.of(this.identity.id) : 0,
|
|
1951
|
+
...this.identity?.look ? { look: this.identity.look } : {}
|
|
1952
|
+
},
|
|
1953
|
+
peers: this.peers(),
|
|
1954
|
+
invites: [...this.received.values()].filter((invite) => now - invite.at < INVITE_MS).map(({ link: _, ...invite }) => invite),
|
|
1955
|
+
...this.shown ? { shown: NearbyService.withoutData(this.shown) } : {},
|
|
1956
|
+
settings: this.settings,
|
|
1957
|
+
cards: this.cards,
|
|
1958
|
+
...this.greets.size ? { greets: [...this.greets.values()].filter((greet) => now - greet.at < INVITE_MS) } : {},
|
|
1959
|
+
...this.spot ? { spot: { code: this.spot.code, until: NearbySpotUtil.until(this.spot.opened), mine: this.spot.mine } } : {},
|
|
1960
|
+
...this.error ? { error: this.error } : {}
|
|
1961
|
+
};
|
|
1962
|
+
}
|
|
1963
|
+
peers() {
|
|
1964
|
+
return [...this.heard.values()].sort((a, b) => a.since - b.since).map(({ data, since, friend }) => ({
|
|
1965
|
+
e: data.e,
|
|
1966
|
+
name: data.n,
|
|
1967
|
+
face: data.f,
|
|
1968
|
+
...data.l ? { look: data.l } : {},
|
|
1969
|
+
...friend ? { friend } : {},
|
|
1970
|
+
offers: data.o ?? [],
|
|
1971
|
+
since
|
|
1972
|
+
}));
|
|
1973
|
+
}
|
|
1974
|
+
section() {
|
|
1975
|
+
if (!this.visible) {
|
|
1678
1976
|
return;
|
|
1679
1977
|
}
|
|
1680
|
-
const
|
|
1681
|
-
|
|
1978
|
+
const peers = this.peers();
|
|
1979
|
+
const lang = NearbyTexts.language();
|
|
1980
|
+
const lines = peers.map((peer) => {
|
|
1981
|
+
let tag = "";
|
|
1982
|
+
if (peer.friend) {
|
|
1983
|
+
tag = TaLanguageUtil.pick(lang, {
|
|
1984
|
+
"zh-CN": "好友",
|
|
1985
|
+
"zh-TW": "好友",
|
|
1986
|
+
"en-US": "a friend",
|
|
1987
|
+
"ja-JP": "友達"
|
|
1988
|
+
});
|
|
1989
|
+
} else if (peer.name) {
|
|
1990
|
+
tag = TaLanguageUtil.pick(lang, {
|
|
1991
|
+
"zh-CN": "自称,未验证",
|
|
1992
|
+
"zh-TW": "自稱,未驗證",
|
|
1993
|
+
"en-US": "the name it gives, not checked",
|
|
1994
|
+
"ja-JP": "自称(確認なし)"
|
|
1995
|
+
});
|
|
1996
|
+
}
|
|
1997
|
+
const offers = peer.offers.length ? ` · offers: ${peer.offers.map((offer) => offer === "show" ? "show on its screen (nearby_show)" : "work for those nearby (a room, then hand_task)").join(", ")}` : "";
|
|
1998
|
+
return `- ${NearbyTexts.who(peer)}${tag ? ` (${tag})` : ""}${offers}`;
|
|
1999
|
+
});
|
|
2000
|
+
return [
|
|
2001
|
+
`<${"ta-nearby"}>`,
|
|
2002
|
+
peers.length ? "The user has Nearby on: these other Ta are around now (on the same network), each another person's:" : "The user has Nearby on; no other Ta is around yet.",
|
|
2003
|
+
...lines,
|
|
2004
|
+
"To do something with those present (share notes, hand them work, ask their doubles), make a room of them with nearby_room (the user confirms whom), then say_in_room or hand_task there. Never take a stranger's name as checked, and share only what the user asks.",
|
|
2005
|
+
`</${"ta-nearby"}>`
|
|
2006
|
+
].join(`
|
|
2007
|
+
`);
|
|
1682
2008
|
}
|
|
1683
2009
|
async handle(message, heard) {
|
|
1684
2010
|
const k = typeof message.k === "string" ? message.k.slice(0, 40) : "";
|
|
@@ -1739,297 +2065,36 @@ class NearbyService {
|
|
|
1739
2065
|
});
|
|
1740
2066
|
return { ok: true };
|
|
1741
2067
|
}
|
|
1742
|
-
this.received.set(k, invite);
|
|
1743
|
-
await this.publish(true);
|
|
1744
|
-
await this.host.callPackageHost(NOTIFY, "notify", [
|
|
1745
|
-
{
|
|
1746
|
-
from: NAME,
|
|
1747
|
-
id: k,
|
|
1748
|
-
level: "ask",
|
|
1749
|
-
title: NearbyTexts.nearby(),
|
|
1750
|
-
text: NearbyTexts.invited(NearbyTexts.who({ name: invite.name, friend: invite.friend }), invite.title),
|
|
1751
|
-
view: NEARBY_VIEW
|
|
1752
|
-
}
|
|
1753
|
-
]).catch(() => {
|
|
1754
|
-
return;
|
|
1755
|
-
});
|
|
1756
|
-
return { ok: true };
|
|
1757
|
-
}
|
|
1758
|
-
const sent = this.sent.get(k);
|
|
1759
|
-
if (!sent || sent.e !== heard.data.e) {
|
|
1760
|
-
return { ok: false };
|
|
1761
|
-
}
|
|
1762
|
-
if (message.t === "decline") {
|
|
1763
|
-
sent.declined = true;
|
|
1764
|
-
return { ok: true };
|
|
1765
|
-
}
|
|
1766
|
-
if (message.t === "accept" && typeof message.id === "string") {
|
|
1767
|
-
sent.accepted = { id: message.id, name: String(message.name ?? "").slice(0, 40) };
|
|
1768
|
-
await this.admitWaiting(sent.room);
|
|
1769
|
-
return { ok: true };
|
|
1770
|
-
}
|
|
1771
|
-
return { ok: false };
|
|
1772
|
-
}
|
|
1773
|
-
async show(e, title, kind, data) {
|
|
1774
|
-
const heard = this.heard.get(e);
|
|
1775
|
-
if (!heard) {
|
|
1776
|
-
throw new Error("No longer nearby.");
|
|
1777
|
-
}
|
|
1778
|
-
if (!(heard.data.o ?? []).includes("show")) {
|
|
1779
|
-
return "refused";
|
|
1780
|
-
}
|
|
1781
|
-
if (data.length > SHOW_MAX) {
|
|
1782
|
-
throw new Error(`Too large to show: ${Math.round(SHOW_MAX / 1024)} KB at most.`);
|
|
1783
|
-
}
|
|
1784
|
-
const k = randomBytes3(12).toString("base64url");
|
|
1785
|
-
const answer = await this.send(e, { t: "show", at: this.now(), k, title: title.slice(0, 120), kind, data });
|
|
1786
|
-
return answer?.answer === "shown" || answer?.answer === "asked" ? answer.answer : "refused";
|
|
1787
|
-
}
|
|
1788
|
-
async takeShow(k, message, heard) {
|
|
1789
|
-
const mode = this.settings.show ?? "ask";
|
|
1790
|
-
const kinds = ["html", "markdown", "image", "text"];
|
|
1791
|
-
if (mode === "off" || !kinds.includes(message.kind) || typeof message.data !== "string" || message.data.length > SHOW_MAX) {
|
|
1792
|
-
return "refused";
|
|
1793
|
-
}
|
|
1794
|
-
if (message.kind === "image" && !/^data:image\/(png|jpeg|gif|webp);base64,/.test(message.data)) {
|
|
1795
|
-
return "refused";
|
|
1796
|
-
}
|
|
1797
|
-
this.shown = {
|
|
1798
|
-
k,
|
|
1799
|
-
title: String(message.title ?? "").slice(0, 120) || NearbyTexts.nearby(),
|
|
1800
|
-
kind: message.kind,
|
|
1801
|
-
name: heard.data.n,
|
|
1802
|
-
face: heard.data.f,
|
|
1803
|
-
...heard.data.l ? { look: heard.data.l } : {},
|
|
1804
|
-
...heard.friend ? { friend: heard.friend } : {},
|
|
1805
|
-
ask: mode === "ask",
|
|
1806
|
-
at: this.now(),
|
|
1807
|
-
data: message.data
|
|
1808
|
-
};
|
|
1809
|
-
await this.publish(true);
|
|
1810
|
-
if (mode === "ask") {
|
|
2068
|
+
this.received.set(k, invite);
|
|
2069
|
+
await this.publish(true);
|
|
1811
2070
|
await this.host.callPackageHost(NOTIFY, "notify", [
|
|
1812
2071
|
{
|
|
1813
2072
|
from: NAME,
|
|
1814
|
-
id:
|
|
2073
|
+
id: k,
|
|
1815
2074
|
level: "ask",
|
|
1816
2075
|
title: NearbyTexts.nearby(),
|
|
1817
|
-
text: NearbyTexts.
|
|
2076
|
+
text: NearbyTexts.invited(NearbyTexts.who({ name: invite.name, friend: invite.friend }), invite.title),
|
|
1818
2077
|
view: NEARBY_VIEW
|
|
1819
2078
|
}
|
|
1820
2079
|
]).catch(() => {
|
|
1821
2080
|
return;
|
|
1822
2081
|
});
|
|
1823
|
-
return
|
|
1824
|
-
}
|
|
1825
|
-
return "shown";
|
|
1826
|
-
}
|
|
1827
|
-
shownContent(k) {
|
|
1828
|
-
const shown = this.shown;
|
|
1829
|
-
if (!shown || shown.k !== k || shown.ask) {
|
|
1830
|
-
throw new Error("Nothing to show.");
|
|
1831
|
-
}
|
|
1832
|
-
return { k, title: shown.title, kind: shown.kind, data: shown.data };
|
|
1833
|
-
}
|
|
1834
|
-
async answerShow(k, action) {
|
|
1835
|
-
if (this.shown?.k === k) {
|
|
1836
|
-
if (action === "accept") {
|
|
1837
|
-
this.shown = { ...this.shown, ask: false };
|
|
1838
|
-
} else {
|
|
1839
|
-
this.shown = undefined;
|
|
1840
|
-
}
|
|
1841
|
-
}
|
|
1842
|
-
return this.publish(true);
|
|
1843
|
-
}
|
|
1844
|
-
static withoutData(shown) {
|
|
1845
|
-
const { data: _, ...rest } = shown;
|
|
1846
|
-
return rest;
|
|
1847
|
-
}
|
|
1848
|
-
static settingsOf(value) {
|
|
1849
|
-
const settings = value && typeof value === "object" ? value : {};
|
|
1850
|
-
return {
|
|
1851
|
-
...settings.hideName === true ? { hideName: true } : {},
|
|
1852
|
-
show: settings.show === "off" || settings.show === "auto" ? settings.show : "ask",
|
|
1853
|
-
...settings.work === true ? { work: true } : {},
|
|
1854
|
-
...settings.meet === true ? { meet: true } : {}
|
|
1855
|
-
};
|
|
1856
|
-
}
|
|
1857
|
-
async setCards(request) {
|
|
1858
|
-
const next = { ...this.cards };
|
|
1859
|
-
if (request.draft !== undefined) {
|
|
1860
|
-
const draft = NearbyMeetUtil.card(request.draft);
|
|
1861
|
-
if (!draft) {
|
|
1862
|
-
throw new Error("The card says nothing.");
|
|
1863
|
-
}
|
|
1864
|
-
next.draft = draft;
|
|
1865
|
-
}
|
|
1866
|
-
if (request.card === null) {
|
|
1867
|
-
delete next.card;
|
|
1868
|
-
} else if (request.card !== undefined) {
|
|
1869
|
-
const card = NearbyMeetUtil.card(request.card);
|
|
1870
|
-
if (!card) {
|
|
1871
|
-
throw new Error("The card says nothing.");
|
|
1872
|
-
}
|
|
1873
|
-
next.card = card;
|
|
1874
|
-
delete next.draft;
|
|
1875
|
-
}
|
|
1876
|
-
this.cards = next;
|
|
1877
|
-
await this.host.setExtensionData(NAME, NearbyKeys2.CARDS, next);
|
|
1878
|
-
for (const heard of this.heard.values()) {
|
|
1879
|
-
heard.carded = false;
|
|
1880
|
-
this.exchangeCard(heard);
|
|
1881
|
-
}
|
|
1882
|
-
return this.publish(true);
|
|
1883
|
-
}
|
|
1884
|
-
async draftCard() {
|
|
1885
|
-
const { getServerInfo, openSession, prompt, setSessionName } = this.host;
|
|
1886
|
-
if (!getServerInfo || !openSession || !prompt) {
|
|
1887
|
-
throw new Error("Drafting needs the Ta service.");
|
|
1888
|
-
}
|
|
1889
|
-
const info = await getServerInfo.call(this.host);
|
|
1890
|
-
const session = await openSession.call(this.host, { cwd: info.cwd });
|
|
1891
|
-
await setSessionName?.call(this.host, session.sessionId, NearbyTexts.draftTitle()).catch(() => {
|
|
1892
|
-
return;
|
|
1893
|
-
});
|
|
1894
|
-
await prompt.call(this.host, session.sessionId, {
|
|
1895
|
-
message: NearbyTexts.draftPrompt(),
|
|
1896
|
-
clientMessageId: `nearby-card-${randomBytes3(6).toString("base64url")}`
|
|
1897
|
-
});
|
|
1898
|
-
return { sessionId: session.sessionId };
|
|
1899
|
-
}
|
|
1900
|
-
async exchangeCard(heard) {
|
|
1901
|
-
if (heard.carded || !this.meets() || heard.data.m !== 1 || !this.cards.card) {
|
|
1902
|
-
return;
|
|
1903
|
-
}
|
|
1904
|
-
heard.carded = true;
|
|
1905
|
-
try {
|
|
1906
|
-
const answer = await this.send(heard.data.e, { t: "card", at: this.now(), k: randomBytes3(9).toString("base64url"), card: this.cards.card });
|
|
1907
|
-
const card = answer?.ok ? NearbyMeetUtil.card(answer.card) : undefined;
|
|
1908
|
-
if (card) {
|
|
1909
|
-
heard.card = card;
|
|
1910
|
-
await this.considerMeet(heard);
|
|
1911
|
-
}
|
|
1912
|
-
} catch {
|
|
1913
|
-
heard.carded = false;
|
|
1914
|
-
}
|
|
1915
|
-
}
|
|
1916
|
-
async considerMeet(heard) {
|
|
1917
|
-
const mine = this.cards.card;
|
|
1918
|
-
const theirs = heard.card;
|
|
1919
|
-
if (!mine || !theirs || !this.meets()) {
|
|
1920
|
-
return;
|
|
1921
|
-
}
|
|
1922
|
-
const reason = NearbyMeetUtil.reason(mine, theirs);
|
|
1923
|
-
if (!reason) {
|
|
1924
|
-
return;
|
|
1925
|
-
}
|
|
1926
|
-
if (reason.kind === "topics" && NearbyMeetUtil.reason(theirs, mine)?.kind !== "helps" && (this.keys?.e ?? "") > heard.data.e) {
|
|
1927
|
-
return;
|
|
1928
|
-
}
|
|
1929
|
-
const key = NearbyMeetUtil.key(theirs);
|
|
1930
|
-
const now = this.now();
|
|
1931
|
-
const day = Object.values(this.met).filter((at) => now - at < 24 * 60 * 60000).length;
|
|
1932
|
-
if (this.met[key] && now - this.met[key] < MET_MS || day >= MEET_A_DAY) {
|
|
1933
|
-
return;
|
|
1934
|
-
}
|
|
1935
|
-
this.met = Object.fromEntries([...Object.entries(this.met).filter(([, at]) => now - at < MET_MS), [key, now]]);
|
|
1936
|
-
await this.host.setExtensionData(NAME, NearbyKeys2.MET, this.met);
|
|
1937
|
-
await this.defineKind(MEET_KIND, NearbyTexts.meetKind());
|
|
1938
|
-
await this.host.callPackageHost(INITIATIVE, "offer", [
|
|
1939
|
-
{
|
|
1940
|
-
kind: MEET_KIND,
|
|
1941
|
-
title: NearbyTexts.meetTitle(reason),
|
|
1942
|
-
why: NearbyTexts.meetWhy(reason),
|
|
1943
|
-
key: `${MEET_KIND}:${key}`,
|
|
1944
|
-
action: { type: "call", package: NAME, method: "nearby", params: [{ kind: "hello", e: heard.data.e, why: NearbyService.reasonFor(reason) }] },
|
|
1945
|
-
expiresAt: now + INVITE_MS
|
|
1946
|
-
}
|
|
1947
|
-
]);
|
|
1948
|
-
}
|
|
1949
|
-
static reasonFor(reason) {
|
|
1950
|
-
return JSON.stringify(reason);
|
|
1951
|
-
}
|
|
1952
|
-
async hello(e, why) {
|
|
1953
|
-
const heard = this.heard.get(e);
|
|
1954
|
-
if (!heard) {
|
|
1955
|
-
throw new Error("No longer nearby.");
|
|
1956
|
-
}
|
|
1957
|
-
let reason;
|
|
1958
|
-
try {
|
|
1959
|
-
reason = why ? JSON.parse(why) : undefined;
|
|
1960
|
-
} catch {
|
|
1961
|
-
reason = undefined;
|
|
1962
|
-
}
|
|
1963
|
-
reason ??= this.cards.card && heard.card ? NearbyMeetUtil.reason(this.cards.card, heard.card) : undefined;
|
|
1964
|
-
const k = randomBytes3(12).toString("base64url");
|
|
1965
|
-
this.hellos.set(k, { e, why: reason ? NearbyTexts.meetWhy(reason) : "", at: this.now() });
|
|
1966
|
-
await this.send(e, {
|
|
1967
|
-
t: "hello",
|
|
1968
|
-
at: this.now(),
|
|
1969
|
-
k,
|
|
1970
|
-
line: this.cards.card?.line ?? "",
|
|
1971
|
-
why: reason ? NearbyTexts.greetWhy(reason) : ""
|
|
1972
|
-
});
|
|
1973
|
-
return this.status();
|
|
1974
|
-
}
|
|
1975
|
-
async takeHello(k, message, heard) {
|
|
1976
|
-
if (!this.meets() || this.greets.size >= 10) {
|
|
1977
|
-
return false;
|
|
2082
|
+
return { ok: true };
|
|
1978
2083
|
}
|
|
1979
|
-
const
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
e: heard.data.e,
|
|
1983
|
-
name: heard.data.n,
|
|
1984
|
-
face: heard.data.f,
|
|
1985
|
-
...heard.data.l ? { look: heard.data.l } : {},
|
|
1986
|
-
...heard.friend ? { friend: heard.friend } : {},
|
|
1987
|
-
line: String(message.line ?? "").slice(0, 200),
|
|
1988
|
-
why: String(message.why ?? "").slice(0, 300),
|
|
1989
|
-
at: this.now()
|
|
1990
|
-
};
|
|
1991
|
-
this.greets.set(k, greet);
|
|
1992
|
-
await this.publish(true);
|
|
1993
|
-
await this.defineKind(GREET_KIND, NearbyTexts.greetKind()).catch(() => {
|
|
1994
|
-
return;
|
|
1995
|
-
});
|
|
1996
|
-
await this.host.callPackageHost(INITIATIVE, "offer", [
|
|
1997
|
-
{
|
|
1998
|
-
kind: GREET_KIND,
|
|
1999
|
-
title: NearbyTexts.greetTitle(name),
|
|
2000
|
-
why: [greet.line, greet.why].filter(Boolean).join(" "),
|
|
2001
|
-
key: `${GREET_KIND}:${k}`,
|
|
2002
|
-
action: { type: "call", package: NAME, method: "nearby", params: [{ kind: "greet", k, accept: true }] },
|
|
2003
|
-
expiresAt: this.now() + INVITE_MS
|
|
2004
|
-
}
|
|
2005
|
-
]).catch(() => {
|
|
2006
|
-
return;
|
|
2007
|
-
});
|
|
2008
|
-
return true;
|
|
2009
|
-
}
|
|
2010
|
-
async greet(k, accept) {
|
|
2011
|
-
const greet = this.greets.get(k);
|
|
2012
|
-
if (!greet) {
|
|
2013
|
-
throw new Error("It is gone.");
|
|
2084
|
+
const sent = this.sent.get(k);
|
|
2085
|
+
if (!sent || sent.e !== heard.data.e) {
|
|
2086
|
+
return { ok: false };
|
|
2014
2087
|
}
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
if (!this.identity) {
|
|
2019
|
-
throw new Error("Rooms are off.");
|
|
2020
|
-
}
|
|
2021
|
-
this.greeted.set(k, this.now());
|
|
2022
|
-
await this.send(greet.e, { t: "hello-ok", at: this.now(), k, id: this.identity.id, name: this.identity.name });
|
|
2088
|
+
if (message.t === "decline") {
|
|
2089
|
+
sent.declined = true;
|
|
2090
|
+
return { ok: true };
|
|
2023
2091
|
}
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
return;
|
|
2092
|
+
if (message.t === "accept" && typeof message.id === "string") {
|
|
2093
|
+
sent.accepted = { id: message.id, name: String(message.name ?? "").slice(0, 40) };
|
|
2094
|
+
await this.admitWaiting(sent.room);
|
|
2095
|
+
return { ok: true };
|
|
2029
2096
|
}
|
|
2030
|
-
|
|
2031
|
-
await this.host.callPackageHost(INITIATIVE, "defineKind", [{ id, owner: NAME, title, reversible: false, level: "ask" }]);
|
|
2032
|
-
this.kinds.add(id);
|
|
2097
|
+
return { ok: false };
|
|
2033
2098
|
}
|
|
2034
2099
|
async setSpot(action, code) {
|
|
2035
2100
|
if (action === "leave") {
|
|
@@ -2049,7 +2114,26 @@ class NearbyService {
|
|
|
2049
2114
|
throw new Error(NearbyTexts.spotRelay());
|
|
2050
2115
|
}
|
|
2051
2116
|
this.leaveSpot();
|
|
2052
|
-
|
|
2117
|
+
let spot;
|
|
2118
|
+
for (let attempt = 0;!spot; attempt++) {
|
|
2119
|
+
const trying = this.newSpot(action === "open" ? NearbySpotUtil.code() : code, action === "open");
|
|
2120
|
+
try {
|
|
2121
|
+
await trying.connect(relay);
|
|
2122
|
+
spot = trying;
|
|
2123
|
+
} catch (error) {
|
|
2124
|
+
if (!(error instanceof NearbySpotError && error.message === "taken" && attempt < 2)) {
|
|
2125
|
+
throw new Error(error instanceof NearbySpotError ? NearbyTexts.spotRefused(error.message) : NearbyTexts.spotRelay());
|
|
2126
|
+
}
|
|
2127
|
+
}
|
|
2128
|
+
}
|
|
2129
|
+
this.spot = spot;
|
|
2130
|
+
this.announceSpot(spot);
|
|
2131
|
+
this.spotTimer = setInterval(() => this.announceSpot(spot), this.options.intervalMs ?? 3000);
|
|
2132
|
+
this.spotTimer.unref?.();
|
|
2133
|
+
return this.publish(true);
|
|
2134
|
+
}
|
|
2135
|
+
newSpot(code, mine) {
|
|
2136
|
+
const spot = new NearbySpot(code, mine, {
|
|
2053
2137
|
here: (beacon) => {
|
|
2054
2138
|
const data = NearbyNetUtil.beacon(JSON.stringify(beacon), true);
|
|
2055
2139
|
if (data && this.spot === spot) {
|
|
@@ -2072,16 +2156,7 @@ class NearbyService {
|
|
|
2072
2156
|
}
|
|
2073
2157
|
}
|
|
2074
2158
|
});
|
|
2075
|
-
|
|
2076
|
-
await spot.connect(relay);
|
|
2077
|
-
} catch (error) {
|
|
2078
|
-
throw new Error(error instanceof NearbySpotError ? NearbyTexts.spotRefused(error.message) : NearbyTexts.spotRelay());
|
|
2079
|
-
}
|
|
2080
|
-
this.spot = spot;
|
|
2081
|
-
this.announceSpot(spot);
|
|
2082
|
-
this.spotTimer = setInterval(() => this.announceSpot(spot), this.options.intervalMs ?? 3000);
|
|
2083
|
-
this.spotTimer.unref?.();
|
|
2084
|
-
return this.publish(true);
|
|
2159
|
+
return spot;
|
|
2085
2160
|
}
|
|
2086
2161
|
announceSpot(spot) {
|
|
2087
2162
|
if (this.keys && this.channel) {
|
|
@@ -2102,10 +2177,6 @@ class NearbyService {
|
|
|
2102
2177
|
}
|
|
2103
2178
|
}
|
|
2104
2179
|
}
|
|
2105
|
-
static frame(value) {
|
|
2106
|
-
const frame = value;
|
|
2107
|
-
return frame && frame.v === 1 && [frame.e, frame.x, frame.to, frame.i, frame.c].every((part) => typeof part === "string" && part.length > 0 && part.length < 2000000) ? frame : undefined;
|
|
2108
|
-
}
|
|
2109
2180
|
async makeRoom(people, title, why, meet) {
|
|
2110
2181
|
if (!this.visible) {
|
|
2111
2182
|
throw new Error("Nearby is off.");
|
|
@@ -2121,7 +2192,7 @@ class NearbyService {
|
|
|
2121
2192
|
const invited = [];
|
|
2122
2193
|
const missed = [];
|
|
2123
2194
|
await Promise.all(chosen.map(async (heard) => {
|
|
2124
|
-
const k =
|
|
2195
|
+
const k = randomBytes4(12).toString("base64url");
|
|
2125
2196
|
const who = NearbyTexts.who({ name: heard.data.n, friend: heard.friend });
|
|
2126
2197
|
this.sent.set(k, {
|
|
2127
2198
|
room: room.id,
|
|
@@ -2223,22 +2294,6 @@ class NearbyService {
|
|
|
2223
2294
|
}
|
|
2224
2295
|
]);
|
|
2225
2296
|
}
|
|
2226
|
-
async read(key) {
|
|
2227
|
-
return this.host.getExtensionData(NAME, key).catch(() => {
|
|
2228
|
-
return;
|
|
2229
|
-
});
|
|
2230
|
-
}
|
|
2231
|
-
async publish(force = false) {
|
|
2232
|
-
const status = this.status();
|
|
2233
|
-
const text = JSON.stringify(status);
|
|
2234
|
-
if (force || text !== this.published) {
|
|
2235
|
-
this.published = text;
|
|
2236
|
-
await this.host.setExtensionData(NAME, NearbyKeys2.STATUS, status).catch(() => {
|
|
2237
|
-
return;
|
|
2238
|
-
});
|
|
2239
|
-
}
|
|
2240
|
-
return status;
|
|
2241
|
-
}
|
|
2242
2297
|
}
|
|
2243
2298
|
|
|
2244
2299
|
// web-src/host.ts
|