@yaks/mail 0.0.0-stage → 0.2.36
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/LICENSE +202 -0
- package/README.md +676 -2
- package/_dist/addr.d.ts +40 -0
- package/_dist/addr.d.ts.map +1 -0
- package/_dist/answers.d.ts +18 -0
- package/_dist/answers.d.ts.map +1 -0
- package/_dist/arrive.d.ts +64 -0
- package/_dist/arrive.d.ts.map +1 -0
- package/_dist/capture.d.ts +22 -0
- package/_dist/capture.d.ts.map +1 -0
- package/_dist/comp.d.ts +27 -0
- package/_dist/comp.d.ts.map +1 -0
- package/_dist/effects.d.ts +9 -0
- package/_dist/effects.d.ts.map +1 -0
- package/_dist/gmail.d.ts +16 -0
- package/_dist/gmail.d.ts.map +1 -0
- package/_dist/graph.d.ts +9 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/inbound.d.ts +105 -0
- package/_dist/inbound.d.ts.map +1 -0
- package/_dist/invite.d.ts +69 -0
- package/_dist/invite.d.ts.map +1 -0
- package/_dist/md.d.ts +34 -0
- package/_dist/md.d.ts.map +1 -0
- package/_dist/mod.d.ts +109 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/options.d.ts +38 -0
- package/_dist/options.d.ts.map +1 -0
- package/_dist/plugin.d.ts +38 -0
- package/_dist/plugin.d.ts.map +1 -0
- package/_dist/pull.d.ts +99 -0
- package/_dist/pull.d.ts.map +1 -0
- package/_dist/receipts.d.ts +8 -0
- package/_dist/receipts.d.ts.map +1 -0
- package/_dist/recipient.d.ts +24 -0
- package/_dist/recipient.d.ts.map +1 -0
- package/_dist/routes.d.ts +19 -0
- package/_dist/routes.d.ts.map +1 -0
- package/_dist/send.d.ts +80 -0
- package/_dist/send.d.ts.map +1 -0
- package/_dist/service.d.ts +14 -0
- package/_dist/service.d.ts.map +1 -0
- package/_dist/stash.d.ts +23 -0
- package/_dist/stash.d.ts.map +1 -0
- package/_dist/tools.d.ts +27 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/transport.d.ts +6 -0
- package/_dist/transport.d.ts.map +1 -0
- package/_dist/vocab.d.ts +7 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/addr.js +92 -0
- package/addr.js.map +1 -0
- package/addr.ts +106 -0
- package/answers.js +71 -0
- package/answers.js.map +1 -0
- package/answers.ts +95 -0
- package/arrive.js +122 -0
- package/arrive.js.map +1 -0
- package/arrive.ts +198 -0
- package/capture.js +21 -0
- package/capture.js.map +1 -0
- package/capture.ts +43 -0
- package/comp.js +76 -0
- package/comp.js.map +1 -0
- package/comp.ts +91 -0
- package/deno.json +1 -0
- package/effects.js +90 -0
- package/effects.js.map +1 -0
- package/effects.ts +90 -0
- package/gmail.js +262 -0
- package/gmail.js.map +1 -0
- package/gmail.ts +314 -0
- package/graph.js +26 -0
- package/graph.js.map +1 -0
- package/graph.ts +30 -0
- package/inbound.js +206 -0
- package/inbound.js.map +1 -0
- package/inbound.ts +238 -0
- package/invite.js +102 -0
- package/invite.js.map +1 -0
- package/invite.ts +143 -0
- package/md.js +188 -0
- package/md.js.map +1 -0
- package/md.ts +245 -0
- package/mod.js +109 -0
- package/mod.js.map +1 -0
- package/mod.ts +111 -0
- package/options.js +24 -0
- package/options.js.map +1 -0
- package/options.ts +83 -0
- package/package.json +66 -4
- package/plugin.js +97 -0
- package/plugin.js.map +1 -0
- package/plugin.ts +88 -0
- package/pull.js +344 -0
- package/pull.js.map +1 -0
- package/pull.ts +443 -0
- package/readings.js +24 -0
- package/readings.js.map +1 -0
- package/readings.ts +34 -0
- package/receipts.js +45 -0
- package/receipts.js.map +1 -0
- package/receipts.ts +45 -0
- package/recipient.js +92 -0
- package/recipient.js.map +1 -0
- package/recipient.ts +89 -0
- package/routes.js +103 -0
- package/routes.js.map +1 -0
- package/routes.ts +125 -0
- package/send.js +284 -0
- package/send.js.map +1 -0
- package/send.ts +325 -0
- package/service.js +64 -0
- package/service.js.map +1 -0
- package/service.ts +75 -0
- package/stash.js +29 -0
- package/stash.js.map +1 -0
- package/stash.ts +49 -0
- package/tools.js +343 -0
- package/tools.js.map +1 -0
- package/tools.ts +416 -0
- package/transport.js +23 -0
- package/transport.js.map +1 -0
- package/transport.ts +24 -0
- package/vocab.js +14 -0
- package/vocab.js.map +1 -0
- package/vocab.json +415 -0
- package/vocab.ts +16 -0
- package/work.js +121 -0
- package/work.js.map +1 -0
- package/work.ts +134 -0
package/send.js
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
// Sending: the interface a transport implements, and the effect that calls it.
|
|
2
|
+
//
|
|
3
|
+
// A letter is sent when it asks to be. The request is the `deliver` component:
|
|
4
|
+
// an entity carrying `mail` and `deliver` is outbound, and one carrying `mail`
|
|
5
|
+
// alone is a letter you are keeping — a draft, or one that arrived. That is the
|
|
6
|
+
// whole rule, and it is why an inbound letter (which carries the address it was
|
|
7
|
+
// delivered to, in `mail.to`) can never echo itself back out.
|
|
8
|
+
//
|
|
9
|
+
// The effect runs post-commit, which is the right place for it: the letter is
|
|
10
|
+
// durable before anyone tries to send it, a mail server that is down cannot
|
|
11
|
+
// fail the write, and the outcome — `delivered` or `bounced` — is written back
|
|
12
|
+
// through the graph's own `apply()` as a transaction of its own. So "what
|
|
13
|
+
// happened to that letter?" is both a query and a live update: it is journaled
|
|
14
|
+
// and pushed to whoever is subscribed, rather than a row found on the next read
|
|
15
|
+
// (T-34044).
|
|
16
|
+
//
|
|
17
|
+
// The transport itself is supplied by the caller. This package composes a
|
|
18
|
+
// message and hands it over; whether that is Cloudflare, an SMTP relay, or an
|
|
19
|
+
// array in memory is the caller's business (a provider plugin lends one through
|
|
20
|
+
// Host.senders; ./stash.ts is the built-in one).
|
|
21
|
+
//
|
|
22
|
+
// A letter is two components: the envelope is `mail` and the words a person
|
|
23
|
+
// reads are @yaks/doc's `doc{title, body}`. So the composition works from the
|
|
24
|
+
// whole entity, which is what the effect reads anyway.
|
|
25
|
+
import { Stale } from "@yaks/graph/errors";
|
|
26
|
+
import { token } from "@yaks/graph/sha256";
|
|
27
|
+
import { after } from "@yaks/fp";
|
|
28
|
+
import { BODY, DOC, TITLE } from "@yaks/doc";
|
|
29
|
+
import { at } from "./addr.js";
|
|
30
|
+
import { human } from "@yaks/id";
|
|
31
|
+
import { eids, html, text } from "./md.js";
|
|
32
|
+
import { BOUNCED, DELIVER, DELIVERED, EMAIL, MAIL } from "./comp.js";
|
|
33
|
+
import { Undeliverable, Unsent } from "@yaks/host/senders";
|
|
34
|
+
export { Undeliverable, Unsent };
|
|
35
|
+
let clock = ()=>new Date().toISOString();
|
|
36
|
+
/** The whole letter as it stands, post-commit. An effect works from storage
|
|
37
|
+
* rather than from the patch, so it sees `mail` and `deliver` together however
|
|
38
|
+
* the transaction that wrote them was shaped. */ export let letterOf = (tx, entity)=>after(tx.get([
|
|
39
|
+
entity.eid
|
|
40
|
+
]), (found)=>found[0]);
|
|
41
|
+
let comp = (b, name)=>b?.[name];
|
|
42
|
+
let str = (c, k)=>c?.[k] == null ? '' : String(c[k]);
|
|
43
|
+
/** Whether a letter is still owed a send: it asks to go, it was never handed
|
|
44
|
+
* to a sender, and it has no outcome. A letter handed over and never settled
|
|
45
|
+
* may have left, so it is not owed — sending is at most once. */ export let owed = (letter)=>!!comp(letter, MAIL) && !!comp(letter, DELIVER) && // A text asks to go by the same `deliver` and is sent by @yaks/phone; the
|
|
46
|
+
// `mail` an app's rows may wear does not make it a letter.
|
|
47
|
+
!comp(letter, 'text') && comp(letter, DELIVER)?.tried == null && !comp(letter, DELIVERED) && !comp(letter, BOUNCED);
|
|
48
|
+
/** What a letter calls each entity its body names: the entity's id (`T-453`),
|
|
49
|
+
* and its page at `mentions`, read from the store at send. One the store does
|
|
50
|
+
* not hold names nothing, and stays as written. */ export let namesOf = (tx, body, mentions)=>{
|
|
51
|
+
let named = eids(body);
|
|
52
|
+
let id = human();
|
|
53
|
+
return after(named.length ? tx.get(named) : [], (found)=>{
|
|
54
|
+
let at = new Map(found.filter(Boolean).map((b)=>{
|
|
55
|
+
let name = id(b);
|
|
56
|
+
let href = mentions?.replaceAll('{id}', encodeURIComponent(name.replace(/^#/, '')));
|
|
57
|
+
return [
|
|
58
|
+
b.entity.eid.toLowerCase(),
|
|
59
|
+
{
|
|
60
|
+
id: name,
|
|
61
|
+
href
|
|
62
|
+
}
|
|
63
|
+
];
|
|
64
|
+
}));
|
|
65
|
+
return (eid)=>at.get(eid);
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
/** The address an entity is reachable at: its own `email.address`. */ export let addressOf = (tx, eid)=>after(tx.get([
|
|
69
|
+
eid
|
|
70
|
+
]), (found)=>str(comp(found[0], EMAIL), 'address'));
|
|
71
|
+
// The Message-ID a reply threads on: the answered letter's own, whether it
|
|
72
|
+
// arrived with it or our transport gave it one when it left.
|
|
73
|
+
let threadOf = (tx, eid)=>after(tx.get([
|
|
74
|
+
eid
|
|
75
|
+
]), (found)=>str(comp(found[0], MAIL), 'message_id'));
|
|
76
|
+
/**
|
|
77
|
+
* The letter as a message: the subject and both body renderings, with the
|
|
78
|
+
* addresses already resolved. Pure — a test asserts on it without a transport
|
|
79
|
+
* anywhere.
|
|
80
|
+
*
|
|
81
|
+
* It takes the whole letter rather than one component, because a letter is two
|
|
82
|
+
* of them: the envelope is `mail` and the words are
|
|
83
|
+
* {@link https://jsr.io/@yaks/doc | @yaks/doc}'s `doc{title, body}`.
|
|
84
|
+
*/ export let message = (letter, to, replyTo, names)=>{
|
|
85
|
+
let mail = comp(letter, MAIL);
|
|
86
|
+
let doc = comp(letter, DOC);
|
|
87
|
+
let body = str(doc, BODY);
|
|
88
|
+
return {
|
|
89
|
+
from: str(mail, 'from'),
|
|
90
|
+
...str(mail, 'connection') ? {
|
|
91
|
+
connection: str(mail, 'connection')
|
|
92
|
+
} : {},
|
|
93
|
+
...str(mail, 'name') ? {
|
|
94
|
+
name: str(mail, 'name')
|
|
95
|
+
} : {},
|
|
96
|
+
to,
|
|
97
|
+
...str(mail, 'cc') ? {
|
|
98
|
+
cc: str(mail, 'cc')
|
|
99
|
+
} : {},
|
|
100
|
+
subject: str(doc, TITLE),
|
|
101
|
+
text: text(body, names),
|
|
102
|
+
html: html(body, names),
|
|
103
|
+
...replyTo ? {
|
|
104
|
+
replyTo
|
|
105
|
+
} : {},
|
|
106
|
+
...str(mail, 'reply_address') ? {
|
|
107
|
+
replyAddress: str(mail, 'reply_address')
|
|
108
|
+
} : {}
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* The `created(mail)` handler: hand an outbound letter to the sender, and
|
|
113
|
+
* write back what became of it.
|
|
114
|
+
*
|
|
115
|
+
* ```ts
|
|
116
|
+
* import { effects } from '@yaks/effects'
|
|
117
|
+
* import { graph } from '@yaks/graph'
|
|
118
|
+
* import { ram } from '@yaks/ram'
|
|
119
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
120
|
+
* import { docDoc, docs } from '@yaks/doc'
|
|
121
|
+
* import { mailDoc, sending, stash } from '@yaks/mail'
|
|
122
|
+
*
|
|
123
|
+
* let vocab = loadVocab([docDoc, mailDoc])
|
|
124
|
+
* let fx = effects(vocab, { write: (b) => g.apply(b, { trusted: true }) })
|
|
125
|
+
* let g = graph({ storage: ram(vocab), vocab, plugins: [fx, docs()] })
|
|
126
|
+
* fx.handle({ mail_post: sending({ sender: stash() }) })
|
|
127
|
+
* ```
|
|
128
|
+
*
|
|
129
|
+
* It reads the whole entity, so it does not care which component triggered
|
|
130
|
+
* it, and it is idempotent: only a
|
|
131
|
+
* letter still {@link owed} a send is touched, and `deliver.tried` is written
|
|
132
|
+
* before the letter reaches the transport, so a crash between the send and its
|
|
133
|
+
* outcome leaves a letter that is never handed over twice. The one exception
|
|
134
|
+
* is a sender's `Unsent` rejection, which knows the letter never left: the
|
|
135
|
+
* claim is given back and the run fails to be tried again, so a dropped
|
|
136
|
+
* connection is a wait, not a bounce. A sender's `Undeliverable` rejection is
|
|
137
|
+
* a bounce and nothing more: the provider refused an address on the letter,
|
|
138
|
+
* so the run ends with the letter bounced rather than failing.
|
|
139
|
+
*
|
|
140
|
+
* The outcome is written back through @yaks/effects' write function — a new
|
|
141
|
+
* transaction through the graph's own `apply()` — so "this letter left" is
|
|
142
|
+
* journaled and pushed to whoever is subscribed to the letter, rather than a
|
|
143
|
+
* row they find next time they read. The registry needs that function:
|
|
144
|
+
* `effects(vocab, { write })`, applied trusted, since `delivered` and
|
|
145
|
+
* `bounced` are the sender's report and therefore server-owned.
|
|
146
|
+
*
|
|
147
|
+
* `local` names a domain whose addresses belong to this graph — an agent, a
|
|
148
|
+
* project, anything reachable here and nowhere else. A letter to one is
|
|
149
|
+
* already where it is going, so it is stamped `delivered` and never handed to
|
|
150
|
+
* the transport; sending it out would bring it back as a
|
|
151
|
+
* second letter about the same words. Leave it out where somebody reads that
|
|
152
|
+
* domain's mail in a mail client: then the mailbox is the destination, and the
|
|
153
|
+
* graph is only the record.
|
|
154
|
+
*/ export let sending = ({ sender, now = clock, local, mentions })=>(event, tx, write)=>after(letterOf(tx, event.entity), (letter)=>{
|
|
155
|
+
// Not a letter, not a request to send one, or one already handed over.
|
|
156
|
+
if (!owed(letter)) return;
|
|
157
|
+
let mail = comp(letter, MAIL);
|
|
158
|
+
let deliver = comp(letter, DELIVER);
|
|
159
|
+
let settle = (out, name)=>write([
|
|
160
|
+
{
|
|
161
|
+
entity: event.entity,
|
|
162
|
+
[name]: {
|
|
163
|
+
at: now(),
|
|
164
|
+
...out
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
]);
|
|
168
|
+
let fail = async (reason, error = new Error(reason))=>{
|
|
169
|
+
await settle({
|
|
170
|
+
reason
|
|
171
|
+
}, BOUNCED);
|
|
172
|
+
// The outcome answers the owner; throwing also reaches the host's
|
|
173
|
+
// reporter. A later attempt sees the bounce and cannot resend.
|
|
174
|
+
throw error;
|
|
175
|
+
};
|
|
176
|
+
let recipient = deliver.to == null ? '' : String(deliver.to);
|
|
177
|
+
if (!recipient) return fail('deliver.to names nobody');
|
|
178
|
+
// Claim the hand-over only if the letter is still addressed and untried.
|
|
179
|
+
// A write that removes `deliver` withdraws the request before hand-over.
|
|
180
|
+
let claim = async (patch)=>{
|
|
181
|
+
try {
|
|
182
|
+
await write([
|
|
183
|
+
{
|
|
184
|
+
...patch,
|
|
185
|
+
$was: {
|
|
186
|
+
[DELIVER]: {
|
|
187
|
+
to: token(deliver.to),
|
|
188
|
+
tried: null
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
]);
|
|
193
|
+
return true;
|
|
194
|
+
} catch (error) {
|
|
195
|
+
if (error instanceof Stale && error.eid == event.entity.eid && error.comp == DELIVER && (error.prop == 'tried' || error.prop == 'to' && error.current == null)) return false;
|
|
196
|
+
throw error;
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
return after(addressOf(tx, recipient), (to)=>{
|
|
200
|
+
if (!to) return fail(`no address on file for ${recipient}`);
|
|
201
|
+
if (!str(mail, 'from')) return fail('the letter has no from address');
|
|
202
|
+
// Local delivery: the address is at a domain this graph owns, so the
|
|
203
|
+
// letter has arrived by being written. The envelope is stamped the same
|
|
204
|
+
// way a sent one is — a later address-book edit must never rewrite
|
|
205
|
+
// where this letter went.
|
|
206
|
+
if (local && at(local)(to)) {
|
|
207
|
+
return claim({
|
|
208
|
+
entity: event.entity,
|
|
209
|
+
[MAIL]: {
|
|
210
|
+
to
|
|
211
|
+
},
|
|
212
|
+
[DELIVER]: {
|
|
213
|
+
waiting: null
|
|
214
|
+
},
|
|
215
|
+
[DELIVERED]: {
|
|
216
|
+
at: now()
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
let answered = mail.reply_to == null ? '' : String(mail.reply_to);
|
|
221
|
+
let stamp = now();
|
|
222
|
+
let tried = ()=>claim({
|
|
223
|
+
entity: event.entity,
|
|
224
|
+
[DELIVER]: {
|
|
225
|
+
tried: stamp,
|
|
226
|
+
waiting: null
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
// The sender knows the letter never left: give the claim back, say
|
|
230
|
+
// why it waits, and throw, so the pool hands it over again after the
|
|
231
|
+
// wait the sender asked for. A terminal failure keeps its receipt for
|
|
232
|
+
// an explicit retry once the transport is repaired.
|
|
233
|
+
let unsent = async (error)=>{
|
|
234
|
+
try {
|
|
235
|
+
await write([
|
|
236
|
+
{
|
|
237
|
+
entity: event.entity,
|
|
238
|
+
[DELIVER]: {
|
|
239
|
+
tried: null,
|
|
240
|
+
waiting: error.message.slice(0, 240)
|
|
241
|
+
},
|
|
242
|
+
$was: {
|
|
243
|
+
[DELIVER]: {
|
|
244
|
+
tried: token(stamp)
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
]);
|
|
249
|
+
} catch (raceError) {
|
|
250
|
+
if (!(raceError instanceof Stale && raceError.eid == event.entity.eid && raceError.comp == DELIVER && raceError.prop == 'tried')) throw raceError;
|
|
251
|
+
}
|
|
252
|
+
throw error;
|
|
253
|
+
};
|
|
254
|
+
let body = str(comp(letter, DOC), BODY);
|
|
255
|
+
return after(Promise.all([
|
|
256
|
+
answered ? threadOf(tx, answered) : '',
|
|
257
|
+
namesOf(tx, body, mentions)
|
|
258
|
+
]), ([replyTo, names])=>tried().then((claimed)=>claimed ? sender.send({
|
|
259
|
+
...message(letter, to, replyTo || undefined, names),
|
|
260
|
+
id: letter.entity.eid
|
|
261
|
+
}) : undefined).then((receipt)=>receipt && write([
|
|
262
|
+
{
|
|
263
|
+
entity: event.entity,
|
|
264
|
+
// The envelope, denormalized onto the letter as data: an
|
|
265
|
+
// address book edited later never rewrites where this one
|
|
266
|
+
// went. The Message-ID the transport gave it goes where an
|
|
267
|
+
// arrival's does, so a reply to it threads the same way.
|
|
268
|
+
[MAIL]: {
|
|
269
|
+
to,
|
|
270
|
+
...receipt.id ? {
|
|
271
|
+
message_id: receipt.id
|
|
272
|
+
} : {}
|
|
273
|
+
},
|
|
274
|
+
[DELIVERED]: {
|
|
275
|
+
at: now()
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
]), (err)=>err instanceof Unsent ? unsent(err) : err instanceof Undeliverable ? settle({
|
|
279
|
+
reason: err.message.slice(0, 240),
|
|
280
|
+
refused: true
|
|
281
|
+
}, BOUNCED) : fail(String(err?.message ?? err).slice(0, 240), err)));
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
//# sourceMappingURL=send.js.map
|
package/send.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"send.js","sources":["./send.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,EAAE;AACF,+EAA+E;AAC/E,+EAA+E;AAC/E,gFAAgF;AAChF,gFAAgF;AAChF,8DAA8D;AAC9D,EAAE;AACF,8EAA8E;AAC9E,4EAA4E;AAC5E,+EAA+E;AAC/E,0EAA0E;AAC1E,+EAA+E;AAC/E,gFAAgF;AAChF,aAAa;AACb,EAAE;AACF,0EAA0E;AAC1E,8EAA8E;AAC9E,gFAAgF;AAChF,iDAAiD;AACjD,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,uDAAuD;AAGvD,SAAS,KAAK,kCAAuC;AACrD,SAAS,KAAK,kCAAuC;AACrD,SAAS,KAAK,wBAA6B;AAE3C,SAAS,IAAI,EAAE,GAAG,EAAE,KAAK,yBAA8B;AACvD,SAAS,EAAE,oBAAmB;AAC9B,SAAS,KAAK,wBAA6B;AAC3C,SAAS,IAAI,EAAE,IAAI,EAAc,IAAI,kBAAiB;AACtD,SAAS,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,IAAI,oBAAmB;AAEpE,SAAS,aAAa,EAAE,MAAM,kCAAuC;AAErE,SAAS,aAAa,EAAE,MAAM,GAAE;AAiBhC,IAAI,QAAQ,IAAM,IAAI,OAAO,WAAW;AAExC;;+CAE+C,GAC/C,OAAO,IAAI,WAAW,CACpB,IACA,SAEA,MAAM,GAAG,GAAG,CAA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,eAAe,SACX,OAAO,OAGP,eAAe,gBACf,OACA;cAAE,QAAQ,IAAI,OAAO,CAAC,KAAK,CAAC,GAAG;cAAM,SAAS;YAAK,GACnD,WAEA,KACA,OAAO,AAAC,KAAe,WAAW,KAAK,KAAK,CAAC,GAAG,MAChD;MAId;IACF,GAAE"}
|
package/send.ts
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
// Sending: the interface a transport implements, and the effect that calls it.
|
|
2
|
+
//
|
|
3
|
+
// A letter is sent when it asks to be. The request is the `deliver` component:
|
|
4
|
+
// an entity carrying `mail` and `deliver` is outbound, and one carrying `mail`
|
|
5
|
+
// alone is a letter you are keeping — a draft, or one that arrived. That is the
|
|
6
|
+
// whole rule, and it is why an inbound letter (which carries the address it was
|
|
7
|
+
// delivered to, in `mail.to`) can never echo itself back out.
|
|
8
|
+
//
|
|
9
|
+
// The effect runs post-commit, which is the right place for it: the letter is
|
|
10
|
+
// durable before anyone tries to send it, a mail server that is down cannot
|
|
11
|
+
// fail the write, and the outcome — `delivered` or `bounced` — is written back
|
|
12
|
+
// through the graph's own `apply()` as a transaction of its own. So "what
|
|
13
|
+
// happened to that letter?" is both a query and a live update: it is journaled
|
|
14
|
+
// and pushed to whoever is subscribed, rather than a row found on the next read
|
|
15
|
+
// (T-34044).
|
|
16
|
+
//
|
|
17
|
+
// The transport itself is supplied by the caller. This package composes a
|
|
18
|
+
// message and hands it over; whether that is Cloudflare, an SMTP relay, or an
|
|
19
|
+
// array in memory is the caller's business (a provider plugin lends one through
|
|
20
|
+
// Host.senders; ./stash.ts is the built-in one).
|
|
21
|
+
//
|
|
22
|
+
// A letter is two components: the envelope is `mail` and the words a person
|
|
23
|
+
// reads are @yaks/doc's `doc{title, body}`. So the composition works from the
|
|
24
|
+
// whole entity, which is what the effect reads anyway.
|
|
25
|
+
|
|
26
|
+
import type { Bundle, Comp, Entity, ReadTx } from 'jsr:@yaks/graph@0.2.36'
|
|
27
|
+
import { Stale } from '@yaks/graph/errors'
|
|
28
|
+
import { token } from '@yaks/graph/sha256'
|
|
29
|
+
import { after } from '@yaks/fp'
|
|
30
|
+
import type { Handler } from 'jsr:@yaks/effects@0.2.36'
|
|
31
|
+
import { BODY, DOC, TITLE } from '@yaks/doc'
|
|
32
|
+
import { at } from './addr.js'
|
|
33
|
+
import { human } from '@yaks/id'
|
|
34
|
+
import { eids, html, type Names, text } from './md.js'
|
|
35
|
+
import { BOUNCED, DELIVER, DELIVERED, EMAIL, MAIL } from './comp.js'
|
|
36
|
+
import type { Message, Sender } from '@yaks/host/senders'
|
|
37
|
+
import { Undeliverable, Unsent } from '@yaks/host/senders'
|
|
38
|
+
export type { Message, Receipt, Sender } from '@yaks/host/senders'
|
|
39
|
+
export { Undeliverable, Unsent }
|
|
40
|
+
|
|
41
|
+
/** How the effect is built. */
|
|
42
|
+
export type Post = {
|
|
43
|
+
/** what to do about a letter that asks to be sent */
|
|
44
|
+
sender: Sender
|
|
45
|
+
/** the clock, passed in so a test can hold it still (default: now) */
|
|
46
|
+
now?: () => string
|
|
47
|
+
/** a domain whose addresses belong to this graph rather than to a mail
|
|
48
|
+
* server. A letter to one is delivered by writing it — see below. */
|
|
49
|
+
local?: string
|
|
50
|
+
/** where an entity a letter names opens: an address with `{id}` where its
|
|
51
|
+
* id goes (`https://tasks.yak.sh/{id}`). Each eid a body holds then reads as
|
|
52
|
+
* the entity's id, linked there; with none, as the id alone. */
|
|
53
|
+
mentions?: string
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let clock = () => new Date().toISOString()
|
|
57
|
+
|
|
58
|
+
/** The whole letter as it stands, post-commit. An effect works from storage
|
|
59
|
+
* rather than from the patch, so it sees `mail` and `deliver` together however
|
|
60
|
+
* the transaction that wrote them was shaped. */
|
|
61
|
+
export let letterOf = (
|
|
62
|
+
tx: ReadTx,
|
|
63
|
+
entity: Entity,
|
|
64
|
+
): Bundle | undefined | Promise<Bundle | undefined> =>
|
|
65
|
+
after(tx.get([entity.eid]), (found) => found[0])
|
|
66
|
+
|
|
67
|
+
let comp = (b: Bundle | undefined, name: string): Comp | undefined =>
|
|
68
|
+
b?.[name] as Comp | undefined
|
|
69
|
+
|
|
70
|
+
let str = (c: Comp | undefined, k: string): string =>
|
|
71
|
+
c?.[k] == null ? '' : String(c[k])
|
|
72
|
+
|
|
73
|
+
/** Whether a letter is still owed a send: it asks to go, it was never handed
|
|
74
|
+
* to a sender, and it has no outcome. A letter handed over and never settled
|
|
75
|
+
* may have left, so it is not owed — sending is at most once. */
|
|
76
|
+
export let owed = (letter: Bundle | undefined): boolean =>
|
|
77
|
+
!!comp(letter, MAIL) && !!comp(letter, DELIVER) &&
|
|
78
|
+
// A text asks to go by the same `deliver` and is sent by @yaks/phone; the
|
|
79
|
+
// `mail` an app's rows may wear does not make it a letter.
|
|
80
|
+
!comp(letter, 'text') &&
|
|
81
|
+
comp(letter, DELIVER)?.tried == null &&
|
|
82
|
+
!comp(letter, DELIVERED) && !comp(letter, BOUNCED)
|
|
83
|
+
|
|
84
|
+
/** What a letter calls each entity its body names: the entity's id (`T-453`),
|
|
85
|
+
* and its page at `mentions`, read from the store at send. One the store does
|
|
86
|
+
* not hold names nothing, and stays as written. */
|
|
87
|
+
export let namesOf = (
|
|
88
|
+
tx: ReadTx,
|
|
89
|
+
body: string,
|
|
90
|
+
mentions?: string,
|
|
91
|
+
): Names | Promise<Names> => {
|
|
92
|
+
let named = eids(body)
|
|
93
|
+
let id = human()
|
|
94
|
+
return after(named.length ? tx.get(named) : [], (found: Bundle[]) => {
|
|
95
|
+
let at = new Map(
|
|
96
|
+
found.filter(Boolean).map((b) => {
|
|
97
|
+
let name = id(b)
|
|
98
|
+
let href = mentions?.replaceAll(
|
|
99
|
+
'{id}',
|
|
100
|
+
encodeURIComponent(name.replace(/^#/, '')),
|
|
101
|
+
)
|
|
102
|
+
return [b.entity.eid.toLowerCase(), { id: name, href }]
|
|
103
|
+
}),
|
|
104
|
+
)
|
|
105
|
+
return (eid: string) => at.get(eid)
|
|
106
|
+
})
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The address an entity is reachable at: its own `email.address`. */
|
|
110
|
+
export let addressOf = (
|
|
111
|
+
tx: ReadTx,
|
|
112
|
+
eid: string,
|
|
113
|
+
): string | Promise<string> =>
|
|
114
|
+
after(tx.get([eid]), (found) => str(comp(found[0], EMAIL), 'address'))
|
|
115
|
+
|
|
116
|
+
// The Message-ID a reply threads on: the answered letter's own, whether it
|
|
117
|
+
// arrived with it or our transport gave it one when it left.
|
|
118
|
+
let threadOf = (tx: ReadTx, eid: string): string | Promise<string> =>
|
|
119
|
+
after(tx.get([eid]), (found) => str(comp(found[0], MAIL), 'message_id'))
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The letter as a message: the subject and both body renderings, with the
|
|
123
|
+
* addresses already resolved. Pure — a test asserts on it without a transport
|
|
124
|
+
* anywhere.
|
|
125
|
+
*
|
|
126
|
+
* It takes the whole letter rather than one component, because a letter is two
|
|
127
|
+
* of them: the envelope is `mail` and the words are
|
|
128
|
+
* {@link https://jsr.io/@yaks/doc | @yaks/doc}'s `doc{title, body}`.
|
|
129
|
+
*/
|
|
130
|
+
export let message = (
|
|
131
|
+
letter: Bundle,
|
|
132
|
+
to: string,
|
|
133
|
+
replyTo?: string,
|
|
134
|
+
names?: Names,
|
|
135
|
+
): Message => {
|
|
136
|
+
let mail = comp(letter, MAIL)
|
|
137
|
+
let doc = comp(letter, DOC)
|
|
138
|
+
let body = str(doc, BODY)
|
|
139
|
+
return {
|
|
140
|
+
from: str(mail, 'from'),
|
|
141
|
+
...(str(mail, 'connection') ? { connection: str(mail, 'connection') } : {}),
|
|
142
|
+
...(str(mail, 'name') ? { name: str(mail, 'name') } : {}),
|
|
143
|
+
to,
|
|
144
|
+
...(str(mail, 'cc') ? { cc: str(mail, 'cc') } : {}),
|
|
145
|
+
subject: str(doc, TITLE),
|
|
146
|
+
text: text(body, names),
|
|
147
|
+
html: html(body, names),
|
|
148
|
+
...(replyTo ? { replyTo } : {}),
|
|
149
|
+
...(str(mail, 'reply_address')
|
|
150
|
+
? { replyAddress: str(mail, 'reply_address') }
|
|
151
|
+
: {}),
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The `created(mail)` handler: hand an outbound letter to the sender, and
|
|
157
|
+
* write back what became of it.
|
|
158
|
+
*
|
|
159
|
+
* ```ts
|
|
160
|
+
* import { effects } from '@yaks/effects'
|
|
161
|
+
* import { graph } from '@yaks/graph'
|
|
162
|
+
* import { ram } from '@yaks/ram'
|
|
163
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
164
|
+
* import { docDoc, docs } from '@yaks/doc'
|
|
165
|
+
* import { mailDoc, sending, stash } from '@yaks/mail'
|
|
166
|
+
*
|
|
167
|
+
* let vocab = loadVocab([docDoc, mailDoc])
|
|
168
|
+
* let fx = effects(vocab, { write: (b) => g.apply(b, { trusted: true }) })
|
|
169
|
+
* let g = graph({ storage: ram(vocab), vocab, plugins: [fx, docs()] })
|
|
170
|
+
* fx.handle({ mail_post: sending({ sender: stash() }) })
|
|
171
|
+
* ```
|
|
172
|
+
*
|
|
173
|
+
* It reads the whole entity, so it does not care which component triggered
|
|
174
|
+
* it, and it is idempotent: only a
|
|
175
|
+
* letter still {@link owed} a send is touched, and `deliver.tried` is written
|
|
176
|
+
* before the letter reaches the transport, so a crash between the send and its
|
|
177
|
+
* outcome leaves a letter that is never handed over twice. The one exception
|
|
178
|
+
* is a sender's `Unsent` rejection, which knows the letter never left: the
|
|
179
|
+
* claim is given back and the run fails to be tried again, so a dropped
|
|
180
|
+
* connection is a wait, not a bounce. A sender's `Undeliverable` rejection is
|
|
181
|
+
* a bounce and nothing more: the provider refused an address on the letter,
|
|
182
|
+
* so the run ends with the letter bounced rather than failing.
|
|
183
|
+
*
|
|
184
|
+
* The outcome is written back through @yaks/effects' write function — a new
|
|
185
|
+
* transaction through the graph's own `apply()` — so "this letter left" is
|
|
186
|
+
* journaled and pushed to whoever is subscribed to the letter, rather than a
|
|
187
|
+
* row they find next time they read. The registry needs that function:
|
|
188
|
+
* `effects(vocab, { write })`, applied trusted, since `delivered` and
|
|
189
|
+
* `bounced` are the sender's report and therefore server-owned.
|
|
190
|
+
*
|
|
191
|
+
* `local` names a domain whose addresses belong to this graph — an agent, a
|
|
192
|
+
* project, anything reachable here and nowhere else. A letter to one is
|
|
193
|
+
* already where it is going, so it is stamped `delivered` and never handed to
|
|
194
|
+
* the transport; sending it out would bring it back as a
|
|
195
|
+
* second letter about the same words. Leave it out where somebody reads that
|
|
196
|
+
* domain's mail in a mail client: then the mailbox is the destination, and the
|
|
197
|
+
* graph is only the record.
|
|
198
|
+
*/
|
|
199
|
+
export let sending =
|
|
200
|
+
({ sender, now = clock, local, mentions }: Post): Handler =>
|
|
201
|
+
(event, tx, write) =>
|
|
202
|
+
after(letterOf(tx, event.entity), (letter) => {
|
|
203
|
+
// Not a letter, not a request to send one, or one already handed over.
|
|
204
|
+
if (!owed(letter)) return
|
|
205
|
+
let mail = comp(letter, MAIL)!
|
|
206
|
+
let deliver = comp(letter, DELIVER)!
|
|
207
|
+
let settle = (out: Comp, name: string) =>
|
|
208
|
+
write([{ entity: event.entity, [name]: { at: now(), ...out } }])
|
|
209
|
+
let fail = async (reason: string, error: unknown = new Error(reason)) => {
|
|
210
|
+
await settle({ reason }, BOUNCED)
|
|
211
|
+
// The outcome answers the owner; throwing also reaches the host's
|
|
212
|
+
// reporter. A later attempt sees the bounce and cannot resend.
|
|
213
|
+
throw error
|
|
214
|
+
}
|
|
215
|
+
let recipient = deliver.to == null ? '' : String(deliver.to)
|
|
216
|
+
if (!recipient) return fail('deliver.to names nobody')
|
|
217
|
+
// Claim the hand-over only if the letter is still addressed and untried.
|
|
218
|
+
// A write that removes `deliver` withdraws the request before hand-over.
|
|
219
|
+
let claim = async (patch: Bundle) => {
|
|
220
|
+
try {
|
|
221
|
+
await write([{
|
|
222
|
+
...patch,
|
|
223
|
+
$was: { [DELIVER]: { to: token(deliver.to), tried: null } },
|
|
224
|
+
}])
|
|
225
|
+
return true
|
|
226
|
+
} catch (error) {
|
|
227
|
+
if (
|
|
228
|
+
error instanceof Stale && error.eid == event.entity.eid &&
|
|
229
|
+
error.comp == DELIVER &&
|
|
230
|
+
(error.prop == 'tried' ||
|
|
231
|
+
error.prop == 'to' && error.current == null)
|
|
232
|
+
) return false
|
|
233
|
+
throw error
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return after(addressOf(tx, recipient), (to) => {
|
|
237
|
+
if (!to) return fail(`no address on file for ${recipient}`)
|
|
238
|
+
if (!str(mail, 'from')) return fail('the letter has no from address')
|
|
239
|
+
// Local delivery: the address is at a domain this graph owns, so the
|
|
240
|
+
// letter has arrived by being written. The envelope is stamped the same
|
|
241
|
+
// way a sent one is — a later address-book edit must never rewrite
|
|
242
|
+
// where this letter went.
|
|
243
|
+
if (local && at(local)(to)) {
|
|
244
|
+
return claim({
|
|
245
|
+
entity: event.entity,
|
|
246
|
+
[MAIL]: { to },
|
|
247
|
+
[DELIVER]: { waiting: null },
|
|
248
|
+
[DELIVERED]: { at: now() },
|
|
249
|
+
})
|
|
250
|
+
}
|
|
251
|
+
let answered = mail.reply_to == null ? '' : String(mail.reply_to)
|
|
252
|
+
let stamp = now()
|
|
253
|
+
let tried = () =>
|
|
254
|
+
claim({
|
|
255
|
+
entity: event.entity,
|
|
256
|
+
[DELIVER]: { tried: stamp, waiting: null },
|
|
257
|
+
})
|
|
258
|
+
// The sender knows the letter never left: give the claim back, say
|
|
259
|
+
// why it waits, and throw, so the pool hands it over again after the
|
|
260
|
+
// wait the sender asked for. A terminal failure keeps its receipt for
|
|
261
|
+
// an explicit retry once the transport is repaired.
|
|
262
|
+
let unsent = async (error: Unsent) => {
|
|
263
|
+
try {
|
|
264
|
+
await write([{
|
|
265
|
+
entity: event.entity,
|
|
266
|
+
[DELIVER]: { tried: null, waiting: error.message.slice(0, 240) },
|
|
267
|
+
$was: { [DELIVER]: { tried: token(stamp) } },
|
|
268
|
+
}])
|
|
269
|
+
} catch (raceError) {
|
|
270
|
+
if (
|
|
271
|
+
!(raceError instanceof Stale &&
|
|
272
|
+
raceError.eid == event.entity.eid &&
|
|
273
|
+
raceError.comp == DELIVER && raceError.prop == 'tried')
|
|
274
|
+
) throw raceError
|
|
275
|
+
}
|
|
276
|
+
throw error
|
|
277
|
+
}
|
|
278
|
+
let body = str(comp(letter, DOC), BODY)
|
|
279
|
+
return after(
|
|
280
|
+
Promise.all([
|
|
281
|
+
answered ? threadOf(tx, answered) : '',
|
|
282
|
+
namesOf(tx, body, mentions),
|
|
283
|
+
]),
|
|
284
|
+
([replyTo, names]) =>
|
|
285
|
+
tried().then(
|
|
286
|
+
(claimed) =>
|
|
287
|
+
claimed
|
|
288
|
+
? sender.send({
|
|
289
|
+
...message(letter!, to, replyTo || undefined, names),
|
|
290
|
+
id: letter!.entity.eid,
|
|
291
|
+
})
|
|
292
|
+
: undefined,
|
|
293
|
+
).then(
|
|
294
|
+
(receipt) =>
|
|
295
|
+
receipt &&
|
|
296
|
+
write([{
|
|
297
|
+
entity: event.entity,
|
|
298
|
+
// The envelope, denormalized onto the letter as data: an
|
|
299
|
+
// address book edited later never rewrites where this one
|
|
300
|
+
// went. The Message-ID the transport gave it goes where an
|
|
301
|
+
// arrival's does, so a reply to it threads the same way.
|
|
302
|
+
[MAIL]: {
|
|
303
|
+
to,
|
|
304
|
+
...(receipt.id ? { message_id: receipt.id } : {}),
|
|
305
|
+
},
|
|
306
|
+
[DELIVERED]: { at: now() },
|
|
307
|
+
}]),
|
|
308
|
+
(err) =>
|
|
309
|
+
err instanceof Unsent
|
|
310
|
+
? unsent(err)
|
|
311
|
+
// The provider refused an address the letter carries: the
|
|
312
|
+
// bounce is its whole outcome, and nothing failed.
|
|
313
|
+
: err instanceof Undeliverable
|
|
314
|
+
? settle(
|
|
315
|
+
{ reason: err.message.slice(0, 240), refused: true },
|
|
316
|
+
BOUNCED,
|
|
317
|
+
)
|
|
318
|
+
: fail(
|
|
319
|
+
String((err as Error)?.message ?? err).slice(0, 240),
|
|
320
|
+
err,
|
|
321
|
+
),
|
|
322
|
+
),
|
|
323
|
+
)
|
|
324
|
+
})
|
|
325
|
+
})
|
package/service.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// The pull, as a duty: the module a server imports at `@yaks/mail/service` to
|
|
2
|
+
// take what arrived at the edge for as long as the server is up (./pull.ts).
|
|
3
|
+
//
|
|
4
|
+
// It is a loop rather than an effect because nothing is committed when a
|
|
5
|
+
// letter reaches the edge; the graph has to ask. It is a duty rather than a
|
|
6
|
+
// timer every process starts because the edge hands each letter to whoever
|
|
7
|
+
// asks first — the host holds it under a lease, so one process over the graph
|
|
8
|
+
// pulls at a time. An explicit one-pass host pulls once when nobody else is.
|
|
9
|
+
//
|
|
10
|
+
// A config that names no `pull` has nothing to take, but holds the duty
|
|
11
|
+
// until shutdown, like a configured poll between passes.
|
|
12
|
+
import { sleep } from "@yaks/effects";
|
|
13
|
+
import { edge, pull } from "./pull.js";
|
|
14
|
+
import { readings } from "./readings.js";
|
|
15
|
+
/** How long a pull waits for the next one, unless the config says. */ export let EVERY = 10_000;
|
|
16
|
+
/** Pull from one edge, then again every `pull.every`, until the signal aborts.
|
|
17
|
+
* An already-aborted signal is one pull, so a tray it cannot take is said at
|
|
18
|
+
* once; a loop logs transient outages and reports letters they delay. */ let polling = async (host, from, options = {}, signal = AbortSignal.abort())=>{
|
|
19
|
+
let at = {
|
|
20
|
+
graph: host.graph,
|
|
21
|
+
...host.report ? {
|
|
22
|
+
report: host.report
|
|
23
|
+
} : {},
|
|
24
|
+
...host.log ? {
|
|
25
|
+
log: host.log
|
|
26
|
+
} : {},
|
|
27
|
+
...options.domain ? {
|
|
28
|
+
domain: options.domain
|
|
29
|
+
} : {},
|
|
30
|
+
...options.triage ? {
|
|
31
|
+
triage: options.triage
|
|
32
|
+
} : {},
|
|
33
|
+
...signal.aborted ? {} : {
|
|
34
|
+
down: new Map()
|
|
35
|
+
},
|
|
36
|
+
readings: await readings(host.config?.plugins)
|
|
37
|
+
};
|
|
38
|
+
let since;
|
|
39
|
+
for(;;){
|
|
40
|
+
let began = Date.now();
|
|
41
|
+
await pull(since == null ? at : {
|
|
42
|
+
...at,
|
|
43
|
+
since
|
|
44
|
+
}, from);
|
|
45
|
+
since = began;
|
|
46
|
+
if (signal.aborted) return;
|
|
47
|
+
await sleep(options.pull?.every ?? EVERY, signal);
|
|
48
|
+
if (signal.aborted) return;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
let open = (options, stop)=>edge(options, fetch, stop);
|
|
52
|
+
/** Pull what arrived, then keep polling until the host stops. */ export let service = (host, options = {}, signal = AbortSignal.abort(), make = open)=>{
|
|
53
|
+
if (!options.pull) {
|
|
54
|
+
if (signal.aborted) return Promise.resolve();
|
|
55
|
+
return (async ()=>{
|
|
56
|
+
while(!signal.aborted)await sleep(EVERY, signal);
|
|
57
|
+
})();
|
|
58
|
+
}
|
|
59
|
+
// Already aborted means one pass. A live signal also stops an in-flight
|
|
60
|
+
// edge request, so shutdown and a lost lease do not wait on the network.
|
|
61
|
+
let stop = signal.aborted ? undefined : signal;
|
|
62
|
+
return polling(host, make(options.pull, stop), options, signal);
|
|
63
|
+
};
|
|
64
|
+
//# sourceMappingURL=service.js.map
|