@remit/mailbox-service 0.0.111 → 0.0.113

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 (298) hide show
  1. package/dist/account-credentials.d.ts +83 -0
  2. package/dist/account-credentials.js +104 -0
  3. package/dist/attribute-mapper.d.ts +36 -0
  4. package/dist/attribute-mapper.js +119 -0
  5. package/dist/body-parse.d.ts +28 -0
  6. package/{src/body-parse.ts → dist/body-parse.js} +18 -26
  7. package/dist/body-part-mapper.d.ts +92 -0
  8. package/dist/body-part-mapper.js +301 -0
  9. package/dist/body-sync-queue.d.ts +59 -0
  10. package/dist/body-sync-queue.js +56 -0
  11. package/dist/body-sync-terminal.d.ts +58 -0
  12. package/dist/body-sync-terminal.js +95 -0
  13. package/dist/body-sync.d.ts +351 -0
  14. package/dist/body-sync.js +1204 -0
  15. package/dist/calendar-parts.d.ts +23 -0
  16. package/dist/calendar-parts.js +18 -0
  17. package/dist/classification-backfill.d.ts +91 -0
  18. package/dist/classification-backfill.js +180 -0
  19. package/dist/classify-message.d.ts +31 -0
  20. package/dist/classify-message.js +89 -0
  21. package/dist/connection-factory.d.ts +54 -0
  22. package/dist/connection-factory.js +54 -0
  23. package/dist/connection-test.d.ts +41 -0
  24. package/dist/connection-test.js +152 -0
  25. package/dist/external-move.d.ts +94 -0
  26. package/dist/external-move.js +173 -0
  27. package/dist/filter-operations.d.ts +42 -0
  28. package/dist/filter-operations.js +211 -0
  29. package/dist/filters/filter-config.d.ts +14 -0
  30. package/dist/filters/filter-config.js +10 -0
  31. package/{src/filters/list-id.ts → dist/filters/list-id.d.ts} +2 -16
  32. package/dist/filters/list-id.js +26 -0
  33. package/dist/filters/match.d.ts +69 -0
  34. package/dist/filters/match.js +165 -0
  35. package/dist/filters/pipeline.d.ts +64 -0
  36. package/dist/filters/pipeline.js +100 -0
  37. package/{src/flag-push-terminal.ts → dist/flag-push-terminal.d.ts} +19 -77
  38. package/dist/flag-push-terminal.js +63 -0
  39. package/dist/flag-push.d.ts +69 -0
  40. package/dist/flag-push.js +111 -0
  41. package/dist/flag-queue.d.ts +175 -0
  42. package/dist/flag-queue.js +291 -0
  43. package/dist/folder-operations.d.ts +39 -0
  44. package/dist/folder-operations.js +112 -0
  45. package/dist/folder-placement.d.ts +13 -0
  46. package/dist/folder-placement.js +23 -0
  47. package/dist/heuristics/authenticationResults.d.ts +10 -0
  48. package/dist/heuristics/authenticationResults.js +187 -0
  49. package/dist/heuristics/classifyByHeaders.d.ts +82 -0
  50. package/dist/heuristics/classifyByHeaders.js +365 -0
  51. package/dist/heuristics/classifyPlacement.d.ts +67 -0
  52. package/dist/heuristics/classifyPlacement.js +153 -0
  53. package/dist/heuristics/machineSenders.d.ts +21 -0
  54. package/{src/heuristics/machineSenders.ts → dist/heuristics/machineSenders.js} +34 -40
  55. package/dist/heuristics/senderMismatch.d.ts +81 -0
  56. package/dist/heuristics/senderMismatch.js +192 -0
  57. package/dist/heuristics/socialDomains.d.ts +14 -0
  58. package/{src/heuristics/socialDomains.ts → dist/heuristics/socialDomains.js} +9 -9
  59. package/dist/heuristics/transactionalDomains.d.ts +18 -0
  60. package/{src/heuristics/transactionalDomains.ts → dist/heuristics/transactionalDomains.js} +8 -8
  61. package/dist/imapflow-connection.d.ts +377 -0
  62. package/dist/imapflow-connection.js +1147 -0
  63. package/dist/index.d.ts +63 -0
  64. package/dist/index.js +65 -0
  65. package/dist/list-id-backfill.d.ts +83 -0
  66. package/dist/list-id-backfill.js +132 -0
  67. package/dist/mailbox-cursor-rebuild.d.ts +74 -0
  68. package/dist/mailbox-cursor-rebuild.js +71 -0
  69. package/{src/mailbox-cursor.ts → dist/mailbox-cursor.d.ts} +20 -98
  70. package/dist/mailbox-cursor.js +102 -0
  71. package/dist/mailbox-intent.d.ts +41 -0
  72. package/{src/mailbox-intent.ts → dist/mailbox-intent.js} +39 -63
  73. package/dist/mailbox-management.d.ts +240 -0
  74. package/dist/mailbox-management.js +576 -0
  75. package/dist/mailbox-presence.d.ts +52 -0
  76. package/{src/mailbox-presence.ts → dist/mailbox-presence.js} +19 -30
  77. package/dist/mailbox-queue.d.ts +126 -0
  78. package/dist/mailbox-queue.js +214 -0
  79. package/dist/mailbox-sync.d.ts +93 -0
  80. package/dist/mailbox-sync.js +490 -0
  81. package/dist/mailbox-upstream.d.ts +38 -0
  82. package/{src/mailbox-upstream.ts → dist/mailbox-upstream.js} +37 -47
  83. package/dist/message-category.d.ts +51 -0
  84. package/{src/message-category.ts → dist/message-category.js} +28 -71
  85. package/dist/message-move.d.ts +285 -0
  86. package/dist/message-move.js +858 -0
  87. package/dist/message-parser.d.ts +14 -0
  88. package/dist/message-parser.js +17 -0
  89. package/{src/message-presence.ts → dist/message-presence.d.ts} +1 -13
  90. package/dist/message-presence.js +29 -0
  91. package/dist/message-read.d.ts +51 -0
  92. package/dist/message-read.js +31 -0
  93. package/dist/message-sync.d.ts +345 -0
  94. package/dist/message-sync.js +1358 -0
  95. package/dist/mime-walker.d.ts +102 -0
  96. package/dist/mime-walker.js +198 -0
  97. package/dist/organize-job.d.ts +20 -0
  98. package/dist/organize-job.js +37 -0
  99. package/dist/outbox-attachment-content.d.ts +37 -0
  100. package/dist/outbox-attachment-content.js +50 -0
  101. package/dist/outbox-attachment-filename.d.ts +19 -0
  102. package/dist/outbox-attachment-filename.js +64 -0
  103. package/dist/outbox-attachment.d.ts +148 -0
  104. package/dist/outbox-attachment.js +273 -0
  105. package/dist/outbox-queue.d.ts +73 -0
  106. package/dist/outbox-queue.js +284 -0
  107. package/{src/outbox-status.ts → dist/outbox-status.d.ts} +1 -6
  108. package/dist/outbox-status.js +18 -0
  109. package/dist/ownership.d.ts +24 -0
  110. package/dist/ownership.js +83 -0
  111. package/dist/placement-move-terminal.d.ts +67 -0
  112. package/dist/placement-move-terminal.js +88 -0
  113. package/dist/placement-move.d.ts +83 -0
  114. package/dist/placement-move.js +216 -0
  115. package/{src/placement-settled.ts → dist/placement-settled.d.ts} +19 -50
  116. package/dist/placement-settled.js +75 -0
  117. package/dist/quarantine.d.ts +95 -0
  118. package/dist/quarantine.js +131 -0
  119. package/dist/refusal.d.ts +31 -0
  120. package/dist/refusal.js +3 -0
  121. package/{src/restore-source-placement.ts → dist/restore-source-placement.d.ts} +29 -101
  122. package/dist/restore-source-placement.js +79 -0
  123. package/dist/search-text-backfill.d.ts +15 -0
  124. package/dist/search-text-backfill.js +33 -0
  125. package/dist/snippet.d.ts +61 -0
  126. package/dist/snippet.js +129 -0
  127. package/dist/spam-report.d.ts +86 -0
  128. package/dist/spam-report.js +241 -0
  129. package/dist/sync-warning.d.ts +21 -0
  130. package/dist/sync-warning.js +26 -0
  131. package/dist/sync-watermarks.d.ts +138 -0
  132. package/dist/sync-watermarks.js +205 -0
  133. package/dist/test-helpers/fake-imap.d.ts +151 -0
  134. package/dist/test-helpers/fake-imap.js +508 -0
  135. package/dist/test-helpers/folder-roles.d.ts +34 -0
  136. package/dist/test-helpers/folder-roles.js +47 -0
  137. package/dist/test-helpers/mime-tree-generator.d.ts +82 -0
  138. package/dist/test-helpers/mime-tree-generator.js +329 -0
  139. package/dist/test-helpers/test-store.d.ts +69 -0
  140. package/dist/test-helpers/test-store.js +193 -0
  141. package/dist/text/normalizer.d.ts +20 -0
  142. package/dist/text/normalizer.js +102 -0
  143. package/dist/thread-message-rows.d.ts +69 -0
  144. package/dist/thread-message-rows.js +40 -0
  145. package/dist/thread-root.d.ts +7 -0
  146. package/dist/thread-root.js +25 -0
  147. package/dist/types.d.ts +343 -0
  148. package/dist/types.js +19 -0
  149. package/package.json +86 -73
  150. package/src/account-credentials.ts +0 -210
  151. package/src/adversarial-mime.e2e.test.ts +0 -307
  152. package/src/attribute-mapper.test.ts +0 -104
  153. package/src/attribute-mapper.ts +0 -148
  154. package/src/body-parse.test.ts +0 -55
  155. package/src/body-part-mapper.property.test.ts +0 -354
  156. package/src/body-part-mapper.ts +0 -360
  157. package/src/body-sync-auto-read-writeonce.test.ts +0 -219
  158. package/src/body-sync-calendar-suggestion.test.ts +0 -433
  159. package/src/body-sync-category-denormalize.test.ts +0 -352
  160. package/src/body-sync-category-override.test.ts +0 -501
  161. package/src/body-sync-category-writeonce.test.ts +0 -234
  162. package/src/body-sync-filter.test.ts +0 -237
  163. package/src/body-sync-message-gone.test.ts +0 -139
  164. package/src/body-sync-placement-flags.test.ts +0 -347
  165. package/src/body-sync-placement-writeonce.test.ts +0 -337
  166. package/src/body-sync-quarantine.test.ts +0 -276
  167. package/src/body-sync-queue.test.ts +0 -124
  168. package/src/body-sync-queue.ts +0 -131
  169. package/src/body-sync-resync.test.ts +0 -605
  170. package/src/body-sync-terminal.test.ts +0 -310
  171. package/src/body-sync-terminal.ts +0 -176
  172. package/src/body-sync-unclassifiable-once.test.ts +0 -260
  173. package/src/body-sync-unsubscribed-auto-read.test.ts +0 -176
  174. package/src/body-sync.ts +0 -1970
  175. package/src/calendar-parts.ts +0 -41
  176. package/src/classification-backfill.test.ts +0 -615
  177. package/src/classification-backfill.ts +0 -363
  178. package/src/classify-message.test.ts +0 -205
  179. package/src/classify-message.ts +0 -116
  180. package/src/connection-factory.ts +0 -102
  181. package/src/connection-test.ts +0 -208
  182. package/src/credentials.test.ts +0 -181
  183. package/src/external-move.test.ts +0 -369
  184. package/src/external-move.ts +0 -272
  185. package/src/filters/anchor-drift.ts +0 -55
  186. package/src/filters/filter-config.test.ts +0 -229
  187. package/src/filters/filter-config.ts +0 -69
  188. package/src/filters/list-id.test.ts +0 -50
  189. package/src/filters/match.test.ts +0 -429
  190. package/src/filters/match.ts +0 -274
  191. package/src/filters/message-embedder.test.ts +0 -39
  192. package/src/filters/message-embedder.ts +0 -33
  193. package/src/filters/pipeline.test.ts +0 -330
  194. package/src/filters/pipeline.ts +0 -284
  195. package/src/flag-push-terminal.test.ts +0 -209
  196. package/src/flag-push.test.ts +0 -408
  197. package/src/flag-push.ts +0 -198
  198. package/src/flag-queue-stars.test.ts +0 -171
  199. package/src/flag-queue.ts +0 -509
  200. package/src/folder-placement.ts +0 -56
  201. package/src/heuristics/authenticationResults.test.ts +0 -189
  202. package/src/heuristics/authenticationResults.ts +0 -212
  203. package/src/heuristics/classifyByHeaders.test.ts +0 -889
  204. package/src/heuristics/classifyByHeaders.ts +0 -467
  205. package/src/heuristics/classifyPlacement.test.ts +0 -366
  206. package/src/heuristics/classifyPlacement.ts +0 -211
  207. package/src/heuristics/classifyRealMail.test.ts +0 -242
  208. package/src/heuristics/machineSenders.test.ts +0 -101
  209. package/src/heuristics/senderMismatch.test.ts +0 -346
  210. package/src/heuristics/senderMismatch.ts +0 -259
  211. package/src/imapflow-connect-retry.test.ts +0 -134
  212. package/src/imapflow-connection.e2e.test.ts +0 -335
  213. package/src/imapflow-connection.integ.test.ts +0 -316
  214. package/src/imapflow-connection.test.ts +0 -469
  215. package/src/imapflow-connection.ts +0 -1475
  216. package/src/index.ts +0 -350
  217. package/src/list-id-backfill.test.ts +0 -573
  218. package/src/list-id-backfill.ts +0 -274
  219. package/src/mailbox-cursor-rebuild.test.ts +0 -171
  220. package/src/mailbox-cursor-rebuild.ts +0 -146
  221. package/src/mailbox-cursor.test.ts +0 -249
  222. package/src/mailbox-delete-intent.test.ts +0 -246
  223. package/src/mailbox-management.test.ts +0 -356
  224. package/src/mailbox-management.ts +0 -839
  225. package/src/mailbox-presence.test.ts +0 -72
  226. package/src/mailbox-queue.ts +0 -415
  227. package/src/mailbox-rename-intent.test.ts +0 -389
  228. package/src/mailbox-rename-subtree.test.ts +0 -664
  229. package/src/mailbox-sync.test.ts +0 -848
  230. package/src/mailbox-sync.ts +0 -724
  231. package/src/message-delete-junk-reconcile.test.ts +0 -117
  232. package/src/message-delete-no-trash.test.ts +0 -245
  233. package/src/message-delete-unsettled-placement.test.ts +0 -396
  234. package/src/message-move-copy.test.ts +0 -562
  235. package/src/message-move-empty-trash.test.ts +0 -326
  236. package/src/message-move-same-mailbox.test.ts +0 -139
  237. package/src/message-move-unsettled-placement.test.ts +0 -378
  238. package/src/message-move.ts +0 -1348
  239. package/src/message-parser.ts +0 -26
  240. package/src/message-presence.test.ts +0 -50
  241. package/src/message-sync-address-spoof.test.ts +0 -293
  242. package/src/message-sync-category.test.ts +0 -142
  243. package/src/message-sync-changedsince.test.ts +0 -840
  244. package/src/message-sync-flags.test.ts +0 -158
  245. package/src/message-sync-junk-harvest.test.ts +0 -244
  246. package/src/message-sync-quarantine.test.ts +0 -416
  247. package/src/message-sync-repoint.test.ts +0 -419
  248. package/src/message-sync.ts +0 -1963
  249. package/src/mime-walker.test.ts +0 -311
  250. package/src/mime-walker.ts +0 -305
  251. package/src/outbox-attachment-content.test.ts +0 -153
  252. package/src/outbox-attachment-content.ts +0 -104
  253. package/src/outbox-attachment-filename.test.ts +0 -132
  254. package/src/outbox-attachment-filename.ts +0 -76
  255. package/src/outbox-attachment.test.ts +0 -738
  256. package/src/outbox-attachment.ts +0 -536
  257. package/src/outbox-failed-recovery.test.ts +0 -370
  258. package/src/outbox-queue-settled-row.test.ts +0 -140
  259. package/src/outbox-queue.test.ts +0 -291
  260. package/src/outbox-queue.ts +0 -508
  261. package/src/pass-through-unit-of-work.ts +0 -17
  262. package/src/placement-move-terminal.test.ts +0 -300
  263. package/src/placement-move-terminal.ts +0 -148
  264. package/src/placement-move-unsettled.test.ts +0 -275
  265. package/src/placement-move.ts +0 -336
  266. package/src/placement-settled.test.ts +0 -147
  267. package/src/placement-transition-race.test.ts +0 -482
  268. package/src/quarantine.test.ts +0 -191
  269. package/src/quarantine.ts +0 -228
  270. package/src/snippet.test.ts +0 -213
  271. package/src/snippet.ts +0 -152
  272. package/src/spam-report.test.ts +0 -801
  273. package/src/spam-report.ts +0 -357
  274. package/src/stale-message-reconcile.test.ts +0 -82
  275. package/src/stale-message-reconcile.ts +0 -55
  276. package/src/sync-watermarks.test.ts +0 -409
  277. package/src/sync-watermarks.ts +0 -319
  278. package/src/system-flag-wire-format.test.ts +0 -77
  279. package/src/test-helpers/folder-roles.ts +0 -66
  280. package/src/test-helpers/isolated-mailbox.ts +0 -44
  281. package/src/test-helpers/mailfuzz-connection.ts +0 -27
  282. package/src/test-helpers/mime-tree-generator.ts +0 -461
  283. package/src/text/normalizer.test.ts +0 -223
  284. package/src/text/normalizer.ts +0 -140
  285. package/src/thread-message-rows.ts +0 -77
  286. package/src/types/mailparser-augment.d.ts +0 -15
  287. package/src/types/natural-porter-stemmer.d.ts +0 -50
  288. package/src/types/stopword.d.ts +0 -69
  289. package/src/types.ts +0 -371
  290. package/test/fixtures/mime-adversarial-e2e/01-octet-stream-pdf.eml +0 -21
  291. package/test/fixtures/mime-adversarial-e2e/02-nested-related-cid-image.eml +0 -31
  292. package/test/fixtures/mime-adversarial-e2e/03-calendar-invite.eml +0 -33
  293. package/test/fixtures/mime-adversarial-e2e/04-forwarded-rfc822-with-attachments.eml +0 -42
  294. package/test/fixtures/mime-adversarial-e2e/05-tnef-winmail.eml +0 -25
  295. package/test/fixtures/mime-adversarial-e2e/06-8bit-non-ascii.eml +0 -15
  296. package/test/fixtures/mime-adversarial-e2e/07-quoted-printable-soft-breaks.eml +0 -17
  297. package/test/fixtures/mime-adversarial-e2e/08-alternative-empty-text.eml +0 -18
  298. package/tsconfig.json +0 -8
