@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.
- package/dist/account-credentials.d.ts +83 -0
- package/dist/account-credentials.js +104 -0
- package/dist/attribute-mapper.d.ts +36 -0
- package/dist/attribute-mapper.js +119 -0
- package/dist/body-parse.d.ts +28 -0
- package/{src/body-parse.ts → dist/body-parse.js} +18 -26
- package/dist/body-part-mapper.d.ts +92 -0
- package/dist/body-part-mapper.js +301 -0
- package/dist/body-sync-queue.d.ts +59 -0
- package/dist/body-sync-queue.js +56 -0
- package/dist/body-sync-terminal.d.ts +58 -0
- package/dist/body-sync-terminal.js +95 -0
- package/dist/body-sync.d.ts +351 -0
- package/dist/body-sync.js +1204 -0
- package/dist/calendar-parts.d.ts +23 -0
- package/dist/calendar-parts.js +18 -0
- package/dist/classification-backfill.d.ts +91 -0
- package/dist/classification-backfill.js +180 -0
- package/dist/classify-message.d.ts +31 -0
- package/dist/classify-message.js +89 -0
- package/dist/connection-factory.d.ts +54 -0
- package/dist/connection-factory.js +54 -0
- package/dist/connection-test.d.ts +41 -0
- package/dist/connection-test.js +152 -0
- package/dist/external-move.d.ts +94 -0
- package/dist/external-move.js +173 -0
- package/dist/filter-operations.d.ts +42 -0
- package/dist/filter-operations.js +211 -0
- package/dist/filters/filter-config.d.ts +14 -0
- package/dist/filters/filter-config.js +10 -0
- package/{src/filters/list-id.ts → dist/filters/list-id.d.ts} +2 -16
- package/dist/filters/list-id.js +26 -0
- package/dist/filters/match.d.ts +69 -0
- package/dist/filters/match.js +165 -0
- package/dist/filters/pipeline.d.ts +64 -0
- package/dist/filters/pipeline.js +100 -0
- package/{src/flag-push-terminal.ts → dist/flag-push-terminal.d.ts} +16 -72
- package/dist/flag-push-terminal.js +63 -0
- package/dist/flag-push.d.ts +69 -0
- package/dist/flag-push.js +111 -0
- package/dist/flag-queue.d.ts +175 -0
- package/dist/flag-queue.js +291 -0
- package/dist/folder-operations.d.ts +39 -0
- package/dist/folder-operations.js +112 -0
- package/dist/folder-placement.d.ts +13 -0
- package/dist/folder-placement.js +23 -0
- package/dist/heuristics/authenticationResults.d.ts +10 -0
- package/dist/heuristics/authenticationResults.js +187 -0
- package/dist/heuristics/classifyByHeaders.d.ts +82 -0
- package/dist/heuristics/classifyByHeaders.js +365 -0
- package/dist/heuristics/classifyPlacement.d.ts +67 -0
- package/dist/heuristics/classifyPlacement.js +153 -0
- package/dist/heuristics/machineSenders.d.ts +21 -0
- package/{src/heuristics/machineSenders.ts → dist/heuristics/machineSenders.js} +34 -40
- package/dist/heuristics/senderMismatch.d.ts +81 -0
- package/dist/heuristics/senderMismatch.js +192 -0
- package/dist/heuristics/socialDomains.d.ts +14 -0
- package/{src/heuristics/socialDomains.ts → dist/heuristics/socialDomains.js} +9 -9
- package/dist/heuristics/transactionalDomains.d.ts +18 -0
- package/{src/heuristics/transactionalDomains.ts → dist/heuristics/transactionalDomains.js} +8 -8
- package/dist/imapflow-connection.d.ts +377 -0
- package/dist/imapflow-connection.js +1147 -0
- package/dist/index.d.ts +63 -0
- package/dist/index.js +65 -0
- package/dist/list-id-backfill.d.ts +83 -0
- package/dist/list-id-backfill.js +132 -0
- package/dist/mailbox-cursor-rebuild.d.ts +74 -0
- package/dist/mailbox-cursor-rebuild.js +71 -0
- package/{src/mailbox-cursor.ts → dist/mailbox-cursor.d.ts} +20 -98
- package/dist/mailbox-cursor.js +102 -0
- package/dist/mailbox-intent.d.ts +41 -0
- package/{src/mailbox-intent.ts → dist/mailbox-intent.js} +38 -64
- package/dist/mailbox-management.d.ts +240 -0
- package/dist/mailbox-management.js +576 -0
- package/dist/mailbox-presence.d.ts +52 -0
- package/{src/mailbox-presence.ts → dist/mailbox-presence.js} +19 -30
- package/dist/mailbox-queue.d.ts +126 -0
- package/dist/mailbox-queue.js +214 -0
- package/dist/mailbox-sync.d.ts +93 -0
- package/dist/mailbox-sync.js +490 -0
- package/dist/mailbox-upstream.d.ts +38 -0
- package/{src/mailbox-upstream.ts → dist/mailbox-upstream.js} +37 -47
- package/dist/message-category.d.ts +51 -0
- package/{src/message-category.ts → dist/message-category.js} +28 -71
- package/dist/message-move.d.ts +285 -0
- package/dist/message-move.js +858 -0
- package/dist/message-parser.d.ts +14 -0
- package/dist/message-parser.js +17 -0
- package/{src/message-presence.ts → dist/message-presence.d.ts} +1 -13
- package/dist/message-presence.js +29 -0
- package/dist/message-read.d.ts +51 -0
- package/dist/message-read.js +31 -0
- package/dist/message-sync.d.ts +345 -0
- package/dist/message-sync.js +1358 -0
- package/dist/mime-walker.d.ts +102 -0
- package/dist/mime-walker.js +198 -0
- package/dist/organize-job.d.ts +20 -0
- package/dist/organize-job.js +37 -0
- package/dist/outbox-attachment-content.d.ts +37 -0
- package/dist/outbox-attachment-content.js +50 -0
- package/dist/outbox-attachment-filename.d.ts +19 -0
- package/dist/outbox-attachment-filename.js +64 -0
- package/dist/outbox-attachment.d.ts +148 -0
- package/dist/outbox-attachment.js +273 -0
- package/dist/outbox-queue.d.ts +73 -0
- package/dist/outbox-queue.js +284 -0
- package/{src/outbox-status.ts → dist/outbox-status.d.ts} +1 -6
- package/dist/outbox-status.js +18 -0
- package/dist/ownership.d.ts +24 -0
- package/dist/ownership.js +83 -0
- package/dist/placement-move-terminal.d.ts +67 -0
- package/dist/placement-move-terminal.js +88 -0
- package/dist/placement-move.d.ts +83 -0
- package/dist/placement-move.js +216 -0
- package/{src/placement-settled.ts → dist/placement-settled.d.ts} +13 -68
- package/dist/placement-settled.js +75 -0
- package/dist/quarantine.d.ts +95 -0
- package/dist/quarantine.js +131 -0
- package/dist/refusal.d.ts +31 -0
- package/dist/refusal.js +3 -0
- package/{src/restore-source-placement.ts → dist/restore-source-placement.d.ts} +29 -97
- package/dist/restore-source-placement.js +79 -0
- package/dist/search-text-backfill.d.ts +15 -0
- package/dist/search-text-backfill.js +33 -0
- package/dist/snippet.d.ts +61 -0
- package/dist/snippet.js +129 -0
- package/dist/spam-report.d.ts +86 -0
- package/dist/spam-report.js +241 -0
- package/dist/sync-warning.d.ts +21 -0
- package/dist/sync-warning.js +26 -0
- package/dist/sync-watermarks.d.ts +138 -0
- package/dist/sync-watermarks.js +205 -0
- package/dist/test-helpers/fake-imap.d.ts +151 -0
- package/dist/test-helpers/fake-imap.js +508 -0
- package/dist/test-helpers/folder-roles.d.ts +34 -0
- package/dist/test-helpers/folder-roles.js +47 -0
- package/dist/test-helpers/mime-tree-generator.d.ts +82 -0
- package/dist/test-helpers/mime-tree-generator.js +329 -0
- package/dist/test-helpers/test-store.d.ts +69 -0
- package/dist/test-helpers/test-store.js +193 -0
- package/dist/text/normalizer.d.ts +20 -0
- package/dist/text/normalizer.js +102 -0
- package/dist/thread-message-rows.d.ts +69 -0
- package/dist/thread-message-rows.js +40 -0
- package/dist/thread-root.d.ts +7 -0
- package/dist/thread-root.js +25 -0
- package/dist/types.d.ts +343 -0
- package/dist/types.js +19 -0
- package/package.json +86 -88
- package/src/account-credentials.ts +0 -210
- package/src/attribute-mapper.test.ts +0 -104
- package/src/attribute-mapper.ts +0 -148
- package/src/body-parse.test.ts +0 -55
- package/src/body-part-mapper.property.test.ts +0 -354
- package/src/body-part-mapper.ts +0 -410
- package/src/body-sync-queue.test.ts +0 -64
- package/src/body-sync-queue.ts +0 -131
- package/src/body-sync-terminal.test.ts +0 -148
- package/src/body-sync-terminal.ts +0 -174
- package/src/body-sync.test.ts +0 -925
- package/src/body-sync.ts +0 -1891
- package/src/calendar-parts.ts +0 -41
- package/src/classification-backfill.test.ts +0 -285
- package/src/classification-backfill.ts +0 -363
- package/src/classify-message.test.ts +0 -142
- package/src/classify-message.ts +0 -116
- package/src/connection-factory.ts +0 -102
- package/src/connection-test.ts +0 -208
- package/src/credentials.test.ts +0 -181
- package/src/external-move.test.ts +0 -262
- package/src/external-move.ts +0 -285
- package/src/filter-operations.test.ts +0 -319
- package/src/filter-operations.ts +0 -377
- package/src/filter-update.test.ts +0 -313
- package/src/filters/filter-config.test.ts +0 -87
- package/src/filters/filter-config.ts +0 -34
- package/src/filters/list-id.test.ts +0 -50
- package/src/filters/match.test.ts +0 -392
- package/src/filters/match.ts +0 -225
- package/src/filters/pipeline.test.ts +0 -199
- package/src/filters/pipeline.ts +0 -170
- package/src/flag-push.test.ts +0 -112
- package/src/flag-push.ts +0 -198
- package/src/flag-queue.test.ts +0 -197
- package/src/flag-queue.ts +0 -509
- package/src/folder-operations.test.ts +0 -273
- package/src/folder-operations.ts +0 -217
- package/src/folder-placement.ts +0 -56
- package/src/forwarded-message.test.ts +0 -122
- package/src/heuristics/authenticationResults.test.ts +0 -189
- package/src/heuristics/authenticationResults.ts +0 -212
- package/src/heuristics/classifyByHeaders.test.ts +0 -889
- package/src/heuristics/classifyByHeaders.ts +0 -467
- package/src/heuristics/classifyPlacement.test.ts +0 -366
- package/src/heuristics/classifyPlacement.ts +0 -211
- package/src/heuristics/classifyRealMail.test.ts +0 -242
- package/src/heuristics/machineSenders.test.ts +0 -101
- package/src/heuristics/senderMismatch.test.ts +0 -346
- package/src/heuristics/senderMismatch.ts +0 -259
- package/src/imapflow-connect-retry.test.ts +0 -97
- package/src/imapflow-connection.test.ts +0 -503
- package/src/imapflow-connection.ts +0 -1476
- package/src/index.ts +0 -397
- package/src/list-id-backfill.test.ts +0 -207
- package/src/list-id-backfill.ts +0 -274
- package/src/mailbox-cursor-rebuild.test.ts +0 -171
- package/src/mailbox-cursor-rebuild.ts +0 -146
- package/src/mailbox-cursor.test.ts +0 -141
- package/src/mailbox-delete-intent.test.ts +0 -119
- package/src/mailbox-management.test.ts +0 -243
- package/src/mailbox-management.ts +0 -838
- package/src/mailbox-presence.test.ts +0 -74
- package/src/mailbox-queue.ts +0 -397
- package/src/mailbox-rename-intent.test.ts +0 -220
- package/src/mailbox-rename-subtree.test.ts +0 -287
- package/src/mailbox-sync.test.ts +0 -286
- package/src/mailbox-sync.ts +0 -724
- package/src/message-move.test.ts +0 -726
- package/src/message-move.ts +0 -1518
- package/src/message-parser.ts +0 -26
- package/src/message-presence.test.ts +0 -50
- package/src/message-read.ts +0 -87
- package/src/message-save-retry.test.ts +0 -90
- package/src/message-sync-flags.test.ts +0 -167
- package/src/message-sync.test.ts +0 -762
- package/src/message-sync.ts +0 -2133
- package/src/mime-walker.test.ts +0 -311
- package/src/mime-walker.ts +0 -306
- package/src/organize-job.ts +0 -93
- package/src/outbox-attachment-content.test.ts +0 -153
- package/src/outbox-attachment-content.ts +0 -104
- package/src/outbox-attachment-filename.test.ts +0 -132
- package/src/outbox-attachment-filename.ts +0 -76
- package/src/outbox-attachment.test.ts +0 -626
- package/src/outbox-attachment.ts +0 -536
- package/src/outbox-failed-recovery.test.ts +0 -299
- package/src/outbox-queue.test.ts +0 -315
- package/src/outbox-queue.ts +0 -525
- package/src/ownership.ts +0 -140
- package/src/placement-move-terminal.ts +0 -145
- package/src/placement-move.ts +0 -363
- package/src/placement-settled.test.ts +0 -147
- package/src/placement.test.ts +0 -404
- package/src/quarantine.test.ts +0 -133
- package/src/quarantine.ts +0 -229
- package/src/refusal.ts +0 -29
- package/src/snippet.test.ts +0 -213
- package/src/snippet.ts +0 -152
- package/src/spam-report.test.ts +0 -329
- package/src/spam-report.ts +0 -357
- package/src/sync-warning.test.ts +0 -213
- package/src/sync-warning.ts +0 -56
- package/src/sync-watermarks.test.ts +0 -409
- package/src/sync-watermarks.ts +0 -319
- package/src/system-flag-wire-format.test.ts +0 -77
- package/src/test-helpers/fake-imap.ts +0 -690
- package/src/test-helpers/folder-roles.ts +0 -66
- package/src/test-helpers/mime-tree-generator.ts +0 -461
- package/src/test-helpers/test-queue.ts +0 -56
- package/src/test-helpers/test-store.ts +0 -299
- package/src/text/normalizer.test.ts +0 -223
- package/src/text/normalizer.ts +0 -140
- package/src/thread-message-rows.ts +0 -77
- package/src/thread-root.test.ts +0 -29
- package/src/thread-root.ts +0 -51
- package/src/types/mailparser-augment.d.ts +0 -23
- package/src/types/natural-porter-stemmer.d.ts +0 -50
- package/src/types/stopword.d.ts +0 -69
- package/src/types.ts +0 -371
- package/tsconfig.json +0 -8
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MIME structure walker.
|
|
3
|
+
*
|
|
4
|
+
* Flattens an IMAP BODYSTRUCTURE tree (as produced by ImapFlow's
|
|
5
|
+
* `MessageStructureObject`) into a list of `BodyPartRecord`s ready to be
|
|
6
|
+
* persisted to the BodyPart / BodyPartParameter tables.
|
|
7
|
+
*
|
|
8
|
+
* Inputs are intentionally typed against a structural subset rather than
|
|
9
|
+
* pulling the imapflow types directly so the walker can be unit-tested
|
|
10
|
+
* without spinning up an IMAP connection.
|
|
11
|
+
*/
|
|
12
|
+
import { ROOT_PART_PATH } from "@remit/data-ports/id";
|
|
13
|
+
import { ContentDisposition, MediaType, MultipartSubtype, TransferEncoding } from "@remit/domain-enums";
|
|
14
|
+
export { ROOT_PART_PATH };
|
|
15
|
+
export type MediaTypeValue = (typeof MediaType)[keyof typeof MediaType];
|
|
16
|
+
export type TransferEncodingValue = (typeof TransferEncoding)[keyof typeof TransferEncoding];
|
|
17
|
+
export type ContentDispositionValue = (typeof ContentDisposition)[keyof typeof ContentDisposition];
|
|
18
|
+
export type MultipartSubtypeValue = (typeof MultipartSubtype)[keyof typeof MultipartSubtype];
|
|
19
|
+
/**
|
|
20
|
+
* Structural shape of an IMAP BODYSTRUCTURE node. Mirrors imapflow's
|
|
21
|
+
* `MessageStructureObject` minus the fields we don't read so this module
|
|
22
|
+
* stays free of an imapflow dependency.
|
|
23
|
+
*/
|
|
24
|
+
export interface MimeNode {
|
|
25
|
+
/**
|
|
26
|
+
* Dot-numbered MIME path (e.g., "1", "1.1", "2"). Empty/undefined for
|
|
27
|
+
* the root node — the walker assigns "0" to it for storage stability.
|
|
28
|
+
*/
|
|
29
|
+
part?: string;
|
|
30
|
+
/** Full Content-Type (e.g., "text/plain", "multipart/mixed"). */
|
|
31
|
+
type: string;
|
|
32
|
+
/** Content-Type parameters (charset, boundary, name, ...). */
|
|
33
|
+
parameters?: Record<string, string>;
|
|
34
|
+
/** Content-ID header value (without angle brackets). */
|
|
35
|
+
id?: string;
|
|
36
|
+
/** Content-Description header value. */
|
|
37
|
+
description?: string;
|
|
38
|
+
/** Transfer encoding (7bit, 8bit, base64, quoted-printable, binary). */
|
|
39
|
+
encoding?: string;
|
|
40
|
+
/** Encoded byte size of this part. */
|
|
41
|
+
size?: number;
|
|
42
|
+
/** Line count for text/* parts. */
|
|
43
|
+
lineCount?: number;
|
|
44
|
+
/** MD5 hash. */
|
|
45
|
+
md5?: string;
|
|
46
|
+
/** Content-Disposition (inline | attachment). */
|
|
47
|
+
disposition?: string;
|
|
48
|
+
/** Content-Disposition parameters (filename, ...). */
|
|
49
|
+
dispositionParameters?: Record<string, string>;
|
|
50
|
+
/** Content-Language values. */
|
|
51
|
+
language?: string[];
|
|
52
|
+
/** Content-Location URI. */
|
|
53
|
+
location?: string;
|
|
54
|
+
/** Children for multipart nodes. */
|
|
55
|
+
childNodes?: MimeNode[];
|
|
56
|
+
}
|
|
57
|
+
export interface BodyPartParameterRecord {
|
|
58
|
+
parameterName: string;
|
|
59
|
+
parameterValue: string;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Flattened MIME node ready to upsert. `parentPartPath` is the parent
|
|
63
|
+
* node's `partPath` (or null for the root) so the caller can map paths to
|
|
64
|
+
* the deterministic `bodyPartId`s without re-walking the tree.
|
|
65
|
+
*/
|
|
66
|
+
export interface BodyPartRecord {
|
|
67
|
+
partPath: string;
|
|
68
|
+
parentPartPath: string | null;
|
|
69
|
+
mediaType: MediaTypeValue;
|
|
70
|
+
mediaSubtype: string;
|
|
71
|
+
contentId?: string;
|
|
72
|
+
contentDescription?: string;
|
|
73
|
+
transferEncoding: TransferEncodingValue;
|
|
74
|
+
sizeOctets: number;
|
|
75
|
+
lineCount?: number;
|
|
76
|
+
md5Hash?: string;
|
|
77
|
+
disposition?: ContentDispositionValue;
|
|
78
|
+
dispositionFilename?: string;
|
|
79
|
+
language?: string;
|
|
80
|
+
location?: string;
|
|
81
|
+
isMultipart: boolean;
|
|
82
|
+
multipartSubtype?: MultipartSubtypeValue;
|
|
83
|
+
parameters: BodyPartParameterRecord[];
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Walk an IMAP BODYSTRUCTURE tree depth-first and return a flat list of
|
|
87
|
+
* `BodyPartRecord`s. The root node uses `ROOT_PART_PATH` ("0"); all other
|
|
88
|
+
* nodes use the dot-numbered IMAP path that ImapFlow assigns (e.g., "1",
|
|
89
|
+
* "1.2", "2.1.3"). Every field comes from an untrusted BODYSTRUCTURE, so an
|
|
90
|
+
* unrecognized MIME type, transfer encoding, or Content-Disposition maps to a
|
|
91
|
+
* safe default rather than throwing — one malformed part must never drop the
|
|
92
|
+
* whole message.
|
|
93
|
+
*
|
|
94
|
+
* **Part-path uniqueness**: imapflow gives the root of a message/rfc822
|
|
95
|
+
* node's encapsulated message the same `part` as the node itself, and some
|
|
96
|
+
* servers give it none. That root is stored as `<messagePath>.0` — the
|
|
97
|
+
* encapsulated message's own ROOT_PART_PATH — so it can neither overwrite the
|
|
98
|
+
* message/rfc822 row nor collide with its children (`<messagePath>.1`, ...).
|
|
99
|
+
* Any other non-root node without a `part` receives a synthetic path
|
|
100
|
+
* `<parentPath>.<siblingIndex>` that is stable across repeated syncs.
|
|
101
|
+
*/
|
|
102
|
+
export declare const walkMimeStructure: (root: MimeNode) => BodyPartRecord[];
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MIME structure walker.
|
|
3
|
+
*
|
|
4
|
+
* Flattens an IMAP BODYSTRUCTURE tree (as produced by ImapFlow's
|
|
5
|
+
* `MessageStructureObject`) into a list of `BodyPartRecord`s ready to be
|
|
6
|
+
* persisted to the BodyPart / BodyPartParameter tables.
|
|
7
|
+
*
|
|
8
|
+
* Inputs are intentionally typed against a structural subset rather than
|
|
9
|
+
* pulling the imapflow types directly so the walker can be unit-tested
|
|
10
|
+
* without spinning up an IMAP connection.
|
|
11
|
+
*/
|
|
12
|
+
import { ROOT_PART_PATH } from "@remit/data-ports/id";
|
|
13
|
+
import { ContentDisposition, MediaType, MultipartSubtype, TransferEncoding, } from "@remit/domain-enums";
|
|
14
|
+
export { ROOT_PART_PATH };
|
|
15
|
+
const MEDIA_TYPE_BY_TOP_LEVEL = {
|
|
16
|
+
text: MediaType.Text,
|
|
17
|
+
image: MediaType.Image,
|
|
18
|
+
audio: MediaType.Audio,
|
|
19
|
+
video: MediaType.Video,
|
|
20
|
+
application: MediaType.Application,
|
|
21
|
+
multipart: MediaType.Multipart,
|
|
22
|
+
message: MediaType.Message,
|
|
23
|
+
};
|
|
24
|
+
const MULTIPART_SUBTYPE_BY_VALUE = {
|
|
25
|
+
mixed: MultipartSubtype.Mixed,
|
|
26
|
+
alternative: MultipartSubtype.Alternative,
|
|
27
|
+
digest: MultipartSubtype.Digest,
|
|
28
|
+
parallel: MultipartSubtype.Parallel,
|
|
29
|
+
related: MultipartSubtype.Related,
|
|
30
|
+
signed: MultipartSubtype.Signed,
|
|
31
|
+
encrypted: MultipartSubtype.Encrypted,
|
|
32
|
+
"form-data": MultipartSubtype.FormData,
|
|
33
|
+
report: MultipartSubtype.Report,
|
|
34
|
+
};
|
|
35
|
+
const TRANSFER_ENCODING_BY_VALUE = {
|
|
36
|
+
"7bit": TransferEncoding.SevenBit,
|
|
37
|
+
"8bit": TransferEncoding.EightBit,
|
|
38
|
+
binary: TransferEncoding.Binary,
|
|
39
|
+
base64: TransferEncoding.Base64,
|
|
40
|
+
"quoted-printable": TransferEncoding.QuotedPrintable,
|
|
41
|
+
};
|
|
42
|
+
const DISPOSITION_BY_VALUE = {
|
|
43
|
+
inline: ContentDisposition.Inline,
|
|
44
|
+
attachment: ContentDisposition.Attachment,
|
|
45
|
+
};
|
|
46
|
+
const FALLBACK_MEDIA_SUBTYPE = "octet-stream";
|
|
47
|
+
/**
|
|
48
|
+
* Split a Content-Type into a known media type and its subtype, tolerating
|
|
49
|
+
* malformed values. The Content-Type is copied from an untrusted header, so a
|
|
50
|
+
* missing "/", an unknown top-level type, or an empty subtype must not throw
|
|
51
|
+
* (that would drop the whole message). RFC 2046 §4.5.3 makes
|
|
52
|
+
* `application/octet-stream` the defined default for an unrecognized type, so
|
|
53
|
+
* that is the fallback; the raw subtype is preserved where present.
|
|
54
|
+
*/
|
|
55
|
+
const splitContentType = (type) => {
|
|
56
|
+
const slashIndex = type.indexOf("/");
|
|
57
|
+
if (slashIndex < 0) {
|
|
58
|
+
return {
|
|
59
|
+
mediaType: MediaType.Application,
|
|
60
|
+
mediaSubtype: FALLBACK_MEDIA_SUBTYPE,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
const topRaw = type.slice(0, slashIndex);
|
|
64
|
+
const subRaw = type.slice(slashIndex + 1);
|
|
65
|
+
const top = topRaw.toLowerCase();
|
|
66
|
+
const mediaType = MEDIA_TYPE_BY_TOP_LEVEL[top] ?? MediaType.Application;
|
|
67
|
+
const subLower = subRaw.toLowerCase();
|
|
68
|
+
const mediaSubtype = subLower.length > 0 ? subLower : FALLBACK_MEDIA_SUBTYPE;
|
|
69
|
+
return { mediaType, mediaSubtype };
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* An absent Content-Transfer-Encoding means 7bit (RFC 2045 §6.1); an
|
|
73
|
+
* unrecognized token is treated as opaque bytes rather than throwing, so a
|
|
74
|
+
* message carrying an exotic encoding still syncs.
|
|
75
|
+
*/
|
|
76
|
+
const mapTransferEncoding = (encoding) => {
|
|
77
|
+
if (!encoding)
|
|
78
|
+
return TransferEncoding.SevenBit;
|
|
79
|
+
return (TRANSFER_ENCODING_BY_VALUE[encoding.toLowerCase()] ??
|
|
80
|
+
TransferEncoding.Binary);
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* An unknown Content-Disposition is treated as absent (undefined) rather than
|
|
84
|
+
* throwing, so a message with a malformed disposition still syncs.
|
|
85
|
+
*/
|
|
86
|
+
const mapDisposition = (disposition) => {
|
|
87
|
+
if (!disposition)
|
|
88
|
+
return undefined;
|
|
89
|
+
return DISPOSITION_BY_VALUE[disposition.toLowerCase()];
|
|
90
|
+
};
|
|
91
|
+
const mapMultipartSubtype = (subtype) => {
|
|
92
|
+
const mapped = MULTIPART_SUBTYPE_BY_VALUE[subtype.toLowerCase()];
|
|
93
|
+
if (!mapped)
|
|
94
|
+
return undefined;
|
|
95
|
+
return mapped;
|
|
96
|
+
};
|
|
97
|
+
const stripAngles = (value) => value.replace(/^<+|>+$/g, "");
|
|
98
|
+
const flattenParameters = (params) => {
|
|
99
|
+
if (!params)
|
|
100
|
+
return [];
|
|
101
|
+
const entries = Object.entries(params).filter(([name, value]) => {
|
|
102
|
+
if (typeof name !== "string" || name.length === 0)
|
|
103
|
+
return false;
|
|
104
|
+
if (typeof value !== "string")
|
|
105
|
+
return false;
|
|
106
|
+
return true;
|
|
107
|
+
});
|
|
108
|
+
entries.sort((a, b) => a[0].localeCompare(b[0]));
|
|
109
|
+
return entries.map(([parameterName, parameterValue]) => ({
|
|
110
|
+
parameterName,
|
|
111
|
+
parameterValue,
|
|
112
|
+
}));
|
|
113
|
+
};
|
|
114
|
+
const toRecord = (node, partPath, parentPartPath) => {
|
|
115
|
+
const { mediaType, mediaSubtype } = splitContentType(node.type);
|
|
116
|
+
const isMultipart = mediaType === MediaType.Multipart;
|
|
117
|
+
const multipartSubtype = isMultipart
|
|
118
|
+
? mapMultipartSubtype(mediaSubtype)
|
|
119
|
+
: undefined;
|
|
120
|
+
const dispositionFilename = node.dispositionParameters?.filename ?? node.parameters?.name ?? undefined;
|
|
121
|
+
const language = node.language?.[0];
|
|
122
|
+
const record = {
|
|
123
|
+
partPath,
|
|
124
|
+
parentPartPath,
|
|
125
|
+
mediaType,
|
|
126
|
+
mediaSubtype,
|
|
127
|
+
transferEncoding: mapTransferEncoding(node.encoding),
|
|
128
|
+
sizeOctets: typeof node.size === "number" ? node.size : 0,
|
|
129
|
+
isMultipart,
|
|
130
|
+
parameters: flattenParameters(node.parameters),
|
|
131
|
+
};
|
|
132
|
+
if (node.id)
|
|
133
|
+
record.contentId = stripAngles(node.id);
|
|
134
|
+
if (node.description)
|
|
135
|
+
record.contentDescription = node.description;
|
|
136
|
+
if (typeof node.lineCount === "number")
|
|
137
|
+
record.lineCount = node.lineCount;
|
|
138
|
+
if (node.md5)
|
|
139
|
+
record.md5Hash = node.md5;
|
|
140
|
+
const mappedDisposition = mapDisposition(node.disposition);
|
|
141
|
+
if (mappedDisposition)
|
|
142
|
+
record.disposition = mappedDisposition;
|
|
143
|
+
if (dispositionFilename)
|
|
144
|
+
record.dispositionFilename = dispositionFilename;
|
|
145
|
+
if (language)
|
|
146
|
+
record.language = language;
|
|
147
|
+
if (node.location)
|
|
148
|
+
record.location = node.location;
|
|
149
|
+
if (multipartSubtype)
|
|
150
|
+
record.multipartSubtype = multipartSubtype;
|
|
151
|
+
return record;
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* Walk an IMAP BODYSTRUCTURE tree depth-first and return a flat list of
|
|
155
|
+
* `BodyPartRecord`s. The root node uses `ROOT_PART_PATH` ("0"); all other
|
|
156
|
+
* nodes use the dot-numbered IMAP path that ImapFlow assigns (e.g., "1",
|
|
157
|
+
* "1.2", "2.1.3"). Every field comes from an untrusted BODYSTRUCTURE, so an
|
|
158
|
+
* unrecognized MIME type, transfer encoding, or Content-Disposition maps to a
|
|
159
|
+
* safe default rather than throwing — one malformed part must never drop the
|
|
160
|
+
* whole message.
|
|
161
|
+
*
|
|
162
|
+
* **Part-path uniqueness**: imapflow gives the root of a message/rfc822
|
|
163
|
+
* node's encapsulated message the same `part` as the node itself, and some
|
|
164
|
+
* servers give it none. That root is stored as `<messagePath>.0` — the
|
|
165
|
+
* encapsulated message's own ROOT_PART_PATH — so it can neither overwrite the
|
|
166
|
+
* message/rfc822 row nor collide with its children (`<messagePath>.1`, ...).
|
|
167
|
+
* Any other non-root node without a `part` receives a synthetic path
|
|
168
|
+
* `<parentPath>.<siblingIndex>` that is stable across repeated syncs.
|
|
169
|
+
*/
|
|
170
|
+
export const walkMimeStructure = (root) => {
|
|
171
|
+
const out = [];
|
|
172
|
+
const visit = (node, parentPath, parentIsMessage, siblingIndex) => {
|
|
173
|
+
let partPath;
|
|
174
|
+
if (parentIsMessage && parentPath !== null) {
|
|
175
|
+
partPath = `${parentPath}.${ROOT_PART_PATH}`;
|
|
176
|
+
}
|
|
177
|
+
else if (node.part && node.part.length > 0) {
|
|
178
|
+
partPath = node.part;
|
|
179
|
+
}
|
|
180
|
+
else if (parentPath === null) {
|
|
181
|
+
// True root of the BODYSTRUCTURE tree.
|
|
182
|
+
partPath = ROOT_PART_PATH;
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
partPath = `${parentPath}.${siblingIndex}`;
|
|
186
|
+
}
|
|
187
|
+
const record = toRecord(node, partPath, parentPath);
|
|
188
|
+
out.push(record);
|
|
189
|
+
const isMessage = record.mediaType === MediaType.Message;
|
|
190
|
+
const children = node.childNodes ?? [];
|
|
191
|
+
for (let i = 0; i < children.length; i++) {
|
|
192
|
+
// biome-ignore lint/style/noNonNullAssertion: value is guaranteed by caller contract
|
|
193
|
+
visit(children[i], partPath, isMessage, i + 1);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
visit(root, null, false, 0);
|
|
197
|
+
return out;
|
|
198
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { CreateOrganizeJobRequestInput, IOrganizeJobRequestRepository, OrganizeJobEvent, OrganizeJobRequestItem, QueueProducer } from "@remit/data-ports";
|
|
2
|
+
export interface OrganizeJobOwner {
|
|
3
|
+
accountConfigId: string;
|
|
4
|
+
userId: string;
|
|
5
|
+
}
|
|
6
|
+
export type OrganizeJobPredicate = Pick<CreateOrganizeJobRequestInput, "matchOperator" | "literalClauses" | "actionLabelId" | "actionMailboxId">;
|
|
7
|
+
export type OrganizeJobRequest = OrganizeJobOwner & ({
|
|
8
|
+
predicate: OrganizeJobPredicate;
|
|
9
|
+
} | {
|
|
10
|
+
filterId: string;
|
|
11
|
+
});
|
|
12
|
+
export interface OrganizeJobLogger {
|
|
13
|
+
info(obj: Record<string, unknown>, msg: string): void;
|
|
14
|
+
}
|
|
15
|
+
export interface OrganizeJobDeps {
|
|
16
|
+
organizeJobRequest: Pick<IOrganizeJobRequestRepository, "create">;
|
|
17
|
+
queue: QueueProducer<OrganizeJobEvent>;
|
|
18
|
+
logger: OrganizeJobLogger;
|
|
19
|
+
}
|
|
20
|
+
export declare const queueOrganizeJob: (deps: OrganizeJobDeps, request: OrganizeJobRequest) => Promise<OrganizeJobRequestItem>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { FilterMatchOperator } from "@remit/domain-enums";
|
|
2
|
+
import { NO_ACTION } from "./filters/match.js";
|
|
3
|
+
const ORGANIZE_JOB_TTL_SECONDS = 7 * 24 * 60 * 60;
|
|
4
|
+
const jobColumns = (request) => {
|
|
5
|
+
if ("filterId" in request) {
|
|
6
|
+
return {
|
|
7
|
+
filterId: request.filterId,
|
|
8
|
+
matchOperator: FilterMatchOperator.And,
|
|
9
|
+
literalClauses: [],
|
|
10
|
+
actionLabelId: NO_ACTION,
|
|
11
|
+
actionMailboxId: NO_ACTION,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
return { filterId: NO_ACTION, ...request.predicate };
|
|
15
|
+
};
|
|
16
|
+
// A filter job names only the filter; the worker reads it when it runs.
|
|
17
|
+
export const queueOrganizeJob = async (deps, request) => {
|
|
18
|
+
const job = await deps.organizeJobRequest.create({
|
|
19
|
+
accountConfigId: request.accountConfigId,
|
|
20
|
+
userId: request.userId,
|
|
21
|
+
...jobColumns(request),
|
|
22
|
+
ttl: Math.floor(Date.now() / 1000) + ORGANIZE_JOB_TTL_SECONDS,
|
|
23
|
+
});
|
|
24
|
+
await deps.queue.send({
|
|
25
|
+
body: {
|
|
26
|
+
type: "OrganizeJob",
|
|
27
|
+
accountConfigId: job.accountConfigId,
|
|
28
|
+
organizeJobId: job.organizeJobId,
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
deps.logger.info({
|
|
32
|
+
accountConfigId: job.accountConfigId,
|
|
33
|
+
organizeJobId: job.organizeJobId,
|
|
34
|
+
filterId: job.filterId,
|
|
35
|
+
}, "Organize back-apply job initiated");
|
|
36
|
+
return job;
|
|
37
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { IOutboxAttachmentRepository } from "@remit/data-ports";
|
|
2
|
+
import type { StorageService } from "@remit/storage-service";
|
|
3
|
+
export interface OutboxAttachmentContent {
|
|
4
|
+
filename: string;
|
|
5
|
+
contentType: string;
|
|
6
|
+
content: Buffer;
|
|
7
|
+
cid: string;
|
|
8
|
+
}
|
|
9
|
+
export declare const outboxAttachmentContentId: (outboxAttachmentId: string) => string;
|
|
10
|
+
/**
|
|
11
|
+
* A file on a message that cannot be put into it. Carries the file's name so
|
|
12
|
+
* whoever settles the message can tell the sender which file to fix.
|
|
13
|
+
*/
|
|
14
|
+
export declare class OutboxAttachmentUnavailableError extends Error {
|
|
15
|
+
readonly filename: string;
|
|
16
|
+
readonly detail: string;
|
|
17
|
+
constructor(filename: string, detail: string, options?: {
|
|
18
|
+
cause: unknown;
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
export interface OutboxAttachmentContentDeps {
|
|
22
|
+
attachments: Pick<IOutboxAttachmentRepository, "listByOutboxMessage">;
|
|
23
|
+
storage: Pick<StorageService, "retrieveOutboxAttachment">;
|
|
24
|
+
}
|
|
25
|
+
export interface OutboxAttachmentOwner {
|
|
26
|
+
accountConfigId: string;
|
|
27
|
+
accountId: string;
|
|
28
|
+
outboxMessageId: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Every file a message goes out with, bytes included, in the order they were
|
|
32
|
+
* attached. The send op refuses a draft with an upload still open, so a live
|
|
33
|
+
* `Pending` row here, or a `Stored` row with nothing behind it, is a message
|
|
34
|
+
* that cannot be built as the sender saw it — and sending it without the file
|
|
35
|
+
* would be worse than not sending it.
|
|
36
|
+
*/
|
|
37
|
+
export declare const loadOutboxAttachmentContents: (deps: OutboxAttachmentContentDeps, owner: OutboxAttachmentOwner, nowSeconds: number) => Promise<OutboxAttachmentContent[]>;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { holdsRoom } from "@remit/data-ports";
|
|
2
|
+
export const outboxAttachmentContentId = (outboxAttachmentId) => `${outboxAttachmentId}@remit`;
|
|
3
|
+
/**
|
|
4
|
+
* A file on a message that cannot be put into it. Carries the file's name so
|
|
5
|
+
* whoever settles the message can tell the sender which file to fix.
|
|
6
|
+
*/
|
|
7
|
+
export class OutboxAttachmentUnavailableError extends Error {
|
|
8
|
+
filename;
|
|
9
|
+
detail;
|
|
10
|
+
constructor(filename, detail, options) {
|
|
11
|
+
super(`"${filename}" could not be read: ${detail}`, options);
|
|
12
|
+
this.filename = filename;
|
|
13
|
+
this.detail = detail;
|
|
14
|
+
this.name = "OutboxAttachmentUnavailableError";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const describeCause = (error) => error instanceof Error ? error.message : String(error);
|
|
18
|
+
/**
|
|
19
|
+
* Every file a message goes out with, bytes included, in the order they were
|
|
20
|
+
* attached. The send op refuses a draft with an upload still open, so a live
|
|
21
|
+
* `Pending` row here, or a `Stored` row with nothing behind it, is a message
|
|
22
|
+
* that cannot be built as the sender saw it — and sending it without the file
|
|
23
|
+
* would be worse than not sending it.
|
|
24
|
+
*/
|
|
25
|
+
export const loadOutboxAttachmentContents = async (deps, owner, nowSeconds) => {
|
|
26
|
+
const held = await deps.attachments.listByOutboxMessage(owner.accountConfigId, owner.outboxMessageId);
|
|
27
|
+
const live = held
|
|
28
|
+
.filter((item) => holdsRoom(item, nowSeconds))
|
|
29
|
+
.sort((a, b) => a.createdAt - b.createdAt);
|
|
30
|
+
const unfinished = live.find((item) => item.state !== "Stored");
|
|
31
|
+
if (unfinished) {
|
|
32
|
+
throw new OutboxAttachmentUnavailableError(unfinished.filename, `attachment ${unfinished.outboxAttachmentId} never finished uploading`);
|
|
33
|
+
}
|
|
34
|
+
return Promise.all(live.map(async (item) => {
|
|
35
|
+
const content = await Promise.resolve()
|
|
36
|
+
.then(() => deps.storage.retrieveOutboxAttachment(owner.accountConfigId, owner.accountId, owner.outboxMessageId, item.outboxAttachmentId))
|
|
37
|
+
.catch((error) => {
|
|
38
|
+
throw new OutboxAttachmentUnavailableError(item.filename, `storage failed (${describeCause(error)})`, { cause: error });
|
|
39
|
+
});
|
|
40
|
+
if (content === null) {
|
|
41
|
+
throw new OutboxAttachmentUnavailableError(item.filename, `nothing is stored at ${item.storageKey}`);
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
filename: item.filename,
|
|
45
|
+
contentType: item.contentType,
|
|
46
|
+
content,
|
|
47
|
+
cid: outboxAttachmentContentId(item.outboxAttachmentId),
|
|
48
|
+
};
|
|
49
|
+
}));
|
|
50
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A filename arrives from a browser's `File.name` and reaches a
|
|
3
|
+
* `Content-Disposition` header on a message a stranger opens. Nothing in the
|
|
4
|
+
* middle re-derives it, and the storage key never contains it — the key is a
|
|
5
|
+
* generated id — so this is the one place the value is made safe.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* The filename to record, or null when nothing usable survives — a name made
|
|
9
|
+
* only of separators, dots or control characters is refused rather than
|
|
10
|
+
* replaced with something invented.
|
|
11
|
+
*/
|
|
12
|
+
export declare const sanitizeAttachmentFilename: (raw: string) => string | null;
|
|
13
|
+
export declare const FALLBACK_CONTENT_TYPE = "application/octet-stream";
|
|
14
|
+
/**
|
|
15
|
+
* The media type to record. Parameters (`; charset=…`) are dropped and anything
|
|
16
|
+
* that is not a bare type/subtype becomes the fallback: the value is recorded
|
|
17
|
+
* for the outgoing MIME part, never trusted for rendering.
|
|
18
|
+
*/
|
|
19
|
+
export declare const normalizeAttachmentContentType: (raw: string) => string;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A filename arrives from a browser's `File.name` and reaches a
|
|
3
|
+
* `Content-Disposition` header on a message a stranger opens. Nothing in the
|
|
4
|
+
* middle re-derives it, and the storage key never contains it — the key is a
|
|
5
|
+
* generated id — so this is the one place the value is made safe.
|
|
6
|
+
*/
|
|
7
|
+
const PATH_SEPARATORS = /^.*[\\/]/;
|
|
8
|
+
// C0/C1 controls and DEL, then the bidi and directional-isolate formatting
|
|
9
|
+
// characters that let a name ending in "exe" render as if it ended in "png".
|
|
10
|
+
const UNSAFE_CHARACTERS =
|
|
11
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: stripping control characters is the point
|
|
12
|
+
/[\u0000-\u001F\u007F-\u009F\u200E\u200F\u202A-\u202E\u2066-\u2069]/g;
|
|
13
|
+
const MAX_FILENAME_LENGTH = 200;
|
|
14
|
+
// By code point, never by UTF-16 unit: cutting mid-surrogate leaves a lone half
|
|
15
|
+
// that a Content-Disposition encoder can only render as U+FFFD.
|
|
16
|
+
const truncateCodePoints = (value, limit) => Array.from(value).slice(0, limit).join("");
|
|
17
|
+
const truncateKeepingExtension = (filename) => {
|
|
18
|
+
const codePoints = Array.from(filename);
|
|
19
|
+
if (codePoints.length <= MAX_FILENAME_LENGTH)
|
|
20
|
+
return filename;
|
|
21
|
+
const dot = filename.lastIndexOf(".");
|
|
22
|
+
const extension = dot > 0 ? Array.from(filename.slice(dot)) : [];
|
|
23
|
+
if (extension.length === 0 || extension.length > 16) {
|
|
24
|
+
return truncateCodePoints(filename, MAX_FILENAME_LENGTH);
|
|
25
|
+
}
|
|
26
|
+
return (truncateCodePoints(filename, MAX_FILENAME_LENGTH - extension.length) +
|
|
27
|
+
extension.join(""));
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* The filename to record, or null when nothing usable survives — a name made
|
|
31
|
+
* only of separators, dots or control characters is refused rather than
|
|
32
|
+
* replaced with something invented.
|
|
33
|
+
*/
|
|
34
|
+
export const sanitizeAttachmentFilename = (raw) => {
|
|
35
|
+
// Strip first, then take the basename. The other order is bypassable: `.`
|
|
36
|
+
// does not match a newline, so `PATH_SEPARATORS` stops at one and leaves
|
|
37
|
+
// every separator after it — "report\r\n/../../.bashrc" survives with its
|
|
38
|
+
// slashes intact.
|
|
39
|
+
const stripped = raw.replace(UNSAFE_CHARACTERS, "");
|
|
40
|
+
const basename = stripped.replace(PATH_SEPARATORS, "").trim();
|
|
41
|
+
const trimmed = basename.replace(/^\.+/, "").trim();
|
|
42
|
+
if (trimmed.length === 0)
|
|
43
|
+
return null;
|
|
44
|
+
return truncateKeepingExtension(trimmed);
|
|
45
|
+
};
|
|
46
|
+
// RFC 9110 token characters, on both sides of the one slash.
|
|
47
|
+
const MEDIA_TYPE = /^[A-Za-z0-9!#$%&'*+.^_`|~-]+\/[A-Za-z0-9!#$%&'*+.^_`|~-]+$/;
|
|
48
|
+
export const FALLBACK_CONTENT_TYPE = "application/octet-stream";
|
|
49
|
+
// The longest registered media type is well under this. A token run past it is
|
|
50
|
+
// a client inventing one, and it would overflow the response field it lands in.
|
|
51
|
+
const MAX_CONTENT_TYPE_LENGTH = 127;
|
|
52
|
+
/**
|
|
53
|
+
* The media type to record. Parameters (`; charset=…`) are dropped and anything
|
|
54
|
+
* that is not a bare type/subtype becomes the fallback: the value is recorded
|
|
55
|
+
* for the outgoing MIME part, never trusted for rendering.
|
|
56
|
+
*/
|
|
57
|
+
export const normalizeAttachmentContentType = (raw) => {
|
|
58
|
+
const bare = raw.split(";")[0].trim().toLowerCase();
|
|
59
|
+
if (bare.length > MAX_CONTENT_TYPE_LENGTH)
|
|
60
|
+
return FALLBACK_CONTENT_TYPE;
|
|
61
|
+
if (!MEDIA_TYPE.test(bare))
|
|
62
|
+
return FALLBACK_CONTENT_TYPE;
|
|
63
|
+
return bare;
|
|
64
|
+
};
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type { IOutboxAttachmentRepository, IOutboxMessageRepository, OutboxAttachmentItem } from "@remit/data-ports";
|
|
2
|
+
import { OutboxAttachmentRejectionReason } from "@remit/domain-enums";
|
|
3
|
+
import type { StorageService } from "@remit/storage-service";
|
|
4
|
+
import { type OutboxAttachmentContent } from "./outbox-attachment-content.js";
|
|
5
|
+
/**
|
|
6
|
+
* 25 MB is what most receiving servers accept, and base64 inflates it to roughly
|
|
7
|
+
* 34 MB on the wire. The same number caps one file and the sum of a draft's
|
|
8
|
+
* files, so a single file can fill a message.
|
|
9
|
+
*/
|
|
10
|
+
export declare const OUTBOX_ATTACHMENT_MAX_TOTAL_BYTES: number;
|
|
11
|
+
/**
|
|
12
|
+
* A ceiling on how many files one draft can accumulate. The byte cap does not
|
|
13
|
+
* bound the count — a thousand one-byte files stay far under it.
|
|
14
|
+
*/
|
|
15
|
+
export declare const OUTBOX_ATTACHMENT_MAX_COUNT = 25;
|
|
16
|
+
export type OutboxAttachmentRejectionReasonValue = (typeof OutboxAttachmentRejectionReason)[keyof typeof OutboxAttachmentRejectionReason];
|
|
17
|
+
export interface OutboxAttachmentReservation {
|
|
18
|
+
outboxAttachmentId: string;
|
|
19
|
+
outboxMessageId: string;
|
|
20
|
+
filename: string;
|
|
21
|
+
contentType: string;
|
|
22
|
+
sizeBytes: number;
|
|
23
|
+
contentId: string;
|
|
24
|
+
uploadUrl: string;
|
|
25
|
+
uploadExpiresAt: number;
|
|
26
|
+
}
|
|
27
|
+
export interface OutboxAttachmentRejectionDetail {
|
|
28
|
+
reason: OutboxAttachmentRejectionReasonValue;
|
|
29
|
+
message: string;
|
|
30
|
+
limitBytes: number;
|
|
31
|
+
usedBytes: number;
|
|
32
|
+
}
|
|
33
|
+
type Refused = {
|
|
34
|
+
readonly outcome: "Rejected";
|
|
35
|
+
readonly rejection: OutboxAttachmentRejectionDetail;
|
|
36
|
+
};
|
|
37
|
+
export type MintOutboxAttachmentOutcome = {
|
|
38
|
+
readonly outcome: "Minted";
|
|
39
|
+
readonly reservation: OutboxAttachmentReservation;
|
|
40
|
+
} | Refused;
|
|
41
|
+
export type CompleteOutboxAttachmentOutcome = {
|
|
42
|
+
readonly outcome: "Completed";
|
|
43
|
+
readonly attachment: OutboxAttachmentItem;
|
|
44
|
+
} | Refused;
|
|
45
|
+
export interface MintOutboxAttachmentInput {
|
|
46
|
+
accountConfigId: string;
|
|
47
|
+
outboxMessageId: string;
|
|
48
|
+
filename: string;
|
|
49
|
+
contentType: string;
|
|
50
|
+
sizeBytes: number;
|
|
51
|
+
}
|
|
52
|
+
export interface CompleteOutboxAttachmentInput {
|
|
53
|
+
accountConfigId: string;
|
|
54
|
+
outboxMessageId: string;
|
|
55
|
+
outboxAttachmentId: string;
|
|
56
|
+
}
|
|
57
|
+
export interface OutboxAttachmentConfig {
|
|
58
|
+
outboxMessageService: IOutboxMessageRepository;
|
|
59
|
+
outboxAttachmentService: IOutboxAttachmentRepository;
|
|
60
|
+
storage: StorageService;
|
|
61
|
+
now?: () => number;
|
|
62
|
+
}
|
|
63
|
+
export declare class OutboxAttachmentService {
|
|
64
|
+
private readonly outboxMessageService;
|
|
65
|
+
private readonly attachments;
|
|
66
|
+
private readonly storage;
|
|
67
|
+
private readonly now;
|
|
68
|
+
constructor(config: OutboxAttachmentConfig);
|
|
69
|
+
/**
|
|
70
|
+
* Resolve a draft the caller is entitled to act on.
|
|
71
|
+
*
|
|
72
|
+
* Mode "act": the caller has named the draft, so a foreign one is denied with
|
|
73
|
+
* 403 rather than feigned as a 404. An entry the user can no longer work on
|
|
74
|
+
* is a conflict. Both abort the request — only the file itself comes back as
|
|
75
|
+
* a result the composer can render next to the row it refused.
|
|
76
|
+
*
|
|
77
|
+
* Same predicate the composer's other two writes use. A `failed` message is
|
|
78
|
+
* editable and sendable again (#933), and an attachment is part of what the
|
|
79
|
+
* correction may be — a message refused for the wrong file would otherwise
|
|
80
|
+
* still be stuck.
|
|
81
|
+
*/
|
|
82
|
+
private getWritableDraft;
|
|
83
|
+
private usedBytesOn;
|
|
84
|
+
private reject;
|
|
85
|
+
/**
|
|
86
|
+
* Room on a draft for one file, and somewhere to put it.
|
|
87
|
+
*
|
|
88
|
+
* The row is written before the URL is handed out, and writing it is what
|
|
89
|
+
* claims the room — the count and the insert are one database transaction, so
|
|
90
|
+
* two requests arriving together cannot both be told there was space. That is
|
|
91
|
+
* the whole of the cap; nothing above this layer needs to serialize anything.
|
|
92
|
+
*/
|
|
93
|
+
mint: (input: MintOutboxAttachmentInput) => Promise<MintOutboxAttachmentOutcome>;
|
|
94
|
+
/**
|
|
95
|
+
* Turn an upload into an attachment, on storage's word rather than the
|
|
96
|
+
* client's.
|
|
97
|
+
*
|
|
98
|
+
* On a hosted deployment the bytes went straight to block storage and this is
|
|
99
|
+
* the first the API hears of them, so the size is read back before the row is
|
|
100
|
+
* moved to Stored. An object that is absent, or not the size reserved for it,
|
|
101
|
+
* does not become an attachment — phase 4 sends these bytes, and an
|
|
102
|
+
* attachment recorded over nothing is a message that cannot be built.
|
|
103
|
+
*/
|
|
104
|
+
complete: (input: CompleteOutboxAttachmentInput) => Promise<CompleteOutboxAttachmentOutcome>;
|
|
105
|
+
/**
|
|
106
|
+
* Whether an attachment is still owed its bytes. The self-hosted upload route
|
|
107
|
+
* asks before it writes, so a URL minted before a discard cannot put bytes
|
|
108
|
+
* back under a draft that is gone.
|
|
109
|
+
*/
|
|
110
|
+
hasLiveReservation: (accountConfigId: string, outboxAttachmentId: string) => Promise<boolean>;
|
|
111
|
+
/**
|
|
112
|
+
* What a draft holds, as the composer should see it: reservations that have
|
|
113
|
+
* lapsed are not files, and showing one with no way to tell it apart is worse
|
|
114
|
+
* than not showing it.
|
|
115
|
+
*/
|
|
116
|
+
listFor: (accountConfigId: string, outboxMessageId: string) => Promise<OutboxAttachmentItem[]>;
|
|
117
|
+
/**
|
|
118
|
+
* The files a message goes out with, bytes included. The Sent copy is built
|
|
119
|
+
* from the same rows the wire copy was, so the two carry the same parts.
|
|
120
|
+
*/
|
|
121
|
+
contentsFor: (accountConfigId: string, accountId: string, outboxMessageId: string) => Promise<OutboxAttachmentContent[]>;
|
|
122
|
+
/**
|
|
123
|
+
* The file that stops a send, if any: one still uploading, or one whose
|
|
124
|
+
* upload never finished. A message must not leave without a file its sender
|
|
125
|
+
* can see in the composer.
|
|
126
|
+
*/
|
|
127
|
+
unfinishedUpload: (accountConfigId: string, outboxMessageId: string) => Promise<OutboxAttachmentItem | undefined>;
|
|
128
|
+
/**
|
|
129
|
+
* Drop the draft's lapsed reservations and answer with the ids that are still
|
|
130
|
+
* good. The sweep runs this before it decides what to collect: a lapsed row
|
|
131
|
+
* would otherwise keep vouching for bytes nothing will ever send, and the
|
|
132
|
+
* object would never be collected while the row that names it survives.
|
|
133
|
+
*/
|
|
134
|
+
reapAndListLive: (accountConfigId: string, outboxMessageId: string) => Promise<string[]>;
|
|
135
|
+
/**
|
|
136
|
+
* Keep only the named attachments, deleting the rest with their bytes. This
|
|
137
|
+
* is how a composer removes a file: the update states what the draft keeps.
|
|
138
|
+
*/
|
|
139
|
+
retainOnly: (accountConfigId: string, accountId: string, outboxMessageId: string, keepIds: readonly string[]) => Promise<void>;
|
|
140
|
+
/**
|
|
141
|
+
* Drop every file a draft holds, rows and bytes, as the draft is retired —
|
|
142
|
+
* a discard, or the APPEND to Sent that removes the row once the message has
|
|
143
|
+
* left. Rows go last: while they exist the objects are accounted for, and a
|
|
144
|
+
* failure part-way leaves the sweep able to finish the job.
|
|
145
|
+
*/
|
|
146
|
+
discardAll: (accountConfigId: string, accountId: string, outboxMessageId: string) => Promise<void>;
|
|
147
|
+
}
|
|
148
|
+
export {};
|