@sayedhfatimi/convex-email 0.0.0-stage → 0.1.1
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/LICENSE +201 -0
- package/README.md +709 -2
- package/dist/adapters/index.d.ts +2 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/index.js +7 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/registry.d.ts +9 -0
- package/dist/adapters/registry.d.ts.map +1 -0
- package/dist/adapters/registry.js +20 -0
- package/dist/adapters/registry.js.map +1 -0
- package/dist/adapters/resend/inbound.d.ts +5 -0
- package/dist/adapters/resend/inbound.d.ts.map +1 -0
- package/dist/adapters/resend/inbound.js +207 -0
- package/dist/adapters/resend/inbound.js.map +1 -0
- package/dist/adapters/resend/index.d.ts +4 -0
- package/dist/adapters/resend/index.d.ts.map +1 -0
- package/dist/adapters/resend/index.js +27 -0
- package/dist/adapters/resend/index.js.map +1 -0
- package/dist/adapters/resend/parse.d.ts +3 -0
- package/dist/adapters/resend/parse.d.ts.map +1 -0
- package/dist/adapters/resend/parse.js +94 -0
- package/dist/adapters/resend/parse.js.map +1 -0
- package/dist/adapters/resend/send.d.ts +15 -0
- package/dist/adapters/resend/send.d.ts.map +1 -0
- package/dist/adapters/resend/send.js +117 -0
- package/dist/adapters/resend/send.js.map +1 -0
- package/dist/adapters/resend/webhook.d.ts +6 -0
- package/dist/adapters/resend/webhook.d.ts.map +1 -0
- package/dist/adapters/resend/webhook.js +66 -0
- package/dist/adapters/resend/webhook.js.map +1 -0
- package/dist/adapters/ses/inbound.d.ts +3 -0
- package/dist/adapters/ses/inbound.d.ts.map +1 -0
- package/dist/adapters/ses/inbound.js +52 -0
- package/dist/adapters/ses/inbound.js.map +1 -0
- package/dist/adapters/ses/index.d.ts +4 -0
- package/dist/adapters/ses/index.d.ts.map +1 -0
- package/dist/adapters/ses/index.js +28 -0
- package/dist/adapters/ses/index.js.map +1 -0
- package/dist/adapters/ses/parse.d.ts +7 -0
- package/dist/adapters/ses/parse.d.ts.map +1 -0
- package/dist/adapters/ses/parse.js +209 -0
- package/dist/adapters/ses/parse.js.map +1 -0
- package/dist/adapters/ses/send.d.ts +10 -0
- package/dist/adapters/ses/send.d.ts.map +1 -0
- package/dist/adapters/ses/send.js +179 -0
- package/dist/adapters/ses/send.js.map +1 -0
- package/dist/adapters/ses/sigv4.d.ts +19 -0
- package/dist/adapters/ses/sigv4.d.ts.map +1 -0
- package/dist/adapters/ses/sigv4.js +50 -0
- package/dist/adapters/ses/sigv4.js.map +1 -0
- package/dist/adapters/ses/sns.d.ts +23 -0
- package/dist/adapters/ses/sns.d.ts.map +1 -0
- package/dist/adapters/ses/sns.js +281 -0
- package/dist/adapters/ses/sns.js.map +1 -0
- package/dist/adapters/types.d.ts +11 -0
- package/dist/adapters/types.d.ts.map +1 -0
- package/dist/adapters/types.js +2 -0
- package/dist/adapters/types.js.map +1 -0
- package/dist/client/index.d.ts +214 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +304 -0
- package/dist/client/index.js.map +1 -0
- package/dist/component/_generated/api.d.ts +62 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +345 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +133 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +80 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/attachments.d.ts +21 -0
- package/dist/component/attachments.d.ts.map +1 -0
- package/dist/component/attachments.js +66 -0
- package/dist/component/attachments.js.map +1 -0
- package/dist/component/cleanup.d.ts +15 -0
- package/dist/component/cleanup.d.ts.map +1 -0
- package/dist/component/cleanup.js +152 -0
- package/dist/component/cleanup.js.map +1 -0
- package/dist/component/config.d.ts +34 -0
- package/dist/component/config.d.ts.map +1 -0
- package/dist/component/config.js +33 -0
- package/dist/component/config.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +8 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/credentials.d.ts +19 -0
- package/dist/component/credentials.d.ts.map +1 -0
- package/dist/component/credentials.js +45 -0
- package/dist/component/credentials.js.map +1 -0
- package/dist/component/deliver.d.ts +58 -0
- package/dist/component/deliver.d.ts.map +1 -0
- package/dist/component/deliver.js +328 -0
- package/dist/component/deliver.js.map +1 -0
- package/dist/component/events.d.ts +15 -0
- package/dist/component/events.d.ts.map +1 -0
- package/dist/component/events.js +141 -0
- package/dist/component/events.js.map +1 -0
- package/dist/component/inbound.d.ts +354 -0
- package/dist/component/inbound.d.ts.map +1 -0
- package/dist/component/inbound.js +933 -0
- package/dist/component/inbound.js.map +1 -0
- package/dist/component/queries.d.ts +76 -0
- package/dist/component/queries.d.ts.map +1 -0
- package/dist/component/queries.js +175 -0
- package/dist/component/queries.js.map +1 -0
- package/dist/component/schema.d.ts +473 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +185 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/send.d.ts +112 -0
- package/dist/component/send.d.ts.map +1 -0
- package/dist/component/send.js +393 -0
- package/dist/component/send.js.map +1 -0
- package/dist/component/status.d.ts +2 -0
- package/dist/component/status.d.ts.map +1 -0
- package/dist/component/status.js +8 -0
- package/dist/component/status.js.map +1 -0
- package/dist/component/suppression.d.ts +36 -0
- package/dist/component/suppression.d.ts.map +1 -0
- package/dist/component/suppression.js +89 -0
- package/dist/component/suppression.js.map +1 -0
- package/dist/component/unsubscribe.d.ts +58 -0
- package/dist/component/unsubscribe.d.ts.map +1 -0
- package/dist/component/unsubscribe.js +129 -0
- package/dist/component/unsubscribe.js.map +1 -0
- package/dist/component/webhooks.d.ts +36 -0
- package/dist/component/webhooks.d.ts.map +1 -0
- package/dist/component/webhooks.js +71 -0
- package/dist/component/webhooks.js.map +1 -0
- package/dist/shared/address.d.ts +9 -0
- package/dist/shared/address.d.ts.map +1 -0
- package/dist/shared/address.js +76 -0
- package/dist/shared/address.js.map +1 -0
- package/dist/shared/authResults.d.ts +15 -0
- package/dist/shared/authResults.d.ts.map +1 -0
- package/dist/shared/authResults.js +86 -0
- package/dist/shared/authResults.js.map +1 -0
- package/dist/shared/htmlToText.d.ts +13 -0
- package/dist/shared/htmlToText.d.ts.map +1 -0
- package/dist/shared/htmlToText.js +221 -0
- package/dist/shared/htmlToText.js.map +1 -0
- package/dist/shared/mime.d.ts +4 -0
- package/dist/shared/mime.d.ts.map +1 -0
- package/dist/shared/mime.js +63 -0
- package/dist/shared/mime.js.map +1 -0
- package/dist/shared/readCapped.d.ts +7 -0
- package/dist/shared/readCapped.d.ts.map +1 -0
- package/dist/shared/readCapped.js +38 -0
- package/dist/shared/readCapped.js.map +1 -0
- package/dist/shared/sanitize.d.ts +10 -0
- package/dist/shared/sanitize.d.ts.map +1 -0
- package/dist/shared/sanitize.js +68 -0
- package/dist/shared/sanitize.js.map +1 -0
- package/dist/shared/threading.d.ts +6 -0
- package/dist/shared/threading.d.ts.map +1 -0
- package/dist/shared/threading.js +23 -0
- package/dist/shared/threading.js.map +1 -0
- package/dist/shared/types.d.ts +260 -0
- package/dist/shared/types.d.ts.map +1 -0
- package/dist/shared/types.js +2 -0
- package/dist/shared/types.js.map +1 -0
- package/dist/shared/unsubToken.d.ts +15 -0
- package/dist/shared/unsubToken.d.ts.map +1 -0
- package/dist/shared/unsubToken.js +83 -0
- package/dist/shared/unsubToken.js.map +1 -0
- package/dist/shared/validators.d.ts +760 -0
- package/dist/shared/validators.d.ts.map +1 -0
- package/dist/shared/validators.js +200 -0
- package/dist/shared/validators.js.map +1 -0
- package/package.json +88 -4
- package/src/adapters/index.ts +8 -0
- package/src/adapters/registry.ts +28 -0
- package/src/adapters/resend/inbound.ts +245 -0
- package/src/adapters/resend/index.ts +29 -0
- package/src/adapters/resend/parse.ts +118 -0
- package/src/adapters/resend/send.ts +125 -0
- package/src/adapters/resend/webhook.ts +77 -0
- package/src/adapters/ses/inbound.ts +63 -0
- package/src/adapters/ses/index.ts +30 -0
- package/src/adapters/ses/parse.ts +236 -0
- package/src/adapters/ses/send.ts +206 -0
- package/src/adapters/ses/sigv4.ts +56 -0
- package/src/adapters/ses/sns.ts +321 -0
- package/src/adapters/types.ts +27 -0
- package/src/client/index.ts +549 -0
- package/src/component/_generated/api.ts +78 -0
- package/src/component/_generated/component.ts +475 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +169 -0
- package/src/component/attachments.ts +69 -0
- package/src/component/cleanup.ts +173 -0
- package/src/component/config.ts +52 -0
- package/src/component/convex.config.ts +9 -0
- package/src/component/credentials.ts +63 -0
- package/src/component/deliver.ts +380 -0
- package/src/component/events.ts +179 -0
- package/src/component/inbound.ts +1107 -0
- package/src/component/queries.ts +203 -0
- package/src/component/schema.ts +222 -0
- package/src/component/send.ts +467 -0
- package/src/component/status.ts +8 -0
- package/src/component/suppression.ts +114 -0
- package/src/component/unsubscribe.ts +154 -0
- package/src/component/webhooks.ts +84 -0
- package/src/shared/address.ts +75 -0
- package/src/shared/authResults.ts +94 -0
- package/src/shared/htmlToText.ts +218 -0
- package/src/shared/mime.ts +80 -0
- package/src/shared/readCapped.ts +39 -0
- package/src/shared/sanitize.ts +73 -0
- package/src/shared/threading.ts +26 -0
- package/src/shared/types.ts +254 -0
- package/src/shared/unsubToken.ts +95 -0
- package/src/shared/validators.ts +282 -0
- package/src/test.ts +25 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { vDisposition } from "../shared/validators.js";
|
|
3
|
+
import { mutation } from "./_generated/server.js";
|
|
4
|
+
|
|
5
|
+
/** Store bytes in component file storage and register them as an attachment. */
|
|
6
|
+
export const store = mutation({
|
|
7
|
+
args: {
|
|
8
|
+
bytes: v.bytes(),
|
|
9
|
+
filename: v.string(),
|
|
10
|
+
contentType: v.string(),
|
|
11
|
+
contentId: v.optional(v.string()),
|
|
12
|
+
disposition: v.optional(vDisposition),
|
|
13
|
+
tenant: v.optional(v.string()),
|
|
14
|
+
},
|
|
15
|
+
returns: v.id("attachments"),
|
|
16
|
+
handler: async (ctx, args) => {
|
|
17
|
+
const storageId = await ctx.storage.store(
|
|
18
|
+
new Blob([args.bytes], { type: args.contentType }),
|
|
19
|
+
);
|
|
20
|
+
return await ctx.db.insert("attachments", {
|
|
21
|
+
storageId,
|
|
22
|
+
filename: args.filename,
|
|
23
|
+
contentType: args.contentType,
|
|
24
|
+
size: args.bytes.byteLength,
|
|
25
|
+
tenant: args.tenant,
|
|
26
|
+
contentId: args.contentId,
|
|
27
|
+
disposition: args.disposition ?? "attachment",
|
|
28
|
+
});
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
/** A one-time upload URL for bytes the app would rather not pass through a mutation. */
|
|
33
|
+
export const uploadUrl = mutation({
|
|
34
|
+
args: {},
|
|
35
|
+
returns: v.string(),
|
|
36
|
+
handler: async (ctx) => await ctx.storage.generateUploadUrl(),
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
/** Register a file uploaded through `uploadUrl`. */
|
|
40
|
+
export const register = mutation({
|
|
41
|
+
args: {
|
|
42
|
+
storageId: v.id("_storage"),
|
|
43
|
+
filename: v.string(),
|
|
44
|
+
contentType: v.optional(v.string()),
|
|
45
|
+
contentId: v.optional(v.string()),
|
|
46
|
+
disposition: v.optional(vDisposition),
|
|
47
|
+
tenant: v.optional(v.string()),
|
|
48
|
+
},
|
|
49
|
+
returns: v.id("attachments"),
|
|
50
|
+
handler: async (ctx, args) => {
|
|
51
|
+
const meta = await ctx.db.system.get("_storage", args.storageId);
|
|
52
|
+
if (!meta) throw new Error("attachment: no such stored file");
|
|
53
|
+
const taken = await ctx.db
|
|
54
|
+
.query("attachments")
|
|
55
|
+
.withIndex("by_storageId", (q) => q.eq("storageId", args.storageId))
|
|
56
|
+
.first();
|
|
57
|
+
if (taken) throw new Error("attachment: stored file already registered");
|
|
58
|
+
return await ctx.db.insert("attachments", {
|
|
59
|
+
storageId: args.storageId,
|
|
60
|
+
filename: args.filename,
|
|
61
|
+
contentType:
|
|
62
|
+
args.contentType ?? meta.contentType ?? "application/octet-stream",
|
|
63
|
+
size: meta.size,
|
|
64
|
+
tenant: args.tenant,
|
|
65
|
+
contentId: args.contentId,
|
|
66
|
+
disposition: args.disposition ?? "attachment",
|
|
67
|
+
});
|
|
68
|
+
},
|
|
69
|
+
});
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import type { Doc } from "./_generated/dataModel.js";
|
|
3
|
+
import { type MutationCtx, mutation } from "./_generated/server.js";
|
|
4
|
+
|
|
5
|
+
/** Messages (and, separately, loose attachments) handled per call. */
|
|
6
|
+
const BATCH = 200;
|
|
7
|
+
/** Attachment rows deleted per call, across all messages. */
|
|
8
|
+
const ATTACHMENT_BUDGET = 1000;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Outbound statuses whose bodies may go. `queued` and `sending` still need
|
|
12
|
+
* theirs; `received` is inbound, which is driven from its `inbound` row.
|
|
13
|
+
* Scanning these through an index (never filtering after the read) means a
|
|
14
|
+
* pile of ineligible rows can never starve the rows behind them.
|
|
15
|
+
*/
|
|
16
|
+
const PURGEABLE: Doc<"messages">["status"][] = [
|
|
17
|
+
"test",
|
|
18
|
+
"suppressed",
|
|
19
|
+
"sent",
|
|
20
|
+
"delivery_delayed",
|
|
21
|
+
"delivered",
|
|
22
|
+
"bounced",
|
|
23
|
+
"complained",
|
|
24
|
+
"failed",
|
|
25
|
+
"rejected",
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
type Budget = { messages: number; attachments: number };
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Remove stored bodies and attachment files of messages older than
|
|
32
|
+
* `olderThanMs`, keeping each message row and its events. Inbound mail is
|
|
33
|
+
* only purged once the app has handled it (and its kept raw MIME goes with
|
|
34
|
+
* it). Attachments stored but never linked to a message and older than the
|
|
35
|
+
* cut-off go too. Bounded per call: when `more` is true, call it again.
|
|
36
|
+
* `removed` counts stored files deleted.
|
|
37
|
+
*/
|
|
38
|
+
export const run = mutation({
|
|
39
|
+
args: { olderThanMs: v.number() },
|
|
40
|
+
returns: v.object({ removed: v.number(), more: v.boolean() }),
|
|
41
|
+
handler: async (ctx, { olderThanMs }) => {
|
|
42
|
+
if (!Number.isFinite(olderThanMs) || olderThanMs < 0) {
|
|
43
|
+
throw new Error("olderThanMs must be a non-negative number");
|
|
44
|
+
}
|
|
45
|
+
const now = Date.now();
|
|
46
|
+
const cutoff = now - olderThanMs;
|
|
47
|
+
const budget: Budget = { messages: BATCH, attachments: ATTACHMENT_BUDGET };
|
|
48
|
+
let removed = 0;
|
|
49
|
+
let more = false;
|
|
50
|
+
|
|
51
|
+
// Outbound.
|
|
52
|
+
let stop = false;
|
|
53
|
+
for (const status of PURGEABLE) {
|
|
54
|
+
const limit = budget.messages;
|
|
55
|
+
if (limit <= 0) {
|
|
56
|
+
more = true;
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
const rows = await ctx.db
|
|
60
|
+
.query("messages")
|
|
61
|
+
.withIndex("by_status_purged", (q) =>
|
|
62
|
+
q
|
|
63
|
+
.eq("status", status)
|
|
64
|
+
.eq("bodiesPurgedAt", undefined)
|
|
65
|
+
.lt("_creationTime", cutoff),
|
|
66
|
+
)
|
|
67
|
+
.take(limit + 1);
|
|
68
|
+
if (rows.length > limit) more = true;
|
|
69
|
+
for (const message of rows.slice(0, limit)) {
|
|
70
|
+
const n = await purgeMessage(ctx, message, now, budget);
|
|
71
|
+
if (n === null) {
|
|
72
|
+
stop = true;
|
|
73
|
+
more = true;
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
removed += n;
|
|
77
|
+
}
|
|
78
|
+
if (stop) break;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// The inbound pass is skipped when outbound spent the whole budget, so
|
|
82
|
+
// there may be work left even if no outbound status said so.
|
|
83
|
+
if (budget.messages <= 0) more = true;
|
|
84
|
+
|
|
85
|
+
// Inbound, once the app has handled it.
|
|
86
|
+
const inboundLimit = budget.messages;
|
|
87
|
+
if (!stop && inboundLimit > 0) {
|
|
88
|
+
const rows = await ctx.db
|
|
89
|
+
.query("inbound")
|
|
90
|
+
.withIndex("by_state_purged", (q) =>
|
|
91
|
+
q
|
|
92
|
+
.eq("state", "handled")
|
|
93
|
+
.eq("purgedAt", undefined)
|
|
94
|
+
.lt("_creationTime", cutoff),
|
|
95
|
+
)
|
|
96
|
+
.take(inboundLimit + 1);
|
|
97
|
+
if (rows.length > inboundLimit) more = true;
|
|
98
|
+
for (const row of rows.slice(0, inboundLimit)) {
|
|
99
|
+
const message = row.messageId ? await ctx.db.get(row.messageId) : null;
|
|
100
|
+
// Only the row that created the message may purge it: a `handled`
|
|
101
|
+
// duplicate must not take the bodies of an original still unhandled.
|
|
102
|
+
if (
|
|
103
|
+
message &&
|
|
104
|
+
message.inboundId === row._id &&
|
|
105
|
+
message.bodiesPurgedAt === undefined
|
|
106
|
+
) {
|
|
107
|
+
const n = await purgeMessage(ctx, message, now, budget);
|
|
108
|
+
if (n === null) {
|
|
109
|
+
more = true;
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
removed += n;
|
|
113
|
+
}
|
|
114
|
+
if (row.rawStorageId !== undefined) {
|
|
115
|
+
await ctx.storage.delete(row.rawStorageId);
|
|
116
|
+
removed += 1;
|
|
117
|
+
}
|
|
118
|
+
await ctx.db.patch(row._id, { rawStorageId: undefined, purgedAt: now });
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const looseLimit = Math.min(BATCH, Math.max(budget.attachments, 0));
|
|
123
|
+
const loose = await ctx.db
|
|
124
|
+
.query("attachments")
|
|
125
|
+
.withIndex("by_message", (q) =>
|
|
126
|
+
q.eq("messageId", undefined).lt("_creationTime", cutoff),
|
|
127
|
+
)
|
|
128
|
+
.take(looseLimit + 1);
|
|
129
|
+
if (loose.length > looseLimit) more = true;
|
|
130
|
+
removed += await deleteAttachments(ctx, loose.slice(0, looseLimit));
|
|
131
|
+
|
|
132
|
+
return { removed, more };
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
/** Returns files deleted, or null when the attachment budget is out. */
|
|
137
|
+
async function purgeMessage(
|
|
138
|
+
ctx: MutationCtx,
|
|
139
|
+
message: Doc<"messages">,
|
|
140
|
+
now: number,
|
|
141
|
+
budget: Budget,
|
|
142
|
+
): Promise<number | null> {
|
|
143
|
+
const attachments = await ctx.db
|
|
144
|
+
.query("attachments")
|
|
145
|
+
.withIndex("by_message", (q) => q.eq("messageId", message._id))
|
|
146
|
+
.take(budget.attachments + 1);
|
|
147
|
+
if (attachments.length > budget.attachments) return null;
|
|
148
|
+
budget.attachments -= attachments.length;
|
|
149
|
+
let removed = await deleteAttachments(ctx, attachments);
|
|
150
|
+
for (const id of [message.htmlStorageId, message.textStorageId]) {
|
|
151
|
+
if (id === undefined) continue;
|
|
152
|
+
await ctx.storage.delete(id);
|
|
153
|
+
removed += 1;
|
|
154
|
+
}
|
|
155
|
+
await ctx.db.patch(message._id, {
|
|
156
|
+
htmlStorageId: undefined,
|
|
157
|
+
textStorageId: undefined,
|
|
158
|
+
bodiesPurgedAt: now,
|
|
159
|
+
});
|
|
160
|
+
budget.messages -= 1;
|
|
161
|
+
return removed;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async function deleteAttachments(
|
|
165
|
+
ctx: MutationCtx,
|
|
166
|
+
rows: Doc<"attachments">[],
|
|
167
|
+
): Promise<number> {
|
|
168
|
+
for (const row of rows) {
|
|
169
|
+
await ctx.storage.delete(row.storageId);
|
|
170
|
+
await ctx.db.delete(row._id);
|
|
171
|
+
}
|
|
172
|
+
return rows.length;
|
|
173
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Infer } from "convex/values";
|
|
2
|
+
import { vRuntimeConfig } from "../shared/validators.js";
|
|
3
|
+
import type { Doc } from "./_generated/dataModel.js";
|
|
4
|
+
import {
|
|
5
|
+
internalMutation,
|
|
6
|
+
type MutationCtx,
|
|
7
|
+
type QueryCtx,
|
|
8
|
+
} from "./_generated/server.js";
|
|
9
|
+
|
|
10
|
+
type ConfigFields = Infer<typeof vRuntimeConfig>;
|
|
11
|
+
|
|
12
|
+
export async function getConfig(
|
|
13
|
+
ctx: Pick<QueryCtx, "db">,
|
|
14
|
+
): Promise<Doc<"config"> | null> {
|
|
15
|
+
return await ctx.db.query("config").first();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Key-order-independent form, since stored documents reorder keys. */
|
|
19
|
+
function canonical(value: unknown): string {
|
|
20
|
+
return JSON.stringify(value, (_k, val) =>
|
|
21
|
+
val && typeof val === "object" && !Array.isArray(val)
|
|
22
|
+
? Object.fromEntries(
|
|
23
|
+
Object.entries(val as Record<string, unknown>)
|
|
24
|
+
.filter(([, x]) => x !== undefined)
|
|
25
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)),
|
|
26
|
+
)
|
|
27
|
+
: val,
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Writes the singleton only when it differs from what is stored. */
|
|
32
|
+
export async function upsertConfig(
|
|
33
|
+
ctx: MutationCtx,
|
|
34
|
+
config: ConfigFields,
|
|
35
|
+
): Promise<void> {
|
|
36
|
+
const existing = await getConfig(ctx);
|
|
37
|
+
if (!existing) {
|
|
38
|
+
await ctx.db.insert("config", config);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const { _id, _creationTime, ...stored } = existing;
|
|
42
|
+
if (canonical(stored) === canonical(config)) return;
|
|
43
|
+
await ctx.db.replace(_id, config);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const upsert = internalMutation({
|
|
47
|
+
args: { config: vRuntimeConfig },
|
|
48
|
+
handler: async (ctx, { config }) => {
|
|
49
|
+
await upsertConfig(ctx, config);
|
|
50
|
+
return null;
|
|
51
|
+
},
|
|
52
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import workpool from "@convex-dev/workpool/convex.config";
|
|
2
|
+
import { defineComponent } from "convex/server";
|
|
3
|
+
|
|
4
|
+
const component = defineComponent("email");
|
|
5
|
+
component.use(workpool, { name: "transactional" });
|
|
6
|
+
component.use(workpool, { name: "bulk" });
|
|
7
|
+
component.use(workpool, { name: "callbacks" });
|
|
8
|
+
|
|
9
|
+
export default component;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { FunctionHandle } from "convex/server";
|
|
2
|
+
import type {
|
|
3
|
+
CredentialPurpose,
|
|
4
|
+
ProviderName,
|
|
5
|
+
RuntimeConfig,
|
|
6
|
+
} from "../shared/types.js";
|
|
7
|
+
import type { ActionCtx } from "./_generated/server.js";
|
|
8
|
+
|
|
9
|
+
/** The only error text a failing credentials callback ever leaves behind. */
|
|
10
|
+
export const CREDENTIALS_FAILED = "credentials callback failed";
|
|
11
|
+
|
|
12
|
+
/** Every string value in `value` (the `provider` name is not a secret). */
|
|
13
|
+
export function secretStrings(value: unknown, out: string[] = []): string[] {
|
|
14
|
+
if (typeof value === "string") {
|
|
15
|
+
if (value !== "") out.push(value);
|
|
16
|
+
} else if (Array.isArray(value)) {
|
|
17
|
+
for (const x of value) secretStrings(x, out);
|
|
18
|
+
} else if (typeof value === "object" && value !== null) {
|
|
19
|
+
for (const [k, x] of Object.entries(value)) {
|
|
20
|
+
if (k !== "provider") secretStrings(x, out);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Adapter text may echo a credential back; scrub it before it is kept. */
|
|
27
|
+
export function redact(text: string, secrets: string[]): string {
|
|
28
|
+
let out = text;
|
|
29
|
+
for (const secret of [...new Set(secrets)].sort(
|
|
30
|
+
(a, b) => b.length - a.length,
|
|
31
|
+
)) {
|
|
32
|
+
out = out.split(secret).join("[redacted]");
|
|
33
|
+
}
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Call the app's credentials callback. Whatever it throws is dropped: its
|
|
39
|
+
* message may carry a secret, so ours is the only text that escapes.
|
|
40
|
+
*/
|
|
41
|
+
export async function fetchCredentials(
|
|
42
|
+
ctx: ActionCtx,
|
|
43
|
+
config: Pick<RuntimeConfig, "credentialsHandle">,
|
|
44
|
+
args: {
|
|
45
|
+
/** `null` for the unsubscribe secret, which belongs to no provider. */
|
|
46
|
+
provider: ProviderName | null;
|
|
47
|
+
tenant?: string;
|
|
48
|
+
purpose: CredentialPurpose;
|
|
49
|
+
},
|
|
50
|
+
): Promise<unknown> {
|
|
51
|
+
try {
|
|
52
|
+
return await ctx.runAction(
|
|
53
|
+
config.credentialsHandle as FunctionHandle<"action">,
|
|
54
|
+
{
|
|
55
|
+
provider: args.provider,
|
|
56
|
+
tenant: args.tenant ?? null,
|
|
57
|
+
purpose: args.purpose,
|
|
58
|
+
},
|
|
59
|
+
);
|
|
60
|
+
} catch {
|
|
61
|
+
throw new Error(CREDENTIALS_FAILED);
|
|
62
|
+
}
|
|
63
|
+
}
|