@scp-dev/broker 0.2.1 → 0.4.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/dist/index.js +33 -6
- package/package.json +3 -3
- package/src/server.ts +33 -6
package/dist/index.js
CHANGED
|
@@ -1375,15 +1375,22 @@ async function startBroker(options) {
|
|
|
1375
1375
|
judge.sweep();
|
|
1376
1376
|
}, SWEEP_MS);
|
|
1377
1377
|
const known = /* @__PURE__ */ new Map();
|
|
1378
|
-
|
|
1378
|
+
const wakes = /* @__PURE__ */ new Map();
|
|
1379
|
+
for (const view of localViews()) {
|
|
1380
|
+
known.set(view.id, view.presence);
|
|
1381
|
+
wakes.set(view.id, view.wake);
|
|
1382
|
+
}
|
|
1379
1383
|
const presenceTimer = setInterval(() => {
|
|
1380
1384
|
core.endExited();
|
|
1381
1385
|
const live = /* @__PURE__ */ new Set();
|
|
1382
1386
|
for (const view of localViews()) {
|
|
1383
1387
|
live.add(view.id);
|
|
1384
1388
|
if ((known.get(view.id) ?? "offline") !== view.presence) presenceChanged(view);
|
|
1389
|
+
else if (wakes.has(view.id) && wakes.get(view.id) !== view.wake) bridge?.changed(view.id);
|
|
1390
|
+
wakes.set(view.id, view.wake);
|
|
1385
1391
|
}
|
|
1386
1392
|
for (const id of known.keys()) if (!live.has(id)) known.delete(id);
|
|
1393
|
+
for (const id of wakes.keys()) if (!live.has(id)) wakes.delete(id);
|
|
1387
1394
|
}, options.presenceEveryMs ?? PRESENCE_MS);
|
|
1388
1395
|
const ownership = setInterval(() => {
|
|
1389
1396
|
if (closing || ownsState()) return;
|
|
@@ -1493,9 +1500,15 @@ async function startBroker(options) {
|
|
|
1493
1500
|
const was = s.data.waker;
|
|
1494
1501
|
if (was === endpoint) return;
|
|
1495
1502
|
s.data.waker = endpoint;
|
|
1496
|
-
if (was)
|
|
1497
|
-
|
|
1498
|
-
|
|
1503
|
+
if (was) {
|
|
1504
|
+
const wake2 = core.whoami(was).wake;
|
|
1505
|
+
core.waker(was, false, { grace: s.data.transient === true });
|
|
1506
|
+
if (!presenceMoved(was) && core.whoami(was).wake !== wake2) bridge?.changed(was);
|
|
1507
|
+
}
|
|
1508
|
+
if (endpoint) {
|
|
1509
|
+
core.waker(endpoint, true);
|
|
1510
|
+
presenceMoved(endpoint);
|
|
1511
|
+
}
|
|
1499
1512
|
}
|
|
1500
1513
|
function presenceMoved(id) {
|
|
1501
1514
|
const view = core.whoami(id);
|
|
@@ -1959,14 +1972,26 @@ async function startBroker(options) {
|
|
|
1959
1972
|
}
|
|
1960
1973
|
case "adopt": {
|
|
1961
1974
|
const actor = actorOf(s);
|
|
1962
|
-
const adopted = core.adopt(
|
|
1975
|
+
const adopted = await core.adopt(
|
|
1963
1976
|
actor,
|
|
1964
1977
|
{ endpoint: params.endpoint, ...params.parent !== void 0 ? { parent: params.parent } : {} },
|
|
1965
|
-
{
|
|
1978
|
+
{
|
|
1979
|
+
tty: actor.kind === "endpoint" && s.data.terminal === true,
|
|
1980
|
+
send: (from, send, origin) => {
|
|
1981
|
+
loadPolicy();
|
|
1982
|
+
if (policyError) throw new RelayError3("policy_refused", policyError);
|
|
1983
|
+
return judge.send(from, send, { tty: false, ...origin }, deliver);
|
|
1984
|
+
}
|
|
1985
|
+
}
|
|
1966
1986
|
);
|
|
1967
1987
|
bridge?.resync();
|
|
1968
1988
|
return adopted;
|
|
1969
1989
|
}
|
|
1990
|
+
case "link_accept": {
|
|
1991
|
+
const linked = core.linkAccept(actorOf(s), { id: params.id });
|
|
1992
|
+
bridge?.resync();
|
|
1993
|
+
return linked;
|
|
1994
|
+
}
|
|
1970
1995
|
case "unlink": {
|
|
1971
1996
|
const unlinked = core.unlink(actorOf(s), params.endpoint !== void 0 ? { endpoint: params.endpoint } : {});
|
|
1972
1997
|
bridge?.resync();
|
|
@@ -1986,11 +2011,13 @@ async function startBroker(options) {
|
|
|
1986
2011
|
return view;
|
|
1987
2012
|
}
|
|
1988
2013
|
case "event": {
|
|
2014
|
+
const wake2 = core.whoami(me).wake;
|
|
1989
2015
|
const row = core.event(me, params, hookOf(s));
|
|
1990
2016
|
notifyFleet({ type: "activity", ...row });
|
|
1991
2017
|
bridge?.activity(me, row);
|
|
1992
2018
|
const after = core.whoami(me);
|
|
1993
2019
|
if ((known.get(me) ?? "offline") !== after.presence) presenceChanged(after);
|
|
2020
|
+
else if (after.wake !== wake2) bridge?.changed(me);
|
|
1994
2021
|
return row;
|
|
1995
2022
|
}
|
|
1996
2023
|
case "inject": {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scp-dev/broker",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Swarm Communication Protocol local broker: a unix socket JSON-RPC daemon over the relay core, its client and the bridge to an account hub.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://scp.dev",
|
|
@@ -37,8 +37,8 @@
|
|
|
37
37
|
"typecheck": "tsc -p ."
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@scp-dev/core": "0.
|
|
41
|
-
"@scp-dev/hub": "0.
|
|
40
|
+
"@scp-dev/core": "0.3.0",
|
|
41
|
+
"@scp-dev/hub": "0.6.1"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"tsup": "^8.5.1"
|
package/src/server.ts
CHANGED
|
@@ -292,15 +292,22 @@ export async function startBroker(options: BrokerOptions): Promise<Broker> {
|
|
|
292
292
|
judge.sweep()
|
|
293
293
|
}, SWEEP_MS)
|
|
294
294
|
const known = new Map<string, Presence>()
|
|
295
|
-
|
|
295
|
+
const wakes = new Map<string, string | null | undefined>()
|
|
296
|
+
for (const view of localViews()) {
|
|
297
|
+
known.set(view.id, view.presence)
|
|
298
|
+
wakes.set(view.id, view.wake)
|
|
299
|
+
}
|
|
296
300
|
const presenceTimer = setInterval(() => {
|
|
297
301
|
core.endExited()
|
|
298
302
|
const live = new Set<string>()
|
|
299
303
|
for (const view of localViews()) {
|
|
300
304
|
live.add(view.id)
|
|
301
305
|
if ((known.get(view.id) ?? "offline") !== view.presence) presenceChanged(view)
|
|
306
|
+
else if (wakes.has(view.id) && wakes.get(view.id) !== view.wake) bridge?.changed(view.id)
|
|
307
|
+
wakes.set(view.id, view.wake)
|
|
302
308
|
}
|
|
303
309
|
for (const id of known.keys()) if (!live.has(id)) known.delete(id)
|
|
310
|
+
for (const id of wakes.keys()) if (!live.has(id)) wakes.delete(id)
|
|
304
311
|
}, options.presenceEveryMs ?? PRESENCE_MS)
|
|
305
312
|
const ownership = setInterval(() => {
|
|
306
313
|
if (closing || ownsState()) return
|
|
@@ -432,9 +439,15 @@ export async function startBroker(options: BrokerOptions): Promise<Broker> {
|
|
|
432
439
|
const was = s.data.waker
|
|
433
440
|
if (was === endpoint) return
|
|
434
441
|
s.data.waker = endpoint
|
|
435
|
-
if (was)
|
|
436
|
-
|
|
437
|
-
|
|
442
|
+
if (was) {
|
|
443
|
+
const wake = core.whoami(was).wake
|
|
444
|
+
core.waker(was, false, { grace: s.data.transient === true })
|
|
445
|
+
if (!presenceMoved(was) && core.whoami(was).wake !== wake) bridge?.changed(was)
|
|
446
|
+
}
|
|
447
|
+
if (endpoint) {
|
|
448
|
+
core.waker(endpoint, true)
|
|
449
|
+
presenceMoved(endpoint)
|
|
450
|
+
}
|
|
438
451
|
}
|
|
439
452
|
|
|
440
453
|
/** reports an endpoint's presence when it is not the one last reported; whether it was not */
|
|
@@ -949,14 +962,26 @@ export async function startBroker(options: BrokerOptions): Promise<Broker> {
|
|
|
949
962
|
}
|
|
950
963
|
case "adopt": {
|
|
951
964
|
const actor = actorOf(s)
|
|
952
|
-
const adopted = core.adopt(
|
|
965
|
+
const adopted = await core.adopt(
|
|
953
966
|
actor,
|
|
954
967
|
{ endpoint: params.endpoint, ...(params.parent !== undefined ? { parent: params.parent } : {}) },
|
|
955
|
-
{
|
|
968
|
+
{
|
|
969
|
+
tty: actor.kind === "endpoint" && s.data.terminal === true,
|
|
970
|
+
send: (from: string, send: SendParams, origin: { link: "session" | "self" }) => {
|
|
971
|
+
loadPolicy()
|
|
972
|
+
if (policyError) throw new RelayError("policy_refused", policyError)
|
|
973
|
+
return judge.send(from, send, { tty: false, ...origin }, deliver)
|
|
974
|
+
},
|
|
975
|
+
},
|
|
956
976
|
)
|
|
957
977
|
bridge?.resync()
|
|
958
978
|
return adopted
|
|
959
979
|
}
|
|
980
|
+
case "link_accept": {
|
|
981
|
+
const linked = core.linkAccept(actorOf(s), { id: params.id })
|
|
982
|
+
bridge?.resync()
|
|
983
|
+
return linked
|
|
984
|
+
}
|
|
960
985
|
case "unlink": {
|
|
961
986
|
const unlinked = core.unlink(actorOf(s), params.endpoint !== undefined ? { endpoint: params.endpoint } : {})
|
|
962
987
|
bridge?.resync()
|
|
@@ -976,11 +1001,13 @@ export async function startBroker(options: BrokerOptions): Promise<Broker> {
|
|
|
976
1001
|
return view
|
|
977
1002
|
}
|
|
978
1003
|
case "event": {
|
|
1004
|
+
const wake = core.whoami(me).wake
|
|
979
1005
|
const row = core.event(me, params, hookOf(s))
|
|
980
1006
|
notifyFleet({ type: "activity", ...row })
|
|
981
1007
|
bridge?.activity(me, row)
|
|
982
1008
|
const after = core.whoami(me)
|
|
983
1009
|
if ((known.get(me) ?? "offline") !== after.presence) presenceChanged(after)
|
|
1010
|
+
else if (after.wake !== wake) bridge?.changed(me)
|
|
984
1011
|
return row
|
|
985
1012
|
}
|
|
986
1013
|
case "inject": {
|