@scp-dev/broker 0.1.2 → 0.1.4
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.d.ts +45 -114
- package/dist/index.js +200 -529
- package/dist/testing.d.ts +2 -0
- package/dist/testing.js +8 -1
- package/package.json +3 -3
- package/src/bridge.ts +19 -3
- package/src/client.ts +2 -0
- package/src/index.ts +4 -2
- package/src/judge-gate.ts +14 -545
- package/src/server.ts +111 -24
- package/src/sessions.ts +39 -0
- package/src/testing.ts +10 -1
package/dist/testing.d.ts
CHANGED
|
@@ -4,6 +4,8 @@ interface FakeToken {
|
|
|
4
4
|
tokenId: string;
|
|
5
5
|
/** the account handle the token acts as */
|
|
6
6
|
user: string;
|
|
7
|
+
/** the org member the token belongs to; none is the personal account's owner */
|
|
8
|
+
actor?: string;
|
|
7
9
|
}
|
|
8
10
|
interface FakeHubServer {
|
|
9
11
|
/** the http origin, as a bridge's url takes it */
|
package/dist/testing.js
CHANGED
|
@@ -50,7 +50,14 @@ function startFakeHubServer(options) {
|
|
|
50
50
|
if (!token) return null;
|
|
51
51
|
return {
|
|
52
52
|
account: token.user,
|
|
53
|
-
grant: {
|
|
53
|
+
grant: {
|
|
54
|
+
tokenId: token.tokenId,
|
|
55
|
+
userId: `usr_${token.user}`,
|
|
56
|
+
actorId: token.actor ?? null,
|
|
57
|
+
expiresAt: Date.now() + 36e5,
|
|
58
|
+
scoped: false,
|
|
59
|
+
role: token.actor ? "member" : null
|
|
60
|
+
}
|
|
54
61
|
};
|
|
55
62
|
}
|
|
56
63
|
const server = Bun.serve({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scp-dev/broker",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
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.1.
|
|
41
|
-
"@scp-dev/hub": "0.
|
|
40
|
+
"@scp-dev/core": "0.1.3",
|
|
41
|
+
"@scp-dev/hub": "0.3.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"tsup": "^8.5.1"
|
package/src/bridge.ts
CHANGED
|
@@ -73,6 +73,8 @@ export interface Bridge {
|
|
|
73
73
|
changed(endpoint: string): void
|
|
74
74
|
/** rules, caps, groups or a title changed, which can touch several endpoints: every mirrored endpoint is sent again */
|
|
75
75
|
resync(): void
|
|
76
|
+
/** a local endpoint took another handle: the hub renames its mirror in place, keeping what queued for it */
|
|
77
|
+
renamed(endpoint: string, was: string): void
|
|
76
78
|
/** a local endpoint's activity, posted to the hub */
|
|
77
79
|
activity(endpoint: string, row: ActivityRow): void
|
|
78
80
|
/** the endpoints of other accounts this account's contacts let it reach */
|
|
@@ -257,9 +259,10 @@ export function startBridge(options: BridgeOptions): Bridge {
|
|
|
257
259
|
}
|
|
258
260
|
|
|
259
261
|
async function handshake(socket: WebSocket) {
|
|
260
|
-
const hello = await request<{ account: string; endpoints: RemoteView[] }>("hello", { protocol: PROTOCOL, machine, bridge: true })
|
|
262
|
+
const hello = await request<{ account: string; endpoints: RemoteView[]; others?: string[] }>("hello", { protocol: PROTOCOL, machine, bridge: true })
|
|
261
263
|
account = hello.account
|
|
262
264
|
core.setAliases([account])
|
|
265
|
+
core.setOthers(account, Array.isArray(hello.others) ? hello.others.filter((name): name is string => typeof name === "string") : [])
|
|
263
266
|
ready = true
|
|
264
267
|
applyRemote(hello.endpoints, true)
|
|
265
268
|
await Promise.all(fullMirror().map((params) => request("mirror", params)))
|
|
@@ -354,7 +357,7 @@ export function startBridge(options: BridgeOptions): Bridge {
|
|
|
354
357
|
function localAddress(address: unknown): unknown {
|
|
355
358
|
if (typeof address !== "string" || !account || !address.startsWith(`${account}/`)) return address
|
|
356
359
|
const rest = address.slice(account.length + 1)
|
|
357
|
-
return rest.includes("/") ?
|
|
360
|
+
return rest.includes("/") ? core.remoteAddress(rest) : `${core.user}/${account}/${rest}`
|
|
358
361
|
}
|
|
359
362
|
|
|
360
363
|
/** the lowest seq a fresh subscribe must replay from: every delivery at or below it was acked */
|
|
@@ -391,6 +394,7 @@ export function startBridge(options: BridgeOptions): Bridge {
|
|
|
391
394
|
}
|
|
392
395
|
case "endpoints": {
|
|
393
396
|
if (typeof params.machine !== "string" || params.machine === machine) return
|
|
397
|
+
if (typeof params.own === "boolean") core.setOther(params.machine, !params.own)
|
|
394
398
|
try {
|
|
395
399
|
core.mirror(params.machine, params.endpoints, { full: params.full === true })
|
|
396
400
|
} catch (error) {
|
|
@@ -601,6 +605,18 @@ export function startBridge(options: BridgeOptions): Bridge {
|
|
|
601
605
|
if (mirrored(view)) fireAndForget("mirror", { endpoints: [outward(view)], full: false })
|
|
602
606
|
}
|
|
603
607
|
|
|
608
|
+
function renamed(endpoint: string, was: string) {
|
|
609
|
+
if (!ready) return
|
|
610
|
+
let view: EndpointView
|
|
611
|
+
try {
|
|
612
|
+
view = core.whoami(endpoint)
|
|
613
|
+
} catch {
|
|
614
|
+
return
|
|
615
|
+
}
|
|
616
|
+
if (mirrored(view)) fireAndForget("mirror", { endpoints: [{ ...outward(view), was }], full: false })
|
|
617
|
+
else resync()
|
|
618
|
+
}
|
|
619
|
+
|
|
604
620
|
function resync() {
|
|
605
621
|
if (!ready) return
|
|
606
622
|
let frames: ReturnType<typeof fullMirror>
|
|
@@ -654,5 +670,5 @@ export function startBridge(options: BridgeOptions): Bridge {
|
|
|
654
670
|
}
|
|
655
671
|
|
|
656
672
|
connect()
|
|
657
|
-
return { forward, changed, resync, activity, contacts, status, relink, stop }
|
|
673
|
+
return { forward, changed, resync, renamed, activity, contacts, status, relink, stop }
|
|
658
674
|
}
|
package/src/client.ts
CHANGED
|
@@ -31,6 +31,8 @@ export interface Hello {
|
|
|
31
31
|
tty?: boolean
|
|
32
32
|
/** a hook's connection: it claims like an agent but never counts as connected, and posts only event and status */
|
|
33
33
|
transient?: boolean
|
|
34
|
+
/** with `transient`: the session joins the relay on demand, taking `handle` when given, and links itself under the agent `under` */
|
|
35
|
+
join?: { handle?: string; under?: string }
|
|
34
36
|
/** a one-shot command: the broker announces no presence change when it connects or goes */
|
|
35
37
|
brief?: boolean
|
|
36
38
|
/** no identity: list, status, watch, log and policy only */
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export { type BrokerInfo, BrokerUnavailable, connectBroker, type GrantParams, type GroupParams, RelayClient, type Hello, type HelloResult } from "./client"
|
|
2
|
-
export { relaySocketUrl, type Bridge, type BridgeConfig, type BridgeStatus } from "./bridge"
|
|
2
|
+
export { relaySocketUrl, type Bridge, type BridgeConfig, type BridgeHooks, type BridgeStatus } from "./bridge"
|
|
3
3
|
export { BrokerRunning } from "./lock"
|
|
4
4
|
export { MAX_LINE } from "./ndjson"
|
|
5
5
|
export { accountHome, localHostName, policyPath, privateDir, relayDir, shortHostname, socketPath, systemHostname, thisHostname } from "./paths"
|
|
6
|
-
export
|
|
6
|
+
export type { Deliver } from "./judge-gate"
|
|
7
|
+
export { startBroker, type Broker, type BrokerOptions, type LinkContext, type LinkFactory } from "./server"
|
|
8
|
+
export { readSessionMarker, type SessionMarker, writeSessionMarker } from "./sessions"
|