@log0u7/opencode-mesh 0.7.0 → 0.8.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/CHANGELOG.md +41 -1
- package/README.md +39 -7
- package/dist/lib/client.d.ts +6 -0
- package/dist/lib/client.d.ts.map +1 -1
- package/dist/lib/client.js +3 -0
- package/dist/lib/client.js.map +1 -1
- package/dist/lib/db.d.ts.map +1 -1
- package/dist/lib/db.js +35 -1
- package/dist/lib/db.js.map +1 -1
- package/dist/lib/mail.d.ts +57 -0
- package/dist/lib/mail.d.ts.map +1 -1
- package/dist/lib/mail.js +210 -1
- package/dist/lib/mail.js.map +1 -1
- package/dist/lib/serve.d.ts.map +1 -1
- package/dist/lib/serve.js +40 -3
- package/dist/lib/serve.js.map +1 -1
- package/dist/lib/wake.d.ts +20 -0
- package/dist/lib/wake.d.ts.map +1 -0
- package/dist/lib/wake.js +50 -0
- package/dist/lib/wake.js.map +1 -0
- package/dist/options.d.ts +2 -0
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +6 -0
- package/dist/options.js.map +1 -1
- package/dist/plugin-v2.d.ts.map +1 -1
- package/dist/plugin-v2.js +21 -0
- package/dist/plugin-v2.js.map +1 -1
- package/dist/sidebar-state.d.ts +17 -0
- package/dist/sidebar-state.d.ts.map +1 -0
- package/dist/sidebar-state.js +25 -0
- package/dist/sidebar-state.js.map +1 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +178 -25
- package/dist/tools.js.map +1 -1
- package/dist/tui.js +5 -6
- package/dist/tui.tsx +5 -4
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,46 @@ All notable changes to this project are documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.8.0] - 2026-11-22
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Named mailboxes: `mesh_register(name, role?)` binds a session to a mailbox;
|
|
13
|
+
`mesh_send(to=...)` routes locally by mailbox name; `mesh_inbox` reads only
|
|
14
|
+
the caller's box. Ends the shared-inbox cross-drain where the first session
|
|
15
|
+
to run consumed every message on the node.
|
|
16
|
+
- Pool broadcast: `mesh_join` / `mesh_leave` / `mesh_pools` manage persisted
|
|
17
|
+
pool membership; `mesh_send(to="team:<pool>")` delivers one copy per member
|
|
18
|
+
(sender excluded), `to="all"` reaches every registered mailbox. Per-member
|
|
19
|
+
delivery rows mean each recipient acks independently.
|
|
20
|
+
- Request/response: `mesh_wait(id, timeout)` blocks the agent turn until a
|
|
21
|
+
message with `reply_to: <id>` lands, then returns it acked.
|
|
22
|
+
- Wake (OpenCode 2.x): the plugin watches the session's mailbox (5s poll,
|
|
23
|
+
60s cooldown) and injects a prompt via `ctx.session.prompt` when unread mail
|
|
24
|
+
arrives, so an idle agent reacts to incoming mail. Disable with `wake: false`.
|
|
25
|
+
- Presence: mailboxes carry role, pools and `last_seen`; `mesh_status` shows
|
|
26
|
+
the age of every registered box.
|
|
27
|
+
- Remote mailbox addressing: `POST /mail` accepts optional `to`, `from_box`,
|
|
28
|
+
`reply_to`; an unknown `to` falls back to the node's global inbox (old
|
|
29
|
+
receivers simply ignore the fields).
|
|
30
|
+
- Retention: messages older than 7 days are swept at server boot.
|
|
31
|
+
- Schema v3: `mailboxes`, `deliveries` tables; `messages.from_box` /
|
|
32
|
+
`messages.reply_to`; migration from v2 in place.
|
|
33
|
+
|
|
34
|
+
### Fixed
|
|
35
|
+
|
|
36
|
+
- `mesh_send`/`mesh_inbox` tolerate a missing session context (tests, exotic
|
|
37
|
+
hosts) instead of failing on a SQLite bind with `undefined`.
|
|
38
|
+
|
|
39
|
+
## [0.7.1] - 2026-11-07
|
|
40
|
+
|
|
41
|
+
### Fixed
|
|
42
|
+
|
|
43
|
+
- The TUI no longer crashes on hosts whose storage layer rejects scoped npm
|
|
44
|
+
plugin ids (`TypeError: Invalid storage segment`, anomalyco/opencode#62634).
|
|
45
|
+
The sidebar falls back to in-memory collapse state: functional, but not
|
|
46
|
+
persisted across TUI restarts until the host-side fix lands.
|
|
47
|
+
|
|
8
48
|
## [0.7.0] - 2026-11-06
|
|
9
49
|
|
|
10
50
|
### Added
|
|
@@ -40,7 +80,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
40
80
|
|
|
41
81
|
- TUI plugin (phase 0+1): toast, `/mesh` slash command, sidebar slot.
|
|
42
82
|
|
|
43
|
-
## [
|
|
83
|
+
## [0.8.0] - 2026-11-22
|
|
44
84
|
|
|
45
85
|
## [0.4.1] - 2026-10-24
|
|
46
86
|
|
package/README.md
CHANGED
|
@@ -91,22 +91,54 @@ Note (npm 12): the package ships no lifecycle install scripts and needs no `allo
|
|
|
91
91
|
Same machine: start two OpenCode instances in the same repository.
|
|
92
92
|
|
|
93
93
|
```
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
mesh_send(
|
|
97
|
-
mesh_inbox() #
|
|
94
|
+
mesh_register(name="purser", role="manager") # claim a named mailbox for this session
|
|
95
|
+
mesh_send(to="quartermaster", subject="r8 go", body="...") # targeted mail (this machine)
|
|
96
|
+
mesh_send(to="team:quartermaster", subject="status", body="...") # pool broadcast
|
|
97
|
+
mesh_inbox() # only YOUR mailbox (plus legacy global mail), ack
|
|
98
|
+
mesh_wait(id="...", timeout=120) # block until a reply_to lands, then return it
|
|
99
|
+
mesh_join(pool="team:quartermaster") / mesh_leave(pool="...") / mesh_pools()
|
|
98
100
|
mesh_lock(path="src/api.ts", ttl=300) # claim a file before editing (this machine only)
|
|
99
101
|
mesh_unlock(path="src/api.ts")
|
|
100
102
|
```
|
|
101
103
|
|
|
104
|
+
### Mailboxes and pools (multi-session routing)
|
|
105
|
+
|
|
106
|
+
All sessions on one machine used to share a single mailbox, so every session
|
|
107
|
+
draining the inbox ate everyone's messages. Mailboxes fix the routing:
|
|
108
|
+
|
|
109
|
+
- `mesh_register(name, role?)` binds this session to a named mailbox. One box
|
|
110
|
+
per session; re-registering the same name from another session is a handoff.
|
|
111
|
+
- `mesh_send(to=...)` routes locally by mailbox name, or broadcasts with
|
|
112
|
+
`team:<pool>` (members of a pool) or `all` (every registered box except the
|
|
113
|
+
sender). `peer=...` still sends to a remote node; `to` is honored on the
|
|
114
|
+
receiving node when its protocol supports it, otherwise the mail falls back
|
|
115
|
+
to that node's global inbox.
|
|
116
|
+
- `mesh_inbox` reads only your box (plus legacy global mail), ack per message.
|
|
117
|
+
- Pools are dynamic and persisted server-side: `mesh_join` / `mesh_leave`, and
|
|
118
|
+
the membership survives session restarts (a replacement session
|
|
119
|
+
re-registering the same name finds its pools intact).
|
|
120
|
+
- `mesh_wait(id, timeout)` completes a request/response within one turn: it
|
|
121
|
+
blocks the agent turn until a message with `reply_to: id` arrives.
|
|
122
|
+
- Presence: every tool interaction refreshes the mailbox `last_seen`;
|
|
123
|
+
`mesh_status` lists mailboxes with role, pools and age.
|
|
124
|
+
- Wake (OpenCode 2.x): a plugin-side loop watches your mailbox and injects a
|
|
125
|
+
prompt into the session when unread mail arrives, so an idle agent reacts
|
|
126
|
+
without a human relaying the message. Disable with `wake: false` in the
|
|
127
|
+
plugin options.
|
|
128
|
+
- Retention: messages older than 7 days are swept at server boot.
|
|
129
|
+
|
|
102
130
|
### Tools
|
|
103
131
|
|
|
104
132
|
| Tool | Args | Description |
|
|
105
133
|
|---|---|---|
|
|
106
|
-
| `mesh_status` | | Local session registry,
|
|
134
|
+
| `mesh_status` | | Local session registry, named mailboxes (role, pools, presence), paired peers, active locks. |
|
|
135
|
+
| `mesh_register` | `name`, `role?` | Bind this session to a named mailbox (one box per session, re-register = handoff). |
|
|
107
136
|
| `mesh_pair` | `node_id?`, `token?`, `address?` | No args: emit a pairing offer (5 min ttl). With args: complete the peer's offer. |
|
|
108
|
-
| `mesh_send` | `peer
|
|
109
|
-
| `mesh_inbox` | `ack?` | Fetch unread messages; ack marks them read. |
|
|
137
|
+
| `mesh_send` | `peer?`, `to?`, `subject`, `body`, `reply_to?` | Deliver mail: `to` a mailbox / `team:<pool>` / `all` locally, and/or `peer` on another node. |
|
|
138
|
+
| `mesh_inbox` | `ack?` | Fetch unread messages addressed to this session's mailbox; ack marks them read. |
|
|
139
|
+
| `mesh_wait` | `id`, `timeout?` | Block until a reply (`reply_to`) to message `id` arrives, return it. |
|
|
140
|
+
| `mesh_join` / `mesh_leave` | `pool` | Add / remove this mailbox from a broadcast pool. |
|
|
141
|
+
| `mesh_pools` | | List pools with their member mailboxes. |
|
|
110
142
|
| `mesh_lock` | `path`, `ttl?` | Exclusive claim on a file or path, enforced only between sessions on this machine (default ttl 600s). Not visible to remote peers. |
|
|
111
143
|
| `mesh_unlock` | `path` | Release a claim. |
|
|
112
144
|
|
package/dist/lib/client.d.ts
CHANGED
|
@@ -4,6 +4,12 @@ export type SendTarget = {
|
|
|
4
4
|
from: string;
|
|
5
5
|
subject: string;
|
|
6
6
|
body: string;
|
|
7
|
+
/** Target mailbox on the peer, when the mail is addressed to a box. */
|
|
8
|
+
to?: string;
|
|
9
|
+
/** The sending node's own mailbox name, when it has one. */
|
|
10
|
+
from_box?: string;
|
|
11
|
+
/** Message id this mail replies to. */
|
|
12
|
+
reply_to?: string;
|
|
7
13
|
/** `<base64 spki>.<base64 ed25519 signature>` over node_id:token. */
|
|
8
14
|
proof?: string;
|
|
9
15
|
};
|
package/dist/lib/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/lib/client.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,UAAU,GAAG;IACvB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,qEAAqE;IACrE,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,IAAI,GAAG,aAAa,GAAG,cAAc,GAAG,OAAO,CAAC;AAIzE,wBAAsB,QAAQ,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/lib/client.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,UAAU,GAAG;IACvB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,uEAAuE;IACvE,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,4DAA4D;IAC5D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,uCAAuC;IACvC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,qEAAqE;IACrE,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,IAAI,GAAG,aAAa,GAAG,cAAc,GAAG,OAAO,CAAC;AAIzE,wBAAsB,QAAQ,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CA6BtE"}
|
package/dist/lib/client.js
CHANGED
|
@@ -12,6 +12,9 @@ export async function sendMail(target) {
|
|
|
12
12
|
from: target.from,
|
|
13
13
|
subject: target.subject,
|
|
14
14
|
body: target.body,
|
|
15
|
+
...(target.to !== undefined ? { to: target.to } : {}),
|
|
16
|
+
...(target.from_box !== undefined ? { from_box: target.from_box } : {}),
|
|
17
|
+
...(target.reply_to !== undefined ? { reply_to: target.reply_to } : {}),
|
|
15
18
|
...(target.proof !== undefined ? { proof: target.proof } : {}),
|
|
16
19
|
}),
|
|
17
20
|
signal: AbortSignal.timeout(5000),
|
package/dist/lib/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/lib/client.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/lib/client.ts"],"names":[],"mappings":"AAkBA,8EAA8E;AAC9E,gCAAgC;AAChC,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,MAAkB;IAC/C,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,MAAM,CAAC,GAAG,OAAO,EAAE;YACjD,MAAM,EAAE,MAAM;YACd,OAAO,EAAE;gBACP,cAAc,EAAE,kBAAkB;gBAClC,aAAa,EAAE,UAAU,MAAM,CAAC,KAAK,EAAE;aACxC;YACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC;gBACnB,IAAI,EAAE,MAAM,CAAC,IAAI;gBACjB,OAAO,EAAE,MAAM,CAAC,OAAO;gBACvB,IAAI,EAAE,MAAM,CAAC,IAAI;gBACjB,GAAG,CAAC,MAAM,CAAC,EAAE,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBACrD,GAAG,CAAC,MAAM,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBACvE,GAAG,CAAC,MAAM,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBACvE,GAAG,CAAC,MAAM,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aAC/D,CAAC;YACF,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC;SAClC,CAAC,CAAC;QACH,IAAI,QAAQ,CAAC,EAAE,EAAE,CAAC;YAChB,OAAO,IAAI,CAAC;QACd,CAAC;QACD,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;YAC5B,OAAO,cAAc,CAAC;QACxB,CAAC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,aAAa,CAAC;IACvB,CAAC;AACH,CAAC"}
|
package/dist/lib/db.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"db.d.ts","sourceRoot":"","sources":["../../src/lib/db.ts"],"names":[],"mappings":"AAGA,OAAO,EAAc,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAE1D,MAAM,MAAM,MAAM,GAAG;IACnB,IAAI,EAAE,UAAU,CAAC;IACjB,KAAK,IAAI,IAAI,CAAC;CACf,CAAC;
|
|
1
|
+
{"version":3,"file":"db.d.ts","sourceRoot":"","sources":["../../src/lib/db.ts"],"names":[],"mappings":"AAGA,OAAO,EAAc,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAE1D,MAAM,MAAM,MAAM,GAAG;IACnB,IAAI,EAAE,UAAU,CAAC;IACjB,KAAK,IAAI,IAAI,CAAC;CACf,CAAC;AA0GF,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAW/C"}
|
package/dist/lib/db.js
CHANGED
|
@@ -3,7 +3,7 @@ import { dirname } from "node:path";
|
|
|
3
3
|
import { openSqlite } from "./sqlite.js";
|
|
4
4
|
// Bump when a schema change needs a migration step; the version anchors future
|
|
5
5
|
// migrations that cannot be expressed as idempotent `CREATE ... IF NOT EXISTS`.
|
|
6
|
-
const SCHEMA_VERSION =
|
|
6
|
+
const SCHEMA_VERSION = 3;
|
|
7
7
|
const SCHEMA = `
|
|
8
8
|
CREATE TABLE IF NOT EXISTS peers (
|
|
9
9
|
node_id TEXT PRIMARY KEY,
|
|
@@ -18,8 +18,25 @@ CREATE TABLE IF NOT EXISTS peers (
|
|
|
18
18
|
CREATE TABLE IF NOT EXISTS messages (
|
|
19
19
|
id TEXT PRIMARY KEY,
|
|
20
20
|
from_node TEXT NOT NULL,
|
|
21
|
+
from_box TEXT,
|
|
21
22
|
subject TEXT NOT NULL,
|
|
22
23
|
body TEXT NOT NULL,
|
|
24
|
+
reply_to TEXT,
|
|
25
|
+
status TEXT NOT NULL DEFAULT 'unread',
|
|
26
|
+
created_at INTEGER NOT NULL
|
|
27
|
+
);
|
|
28
|
+
CREATE TABLE IF NOT EXISTS mailboxes (
|
|
29
|
+
name TEXT PRIMARY KEY,
|
|
30
|
+
session_id TEXT NOT NULL UNIQUE,
|
|
31
|
+
role TEXT,
|
|
32
|
+
pools TEXT NOT NULL DEFAULT '[]',
|
|
33
|
+
last_seen INTEGER NOT NULL,
|
|
34
|
+
created_at INTEGER NOT NULL
|
|
35
|
+
);
|
|
36
|
+
CREATE TABLE IF NOT EXISTS deliveries (
|
|
37
|
+
id TEXT PRIMARY KEY,
|
|
38
|
+
message_id TEXT NOT NULL,
|
|
39
|
+
to_box TEXT NOT NULL,
|
|
23
40
|
status TEXT NOT NULL DEFAULT 'unread',
|
|
24
41
|
created_at INTEGER NOT NULL
|
|
25
42
|
);
|
|
@@ -51,6 +68,11 @@ CREATE INDEX IF NOT EXISTS idx_peers_token ON peers(token);
|
|
|
51
68
|
CREATE INDEX IF NOT EXISTS idx_workers_status ON workers(status);
|
|
52
69
|
CREATE INDEX IF NOT EXISTS idx_locks_expires ON locks(expires_at);
|
|
53
70
|
`;
|
|
71
|
+
const INDEXES_V3 = `
|
|
72
|
+
CREATE INDEX IF NOT EXISTS idx_messages_reply_to ON messages(reply_to);
|
|
73
|
+
CREATE INDEX IF NOT EXISTS idx_deliveries_box_status ON deliveries(to_box, status);
|
|
74
|
+
CREATE INDEX IF NOT EXISTS idx_deliveries_message ON deliveries(message_id);
|
|
75
|
+
`;
|
|
54
76
|
function migrate(conn) {
|
|
55
77
|
const current = conn.get("PRAGMA user_version")?.user_version ?? 0;
|
|
56
78
|
if (current >= SCHEMA_VERSION) {
|
|
@@ -65,6 +87,18 @@ function migrate(conn) {
|
|
|
65
87
|
if (!peerColumns.includes("public_key")) {
|
|
66
88
|
conn.exec("ALTER TABLE peers ADD COLUMN public_key TEXT");
|
|
67
89
|
}
|
|
90
|
+
// v3: named mailboxes. messages gains from_box/reply_to; guarded ALTERs for
|
|
91
|
+
// databases created before v3 (fresh installs already have the columns).
|
|
92
|
+
const messageColumns = conn
|
|
93
|
+
.all("PRAGMA table_info(messages)")
|
|
94
|
+
.map((row) => row.name);
|
|
95
|
+
if (!messageColumns.includes("from_box")) {
|
|
96
|
+
conn.exec("ALTER TABLE messages ADD COLUMN from_box TEXT");
|
|
97
|
+
}
|
|
98
|
+
if (!messageColumns.includes("reply_to")) {
|
|
99
|
+
conn.exec("ALTER TABLE messages ADD COLUMN reply_to TEXT");
|
|
100
|
+
}
|
|
101
|
+
conn.exec(INDEXES_V3);
|
|
68
102
|
conn.run(`PRAGMA user_version = ${SCHEMA_VERSION}`);
|
|
69
103
|
}
|
|
70
104
|
export function openMeshDb(path) {
|
package/dist/lib/db.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"db.js","sourceRoot":"","sources":["../../src/lib/db.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,EAAE,UAAU,EAAmB,MAAM,aAAa,CAAC;AAO1D,+EAA+E;AAC/E,gFAAgF;AAChF,MAAM,cAAc,GAAG,CAAC,CAAC;AAEzB,MAAM,MAAM,GAAG
|
|
1
|
+
{"version":3,"file":"db.js","sourceRoot":"","sources":["../../src/lib/db.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAA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QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC;QACzC,IAAI,CAAC,IAAI,CAAC,+CAA+C,CAAC,CAAC;IAC7D,CAAC;IACD,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACtB,IAAI,CAAC,GAAG,CAAC,yBAAyB,cAAc,EAAE,CAAC,CAAC;AACtD,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,IAAY;IACrC,IAAI,IAAI,KAAK,UAAU,EAAE,CAAC;QACxB,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAChD,CAAC;IAED,MAAM,IAAI,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC;IAC9B,IAAI,CAAC,GAAG,CAAC,2BAA2B,CAAC,CAAC;IACtC,IAAI,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC;IACvC,OAAO,CAAC,IAAI,CAAC,CAAC;IAEd,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,EAAE,CAAC;AAC7C,CAAC"}
|
package/dist/lib/mail.d.ts
CHANGED
|
@@ -1,11 +1,68 @@
|
|
|
1
1
|
import type { MeshDb } from "./db.js";
|
|
2
2
|
import type { MailMessage } from "../types/peer.js";
|
|
3
|
+
export type Mailbox = {
|
|
4
|
+
name: string;
|
|
5
|
+
session_id: string;
|
|
6
|
+
role: string | null;
|
|
7
|
+
pools: string[];
|
|
8
|
+
last_seen: number;
|
|
9
|
+
};
|
|
10
|
+
export type RegisterMailboxInput = {
|
|
11
|
+
name: string;
|
|
12
|
+
session_id: string;
|
|
13
|
+
role?: string | null;
|
|
14
|
+
now: number;
|
|
15
|
+
};
|
|
16
|
+
export type DeliverInput = {
|
|
17
|
+
from: string;
|
|
18
|
+
from_box?: string | null;
|
|
19
|
+
to_box?: string | null;
|
|
20
|
+
subject: string;
|
|
21
|
+
body: string;
|
|
22
|
+
reply_to?: string | null;
|
|
23
|
+
created_at?: number;
|
|
24
|
+
};
|
|
25
|
+
export type DeliverResult = {
|
|
26
|
+
ok: true;
|
|
27
|
+
id: string;
|
|
28
|
+
recipients: string[];
|
|
29
|
+
} | {
|
|
30
|
+
ok: false;
|
|
31
|
+
error: "unknown_mailbox" | "empty_broadcast";
|
|
32
|
+
};
|
|
33
|
+
export type InboxRow = {
|
|
34
|
+
delivery_id: string;
|
|
35
|
+
id: string;
|
|
36
|
+
from: string;
|
|
37
|
+
from_box: string | null;
|
|
38
|
+
subject: string;
|
|
39
|
+
body: string;
|
|
40
|
+
reply_to: string | null;
|
|
41
|
+
created_at: number;
|
|
42
|
+
};
|
|
43
|
+
export declare const MESSAGE_TTL_MS: number;
|
|
44
|
+
export declare function registerMailbox(db: MeshDb, input: RegisterMailboxInput): Mailbox;
|
|
45
|
+
export declare function mailboxBySession(db: MeshDb, session_id: string): Mailbox | null;
|
|
46
|
+
export declare function mailboxByName(db: MeshDb, name: string): Mailbox | null;
|
|
47
|
+
export declare function listMailboxes(db: MeshDb): Mailbox[];
|
|
48
|
+
export declare function touchMailbox(db: MeshDb, session_id: string, now: number): void;
|
|
49
|
+
export declare function joinPool(db: MeshDb, name: string, pool: string): void;
|
|
50
|
+
export declare function leavePool(db: MeshDb, name: string, pool: string): void;
|
|
51
|
+
export declare function deliverMessage(db: MeshDb, input: DeliverInput): DeliverResult;
|
|
52
|
+
export declare function inboxRows(db: MeshDb, session_id: string): InboxRow[];
|
|
53
|
+
export declare function firstReply(db: MeshDb, reply_to: string, session_id: string): InboxRow | null;
|
|
54
|
+
export declare function ackInbox(db: MeshDb, delivery_ids: string[]): void;
|
|
55
|
+
export declare function ackReply(db: MeshDb, row: InboxRow): void;
|
|
3
56
|
export declare function receiveMessage(db: MeshDb, input: {
|
|
4
57
|
from: string;
|
|
5
58
|
subject: string;
|
|
6
59
|
body: string;
|
|
60
|
+
reply_to?: string;
|
|
61
|
+
from_box?: string;
|
|
7
62
|
}): MailMessage;
|
|
8
63
|
export declare function unreadMessages(db: MeshDb): MailMessage[];
|
|
9
64
|
export declare function unreadCount(db: MeshDb): number;
|
|
65
|
+
export declare function unreadBoxCount(db: MeshDb, box: string): number;
|
|
10
66
|
export declare function ackMessages(db: MeshDb, ids: string[]): void;
|
|
67
|
+
export declare function purgeExpired(db: MeshDb, ttl_ms: number, now: number): void;
|
|
11
68
|
//# sourceMappingURL=mail.d.ts.map
|
package/dist/lib/mail.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mail.d.ts","sourceRoot":"","sources":["../../src/lib/mail.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACtC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"mail.d.ts","sourceRoot":"","sources":["../../src/lib/mail.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACtC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAQpD,MAAM,MAAM,OAAO,GAAG;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,MAAM,CAAC,EAAE,MAAM,G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MAAM,EAAE,GAAG,IAAI,CAI3D;AAID,wBAAgB,YAAY,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,CAO1E"}
|
package/dist/lib/mail.js
CHANGED
|
@@ -1,8 +1,208 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
+
// 7 days: read or not, a message older than this is swept at server boot.
|
|
3
|
+
export const MESSAGE_TTL_MS = 7 * 24 * 60 * 60 * 1000;
|
|
4
|
+
function parsePools(raw) {
|
|
5
|
+
try {
|
|
6
|
+
const parsed = JSON.parse(raw);
|
|
7
|
+
return Array.isArray(parsed) ? parsed.filter((p) => typeof p === "string") : [];
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return [];
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function rowToMailbox(row) {
|
|
14
|
+
return {
|
|
15
|
+
name: row.name,
|
|
16
|
+
session_id: row.session_id,
|
|
17
|
+
role: row.role ?? null,
|
|
18
|
+
pools: parsePools(row.pools),
|
|
19
|
+
last_seen: row.last_seen,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
// ---- registration and presence ----
|
|
23
|
+
export function registerMailbox(db, input) {
|
|
24
|
+
// One box per session: registering under a new name releases the old one.
|
|
25
|
+
// Re-registering an existing name rebinds it to this session (handoff).
|
|
26
|
+
db.conn.run("DELETE FROM mailboxes WHERE session_id = ? AND name != ?", [
|
|
27
|
+
input.session_id,
|
|
28
|
+
input.name,
|
|
29
|
+
]);
|
|
30
|
+
db.conn.run(`INSERT INTO mailboxes (name, session_id, role, pools, last_seen, created_at)
|
|
31
|
+
VALUES (?, ?, ?, '[]', ?, ?)
|
|
32
|
+
ON CONFLICT(name) DO UPDATE SET session_id = excluded.session_id,
|
|
33
|
+
role = excluded.role, last_seen = excluded.last_seen`, [input.name, input.session_id, input.role ?? null, input.now, input.now]);
|
|
34
|
+
const row = db.conn.get("SELECT * FROM mailboxes WHERE name = ?", [input.name]);
|
|
35
|
+
if (!row) {
|
|
36
|
+
throw new Error("registerMailbox: row missing after upsert");
|
|
37
|
+
}
|
|
38
|
+
return rowToMailbox(row);
|
|
39
|
+
}
|
|
40
|
+
export function mailboxBySession(db, session_id) {
|
|
41
|
+
const row = db.conn.get("SELECT * FROM mailboxes WHERE session_id = ?", [session_id]);
|
|
42
|
+
return row ? rowToMailbox(row) : null;
|
|
43
|
+
}
|
|
44
|
+
export function mailboxByName(db, name) {
|
|
45
|
+
const row = db.conn.get("SELECT * FROM mailboxes WHERE name = ?", [name]);
|
|
46
|
+
return row ? rowToMailbox(row) : null;
|
|
47
|
+
}
|
|
48
|
+
export function listMailboxes(db) {
|
|
49
|
+
return db.conn
|
|
50
|
+
.all("SELECT * FROM mailboxes ORDER BY created_at ASC")
|
|
51
|
+
.map(rowToMailbox);
|
|
52
|
+
}
|
|
53
|
+
export function touchMailbox(db, session_id, now) {
|
|
54
|
+
db.conn.run("UPDATE mailboxes SET last_seen = ? WHERE session_id = ?", [now, session_id]);
|
|
55
|
+
}
|
|
56
|
+
export function joinPool(db, name, pool) {
|
|
57
|
+
const box = mailboxByName(db, name);
|
|
58
|
+
if (!box)
|
|
59
|
+
return;
|
|
60
|
+
if (box.pools.includes(pool))
|
|
61
|
+
return;
|
|
62
|
+
db.conn.run("UPDATE mailboxes SET pools = ? WHERE name = ?", [
|
|
63
|
+
JSON.stringify([...box.pools, pool]),
|
|
64
|
+
name,
|
|
65
|
+
]);
|
|
66
|
+
}
|
|
67
|
+
export function leavePool(db, name, pool) {
|
|
68
|
+
const box = mailboxByName(db, name);
|
|
69
|
+
if (!box)
|
|
70
|
+
return;
|
|
71
|
+
if (!box.pools.includes(pool))
|
|
72
|
+
return;
|
|
73
|
+
db.conn.run("UPDATE mailboxes SET pools = ? WHERE name = ?", [
|
|
74
|
+
JSON.stringify(box.pools.filter((p) => p !== pool)),
|
|
75
|
+
name,
|
|
76
|
+
]);
|
|
77
|
+
}
|
|
78
|
+
// ---- delivery ----
|
|
79
|
+
export function deliverMessage(db, input) {
|
|
80
|
+
const id = randomUUID();
|
|
81
|
+
const created_at = input.created_at ?? Date.now();
|
|
82
|
+
// Resolve first: a legacy global mail or a failure never leaves a message row.
|
|
83
|
+
const global = input.to_box == null;
|
|
84
|
+
let recipients;
|
|
85
|
+
if (global) {
|
|
86
|
+
recipients = [];
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
const resolved = resolveRecipients(db, input);
|
|
90
|
+
if (resolved === null) {
|
|
91
|
+
return { ok: false, error: "unknown_mailbox" };
|
|
92
|
+
}
|
|
93
|
+
if (resolved.length === 0) {
|
|
94
|
+
return { ok: false, error: "empty_broadcast" };
|
|
95
|
+
}
|
|
96
|
+
recipients = resolved;
|
|
97
|
+
}
|
|
98
|
+
db.conn.run(`INSERT INTO messages (id, from_node, from_box, subject, body, reply_to, status, created_at)
|
|
99
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, [
|
|
100
|
+
id,
|
|
101
|
+
input.from,
|
|
102
|
+
input.from_box ?? null,
|
|
103
|
+
input.subject,
|
|
104
|
+
input.body,
|
|
105
|
+
input.reply_to ?? null,
|
|
106
|
+
global ? "unread" : "delivered",
|
|
107
|
+
created_at,
|
|
108
|
+
]);
|
|
109
|
+
for (const box of recipients) {
|
|
110
|
+
db.conn.run("INSERT INTO deliveries (id, message_id, to_box, status, created_at) VALUES (?, ?, ?, 'unread', ?)", [randomUUID(), id, box, created_at]);
|
|
111
|
+
}
|
|
112
|
+
return { ok: true, id, recipients };
|
|
113
|
+
}
|
|
114
|
+
// null = unknown single mailbox. Callers guarantee to_box is set.
|
|
115
|
+
function resolveRecipients(db, input) {
|
|
116
|
+
const target = input.to_box ?? "";
|
|
117
|
+
if (target === "all") {
|
|
118
|
+
return listMailboxes(db)
|
|
119
|
+
.map((b) => b.name)
|
|
120
|
+
.filter((name) => name !== input.from_box);
|
|
121
|
+
}
|
|
122
|
+
if (target.startsWith("team:")) {
|
|
123
|
+
// Pools are stored under their full "team:<name>" id, matching mesh_join.
|
|
124
|
+
return listMailboxes(db)
|
|
125
|
+
.filter((b) => b.pools.includes(target) && b.name !== input.from_box)
|
|
126
|
+
.map((b) => b.name);
|
|
127
|
+
}
|
|
128
|
+
const box = mailboxByName(db, target);
|
|
129
|
+
return box ? [box.name] : null;
|
|
130
|
+
}
|
|
131
|
+
// ---- reading ----
|
|
132
|
+
function deliveryRows(db, box) {
|
|
133
|
+
return db.conn
|
|
134
|
+
.all(`SELECT d.id AS delivery_id, m.id AS m_id, m.from_node AS m_from, m.from_box AS m_from_box,
|
|
135
|
+
m.subject AS m_subject, m.body AS m_body, m.reply_to AS m_reply_to,
|
|
136
|
+
m.created_at AS m_created
|
|
137
|
+
FROM deliveries d JOIN messages m ON m.id = d.message_id
|
|
138
|
+
WHERE d.to_box = ? AND d.status = 'unread'
|
|
139
|
+
ORDER BY d.created_at ASC`, [box])
|
|
140
|
+
.map((row) => ({
|
|
141
|
+
delivery_id: row.delivery_id,
|
|
142
|
+
id: row.m_id,
|
|
143
|
+
from: row.m_from,
|
|
144
|
+
from_box: row.m_from_box,
|
|
145
|
+
subject: row.m_subject,
|
|
146
|
+
body: row.m_body,
|
|
147
|
+
reply_to: row.m_reply_to,
|
|
148
|
+
created_at: row.m_created,
|
|
149
|
+
}));
|
|
150
|
+
}
|
|
151
|
+
function globalRows(db) {
|
|
152
|
+
return db.conn
|
|
153
|
+
.all("SELECT * FROM messages WHERE status = 'unread' ORDER BY created_at ASC")
|
|
154
|
+
.map((row) => ({
|
|
155
|
+
delivery_id: "",
|
|
156
|
+
id: row.id,
|
|
157
|
+
from: row.from_node,
|
|
158
|
+
from_box: row.from_box,
|
|
159
|
+
subject: row.subject,
|
|
160
|
+
body: row.body,
|
|
161
|
+
reply_to: row.reply_to,
|
|
162
|
+
created_at: row.created_at,
|
|
163
|
+
}));
|
|
164
|
+
}
|
|
165
|
+
export function inboxRows(db, session_id) {
|
|
166
|
+
const box = mailboxBySession(db, session_id);
|
|
167
|
+
const rows = box ? [...deliveryRows(db, box.name), ...globalRows(db)] : globalRows(db);
|
|
168
|
+
return rows.sort((a, b) => a.created_at - b.created_at);
|
|
169
|
+
}
|
|
170
|
+
// First unread reply to a message id, for mesh_wait. Box deliveries take
|
|
171
|
+
// priority over legacy global mail.
|
|
172
|
+
export function firstReply(db, reply_to, session_id) {
|
|
173
|
+
const box = mailboxBySession(db, session_id);
|
|
174
|
+
const rows = box ? [...deliveryRows(db, box.name), ...globalRows(db)] : globalRows(db);
|
|
175
|
+
const reply = rows.find((r) => r.reply_to === reply_to);
|
|
176
|
+
return reply ?? null;
|
|
177
|
+
}
|
|
178
|
+
export function ackInbox(db, delivery_ids) {
|
|
179
|
+
for (const id of delivery_ids) {
|
|
180
|
+
if (id.length === 0)
|
|
181
|
+
continue;
|
|
182
|
+
db.conn.run("UPDATE deliveries SET status = 'read' WHERE id = ?", [id]);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
export function ackReply(db, row) {
|
|
186
|
+
if (row.delivery_id.length > 0) {
|
|
187
|
+
db.conn.run("UPDATE deliveries SET status = 'read' WHERE id = ?", [row.delivery_id]);
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
db.conn.run("UPDATE messages SET status = 'read' WHERE id = ?", [row.id]);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// ---- legacy API kept for the remote wire path and existing callers ----
|
|
2
194
|
export function receiveMessage(db, input) {
|
|
3
195
|
const id = randomUUID();
|
|
4
196
|
const created_at = Date.now();
|
|
5
|
-
db.conn.run("INSERT INTO messages (id, from_node, subject, body, status, created_at) VALUES (?, ?, ?, ?, 'unread', ?)", [
|
|
197
|
+
db.conn.run("INSERT INTO messages (id, from_node, from_box, subject, body, reply_to, status, created_at) VALUES (?, ?, ?, ?, ?, ?, 'unread', ?)", [
|
|
198
|
+
id,
|
|
199
|
+
input.from,
|
|
200
|
+
input.from_box ?? null,
|
|
201
|
+
input.subject,
|
|
202
|
+
input.body,
|
|
203
|
+
input.reply_to ?? null,
|
|
204
|
+
created_at,
|
|
205
|
+
]);
|
|
6
206
|
return {
|
|
7
207
|
id,
|
|
8
208
|
from: input.from,
|
|
@@ -21,11 +221,20 @@ export function unreadCount(db) {
|
|
|
21
221
|
return (db.conn.get("SELECT COUNT(*) AS count FROM messages WHERE status = 'unread'")
|
|
22
222
|
?.count ?? 0);
|
|
23
223
|
}
|
|
224
|
+
export function unreadBoxCount(db, box) {
|
|
225
|
+
return (db.conn.get("SELECT COUNT(*) AS count FROM deliveries WHERE to_box = ? AND status = 'unread'", [box])?.count ?? 0);
|
|
226
|
+
}
|
|
24
227
|
export function ackMessages(db, ids) {
|
|
25
228
|
for (const id of ids) {
|
|
26
229
|
db.conn.run("UPDATE messages SET status = 'read' WHERE id = ?", [id]);
|
|
27
230
|
}
|
|
28
231
|
}
|
|
232
|
+
// ---- retention ----
|
|
233
|
+
export function purgeExpired(db, ttl_ms, now) {
|
|
234
|
+
const cutoff = now - ttl_ms;
|
|
235
|
+
db.conn.run("DELETE FROM deliveries WHERE message_id IN (SELECT id FROM messages WHERE created_at < ?)", [cutoff]);
|
|
236
|
+
db.conn.run("DELETE FROM messages WHERE created_at < ?", [cutoff]);
|
|
237
|
+
}
|
|
29
238
|
function rowToMessage(row) {
|
|
30
239
|
return {
|
|
31
240
|
id: row.id,
|
package/dist/lib/mail.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mail.js","sourceRoot":"","sources":["../../src/lib/mail.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"mail.js","sourceRoot":"","sources":["../../src/lib/mail.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAmDzC,0EAA0E;AAC1E,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,GAAG,EAAE,G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ist/lib/serve.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serve.d.ts","sourceRoot":"","sources":["../../src/lib/serve.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAmB,MAAM,EAAkB,MAAM,WAAW,CAAC;AAGzE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"serve.d.ts","sourceRoot":"","sources":["../../src/lib/serve.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAmB,MAAM,EAAkB,MAAM,WAAW,CAAC;AAGzE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAgBtC,MAAM,MAAM,iBAAiB,GAAG;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,UAAU,GAAG,KAAK,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAIF,wBAAsB,eAAe,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,UAAU,CAAC,CAgCrF"}
|
package/dist/lib/serve.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createServer } from "node:http";
|
|
2
2
|
import { authenticateToken } from "./peers.js";
|
|
3
|
-
import { receiveMessage, unreadCount } from "./mail.js";
|
|
3
|
+
import { deliverMessage, MESSAGE_TTL_MS, purgeExpired, receiveMessage, unreadCount, } from "./mail.js";
|
|
4
4
|
const BODY_LIMIT = 64 * 1024;
|
|
5
5
|
// Loopback by default: the mesh HTTP API must not be reachable from the
|
|
6
6
|
// network unless the operator explicitly opted into a LAN pool.
|
|
@@ -9,6 +9,8 @@ const DEFAULT_BIND = "127.0.0.1";
|
|
|
9
9
|
// port is taken (two mesh nodes on one machine, e.g. a spawned worker loading
|
|
10
10
|
// the same plugin), fall back to an ephemeral port instead of crashing.
|
|
11
11
|
export async function startMeshServer(options) {
|
|
12
|
+
// Messages older than 7 days are swept at boot.
|
|
13
|
+
purgeExpired(options.db, MESSAGE_TTL_MS, Date.now());
|
|
12
14
|
const host = options.bind === "lan" ? "0.0.0.0" : DEFAULT_BIND;
|
|
13
15
|
const server = createServer((req, res) => {
|
|
14
16
|
void handle(options.db, req, res);
|
|
@@ -78,8 +80,35 @@ async function handle(db, req, res) {
|
|
|
78
80
|
json(res, 401, { error: "from does not match token" });
|
|
79
81
|
return;
|
|
80
82
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
+
// Addressed mail first: a known box routes to per-box deliveries, an
|
|
84
|
+
// unknown box falls back to the legacy global inbox so remote mail is
|
|
85
|
+
// never lost, and a broadcast with no local members is not an error.
|
|
86
|
+
const result = deliverMessage(db, {
|
|
87
|
+
from: parsed.from,
|
|
88
|
+
from_box: parsed.from_box ?? null,
|
|
89
|
+
to_box: parsed.to ?? null,
|
|
90
|
+
subject: parsed.subject,
|
|
91
|
+
body: parsed.body,
|
|
92
|
+
reply_to: parsed.reply_to ?? null,
|
|
93
|
+
});
|
|
94
|
+
let delivered = 0;
|
|
95
|
+
if (!result.ok && result.error === "unknown_mailbox") {
|
|
96
|
+
receiveMessage(db, {
|
|
97
|
+
from: parsed.from,
|
|
98
|
+
subject: parsed.subject,
|
|
99
|
+
body: parsed.body,
|
|
100
|
+
...(parsed.from_box !== undefined ? { from_box: parsed.from_box } : {}),
|
|
101
|
+
...(parsed.reply_to !== undefined ? { reply_to: parsed.reply_to } : {}),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
else if (result.ok) {
|
|
105
|
+
delivered = result.recipients.length;
|
|
106
|
+
}
|
|
107
|
+
json(res, 200, {
|
|
108
|
+
ok: true,
|
|
109
|
+
unread: unreadCount(db) + unreadDeliveries(db),
|
|
110
|
+
delivered,
|
|
111
|
+
});
|
|
83
112
|
return;
|
|
84
113
|
}
|
|
85
114
|
if (req.method === "GET" && url.pathname === "/status") {
|
|
@@ -130,6 +159,11 @@ function readBody(req) {
|
|
|
130
159
|
req.on("close", () => resolve({ error: "read" }));
|
|
131
160
|
});
|
|
132
161
|
}
|
|
162
|
+
// Unread deliveries across every box, added to the legacy global count for
|
|
163
|
+
// the /mail and /status unread totals.
|
|
164
|
+
function unreadDeliveries(db) {
|
|
165
|
+
return (db.conn.get("SELECT COUNT(*) AS count FROM deliveries WHERE status = 'unread'")?.count ?? 0);
|
|
166
|
+
}
|
|
133
167
|
function parseMail(raw) {
|
|
134
168
|
try {
|
|
135
169
|
const parsed = JSON.parse(raw);
|
|
@@ -143,6 +177,9 @@ function parseMail(raw) {
|
|
|
143
177
|
subject: parsed.subject,
|
|
144
178
|
body: parsed.body,
|
|
145
179
|
...(typeof parsed.proof === "string" ? { proof: parsed.proof } : {}),
|
|
180
|
+
...(typeof parsed.to === "string" ? { to: parsed.to } : {}),
|
|
181
|
+
...(typeof parsed.from_box === "string" ? { from_box: parsed.from_box } : {}),
|
|
182
|
+
...(typeof parsed.reply_to === "string" ? { reply_to: parsed.reply_to } : {}),
|
|
146
183
|
};
|
|
147
184
|
}
|
|
148
185
|
catch {
|