@@ -1,738 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { describe, it } from "node:test";
3
- import type {
4
- CreateOutboxAttachmentInput,
5
- IOutboxAttachmentRepository,
6
- IOutboxMessageRepository,
7
- OutboxAttachmentCap,
8
- OutboxAttachmentItem,
9
- OutboxMessageItem,
10
- ReserveOutboxAttachmentResult,
11
- } from "@remit/data-ports";
12
- import { holdsRoom } from "@remit/data-ports";
13
- import { ForbiddenError, NotFoundError } from "@remit/data-ports/errors";
14
- import {
15
- OutboxAttachmentRejectionReason,
16
- OutboxMessageStatus,
17
- } from "@remit/domain-enums";
18
- import {
19
- createMockStorageService,
20
- type StorageService,
21
- UPLOAD_URL_TTL_SECONDS,
22
- } from "@remit/storage-service";
23
- import {
24
- OUTBOX_ATTACHMENT_MAX_COUNT,
25
- OUTBOX_ATTACHMENT_MAX_TOTAL_BYTES,
26
- OutboxAttachmentService,
27
- } from "./outbox-attachment.js";
28
-
29
- const ACCOUNT_CONFIG_ID = "cfg-679";
30
- const ACCOUNT_ID = "acc-679";
31
- const DRAFT_ID = "draft-679";
32
-
33
- /**
34
- * An in-memory stand-in for the repository, with the one property that matters:
35
- * `reserve` counts and inserts without yielding, the way a database transaction
36
- * does. Everything else may interleave freely.
37
- */
38
- const createRepository = (): IOutboxAttachmentRepository & {
39
- rows: Map<string, OutboxAttachmentItem>;
40
- } => {
41
- const rows = new Map<string, OutboxAttachmentItem>();
42
- let sequence = 0;
43
-
44
- const forDraft = (
45
- accountConfigId: string,
46
- outboxMessageId: string,
47
- ): OutboxAttachmentItem[] =>
48
- [...rows.values()].filter(
49
- (row) =>
50
- row.accountConfigId === accountConfigId &&
51
- row.outboxMessageId === outboxMessageId,
52
- );
53
-
54
- return {
55
- rows,
56
- reserve: async (
57
- input: CreateOutboxAttachmentInput,
58
- cap: OutboxAttachmentCap,
59
- ): Promise<ReserveOutboxAttachmentResult> => {
60
- // No await inside: the whole point of the real implementation is that
61
- // counting and inserting are one atomic step.
62
- const live = forDraft(
63
- input.accountConfigId,
64
- input.outboxMessageId,
65
- ).filter((row) => holdsRoom(row, cap.nowSeconds));
66
- const usedBytes = live.reduce((total, row) => total + row.sizeBytes, 0);
67
- if (live.length >= cap.maxCount) {
68
- return { outcome: "OverCountCap", usedBytes };
69
- }
70
- if (usedBytes + input.sizeBytes > cap.maxTotalBytes) {
71
- return { outcome: "OverByteCap", usedBytes };
72
- }
73
- sequence += 1;
74
- const item: OutboxAttachmentItem = {
75
- ...input,
76
- state: "Pending",
77
- createdAt: sequence,
78
- updatedAt: sequence,
79
- };
80
- rows.set(item.outboxAttachmentId, item);
81
- return { outcome: "Reserved", item };
82
- },
83
- get: async (accountConfigId, outboxAttachmentId) => {
84
- const row = rows.get(outboxAttachmentId);
85
- if (!row || row.accountConfigId !== accountConfigId) {
86
- throw new NotFoundError(`No outbox attachment ${outboxAttachmentId}`);
87
- }
88
- return row;
89
- },
90
- listByOutboxMessage: async (accountConfigId, outboxMessageId) =>
91
- forDraft(accountConfigId, outboxMessageId),
92
- markStored: async (accountConfigId, outboxAttachmentId, sizeBytes) => {
93
- const row = rows.get(outboxAttachmentId);
94
- if (!row || row.accountConfigId !== accountConfigId) return null;
95
- if (row.state !== "Pending") return null;
96
- const next: OutboxAttachmentItem = {
97
- ...row,
98
- state: "Stored",
99
- sizeBytes,
100
- reservationExpiresAt: 0,
101
- };
102
- rows.set(outboxAttachmentId, next);
103
- return next;
104
- },
105
- deleteLapsedReservations: async (
106
- accountConfigId: string,
107
- outboxMessageId: string,
108
- nowSeconds: number,
109
- ) => {
110
- const gone: string[] = [];
111
- for (const row of [...rows.values()]) {
112
- if (
113
- row.accountConfigId === accountConfigId &&
114
- row.outboxMessageId === outboxMessageId &&
115
- row.state === "Pending" &&
116
- row.reservationExpiresAt < nowSeconds
117
- ) {
118
- rows.delete(row.outboxAttachmentId);
119
- gone.push(row.outboxAttachmentId);
120
- }
121
- }
122
- return gone;
123
- },
124
- deleteMany: async (accountConfigId, ids) => {
125
- for (const id of ids) {
126
- if (rows.get(id)?.accountConfigId === accountConfigId) rows.delete(id);
127
- }
128
- },
129
- deleteByOutboxMessage: async (accountConfigId, outboxMessageId) => {
130
- for (const row of forDraft(accountConfigId, outboxMessageId)) {
131
- rows.delete(row.outboxAttachmentId);
132
- }
133
- },
134
- };
135
- };
136
-
137
- const build = (
138
- status: OutboxMessageItem["status"] = OutboxMessageStatus.draft,
139
- now?: () => number,
140
- ) => {
141
- const storage = createMockStorageService();
142
- const repository = createRepository();
143
- const outboxMessageService = {
144
- get: async (
145
- accountConfigId: string,
146
- _id: string,
147
- mode?: "read" | "act",
148
- ) => {
149
- if (accountConfigId !== ACCOUNT_CONFIG_ID) {
150
- assert.equal(mode, "act");
151
- throw new ForbiddenError("not yours");
152
- }
153
- return {
154
- outboxMessageId: DRAFT_ID,
155
- accountId: ACCOUNT_ID,
156
- accountConfigId,
157
- status,
158
- } as OutboxMessageItem;
159
- },
160
- } as unknown as IOutboxMessageRepository;
161
-
162
- return {
163
- service: new OutboxAttachmentService({
164
- outboxMessageService,
165
- outboxAttachmentService: repository,
166
- storage,
167
- now,
168
- }),
169
- storage,
170
- repository,
171
- };
172
- };
173
-
174
- const mint = (
175
- service: OutboxAttachmentService,
176
- overrides: Partial<{
177
- accountConfigId: string;
178
- filename: string;
179
- contentType: string;
180
- sizeBytes: number;
181
- }> = {},
182
- ) =>
183
- service.mint({
184
- accountConfigId: overrides.accountConfigId ?? ACCOUNT_CONFIG_ID,
185
- outboxMessageId: DRAFT_ID,
186
- filename: overrides.filename ?? "notes.txt",
187
- contentType: overrides.contentType ?? "text/plain",
188
- sizeBytes: overrides.sizeBytes ?? 10,
189
- });
190
-
191
- const uploadFor = (
192
- storage: StorageService,
193
- outboxAttachmentId: string,
194
- sizeBytes: number,
195
- ) =>
196
- storage.storeOutboxAttachment({
197
- accountConfigId: ACCOUNT_CONFIG_ID,
198
- accountId: ACCOUNT_ID,
199
- outboxMessageId: DRAFT_ID,
200
- outboxAttachmentId,
201
- content: Buffer.alloc(sizeBytes),
202
- });
203
-
204
- describe("reserving room on a draft", () => {
205
- it("writes a Pending row and hands back a URL bound to its size", async () => {
206
- const { service, repository } = build();
207
-
208
- const result = await mint(service, {
209
- filename: "invoice.pdf",
210
- contentType: "application/pdf",
211
- sizeBytes: 2048,
212
- });
213
-
214
- assert.equal(result.outcome, "Minted");
215
- if (result.outcome !== "Minted") return;
216
- assert.equal(result.reservation.filename, "invoice.pdf");
217
- assert.equal(result.reservation.contentType, "application/pdf");
218
- assert.match(result.reservation.uploadUrl, /max=2048/);
219
-
220
- const row = repository.rows.get(result.reservation.outboxAttachmentId);
221
- assert.equal(row?.state, "Pending");
222
- assert.equal(row?.sizeBytes, 2048);
223
- // The key on the row is the one the URL addresses — one identity, not two.
224
- assert.ok(row?.storageKey.endsWith(result.reservation.outboxAttachmentId));
225
- });
226
-
227
- it("names the content-id a body can refer to the file by", async () => {
228
- const { service } = build();
229
-
230
- const result = await mint(service, {
231
- filename: "shot.png",
232
- contentType: "image/png",
233
- sizeBytes: 64,
234
- });
235
-
236
- assert.equal(result.outcome, "Minted");
237
- if (result.outcome !== "Minted") return;
238
- assert.equal(
239
- result.reservation.contentId,
240
- `${result.reservation.outboxAttachmentId}@remit`,
241
- );
242
- });
243
-
244
- it("refuses a declared size over the cap", async () => {
245
- const { service } = build();
246
-
247
- const result = await mint(service, {
248
- filename: "huge.bin",
249
- sizeBytes: OUTBOX_ATTACHMENT_MAX_TOTAL_BYTES + 1,
250
- });
251
-
252
- assert.equal(result.outcome, "Rejected");
253
- if (result.outcome !== "Rejected") return;
254
- assert.equal(
255
- result.rejection.reason,
256
- OutboxAttachmentRejectionReason.FileTooLarge,
257
- );
258
- });
259
-
260
- it("counts a reservation nobody has uploaded against yet", async () => {
261
- const { service } = build();
262
- await mint(service, {
263
- sizeBytes: OUTBOX_ATTACHMENT_MAX_TOTAL_BYTES - 1024,
264
- });
265
-
266
- const result = await mint(service, { sizeBytes: 4096 });
267
-
268
- assert.equal(result.outcome, "Rejected");
269
- if (result.outcome !== "Rejected") return;
270
- assert.equal(
271
- result.rejection.reason,
272
- OutboxAttachmentRejectionReason.MessageTooLarge,
273
- );
274
- assert.equal(
275
- result.rejection.usedBytes,
276
- OUTBOX_ATTACHMENT_MAX_TOTAL_BYTES - 1024,
277
- );
278
- });
279
-
280
- it("refuses once the draft holds the most files a message can", async () => {
281
- const { service } = build();
282
- for (let index = 0; index < OUTBOX_ATTACHMENT_MAX_COUNT; index += 1) {
283
- await mint(service, { sizeBytes: 1 });
284
- }
285
-
286
- const result = await mint(service, { sizeBytes: 1 });
287
-
288
- assert.equal(result.outcome, "Rejected");
289
- if (result.outcome !== "Rejected") return;
290
- assert.equal(
291
- result.rejection.reason,
292
- OutboxAttachmentRejectionReason.TooManyAttachments,
293
- );
294
- });
295
-
296
- it("refuses a file declared as empty, and a filename that sanitizes away", async () => {
297
- const { service } = build();
298
-
299
- const empty = await mint(service, { sizeBytes: 0 });
300
- assert.equal(
301
- empty.outcome === "Rejected" && empty.rejection.reason,
302
- OutboxAttachmentRejectionReason.EmptyFile,
303
- );
304
-
305
- const unnamed = await mint(service, { filename: "../.." });
306
- assert.equal(
307
- unnamed.outcome === "Rejected" && unnamed.rejection.reason,
308
- OutboxAttachmentRejectionReason.UnusableFilename,
309
- );
310
- });
311
-
312
- it("records a filename stripped of its path and a media type it cannot read", async () => {
313
- const { service } = build();
314
-
315
- const result = await mint(service, {
316
- filename: "../../etc/passwd",
317
- contentType: "",
318
- });
319
-
320
- assert.equal(result.outcome, "Minted");
321
- if (result.outcome !== "Minted") return;
322
- assert.equal(result.reservation.filename, "passwd");
323
- assert.equal(result.reservation.contentType, "application/octet-stream");
324
- });
325
-
326
- it("stops holding room once the reservation lapses", async () => {
327
- const clock = { seconds: 1_000_000 };
328
- const { service } = build(OutboxMessageStatus.draft, () => clock.seconds);
329
- await mint(service, {
330
- sizeBytes: OUTBOX_ATTACHMENT_MAX_TOTAL_BYTES - 1024,
331
- });
332
-
333
- assert.equal(
334
- (await mint(service, { sizeBytes: 4096 })).outcome,
335
- "Rejected",
336
- );
337
-
338
- clock.seconds += UPLOAD_URL_TTL_SECONDS + 1;
339
-
340
- assert.equal((await mint(service, { sizeBytes: 4096 })).outcome, "Minted");
341
- });
342
-
343
- it("denies a mint against a draft owned by someone else", async () => {
344
- const { service, repository } = build();
345
-
346
- await assert.rejects(
347
- () => mint(service, { accountConfigId: "cfg-stranger" }),
348
- ForbiddenError,
349
- );
350
- assert.equal(repository.rows.size, 0);
351
- });
352
-
353
- it("refuses to reserve on a message that has left draft", async () => {
354
- const { service } = build(OutboxMessageStatus.queued);
355
-
356
- await assert.rejects(() => mint(service), {
357
- name: "ConflictError",
358
- message: /no longer take an attachment/,
359
- });
360
- });
361
- });
362
-
363
- describe("the cap under concurrency", () => {
364
- /**
365
- * Nothing in this service serializes anything, and nothing needs to: the
366
- * repository counts and inserts in one transaction, so parallel mints — in
367
- * this process or six others — are ordered by the database. These would fail
368
- * if `reserve` ever grew an await between its count and its insert.
369
- */
370
- it("holds the byte cap when files are dropped in together", async () => {
371
- const { service } = build();
372
- const fiveMegabytes = 5 * 1024 * 1024;
373
-
374
- const results = await Promise.all(
375
- Array.from({ length: 6 }, () =>
376
- mint(service, { sizeBytes: fiveMegabytes }),
377
- ),
378
- );
379
-
380
- assert.equal(
381
- results.filter((result) => result.outcome === "Minted").length,
382
- 5,
383
- );
384
- const rejected = results.filter((result) => result.outcome === "Rejected");
385
- assert.equal(rejected.length, 1);
386
- assert.equal(
387
- rejected[0].outcome === "Rejected" && rejected[0].rejection.reason,
388
- OutboxAttachmentRejectionReason.MessageTooLarge,
389
- );
390
- });
391
-
392
- it("holds the file-count ceiling when files are dropped in together", async () => {
393
- const { service, repository } = build();
394
-
395
- const results = await Promise.all(
396
- Array.from({ length: OUTBOX_ATTACHMENT_MAX_COUNT + 5 }, () =>
397
- mint(service, { sizeBytes: 1 }),
398
- ),
399
- );
400
-
401
- assert.equal(
402
- results.filter((result) => result.outcome === "Minted").length,
403
- OUTBOX_ATTACHMENT_MAX_COUNT,
404
- );
405
- assert.equal(repository.rows.size, OUTBOX_ATTACHMENT_MAX_COUNT);
406
- });
407
- });
408
-
409
- describe("completing an attachment", () => {
410
- it("believes storage about the size and moves the row to Stored", async () => {
411
- const { service, storage, repository } = build();
412
- const minted = await mint(service, { sizeBytes: 512 });
413
- assert.equal(minted.outcome, "Minted");
414
- if (minted.outcome !== "Minted") return;
415
- await uploadFor(storage, minted.reservation.outboxAttachmentId, 512);
416
-
417
- const completed = await service.complete({
418
- accountConfigId: ACCOUNT_CONFIG_ID,
419
- outboxMessageId: DRAFT_ID,
420
- outboxAttachmentId: minted.reservation.outboxAttachmentId,
421
- });
422
-
423
- assert.equal(completed.outcome, "Completed");
424
- if (completed.outcome !== "Completed") return;
425
- assert.equal(completed.attachment.sizeBytes, 512);
426
- assert.equal(completed.attachment.state, "Stored");
427
- assert.equal(
428
- repository.rows.get(minted.reservation.outboxAttachmentId)
429
- ?.reservationExpiresAt,
430
- 0,
431
- );
432
- });
433
-
434
- it("answers a repeated completion the same way, not with an error", async () => {
435
- const { service, storage } = build();
436
- const minted = await mint(service, { sizeBytes: 64 });
437
- assert.equal(minted.outcome, "Minted");
438
- if (minted.outcome !== "Minted") return;
439
- await uploadFor(storage, minted.reservation.outboxAttachmentId, 64);
440
-
441
- const input = {
442
- accountConfigId: ACCOUNT_CONFIG_ID,
443
- outboxMessageId: DRAFT_ID,
444
- outboxAttachmentId: minted.reservation.outboxAttachmentId,
445
- };
446
- const first = await service.complete(input);
447
- const second = await service.complete(input);
448
-
449
- assert.equal(first.outcome, "Completed");
450
- assert.equal(second.outcome, "Completed");
451
- assert.deepEqual(
452
- first.outcome === "Completed" && first.attachment,
453
- second.outcome === "Completed" && second.attachment,
454
- );
455
- });
456
-
457
- it("never completes an attachment whose object is absent", async () => {
458
- const { service } = build();
459
- const minted = await mint(service, { sizeBytes: 512 });
460
- assert.equal(minted.outcome, "Minted");
461
- if (minted.outcome !== "Minted") return;
462
-
463
- const completed = await service.complete({
464
- accountConfigId: ACCOUNT_CONFIG_ID,
465
- outboxMessageId: DRAFT_ID,
466
- outboxAttachmentId: minted.reservation.outboxAttachmentId,
467
- });
468
-
469
- assert.equal(
470
- completed.outcome === "Rejected" && completed.rejection.reason,
471
- OutboxAttachmentRejectionReason.UploadMissing,
472
- );
473
- });
474
-
475
- it("refuses a wrong-sized object, removes it, and gives the room back", async () => {
476
- const { service, storage, repository } = build();
477
- const minted = await mint(service, { sizeBytes: 512 });
478
- assert.equal(minted.outcome, "Minted");
479
- if (minted.outcome !== "Minted") return;
480
- await uploadFor(storage, minted.reservation.outboxAttachmentId, 4096);
481
-
482
- const completed = await service.complete({
483
- accountConfigId: ACCOUNT_CONFIG_ID,
484
- outboxMessageId: DRAFT_ID,
485
- outboxAttachmentId: minted.reservation.outboxAttachmentId,
486
- });
487
-
488
- assert.equal(completed.outcome, "Rejected");
489
- if (completed.outcome !== "Rejected") return;
490
- assert.equal(
491
- completed.rejection.reason,
492
- OutboxAttachmentRejectionReason.SizeMismatch,
493
- );
494
- // The room the reservation held is reported as released, not as still held.
495
- assert.equal(completed.rejection.usedBytes, 0);
496
- assert.equal(repository.rows.size, 0);
497
- assert.equal(
498
- await storage.statOutboxAttachment(
499
- ACCOUNT_CONFIG_ID,
500
- ACCOUNT_ID,
501
- DRAFT_ID,
502
- minted.reservation.outboxAttachmentId,
503
- ),
504
- null,
505
- );
506
- });
507
-
508
- it("refuses a completion once the reservation has lapsed", async () => {
509
- const clock = { seconds: 3_000_000 };
510
- const { service, storage } = build(
511
- OutboxMessageStatus.draft,
512
- () => clock.seconds,
513
- );
514
- const minted = await mint(service, { sizeBytes: 64 });
515
- assert.equal(minted.outcome, "Minted");
516
- if (minted.outcome !== "Minted") return;
517
- await uploadFor(storage, minted.reservation.outboxAttachmentId, 64);
518
-
519
- clock.seconds += UPLOAD_URL_TTL_SECONDS + 1;
520
-
521
- const completed = await service.complete({
522
- accountConfigId: ACCOUNT_CONFIG_ID,
523
- outboxMessageId: DRAFT_ID,
524
- outboxAttachmentId: minted.reservation.outboxAttachmentId,
525
- });
526
-
527
- assert.equal(
528
- completed.outcome === "Rejected" && completed.rejection.reason,
529
- OutboxAttachmentRejectionReason.ReservationExpired,
530
- );
531
- });
532
- });
533
-
534
- describe("removing and discarding", () => {
535
- it("retainOnly drops the rows and the bytes it was not told to keep", async () => {
536
- const { service, storage, repository } = build();
537
- const kept = await mint(service, { filename: "keep.txt", sizeBytes: 8 });
538
- const dropped = await mint(service, { filename: "drop.txt", sizeBytes: 8 });
539
- assert.equal(kept.outcome, "Minted");
540
- assert.equal(dropped.outcome, "Minted");
541
- if (kept.outcome !== "Minted" || dropped.outcome !== "Minted") return;
542
- await uploadFor(storage, dropped.reservation.outboxAttachmentId, 8);
543
-
544
- await service.retainOnly(ACCOUNT_CONFIG_ID, ACCOUNT_ID, DRAFT_ID, [
545
- kept.reservation.outboxAttachmentId,
546
- ]);
547
-
548
- assert.deepEqual(
549
- [...repository.rows.keys()],
550
- [kept.reservation.outboxAttachmentId],
551
- );
552
- assert.equal(
553
- await storage.statOutboxAttachment(
554
- ACCOUNT_CONFIG_ID,
555
- ACCOUNT_ID,
556
- DRAFT_ID,
557
- dropped.reservation.outboxAttachmentId,
558
- ),
559
- null,
560
- );
561
- });
562
-
563
- it("discardAll takes every row and every object", async () => {
564
- const { service, storage, repository } = build();
565
- const minted = await mint(service, { sizeBytes: 8 });
566
- assert.equal(minted.outcome, "Minted");
567
- if (minted.outcome !== "Minted") return;
568
- await uploadFor(storage, minted.reservation.outboxAttachmentId, 8);
569
-
570
- await service.discardAll(ACCOUNT_CONFIG_ID, ACCOUNT_ID, DRAFT_ID);
571
-
572
- assert.equal(repository.rows.size, 0);
573
- assert.deepEqual(
574
- await storage.listOutboxAttachments(
575
- ACCOUNT_CONFIG_ID,
576
- ACCOUNT_ID,
577
- DRAFT_ID,
578
- ),
579
- [],
580
- );
581
- });
582
- });
583
-
584
- describe("retainOnly, which is what attachmentIds drives", () => {
585
- const mintTwo = async (service: OutboxAttachmentService) => {
586
- const first = await mint(service, { filename: "one.txt", sizeBytes: 8 });
587
- const second = await mint(service, { filename: "two.txt", sizeBytes: 8 });
588
- assert.equal(first.outcome, "Minted");
589
- assert.equal(second.outcome, "Minted");
590
- if (first.outcome !== "Minted" || second.outcome !== "Minted") {
591
- throw new Error("unreachable");
592
- }
593
- return [first.reservation, second.reservation] as const;
594
- };
595
-
596
- it("keeps everything named and removes everything else, bytes included", async () => {
597
- const { service, storage, repository } = build();
598
- const [keep, drop] = await mintTwo(service);
599
- await uploadFor(storage, keep.outboxAttachmentId, 8);
600
- await uploadFor(storage, drop.outboxAttachmentId, 8);
601
-
602
- await service.retainOnly(ACCOUNT_CONFIG_ID, ACCOUNT_ID, DRAFT_ID, [
603
- keep.outboxAttachmentId,
604
- ]);
605
-
606
- assert.deepEqual([...repository.rows.keys()], [keep.outboxAttachmentId]);
607
- assert.ok(
608
- await storage.statOutboxAttachment(
609
- ACCOUNT_CONFIG_ID,
610
- ACCOUNT_ID,
611
- DRAFT_ID,
612
- keep.outboxAttachmentId,
613
- ),
614
- );
615
- assert.equal(
616
- await storage.statOutboxAttachment(
617
- ACCOUNT_CONFIG_ID,
618
- ACCOUNT_ID,
619
- DRAFT_ID,
620
- drop.outboxAttachmentId,
621
- ),
622
- null,
623
- );
624
- });
625
-
626
- it("an empty list is a real instruction: everything goes", async () => {
627
- const { service, storage, repository } = build();
628
- const [first] = await mintTwo(service);
629
- await uploadFor(storage, first.outboxAttachmentId, 8);
630
-
631
- await service.retainOnly(ACCOUNT_CONFIG_ID, ACCOUNT_ID, DRAFT_ID, []);
632
-
633
- assert.equal(repository.rows.size, 0);
634
- assert.deepEqual(
635
- await storage.listOutboxAttachments(
636
- ACCOUNT_CONFIG_ID,
637
- ACCOUNT_ID,
638
- DRAFT_ID,
639
- ),
640
- [],
641
- );
642
- });
643
-
644
- it("naming every id changes nothing", async () => {
645
- const { service, repository } = build();
646
- const [first, second] = await mintTwo(service);
647
-
648
- await service.retainOnly(ACCOUNT_CONFIG_ID, ACCOUNT_ID, DRAFT_ID, [
649
- first.outboxAttachmentId,
650
- second.outboxAttachmentId,
651
- ]);
652
-
653
- assert.equal(repository.rows.size, 2);
654
- });
655
-
656
- it("an id the draft never held is a no-op, not a removal of the rest", async () => {
657
- const { service, repository } = build();
658
- const [first, second] = await mintTwo(service);
659
-
660
- await service.retainOnly(ACCOUNT_CONFIG_ID, ACCOUNT_ID, DRAFT_ID, [
661
- first.outboxAttachmentId,
662
- second.outboxAttachmentId,
663
- "never-existed",
664
- ]);
665
-
666
- assert.equal(repository.rows.size, 2);
667
- });
668
- });
669
-
670
- describe("a reservation that lapses without ever completing", () => {
671
- it("is reaped, so the sweep can collect the bytes it was vouching for", async () => {
672
- const clock = { seconds: 5_000_000 };
673
- const { service, storage, repository } = build(
674
- OutboxMessageStatus.draft,
675
- () => clock.seconds,
676
- );
677
- const minted = await mint(service, { sizeBytes: 32 });
678
- assert.equal(minted.outcome, "Minted");
679
- if (minted.outcome !== "Minted") return;
680
- // Uploaded, never confirmed — the shape a closed tab leaves behind.
681
- await uploadFor(storage, minted.reservation.outboxAttachmentId, 32);
682
-
683
- clock.seconds += UPLOAD_URL_TTL_SECONDS + 1;
684
-
685
- // Before the reap the row still names the object, which is enough for the
686
- // sweep to leave it alone forever.
687
- const live = await service.reapAndListLive(ACCOUNT_CONFIG_ID, DRAFT_ID);
688
-
689
- assert.deepEqual(live, []);
690
- assert.equal(repository.rows.size, 0);
691
- });
692
-
693
- it("is not shown to the composer as a file the draft holds", async () => {
694
- const clock = { seconds: 6_000_000 };
695
- const { service } = build(OutboxMessageStatus.draft, () => clock.seconds);
696
- await mint(service, { sizeBytes: 32 });
697
-
698
- assert.equal(
699
- (await service.listFor(ACCOUNT_CONFIG_ID, DRAFT_ID)).length,
700
- 1,
701
- );
702
-
703
- clock.seconds += UPLOAD_URL_TTL_SECONDS + 1;
704
-
705
- assert.deepEqual(await service.listFor(ACCOUNT_CONFIG_ID, DRAFT_ID), []);
706
- });
707
-
708
- it("a confirmed attachment cannot be overwritten through its old URL", async () => {
709
- const { service, storage } = build();
710
- const minted = await mint(service, { sizeBytes: 16 });
711
- assert.equal(minted.outcome, "Minted");
712
- if (minted.outcome !== "Minted") return;
713
- await uploadFor(storage, minted.reservation.outboxAttachmentId, 16);
714
-
715
- assert.equal(
716
- await service.hasLiveReservation(
717
- ACCOUNT_CONFIG_ID,
718
- minted.reservation.outboxAttachmentId,
719
- ),
720
- true,
721
- );
722
-
723
- await service.complete({
724
- accountConfigId: ACCOUNT_CONFIG_ID,
725
- outboxMessageId: DRAFT_ID,
726
- outboxAttachmentId: minted.reservation.outboxAttachmentId,
727
- });
728
-
729
- // The URL stays signed for the rest of its window; the row is what refuses.
730
- assert.equal(
731
- await service.hasLiveReservation(
732
- ACCOUNT_CONFIG_ID,
733
- minted.reservation.outboxAttachmentId,
734
- ),
735
- false,
736
- );
737
- });
738
- });