@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.
Files changed (131) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +676 -2
  3. package/_dist/addr.d.ts +40 -0
  4. package/_dist/addr.d.ts.map +1 -0
  5. package/_dist/answers.d.ts +18 -0
  6. package/_dist/answers.d.ts.map +1 -0
  7. package/_dist/arrive.d.ts +64 -0
  8. package/_dist/arrive.d.ts.map +1 -0
  9. package/_dist/capture.d.ts +22 -0
  10. package/_dist/capture.d.ts.map +1 -0
  11. package/_dist/comp.d.ts +27 -0
  12. package/_dist/comp.d.ts.map +1 -0
  13. package/_dist/effects.d.ts +9 -0
  14. package/_dist/effects.d.ts.map +1 -0
  15. package/_dist/gmail.d.ts +16 -0
  16. package/_dist/gmail.d.ts.map +1 -0
  17. package/_dist/graph.d.ts +9 -0
  18. package/_dist/graph.d.ts.map +1 -0
  19. package/_dist/inbound.d.ts +105 -0
  20. package/_dist/inbound.d.ts.map +1 -0
  21. package/_dist/invite.d.ts +69 -0
  22. package/_dist/invite.d.ts.map +1 -0
  23. package/_dist/md.d.ts +34 -0
  24. package/_dist/md.d.ts.map +1 -0
  25. package/_dist/mod.d.ts +109 -0
  26. package/_dist/mod.d.ts.map +1 -0
  27. package/_dist/options.d.ts +38 -0
  28. package/_dist/options.d.ts.map +1 -0
  29. package/_dist/plugin.d.ts +38 -0
  30. package/_dist/plugin.d.ts.map +1 -0
  31. package/_dist/pull.d.ts +99 -0
  32. package/_dist/pull.d.ts.map +1 -0
  33. package/_dist/receipts.d.ts +8 -0
  34. package/_dist/receipts.d.ts.map +1 -0
  35. package/_dist/recipient.d.ts +24 -0
  36. package/_dist/recipient.d.ts.map +1 -0
  37. package/_dist/routes.d.ts +19 -0
  38. package/_dist/routes.d.ts.map +1 -0
  39. package/_dist/send.d.ts +80 -0
  40. package/_dist/send.d.ts.map +1 -0
  41. package/_dist/service.d.ts +14 -0
  42. package/_dist/service.d.ts.map +1 -0
  43. package/_dist/stash.d.ts +23 -0
  44. package/_dist/stash.d.ts.map +1 -0
  45. package/_dist/tools.d.ts +27 -0
  46. package/_dist/tools.d.ts.map +1 -0
  47. package/_dist/transport.d.ts +6 -0
  48. package/_dist/transport.d.ts.map +1 -0
  49. package/_dist/vocab.d.ts +7 -0
  50. package/_dist/vocab.d.ts.map +1 -0
  51. package/addr.js +92 -0
  52. package/addr.js.map +1 -0
  53. package/addr.ts +106 -0
  54. package/answers.js +71 -0
  55. package/answers.js.map +1 -0
  56. package/answers.ts +95 -0
  57. package/arrive.js +122 -0
  58. package/arrive.js.map +1 -0
  59. package/arrive.ts +198 -0
  60. package/capture.js +21 -0
  61. package/capture.js.map +1 -0
  62. package/capture.ts +43 -0
  63. package/comp.js +76 -0
  64. package/comp.js.map +1 -0
  65. package/comp.ts +91 -0
  66. package/deno.json +1 -0
  67. package/effects.js +90 -0
  68. package/effects.js.map +1 -0
  69. package/effects.ts +90 -0
  70. package/gmail.js +262 -0
  71. package/gmail.js.map +1 -0
  72. package/gmail.ts +314 -0
  73. package/graph.js +26 -0
  74. package/graph.js.map +1 -0
  75. package/graph.ts +30 -0
  76. package/inbound.js +206 -0
  77. package/inbound.js.map +1 -0
  78. package/inbound.ts +238 -0
  79. package/invite.js +102 -0
  80. package/invite.js.map +1 -0
  81. package/invite.ts +143 -0
  82. package/md.js +188 -0
  83. package/md.js.map +1 -0
  84. package/md.ts +245 -0
  85. package/mod.js +109 -0
  86. package/mod.js.map +1 -0
  87. package/mod.ts +111 -0
  88. package/options.js +24 -0
  89. package/options.js.map +1 -0
  90. package/options.ts +83 -0
  91. package/package.json +66 -4
  92. package/plugin.js +97 -0
  93. package/plugin.js.map +1 -0
  94. package/plugin.ts +88 -0
  95. package/pull.js +344 -0
  96. package/pull.js.map +1 -0
  97. package/pull.ts +443 -0
  98. package/readings.js +24 -0
  99. package/readings.js.map +1 -0
  100. package/readings.ts +34 -0
  101. package/receipts.js +45 -0
  102. package/receipts.js.map +1 -0
  103. package/receipts.ts +45 -0
  104. package/recipient.js +92 -0
  105. package/recipient.js.map +1 -0
  106. package/recipient.ts +89 -0
  107. package/routes.js +103 -0
  108. package/routes.js.map +1 -0
  109. package/routes.ts +125 -0
  110. package/send.js +284 -0
  111. package/send.js.map +1 -0
  112. package/send.ts +325 -0
  113. package/service.js +64 -0
  114. package/service.js.map +1 -0
  115. package/service.ts +75 -0
  116. package/stash.js +29 -0
  117. package/stash.js.map +1 -0
  118. package/stash.ts +49 -0
  119. package/tools.js +343 -0
  120. package/tools.js.map +1 -0
  121. package/tools.ts +416 -0
  122. package/transport.js +23 -0
  123. package/transport.js.map +1 -0
  124. package/transport.ts +24 -0
  125. package/vocab.js +14 -0
  126. package/vocab.js.map +1 -0
  127. package/vocab.json +415 -0
  128. package/vocab.ts +16 -0
  129. package/work.js +121 -0
  130. package/work.js.map +1 -0
  131. package/work.ts +134 -0
