@remit/mailbox-service 0.0.112 → 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 (270) 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} +16 -72
  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} +38 -64
  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} +13 -68
  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 -97
  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 -88
  150. package/src/account-credentials.ts +0 -210
  151. package/src/attribute-mapper.test.ts +0 -104
  152. package/src/attribute-mapper.ts +0 -148
  153. package/src/body-parse.test.ts +0 -55
  154. package/src/body-part-mapper.property.test.ts +0 -354
  155. package/src/body-part-mapper.ts +0 -410
  156. package/src/body-sync-queue.test.ts +0 -64
  157. package/src/body-sync-queue.ts +0 -131
  158. package/src/body-sync-terminal.test.ts +0 -148
  159. package/src/body-sync-terminal.ts +0 -174
  160. package/src/body-sync.test.ts +0 -925
  161. package/src/body-sync.ts +0 -1891
  162. package/src/calendar-parts.ts +0 -41
  163. package/src/classification-backfill.test.ts +0 -285
  164. package/src/classification-backfill.ts +0 -363
  165. package/src/classify-message.test.ts +0 -142
  166. package/src/classify-message.ts +0 -116
  167. package/src/connection-factory.ts +0 -102
  168. package/src/connection-test.ts +0 -208
  169. package/src/credentials.test.ts +0 -181
  170. package/src/external-move.test.ts +0 -262
  171. package/src/external-move.ts +0 -285
  172. package/src/filter-operations.test.ts +0 -319
  173. package/src/filter-operations.ts +0 -377
  174. package/src/filter-update.test.ts +0 -313
  175. package/src/filters/filter-config.test.ts +0 -87
  176. package/src/filters/filter-config.ts +0 -34
  177. package/src/filters/list-id.test.ts +0 -50
  178. package/src/filters/match.test.ts +0 -392
  179. package/src/filters/match.ts +0 -225
  180. package/src/filters/pipeline.test.ts +0 -199
  181. package/src/filters/pipeline.ts +0 -170
  182. package/src/flag-push.test.ts +0 -112
  183. package/src/flag-push.ts +0 -198
  184. package/src/flag-queue.test.ts +0 -197
  185. package/src/flag-queue.ts +0 -509
  186. package/src/folder-operations.test.ts +0 -273
  187. package/src/folder-operations.ts +0 -217
  188. package/src/folder-placement.ts +0 -56
  189. package/src/forwarded-message.test.ts +0 -122
  190. package/src/heuristics/authenticationResults.test.ts +0 -189
  191. package/src/heuristics/authenticationResults.ts +0 -212
  192. package/src/heuristics/classifyByHeaders.test.ts +0 -889
  193. package/src/heuristics/classifyByHeaders.ts +0 -467
  194. package/src/heuristics/classifyPlacement.test.ts +0 -366
  195. package/src/heuristics/classifyPlacement.ts +0 -211
  196. package/src/heuristics/classifyRealMail.test.ts +0 -242
  197. package/src/heuristics/machineSenders.test.ts +0 -101
  198. package/src/heuristics/senderMismatch.test.ts +0 -346
  199. package/src/heuristics/senderMismatch.ts +0 -259
  200. package/src/imapflow-connect-retry.test.ts +0 -97
  201. package/src/imapflow-connection.test.ts +0 -503
  202. package/src/imapflow-connection.ts +0 -1476
  203. package/src/index.ts +0 -397
  204. package/src/list-id-backfill.test.ts +0 -207
  205. package/src/list-id-backfill.ts +0 -274
  206. package/src/mailbox-cursor-rebuild.test.ts +0 -171
  207. package/src/mailbox-cursor-rebuild.ts +0 -146
  208. package/src/mailbox-cursor.test.ts +0 -141
  209. package/src/mailbox-delete-intent.test.ts +0 -119
  210. package/src/mailbox-management.test.ts +0 -243
  211. package/src/mailbox-management.ts +0 -838
  212. package/src/mailbox-presence.test.ts +0 -74
  213. package/src/mailbox-queue.ts +0 -397
  214. package/src/mailbox-rename-intent.test.ts +0 -220
  215. package/src/mailbox-rename-subtree.test.ts +0 -287
  216. package/src/mailbox-sync.test.ts +0 -286
  217. package/src/mailbox-sync.ts +0 -724
  218. package/src/message-move.test.ts +0 -726
  219. package/src/message-move.ts +0 -1518
  220. package/src/message-parser.ts +0 -26
  221. package/src/message-presence.test.ts +0 -50
  222. package/src/message-read.ts +0 -87
  223. package/src/message-save-retry.test.ts +0 -90
  224. package/src/message-sync-flags.test.ts +0 -167
  225. package/src/message-sync.test.ts +0 -762
  226. package/src/message-sync.ts +0 -2133
  227. package/src/mime-walker.test.ts +0 -311
  228. package/src/mime-walker.ts +0 -306
  229. package/src/organize-job.ts +0 -93
  230. package/src/outbox-attachment-content.test.ts +0 -153
  231. package/src/outbox-attachment-content.ts +0 -104
  232. package/src/outbox-attachment-filename.test.ts +0 -132
  233. package/src/outbox-attachment-filename.ts +0 -76
  234. package/src/outbox-attachment.test.ts +0 -626
  235. package/src/outbox-attachment.ts +0 -536
  236. package/src/outbox-failed-recovery.test.ts +0 -299
  237. package/src/outbox-queue.test.ts +0 -315
  238. package/src/outbox-queue.ts +0 -525
  239. package/src/ownership.ts +0 -140
  240. package/src/placement-move-terminal.ts +0 -145
  241. package/src/placement-move.ts +0 -363
  242. package/src/placement-settled.test.ts +0 -147
  243. package/src/placement.test.ts +0 -404
  244. package/src/quarantine.test.ts +0 -133
  245. package/src/quarantine.ts +0 -229
  246. package/src/refusal.ts +0 -29
  247. package/src/snippet.test.ts +0 -213
  248. package/src/snippet.ts +0 -152
  249. package/src/spam-report.test.ts +0 -329
  250. package/src/spam-report.ts +0 -357
  251. package/src/sync-warning.test.ts +0 -213
  252. package/src/sync-warning.ts +0 -56
  253. package/src/sync-watermarks.test.ts +0 -409
  254. package/src/sync-watermarks.ts +0 -319
  255. package/src/system-flag-wire-format.test.ts +0 -77
  256. package/src/test-helpers/fake-imap.ts +0 -690
  257. package/src/test-helpers/folder-roles.ts +0 -66
  258. package/src/test-helpers/mime-tree-generator.ts +0 -461
  259. package/src/test-helpers/test-queue.ts +0 -56
  260. package/src/test-helpers/test-store.ts +0 -299
  261. package/src/text/normalizer.test.ts +0 -223
  262. package/src/text/normalizer.ts +0 -140
  263. package/src/thread-message-rows.ts +0 -77
  264. package/src/thread-root.test.ts +0 -29
  265. package/src/thread-root.ts +0 -51
  266. package/src/types/mailparser-augment.d.ts +0 -23
  267. package/src/types/natural-porter-stemmer.d.ts +0 -50
  268. package/src/types/stopword.d.ts +0 -69
  269. package/src/types.ts +0 -371
  270. package/tsconfig.json +0 -8
