@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/recipient.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// The address a letter is for, kept with the letters. A letter's `mail` is read
|
|
2
|
+
// only by an app's owners and editors, but the `email{address}` row its
|
|
3
|
+
// `deliver.to` names carries no such mark, so in a public app anybody listed
|
|
4
|
+
// who had been written to. `email` itself cannot be kept (a public staff list
|
|
5
|
+
// uses it), so the row is marked when it is nothing but an address: a row the
|
|
6
|
+
// app also gave something of its own (`individual`, a form's `form_mail`) is
|
|
7
|
+
// left as the app wrote it.
|
|
8
|
+
import { hook, merged } from "@yaks/graph";
|
|
9
|
+
import { comps } from "@yaks/graph/bundle";
|
|
10
|
+
import { PROVENANCE } from "@yaks/graph/provenance";
|
|
11
|
+
import { after, each } from "@yaks/fp";
|
|
12
|
+
import { and, eq } from "@yaks/query";
|
|
13
|
+
import { DELIVER, EMAIL, RECIPIENT } from "./comp.js";
|
|
14
|
+
/** Whether a row is only an address: it wears `email` and nothing of an app's
|
|
15
|
+
* own. */ export let bare = (row)=>{
|
|
16
|
+
let names = comps(row).filter(([, c])=>c != null).map(([n])=>n);
|
|
17
|
+
return names.includes(EMAIL) && names.every((n)=>n == EMAIL || n == RECIPIENT || PROVENANCE.includes(n));
|
|
18
|
+
};
|
|
19
|
+
let pointing = (tx, eid)=>tx.about ? tx.about([
|
|
20
|
+
eid
|
|
21
|
+
], [
|
|
22
|
+
DELIVER
|
|
23
|
+
]) : tx.read(and(eq(`${DELIVER}.to`, eid)));
|
|
24
|
+
// The `recipient` mark owed to the addresses this batch writes or names, as
|
|
25
|
+
// they will stand once it is written: an address a letter in the batch is for,
|
|
26
|
+
// or one in the batch that a stored letter is for. A store that answers
|
|
27
|
+
// without waiting is not made to wait: a batch that names no address is
|
|
28
|
+
// passed through as it came.
|
|
29
|
+
let marking = (bundles, tx)=>{
|
|
30
|
+
let named = new Set(), written = new Set();
|
|
31
|
+
for (let b of bundles){
|
|
32
|
+
let to = b[DELIVER]?.to;
|
|
33
|
+
if (typeof to == 'string') named.add(to);
|
|
34
|
+
if (b[EMAIL]) written.add(b.entity.eid);
|
|
35
|
+
}
|
|
36
|
+
let eids = [
|
|
37
|
+
...new Set([
|
|
38
|
+
...named,
|
|
39
|
+
...written
|
|
40
|
+
])
|
|
41
|
+
];
|
|
42
|
+
if (!eids.length) return bundles;
|
|
43
|
+
return after(tx.get(eids), (held)=>after(each(eids, [], (marks, eid)=>{
|
|
44
|
+
let row = bundles.filter((b)=>b.entity.eid == eid).reduce(merged, held.find((h)=>h.entity.eid == eid) ?? null);
|
|
45
|
+
if (!row || row[RECIPIENT] || !bare(row)) return marks;
|
|
46
|
+
let mark = (lettered)=>lettered ? [
|
|
47
|
+
...marks,
|
|
48
|
+
{
|
|
49
|
+
entity: {
|
|
50
|
+
eid
|
|
51
|
+
},
|
|
52
|
+
[RECIPIENT]: {}
|
|
53
|
+
}
|
|
54
|
+
] : marks;
|
|
55
|
+
return named.has(eid) ? mark(true) : after(pointing(tx, eid), (letters)=>mark(letters.length > 0));
|
|
56
|
+
}), (marks)=>[
|
|
57
|
+
...bundles,
|
|
58
|
+
...marks
|
|
59
|
+
]));
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Marks the address a letter is for `recipient` when it is only an address, so
|
|
63
|
+
* an app's owners and editors read it and a stranger does not.
|
|
64
|
+
*
|
|
65
|
+
* ```ts
|
|
66
|
+
* import { graph } from '@yaks/graph'
|
|
67
|
+
* import { ram } from '@yaks/ram'
|
|
68
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
69
|
+
* import { mailDoc, recipients } from '@yaks/mail'
|
|
70
|
+
* import { equal } from '@yaks/testing'
|
|
71
|
+
*
|
|
72
|
+
* let vocab = loadVocab([mailDoc])
|
|
73
|
+
* let g = graph({ storage: ram(vocab), vocab, plugins: [recipients()] })
|
|
74
|
+
* await g.apply([
|
|
75
|
+
* { entity: { eid: 'a' }, email: { address: 'ann@example.com' } },
|
|
76
|
+
* { entity: { eid: 'l' }, mail: {}, deliver: { to: 'a' } },
|
|
77
|
+
* ])
|
|
78
|
+
* equal((await g.read('.recipient')).map((b) => b.entity.eid), ['a'])
|
|
79
|
+
* ```
|
|
80
|
+
*/ export let recipients = ()=>({
|
|
81
|
+
name: '@yaks/mail/recipients',
|
|
82
|
+
// It only adds a mark to the batch, so a check of a batch can run without it
|
|
83
|
+
// writing.
|
|
84
|
+
admission: ()=>true,
|
|
85
|
+
hooks: {
|
|
86
|
+
rules: hook([
|
|
87
|
+
EMAIL,
|
|
88
|
+
DELIVER
|
|
89
|
+
], marking)
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
//# sourceMappingURL=recipient.js.map
|
package/recipient.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recipient.js","sources":["./recipient.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,wEAAwE;AACxE,6EAA6E;AAC7E,8EAA8E;AAC9E,8EAA8E;AAC9E,6EAA6E;AAC7E,4BAA4B;AAE5B,SAAgC,IAAI,EAAE,MAAM,2BAA6C;AACzF,SAAS,KAAK,kCAAuC;AACrD,SAAS,UAAU,sCAA2C;AAE9D,SAAS,KAAK,EAAE,IAAI,wBAA6B;AACjD,SAAS,GAAG,EAAE,EAAE,2BAAgC;AAChD,SAAS,OAAO,EAAE,KAAK,EAAE,SAAS,oBAAmB;AAErD;QACQ,GACR,OAAO,IAAI,OAAO,CAAC;EACjB,IAAI,QAAQ,MAAM,KAAK,MAAM,CAAC,CAAC,GAAG,EAAE,GAAK,KAAK,MAAM,GAAG,CAAC,C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GAAG;MAChE,OAAO,MAAM,GAAG,CAAC,OACb,KAAK,QACL,MAAM,SAAS,IAAI,MAAM,CAAC,UAAY,KAAK,QAAQ,MAAM,GAAG;IAClE,IACA,CAAC,QAAU;WAAI;WAAY;OAAM;AAGzC;AAEA;;;;;;;;;;;;;;;;;;;CAmBC,GACD,OAAO,IAAI,aAAa,IAAc,CAAC;IACrC,MAAM;IACN,6EAA6E;IAC7E,WAAW;IACX,WAAW,IAAM;IACjB,OAAO;MAAE,OAAO,KAAK;QAAC;QAAO;OAAQ,EAAE;IAAS;EAClD,CAAC,EAAC"}
|
package/recipient.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// The address a letter is for, kept with the letters. A letter's `mail` is read
|
|
2
|
+
// only by an app's owners and editors, but the `email{address}` row its
|
|
3
|
+
// `deliver.to` names carries no such mark, so in a public app anybody listed
|
|
4
|
+
// who had been written to. `email` itself cannot be kept (a public staff list
|
|
5
|
+
// uses it), so the row is marked when it is nothing but an address: a row the
|
|
6
|
+
// app also gave something of its own (`individual`, a form's `form_mail`) is
|
|
7
|
+
// left as the app wrote it.
|
|
8
|
+
|
|
9
|
+
import { type Bundle, type Eid, hook, merged, type Plugin } from '@yaks/graph'
|
|
10
|
+
import { comps } from '@yaks/graph/bundle'
|
|
11
|
+
import { PROVENANCE } from '@yaks/graph/provenance'
|
|
12
|
+
import type { Tx } from 'jsr:@yaks/graph@0.2.36/storage'
|
|
13
|
+
import { after, each } from '@yaks/fp'
|
|
14
|
+
import { and, eq } from '@yaks/query'
|
|
15
|
+
import { DELIVER, EMAIL, RECIPIENT } from './comp.js'
|
|
16
|
+
|
|
17
|
+
/** Whether a row is only an address: it wears `email` and nothing of an app's
|
|
18
|
+
* own. */
|
|
19
|
+
export let bare = (row: Bundle): boolean => {
|
|
20
|
+
let names = comps(row).filter(([, c]) => c != null).map(([n]) => n)
|
|
21
|
+
return names.includes(EMAIL) &&
|
|
22
|
+
names.every((n) => n == EMAIL || n == RECIPIENT || PROVENANCE.includes(n))
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let pointing = (tx: Tx, eid: Eid) =>
|
|
26
|
+
tx.about ? tx.about([eid], [DELIVER]) : tx.read(and(eq(`${DELIVER}.to`, eid)))
|
|
27
|
+
|
|
28
|
+
// The `recipient` mark owed to the addresses this batch writes or names, as
|
|
29
|
+
// they will stand once it is written: an address a letter in the batch is for,
|
|
30
|
+
// or one in the batch that a stored letter is for. A store that answers
|
|
31
|
+
// without waiting is not made to wait: a batch that names no address is
|
|
32
|
+
// passed through as it came.
|
|
33
|
+
let marking = (bundles: Bundle[], tx: Tx): Bundle[] | Promise<Bundle[]> => {
|
|
34
|
+
let named = new Set<Eid>(), written = new Set<Eid>()
|
|
35
|
+
for (let b of bundles) {
|
|
36
|
+
let to = (b[DELIVER] as { to?: unknown } | null | undefined)?.to
|
|
37
|
+
if (typeof to == 'string') named.add(to)
|
|
38
|
+
if (b[EMAIL]) written.add(b.entity.eid)
|
|
39
|
+
}
|
|
40
|
+
let eids = [...new Set([...named, ...written])]
|
|
41
|
+
if (!eids.length) return bundles
|
|
42
|
+
return after(
|
|
43
|
+
tx.get(eids),
|
|
44
|
+
(held) =>
|
|
45
|
+
after(
|
|
46
|
+
each(eids, [] as Bundle[], (marks, eid) => {
|
|
47
|
+
let row = bundles.filter((b) => b.entity.eid == eid).reduce(
|
|
48
|
+
merged,
|
|
49
|
+
held.find((h) => h.entity.eid == eid) ?? null,
|
|
50
|
+
)
|
|
51
|
+
if (!row || row[RECIPIENT] || !bare(row)) return marks
|
|
52
|
+
let mark = (lettered: boolean) =>
|
|
53
|
+
lettered ? [...marks, { entity: { eid }, [RECIPIENT]: {} }] : marks
|
|
54
|
+
return named.has(eid)
|
|
55
|
+
? mark(true)
|
|
56
|
+
: after(pointing(tx, eid), (letters) => mark(letters.length > 0))
|
|
57
|
+
}),
|
|
58
|
+
(marks) => [...bundles, ...marks],
|
|
59
|
+
),
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Marks the address a letter is for `recipient` when it is only an address, so
|
|
65
|
+
* an app's owners and editors read it and a stranger does not.
|
|
66
|
+
*
|
|
67
|
+
* ```ts
|
|
68
|
+
* import { graph } from '@yaks/graph'
|
|
69
|
+
* import { ram } from '@yaks/ram'
|
|
70
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
71
|
+
* import { mailDoc, recipients } from '@yaks/mail'
|
|
72
|
+
* import { equal } from '@yaks/testing'
|
|
73
|
+
*
|
|
74
|
+
* let vocab = loadVocab([mailDoc])
|
|
75
|
+
* let g = graph({ storage: ram(vocab), vocab, plugins: [recipients()] })
|
|
76
|
+
* await g.apply([
|
|
77
|
+
* { entity: { eid: 'a' }, email: { address: 'ann@example.com' } },
|
|
78
|
+
* { entity: { eid: 'l' }, mail: {}, deliver: { to: 'a' } },
|
|
79
|
+
* ])
|
|
80
|
+
* equal((await g.read('.recipient')).map((b) => b.entity.eid), ['a'])
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export let recipients = (): Plugin => ({
|
|
84
|
+
name: '@yaks/mail/recipients',
|
|
85
|
+
// It only adds a mark to the batch, so a check of a batch can run without it
|
|
86
|
+
// writing.
|
|
87
|
+
admission: () => true,
|
|
88
|
+
hooks: { rules: hook([EMAIL, DELIVER], marking) },
|
|
89
|
+
})
|
package/routes.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// The arrival endpoint: the `@yaks/mail/routes` entry point — the one HTTP
|
|
2
|
+
// route a mail edge posts a letter to this graph on.
|
|
3
|
+
//
|
|
4
|
+
// Mail lands at the edge of the world, in front of a domain, and the graph is
|
|
5
|
+
// usually somewhere that edge cannot reach back into. So the letter is posted
|
|
6
|
+
// here, as the message itself: the envelope, the headers exactly as they
|
|
7
|
+
// arrived, and the body as text once something upstream has parsed the MIME.
|
|
8
|
+
// The subject, the Message-ID, the date and the DKIM verdict are read out of
|
|
9
|
+
// those headers (./inbound.ts) rather than repeated in the request body — two
|
|
10
|
+
// copies of one fact is how they come to disagree.
|
|
11
|
+
//
|
|
12
|
+
// Who may post is this plugin's option, not a server-wide setting: which
|
|
13
|
+
// edge a mailbox trusts is a fact about the mailbox. The edge presents the
|
|
14
|
+
// bearer token `door.secret` names, and its word on who wrote a letter
|
|
15
|
+
// (`vouched`) is taken because only it can present that token. With no secret
|
|
16
|
+
// configured the door takes no letter: a letter's caller would otherwise say
|
|
17
|
+
// who wrote it.
|
|
18
|
+
//
|
|
19
|
+
// Nothing here decides what a letter means. It records one, as the configured
|
|
20
|
+
// packages read it (./readings.ts), responds with its id, and the effects
|
|
21
|
+
// registered on `mail` do the rest — which is why posting the same letter
|
|
22
|
+
// twice needs no lock: the Message-ID already identifies which letter this is
|
|
23
|
+
// (./arrive.ts).
|
|
24
|
+
import { Refused } from "@yaks/graph/errors";
|
|
25
|
+
import { json, refuse, Unauthorized } from "@yaks/api";
|
|
26
|
+
import { arrived } from "./arrive.js";
|
|
27
|
+
import { readings } from "./readings.js";
|
|
28
|
+
/** The path a letter arrives on, unless the config sets another. */ export let PATH = '/mail/inbound';
|
|
29
|
+
// Headers as a plain object, read the way a `Headers` is read. A caller sends
|
|
30
|
+
// them with whatever capitalization its runtime used, so the lookup is
|
|
31
|
+
// case-insensitive.
|
|
32
|
+
let head = (said = {})=>{
|
|
33
|
+
let by = new Map(Object.entries(said).map(([k, v])=>[
|
|
34
|
+
k.toLowerCase(),
|
|
35
|
+
String(v)
|
|
36
|
+
]));
|
|
37
|
+
return {
|
|
38
|
+
get: (name)=>by.get(name.toLowerCase()) ?? null
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
// A constant-time comparison: a secret checked with `==` tells anyone willing
|
|
42
|
+
// to time the request how long a prefix they got right. The length still
|
|
43
|
+
// leaks, which is why the secret is a token rather than a password.
|
|
44
|
+
let same = (a, b)=>{
|
|
45
|
+
if (a.length != b.length) return false;
|
|
46
|
+
let diff = 0;
|
|
47
|
+
for(let i = 0; i < a.length; i++)diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
48
|
+
return diff == 0;
|
|
49
|
+
};
|
|
50
|
+
let bearer = (request)=>(request.headers.get('authorization') ?? '').replace(/^Bearer\s+/i, '');
|
|
51
|
+
let said = (body)=>{
|
|
52
|
+
let m = body;
|
|
53
|
+
if (!m || typeof m != 'object' || !m.from || !m.to) {
|
|
54
|
+
throw new Refused('a letter arrives as {from, to, headers?, text?}');
|
|
55
|
+
}
|
|
56
|
+
return m;
|
|
57
|
+
};
|
|
58
|
+
/** `POST /mail/inbound` — one letter, as it arrived. */ export let routes = (host, options = {})=>{
|
|
59
|
+
let { path = PATH, secret } = options.door ?? {};
|
|
60
|
+
// Asked at the first letter, not at start-up: a host serving no letter
|
|
61
|
+
// imports no other package's reading.
|
|
62
|
+
let read;
|
|
63
|
+
return [
|
|
64
|
+
{
|
|
65
|
+
method: 'POST',
|
|
66
|
+
path,
|
|
67
|
+
handle: async (request)=>{
|
|
68
|
+
try {
|
|
69
|
+
if (!secret || !same(secret, bearer(request))) {
|
|
70
|
+
throw new Unauthorized('the arrival endpoint needs a bearer token');
|
|
71
|
+
}
|
|
72
|
+
let { from, to, headers, text, verified, vouched } = said(await request.json());
|
|
73
|
+
let receive = arrived({
|
|
74
|
+
graph: host.graph,
|
|
75
|
+
domain: options.domain,
|
|
76
|
+
triage: options.triage,
|
|
77
|
+
readings: await (read ??= readings(host.config?.plugins))
|
|
78
|
+
});
|
|
79
|
+
let bundles = await receive({
|
|
80
|
+
from,
|
|
81
|
+
to,
|
|
82
|
+
headers: head(headers)
|
|
83
|
+
}, {
|
|
84
|
+
text,
|
|
85
|
+
...verified == null ? {} : {
|
|
86
|
+
verified
|
|
87
|
+
},
|
|
88
|
+
vouched: vouched === true
|
|
89
|
+
});
|
|
90
|
+
// No bundles: this Message-ID is already here. A caller that cannot
|
|
91
|
+
// tell "recorded" from "recorded earlier" would post it again.
|
|
92
|
+
let landed = bundles.length ? await host.graph.apply(bundles) : [];
|
|
93
|
+
return json({
|
|
94
|
+
eid: landed[0]?.entity.eid ?? null
|
|
95
|
+
});
|
|
96
|
+
} catch (err) {
|
|
97
|
+
return refuse(err, request);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
];
|
|
102
|
+
};
|
|
103
|
+
//# sourceMappingURL=routes.js.map
|
package/routes.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"routes.js","sources":["./routes.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,qDAAqD;AACrD,EAAE;AACF,8EAA8E;AAC9E,8EAA8E;AAC9E,yEAAyE;AACzE,6EAA6E;AAC7E,6EAA6E;AAC7E,8EAA8E;AAC9E,mDAAmD;AACnD,EAAE;AACF,yEAAyE;AACzE,2EAA2E;AAC3E,uEAAuE;AACvE,8EAA8E;AAC9E,6EAA6E;AAC7E,gBAAgB;AAChB,EAAE;AACF,8EAA8E;AAC9E,0EAA0E;AAC1E,0EAA0E;AAC1E,8EAA8E;AAC9E,iBAAiB;AAGjB,SAAS,OAAO,kCAAuC;AAEvD,SAAS,IAAI,EAAE,MAAM,EAAc,YAAY,yBAA8B;AAC7E,SAAS,OAAO,sBAAqB;AAGrC,SAAS,QAAQ,wBAAuB;AAExC,kEAAkE,GAClE,OAAO,IAAI,OAAO,gBAAe;AAmBjC,8EAA8E;AAC9E,uEAAuE;AACvE,oBAAoB;AACpB,IAAI,OAAO,CAAC,OAA+B,CAAC,CAAC;EAC3C,IAAI,KAAK,IAAI,IACX,OAAO,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,GAAK;MAAC,EAAE,WAAW;MAAI,OAAO;KAAG;EAEnE,OAAO;IAAE,KAAK,CAAC,OAAS,GAAG,GAAG,CAAC,KAAK,WAAW,OAAO;EAAK;AAC7D;AAEA,8EAA8E;AAC9E,yEAAyE;AACzE,oEAAoE;AACpE,IAAI,OAAO,CAAC,GAA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,IAAI;cAAE;YAAS,CAAC;YACxC,SAAS,YAAY;UACvB;UACA,oEAAoE;UACpE,+DAA+D;UAC/D,IAAI,SAAS,QAAQ,MAAM,GAAG,MAAM,KAAK,KAAK,CAAC,KAAK,CAAC,WAAW,EAAE;UAClE,OAAO,KAAK;YAAE,KAAK,MAAM,CAAC,EAAE,EAAE,OAAO,OAAO;UAAK;QACnD,EAAE,OAAO,KAAK;UACZ,OAAO,OAAO,KAAK;QACrB;MACF;IACF;GAAE;AACJ,EAAC"}
|
package/routes.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// The arrival endpoint: the `@yaks/mail/routes` entry point — the one HTTP
|
|
2
|
+
// route a mail edge posts a letter to this graph on.
|
|
3
|
+
//
|
|
4
|
+
// Mail lands at the edge of the world, in front of a domain, and the graph is
|
|
5
|
+
// usually somewhere that edge cannot reach back into. So the letter is posted
|
|
6
|
+
// here, as the message itself: the envelope, the headers exactly as they
|
|
7
|
+
// arrived, and the body as text once something upstream has parsed the MIME.
|
|
8
|
+
// The subject, the Message-ID, the date and the DKIM verdict are read out of
|
|
9
|
+
// those headers (./inbound.ts) rather than repeated in the request body — two
|
|
10
|
+
// copies of one fact is how they come to disagree.
|
|
11
|
+
//
|
|
12
|
+
// Who may post is this plugin's option, not a server-wide setting: which
|
|
13
|
+
// edge a mailbox trusts is a fact about the mailbox. The edge presents the
|
|
14
|
+
// bearer token `door.secret` names, and its word on who wrote a letter
|
|
15
|
+
// (`vouched`) is taken because only it can present that token. With no secret
|
|
16
|
+
// configured the door takes no letter: a letter's caller would otherwise say
|
|
17
|
+
// who wrote it.
|
|
18
|
+
//
|
|
19
|
+
// Nothing here decides what a letter means. It records one, as the configured
|
|
20
|
+
// packages read it (./readings.ts), responds with its id, and the effects
|
|
21
|
+
// registered on `mail` do the rest — which is why posting the same letter
|
|
22
|
+
// twice needs no lock: the Message-ID already identifies which letter this is
|
|
23
|
+
// (./arrive.ts).
|
|
24
|
+
|
|
25
|
+
import type { Graph } from 'jsr:@yaks/graph@0.2.36'
|
|
26
|
+
import { Refused } from '@yaks/graph/errors'
|
|
27
|
+
import type { Config } from 'jsr:@yaks/host@0.2.36'
|
|
28
|
+
import { json, refuse, type Route, Unauthorized } from '@yaks/api'
|
|
29
|
+
import { arrived } from './arrive.js'
|
|
30
|
+
import type { Head } from './inbound.ts'
|
|
31
|
+
import type { Options } from './options.ts'
|
|
32
|
+
import { readings } from './readings.js'
|
|
33
|
+
|
|
34
|
+
/** The path a letter arrives on, unless the config sets another. */
|
|
35
|
+
export let PATH = '/mail/inbound'
|
|
36
|
+
|
|
37
|
+
/** A letter as it is posted: the message, and what only the poster knows. */
|
|
38
|
+
export type Posted = {
|
|
39
|
+
/** the envelope sender — SMTP plumbing; the author is the `From:` header */
|
|
40
|
+
from: string
|
|
41
|
+
/** the address it was delivered to */
|
|
42
|
+
to: string
|
|
43
|
+
/** its headers, as they arrived */
|
|
44
|
+
headers?: Record<string, string>
|
|
45
|
+
/** the body as text, once something has parsed the MIME */
|
|
46
|
+
text?: string
|
|
47
|
+
/** whether the sending domain signed for it, where the receiving MTA told
|
|
48
|
+
* the caller and left no `Authentication-Results` header to read */
|
|
49
|
+
verified?: boolean
|
|
50
|
+
/** whether the edge authenticated the From address (`Arrival.vouched`) */
|
|
51
|
+
vouched?: boolean
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Headers as a plain object, read the way a `Headers` is read. A caller sends
|
|
55
|
+
// them with whatever capitalization its runtime used, so the lookup is
|
|
56
|
+
// case-insensitive.
|
|
57
|
+
let head = (said: Record<string, string> = {}): Head => {
|
|
58
|
+
let by = new Map(
|
|
59
|
+
Object.entries(said).map(([k, v]) => [k.toLowerCase(), String(v)]),
|
|
60
|
+
)
|
|
61
|
+
return { get: (name) => by.get(name.toLowerCase()) ?? null }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// A constant-time comparison: a secret checked with `==` tells anyone willing
|
|
65
|
+
// to time the request how long a prefix they got right. The length still
|
|
66
|
+
// leaks, which is why the secret is a token rather than a password.
|
|
67
|
+
let same = (a: string, b: string): boolean => {
|
|
68
|
+
if (a.length != b.length) return false
|
|
69
|
+
let diff = 0
|
|
70
|
+
for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i)
|
|
71
|
+
return diff == 0
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
let bearer = (request: Request): string =>
|
|
75
|
+
(request.headers.get('authorization') ?? '').replace(/^Bearer\s+/i, '')
|
|
76
|
+
|
|
77
|
+
let said = (body: unknown): Posted => {
|
|
78
|
+
let m = body as Posted | null
|
|
79
|
+
if (!m || typeof m != 'object' || !m.from || !m.to) {
|
|
80
|
+
throw new Refused('a letter arrives as {from, to, headers?, text?}')
|
|
81
|
+
}
|
|
82
|
+
return m
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** `POST /mail/inbound` — one letter, as it arrived. */
|
|
86
|
+
export let routes = (
|
|
87
|
+
host: { graph: Graph; config?: Config },
|
|
88
|
+
options: Options = {},
|
|
89
|
+
): Route[] => {
|
|
90
|
+
let { path = PATH, secret } = options.door ?? {}
|
|
91
|
+
// Asked at the first letter, not at start-up: a host serving no letter
|
|
92
|
+
// imports no other package's reading.
|
|
93
|
+
let read: ReturnType<typeof readings> | undefined
|
|
94
|
+
return [{
|
|
95
|
+
method: 'POST',
|
|
96
|
+
path,
|
|
97
|
+
handle: async (request) => {
|
|
98
|
+
try {
|
|
99
|
+
if (!secret || !same(secret, bearer(request))) {
|
|
100
|
+
throw new Unauthorized('the arrival endpoint needs a bearer token')
|
|
101
|
+
}
|
|
102
|
+
let { from, to, headers, text, verified, vouched } = said(
|
|
103
|
+
await request.json(),
|
|
104
|
+
)
|
|
105
|
+
let receive = arrived({
|
|
106
|
+
graph: host.graph,
|
|
107
|
+
domain: options.domain,
|
|
108
|
+
triage: options.triage,
|
|
109
|
+
readings: await (read ??= readings(host.config?.plugins)),
|
|
110
|
+
})
|
|
111
|
+
let bundles = await receive({ from, to, headers: head(headers) }, {
|
|
112
|
+
text,
|
|
113
|
+
...(verified == null ? {} : { verified }),
|
|
114
|
+
vouched: vouched === true,
|
|
115
|
+
})
|
|
116
|
+
// No bundles: this Message-ID is already here. A caller that cannot
|
|
117
|
+
// tell "recorded" from "recorded earlier" would post it again.
|
|
118
|
+
let landed = bundles.length ? await host.graph.apply(bundles) : []
|
|
119
|
+
return json({ eid: landed[0]?.entity.eid ?? null })
|
|
120
|
+
} catch (err) {
|
|
121
|
+
return refuse(err, request)
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
}]
|
|
125
|
+
}
|