@open-mercato/channel-imap 0.6.4
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/.turbo/turbo-build.log +2 -0
- package/AGENTS.md +56 -0
- package/build.mjs +7 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +7 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-001.spec.js +62 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-001.spec.js.map +7 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-002.spec.js +19 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-002.spec.js.map +7 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-003.spec.js +16 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-003.spec.js.map +7 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-021.spec.js +26 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-021.spec.js.map +7 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-022.spec.js +27 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-022.spec.js.map +7 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-023.spec.js +15 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-023.spec.js.map +7 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-024.spec.js +15 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-024.spec.js.map +7 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-025.spec.js +6 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-025.spec.js.map +7 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-026.spec.js +6 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-026.spec.js.map +7 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-027.spec.js +6 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-027.spec.js.map +7 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-028.spec.js +6 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-028.spec.js.map +7 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-029.spec.js +48 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-029.spec.js.map +7 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-030.spec.js +6 -0
- package/dist/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-030.spec.js.map +7 -0
- package/dist/modules/channel_imap/acl.js +10 -0
- package/dist/modules/channel_imap/acl.js.map +7 -0
- package/dist/modules/channel_imap/di.js +23 -0
- package/dist/modules/channel_imap/di.js.map +7 -0
- package/dist/modules/channel_imap/index.js +9 -0
- package/dist/modules/channel_imap/index.js.map +7 -0
- package/dist/modules/channel_imap/integration.js +135 -0
- package/dist/modules/channel_imap/integration.js.map +7 -0
- package/dist/modules/channel_imap/lib/adapter.js +291 -0
- package/dist/modules/channel_imap/lib/adapter.js.map +7 -0
- package/dist/modules/channel_imap/lib/capabilities.js +8 -0
- package/dist/modules/channel_imap/lib/capabilities.js.map +7 -0
- package/dist/modules/channel_imap/lib/convert-outbound.js +54 -0
- package/dist/modules/channel_imap/lib/convert-outbound.js.map +7 -0
- package/dist/modules/channel_imap/lib/credentials.js +104 -0
- package/dist/modules/channel_imap/lib/credentials.js.map +7 -0
- package/dist/modules/channel_imap/lib/health.js +39 -0
- package/dist/modules/channel_imap/lib/health.js.map +7 -0
- package/dist/modules/channel_imap/lib/host-pinning.js +34 -0
- package/dist/modules/channel_imap/lib/host-pinning.js.map +7 -0
- package/dist/modules/channel_imap/lib/imap-client.js +210 -0
- package/dist/modules/channel_imap/lib/imap-client.js.map +7 -0
- package/dist/modules/channel_imap/lib/normalize-inbound.js +19 -0
- package/dist/modules/channel_imap/lib/normalize-inbound.js.map +7 -0
- package/dist/modules/channel_imap/lib/smtp-client.js +113 -0
- package/dist/modules/channel_imap/lib/smtp-client.js.map +7 -0
- package/dist/modules/channel_imap/lib/transport.js +17 -0
- package/dist/modules/channel_imap/lib/transport.js.map +7 -0
- package/dist/modules/channel_imap/lib/validate-credentials.js +69 -0
- package/dist/modules/channel_imap/lib/validate-credentials.js.map +7 -0
- package/dist/modules/channel_imap/setup.js +25 -0
- package/dist/modules/channel_imap/setup.js.map +7 -0
- package/dist/modules/channel_imap/widgets/injection/connect/widget.client.js +337 -0
- package/dist/modules/channel_imap/widgets/injection/connect/widget.client.js.map +7 -0
- package/dist/modules/channel_imap/widgets/injection/connect/widget.js +17 -0
- package/dist/modules/channel_imap/widgets/injection/connect/widget.js.map +7 -0
- package/dist/modules/channel_imap/widgets/injection-table.js +14 -0
- package/dist/modules/channel_imap/widgets/injection-table.js.map +7 -0
- package/jest.config.cjs +34 -0
- package/package.json +99 -0
- package/src/index.ts +1 -0
- package/src/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-001.spec.ts +80 -0
- package/src/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-002.spec.ts +28 -0
- package/src/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-003.spec.ts +23 -0
- package/src/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-021.spec.ts +40 -0
- package/src/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-022.spec.ts +38 -0
- package/src/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-023.spec.ts +31 -0
- package/src/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-024.spec.ts +27 -0
- package/src/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-025.spec.ts +23 -0
- package/src/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-026.spec.ts +18 -0
- package/src/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-027.spec.ts +18 -0
- package/src/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-028.spec.ts +19 -0
- package/src/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-029.spec.ts +72 -0
- package/src/modules/channel_imap/__integration__/TC-CHANNEL-EMAIL-030.spec.ts +19 -0
- package/src/modules/channel_imap/acl.ts +6 -0
- package/src/modules/channel_imap/di.ts +26 -0
- package/src/modules/channel_imap/index.ts +6 -0
- package/src/modules/channel_imap/integration.ts +131 -0
- package/src/modules/channel_imap/lib/__tests__/adapter.test.ts +499 -0
- package/src/modules/channel_imap/lib/__tests__/convert-outbound.test.ts +73 -0
- package/src/modules/channel_imap/lib/__tests__/credentials.test.ts +154 -0
- package/src/modules/channel_imap/lib/__tests__/host-pinning.test.ts +68 -0
- package/src/modules/channel_imap/lib/__tests__/imap-client.test.ts +180 -0
- package/src/modules/channel_imap/lib/__tests__/normalize-inbound.test.ts +126 -0
- package/src/modules/channel_imap/lib/__tests__/transport.test.ts +68 -0
- package/src/modules/channel_imap/lib/__tests__/validate-credentials.test.ts +156 -0
- package/src/modules/channel_imap/lib/adapter.ts +451 -0
- package/src/modules/channel_imap/lib/capabilities.ts +16 -0
- package/src/modules/channel_imap/lib/convert-outbound.ts +79 -0
- package/src/modules/channel_imap/lib/credentials.ts +172 -0
- package/src/modules/channel_imap/lib/health.ts +70 -0
- package/src/modules/channel_imap/lib/host-pinning.ts +59 -0
- package/src/modules/channel_imap/lib/imap-client.ts +382 -0
- package/src/modules/channel_imap/lib/normalize-inbound.ts +47 -0
- package/src/modules/channel_imap/lib/smtp-client.ts +214 -0
- package/src/modules/channel_imap/lib/transport.ts +37 -0
- package/src/modules/channel_imap/lib/validate-credentials.ts +98 -0
- package/src/modules/channel_imap/setup.ts +34 -0
- package/src/modules/channel_imap/widgets/injection/connect/widget.client.tsx +359 -0
- package/src/modules/channel_imap/widgets/injection/connect/widget.ts +16 -0
- package/src/modules/channel_imap/widgets/injection-table.ts +12 -0
- package/tsconfig.json +9 -0
- package/watch.mjs +7 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { buildIntegrationDetailWidgetSpotId } from "@open-mercato/shared/modules/integrations/types";
|
|
2
|
+
const channelImapDetailWidgetSpotId = buildIntegrationDetailWidgetSpotId("channel_imap");
|
|
3
|
+
const integration = {
|
|
4
|
+
id: "channel_imap",
|
|
5
|
+
title: "IMAP + SMTP",
|
|
6
|
+
description: "Connect any IMAP-capable mailbox (Fastmail, Proton Bridge, generic IMAP host) for inbound polling and outbound SMTP send.",
|
|
7
|
+
category: "communication",
|
|
8
|
+
hub: "communication_channels",
|
|
9
|
+
providerKey: "imap",
|
|
10
|
+
icon: "mail",
|
|
11
|
+
docsUrl: "https://datatracker.ietf.org/doc/html/rfc3501",
|
|
12
|
+
package: "@open-mercato/channel-imap",
|
|
13
|
+
version: "0.1.0",
|
|
14
|
+
author: "Open Mercato Team",
|
|
15
|
+
company: "Open Mercato",
|
|
16
|
+
license: "MIT",
|
|
17
|
+
tags: ["email", "imap", "smtp", "polling", "communication"],
|
|
18
|
+
detailPage: {
|
|
19
|
+
widgetSpotId: channelImapDetailWidgetSpotId
|
|
20
|
+
},
|
|
21
|
+
apiVersions: [
|
|
22
|
+
{
|
|
23
|
+
id: "rfc3501+rfc5321",
|
|
24
|
+
label: "IMAP4 (RFC3501) + SMTP (RFC5321)",
|
|
25
|
+
status: "stable",
|
|
26
|
+
default: true,
|
|
27
|
+
changelog: "Initial IMAP4/SMTP baseline. UIDVALIDITY+UIDNEXT polling, SMTP STARTTLS+SSL."
|
|
28
|
+
}
|
|
29
|
+
],
|
|
30
|
+
credentials: {
|
|
31
|
+
fields: [
|
|
32
|
+
{
|
|
33
|
+
key: "imapHost",
|
|
34
|
+
label: "IMAP host",
|
|
35
|
+
type: "text",
|
|
36
|
+
required: true,
|
|
37
|
+
placeholder: "imap.fastmail.com",
|
|
38
|
+
helpText: "Hostname of the IMAP server. Typically the same hostname your mail client uses."
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
key: "imapPort",
|
|
42
|
+
label: "IMAP port",
|
|
43
|
+
type: "text",
|
|
44
|
+
required: true,
|
|
45
|
+
placeholder: "993",
|
|
46
|
+
helpText: "993 for IMAPS (TLS) or 143 for STARTTLS."
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
key: "imapTls",
|
|
50
|
+
label: "IMAP TLS mode",
|
|
51
|
+
type: "select",
|
|
52
|
+
required: true,
|
|
53
|
+
options: [
|
|
54
|
+
{ value: "tls", label: "Implicit TLS (port 993)" },
|
|
55
|
+
{ value: "starttls", label: "STARTTLS (port 143)" },
|
|
56
|
+
{ value: "none", label: "None (insecure \u2014 testing only)" }
|
|
57
|
+
],
|
|
58
|
+
helpText: "Prefer implicit TLS. STARTTLS is acceptable. None disables encryption and should only be used inside a private network for testing."
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
key: "imapUser",
|
|
62
|
+
label: "IMAP username",
|
|
63
|
+
type: "text",
|
|
64
|
+
required: true,
|
|
65
|
+
helpText: "Usually your email address."
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
key: "imapPassword",
|
|
69
|
+
label: "IMAP password (or app password)",
|
|
70
|
+
type: "secret",
|
|
71
|
+
required: true,
|
|
72
|
+
helpText: "Use a per-app password if your provider offers one. Stored encrypted at rest."
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
key: "smtpHost",
|
|
76
|
+
label: "SMTP host",
|
|
77
|
+
type: "text",
|
|
78
|
+
required: true,
|
|
79
|
+
placeholder: "smtp.fastmail.com"
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
key: "smtpPort",
|
|
83
|
+
label: "SMTP port",
|
|
84
|
+
type: "text",
|
|
85
|
+
required: true,
|
|
86
|
+
placeholder: "465",
|
|
87
|
+
helpText: "465 for implicit TLS, 587 for STARTTLS."
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
key: "smtpTls",
|
|
91
|
+
label: "SMTP TLS mode",
|
|
92
|
+
type: "select",
|
|
93
|
+
required: true,
|
|
94
|
+
options: [
|
|
95
|
+
{ value: "tls", label: "Implicit TLS (port 465)" },
|
|
96
|
+
{ value: "starttls", label: "STARTTLS (port 587)" },
|
|
97
|
+
{ value: "none", label: "None (insecure \u2014 testing only)" }
|
|
98
|
+
]
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
key: "smtpUser",
|
|
102
|
+
label: "SMTP username",
|
|
103
|
+
type: "text",
|
|
104
|
+
required: true
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
key: "smtpPassword",
|
|
108
|
+
label: "SMTP password (or app password)",
|
|
109
|
+
type: "secret",
|
|
110
|
+
required: true,
|
|
111
|
+
helpText: "Often the same as the IMAP password; provider-dependent."
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
key: "fromAddress",
|
|
115
|
+
label: "From address",
|
|
116
|
+
type: "text",
|
|
117
|
+
required: true,
|
|
118
|
+
placeholder: "name@example.com",
|
|
119
|
+
helpText: "Address used as the From header when sending. Must be deliverable by the SMTP server."
|
|
120
|
+
}
|
|
121
|
+
]
|
|
122
|
+
},
|
|
123
|
+
healthCheck: { service: "channelImapHealthCheck" }
|
|
124
|
+
};
|
|
125
|
+
const integrations = [integration];
|
|
126
|
+
const bundles = [];
|
|
127
|
+
const bundle = void 0;
|
|
128
|
+
export {
|
|
129
|
+
bundle,
|
|
130
|
+
bundles,
|
|
131
|
+
channelImapDetailWidgetSpotId,
|
|
132
|
+
integration,
|
|
133
|
+
integrations
|
|
134
|
+
};
|
|
135
|
+
//# sourceMappingURL=integration.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/modules/channel_imap/integration.ts"],
|
|
4
|
+
"sourcesContent": ["import { buildIntegrationDetailWidgetSpotId, type IntegrationBundle, type IntegrationDefinition } from '@open-mercato/shared/modules/integrations/types'\n\nexport const channelImapDetailWidgetSpotId = buildIntegrationDetailWidgetSpotId('channel_imap')\n\nexport const integration: IntegrationDefinition = {\n id: 'channel_imap',\n title: 'IMAP + SMTP',\n description:\n 'Connect any IMAP-capable mailbox (Fastmail, Proton Bridge, generic IMAP host) for inbound polling and outbound SMTP send.',\n category: 'communication',\n hub: 'communication_channels',\n providerKey: 'imap',\n icon: 'mail',\n docsUrl: 'https://datatracker.ietf.org/doc/html/rfc3501',\n package: '@open-mercato/channel-imap',\n version: '0.1.0',\n author: 'Open Mercato Team',\n company: 'Open Mercato',\n license: 'MIT',\n tags: ['email', 'imap', 'smtp', 'polling', 'communication'],\n detailPage: {\n widgetSpotId: channelImapDetailWidgetSpotId,\n },\n apiVersions: [\n {\n id: 'rfc3501+rfc5321',\n label: 'IMAP4 (RFC3501) + SMTP (RFC5321)',\n status: 'stable',\n default: true,\n changelog: 'Initial IMAP4/SMTP baseline. UIDVALIDITY+UIDNEXT polling, SMTP STARTTLS+SSL.',\n },\n ],\n credentials: {\n fields: [\n {\n key: 'imapHost',\n label: 'IMAP host',\n type: 'text',\n required: true,\n placeholder: 'imap.fastmail.com',\n helpText: 'Hostname of the IMAP server. Typically the same hostname your mail client uses.',\n },\n {\n key: 'imapPort',\n label: 'IMAP port',\n type: 'text',\n required: true,\n placeholder: '993',\n helpText: '993 for IMAPS (TLS) or 143 for STARTTLS.',\n },\n {\n key: 'imapTls',\n label: 'IMAP TLS mode',\n type: 'select',\n required: true,\n options: [\n { value: 'tls', label: 'Implicit TLS (port 993)' },\n { value: 'starttls', label: 'STARTTLS (port 143)' },\n { value: 'none', label: 'None (insecure \u2014 testing only)' },\n ],\n helpText: 'Prefer implicit TLS. STARTTLS is acceptable. None disables encryption and should only be used inside a private network for testing.',\n },\n {\n key: 'imapUser',\n label: 'IMAP username',\n type: 'text',\n required: true,\n helpText: 'Usually your email address.',\n },\n {\n key: 'imapPassword',\n label: 'IMAP password (or app password)',\n type: 'secret',\n required: true,\n helpText: 'Use a per-app password if your provider offers one. Stored encrypted at rest.',\n },\n {\n key: 'smtpHost',\n label: 'SMTP host',\n type: 'text',\n required: true,\n placeholder: 'smtp.fastmail.com',\n },\n {\n key: 'smtpPort',\n label: 'SMTP port',\n type: 'text',\n required: true,\n placeholder: '465',\n helpText: '465 for implicit TLS, 587 for STARTTLS.',\n },\n {\n key: 'smtpTls',\n label: 'SMTP TLS mode',\n type: 'select',\n required: true,\n options: [\n { value: 'tls', label: 'Implicit TLS (port 465)' },\n { value: 'starttls', label: 'STARTTLS (port 587)' },\n { value: 'none', label: 'None (insecure \u2014 testing only)' },\n ],\n },\n {\n key: 'smtpUser',\n label: 'SMTP username',\n type: 'text',\n required: true,\n },\n {\n key: 'smtpPassword',\n label: 'SMTP password (or app password)',\n type: 'secret',\n required: true,\n helpText: 'Often the same as the IMAP password; provider-dependent.',\n },\n {\n key: 'fromAddress',\n label: 'From address',\n type: 'text',\n required: true,\n placeholder: 'name@example.com',\n helpText: 'Address used as the From header when sending. Must be deliverable by the SMTP server.',\n },\n ],\n },\n healthCheck: { service: 'channelImapHealthCheck' },\n}\n\nexport const integrations: IntegrationDefinition[] = [integration]\nexport const bundles: IntegrationBundle[] = []\nexport const bundle: IntegrationBundle | undefined = undefined\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,0CAA8F;AAEhG,MAAM,gCAAgC,mCAAmC,cAAc;AAEvF,MAAM,cAAqC;AAAA,EAChD,IAAI;AAAA,EACJ,OAAO;AAAA,EACP,aACE;AAAA,EACF,UAAU;AAAA,EACV,KAAK;AAAA,EACL,aAAa;AAAA,EACb,MAAM;AAAA,EACN,SAAS;AAAA,EACT,SAAS;AAAA,EACT,SAAS;AAAA,EACT,QAAQ;AAAA,EACR,SAAS;AAAA,EACT,SAAS;AAAA,EACT,MAAM,CAAC,SAAS,QAAQ,QAAQ,WAAW,eAAe;AAAA,EAC1D,YAAY;AAAA,IACV,cAAc;AAAA,EAChB;AAAA,E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EAAE,OAAO,OAAO,OAAO,0BAA0B;AAAA,UACjD,EAAE,OAAO,YAAY,OAAO,sBAAsB;AAAA,UAClD,EAAE,OAAO,QAAQ,OAAO,sCAAiC;AAAA,QAC3D;AAAA,MACF;AAAA,MACA;AAAA,QACE,KAAK;AAAA,QACL,OAAO;AAAA,QACP,MAAM;AAAA,QACN,UAAU;AAAA,MACZ;AAAA,MACA;AAAA,QACE,KAAK;AAAA,QACL,OAAO;AAAA,QACP,MAAM;AAAA,QACN,UAAU;AAAA,QACV,UAAU;AAAA,MACZ;AAAA,MACA;AAAA,QACE,KAAK;AAAA,QACL,OAAO;AAAA,QACP,MAAM;AAAA,QACN,UAAU;AAAA,QACV,aAAa;AAAA,QACb,UAAU;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AAAA,EACA,aAAa,EAAE,SAAS,yBAAyB;AACnD;AAEO,MAAM,eAAwC,CAAC,WAAW;AAC1D,MAAM,UAA+B,CAAC;AACtC,MAAM,SAAwC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { imapCapabilities } from "./capabilities.js";
|
|
2
|
+
import { imapCredentialsSchema, imapChannelStateSchema } from "./credentials.js";
|
|
3
|
+
import {
|
|
4
|
+
credentialsToConnection,
|
|
5
|
+
getImapClient
|
|
6
|
+
} from "./imap-client.js";
|
|
7
|
+
import {
|
|
8
|
+
credentialsToSmtpConnection,
|
|
9
|
+
getSmtpClient
|
|
10
|
+
} from "./smtp-client.js";
|
|
11
|
+
import { convertOutboundForEmail } from "./convert-outbound.js";
|
|
12
|
+
import { normalizeInboundImapMessage } from "./normalize-inbound.js";
|
|
13
|
+
import { validateImapCredentials } from "./validate-credentials.js";
|
|
14
|
+
import { emailResolveContact } from "@open-mercato/core/modules/communication_channels/lib/email-contact";
|
|
15
|
+
import { decodeCursor, encodeCursor } from "@open-mercato/core/modules/communication_channels/lib/email-mime";
|
|
16
|
+
class ImapChannelAdapter {
|
|
17
|
+
constructor() {
|
|
18
|
+
this.providerKey = "imap";
|
|
19
|
+
this.channelType = "email";
|
|
20
|
+
this.capabilities = imapCapabilities;
|
|
21
|
+
}
|
|
22
|
+
async sendMessage(input) {
|
|
23
|
+
const credentials = parseCredentialsOrThrow(input.credentials);
|
|
24
|
+
if (Array.isArray(input.content.attachments) && input.content.attachments.length > 0) {
|
|
25
|
+
return {
|
|
26
|
+
externalMessageId: "",
|
|
27
|
+
status: "failed",
|
|
28
|
+
error: "[internal] IMAP/SMTP adapter does not yet support attachments. Send the message without attachments or use a provider that supports them (Gmail)."
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
let native;
|
|
32
|
+
try {
|
|
33
|
+
native = await convertOutboundForEmail({
|
|
34
|
+
body: input.content.html ?? input.content.text ?? "",
|
|
35
|
+
bodyFormat: input.content.bodyFormat ?? (input.content.html ? "html" : "text"),
|
|
36
|
+
attachments: input.content.attachments,
|
|
37
|
+
channelMetadata: input.metadata
|
|
38
|
+
});
|
|
39
|
+
} catch (error) {
|
|
40
|
+
const message = error instanceof Error ? error.message : "Outbound conversion failed";
|
|
41
|
+
return { externalMessageId: "", status: "failed", error: message };
|
|
42
|
+
}
|
|
43
|
+
const meta = native.metadata ?? {};
|
|
44
|
+
const to = Array.isArray(meta.to) ? meta.to : [];
|
|
45
|
+
if (to.length === 0) {
|
|
46
|
+
return { externalMessageId: "", status: "failed", error: "[internal] Email send requires at least one recipient" };
|
|
47
|
+
}
|
|
48
|
+
const smtp = getSmtpClient();
|
|
49
|
+
const result = await smtp.send(credentialsToSmtpConnection(credentials), {
|
|
50
|
+
from: credentials.fromAddress,
|
|
51
|
+
to,
|
|
52
|
+
cc: Array.isArray(meta.cc) ? meta.cc : void 0,
|
|
53
|
+
bcc: Array.isArray(meta.bcc) ? meta.bcc : void 0,
|
|
54
|
+
subject: typeof meta.subject === "string" ? meta.subject : void 0,
|
|
55
|
+
text: native.content.text,
|
|
56
|
+
html: native.content.html,
|
|
57
|
+
inReplyTo: typeof meta.inReplyTo === "string" ? meta.inReplyTo : void 0,
|
|
58
|
+
references: Array.isArray(meta.references) ? meta.references : void 0,
|
|
59
|
+
messageId: typeof meta.messageId === "string" ? meta.messageId : void 0
|
|
60
|
+
});
|
|
61
|
+
const imap = getImapClient();
|
|
62
|
+
try {
|
|
63
|
+
if (result.raw.length > 0) {
|
|
64
|
+
await imap.appendSent(credentialsToConnection(credentials), result.raw);
|
|
65
|
+
}
|
|
66
|
+
} catch (appendError) {
|
|
67
|
+
console.warn(
|
|
68
|
+
"[internal] channel_imap: failed to append outbound message to Sent folder:",
|
|
69
|
+
appendError instanceof Error ? appendError.message : appendError
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
externalMessageId: result.messageId,
|
|
74
|
+
conversationId: input.conversationId,
|
|
75
|
+
status: "sent",
|
|
76
|
+
metadata: { response: result.response }
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
async verifyWebhook(_input) {
|
|
80
|
+
return { raw: {}, eventType: "other", metadata: { reason: "imap-does-not-use-webhooks" } };
|
|
81
|
+
}
|
|
82
|
+
async getStatus(_input) {
|
|
83
|
+
return { status: "sent" };
|
|
84
|
+
}
|
|
85
|
+
async convertOutbound(input) {
|
|
86
|
+
return convertOutboundForEmail(input);
|
|
87
|
+
}
|
|
88
|
+
async normalizeInbound(raw) {
|
|
89
|
+
const rawBuffer = pickRawMimeBuffer(raw);
|
|
90
|
+
const accountIdentifier = pickAccountIdentifier(raw);
|
|
91
|
+
const uid = pickUid(raw);
|
|
92
|
+
return normalizeInboundImapMessage({
|
|
93
|
+
rawMessage: rawBuffer,
|
|
94
|
+
uid,
|
|
95
|
+
accountIdentifier
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
async validateCredentials(input) {
|
|
99
|
+
return validateImapCredentials(input.credentials);
|
|
100
|
+
}
|
|
101
|
+
async fetchHistory(input) {
|
|
102
|
+
const credentials = parseCredentialsOrThrow(input.credentials);
|
|
103
|
+
const channelState = imapChannelStateSchema.parse(input.channelState ?? {});
|
|
104
|
+
const imap = getImapClient();
|
|
105
|
+
const connection = credentialsToConnection(credentials);
|
|
106
|
+
const folderState = await imap.selectInbox(connection);
|
|
107
|
+
const previousUidValidity = toNumberOrUndefined(channelState.uidValidity);
|
|
108
|
+
const previousUidNext = toNumberOrUndefined(channelState.uidNext);
|
|
109
|
+
const serverUidNext = toNumberOrUndefined(folderState.uidNext);
|
|
110
|
+
const HARD_CAP = clampHardCap(input.limit);
|
|
111
|
+
const uidValidityMismatch = previousUidValidity !== void 0 && folderState.uidValidity !== void 0 && folderState.uidValidity !== previousUidValidity;
|
|
112
|
+
if (uidValidityMismatch) {
|
|
113
|
+
console.warn(
|
|
114
|
+
"[channel-imap] UIDVALIDITY changed for INBOX (was %s, now %s) \u2014 discarding cursor and re-bootstrapping",
|
|
115
|
+
previousUidValidity,
|
|
116
|
+
folderState.uidValidity
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
const needsBootstrap = uidValidityMismatch || previousUidNext === void 0 || previousUidNext === null;
|
|
120
|
+
let fetched;
|
|
121
|
+
let hasMore = false;
|
|
122
|
+
if (needsBootstrap) {
|
|
123
|
+
fetched = [];
|
|
124
|
+
hasMore = false;
|
|
125
|
+
} else if (previousUidNext !== void 0 && serverUidNext !== void 0 && previousUidNext >= serverUidNext) {
|
|
126
|
+
fetched = [];
|
|
127
|
+
hasMore = false;
|
|
128
|
+
} else {
|
|
129
|
+
const range = `${previousUidNext}:*`;
|
|
130
|
+
const probeLimit = HARD_CAP + 1;
|
|
131
|
+
const raw = await imap.fetchUidRange(connection, range, { limit: probeLimit });
|
|
132
|
+
if (raw.length > HARD_CAP) {
|
|
133
|
+
fetched = raw.slice(0, HARD_CAP);
|
|
134
|
+
hasMore = true;
|
|
135
|
+
} else {
|
|
136
|
+
fetched = raw;
|
|
137
|
+
hasMore = false;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
const messages = [];
|
|
141
|
+
for (const item of fetched) {
|
|
142
|
+
const normalized = await normalizeInboundImapMessage({
|
|
143
|
+
rawMessage: item.rawBody,
|
|
144
|
+
uid: item.uid,
|
|
145
|
+
accountIdentifier: credentials.fromAddress,
|
|
146
|
+
fallbackDate: item.internalDate
|
|
147
|
+
});
|
|
148
|
+
messages.push(normalized);
|
|
149
|
+
}
|
|
150
|
+
const advancedUidNext = (() => {
|
|
151
|
+
if (needsBootstrap) return serverUidNext;
|
|
152
|
+
if (fetched.length === 0) return previousUidNext;
|
|
153
|
+
const highest = fetched.reduce((max, item) => item.uid > max ? item.uid : max, 0);
|
|
154
|
+
return highest + 1;
|
|
155
|
+
})();
|
|
156
|
+
const nextChannelState = {
|
|
157
|
+
uidValidity: folderState.uidValidity ?? previousUidValidity,
|
|
158
|
+
uidNext: advancedUidNext,
|
|
159
|
+
lastFolder: "INBOX"
|
|
160
|
+
};
|
|
161
|
+
const nextCursor = encodeCursor(nextChannelState);
|
|
162
|
+
return { messages, nextCursor, hasMore };
|
|
163
|
+
}
|
|
164
|
+
async importHistory(input) {
|
|
165
|
+
const credentials = parseCredentialsOrThrow(input.credentials);
|
|
166
|
+
const connection = credentialsToConnection(credentials);
|
|
167
|
+
const imap = getImapClient();
|
|
168
|
+
const sinceDaysRaw = Number.isFinite(input.sinceDays) ? Math.trunc(input.sinceDays) : 30;
|
|
169
|
+
const sinceDays = Math.max(1, Math.min(365, sinceDaysRaw));
|
|
170
|
+
const sinceDate = new Date(Date.now() - sinceDays * 24 * 60 * 60 * 1e3);
|
|
171
|
+
const maxMessagesRaw = Number.isFinite(input.maxMessages) ? Math.trunc(input.maxMessages) : 1e3;
|
|
172
|
+
const maxMessages = Math.max(1, Math.min(5e3, maxMessagesRaw));
|
|
173
|
+
const PAGE_SIZE = clampHardCap(void 0);
|
|
174
|
+
let allUids;
|
|
175
|
+
let remainingUids;
|
|
176
|
+
let collectedSoFar;
|
|
177
|
+
let totalCandidates;
|
|
178
|
+
const cursor = decodeImportCursor(input.cursor);
|
|
179
|
+
if (cursor) {
|
|
180
|
+
remainingUids = cursor.remaining;
|
|
181
|
+
collectedSoFar = cursor.collected;
|
|
182
|
+
totalCandidates = cursor.total;
|
|
183
|
+
allUids = cursor.remaining;
|
|
184
|
+
} else {
|
|
185
|
+
const senders = (input.contactEmails ?? []).filter((s) => typeof s === "string" && s.includes("@"));
|
|
186
|
+
const uidSet = /* @__PURE__ */ new Set();
|
|
187
|
+
if (senders.length === 0) {
|
|
188
|
+
const uids = await imap.searchUidsByFromAndSince(connection, { sinceDate });
|
|
189
|
+
for (const uid of uids) uidSet.add(uid);
|
|
190
|
+
} else {
|
|
191
|
+
const CHUNK_SIZE = 30;
|
|
192
|
+
for (let i = 0; i < senders.length; i += CHUNK_SIZE) {
|
|
193
|
+
const chunk = senders.slice(i, i + CHUNK_SIZE);
|
|
194
|
+
const uids = await imap.searchUidsByFromAndSince(connection, { fromAddresses: chunk, sinceDate });
|
|
195
|
+
for (const uid of uids) uidSet.add(uid);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
allUids = Array.from(uidSet).sort((a, b) => b - a).slice(0, maxMessages);
|
|
199
|
+
remainingUids = allUids;
|
|
200
|
+
collectedSoFar = 0;
|
|
201
|
+
totalCandidates = allUids.length;
|
|
202
|
+
}
|
|
203
|
+
if (remainingUids.length === 0) {
|
|
204
|
+
return { messages: [], hasMore: false, totalCandidates };
|
|
205
|
+
}
|
|
206
|
+
const batchUids = remainingUids.slice(0, PAGE_SIZE);
|
|
207
|
+
const stillRemaining = remainingUids.slice(PAGE_SIZE);
|
|
208
|
+
const uidSetExpression = batchUids.join(",");
|
|
209
|
+
const fetched = await imap.fetchUidRange(connection, uidSetExpression, { limit: PAGE_SIZE });
|
|
210
|
+
const messages = [];
|
|
211
|
+
for (const item of fetched) {
|
|
212
|
+
const normalized = await normalizeInboundImapMessage({
|
|
213
|
+
rawMessage: item.rawBody,
|
|
214
|
+
uid: item.uid,
|
|
215
|
+
accountIdentifier: credentials.fromAddress,
|
|
216
|
+
fallbackDate: item.internalDate
|
|
217
|
+
});
|
|
218
|
+
messages.push(normalized);
|
|
219
|
+
}
|
|
220
|
+
const newCollected = collectedSoFar + messages.length;
|
|
221
|
+
const hasMore = stillRemaining.length > 0 && newCollected < maxMessages;
|
|
222
|
+
const nextCursor = hasMore ? encodeImportCursor({ remaining: stillRemaining, collected: newCollected, total: totalCandidates }) : void 0;
|
|
223
|
+
return { messages, nextCursor, hasMore, totalCandidates };
|
|
224
|
+
}
|
|
225
|
+
async resolveContact(input) {
|
|
226
|
+
return emailResolveContact(input);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
function parseCredentialsOrThrow(value) {
|
|
230
|
+
const parsed = imapCredentialsSchema.safeParse(value);
|
|
231
|
+
if (!parsed.success) {
|
|
232
|
+
const first = parsed.error.issues[0];
|
|
233
|
+
throw new Error(`Invalid IMAP credentials: ${first?.message ?? "unknown validation error"}`);
|
|
234
|
+
}
|
|
235
|
+
return parsed.data;
|
|
236
|
+
}
|
|
237
|
+
function pickRawMimeBuffer(raw) {
|
|
238
|
+
const candidate = raw.raw;
|
|
239
|
+
const value = candidate?.rawBody ?? candidate?.mime ?? raw.raw;
|
|
240
|
+
if (Buffer.isBuffer(value)) return value;
|
|
241
|
+
if (value instanceof Uint8Array) return Buffer.from(value);
|
|
242
|
+
if (typeof value === "string") return Buffer.from(value, "utf-8");
|
|
243
|
+
throw new Error("[internal] IMAP normalizeInbound requires `raw.rawBody` to be a Buffer or string MIME payload");
|
|
244
|
+
}
|
|
245
|
+
function pickAccountIdentifier(raw) {
|
|
246
|
+
const candidate = raw.raw;
|
|
247
|
+
const id = typeof candidate?.accountIdentifier === "string" ? candidate.accountIdentifier : void 0;
|
|
248
|
+
return id ?? "unknown@imap";
|
|
249
|
+
}
|
|
250
|
+
function pickUid(raw) {
|
|
251
|
+
const candidate = raw.raw;
|
|
252
|
+
return typeof candidate?.uid === "number" ? candidate.uid : void 0;
|
|
253
|
+
}
|
|
254
|
+
function clampHardCap(callerLimit) {
|
|
255
|
+
const envOverride = Number.parseInt(process.env.OM_CHANNEL_IMAP_HARD_CAP_PER_POLL ?? "", 10);
|
|
256
|
+
const HARD_CAP_MAX = Number.isFinite(envOverride) && envOverride > 0 ? envOverride : 200;
|
|
257
|
+
if (typeof callerLimit === "number" && callerLimit > 0) {
|
|
258
|
+
return Math.min(callerLimit, HARD_CAP_MAX);
|
|
259
|
+
}
|
|
260
|
+
return HARD_CAP_MAX;
|
|
261
|
+
}
|
|
262
|
+
function encodeImportCursor(cursor) {
|
|
263
|
+
return encodeCursor(cursor);
|
|
264
|
+
}
|
|
265
|
+
function decodeImportCursor(value) {
|
|
266
|
+
const parsed = decodeCursor(value);
|
|
267
|
+
if (!parsed || typeof parsed !== "object") return null;
|
|
268
|
+
const obj = parsed;
|
|
269
|
+
const remaining = Array.isArray(obj.remaining) ? obj.remaining.filter((n) => typeof n === "number" && Number.isFinite(n)) : [];
|
|
270
|
+
const collected = typeof obj.collected === "number" ? obj.collected : 0;
|
|
271
|
+
const total = typeof obj.total === "number" ? obj.total : void 0;
|
|
272
|
+
return { remaining, collected, total };
|
|
273
|
+
}
|
|
274
|
+
function toNumberOrUndefined(value) {
|
|
275
|
+
if (typeof value === "number") return value;
|
|
276
|
+
if (typeof value === "string" && value.length > 0) {
|
|
277
|
+
const n = Number(value);
|
|
278
|
+
return Number.isFinite(n) ? n : void 0;
|
|
279
|
+
}
|
|
280
|
+
return void 0;
|
|
281
|
+
}
|
|
282
|
+
let cachedAdapter = null;
|
|
283
|
+
function getImapChannelAdapter() {
|
|
284
|
+
if (!cachedAdapter) cachedAdapter = new ImapChannelAdapter();
|
|
285
|
+
return cachedAdapter;
|
|
286
|
+
}
|
|
287
|
+
export {
|
|
288
|
+
ImapChannelAdapter,
|
|
289
|
+
getImapChannelAdapter
|
|
290
|
+
};
|
|
291
|
+
//# sourceMappingURL=adapter.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/modules/channel_imap/lib/adapter.ts"],
|
|
4
|
+
"sourcesContent": ["import type {\n ChannelAdapter,\n ConvertOutboundInput,\n ChannelNativeContent,\n FetchHistoryInput,\n GetMessageStatusInput,\n HistoryPage,\n ImportHistoryInput,\n ImportHistoryPage,\n InboundMessage,\n MessageStatus,\n NormalizedInboundMessage,\n ResolveContactInput,\n ContactHint,\n SendMessageInput,\n SendMessageResult,\n ValidateCredentialsInput,\n ValidateCredentialsResult,\n VerifyWebhookInput,\n} from '@open-mercato/core/modules/communication_channels/lib/adapter'\nimport { imapCapabilities } from './capabilities'\nimport { imapCredentialsSchema, imapChannelStateSchema, type ImapCredentials, type ImapChannelState } from './credentials'\nimport {\n credentialsToConnection,\n getImapClient,\n} from './imap-client'\nimport {\n credentialsToSmtpConnection,\n getSmtpClient,\n} from './smtp-client'\nimport { convertOutboundForEmail } from './convert-outbound'\nimport { normalizeInboundImapMessage } from './normalize-inbound'\nimport { validateImapCredentials } from './validate-credentials'\nimport { emailResolveContact } from '@open-mercato/core/modules/communication_channels/lib/email-contact'\nimport { decodeCursor, encodeCursor } from '@open-mercato/core/modules/communication_channels/lib/email-mime'\n\n/**\n * IMAP+SMTP `ChannelAdapter`. Inbound is polling-driven (`realtimePush: false`),\n * outbound is SMTP. Threading is RFC2822 (In-Reply-To / References).\n *\n * Why this adapter omits some methods:\n * - `verifyWebhook` \u2014 IMAP has no webhook; we return a no-op event with\n * `eventType: 'other'` so the hub's webhook route returns 202 if anyone\n * ever POSTs at `/api/communication_channels/webhook/imap`.\n * - `getStatus` \u2014 IMAP has no delivery-status concept beyond `\\Seen`; we\n * return `{ status: 'sent' }` as a best-effort placeholder.\n * - No `sendReaction` / `editMessage` / `deleteMessage` \u2014 email doesn't\n * support these capabilities.\n */\nclass ImapChannelAdapter implements ChannelAdapter {\n readonly providerKey = 'imap'\n readonly channelType = 'email'\n readonly capabilities = imapCapabilities\n\n async sendMessage(input: SendMessageInput): Promise<SendMessageResult> {\n const credentials = parseCredentialsOrThrow(input.credentials)\n\n // Reject attachments at the boundary BEFORE building the MIME body. The hub\n // passes attachments as URL pointers; until the IMAP/SMTP adapter wires a\n // fetcher (with size + content-type validation), inlining them is unsafe \u2014\n // a 0-byte attachment looks \"delivered\" but conveys nothing. Checking here\n // (rather than after conversion) avoids wasted MIME-build work and surfaces\n // the clearer \"attachments unsupported\" error even when recipients are also\n // missing. Documented in review M2 (2026-05-26) and tracked as a follow-up.\n if (Array.isArray(input.content.attachments) && input.content.attachments.length > 0) {\n return {\n externalMessageId: '',\n status: 'failed',\n error:\n '[internal] IMAP/SMTP adapter does not yet support attachments. Send the message without attachments or use a provider that supports them (Gmail).',\n }\n }\n\n let native: ChannelNativeContent\n try {\n native = await convertOutboundForEmail({\n body: input.content.html ?? input.content.text ?? '',\n bodyFormat: input.content.bodyFormat ?? (input.content.html ? 'html' : 'text'),\n attachments: input.content.attachments,\n channelMetadata: input.metadata,\n })\n } catch (error) {\n const message = error instanceof Error ? error.message : 'Outbound conversion failed'\n return { externalMessageId: '', status: 'failed', error: message }\n }\n const meta = (native.metadata ?? {}) as Record<string, unknown>\n const to = Array.isArray(meta.to) ? (meta.to as string[]) : []\n if (to.length === 0) {\n return { externalMessageId: '', status: 'failed', error: '[internal] Email send requires at least one recipient' }\n }\n\n const smtp = getSmtpClient()\n const result = await smtp.send(credentialsToSmtpConnection(credentials), {\n from: credentials.fromAddress,\n to,\n cc: Array.isArray(meta.cc) ? (meta.cc as string[]) : undefined,\n bcc: Array.isArray(meta.bcc) ? (meta.bcc as string[]) : undefined,\n subject: typeof meta.subject === 'string' ? (meta.subject as string) : undefined,\n text: native.content.text,\n html: native.content.html,\n inReplyTo: typeof meta.inReplyTo === 'string' ? (meta.inReplyTo as string) : undefined,\n references: Array.isArray(meta.references) ? (meta.references as string[]) : undefined,\n messageId: typeof meta.messageId === 'string' ? (meta.messageId as string) : undefined,\n })\n\n // Best-effort append to Sent \u2014 many servers auto-store via \"Submission\" but not all do.\n const imap = getImapClient()\n try {\n // Skip when the RFC2822 bytes are empty (MailComposer build failed upstream):\n // appending a 0-byte buffer would create a corrupt Sent-folder entry, and the\n // send itself already succeeded.\n if (result.raw.length > 0) {\n await imap.appendSent(credentialsToConnection(credentials), result.raw)\n }\n } catch (appendError) {\n // Best-effort: many servers auto-store sent mail via Submission, and the\n // Sent mailbox name is provider-specific (localized, or \"[Gmail]/Sent Mail\").\n // Log so operators can diagnose missing Sent-folder archival rather than\n // failing the send.\n console.warn(\n '[internal] channel_imap: failed to append outbound message to Sent folder:',\n appendError instanceof Error ? appendError.message : appendError,\n )\n }\n\n return {\n externalMessageId: result.messageId,\n conversationId: input.conversationId,\n status: 'sent',\n metadata: { response: result.response },\n }\n }\n\n async verifyWebhook(_input: VerifyWebhookInput): Promise<InboundMessage> {\n return { raw: {}, eventType: 'other', metadata: { reason: 'imap-does-not-use-webhooks' } }\n }\n\n async getStatus(_input: GetMessageStatusInput): Promise<MessageStatus> {\n return { status: 'sent' }\n }\n\n async convertOutbound(input: ConvertOutboundInput): Promise<ChannelNativeContent> {\n return convertOutboundForEmail(input)\n }\n\n async normalizeInbound(raw: InboundMessage): Promise<NormalizedInboundMessage> {\n const rawBuffer = pickRawMimeBuffer(raw)\n const accountIdentifier = pickAccountIdentifier(raw)\n const uid = pickUid(raw)\n return normalizeInboundImapMessage({\n rawMessage: rawBuffer,\n uid,\n accountIdentifier,\n })\n }\n\n async validateCredentials(input: ValidateCredentialsInput): Promise<ValidateCredentialsResult> {\n return validateImapCredentials(input.credentials)\n }\n\n async fetchHistory(input: FetchHistoryInput): Promise<HistoryPage> {\n const credentials = parseCredentialsOrThrow(input.credentials)\n const channelState = imapChannelStateSchema.parse(input.channelState ?? {}) satisfies ImapChannelState\n const imap = getImapClient()\n const connection = credentialsToConnection(credentials)\n\n // Spec B \u00A7 Bounded, cursor-driven IMAP inbound:\n // - Bootstrap (no cursor): SELECT INBOX, persist UIDVALIDITY + UIDNEXT,\n // return ZERO messages. Backlog import happens via the explicit\n // `/import-history` endpoint, not via the silent connect flow.\n // - Incremental (cursor exists): UID FETCH `previousUidNext:*`, capped\n // at HARD_CAP = 200. If more available, set `hasMore: true` so the\n // hub re-enqueues immediately and drains the backlog.\n // - UIDVALIDITY mismatch: discard cursor and treat as bootstrap (the\n // mailbox was recreated or renamed; we cannot trust the prior cursor).\n const folderState = await imap.selectInbox(connection)\n const previousUidValidity = toNumberOrUndefined(channelState.uidValidity)\n const previousUidNext = toNumberOrUndefined(channelState.uidNext)\n const serverUidNext = toNumberOrUndefined(folderState.uidNext)\n const HARD_CAP = clampHardCap(input.limit)\n\n const uidValidityMismatch =\n previousUidValidity !== undefined &&\n folderState.uidValidity !== undefined &&\n folderState.uidValidity !== previousUidValidity\n if (uidValidityMismatch) {\n console.warn(\n '[channel-imap] UIDVALIDITY changed for INBOX (was %s, now %s) \u2014 discarding cursor and re-bootstrapping',\n previousUidValidity,\n folderState.uidValidity,\n )\n }\n const needsBootstrap =\n uidValidityMismatch || previousUidNext === undefined || previousUidNext === null\n\n let fetched: { uid: number; rawBody: Buffer; internalDate?: Date; flags?: string[] }[]\n let hasMore = false\n if (needsBootstrap) {\n // \u2500\u2500 Bootstrap: persist cursor only, fetch zero messages \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n // Spec B \u00A7 Bootstrap. The \"1M inbox\" failure mode is fixed by\n // construction: a fresh user sees zero history until they explicitly\n // request `/import-history`. Set `hasMore: false` so the poll worker\n // does NOT immediately re-enqueue.\n fetched = []\n hasMore = false\n } else if (previousUidNext !== undefined && serverUidNext !== undefined && previousUidNext >= serverUidNext) {\n // \u2500\u2500 Idle: UIDNEXT did not advance, so there is no new mail \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n // Skip the FETCH entirely. IMAP `<n>:*` always matches at least the\n // highest existing UID, so an idle mailbox would otherwise re-fetch and\n // re-normalize one already-ingested message every tick. The cursor is\n // retained downstream (an empty fetch does not advance it).\n fetched = []\n hasMore = false\n } else {\n // \u2500\u2500 Incremental: UID FETCH previousUidNext:* up to HARD_CAP \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n // On a mature mailbox this is typically 0-N UIDs. The HARD_CAP bounds\n // the per-poll wall-clock + DB transaction size; if more remain, the\n // hub re-enqueues us immediately via `hasMore: true`.\n const range = `${previousUidNext}:*`\n // Fetch up to HARD_CAP + 1 so we can detect whether more remain\n // without paying for an extra round-trip later.\n const probeLimit = HARD_CAP + 1\n const raw = await imap.fetchUidRange(connection, range, { limit: probeLimit })\n if (raw.length > HARD_CAP) {\n fetched = raw.slice(0, HARD_CAP)\n hasMore = true\n } else {\n fetched = raw\n hasMore = false\n }\n }\n\n const messages: NormalizedInboundMessage[] = []\n for (const item of fetched) {\n const normalized = await normalizeInboundImapMessage({\n rawMessage: item.rawBody,\n uid: item.uid,\n accountIdentifier: credentials.fromAddress,\n fallbackDate: item.internalDate,\n })\n messages.push(normalized)\n }\n\n // Cursor advancement contract:\n // - Bootstrap: persist the server's current UIDNEXT so the next poll\n // becomes incremental from this point onward (intentionally skips the\n // pre-existing backlog; use `/import-history` to pull it).\n // - Incremental: persist `highestFetchedUid + 1` \u2014 NEVER the server's\n // UIDNEXT. When `fetched` is empty we retain `previousUidNext` so the\n // next poll resumes from the same point.\n const advancedUidNext = (() => {\n if (needsBootstrap) return serverUidNext\n if (fetched.length === 0) return previousUidNext\n const highest = fetched.reduce((max, item) => (item.uid > max ? item.uid : max), 0)\n // Anchor the cursor to the highest UID we ACTUALLY fetched \u2014 never to the\n // server's UIDNEXT. Providers like Gmail report a UIDNEXT that runs ahead\n // of the highest message currently in the folder (UID gaps from\n // labels/threads, or a message materialising into INBOX at a UID below\n // UIDNEXT). Jumping the cursor to `serverUidNext` then steps over any\n // INBOX message that sits below it: that message lands permanently below\n // the cursor and is never fetched again \u2014 the bug that silently dropped\n // inbound replies (UID 61979 skipped while cursor jumped to 61981).\n // `highest + 1` guarantees we never step over an unfetched message; if the\n // server's UIDNEXT is higher, the next poll just re-scans `highest+1:*`\n // (idempotent \u2014 the hub dedups on (channel_id, external_message_id)).\n return highest + 1\n })()\n const nextChannelState: ImapChannelState = {\n uidValidity: folderState.uidValidity ?? previousUidValidity,\n uidNext: advancedUidNext,\n lastFolder: 'INBOX',\n }\n\n // The hub's polling worker re-reads cursor through `fetchHistory`. We embed the\n // next-poll state in `nextCursor` (base64-encoded JSON) so workers can persist\n // it onto `CommunicationChannel.channelState` without depending on a hub-specific\n // contract beyond the existing `HistoryPage` shape.\n const nextCursor = encodeCursor(nextChannelState)\n return { messages, nextCursor, hasMore }\n }\n\n async importHistory(input: ImportHistoryInput): Promise<ImportHistoryPage> {\n const credentials = parseCredentialsOrThrow(input.credentials)\n const connection = credentialsToConnection(credentials)\n const imap = getImapClient()\n\n const sinceDaysRaw = Number.isFinite(input.sinceDays) ? Math.trunc(input.sinceDays) : 30\n const sinceDays = Math.max(1, Math.min(365, sinceDaysRaw))\n const sinceDate = new Date(Date.now() - sinceDays * 24 * 60 * 60 * 1000)\n\n const maxMessagesRaw = Number.isFinite(input.maxMessages) ? Math.trunc(input.maxMessages as number) : 1000\n const maxMessages = Math.max(1, Math.min(5000, maxMessagesRaw))\n\n const PAGE_SIZE = clampHardCap(undefined)\n\n // Resume previous page or perform initial SEARCH on first call. The cursor\n // encodes the full remaining UID list discovered server-side so subsequent\n // pages don't re-issue SEARCH (which on large mailboxes is expensive).\n let allUids: number[]\n let remainingUids: number[]\n let collectedSoFar: number\n let totalCandidates: number | undefined\n const cursor = decodeImportCursor(input.cursor)\n if (cursor) {\n remainingUids = cursor.remaining\n collectedSoFar = cursor.collected\n totalCandidates = cursor.total\n allUids = cursor.remaining\n } else {\n // FROM-chunking: SEARCH with very long OR chains can blow imapflow's\n // tag-buffer; chunk to \u226430 senders and union the results. When\n // contactEmails is empty we issue a single SINCE-only search.\n const senders = (input.contactEmails ?? []).filter((s): s is string => typeof s === 'string' && s.includes('@'))\n const uidSet = new Set<number>()\n if (senders.length === 0) {\n const uids = await imap.searchUidsByFromAndSince(connection, { sinceDate })\n for (const uid of uids) uidSet.add(uid)\n } else {\n const CHUNK_SIZE = 30\n for (let i = 0; i < senders.length; i += CHUNK_SIZE) {\n const chunk = senders.slice(i, i + CHUNK_SIZE)\n const uids = await imap.searchUidsByFromAndSince(connection, { fromAddresses: chunk, sinceDate })\n for (const uid of uids) uidSet.add(uid)\n }\n }\n // Process newest first (highest UIDs ~= most recent on standard servers).\n allUids = Array.from(uidSet).sort((a, b) => b - a).slice(0, maxMessages)\n remainingUids = allUids\n collectedSoFar = 0\n totalCandidates = allUids.length\n }\n\n if (remainingUids.length === 0) {\n return { messages: [], hasMore: false, totalCandidates }\n }\n\n const batchUids = remainingUids.slice(0, PAGE_SIZE)\n const stillRemaining = remainingUids.slice(PAGE_SIZE)\n const uidSetExpression = batchUids.join(',')\n const fetched = await imap.fetchUidRange(connection, uidSetExpression, { limit: PAGE_SIZE })\n\n const messages: NormalizedInboundMessage[] = []\n for (const item of fetched) {\n const normalized = await normalizeInboundImapMessage({\n rawMessage: item.rawBody,\n uid: item.uid,\n accountIdentifier: credentials.fromAddress,\n fallbackDate: item.internalDate,\n })\n messages.push(normalized)\n }\n\n const newCollected = collectedSoFar + messages.length\n const hasMore = stillRemaining.length > 0 && newCollected < maxMessages\n const nextCursor = hasMore\n ? encodeImportCursor({ remaining: stillRemaining, collected: newCollected, total: totalCandidates })\n : undefined\n\n return { messages, nextCursor, hasMore, totalCandidates }\n }\n\n async resolveContact(input: ResolveContactInput): Promise<ContactHint | null> {\n return emailResolveContact(input)\n }\n}\n\nfunction parseCredentialsOrThrow(value: unknown): ImapCredentials {\n const parsed = imapCredentialsSchema.safeParse(value)\n if (!parsed.success) {\n const first = parsed.error.issues[0]\n throw new Error(`Invalid IMAP credentials: ${first?.message ?? 'unknown validation error'}`)\n }\n return parsed.data\n}\n\nfunction pickRawMimeBuffer(raw: InboundMessage): Buffer {\n const candidate = raw.raw as { rawBody?: unknown; mime?: unknown }\n const value = candidate?.rawBody ?? candidate?.mime ?? raw.raw\n if (Buffer.isBuffer(value)) return value\n if (value instanceof Uint8Array) return Buffer.from(value)\n if (typeof value === 'string') return Buffer.from(value, 'utf-8')\n throw new Error('[internal] IMAP normalizeInbound requires `raw.rawBody` to be a Buffer or string MIME payload')\n}\n\nfunction pickAccountIdentifier(raw: InboundMessage): string {\n const candidate = raw.raw as { accountIdentifier?: unknown }\n const id = typeof candidate?.accountIdentifier === 'string' ? candidate.accountIdentifier : undefined\n return id ?? 'unknown@imap'\n}\n\nfunction pickUid(raw: InboundMessage): number | undefined {\n const candidate = raw.raw as { uid?: unknown }\n return typeof candidate?.uid === 'number' ? candidate.uid : undefined\n}\n\n/**\n * Spec B \u00A7 HARD_CAP. Bound each poll's wall-clock + DB transaction size.\n * Honor the caller's `limit` hint but never exceed `HARD_CAP_MAX`. A\n * single poll will fetch at most this many UIDs; if more remain we set\n * `hasMore: true` and the hub re-enqueues immediately.\n *\n * Configurable via `OM_CHANNEL_IMAP_HARD_CAP_PER_POLL` (default 200).\n */\nfunction clampHardCap(callerLimit: number | undefined): number {\n const envOverride = Number.parseInt(process.env.OM_CHANNEL_IMAP_HARD_CAP_PER_POLL ?? '', 10)\n const HARD_CAP_MAX = Number.isFinite(envOverride) && envOverride > 0 ? envOverride : 200\n if (typeof callerLimit === 'number' && callerLimit > 0) {\n return Math.min(callerLimit, HARD_CAP_MAX)\n }\n return HARD_CAP_MAX\n}\n\ninterface ImportCursor {\n remaining: number[]\n collected: number\n total?: number\n}\n\nfunction encodeImportCursor(cursor: ImportCursor): string {\n return encodeCursor(cursor)\n}\n\nfunction decodeImportCursor(value: string | undefined): ImportCursor | null {\n const parsed = decodeCursor(value)\n if (!parsed || typeof parsed !== 'object') return null\n const obj = parsed as { remaining?: unknown; collected?: unknown; total?: unknown }\n const remaining = Array.isArray(obj.remaining)\n ? obj.remaining.filter((n): n is number => typeof n === 'number' && Number.isFinite(n))\n : []\n const collected = typeof obj.collected === 'number' ? obj.collected : 0\n const total = typeof obj.total === 'number' ? obj.total : undefined\n return { remaining, collected, total }\n}\n\nfunction toNumberOrUndefined(value: unknown): number | undefined {\n if (typeof value === 'number') return value\n if (typeof value === 'string' && value.length > 0) {\n const n = Number(value)\n return Number.isFinite(n) ? n : undefined\n }\n return undefined\n}\n\nlet cachedAdapter: ImapChannelAdapter | null = null\n\nexport function getImapChannelAdapter(): ImapChannelAdapter {\n if (!cachedAdapter) cachedAdapter = new ImapChannelAdapter()\n return cachedAdapter\n}\n\nexport { ImapChannelAdapter }\n"],
|
|
5
|
+
"mappings": "AAoBA,SAAS,wBAAwB;AACjC,SAAS,uBAAuB,8BAA2E;AAC3G;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP,SAAS,+BAA+B;AACxC,SAAS,mCAAmC;AAC5C,SAAS,+BAA+B;AACxC,SAAS,2BAA2B;AACpC,SAAS,cAAc,oBAAoB;AAe3C,MAAM,mBAA6C;AAAA,EAAnD;AACE,SAAS,cAAc;AACvB,SAAS,cAAc;AACvB,SAAS,eAAe;AAAA;AAAA,EAExB,MAAM,YAAY,OAAqD;AACrE,UAAM,cAAc,wBAAwB,MAAM,WAAW;AAS7D,QAAI,MAAM,QAAQ,MAAM,QAAQ,WAAW,KAAK,MAAM,QAAQ,YAAY,SAAS,GAAG;AACpF,aAAO;AAAA,QACL,mBAAmB;AAAA,QACnB,QAAQ;AAAA,QACR,OACE;AAAA,MACJ;AAAA,IACF;AAEA,QAAI;AACJ,QAAI;AACF,eAAS,MAAM,wBAAwB;AAAA,QACrC,MAAM,MAAM,QAAQ,QAAQ,MAAM,QAAQ,QAAQ;AAAA,QAClD,YAAY,MAAM,QAAQ,eAAe,MAAM,QAAQ,OAAO,SAAS;AAAA,QACvE,aAAa,MAAM,QAAQ;AAAA,QAC3B,iBAAiB,MAAM;AAAA,MACzB,C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AAM;AACvC;AAEA,SAAS,oBAAoB,OAAoC;AAC/D,MAAI,OAAO,UAAU,SAAU,QAAO;AACtC,MAAI,OAAO,UAAU,YAAY,MAAM,SAAS,GAAG;AACjD,UAAM,IAAI,OAAO,KAAK;AACtB,WAAO,OAAO,SAAS,CAAC,IAAI,IAAI;AAAA,EAClC;AACA,SAAO;AACT;AAEA,IAAI,gBAA2C;AAExC,SAAS,wBAA4C;AAC1D,MAAI,CAAC,cAAe,iBAAgB,IAAI,mBAAmB;AAC3D,SAAO;AACT;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/modules/channel_imap/lib/capabilities.ts"],
|
|
4
|
+
"sourcesContent": ["import type { ChannelCapabilities } from '@open-mercato/core/modules/communication_channels/lib/adapter'\nimport { baseEmailCapabilities } from '@open-mercato/core/modules/communication_channels/lib/email-capabilities'\n\n/**\n * IMAP+SMTP capabilities. Polling-based (no real-time push), threaded via\n * RFC2822 In-Reply-To / References, rich HTML/plain-text body, inline + regular\n * attachments. No reactions, no edit/delete (only the IMAP `\\Seen` flag, which\n * the user controls locally and is not reliably surfaced).\n *\n * `fileSharing: false` (R2-M4 / F11, 2026-05-26): the adapter's `sendMessage`\n * fails-fast on attachments (it doesn't yet fetch + inline attachment URLs into\n * MIME bodies). Re-enable when URL-fetch + size-validation lands.\n */\nexport const imapCapabilities: ChannelCapabilities = {\n ...baseEmailCapabilities,\n}\n"],
|
|
5
|
+
"mappings": "AACA,SAAS,6BAA6B;AAY/B,MAAM,mBAAwC;AAAA,EACnD,GAAG;AACL;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {
|
|
2
|
+
htmlToText,
|
|
3
|
+
referencesFromMeta,
|
|
4
|
+
sanitizeHeaderValue,
|
|
5
|
+
stringOrUndefined,
|
|
6
|
+
toAddressList
|
|
7
|
+
} from "@open-mercato/core/modules/communication_channels/lib/email-mime";
|
|
8
|
+
async function convertOutboundForEmail(input) {
|
|
9
|
+
const meta = input.channelMetadata ?? {};
|
|
10
|
+
const sanitizeOptionalHeader = (value) => value === void 0 ? void 0 : sanitizeHeaderValue(value);
|
|
11
|
+
const subject = sanitizeOptionalHeader(stringOrUndefined(meta.subject));
|
|
12
|
+
const to = toAddressList(meta.to).map(sanitizeHeaderValue);
|
|
13
|
+
if (to.length === 0) {
|
|
14
|
+
throw new Error("Email outbound conversion requires at least one recipient (channelMetadata.to)");
|
|
15
|
+
}
|
|
16
|
+
const cc = toAddressList(meta.cc).map(sanitizeHeaderValue);
|
|
17
|
+
const bcc = toAddressList(meta.bcc).map(sanitizeHeaderValue);
|
|
18
|
+
const inReplyTo = sanitizeOptionalHeader(stringOrUndefined(meta.inReplyTo));
|
|
19
|
+
const references = referencesFromMeta(meta.references)?.map(sanitizeHeaderValue);
|
|
20
|
+
const messageId = sanitizeOptionalHeader(stringOrUndefined(meta.messageId));
|
|
21
|
+
const html = input.bodyFormat === "html" ? input.body : void 0;
|
|
22
|
+
const text = input.bodyFormat === "html" ? htmlToText(input.body) : input.body;
|
|
23
|
+
const native = {
|
|
24
|
+
content: {
|
|
25
|
+
text,
|
|
26
|
+
html,
|
|
27
|
+
bodyFormat: input.bodyFormat,
|
|
28
|
+
attachments: input.attachments,
|
|
29
|
+
raw: {
|
|
30
|
+
subject,
|
|
31
|
+
to,
|
|
32
|
+
cc,
|
|
33
|
+
bcc,
|
|
34
|
+
inReplyTo,
|
|
35
|
+
references,
|
|
36
|
+
messageId
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
metadata: {
|
|
40
|
+
subject,
|
|
41
|
+
to,
|
|
42
|
+
cc,
|
|
43
|
+
bcc,
|
|
44
|
+
inReplyTo,
|
|
45
|
+
references,
|
|
46
|
+
messageId
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
return native;
|
|
50
|
+
}
|
|
51
|
+
export {
|
|
52
|
+
convertOutboundForEmail
|
|
53
|
+
};
|
|
54
|
+
//# sourceMappingURL=convert-outbound.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/modules/channel_imap/lib/convert-outbound.ts"],
|
|
4
|
+
"sourcesContent": ["import type {\n ChannelNativeContent,\n ConvertOutboundInput,\n} from '@open-mercato/core/modules/communication_channels/lib/adapter'\nimport {\n htmlToText,\n referencesFromMeta,\n sanitizeHeaderValue,\n stringOrUndefined,\n toAddressList,\n} from '@open-mercato/core/modules/communication_channels/lib/email-mime'\n\n/**\n * Convert a hub-canonical outbound payload to an email-shaped `ChannelNativeContent`.\n *\n * Subject and threading headers come from `channelMetadata` populated by the hub:\n * - `subject` (string)\n * - `to` / `cc` / `bcc` (string | string[])\n * - `inReplyTo` (string)\n * - `references` (string[])\n *\n * Body format:\n * - `'html'` keeps the HTML body as-is, derives plain-text via a naive strip.\n * - `'text'` produces text-only.\n * - `'markdown'` is not supported by email; we treat it as text (the hub limits\n * `supportedBodyFormats` to ['text','html'] so this should not occur in practice).\n */\n\nexport async function convertOutboundForEmail(\n input: ConvertOutboundInput,\n): Promise<ChannelNativeContent> {\n const meta = (input.channelMetadata ?? {}) as Record<string, unknown>\n // Defense-in-depth: strip CR/LF/tab from every header-shaped field so a crafted\n // subject or recipient cannot smuggle an extra header (e.g. a hidden Bcc),\n // instead of relying solely on the downstream SMTP composer to neutralize it.\n const sanitizeOptionalHeader = (value: string | undefined): string | undefined =>\n value === undefined ? undefined : sanitizeHeaderValue(value)\n const subject = sanitizeOptionalHeader(stringOrUndefined(meta.subject))\n const to = toAddressList(meta.to).map(sanitizeHeaderValue)\n if (to.length === 0) {\n throw new Error('Email outbound conversion requires at least one recipient (channelMetadata.to)')\n }\n const cc = toAddressList(meta.cc).map(sanitizeHeaderValue)\n const bcc = toAddressList(meta.bcc).map(sanitizeHeaderValue)\n const inReplyTo = sanitizeOptionalHeader(stringOrUndefined(meta.inReplyTo))\n const references = referencesFromMeta(meta.references)?.map(sanitizeHeaderValue)\n const messageId = sanitizeOptionalHeader(stringOrUndefined(meta.messageId))\n\n const html = input.bodyFormat === 'html' ? input.body : undefined\n const text = input.bodyFormat === 'html' ? htmlToText(input.body) : input.body\n\n const native: ChannelNativeContent = {\n content: {\n text,\n html,\n bodyFormat: input.bodyFormat,\n attachments: input.attachments,\n raw: {\n subject,\n to,\n cc,\n bcc,\n inReplyTo,\n references,\n messageId,\n },\n },\n metadata: {\n subject,\n to,\n cc,\n bcc,\n inReplyTo,\n references,\n messageId,\n },\n }\n return native\n}\n"],
|
|
5
|
+
"mappings": "AAIA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAkBP,eAAsB,wBACpB,OAC+B;AAC/B,QAAM,OAAQ,MAAM,mBAAmB,CAAC;AAIxC,QAAM,yBAAyB,CAAC,UAC9B,UAAU,SAAY,SAAY,oBAAoB,KAAK;AAC7D,QAAM,UAAU,uBAAuB,kBAAkB,KAAK,OAAO,CAAC;AACtE,QAAM,KAAK,cAAc,KAAK,EAAE,EAAE,IAAI,mBAAmB;AACzD,MAAI,GAAG,WAAW,GAAG;AACnB,UAAM,IAAI,MAAM,gFAAgF;AAAA,EAClG;AACA,QAAM,KAAK,cAAc,KAAK,EAAE,EAAE,IAAI,mBAAmB;AACzD,QAAM,MAAM,cAAc,KAAK,GAAG,EAAE,IAAI,mBAAmB;AAC3D,QAAM,YAAY,uBAAuB,kBAAkB,KAAK,SAAS,CAAC;AAC1E,QAAM,aAAa,mBAAmB,KAAK,UAAU,GAAG,IAAI,mBAAmB;AAC/E,QAAM,YAAY,uBAAuB,kBAAkB,KAAK,SAAS,CAAC;AAE1E,QAAM,OAAO,MAAM,eAAe,SAAS,MAAM,OAAO;AACxD,QAAM,OAAO,MAAM,eAAe,SAAS,WAAW,MAAM,IAAI,IAAI,MAAM;AAE1E,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