@@ -1,626 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { afterEach, describe, it } from "node:test";
3
- import type { OutboxMessageItem } from "@remit/data-ports";
4
- import { ForbiddenError } from "@remit/data-ports/errors";
5
- import {
6
- OutboxAttachmentRejectionReason,
7
- OutboxMessageStatus,
8
- } from "@remit/domain-enums";
9
- import {
10
- type StorageService,
11
- UPLOAD_URL_TTL_SECONDS,
12
- } from "@remit/storage-service";
13
- import {
14
- OUTBOX_ATTACHMENT_MAX_COUNT,
15
- OUTBOX_ATTACHMENT_MAX_TOTAL_BYTES,
16
- OutboxAttachmentService,
17
- } from "./outbox-attachment.js";
18
- import { openStore, type Store } from "./test-helpers/test-store.js";
19
-
20
- const ACCOUNT_CONFIG_ID = "cfg-679";
21
- const ACCOUNT_ID = "acc-679";
22
-
23
- let store: Store;
24
- let draftId: string;
25
-
26
- afterEach(() => store?.close());
27
-
28
- const build = async (
29
- status: OutboxMessageItem["status"] = OutboxMessageStatus.draft,
30
- now?: () => number,
31
- ) => {
32
- store = await openStore();
33
- const storage = store.storage();
34
- const draft = await store.outboxMessage.create({
35
- accountId: ACCOUNT_ID,
36
- accountConfigId: ACCOUNT_CONFIG_ID,
37
- fromAddress: "me@example.com",
38
- toAddresses: ["them@example.com"],
39
- messageIdValue: "<draft@example.com>",
40
- status,
41
- });
42
- draftId = draft.outboxMessageId;
43
- return {
44
- service: new OutboxAttachmentService({
45
- outboxMessageService: store.outboxMessage,
46
- outboxAttachmentService: store.outboxAttachment,
47
- storage,
48
- now,
49
- }),
50
- storage,
51
- };
52
- };
53
-
54
- const rows = () =>
55
- store.outboxAttachment.listByOutboxMessage(ACCOUNT_CONFIG_ID, draftId);
56
-
57
- const rowOf = (outboxAttachmentId: string) =>
58
- store.outboxAttachment.get(ACCOUNT_CONFIG_ID, outboxAttachmentId);
59
-
60
- const ids = async () => (await rows()).map((row) => row.outboxAttachmentId);
61
-
62
- const mint = (
63
- service: OutboxAttachmentService,
64
- overrides: Partial<{
65
- accountConfigId: string;
66
- filename: string;
67
- contentType: string;
68
- sizeBytes: number;
69
- }> = {},
70
- ) =>
71
- service.mint({
72
- accountConfigId: overrides.accountConfigId ?? ACCOUNT_CONFIG_ID,
73
- outboxMessageId: draftId,
74
- filename: overrides.filename ?? "notes.txt",
75
- contentType: overrides.contentType ?? "text/plain",
76
- sizeBytes: overrides.sizeBytes ?? 10,
77
- });
78
-
79
- const uploadFor = (
80
- storage: StorageService,
81
- outboxAttachmentId: string,
82
- sizeBytes: number,
83
- ) =>
84
- storage.storeOutboxAttachment({
85
- accountConfigId: ACCOUNT_CONFIG_ID,
86
- accountId: ACCOUNT_ID,
87
- outboxMessageId: draftId,
88
- outboxAttachmentId,
89
- content: Buffer.alloc(sizeBytes),
90
- });
91
-
92
- describe("reserving room on a draft", () => {
93
- it("writes a Pending row and hands back a URL bound to its size", async () => {
94
- const { service } = await build();
95
-
96
- const result = await mint(service, {
97
- filename: "invoice.pdf",
98
- contentType: "application/pdf",
99
- sizeBytes: 2048,
100
- });
101
-
102
- assert.equal(result.outcome, "Minted");
103
- if (result.outcome !== "Minted") return;
104
- assert.equal(result.reservation.filename, "invoice.pdf");
105
- assert.equal(result.reservation.contentType, "application/pdf");
106
- assert.match(result.reservation.uploadUrl, /max=2048/);
107
-
108
- const row = await rowOf(result.reservation.outboxAttachmentId);
109
- assert.equal(row?.state, "Pending");
110
- assert.equal(row?.sizeBytes, 2048);
111
- // The key on the row is the one the URL addresses — one identity, not two.
112
- assert.ok(row?.storageKey.endsWith(result.reservation.outboxAttachmentId));
113
- });
114
-
115
- it("names the content-id a body can refer to the file by", async () => {
116
- const { service } = await build();
117
-
118
- const result = await mint(service, {
119
- filename: "shot.png",
120
- contentType: "image/png",
121
- sizeBytes: 64,
122
- });
123
-
124
- assert.equal(result.outcome, "Minted");
125
- if (result.outcome !== "Minted") return;
126
- assert.equal(
127
- result.reservation.contentId,
128
- `${result.reservation.outboxAttachmentId}@remit`,
129
- );
130
- });
131
-
132
- it("refuses a declared size over the cap", async () => {
133
- const { service } = await build();
134
-
135
- const result = await mint(service, {
136
- filename: "huge.bin",
137
- sizeBytes: OUTBOX_ATTACHMENT_MAX_TOTAL_BYTES + 1,
138
- });
139
-
140
- assert.equal(result.outcome, "Rejected");
141
- if (result.outcome !== "Rejected") return;
142
- assert.equal(
143
- result.rejection.reason,
144
- OutboxAttachmentRejectionReason.FileTooLarge,
145
- );
146
- });
147
-
148
- it("counts a reservation nobody has uploaded against yet", async () => {
149
- const { service } = await build();
150
- await mint(service, {
151
- sizeBytes: OUTBOX_ATTACHMENT_MAX_TOTAL_BYTES - 1024,
152
- });
153
-
154
- const result = await mint(service, { sizeBytes: 4096 });
155
-
156
- assert.equal(result.outcome, "Rejected");
157
- if (result.outcome !== "Rejected") return;
158
- assert.equal(
159
- result.rejection.reason,
160
- OutboxAttachmentRejectionReason.MessageTooLarge,
161
- );
162
- assert.equal(
163
- result.rejection.usedBytes,
164
- OUTBOX_ATTACHMENT_MAX_TOTAL_BYTES - 1024,
165
- );
166
- });
167
-
168
- it("refuses once the draft holds the most files a message can", async () => {
169
- const { service } = await build();
170
- for (let index = 0; index < OUTBOX_ATTACHMENT_MAX_COUNT; index += 1) {
171
- await mint(service, { sizeBytes: 1 });
172
- }
173
-
174
- const result = await mint(service, { sizeBytes: 1 });
175
-
176
- assert.equal(result.outcome, "Rejected");
177
- if (result.outcome !== "Rejected") return;
178
- assert.equal(
179
- result.rejection.reason,
180
- OutboxAttachmentRejectionReason.TooManyAttachments,
181
- );
182
- });
183
-
184
- it("refuses a file declared as empty, and a filename that sanitizes away", async () => {
185
- const { service } = await build();
186
-
187
- const empty = await mint(service, { sizeBytes: 0 });
188
- assert.equal(
189
- empty.outcome === "Rejected" && empty.rejection.reason,
190
- OutboxAttachmentRejectionReason.EmptyFile,
191
- );
192
-
193
- const unnamed = await mint(service, { filename: "../.." });
194
- assert.equal(
195
- unnamed.outcome === "Rejected" && unnamed.rejection.reason,
196
- OutboxAttachmentRejectionReason.UnusableFilename,
197
- );
198
- });
199
-
200
- it("records a filename stripped of its path and a media type it cannot read", async () => {
201
- const { service } = await build();
202
-
203
- const result = await mint(service, {
204
- filename: "../../etc/passwd",
205
- contentType: "",
206
- });
207
-
208
- assert.equal(result.outcome, "Minted");
209
- if (result.outcome !== "Minted") return;
210
- assert.equal(result.reservation.filename, "passwd");
211
- assert.equal(result.reservation.contentType, "application/octet-stream");
212
- });
213
-
214
- it("stops holding room once the reservation lapses", async () => {
215
- const clock = { seconds: 1_000_000 };
216
- const { service } = await build(
217
- OutboxMessageStatus.draft,
218
- () => clock.seconds,
219
- );
220
- await mint(service, {
221
- sizeBytes: OUTBOX_ATTACHMENT_MAX_TOTAL_BYTES - 1024,
222
- });
223
-
224
- assert.equal(
225
- (await mint(service, { sizeBytes: 4096 })).outcome,
226
- "Rejected",
227
- );
228
-
229
- clock.seconds += UPLOAD_URL_TTL_SECONDS + 1;
230
-
231
- assert.equal((await mint(service, { sizeBytes: 4096 })).outcome, "Minted");
232
- });
233
-
234
- it("denies a mint against a draft owned by someone else", async () => {
235
- const { service } = await build();
236
-
237
- await assert.rejects(
238
- () => mint(service, { accountConfigId: "cfg-stranger" }),
239
- ForbiddenError,
240
- );
241
- assert.equal((await rows()).length, 0);
242
- });
243
-
244
- it("refuses to reserve on a message that has left draft", async () => {
245
- const { service } = await build(OutboxMessageStatus.queued);
246
-
247
- await assert.rejects(() => mint(service), {
248
- name: "ConflictError",
249
- message: /no longer take an attachment/,
250
- });
251
- });
252
- });
253
-
254
- describe("the cap under concurrency", () => {
255
- /**
256
- * Nothing in this service serializes anything, and nothing needs to: the
257
- * repository counts and inserts in one transaction, so parallel mints — in
258
- * this process or six others — are ordered by the database. These would fail
259
- * if `reserve` ever grew an await between its count and its insert.
260
- */
261
- it("holds the byte cap when files are dropped in together", async () => {
262
- const { service } = await build();
263
- const fiveMegabytes = 5 * 1024 * 1024;
264
-
265
- const results = await Promise.all(
266
- Array.from({ length: 6 }, () =>
267
- mint(service, { sizeBytes: fiveMegabytes }),
268
- ),
269
- );
270
-
271
- assert.equal(
272
- results.filter((result) => result.outcome === "Minted").length,
273
- 5,
274
- );
275
- const rejected = results.filter((result) => result.outcome === "Rejected");
276
- assert.equal(rejected.length, 1);
277
- assert.equal(
278
- rejected[0].outcome === "Rejected" && rejected[0].rejection.reason,
279
- OutboxAttachmentRejectionReason.MessageTooLarge,
280
- );
281
- });
282
-
283
- it("holds the file-count ceiling when files are dropped in together", async () => {
284
- const { service } = await build();
285
-
286
- const results = await Promise.all(
287
- Array.from({ length: OUTBOX_ATTACHMENT_MAX_COUNT + 5 }, () =>
288
- mint(service, { sizeBytes: 1 }),
289
- ),
290
- );
291
-
292
- assert.equal(
293
- results.filter((result) => result.outcome === "Minted").length,
294
- OUTBOX_ATTACHMENT_MAX_COUNT,
295
- );
296
- assert.equal((await rows()).length, OUTBOX_ATTACHMENT_MAX_COUNT);
297
- });
298
- });
299
-
300
- describe("completing an attachment", () => {
301
- it("believes storage about the size and moves the row to Stored", async () => {
302
- const { service, storage } = await build();
303
- const minted = await mint(service, { sizeBytes: 512 });
304
- assert.equal(minted.outcome, "Minted");
305
- if (minted.outcome !== "Minted") return;
306
- await uploadFor(storage, minted.reservation.outboxAttachmentId, 512);
307
-
308
- const completed = await service.complete({
309
- accountConfigId: ACCOUNT_CONFIG_ID,
310
- outboxMessageId: draftId,
311
- outboxAttachmentId: minted.reservation.outboxAttachmentId,
312
- });
313
-
314
- assert.equal(completed.outcome, "Completed");
315
- if (completed.outcome !== "Completed") return;
316
- assert.equal(completed.attachment.sizeBytes, 512);
317
- assert.equal(completed.attachment.state, "Stored");
318
- assert.equal(
319
- (await rowOf(minted.reservation.outboxAttachmentId))
320
- ?.reservationExpiresAt,
321
- 0,
322
- );
323
- });
324
-
325
- it("answers a repeated completion the same way, not with an error", async () => {
326
- const { service, storage } = await build();
327
- const minted = await mint(service, { sizeBytes: 64 });
328
- assert.equal(minted.outcome, "Minted");
329
- if (minted.outcome !== "Minted") return;
330
- await uploadFor(storage, minted.reservation.outboxAttachmentId, 64);
331
-
332
- const input = {
333
- accountConfigId: ACCOUNT_CONFIG_ID,
334
- outboxMessageId: draftId,
335
- outboxAttachmentId: minted.reservation.outboxAttachmentId,
336
- };
337
- const first = await service.complete(input);
338
- const second = await service.complete(input);
339
-
340
- assert.equal(first.outcome, "Completed");
341
- assert.equal(second.outcome, "Completed");
342
- assert.deepEqual(
343
- first.outcome === "Completed" && first.attachment,
344
- second.outcome === "Completed" && second.attachment,
345
- );
346
- });
347
-
348
- it("never completes an attachment whose object is absent", async () => {
349
- const { service } = await build();
350
- const minted = await mint(service, { sizeBytes: 512 });
351
- assert.equal(minted.outcome, "Minted");
352
- if (minted.outcome !== "Minted") return;
353
-
354
- const completed = await service.complete({
355
- accountConfigId: ACCOUNT_CONFIG_ID,
356
- outboxMessageId: draftId,
357
- outboxAttachmentId: minted.reservation.outboxAttachmentId,
358
- });
359
-
360
- assert.equal(
361
- completed.outcome === "Rejected" && completed.rejection.reason,
362
- OutboxAttachmentRejectionReason.UploadMissing,
363
- );
364
- });
365
-
366
- it("refuses a wrong-sized object, removes it, and gives the room back", async () => {
367
- const { service, storage } = await build();
368
- const minted = await mint(service, { sizeBytes: 512 });
369
- assert.equal(minted.outcome, "Minted");
370
- if (minted.outcome !== "Minted") return;
371
- await uploadFor(storage, minted.reservation.outboxAttachmentId, 4096);
372
-
373
- const completed = await service.complete({
374
- accountConfigId: ACCOUNT_CONFIG_ID,
375
- outboxMessageId: draftId,
376
- outboxAttachmentId: minted.reservation.outboxAttachmentId,
377
- });
378
-
379
- assert.equal(completed.outcome, "Rejected");
380
- if (completed.outcome !== "Rejected") return;
381
- assert.equal(
382
- completed.rejection.reason,
383
- OutboxAttachmentRejectionReason.SizeMismatch,
384
- );
385
- // The room the reservation held is reported as released, not as still held.
386
- assert.equal(completed.rejection.usedBytes, 0);
387
- assert.equal((await rows()).length, 0);
388
- assert.equal(
389
- await storage.statOutboxAttachment(
390
- ACCOUNT_CONFIG_ID,
391
- ACCOUNT_ID,
392
- draftId,
393
- minted.reservation.outboxAttachmentId,
394
- ),
395
- null,
396
- );
397
- });
398
-
399
- it("refuses a completion once the reservation has lapsed", async () => {
400
- const clock = { seconds: 3_000_000 };
401
- const { service, storage } = await build(
402
- OutboxMessageStatus.draft,
403
- () => clock.seconds,
404
- );
405
- const minted = await mint(service, { sizeBytes: 64 });
406
- assert.equal(minted.outcome, "Minted");
407
- if (minted.outcome !== "Minted") return;
408
- await uploadFor(storage, minted.reservation.outboxAttachmentId, 64);
409
-
410
- clock.seconds += UPLOAD_URL_TTL_SECONDS + 1;
411
-
412
- const completed = await service.complete({
413
- accountConfigId: ACCOUNT_CONFIG_ID,
414
- outboxMessageId: draftId,
415
- outboxAttachmentId: minted.reservation.outboxAttachmentId,
416
- });
417
-
418
- assert.equal(
419
- completed.outcome === "Rejected" && completed.rejection.reason,
420
- OutboxAttachmentRejectionReason.ReservationExpired,
421
- );
422
- });
423
- });
424
-
425
- describe("removing and discarding", () => {
426
- it("retainOnly drops the rows and the bytes it was not told to keep", async () => {
427
- const { service, storage } = await build();
428
- const kept = await mint(service, { filename: "keep.txt", sizeBytes: 8 });
429
- const dropped = await mint(service, { filename: "drop.txt", sizeBytes: 8 });
430
- assert.equal(kept.outcome, "Minted");
431
- assert.equal(dropped.outcome, "Minted");
432
- if (kept.outcome !== "Minted" || dropped.outcome !== "Minted") return;
433
- await uploadFor(storage, dropped.reservation.outboxAttachmentId, 8);
434
-
435
- await service.retainOnly(ACCOUNT_CONFIG_ID, ACCOUNT_ID, draftId, [
436
- kept.reservation.outboxAttachmentId,
437
- ]);
438
-
439
- assert.deepEqual(await ids(), [kept.reservation.outboxAttachmentId]);
440
- assert.equal(
441
- await storage.statOutboxAttachment(
442
- ACCOUNT_CONFIG_ID,
443
- ACCOUNT_ID,
444
- draftId,
445
- dropped.reservation.outboxAttachmentId,
446
- ),
447
- null,
448
- );
449
- });
450
-
451
- it("discardAll takes every row and every object", async () => {
452
- const { service, storage } = await build();
453
- const minted = await mint(service, { sizeBytes: 8 });
454
- assert.equal(minted.outcome, "Minted");
455
- if (minted.outcome !== "Minted") return;
456
- await uploadFor(storage, minted.reservation.outboxAttachmentId, 8);
457
-
458
- await service.discardAll(ACCOUNT_CONFIG_ID, ACCOUNT_ID, draftId);
459
-
460
- assert.equal((await rows()).length, 0);
461
- assert.deepEqual(
462
- await storage.listOutboxAttachments(
463
- ACCOUNT_CONFIG_ID,
464
- ACCOUNT_ID,
465
- draftId,
466
- ),
467
- [],
468
- );
469
- });
470
- });
471
-
472
- describe("retainOnly, which is what attachmentIds drives", () => {
473
- const mintTwo = async (service: OutboxAttachmentService) => {
474
- const first = await mint(service, { filename: "one.txt", sizeBytes: 8 });
475
- const second = await mint(service, { filename: "two.txt", sizeBytes: 8 });
476
- assert.equal(first.outcome, "Minted");
477
- assert.equal(second.outcome, "Minted");
478
- if (first.outcome !== "Minted" || second.outcome !== "Minted") {
479
- throw new Error("unreachable");
480
- }
481
- return [first.reservation, second.reservation] as const;
482
- };
483
-
484
- it("keeps everything named and removes everything else, bytes included", async () => {
485
- const { service, storage } = await build();
486
- const [keep, drop] = await mintTwo(service);
487
- await uploadFor(storage, keep.outboxAttachmentId, 8);
488
- await uploadFor(storage, drop.outboxAttachmentId, 8);
489
-
490
- await service.retainOnly(ACCOUNT_CONFIG_ID, ACCOUNT_ID, draftId, [
491
- keep.outboxAttachmentId,
492
- ]);
493
-
494
- assert.deepEqual(await ids(), [keep.outboxAttachmentId]);
495
- assert.ok(
496
- await storage.statOutboxAttachment(
497
- ACCOUNT_CONFIG_ID,
498
- ACCOUNT_ID,
499
- draftId,
500
- keep.outboxAttachmentId,
501
- ),
502
- );
503
- assert.equal(
504
- await storage.statOutboxAttachment(
505
- ACCOUNT_CONFIG_ID,
506
- ACCOUNT_ID,
507
- draftId,
508
- drop.outboxAttachmentId,
509
- ),
510
- null,
511
- );
512
- });
513
-
514
- it("an empty list is a real instruction: everything goes", async () => {
515
- const { service, storage } = await build();
516
- const [first] = await mintTwo(service);
517
- await uploadFor(storage, first.outboxAttachmentId, 8);
518
-
519
- await service.retainOnly(ACCOUNT_CONFIG_ID, ACCOUNT_ID, draftId, []);
520
-
521
- assert.equal((await rows()).length, 0);
522
- assert.deepEqual(
523
- await storage.listOutboxAttachments(
524
- ACCOUNT_CONFIG_ID,
525
- ACCOUNT_ID,
526
- draftId,
527
- ),
528
- [],
529
- );
530
- });
531
-
532
- it("naming every id changes nothing", async () => {
533
- const { service } = await build();
534
- const [first, second] = await mintTwo(service);
535
-
536
- await service.retainOnly(ACCOUNT_CONFIG_ID, ACCOUNT_ID, draftId, [
537
- first.outboxAttachmentId,
538
- second.outboxAttachmentId,
539
- ]);
540
-
541
- assert.equal((await rows()).length, 2);
542
- });
543
-
544
- it("an id the draft never held is a no-op, not a removal of the rest", async () => {
545
- const { service } = await build();
546
- const [first, second] = await mintTwo(service);
547
-
548
- await service.retainOnly(ACCOUNT_CONFIG_ID, ACCOUNT_ID, draftId, [
549
- first.outboxAttachmentId,
550
- second.outboxAttachmentId,
551
- "never-existed",
552
- ]);
553
-
554
- assert.equal((await rows()).length, 2);
555
- });
556
- });
557
-
558
- describe("a reservation that lapses without ever completing", () => {
559
- it("is reaped, so the sweep can collect the bytes it was vouching for", async () => {
560
- const clock = { seconds: 5_000_000 };
561
- const { service, storage } = await build(
562
- OutboxMessageStatus.draft,
563
- () => clock.seconds,
564
- );
565
- const minted = await mint(service, { sizeBytes: 32 });
566
- assert.equal(minted.outcome, "Minted");
567
- if (minted.outcome !== "Minted") return;
568
- // Uploaded, never confirmed — the shape a closed tab leaves behind.
569
- await uploadFor(storage, minted.reservation.outboxAttachmentId, 32);
570
-
571
- clock.seconds += UPLOAD_URL_TTL_SECONDS + 1;
572
-
573
- // Before the reap the row still names the object, which is enough for the
574
- // sweep to leave it alone forever.
575
- const live = await service.reapAndListLive(ACCOUNT_CONFIG_ID, draftId);
576
-
577
- assert.deepEqual(live, []);
578
- assert.equal((await rows()).length, 0);
579
- });
580
-
581
- it("is not shown to the composer as a file the draft holds", async () => {
582
- const clock = { seconds: 6_000_000 };
583
- const { service } = await build(
584
- OutboxMessageStatus.draft,
585
- () => clock.seconds,
586
- );
587
- await mint(service, { sizeBytes: 32 });
588
-
589
- assert.equal((await service.listFor(ACCOUNT_CONFIG_ID, draftId)).length, 1);
590
-
591
- clock.seconds += UPLOAD_URL_TTL_SECONDS + 1;
592
-
593
- assert.deepEqual(await service.listFor(ACCOUNT_CONFIG_ID, draftId), []);
594
- });
595
-
596
- it("a confirmed attachment cannot be overwritten through its old URL", async () => {
597
- const { service, storage } = await build();
598
- const minted = await mint(service, { sizeBytes: 16 });
599
- assert.equal(minted.outcome, "Minted");
600
- if (minted.outcome !== "Minted") return;
601
- await uploadFor(storage, minted.reservation.outboxAttachmentId, 16);
602
-
603
- assert.equal(
604
- await service.hasLiveReservation(
605
- ACCOUNT_CONFIG_ID,
606
- minted.reservation.outboxAttachmentId,
607
- ),
608
- true,
609
- );
610
-
611
- await service.complete({
612
- accountConfigId: ACCOUNT_CONFIG_ID,
613
- outboxMessageId: draftId,
614
- outboxAttachmentId: minted.reservation.outboxAttachmentId,
615
- });
616
-
617
- // The URL stays signed for the rest of its window; the row is what refuses.
618
- assert.equal(
619
- await service.hasLiveReservation(
620
- ACCOUNT_CONFIG_ID,
621
- minted.reservation.outboxAttachmentId,
622
- ),
623
- false,
624
- );
625
- });
626
- });