@@ -0,0 +1,38 @@
1
+ import type { Plugin } from "@yaks/graph";
2
+ import type { Effects } from "@yaks/effects";
3
+ import { type Post } from "./send.js";
4
+ /** How the plugin is built. */ export type Mailbox = {
5
+ /** your own mail domain — the addresses this graph canonicalizes on write */ domain?: string;
6
+ /** handle `mail_post` on this registry, so letters actually go. The
7
+ * registry's vocabulary carries {@link mailDoc}, which declares it, and it
8
+ * needs a write function — `effects(vocab, { write })`, applied trusted —
9
+ * since that is how the outcome is written back onto the letter. */ effects?: Effects;
10
+ } & Partial<Post>;
11
+ /**
12
+ * The mail plugin: the `mail`, `email`, `deliver`, `delivered`, `bounced` and
13
+ * `notified` components, the address canonicalizer, and the sending effect.
14
+ *
15
+ * ```ts
16
+ * import { graph } from '@yaks/graph'
17
+ * import { effects } from '@yaks/effects'
18
+ * import { ram } from '@yaks/ram'
19
+ * import { loadVocab } from '@yaks/vocab'
20
+ * import { docDoc, docs } from '@yaks/doc'
21
+ * import { mailbox, mailDoc, stash } from '@yaks/mail'
22
+ *
23
+ * let vocab = loadVocab([docDoc, mailDoc])
24
+ * let fx = effects(vocab, { write: (b) => g.apply(b, { trusted: true }) })
25
+ * let box = mailbox({ domain: 'books.example', sender: stash(), effects: fx })
26
+ * let g = graph({ storage: ram(vocab), vocab, plugins: [fx, docs(), box] })
27
+ * ```
28
+ *
29
+ * {@link https://jsr.io/@yaks/doc | @yaks/doc} is composed beside this plugin
30
+ * rather than inside it: a letter's subject and body are `doc{title, body}`, and
31
+ * a vocabulary rejects a component declared twice — so `doc` keeps one home and
32
+ * an application that already declares it is not fought over it.
33
+ *
34
+ * Pass `effects` and a `sender` together and outbound letters are sent
35
+ * automatically; pass neither and this is the vocabulary and the canonicalizer,
36
+ * which is all a graph that only receives mail needs.
37
+ */ export declare let mailbox: (_dts_1: never) => Plugin;
38
+ //# sourceMappingURL=plugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.d.ts","sources":["../plugin.ts"],"names":[],"mappings":"AAaA,cAA4B,MAAM,2BAAgC;AAClE,cAAc,OAAO,6BAAkC;AAGvD,SAAS,KAAK,IAAI,oBAA4B;AAE9C,6BAA6B,GAC7B,YAAY;EACV,2EAA2E,GAC3E,SAAS,MAAM;EACf;;;oEAGkE,GAClE,UAAU;IACR,QAAQ;AAoBZ;;;;;;;;;;;;;;;;;;;;;;;;;;CA0BC,GACD,OAAO,YAAI,UACT,kBACC,OAUF"}
@@ -0,0 +1,99 @@
1
+ import { type Outage } from "@yaks/effects/outage";
2
+ import type { Eid, Graph } from "@yaks/graph";
3
+ import { type Arrivals } from "./arrive.js";
4
+ import type { Arrival, Received } from "./inbound.js";
5
+ import type { Pull } from "./options.js";
6
+ /** Where a failure is told: the host's telemetry, else the console. */ export type Report = (error: unknown, context?: {
7
+ tags?: Record<string, string>;
8
+ }) => unknown;
9
+ /** How long a letter may wait at the edge before taking it is itself a
10
+ * failure: a pull runs every few seconds, so a letter this old, which a pass
11
+ * that began after it arrived did not take, was held up at the edge. */ export declare let LATE: number;
12
+ /** A letter as the edge holds it: the envelope and what it parsed out. */ export type EdgeMessage = {
13
+ /** the edge's key for it: `msg:<ms>:<Message-ID>` */ id: string;
14
+ /** when it arrived, in milliseconds */ ts?: number | null;
15
+ /** when it arrived, as ISO */ received_at?: string | null;
16
+ /** `in` for an arrival */ dir?: string | null;
17
+ /** the envelope sender */ from?: string | null;
18
+ /** the `From:` header — the author */ from_header?: string | null;
19
+ /** the address it was delivered to */ to?: string | null;
20
+ subject?: string | null;
21
+ /** the body as text */ text?: string | null;
22
+ /** whether the edge authenticated the From address: an aligned DMARC or
23
+ * DKIM pass, as the fleet's inbox worker grades it, so it also vouches for
24
+ * the author (`Arrival.vouched`) */ verified?: boolean | null;
25
+ /** the `In-Reply-To:` header */ in_reply_to?: string | null;
26
+ /** the `Reply-To:` header, as written */ reply_to?: string | null;
27
+ /** the headers the edge kept: a JSON object, as text */ headers?: string | null;
28
+ };
29
+ /** A request as the edge holds it, captured as it was received. */ export type EdgeRequest = {
30
+ id: string;
31
+ ts?: number | null;
32
+ /** who sent it, as the edge named them */ source?: string | null;
33
+ method?: string | null;
34
+ path?: string | null;
35
+ /** the headers: a JSON object, as text */ headers?: string | null;
36
+ body?: string | null;
37
+ /** 1 where the sender's signature verified, 0 where it did not */ sig_ok?: number | null;
38
+ };
39
+ /** The edge as a pull uses it — a test hands in fixtures instead. `requests`
40
+ * answers null where the edge has no request tray. */ export type Edge = {
41
+ messages: () => Promise<EdgeMessage[]>;
42
+ notified: (ids: string[]) => Promise<void>;
43
+ requests: () => Promise<EdgeRequest[] | null>;
44
+ processed: (ids: string[]) => Promise<void>;
45
+ };
46
+ /** How long one request to the edge may make no answer. */ export declare let WAIT: number;
47
+ /** A transport failure another pass can recover from. Parsing, authentication
48
+ * and recording failures are unexpected and still report immediately. */ export declare class Unavailable extends Error {
49
+ }
50
+ /** The edge's HTTP API, with the bearer token it asks for. */ export declare let edge: ({}: Pull, go?: (url: string, init: RequestInit) => Promise<Response>, stop?: AbortSignal) => Edge;
51
+ /**
52
+ * The Message-ID inside the edge's key for a letter.
53
+ *
54
+ * ```ts
55
+ * import { messageIdOf } from '@yaks/mail'
56
+ * messageIdOf('msg:1789861530206:<a1@x.example>') // 'a1@x.example'
57
+ * messageIdOf('a1@x.example') // 'a1@x.example'
58
+ * ```
59
+ */ export declare let messageIdOf: (key: string) => string;
60
+ /** A letter the edge holds, as the message and arrival ./arrive.ts records:
61
+ * the headers it kept, with the ones it parsed out written back over them. */ export declare let received: (m: EdgeMessage) => [Received, Arrival];
62
+ /** Whom a request is for: the mailbox its first path segment names at your
63
+ * domain — `/hook/books/…` is whoever has `books@<domain>` — else triage. */ export declare let hookTo: ({}: Arrivals, path: string | null | undefined) => Promise<Eid | undefined>;
64
+ /** Each tray's outage (@yaks/effects/outage), kept until it answers, with
65
+ * the failure a delayed letter reports as its cause. A caller that retries
66
+ * shares this map across passes. */ export type Down = Map<string, Outage & {
67
+ error: unknown;
68
+ }>;
69
+ /** What one pull took: how many letters and requests it recorded and
70
+ * acknowledged. */ export type Pulled = {
71
+ messages: number;
72
+ requests: number;
73
+ };
74
+ /**
75
+ * One pull: take the letters and the requests the edge holds, record them, and
76
+ * acknowledge them. Each tray fails alone, so an edge that cannot list its
77
+ * letters still hands over its requests.
78
+ *
79
+ * Requests are recorded only where the graph declares `hook`, and aimed at
80
+ * whom they are for only where it declares `about`; a graph without `hook`
81
+ * leaves the request tray for one that has it.
82
+ *
83
+ * A caller that pulls again hands in the same `down` each pass. Transient
84
+ * failures are logged and retained as the cause of a delayed letter's report;
85
+ * without another pass, every failure reports at once.
86
+ *
87
+ * It also hands in `since`, when its previous pass began. A letter that
88
+ * arrived after that waited only for this pass, however long the process
89
+ * between them was stopped or frozen, so it is not late at the edge; the
90
+ * process's own stall says why the pass was late.
91
+ */ export declare let pull: (at: Arrivals & {
92
+ graph: Graph;
93
+ report?: Report;
94
+ log?: Report;
95
+ now?: () => number;
96
+ down?: Down;
97
+ since?: number;
98
+ }, from: Edge) => Promise<Pulled>;
99
+ //# sourceMappingURL=pull.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pull.d.ts","sources":["../pull.ts"],"names":[],"mappings":"AAiCA,SAA4B,KAAK,MAAM,oCAAyC;AAChF,cAAc,GAAG,EAAE,KAAK,2BAAgC;AAExD,SAAS,KAAK,QAAQ,sBAAsC;AAC5D,cAAc,OAAO,EAAE,QAAQ,uBAAsB;AACrD,cAAc,IAAI,uBAAsB;AAExC,qEAAqE,GACrE,YAAY,UACV,OAAO,OAAO,EACd;EAAY,OAAO,OAAO,MAAM,EAAE,MAAM;MACrC,OAAO;AAEZ;;sEAEsE,GACtE,OAAO,YAAI,aAAa;AAExB,wEAAwE,GACxE,YAAY;EACV,mDAAmD,GACnD,IAAI,MAAM;EACV,qCAAqC,GACrC,KAAK,MAAM,GAAG,IAAI;EAClB,4BAA4B,GAC5B,cAAc,MAAM,GAAG,IAAI;EAC3B,wBAAwB,GACxB,MAAM,MAAM,GAAG,IAAI;EACnB,wBAAwB,GACxB,OAAO,MAAM,GAAG,IAAI;EACpB,oCAAoC,GACpC,cAAc,MAAM,GAAG,IAAI;EAC3B,oCAAoC,GACpC,KAAK,MAAM,GAAG,IAAI;EAClB,UAAU,MAAM,GAAG,IAAI;EACvB,qBAAqB,GACrB,OAAO,MAAM,GAAG,IAAI;EACpB;;oCAEkC,GAClC,WAAW,OAAO,GAAG,IAAI;EACzB,8BAA8B,GAC9B,cAAc,MAAM,GAAG,IAAI;EAC3B,uCAAuC,GACvC,WAAW,MAAM,GAAG,IAAI;EACxB,sDAAsD,GACtD,UAAU,MAAM,GAAG,IAAI;;AAGzB,iEAAiE,GACjE,YAAY;EACV,IAAI,MAAM;EACV,KAAK,MAAM,GAAG,IAAI;EAClB,wCAAwC,GACxC,SAAS,MAAM,GAAG,IAAI;EACtB,SAAS,MAAM,GAAG,IAAI;EACtB,OAAO,MAAM,GAAG,IAAI;EACpB,wCAAwC,GACxC,UAAU,MAAM,GAAG,IAAI;EACvB,OAAO,MAAM,GAAG,IAAI;EACpB,gEAAgE,GAChE,SAAS,MAAM,GAAG,IAAI;;AAGxB;oDACoD,GACpD,YAAY;EACV,gBAAgB,QAAQ;EACxB,WAAW,KAAK,MAAM,OAAO,QAAQ,IAAI;EACzC,gBAAgB,QAAQ,gBAAgB,IAAI;EAC5C,YAAY,KAAK,MAAM,OAAO,QAAQ,IAAI;;AAG5C,yDAAyD,GACzD,OAAO,YAAI,aAAa;AAExB;uEACuE,GACvE,OAAO,cAAM,oBAAoB;AAAO;AAExC,4DAA4D,GAC5D,OAAO,YAAI,OACT,IAAgB,IAAI,EACpB,MAAK,KAAK,MAAM,EAAE,MAAM,gBAAgB,QAAQ,WAChD,OAAO,gBAEN,KAyEF;AAED;;;;;;;;CAQC,GACD,OAAO,YAAI,cAAe,KAAK,MAAM,KAAG,MAAM,CACgC;AAW9E;4EAC4E,GAC5E,OAAO,YAAI,WAAY,GAAG,iBAAe,UAAU,SA6BlD;AAED;2EAC2E,GAC3E,OAAO,YAAI,SACT,IAA2B,QAAQ,EACnC,MAAM,MAAM,GAAG,IAAI,GAAG,SAAS,KAC9B,QAAQ,MAAM,SAAS,EAMzB;AAaD;;kCAEkC,GAClC,YAAY,OAAO,IAAI,MAAM,EAAE;EAAW,OAAO,OAAO;;AAiExD;iBACiB,GACjB,YAAY;EAAW,UAAU,MAAM;EAAE,UAAU,MAAM;;AAEzD;;;;;;;;;;;;;;;;;CAiBC,GACD,OAAO,YAAI,OACT,IAAI;EACF,OAAO;EACP,SAAS;EACT,MAAM;EACN,YAAY,MAAM;EAClB,OAAO;EACP,QAAQ,MAAM;GAEhB,MAAM,SACL,QAAQ,QAsEV"}
@@ -0,0 +1,8 @@
1
+ import type { Bundle, Graph } from "@yaks/graph";
2
+ /** The validated envelope values the transport files after handing off
3
+ * an outbound letter. Other fields can carry words from outside. */ export declare let deliveryField: (row: Bundle, []: string[]) => boolean;
4
+ /** Apply a provider-neutral receipt to the same recorded letter. */ export declare let receipted: (graph: Pick<Graph, 'get' | 'apply'>, found: {
5
+ letter: string;
6
+ id: string;
7
+ }) => Promise<void>;
8
+ //# sourceMappingURL=receipts.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"receipts.d.ts","sources":["../receipts.ts"],"names":[],"mappings":"AAKA,cAAc,MAAM,EAAQ,KAAK,2BAAgC;AAKjE;kEACkE,GAClE,OAAO,YAAI,gBACT,KAAK,QACL,IAAuB,MAAM,EAAE,KAC9B,OAAO,CAMT;AAED,kEAAkE,GAClE,OAAO,YAAI,YACT,OAAO,KAAK,OAAO,QAAQ,UAC3B;EAAS,QAAQ,MAAM;EAAE,IAAI,MAAM;MAClC,QAAQ,IAAI,EAiBd"}
@@ -0,0 +1,24 @@
1
+ import { type Bundle, type Plugin } from "@yaks/graph";
2
+ /** Whether a row is only an address: it wears `email` and nothing of an app's
3
+ * own. */ export declare let bare: (row: Bundle) => boolean;
4
+ /**
5
+ * Marks the address a letter is for `recipient` when it is only an address, so
6
+ * an app's owners and editors read it and a stranger does not.
7
+ *
8
+ * ```ts
9
+ * import { graph } from '@yaks/graph'
10
+ * import { ram } from '@yaks/ram'
11
+ * import { loadVocab } from '@yaks/vocab'
12
+ * import { mailDoc, recipients } from '@yaks/mail'
13
+ * import { equal } from '@yaks/testing'
14
+ *
15
+ * let vocab = loadVocab([mailDoc])
16
+ * let g = graph({ storage: ram(vocab), vocab, plugins: [recipients()] })
17
+ * await g.apply([
18
+ * { entity: { eid: 'a' }, email: { address: 'ann@example.com' } },
19
+ * { entity: { eid: 'l' }, mail: {}, deliver: { to: 'a' } },
20
+ * ])
21
+ * equal((await g.read('.recipient')).map((b) => b.entity.eid), ['a'])
22
+ * ```
23
+ */ export declare let recipients: () => Plugin;
24
+ //# sourceMappingURL=recipient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"recipient.d.ts","sources":["../recipient.ts"],"names":[],"mappings":"AAQA,SAAS,KAAK,MAAM,EAA0B,KAAK,MAAM,2BAAgC;AAQzF;QACQ,GACR,OAAO,YAAI,OAAQ,KAAK,WAAS,OAAO,CAIvC;AAwCD;;;;;;;;;;;;;;;;;;;CAmBC,GACD,OAAO,YAAI,kBAAiB,OAM1B"}
@@ -0,0 +1,19 @@
1
+ import type { Graph } from "@yaks/graph";
2
+ import type { Config } from "@yaks/host";
3
+ import { type Route } from "@yaks/api";
4
+ import type { Options } from "./options.js";
5
+ /** The path a letter arrives on, unless the config sets another. */ export declare let PATH: string;
6
+ /** A letter as it is posted: the message, and what only the poster knows. */ export type Posted = {
7
+ /** the envelope sender — SMTP plumbing; the author is the `From:` header */ from: string;
8
+ /** the address it was delivered to */ to: string;
9
+ /** its headers, as they arrived */ headers?: Record<string, string>;
10
+ /** the body as text, once something has parsed the MIME */ text?: string;
11
+ /** whether the sending domain signed for it, where the receiving MTA told
12
+ * the caller and left no `Authentication-Results` header to read */ verified?: boolean;
13
+ /** whether the edge authenticated the From address (`Arrival.vouched`) */ vouched?: boolean;
14
+ };
15
+ /** `POST /mail/inbound` — one letter, as it arrived. */ export declare let routes: (host: {
16
+ graph: Graph;
17
+ config?: Config;
18
+ }, options?: Options) => Route[];
19
+ //# sourceMappingURL=routes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"routes.d.ts","sources":["../routes.ts"],"names":[],"mappings":"AAwBA,cAAc,KAAK,2BAAgC;AAEnD,cAAc,MAAM,0BAA+B;AACnD,SAAuB,KAAK,KAAK,yBAA4C;AAG7E,cAAc,OAAO,uBAAsB;AAG3C,kEAAkE,GAClE,OAAO,YAAI,aAAsB;AAEjC,2EAA2E,GAC3E,YAAY;EACV,0EAA0E,GAC1E,MAAM,MAAM;EACZ,oCAAoC,GACpC,IAAI,MAAM;EACV,iCAAiC,GACjC,UAAU,OAAO,MAAM,EAAE,MAAM;EAC/B,yDAAyD,GACzD,OAAO,MAAM;EACb;oEACkE,GAClE,WAAW,OAAO;EAClB,wEAAwE,GACxE,UAAU,OAAO;;AAkCnB,sDAAsD,GACtD,OAAO,YAAI,SACT;EAAQ,OAAO;EAAO,SAAS;GAC/B,UAAS,YACR,QAoCF"}
@@ -0,0 +1,80 @@
1
+ import type { Bundle, Entity, ReadTx } from "@yaks/graph";
2
+ import type { Handler } from "@yaks/effects";
3
+ import { type Names } from "./md.js";
4
+ import type { Message, Sender } from "@yaks/host/senders";
5
+ import { Undeliverable, Unsent } from "@yaks/host/senders";
6
+ export type { Message, Receipt, Sender } from "@yaks/host/senders";
7
+ export { Undeliverable, Unsent };
8
+ /** How the effect is built. */ export type Post = {
9
+ /** what to do about a letter that asks to be sent */ sender: Sender;
10
+ /** the clock, passed in so a test can hold it still (default: now) */ now?: () => string;
11
+ /** a domain whose addresses belong to this graph rather than to a mail
12
+ * server. A letter to one is delivered by writing it — see below. */ local?: string;
13
+ /** where an entity a letter names opens: an address with `{id}` where its
14
+ * id goes (`https://tasks.yak.sh/{id}`). Each eid a body holds then reads as
15
+ * the entity's id, linked there; with none, as the id alone. */ mentions?: string;
16
+ };
17
+ /** The whole letter as it stands, post-commit. An effect works from storage
18
+ * rather than from the patch, so it sees `mail` and `deliver` together however
19
+ * the transaction that wrote them was shaped. */ export declare let letterOf: (tx: ReadTx, entity: Entity) => Bundle | undefined | Promise<Bundle | undefined>;
20
+ /** Whether a letter is still owed a send: it asks to go, it was never handed
21
+ * to a sender, and it has no outcome. A letter handed over and never settled
22
+ * may have left, so it is not owed — sending is at most once. */ export declare let owed: (letter: Bundle | undefined) => boolean;
23
+ /** What a letter calls each entity its body names: the entity's id (`T-453`),
24
+ * and its page at `mentions`, read from the store at send. One the store does
25
+ * not hold names nothing, and stays as written. */ export declare let namesOf: (tx: ReadTx, body: string, mentions?: string) => Names | Promise<Names>;
26
+ /** The address an entity is reachable at: its own `email.address`. */ export declare let addressOf: (tx: ReadTx, eid: string) => string | Promise<string>;
27
+ /**
28
+ * The letter as a message: the subject and both body renderings, with the
29
+ * addresses already resolved. Pure — a test asserts on it without a transport
30
+ * anywhere.
31
+ *
32
+ * It takes the whole letter rather than one component, because a letter is two
33
+ * of them: the envelope is `mail` and the words are
34
+ * {@link https://jsr.io/@yaks/doc | @yaks/doc}'s `doc{title, body}`.
35
+ */ export declare let message: (letter: Bundle, to: string, replyTo?: string, names?: Names) => Message;
36
+ /**
37
+ * The `created(mail)` handler: hand an outbound letter to the sender, and
38
+ * write back what became of it.
39
+ *
40
+ * ```ts
41
+ * import { effects } from '@yaks/effects'
42
+ * import { graph } from '@yaks/graph'
43
+ * import { ram } from '@yaks/ram'
44
+ * import { loadVocab } from '@yaks/vocab'
45
+ * import { docDoc, docs } from '@yaks/doc'
46
+ * import { mailDoc, sending, stash } from '@yaks/mail'
47
+ *
48
+ * let vocab = loadVocab([docDoc, mailDoc])
49
+ * let fx = effects(vocab, { write: (b) => g.apply(b, { trusted: true }) })
50
+ * let g = graph({ storage: ram(vocab), vocab, plugins: [fx, docs()] })
51
+ * fx.handle({ mail_post: sending({ sender: stash() }) })
52
+ * ```
53
+ *
54
+ * It reads the whole entity, so it does not care which component triggered
55
+ * it, and it is idempotent: only a
56
+ * letter still {@link owed} a send is touched, and `deliver.tried` is written
57
+ * before the letter reaches the transport, so a crash between the send and its
58
+ * outcome leaves a letter that is never handed over twice. The one exception
59
+ * is a sender's `Unsent` rejection, which knows the letter never left: the
60
+ * claim is given back and the run fails to be tried again, so a dropped
61
+ * connection is a wait, not a bounce. A sender's `Undeliverable` rejection is
62
+ * a bounce and nothing more: the provider refused an address on the letter,
63
+ * so the run ends with the letter bounced rather than failing.
64
+ *
65
+ * The outcome is written back through @yaks/effects' write function — a new
66
+ * transaction through the graph's own `apply()` — so "this letter left" is
67
+ * journaled and pushed to whoever is subscribed to the letter, rather than a
68
+ * row they find next time they read. The registry needs that function:
69
+ * `effects(vocab, { write })`, applied trusted, since `delivered` and
70
+ * `bounced` are the sender's report and therefore server-owned.
71
+ *
72
+ * `local` names a domain whose addresses belong to this graph — an agent, a
73
+ * project, anything reachable here and nowhere else. A letter to one is
74
+ * already where it is going, so it is stamped `delivered` and never handed to
75
+ * the transport; sending it out would bring it back as a
76
+ * second letter about the same words. Leave it out where somebody reads that
77
+ * domain's mail in a mail client: then the mailbox is the destination, and the
78
+ * graph is only the record.
79
+ */ export declare let sending: ({}: Post) => Handler;
80
+ //# sourceMappingURL=send.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"send.d.ts","sources":["../send.ts"],"names":[],"mappings":"AAyBA,cAAc,MAAM,EAAQ,MAAM,EAAE,MAAM,2BAAgC;AAI1E,cAAc,OAAO,6BAAkC;AAIvD,SAAqB,KAAK,KAAK,kBAAuB;AAEtD,cAAc,OAAO,EAAE,MAAM,kCAAuC;AACpE,SAAS,aAAa,EAAE,MAAM,kCAAuC;AACrE,cAAc,OAAO,EAAE,OAAO,EAAE,MAAM,kCAAuC;AAC7E,SAAS,aAAa,EAAE,MAAM,GAAE;AAEhC,6BAA6B,GAC7B,YAAY;EACV,mDAAmD,GACnD,QAAQ;EACR,oEAAoE,GACpE,YAAY,MAAM;EAClB;qEACmE,GACnE,QAAQ,MAAM;EACd;;gEAE8D,GAC9D,WAAW,MAAM;;AAKnB;;+CAE+C,GAC/C,OAAO,YAAI,WACT,IAAI,QACJ,QAAQ,WACP,SAAS,SAAS,GAAG,QAAQ,SAAS,SAAS,EACA;AAQlD;;+DAE+D,GAC/D,OAAO,YAAI,OAAQ,QAAQ,SAAS,SAAS,KAAG,OAAO,CAMH;AAEpD;;iDAEiD,GACjD,OAAO,YAAI,UACT,IAAI,QACJ,MAAM,MAAM,EACZ,WAAW,MAAM,KAChB,QAAQ,QAAQ,OAgBlB;AAED,oEAAoE,GACpE,OAAO,YAAI,YACT,IAAI,QACJ,KAAK,MAAM,KACV,MAAM,GAAG,QAAQ,MAAM,EAC8C;AAOxE;;;;;;;;CAQC,GACD,OAAO,YAAI,UACT,QAAQ,QACR,IAAI,MAAM,EACV,UAAU,MAAM,EAChB,QAAQ,UACP,QAkBF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA2CC,GACD,OAAO,YAAI,UACR,IAA0C,IAAI,KAAG,QA6H9C"}
@@ -0,0 +1,14 @@
1
+ import type { Graph } from "@yaks/graph";
2
+ import type { Config } from "@yaks/host";
3
+ import type { Options, Pull } from "./options.js";
4
+ import { type Edge, type Report } from "./pull.js";
5
+ /** How long a pull waits for the next one, unless the config says. */ export declare let EVERY: number;
6
+ type Host = {
7
+ graph: Graph;
8
+ config?: Config;
9
+ report?: Report;
10
+ log?: Report;
11
+ };
12
+ type Open = (options: Pull, stop?: AbortSignal) => Edge;
13
+ /** Pull what arrived, then keep polling until the host stops. */ export declare let service: (host: Host, options?: Options, signal?: AbortSignal, make?: Open) => Promise<void>;
14
+ //# sourceMappingURL=service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"service.d.ts","sources":["../service.ts"],"names":[],"mappings":"AAYA,cAAc,KAAK,2BAAgC;AACnD,cAAc,MAAM,0BAA+B;AAEnD,cAAc,OAAO,EAAE,IAAI,uBAAsB;AACjD,SAAoB,KAAK,IAAI,EAAc,KAAK,MAAM,oBAAmB;AAGzE,oEAAoE,GACpE,OAAO,YAAI,cAAc;KAEpB;EAAS,OAAO;EAAO,SAAS;EAAQ,SAAS;EAAQ,MAAM;;KA+B/D,QAAQ,SAAS,MAAM,OAAO,gBAAgB;AAInD,+DAA+D,GAC/D,OAAO,YAAI,UACT,MAAM,MACN,UAAS,SACT,SAAQ,aACR,OAAM,SACL,QAAQ,IAAI,EAWd"}
@@ -0,0 +1,23 @@
1
+ import type { Message, Sender } from "./send.js";
2
+ /** A {@link Sender} that keeps what it was given. */ export type Stash = Sender & {
3
+ /** every message handed over, in order */ sent: Message[];
4
+ /** the last one, for the common assertion */ last: () => Message | undefined;
5
+ };
6
+ /** How a stash behaves. */ export type Kept = {
7
+ /** reject every message with this reason, to exercise the bounce path */ refuse?: string;
8
+ };
9
+ /**
10
+ * A sender that keeps messages in memory.
11
+ *
12
+ * ```ts
13
+ * import { stash } from '@yaks/mail'
14
+ *
15
+ * let post = stash()
16
+ * // … send a letter …
17
+ * post.last()?.subject // 'You are invited'
18
+ * ```
19
+ *
20
+ * Receipts are `stash-1`, `stash-2`, … so a test can assert on the
21
+ * `mail.message_id` the letter was given.
22
+ */ export declare let stash: (_dts_1: never) => Stash;
23
+ //# sourceMappingURL=stash.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stash.d.ts","sources":["../stash.ts"],"names":[],"mappings":"AAMA,cAAc,OAAO,EAAW,MAAM,oBAAmB;AAEzD,mDAAmD,GACnD,YAAY,QAAQ;EAClB,wCAAwC,GACxC,MAAM;EACN,2CAA2C,GAC3C,YAAY,UAAU,SAAS;;AAGjC,yBAAyB,GACzB,YAAY;EACV,uEAAuE,GACvE,SAAS,MAAM;;AAGjB;;;;;;;;;;;;;CAaC,GACD,OAAO,YAAI,QAAS,kBAAwB,MAY3C"}
@@ -0,0 +1,27 @@
1
+ import type { Bundle, Graph } from "@yaks/graph";
2
+ import type { Runs } from "@yaks/graph/tools";
3
+ import type { Options } from "./options.js";
4
+ import type { Host } from "@yaks/host";
5
+ /** `Re:` derivation — shed however many `Re:`/`Fwd:` layers already piled up,
6
+ * so a long thread does not grow a subject line. */ export declare let reSubject: (said: string) => string;
7
+ /** A letter's two sides as a reply to it sees them. An arrival — a letter
8
+ * with a Message-ID that never asked to be sent; one of ours carries
9
+ * `deliver`, and has a Message-ID too once the transport gave it one — is
10
+ * answered at whom it says to answer, its Reply-To, else its author, and from
11
+ * the address it was delivered to. Our own letter is answered to its
12
+ * recipient, from the address it went out from. */ export declare let sides: (letter: Bundle) => {
13
+ /** whether it arrived here */ arrived: boolean;
14
+ /** the far side: an arrival's address, or our letter's recipient's eid */ far: string;
15
+ /** the address a reply goes from, this side of the thread */ from: string;
16
+ };
17
+ /**
18
+ * A letter's thread: up its one-parent `reply_to` chain, then down over
19
+ * whatever answers anything already found. Each read is keyed by the letters
20
+ * in hand, so a thread costs its own rows rather than every letter ever sent.
21
+ */ export declare let threadOf: (graph: Graph, letter: Bundle) => Promise<Bundle[]>;
22
+ /** The implementations of the tools ./vocab.json declares. A factory, like
23
+ * every other entry point in these packages: the domain is what an address a
24
+ * person typed is canonicalized against before the address book is queried,
25
+ * into the same form the canonicalizer stored it in (./plugin.ts). Everything
26
+ * else a handler needs arrives on the call it is handed. */ export declare let runs: (host?: Pick<Host, 'senders'> | null, options?: Options) => Runs;
27
+ //# sourceMappingURL=tools.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tools.d.ts","sources":["../tools.ts"],"names":[],"mappings":"AA2BA,cAAc,MAAM,EAAa,KAAK,2BAAgC;AAWtE,cAAc,IAAI,iCAAsC;AAIxD,cAAc,OAAO,uBAAsB;AAC3C,cAAc,IAAI,0BAA+B;AA8BjD;kDACkD,GAClD,OAAO,YAAI,YAAa,MAAM,MAAM,KAAG,MAAM,CACc;AAE3D;;;;;iDAKiD,GACjD,OAAO,YAAI,QAAS,QAAQ;EAC1B,4BAA4B,GAC5B,SAAS,OAAO;EAChB,wEAAwE,GACxE,KAAK,MAAM;EACX,2DAA2D,GAC3D,MAAM,MAAM;EAYb;AA+BD;;;;CAIC,GACD,OAAO,YAAI,WACT,OAAO,OACP,QAAQ,WACP,QAAQ,UA2BV;AAgGD;;;;0DAI0D,GAC1D,OAAO,YAAI,OACT,OAAO,KAAK,MAAM,aAAa,IAAI,EACnC,UAAS,YACR,KA+ID"}
@@ -0,0 +1,6 @@
1
+ import type { Host } from "@yaks/host";
2
+ import type { Built } from "@yaks/host/senders";
3
+ import type { Transport } from "./options.js";
4
+ /** The sender a name selects from the host's, or why it cannot send yet.
5
+ * `stash` is the built-in one that keeps letters in memory. */ export declare let post: (said: Transport, host?: Pick<Host, 'senders'> | null) => Built;
6
+ //# sourceMappingURL=transport.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transport.d.ts","sources":["../transport.ts"],"names":[],"mappings":"AAIA,cAAc,IAAI,0BAA+B;AACjD,cAAc,KAAK,kCAAuC;AAC1D,cAAc,SAAS,uBAAsB;AAG7C;6DAC6D,GAC7D,OAAO,YAAI,OACT,MAAM,WACN,OAAO,KAAK,MAAM,aAAa,IAAI,KAClC,MASF"}
@@ -0,0 +1,7 @@
1
+ import type { VocabDoc } from "@yaks/vocab";
2
+ import { mailDoc } from "./comp.js";
3
+ export { mailDoc };
4
+ /** What this package is: its manifest's description, the one place it is
5
+ * written, which a host stamps on each document here. */ export declare let description: string;
6
+ /** Every document this plugin declares. */ export declare let docs: VocabDoc[];
7
+ //# sourceMappingURL=vocab.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vocab.d.ts","sources":["../vocab.ts"],"names":[],"mappings":"AAIA,cAAc,QAAQ,2BAAgC;AACtD,SAAS,OAAO,oBAAmB;AAGnC,SAAS,OAAO,GAAE;AAElB;uDACuD,GACvD,OAAO,YAAI,aAAa,MAAM,CAAuB;AAErD,yCAAyC,GACzC,OAAO,YAAI,MAAM,WAAsB"}
package/addr.js ADDED
@@ -0,0 +1,92 @@
1
+ // Addresses, and the one rule about them worth writing down: the canonical
2
+ // form of an address at your own domain.
3
+ //
4
+ // Everyone else's domain is nobody's business but theirs — `ana@example.com`
5
+ // passes through this file untouched, because only the mail server behind
6
+ // example.com knows whether that is the same mailbox as `Ana@example.com`. At
7
+ // your domain you do know, and knowing is worth something: canonicalize on the
8
+ // way in and a club's address book cannot end up holding two rows for one
9
+ // person.
10
+ //
11
+ // The canonical form lowercases the local part and drops underscores. The
12
+ // lowercase is the ordinary courtesy; the underscore is a hard-won fact —
13
+ // Cloudflare Email Routing rejects an underscore in the local part at RCPT,
14
+ // upstream of anything you deploy, so a letter to `book_club@…` bounces
15
+ // whatever your routing rules say. Dropping it on the way in is the only place
16
+ // the fix works.
17
+ //
18
+ // Every function here is curried domain-first, so an app applies its own
19
+ // domain once and passes the result around:
20
+ // `let mine = canon('books.example')`.
21
+ /** An address split at its one `@`, or `null` when it has none or several. */ export let parts = (address)=>{
22
+ let [local, domain, ...extra] = address.trim().split('@');
23
+ return local && domain && !extra.length ? [
24
+ local,
25
+ domain
26
+ ] : null;
27
+ };
28
+ // An address as a sending API takes one: the address form of WHATWG's
29
+ // `<input type=email>`, ASCII and unquoted, whose local part neither starts,
30
+ // ends nor doubles a dot, at a domain of two labels or more.
31
+ let LOCAL = /^[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*$/;
32
+ let LABEL = /^[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?$/;
33
+ /**
34
+ * Whether a typed address is one a letter can be sent to, as a provider such
35
+ * as Resend checks it before taking a letter. Ask before sending, so a typo
36
+ * is the writer's to fix rather than a send that fails.
37
+ *
38
+ * ```ts
39
+ * import { sendable } from '@yaks/mail'
40
+ * sendable('ana@books.example') // true
41
+ * sendable('ana@books') // false
42
+ * sendable('ana@@books.example') // false
43
+ * ```
44
+ */ export let sendable = (address)=>{
45
+ let split = parts(address);
46
+ if (!split || address != address.trim() || address.length > 254) return false;
47
+ let labels = split[1].split('.');
48
+ return split[0].length <= 64 && LOCAL.test(split[0]) && labels.length > 1 && labels.every((l)=>LABEL.test(l));
49
+ };
50
+ /** An address at a domain: `address('ana', 'books.example')`. */ export let address = (local, domain)=>`${local}@${domain}`;
51
+ /**
52
+ * The local part of an address at `domain`, or `null` for an address anywhere
53
+ * else. The domain is compared case-insensitively; the local part comes back
54
+ * as it was written.
55
+ *
56
+ * ```ts
57
+ * import { local } from '@yaks/mail'
58
+ * local('books.example')('Ana@Books.Example') // 'Ana'
59
+ * local('books.example')('ana@elsewhere.com') // null
60
+ * ```
61
+ */ export let local = (domain)=>{
62
+ let mine = domain.trim().toLowerCase();
63
+ return (address)=>{
64
+ let split = parts(address);
65
+ return split && split[1].toLowerCase() == mine ? split[0] : null;
66
+ };
67
+ };
68
+ /** Whether an address is at `domain`. */ export let at = (domain)=>{
69
+ let get = local(domain);
70
+ return (address)=>get(address) != null;
71
+ };
72
+ /**
73
+ * The canonical, deliverable form of an address at `domain`: the local part
74
+ * lowercased with its underscores dropped, at the domain as configured. An
75
+ * address at any other domain is returned as it was given — canonicalizing
76
+ * somebody else's domain is a guess, and a guess here misdelivers mail.
77
+ *
78
+ * ```ts
79
+ * import { canon } from '@yaks/mail'
80
+ * let mine = canon('books.example')
81
+ * mine('Book_Club@Books.Example') // 'bookclub@books.example'
82
+ * mine('ana@elsewhere.com') // 'ana@elsewhere.com'
83
+ * ```
84
+ */ export let canon = (domain)=>{
85
+ let mine = domain.trim().toLowerCase();
86
+ let get = local(mine);
87
+ return (raw)=>{
88
+ let one = get(raw);
89
+ return one == null ? raw : address(one.toLowerCase().replace(/_/g, ''), mine);
90
+ };
91
+ };
92
+ //# sourceMappingURL=addr.js.map
package/addr.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"addr.js","sources":["./addr.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,yCAAyC;AACzC,EAAE;AACF,6EAA6E;AAC7E,0EAA0E;AAC1E,8EAA8E;AAC9E,+EAA+E;AAC/E,0EAA0E;AAC1E,UAAU;AACV,EAAE;AACF,0EAA0E;AAC1E,0EAA0E;AAC1E,4EAA4E;AAC5E,wEAAwE;AACxE,+EAA+E;AAC/E,iBAAiB;AACjB,EAAE;AACF,yEAAyE;AACzE,4CAA4C;AAC5C,uCAAuC;AAEvC,4EAA4E,GAC5E,OAAO,IAAI,QAAQ,CAAC;EAClB,IAAI,CAAC,OAAO,QAAQ,GAAG,MAAM,GAAG,QAAQ,IAAI,GAAG,KAAK,CAAC;EACrD,OAAO,SAAS,UAAU,CAAC,MAAM,MAAM,GAAG;IAAC;IAAO;GAAO,GAAG;AAC9D,EAAC;AAED,sEAAsE;AACtE,6EAA6E;AAC7E,6DAA6D;AAC7D,IAAI,QACF;AACF,IAAI,QAAQ;AAEZ;;;;;;;;;;;CAWC,GACD,OAAO,IAAI,WAAW,CAAC;EACrB,IAAI,QAAQ,MAAM;EAClB,IAAI,CAAC,SAAS,WAAW,QAAQ,IAAI,MAAM,QAAQ,MAAM,GAAG,KAAK,OAAO;EACxE,IAAI,SAAS,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC;EAC5B,OAAO,KAAK,CAAC,EAAE,CAAC,MAAM,IAAI,MAAM,MAAM,IAAI,CAAC,KAAK,CAAC,EAAE,KACjD,OAAO,MAAM,GAAG,KAAK,OAAO,KAAK,CAAC,CAAC,IAAM,MAAM,IAAI,CAAC;AACxD,EAAC;AAED,+DAA+D,GAC/D,OAAO,IAAI,UAAU,CAAC,OAAe,SACnC,GAAG,MAAM,CAAC,EAAE,QAAQ,CAAA;AAEtB;;;;;;;;;;CAUC,GACD,OAAO,IAAI,QAAQ,CAAC;EAClB,IAAI,OAAO,OAAO,IAAI,GAAG,WAAW;EACpC,OAAO,CAAC;IACN,IAAI,QAAQ,MAAM;IAClB,OAAO,SAAS,KAAK,CAAC,EAAE,CAAC,WAAW,MAAM,OAAO,KAAK,CAAC,EAAE,GAAG;EAC9D;AACF,EAAC;AAED,uCAAuC,GACvC,OAAO,IAAI,KAAK,CAAC;EACf,IAAI,MAAM,MAAM;EAChB,OAAO,CAAC,UAAY,IAAI,YAAY;AACtC,EAAC;AAED;;;;;;;;;;;;CAYC,GACD,OAAO,IAAI,QAAQ,CAAC;EAClB,IAAI,OAAO,OAAO,IAAI,GAAG,WAAW;EACpC,IAAI,MAAM,MAAM;EAChB,OAAO,CAAC;IACN,IAAI,MAAM,IAAI;IACd,OAAO,OAAO,OACV,MACA,QAAQ,IAAI,WAAW,GAAG,OAAO,CAAC,MAAM,KAAK;EACnD;AACF,EAAC"}
package/addr.ts ADDED
@@ -0,0 +1,106 @@
1
+ // Addresses, and the one rule about them worth writing down: the canonical
2
+ // form of an address at your own domain.
3
+ //
4
+ // Everyone else's domain is nobody's business but theirs — `ana@example.com`
5
+ // passes through this file untouched, because only the mail server behind
6
+ // example.com knows whether that is the same mailbox as `Ana@example.com`. At
7
+ // your domain you do know, and knowing is worth something: canonicalize on the
8
+ // way in and a club's address book cannot end up holding two rows for one
9
+ // person.
10
+ //
11
+ // The canonical form lowercases the local part and drops underscores. The
12
+ // lowercase is the ordinary courtesy; the underscore is a hard-won fact —
13
+ // Cloudflare Email Routing rejects an underscore in the local part at RCPT,
14
+ // upstream of anything you deploy, so a letter to `book_club@…` bounces
15
+ // whatever your routing rules say. Dropping it on the way in is the only place
16
+ // the fix works.
17
+ //
18
+ // Every function here is curried domain-first, so an app applies its own
19
+ // domain once and passes the result around:
20
+ // `let mine = canon('books.example')`.
21
+
22
+ /** An address split at its one `@`, or `null` when it has none or several. */
23
+ export let parts = (address: string): [string, string] | null => {
24
+ let [local, domain, ...extra] = address.trim().split('@')
25
+ return local && domain && !extra.length ? [local, domain] : null
26
+ }
27
+
28
+ // An address as a sending API takes one: the address form of WHATWG's
29
+ // `<input type=email>`, ASCII and unquoted, whose local part neither starts,
30
+ // ends nor doubles a dot, at a domain of two labels or more.
31
+ let LOCAL =
32
+ /^[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*$/
33
+ let LABEL = /^[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?$/
34
+
35
+ /**
36
+ * Whether a typed address is one a letter can be sent to, as a provider such
37
+ * as Resend checks it before taking a letter. Ask before sending, so a typo
38
+ * is the writer's to fix rather than a send that fails.
39
+ *
40
+ * ```ts
41
+ * import { sendable } from '@yaks/mail'
42
+ * sendable('ana@books.example') // true
43
+ * sendable('ana@books') // false
44
+ * sendable('ana@@books.example') // false
45
+ * ```
46
+ */
47
+ export let sendable = (address: string): boolean => {
48
+ let split = parts(address)
49
+ if (!split || address != address.trim() || address.length > 254) return false
50
+ let labels = split[1].split('.')
51
+ return split[0].length <= 64 && LOCAL.test(split[0]) &&
52
+ labels.length > 1 && labels.every((l) => LABEL.test(l))
53
+ }
54
+
55
+ /** An address at a domain: `address('ana', 'books.example')`. */
56
+ export let address = (local: string, domain: string): string =>
57
+ `${local}@${domain}`
58
+
59
+ /**
60
+ * The local part of an address at `domain`, or `null` for an address anywhere
61
+ * else. The domain is compared case-insensitively; the local part comes back
62
+ * as it was written.
63
+ *
64
+ * ```ts
65
+ * import { local } from '@yaks/mail'
66
+ * local('books.example')('Ana@Books.Example') // 'Ana'
67
+ * local('books.example')('ana@elsewhere.com') // null
68
+ * ```
69
+ */
70
+ export let local = (domain: string): (address: string) => string | null => {
71
+ let mine = domain.trim().toLowerCase()
72
+ return (address: string): string | null => {
73
+ let split = parts(address)
74
+ return split && split[1].toLowerCase() == mine ? split[0] : null
75
+ }
76
+ }
77
+
78
+ /** Whether an address is at `domain`. */
79
+ export let at = (domain: string): (address: string) => boolean => {
80
+ let get = local(domain)
81
+ return (address) => get(address) != null
82
+ }
83
+
84
+ /**
85
+ * The canonical, deliverable form of an address at `domain`: the local part
86
+ * lowercased with its underscores dropped, at the domain as configured. An
87
+ * address at any other domain is returned as it was given — canonicalizing
88
+ * somebody else's domain is a guess, and a guess here misdelivers mail.
89
+ *
90
+ * ```ts
91
+ * import { canon } from '@yaks/mail'
92
+ * let mine = canon('books.example')
93
+ * mine('Book_Club@Books.Example') // 'bookclub@books.example'
94
+ * mine('ana@elsewhere.com') // 'ana@elsewhere.com'
95
+ * ```
96
+ */
97
+ export let canon = (domain: string): (raw: string) => string => {
98
+ let mine = domain.trim().toLowerCase()
99
+ let get = local(mine)
100
+ return (raw) => {
101
+ let one = get(raw)
102
+ return one == null
103
+ ? raw
104
+ : address(one.toLowerCase().replace(/_/g, ''), mine)
105
+ }
106
+ }