@midsummer-ai/sdk 0.1.0-staging.3428 → 0.1.0-staging.3434
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/auth-B7rCtXFQ.d.mts +36 -0
- package/{build-DQo-JrlN.d.mts → build-BhmOZmGQ.d.mts} +2 -27
- package/client.mjs +1 -1
- package/contract.mjs +2 -2
- package/database/build.d.mts +1 -1
- package/database/build.mjs +35 -2
- package/database/server.mjs +1 -1
- package/database/stand-in.d.mts +2 -1
- package/database/stand-in.mjs +2 -2
- package/email/client.d.mts +5 -4
- package/email/client.mjs +1 -1
- package/email/contract.d.mts +3 -1
- package/email/contract.mjs +111 -4
- package/email/index.d.mts +85 -16
- package/email/index.mjs +2 -2
- package/email/smtp.mjs +24 -8
- package/email/stand-in.d.mts +3 -0
- package/email/stand-in.mjs +17 -7
- package/email-IH5OllU2.mjs +337 -0
- package/errors-B1tO_wdm.mjs +28 -0
- package/flows/contract.d.mts +13 -0
- package/flows/contract.mjs +483 -0
- package/flows/index.d.mts +148 -0
- package/flows/index.mjs +347 -0
- package/index.mjs +2 -2
- package/{names-DfCBKupJ.mjs → names-Do0ot5Ha.mjs} +2 -28
- package/package.json +29 -1
- package/queues/contract.mjs +144 -1
- package/queues/index.d.mts +149 -5
- package/queues/index.mjs +56 -12
- package/queues/stand-in.mjs +333 -31
- package/queues/wire.mjs +302 -16
- package/s3.mjs +3 -2
- package/{server-EDRbF2u-.mjs → server-Drjni3-F.mjs} +4 -2
- package/socket.mjs +1 -1
- package/{src-BKrQAqJN.mjs → src-CkP59_Qu.mjs} +2 -1
- package/stand-in.mjs +2 -1
- package/storage/contract.d.mts +25 -0
- package/storage/contract.mjs +153 -0
- package/storage/files.d.mts +144 -0
- package/storage/files.mjs +513 -0
- package/wire.mjs +2 -1
- package/email-DXtpr7pT.mjs +0 -214
package/email-DXtpr7pT.mjs
DELETED
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
import { n as late, t as here } from "./here-CeqGJW21.mjs";
|
|
2
|
-
//#region src/email/client.ts
|
|
3
|
-
/**
|
|
4
|
-
* Email's wire: code holding an Email key sends through `client`, and the platform replies with
|
|
5
|
-
* `serveMessages`. One Message is one POST of JSON to `<origin>/email/messages` with
|
|
6
|
-
* `authorization: Bearer <the key's secret>`; the answer is the `SentMessage`, or
|
|
7
|
-
* `{ "error": { "code", "message" } }` with the code's status.
|
|
8
|
-
*
|
|
9
|
-
* const mail = email(client("https://api.<site>", { key }))
|
|
10
|
-
*/
|
|
11
|
-
const MESSAGES_PATH = "/email/messages";
|
|
12
|
-
const STATUS = {
|
|
13
|
-
invalid: 400,
|
|
14
|
-
forbidden: 401,
|
|
15
|
-
unverified: 422,
|
|
16
|
-
unavailable: 503
|
|
17
|
-
};
|
|
18
|
-
/** Email's engine at `origin` (the API's) for the Email key `key`. */
|
|
19
|
-
function client(origin, options) {
|
|
20
|
-
const url = `${origin.replace(/\/+$/, "")}${MESSAGES_PATH}`;
|
|
21
|
-
const fetcher = options.fetch ?? ((input, init) => fetch(input, init));
|
|
22
|
-
return { async send(message) {
|
|
23
|
-
let response;
|
|
24
|
-
try {
|
|
25
|
-
response = await fetcher(url, {
|
|
26
|
-
method: "POST",
|
|
27
|
-
headers: {
|
|
28
|
-
authorization: `Bearer ${options.key}`,
|
|
29
|
-
"content-type": "application/json"
|
|
30
|
-
},
|
|
31
|
-
body: JSON.stringify(message)
|
|
32
|
-
});
|
|
33
|
-
} catch {
|
|
34
|
-
throw new EmailError("unavailable", "Email could not be reached");
|
|
35
|
-
}
|
|
36
|
-
const body = await response.json().catch(() => void 0);
|
|
37
|
-
if (response.ok && body && typeof body.id === "string") return body;
|
|
38
|
-
const code = body?.error?.code;
|
|
39
|
-
throw new EmailError(code && code in STATUS ? code : "unavailable", body?.error?.message ?? `Email answered ${response.status}`);
|
|
40
|
-
} };
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* The platform's side of the wire: checks the request again, as it comes from the network, and hands
|
|
44
|
-
* the key and the Message to `send`, which throws an `EmailError` for a refusal.
|
|
45
|
-
*/
|
|
46
|
-
function serveMessages(send) {
|
|
47
|
-
const refuse = (code, message) => Response.json({ error: {
|
|
48
|
-
code,
|
|
49
|
-
message
|
|
50
|
-
} }, { status: STATUS[code] });
|
|
51
|
-
return async (request) => {
|
|
52
|
-
if (request.method !== "POST") return Response.json({ error: {
|
|
53
|
-
code: "invalid",
|
|
54
|
-
message: "Use POST."
|
|
55
|
-
} }, { status: 405 });
|
|
56
|
-
const key = /^Bearer (\S+)$/.exec(request.headers.get("authorization") ?? "")?.[1];
|
|
57
|
-
if (!key) return refuse("forbidden", "Send with an Email key: authorization: Bearer <key>.");
|
|
58
|
-
const text = await request.text();
|
|
59
|
-
if (text.length > 2e6) return refuse("invalid", "A Message is at most 2 MB.");
|
|
60
|
-
let body;
|
|
61
|
-
try {
|
|
62
|
-
body = JSON.parse(text);
|
|
63
|
-
} catch {
|
|
64
|
-
body = void 0;
|
|
65
|
-
}
|
|
66
|
-
if (!isMessage(body)) return refuse("invalid", "The body is not a Message.");
|
|
67
|
-
try {
|
|
68
|
-
const sent = await email({ send: (message) => send(key, message) }).send(body);
|
|
69
|
-
return Response.json({
|
|
70
|
-
id: sent.id,
|
|
71
|
-
state: sent.state,
|
|
72
|
-
suppressed: sent.suppressed,
|
|
73
|
-
...sent.error !== void 0 ? { error: sent.error } : {}
|
|
74
|
-
});
|
|
75
|
-
} catch (error) {
|
|
76
|
-
if (error instanceof EmailError) return refuse(error.code, error.message);
|
|
77
|
-
throw error;
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* What Midsummer hands a Deployment as `context.email`: sends from the Sending domains its
|
|
83
|
-
* Environment binds, with no key.
|
|
84
|
-
*
|
|
85
|
-
* await email(boundEmail(context.email)).send({ from: "Acme <hi@acme.dev>", to: ["ada@example.com"], subject: "Hi", text: "Hello" })
|
|
86
|
-
*/
|
|
87
|
-
function boundEmail(handle) {
|
|
88
|
-
return { async send(message) {
|
|
89
|
-
let answer;
|
|
90
|
-
try {
|
|
91
|
-
answer = await handle.send(message);
|
|
92
|
-
} catch {
|
|
93
|
-
throw new EmailError("unavailable", "Email could not be reached");
|
|
94
|
-
}
|
|
95
|
-
if ("id" in answer) return answer;
|
|
96
|
-
throw new EmailError(answer.error.code in STATUS ? answer.error.code : "unavailable", answer.error.message);
|
|
97
|
-
} };
|
|
98
|
-
}
|
|
99
|
-
/** The platform's side of a bound handle: the same checks as `serveMessages`, a refusal returned as data. */
|
|
100
|
-
function serveBound(send) {
|
|
101
|
-
return async (message) => {
|
|
102
|
-
if (!isMessage(message)) return { error: {
|
|
103
|
-
code: "invalid",
|
|
104
|
-
message: "The body is not a Message."
|
|
105
|
-
} };
|
|
106
|
-
try {
|
|
107
|
-
const sent = await email({ send }).send(message);
|
|
108
|
-
return {
|
|
109
|
-
id: sent.id,
|
|
110
|
-
state: sent.state,
|
|
111
|
-
suppressed: sent.suppressed,
|
|
112
|
-
...sent.error !== void 0 ? { error: sent.error } : {}
|
|
113
|
-
};
|
|
114
|
-
} catch (error) {
|
|
115
|
-
if (error instanceof EmailError) return { error: {
|
|
116
|
-
code: error.code,
|
|
117
|
-
message: error.message
|
|
118
|
-
} };
|
|
119
|
-
throw error;
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
const strings = (value) => Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
124
|
-
const optional = (value) => value === void 0 || typeof value === "string";
|
|
125
|
-
function isMessage(value) {
|
|
126
|
-
if (typeof value !== "object" || value === null) return false;
|
|
127
|
-
const m = value;
|
|
128
|
-
return typeof m.from === "string" && strings(m.to) && (m.cc === void 0 || strings(m.cc)) && (m.bcc === void 0 || strings(m.bcc)) && typeof m.subject === "string" && optional(m.replyTo) && optional(m.text) && optional(m.html) && optional(m.key);
|
|
129
|
-
}
|
|
130
|
-
//#endregion
|
|
131
|
-
//#region src/email/index.ts
|
|
132
|
-
/**
|
|
133
|
-
* Email from a Project's code: Messages sent from the sending domains its environment binds.
|
|
134
|
-
*
|
|
135
|
-
* import { email } from "@midsummer-ai/sdk/email"
|
|
136
|
-
*
|
|
137
|
-
* const sent = await email().send({ from: "Acme <hi@mail.acme.dev>", to: "ada@example.com", subject: "Welcome", text: "Hello", key: "welcome-ada" })
|
|
138
|
-
*
|
|
139
|
-
* With no engine, `email()` sends from a deployment, in any framework, with nothing to connect.
|
|
140
|
-
* Otherwise `engine` is where Messages go: the Stand-in (`@midsummer-ai/sdk/email/stand-in`) in tests,
|
|
141
|
-
* or code outside Midsummer with an email key (`email(client(origin, { key }))` from
|
|
142
|
-
* `@midsummer-ai/sdk/email/client`). Every engine is held to the cases in `@midsummer-ai/sdk/email/contract`.
|
|
143
|
-
*
|
|
144
|
-
* Addresses and the Message's shape are checked here, before anything leaves the caller. Reuse a
|
|
145
|
-
* Message's `key` to retry while the original Message is retained for thirty days. After that
|
|
146
|
-
* Message is purged, the same key can send another Message. A suppressed recipient (one that bounced
|
|
147
|
-
* or complained) is left out and named in `suppressed`; a Message with nobody left is `suppressed`.
|
|
148
|
-
* Every failure is an `EmailError`, whose `code` tells you which kind.
|
|
149
|
-
*
|
|
150
|
-
* Delivery events reach a Project's webhooks signed; `verifyWebhook` checks a delivery.
|
|
151
|
-
*/
|
|
152
|
-
var EmailError = class extends Error {
|
|
153
|
-
code;
|
|
154
|
-
constructor(code, message) {
|
|
155
|
-
super(message);
|
|
156
|
-
this.code = code;
|
|
157
|
-
this.name = "EmailError";
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
const ADDRESS = /^[^\s@<>()",;:]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)+$/i;
|
|
161
|
-
/** The bare address of `Ada <ada@acme.dev>` or `ada@acme.dev`, lowercase; `invalid` when it is none. */
|
|
162
|
-
function addressOf(value) {
|
|
163
|
-
const bare = (/<([^<>]+)>\s*$/.exec(value)?.[1] ?? value).trim().toLowerCase();
|
|
164
|
-
if (!ADDRESS.test(bare) || bare.length > 254) throw new EmailError("invalid", `${value} is not an email address`);
|
|
165
|
-
return bare;
|
|
166
|
-
}
|
|
167
|
-
/** At most this many recipients, to, cc and bcc together. */
|
|
168
|
-
const MAX_RECIPIENTS = 50;
|
|
169
|
-
function email(given) {
|
|
170
|
-
const engine = given ?? late(() => {
|
|
171
|
-
const handle = here()?.email;
|
|
172
|
-
if (handle) return boundEmail(handle);
|
|
173
|
-
throw new EmailError("forbidden", "This code runs outside a deployment, so it sends with an email key: email(client(origin, { key })).");
|
|
174
|
-
});
|
|
175
|
-
return { async send(message) {
|
|
176
|
-
const to = typeof message.to === "string" ? [message.to] : [...message.to];
|
|
177
|
-
if (!to.length) throw new EmailError("invalid", "a Message needs someone to send to");
|
|
178
|
-
const all = [
|
|
179
|
-
message.from,
|
|
180
|
-
...to,
|
|
181
|
-
...message.cc ?? [],
|
|
182
|
-
...message.bcc ?? [],
|
|
183
|
-
...message.replyTo ? [message.replyTo] : []
|
|
184
|
-
];
|
|
185
|
-
all.forEach(addressOf);
|
|
186
|
-
if (all.length - 1 - (message.replyTo ? 1 : 0) > 50) throw new EmailError("invalid", `a Message goes to at most 50 recipients`);
|
|
187
|
-
if (!message.subject.trim()) throw new EmailError("invalid", "a Message needs a subject");
|
|
188
|
-
if (message.text === void 0 && message.html === void 0) throw new EmailError("invalid", "a Message needs text or html");
|
|
189
|
-
return engine.send({
|
|
190
|
-
...message,
|
|
191
|
-
to
|
|
192
|
-
});
|
|
193
|
-
} };
|
|
194
|
-
}
|
|
195
|
-
const encoder = new TextEncoder();
|
|
196
|
-
/**
|
|
197
|
-
* Whether a webhook delivery is genuine: its `Midsummer-Signature` header (`t=<ms>,v1=<hex>`) is the
|
|
198
|
-
* HMAC-SHA256 of `<t>.<body>` under the webhook's secret, made within `toleranceMs` (five minutes) of
|
|
199
|
-
* `now`. Pass the body exactly as it arrived. Deliveries are at least once: dedupe by `Midsummer-Event-Id`.
|
|
200
|
-
*/
|
|
201
|
-
async function verifyWebhook(secret, signature, body, options = {}) {
|
|
202
|
-
const match = /^t=(\d+),v1=([0-9a-f]{64})$/.exec(signature ?? "");
|
|
203
|
-
if (!match) return false;
|
|
204
|
-
const at = Number(match[1]);
|
|
205
|
-
if (Math.abs((options.now ?? Date.now()) - at) > (options.toleranceMs ?? 3e5)) return false;
|
|
206
|
-
const key = await crypto.subtle.importKey("raw", encoder.encode(secret), {
|
|
207
|
-
name: "HMAC",
|
|
208
|
-
hash: "SHA-256"
|
|
209
|
-
}, false, ["verify"]);
|
|
210
|
-
const given = new Uint8Array(match[2].match(/../g).map((pair) => parseInt(pair, 16)));
|
|
211
|
-
return crypto.subtle.verify("HMAC", key, given, encoder.encode(`${at}.${body}`));
|
|
212
|
-
}
|
|
213
|
-
//#endregion
|
|
214
|
-
export { verifyWebhook as a, client as c, email as i, serveBound as l, MAX_RECIPIENTS as n, MESSAGES_PATH as o, addressOf as r, boundEmail as s, EmailError as t, serveMessages as u };
|