@activeboxes/piece-imap 0.2.10

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/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # pieces-imap
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Running lint
6
+
7
+ Run `nx lint pieces-imap` to execute the lint via [ESLint](https://eslint.org/).
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@activeboxes/piece-imap",
3
+ "version": "0.2.10",
4
+ "dependencies": {
5
+ "@sinclair/typebox": "0.34.11",
6
+ "dayjs": "1.11.9",
7
+ "deepmerge-ts": "7.1.0",
8
+ "imapflow": "1.0.152",
9
+ "mailparser": "3.6.7",
10
+ "nanoid": "3.3.8",
11
+ "semver": "7.6.0",
12
+ "@activeboxes/pieces-framework": "0.7.41",
13
+ "@activeboxes/shared": "0.10.146",
14
+ "tslib": "1.14.1"
15
+ },
16
+ "overrides": {
17
+ "cross-spawn": "7.0.6",
18
+ "@tryfabric/martian": {
19
+ "@notionhq/client": "$@notionhq/client"
20
+ },
21
+ "vite": {
22
+ "rollup": "npm:@rollup/wasm-node"
23
+ }
24
+ },
25
+ "resolutions": {
26
+ "rollup": "npm:@rollup/wasm-node"
27
+ },
28
+ "main": "./src/index.js",
29
+ "type": "commonjs",
30
+ "types": "./src/index.d.ts"
31
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ export declare const imapAuth: import("@activeboxes/pieces-framework").CustomAuthProperty<{
2
+ host: import("@activeboxes/pieces-framework").ShortTextProperty<true>;
3
+ username: import("@activeboxes/pieces-framework").ShortTextProperty<true>;
4
+ password: import("@activeboxes/pieces-framework").SecretTextProperty<true>;
5
+ port: import("@activeboxes/pieces-framework").NumberProperty<true>;
6
+ tls: import("@activeboxes/pieces-framework").CheckboxProperty<true>;
7
+ tlsRejectUnauthorized: import("@activeboxes/pieces-framework").CheckboxProperty<false>;
8
+ }>;
9
+ export declare const imapPiece: import("@activeboxes/pieces-framework").Piece<import("@activeboxes/pieces-framework").CustomAuthProperty<{
10
+ host: import("@activeboxes/pieces-framework").ShortTextProperty<true>;
11
+ username: import("@activeboxes/pieces-framework").ShortTextProperty<true>;
12
+ password: import("@activeboxes/pieces-framework").SecretTextProperty<true>;
13
+ port: import("@activeboxes/pieces-framework").NumberProperty<true>;
14
+ tls: import("@activeboxes/pieces-framework").CheckboxProperty<true>;
15
+ tlsRejectUnauthorized: import("@activeboxes/pieces-framework").CheckboxProperty<false>;
16
+ }>>;
package/src/index.js ADDED
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.imapPiece = exports.imapAuth = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const pieces_framework_1 = require("@activeboxes/pieces-framework");
6
+ const shared_1 = require("@activeboxes/shared");
7
+ const imapflow_1 = require("imapflow");
8
+ const common_1 = require("./lib/common");
9
+ const new_email_1 = require("./lib/triggers/new-email");
10
+ const description = `
11
+ **Gmail Users:**
12
+ <br><br>
13
+ Make Sure of the following:
14
+ <br>
15
+ * IMAP is enabled in your Gmail settings (https://support.google.com/mail/answer/7126229?hl=en)
16
+ * You have created an App Password to login with (https://support.google.com/accounts/answer/185833?hl=en)
17
+ * Enable TLS and set the port to 993 and the host to imap.gmail.com
18
+ `;
19
+ exports.imapAuth = pieces_framework_1.PieceAuth.CustomAuth({
20
+ description: description,
21
+ props: {
22
+ host: pieces_framework_1.Property.ShortText({
23
+ displayName: 'Host',
24
+ required: true,
25
+ }),
26
+ username: pieces_framework_1.Property.ShortText({
27
+ displayName: 'Username',
28
+ required: true,
29
+ }),
30
+ password: pieces_framework_1.PieceAuth.SecretText({
31
+ displayName: 'Password',
32
+ required: true,
33
+ }),
34
+ port: pieces_framework_1.Property.Number({
35
+ displayName: 'Port',
36
+ required: true,
37
+ defaultValue: 143,
38
+ }),
39
+ tls: pieces_framework_1.Property.Checkbox({
40
+ displayName: 'Use TLS',
41
+ defaultValue: false,
42
+ required: true,
43
+ }),
44
+ tlsRejectUnauthorized: pieces_framework_1.Property.Checkbox({
45
+ displayName: 'Require valid SSL certificate',
46
+ defaultValue: true,
47
+ required: false,
48
+ }),
49
+ },
50
+ validate: (_a) => tslib_1.__awaiter(void 0, [_a], void 0, function* ({ auth }) {
51
+ const imapConfig = common_1.imapCommon.constructConfig(auth);
52
+ const imapClient = new imapflow_1.ImapFlow(Object.assign(Object.assign({}, imapConfig), { logger: false }));
53
+ try {
54
+ yield imapClient.connect();
55
+ yield imapClient.noop();
56
+ return { valid: true };
57
+ }
58
+ catch (e) {
59
+ return {
60
+ valid: false,
61
+ error: JSON.stringify(e),
62
+ };
63
+ }
64
+ finally {
65
+ yield imapClient.logout();
66
+ }
67
+ }),
68
+ required: true,
69
+ });
70
+ exports.imapPiece = (0, pieces_framework_1.createPiece)({
71
+ displayName: 'IMAP',
72
+ description: 'Receive new email trigger',
73
+ minimumSupportedRelease: '0.30.0',
74
+ logoUrl: 'https://cdn.activeboxes.org/pieces/imap.png',
75
+ categories: [shared_1.PieceCategory.BUSINESS_INTELLIGENCE],
76
+ authors: ["kishanprmr", "MoShizzle", "khaledmashaly", "abuaboud"],
77
+ auth: exports.imapAuth,
78
+ actions: [],
79
+ triggers: [new_email_1.newEmail],
80
+ });
81
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/pieces/community/imap/src/index.ts"],"names":[],"mappings":";;;;AAAA,oEAIuC;AACvC,gDAAoD;AACpD,uCAAoC;AACpC,yCAA0C;AAC1C,wDAAoD;AAEpD,MAAM,WAAW,GAAG;;;;;;;;CAQnB,CAAC;AACW,QAAA,QAAQ,GAAG,4BAAS,CAAC,UAAU,CAAC;IAC3C,WAAW,EAAE,WAAW;IACxB,KAAK,EAAE;QACL,IAAI,EAAE,2BAAQ,CAAC,SAAS,CAAC;YACvB,WAAW,EAAE,MAA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IACH,CAAC,CAAA;IACD,QAAQ,EAAE,IAAI;CACf,CAAC,CAAC;AAEU,QAAA,SAAS,GAAG,IAAA,8BAAW,EAAC;IACnC,WAAW,EAAE,MAAM;IACnB,WAAW,EAAE,2BAA2B;IACxC,uBAAuB,EAAE,QAAQ;IACjC,OAAO,EAAE,6CAA6C;IACtD,UAAU,EAAE,CAAC,sBAAa,CAAC,qBAAqB,CAAC;IACjD,OAAO,EAAE,CAAC,YAAY,EAAC,WAAW,EAAC,eAAe,EAAC,UAAU,CAAC;IAC9D,IAAI,EAAE,gBAAQ;IACd,OAAO,EAAE,EAAE;IACX,QAAQ,EAAE,CAAC,oBAAQ,CAAC;CACrB,CAAC,CAAC"}
@@ -0,0 +1,35 @@
1
+ import { FilesService, PiecePropValueSchema } from '@activeboxes/pieces-framework';
2
+ import { imapAuth } from '../../';
3
+ import { Attachment, ParsedMail } from 'mailparser';
4
+ export declare const imapCommon: {
5
+ constructConfig(auth: {
6
+ host: string;
7
+ username: string;
8
+ password: string;
9
+ port: number;
10
+ tls: boolean;
11
+ tlsRejectUnauthorized: boolean | undefined;
12
+ }): {
13
+ host: string;
14
+ port: number;
15
+ secure: boolean;
16
+ auth: {
17
+ user: string;
18
+ pass: string;
19
+ };
20
+ tls: {
21
+ rejectUnauthorized: boolean;
22
+ };
23
+ };
24
+ mailbox: import("@activeboxes/pieces-framework").DropdownProperty<string, true>;
25
+ fetchEmails({ auth, lastEpochMilliSeconds, mailbox, }: {
26
+ auth: PiecePropValueSchema<typeof imapAuth>;
27
+ lastEpochMilliSeconds: number;
28
+ mailbox: string;
29
+ files: FilesService;
30
+ }): Promise<{
31
+ data: ParsedMail;
32
+ epochMilliSeconds: number;
33
+ }[]>;
34
+ };
35
+ export declare function convertAttachment(attachments: Attachment[], files: FilesService): Promise<string[]>;
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.imapCommon = void 0;
4
+ exports.convertAttachment = convertAttachment;
5
+ const tslib_1 = require("tslib");
6
+ const pieces_framework_1 = require("@activeboxes/pieces-framework");
7
+ const imapflow_1 = require("imapflow");
8
+ const dayjs_1 = tslib_1.__importDefault(require("dayjs"));
9
+ const mailparser_1 = require("mailparser");
10
+ exports.imapCommon = {
11
+ constructConfig(auth) {
12
+ return {
13
+ host: auth.host,
14
+ port: auth.port,
15
+ secure: auth.tls,
16
+ auth: { user: auth.username, pass: auth.password },
17
+ tls: { rejectUnauthorized: !!auth.tlsRejectUnauthorized, },
18
+ };
19
+ },
20
+ mailbox: pieces_framework_1.Property.Dropdown({
21
+ displayName: 'Mailbox',
22
+ description: 'Select the mailbox to search',
23
+ required: true,
24
+ refreshers: [],
25
+ options: (_a) => tslib_1.__awaiter(void 0, [_a], void 0, function* ({ auth }) {
26
+ const imapConfig = exports.imapCommon.constructConfig(auth);
27
+ let options = [];
28
+ const imapClient = new imapflow_1.ImapFlow(Object.assign(Object.assign({}, imapConfig), { logger: false }));
29
+ try {
30
+ yield imapClient.connect();
31
+ const mailBoxList = yield imapClient.list();
32
+ options = mailBoxList.map((mailbox) => {
33
+ return {
34
+ label: mailbox.name,
35
+ value: mailbox.path,
36
+ };
37
+ });
38
+ }
39
+ finally {
40
+ yield imapClient.logout();
41
+ }
42
+ return {
43
+ disabled: false,
44
+ options: options,
45
+ };
46
+ }),
47
+ }),
48
+ fetchEmails(_a) {
49
+ return tslib_1.__awaiter(this, arguments, void 0, function* ({ auth, lastEpochMilliSeconds, mailbox, }) {
50
+ var _b, e_1, _c, _d;
51
+ const imapConfig = exports.imapCommon.constructConfig(auth);
52
+ const imapClient = new imapflow_1.ImapFlow(Object.assign(Object.assign({}, imapConfig), { logger: false }));
53
+ yield imapClient.connect();
54
+ const lock = yield imapClient.getMailboxLock(mailbox);
55
+ try {
56
+ const res = imapClient.fetch({
57
+ since: lastEpochMilliSeconds === 0
58
+ ? (0, dayjs_1.default)().subtract(2, 'hour').toISOString()
59
+ : (0, dayjs_1.default)(lastEpochMilliSeconds).toISOString(),
60
+ }, {
61
+ source: true,
62
+ });
63
+ const messages = [];
64
+ try {
65
+ for (var _e = true, res_1 = tslib_1.__asyncValues(res), res_1_1; res_1_1 = yield res_1.next(), _b = res_1_1.done, !_b; _e = true) {
66
+ _d = res_1_1.value;
67
+ _e = false;
68
+ const message = _d;
69
+ const castedItem = yield parseStream(message.source);
70
+ messages.push({
71
+ data: castedItem,
72
+ epochMilliSeconds: (0, dayjs_1.default)(castedItem.date).valueOf(),
73
+ });
74
+ }
75
+ }
76
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
77
+ finally {
78
+ try {
79
+ if (!_e && !_b && (_c = res_1.return)) yield _c.call(res_1);
80
+ }
81
+ finally { if (e_1) throw e_1.error; }
82
+ }
83
+ return messages;
84
+ }
85
+ finally {
86
+ lock.release();
87
+ yield imapClient.logout();
88
+ }
89
+ });
90
+ },
91
+ };
92
+ function convertAttachment(attachments, files) {
93
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
94
+ const promises = attachments.map((attachment) => tslib_1.__awaiter(this, void 0, void 0, function* () {
95
+ var _a;
96
+ return files.write({
97
+ fileName: (_a = attachment.filename) !== null && _a !== void 0 ? _a : `attachment-${Date.now()}`,
98
+ data: attachment.content,
99
+ });
100
+ }));
101
+ return Promise.all(promises);
102
+ });
103
+ }
104
+ function parseStream(stream) {
105
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
106
+ return new Promise((resolve, reject) => {
107
+ (0, mailparser_1.simpleParser)(stream, (err, parsed) => {
108
+ if (err) {
109
+ reject(err);
110
+ }
111
+ else {
112
+ resolve(parsed);
113
+ }
114
+ });
115
+ });
116
+ });
117
+ }
118
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../../packages/pieces/community/imap/src/lib/common/index.ts"],"names":[],"mappings":";;;AAqHA,8CAWC;;AAhID,oEAIuC;AACvC,uCAAoC;AAGpC,0DAA0B;AAC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,CAAC;YAC5B,CAAC;QACH,CAAC;KAAA;CACF,CAAC;AAEF,SAAsB,iBAAiB,CACrC,WAAyB,EACzB,KAAmB;;QAEnB,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,CAAO,UAAU,EAAE,EAAE;;YACpD,OAAO,KAAK,CAAC,KAAK,CAAC;gBACjB,QAAQ,EAAE,MAAA,UAAU,CAAC,QAAQ,mCAAI,cAAc,IAAI,CAAC,GAAG,EAAE,EAAE;gBAC3D,IAAI,EAAE,UAAU,CAAC,OAAO;aACzB,CAAC,CAAC;QACL,CAAC,CAAA,CAAC,CAAC;QACH,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC/B,CAAC;CAAA;AAED,SAAe,WAAW,CAAC,MAAW;;QACpC,OAAO,IAAI,OAAO,CAAa,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACjD,IAAA,yBAAY,EAAC,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE;gBACnC,IAAI,GAAG,EAAE,CAAC;oBACR,MAAM,CAAC,GAAG,CAAC,CAAC;gBACd,CAAC;qBAAM,CAAC;oBACN,OAAO,CAAC,MAAM,CAAC,CAAC;gBAClB,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CAAA"}
@@ -0,0 +1,32 @@
1
+ import { TriggerStrategy } from '@activeboxes/pieces-framework';
2
+ export declare const newEmail: import("@activeboxes/pieces-framework").ITrigger<TriggerStrategy.WEBHOOK, import("@activeboxes/pieces-framework").CustomAuthProperty<{
3
+ host: import("@activeboxes/pieces-framework").ShortTextProperty<true>;
4
+ username: import("@activeboxes/pieces-framework").ShortTextProperty<true>;
5
+ password: import("@activeboxes/pieces-framework").SecretTextProperty<true>;
6
+ port: import("@activeboxes/pieces-framework").NumberProperty<true>;
7
+ tls: import("@activeboxes/pieces-framework").CheckboxProperty<true>;
8
+ tlsRejectUnauthorized: import("@activeboxes/pieces-framework").CheckboxProperty<false>;
9
+ }>, {
10
+ mailbox: import("@activeboxes/pieces-framework").DropdownProperty<string, true>;
11
+ filterInstructions: import("dist/packages/pieces/community/framework/src/lib/property/input/markdown-property").MarkDownProperty;
12
+ }> | import("@activeboxes/pieces-framework").ITrigger<TriggerStrategy.POLLING, import("@activeboxes/pieces-framework").CustomAuthProperty<{
13
+ host: import("@activeboxes/pieces-framework").ShortTextProperty<true>;
14
+ username: import("@activeboxes/pieces-framework").ShortTextProperty<true>;
15
+ password: import("@activeboxes/pieces-framework").SecretTextProperty<true>;
16
+ port: import("@activeboxes/pieces-framework").NumberProperty<true>;
17
+ tls: import("@activeboxes/pieces-framework").CheckboxProperty<true>;
18
+ tlsRejectUnauthorized: import("@activeboxes/pieces-framework").CheckboxProperty<false>;
19
+ }>, {
20
+ mailbox: import("@activeboxes/pieces-framework").DropdownProperty<string, true>;
21
+ filterInstructions: import("dist/packages/pieces/community/framework/src/lib/property/input/markdown-property").MarkDownProperty;
22
+ }> | import("@activeboxes/pieces-framework").ITrigger<TriggerStrategy.APP_WEBHOOK, import("@activeboxes/pieces-framework").CustomAuthProperty<{
23
+ host: import("@activeboxes/pieces-framework").ShortTextProperty<true>;
24
+ username: import("@activeboxes/pieces-framework").ShortTextProperty<true>;
25
+ password: import("@activeboxes/pieces-framework").SecretTextProperty<true>;
26
+ port: import("@activeboxes/pieces-framework").NumberProperty<true>;
27
+ tls: import("@activeboxes/pieces-framework").CheckboxProperty<true>;
28
+ tlsRejectUnauthorized: import("@activeboxes/pieces-framework").CheckboxProperty<false>;
29
+ }>, {
30
+ mailbox: import("@activeboxes/pieces-framework").DropdownProperty<string, true>;
31
+ filterInstructions: import("dist/packages/pieces/community/framework/src/lib/property/input/markdown-property").MarkDownProperty;
32
+ }>;
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.newEmail = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const pieces_framework_1 = require("@activeboxes/pieces-framework");
6
+ const __1 = require("../..");
7
+ const common_1 = require("../common");
8
+ const filterInstructions = `
9
+ **Filter Emails:**
10
+
11
+ You can add Branch Piece to filter emails based on the subject, to, from, cc or other fields.
12
+ `;
13
+ exports.newEmail = (0, pieces_framework_1.createTrigger)({
14
+ auth: __1.imapAuth,
15
+ name: 'new_email',
16
+ displayName: 'New Email',
17
+ description: 'Trigger when a new email is received.',
18
+ props: {
19
+ mailbox: common_1.imapCommon.mailbox,
20
+ filterInstructions: pieces_framework_1.Property.MarkDown({
21
+ value: filterInstructions,
22
+ }),
23
+ },
24
+ type: pieces_framework_1.TriggerStrategy.POLLING,
25
+ onEnable: (context) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
26
+ yield context.store.put('lastPoll', Date.now());
27
+ }),
28
+ onDisable: (context) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
29
+ yield context.store.delete('lastPoll');
30
+ return;
31
+ }),
32
+ run: (context) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
33
+ var _a;
34
+ const { auth, store, propsValue, files } = context;
35
+ const mailbox = propsValue.mailbox;
36
+ const lastEpochMilliSeconds = (_a = (yield store.get('lastPoll'))) !== null && _a !== void 0 ? _a : 0;
37
+ const items = yield common_1.imapCommon.fetchEmails({
38
+ auth,
39
+ lastEpochMilliSeconds,
40
+ mailbox,
41
+ files,
42
+ });
43
+ const newLastEpochMilliSeconds = items.reduce((acc, item) => Math.max(acc, item.epochMilliSeconds), lastEpochMilliSeconds);
44
+ yield store.put('lastPoll', newLastEpochMilliSeconds);
45
+ const filteredEmail = items
46
+ .filter((f) => f.epochMilliSeconds > lastEpochMilliSeconds);
47
+ return enrichAttachments(filteredEmail, files);
48
+ }),
49
+ test: (context) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
50
+ const { auth, propsValue, files } = context;
51
+ const mailbox = propsValue.mailbox;
52
+ const lastEpochMilliSeconds = 0;
53
+ const items = yield common_1.imapCommon.fetchEmails({
54
+ auth,
55
+ lastEpochMilliSeconds,
56
+ mailbox,
57
+ files,
58
+ });
59
+ const filteredEmails = getFirstFiveOrAll(items);
60
+ return enrichAttachments(filteredEmails, files);
61
+ }),
62
+ sampleData: {
63
+ html: 'My email body',
64
+ text: 'My email body',
65
+ textAsHtml: '<p>My email body</p>',
66
+ subject: 'Email Subject',
67
+ date: '2023-06-18T11:30:09.000Z',
68
+ to: {
69
+ value: [
70
+ {
71
+ address: 'email@address.com',
72
+ name: 'Name',
73
+ },
74
+ ],
75
+ },
76
+ from: {
77
+ value: [
78
+ {
79
+ address: 'email@address.com',
80
+ name: 'Name',
81
+ },
82
+ ],
83
+ },
84
+ cc: {
85
+ value: [
86
+ {
87
+ address: 'email@address.com',
88
+ name: 'Name',
89
+ },
90
+ ],
91
+ },
92
+ messageId: '<CxE49ifJT5YZN9OE2O6j6Ef+BYgkKWq7X-deg483GkM1ui1xj3g@mail.gmail.com>',
93
+ },
94
+ });
95
+ function enrichAttachments(item, files) {
96
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
97
+ return Promise.all(item.map((item) => tslib_1.__awaiter(this, void 0, void 0, function* () {
98
+ const _a = item.data, { attachments } = _a, rest = tslib_1.__rest(_a, ["attachments"]);
99
+ return {
100
+ data: Object.assign({}, rest),
101
+ epochMilliSeconds: item.epochMilliSeconds,
102
+ attachments: yield (0, common_1.convertAttachment)(item.data.attachments, files),
103
+ };
104
+ })));
105
+ });
106
+ }
107
+ function getFirstFiveOrAll(array) {
108
+ if (array.length <= 5) {
109
+ return array;
110
+ }
111
+ else {
112
+ return array.slice(0, 5);
113
+ }
114
+ }
115
+ //# sourceMappingURL=new-email.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"new-email.js","sourceRoot":"","sources":["../../../../../../../../packages/pieces/community/imap/src/lib/triggers/new-email.ts"],"names":[],"mappings":";;;;AAAA,oEAKuC;AACvC,6BAAiC;AACjC,sCAA0D;AAG1D,MAAM,kBAAkB,GAAG;;;;CAI1B,CAAC;A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