@midsummer-ai/sdk 0.1.0-staging.3121 → 0.1.0-staging.3127
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/ai/contract.mjs +39 -0
- package/ai/index.d.mts +70 -12
- package/ai/index.mjs +74 -5
- package/ai/stand-in.mjs +26 -4
- package/client.d.mts +1 -1
- package/client.mjs +1 -105
- package/{connection-Cu2GSOaR.mjs → connection-C_JSWUrt.mjs} +7 -6
- package/contract.d.mts +1 -1
- package/contract.mjs +1 -1
- package/database/client.d.mts +20 -1
- package/database/client.mjs +31 -8
- package/database/stand-in.d.mts +2 -2
- package/database/stand-in.mjs +1 -1
- package/email/client.mjs +1 -130
- package/email/contract.mjs +1 -1
- package/email/index.d.mts +9 -7
- package/email/index.mjs +1 -58
- package/email/smtp.mjs +1 -2
- package/email/stand-in.mjs +1 -1
- package/email-DXtpr7pT.mjs +214 -0
- package/here-CeqGJW21.mjs +28 -0
- package/{index-DNNXNny7.d.mts → index-B9zs0wIp.d.mts} +6 -1
- package/{index-sgdyoA2P.d.mts → index-Ci6mGXqz.d.mts} +7 -1
- package/index.d.mts +2 -2
- package/index.mjs +3 -137
- package/package.json +1 -1
- package/queues/index.d.mts +17 -5
- package/queues/index.mjs +71 -1
- package/s3.d.mts +1 -1
- package/s3.mjs +1 -1
- package/socket.d.mts +1 -1
- package/socket.mjs +1 -262
- package/src-BKrQAqJN.mjs +519 -0
- package/stand-in.d.mts +1 -1
- package/{store-lIZ5GxVv.mjs → store-CsBhnabU.mjs} +1 -1
- package/{tabs-zCh8kceo.mjs → tabs-gHJHkhm2.mjs} +1 -1
- package/vector/contract.d.mts +1 -1
- package/vector/contract.mjs +1 -1
- package/vector/index.d.mts +2 -2
- package/vector/index.mjs +18 -7
- package/vector/stand-in.d.mts +1 -1
- package/vector/stand-in.mjs +1 -1
- package/vector/wire.d.mts +1 -1
- package/vector/wire.mjs +1 -339
- package/wire-BdNDzlIN.mjs +554 -0
- package/wire.d.mts +1 -1
- package/schema-CTHQMRsW.mjs +0 -217
package/database/stand-in.d.mts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { p as Storage } from "../index-
|
|
1
|
+
import { p as Storage } from "../index-Ci6mGXqz.mjs";
|
|
2
2
|
import { h as AuthConfig, r as Built } from "../build-DQo-JrlN.mjs";
|
|
3
3
|
import { a as FileMetadata, c as FunctionReference, d as HttpRouter, n as CronJob, s as FunctionKind, v as RegisteredFunction, y as Row } from "../server-B5HK2Ou9.mjs";
|
|
4
4
|
import { t as DatabaseError } from "../errors-x2vXKi0u.mjs";
|
|
5
|
-
import { A as VectorControl, M as VectorEngine } from "../index-
|
|
5
|
+
import { A as VectorControl, M as VectorEngine } from "../index-B9zs0wIp.mjs";
|
|
6
6
|
//#region src/database/stand-in.d.ts
|
|
7
7
|
/** A Database as its client reaches it: by function name or reference. A refusal throws `DatabaseError`. */
|
|
8
8
|
interface DatabaseClient {
|
package/database/stand-in.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { storage } from "../
|
|
1
|
+
import { n as storage } from "../src-BKrQAqJN.mjs";
|
|
2
2
|
import { standIn } from "../stand-in.mjs";
|
|
3
3
|
import { _ as shapeError, a as tagBytes, b as valueAt, f as indexFields, g as shapeAt, i as keyBytes, o as untagBytes, v as tableCode } from "../bytes-C2pcyl9u.mjs";
|
|
4
4
|
import { A as comparePrefix, M as indexTuple, O as valueKey, S as nextCron, T as readLimited, _ as isFunction, a as Refusal, b as limits, i as NotFoundError, j as documentBytes, k as compareKey, l as fetchOver, o as TooLarge, r as InsufficientError, y as isRouter } from "../server-EDRbF2u-.mjs";
|
package/email/client.mjs
CHANGED
|
@@ -1,131 +1,2 @@
|
|
|
1
|
-
import {
|
|
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
|
|
1
|
+
import { c as client, l as serveBound, o as MESSAGES_PATH, s as boundEmail, u as serveMessages } from "../email-DXtpr7pT.mjs";
|
|
131
2
|
export { MESSAGES_PATH, boundEmail, client, serveBound, serveMessages };
|
package/email/contract.mjs
CHANGED
package/email/index.d.mts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
//#region src/email/index.d.ts
|
|
2
2
|
/**
|
|
3
|
-
* Email from a Project's code: Messages sent from its
|
|
3
|
+
* Email from a Project's code: Messages sent from the sending domains its environment binds.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
* const sent = await mail.send({ from: "Acme <hi@mail.acme.dev>", to: "ada@example.com", subject: "Welcome", text: "Hello", key: "welcome-ada" })
|
|
5
|
+
* import { email } from "@midsummer-ai/sdk/email"
|
|
7
6
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
7
|
+
* const sent = await email().send({ from: "Acme <hi@mail.acme.dev>", to: "ada@example.com", subject: "Welcome", text: "Hello", key: "welcome-ada" })
|
|
8
|
+
*
|
|
9
|
+
* With no engine, `email()` sends from a deployment, in any framework, with nothing to connect.
|
|
10
|
+
* Otherwise `engine` is where Messages go: the Stand-in (`@midsummer-ai/sdk/email/stand-in`) in tests,
|
|
11
|
+
* or code outside Midsummer with an email key (`email(client(origin, { key }))` from
|
|
12
|
+
* `@midsummer-ai/sdk/email/client`). Every engine is held to the cases in `@midsummer-ai/sdk/email/contract`.
|
|
11
13
|
*
|
|
12
14
|
* Addresses and the Message's shape are checked here, before anything leaves the caller. Reuse a
|
|
13
15
|
* Message's `key` to retry while the original Message is retained for thirty days. After that
|
|
@@ -60,7 +62,7 @@ interface Email {
|
|
|
60
62
|
declare function addressOf(value: string): string;
|
|
61
63
|
/** At most this many recipients, to, cc and bcc together. */
|
|
62
64
|
declare const MAX_RECIPIENTS = 50;
|
|
63
|
-
declare function email(
|
|
65
|
+
declare function email(given?: EmailEngine): Email;
|
|
64
66
|
/**
|
|
65
67
|
* Whether a webhook delivery is genuine: its `Midsummer-Signature` header (`t=<ms>,v1=<hex>`) is the
|
|
66
68
|
* HMAC-SHA256 of `<t>.<body>` under the webhook's secret, made within `toleranceMs` (five minutes) of
|
package/email/index.mjs
CHANGED
|
@@ -1,59 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
var EmailError = class extends Error {
|
|
3
|
-
code;
|
|
4
|
-
constructor(code, message) {
|
|
5
|
-
super(message);
|
|
6
|
-
this.code = code;
|
|
7
|
-
this.name = "EmailError";
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
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;
|
|
11
|
-
/** The bare address of `Ada <ada@acme.dev>` or `ada@acme.dev`, lowercase; `invalid` when it is none. */
|
|
12
|
-
function addressOf(value) {
|
|
13
|
-
const bare = (/<([^<>]+)>\s*$/.exec(value)?.[1] ?? value).trim().toLowerCase();
|
|
14
|
-
if (!ADDRESS.test(bare) || bare.length > 254) throw new EmailError("invalid", `${value} is not an email address`);
|
|
15
|
-
return bare;
|
|
16
|
-
}
|
|
17
|
-
/** At most this many recipients, to, cc and bcc together. */
|
|
18
|
-
const MAX_RECIPIENTS = 50;
|
|
19
|
-
function email(engine) {
|
|
20
|
-
return { async send(message) {
|
|
21
|
-
const to = typeof message.to === "string" ? [message.to] : [...message.to];
|
|
22
|
-
if (!to.length) throw new EmailError("invalid", "a Message needs someone to send to");
|
|
23
|
-
const all = [
|
|
24
|
-
message.from,
|
|
25
|
-
...to,
|
|
26
|
-
...message.cc ?? [],
|
|
27
|
-
...message.bcc ?? [],
|
|
28
|
-
...message.replyTo ? [message.replyTo] : []
|
|
29
|
-
];
|
|
30
|
-
all.forEach(addressOf);
|
|
31
|
-
if (all.length - 1 - (message.replyTo ? 1 : 0) > 50) throw new EmailError("invalid", `a Message goes to at most 50 recipients`);
|
|
32
|
-
if (!message.subject.trim()) throw new EmailError("invalid", "a Message needs a subject");
|
|
33
|
-
if (message.text === void 0 && message.html === void 0) throw new EmailError("invalid", "a Message needs text or html");
|
|
34
|
-
return engine.send({
|
|
35
|
-
...message,
|
|
36
|
-
to
|
|
37
|
-
});
|
|
38
|
-
} };
|
|
39
|
-
}
|
|
40
|
-
const encoder = new TextEncoder();
|
|
41
|
-
/**
|
|
42
|
-
* Whether a webhook delivery is genuine: its `Midsummer-Signature` header (`t=<ms>,v1=<hex>`) is the
|
|
43
|
-
* HMAC-SHA256 of `<t>.<body>` under the webhook's secret, made within `toleranceMs` (five minutes) of
|
|
44
|
-
* `now`. Pass the body exactly as it arrived. Deliveries are at least once: dedupe by `Midsummer-Event-Id`.
|
|
45
|
-
*/
|
|
46
|
-
async function verifyWebhook(secret, signature, body, options = {}) {
|
|
47
|
-
const match = /^t=(\d+),v1=([0-9a-f]{64})$/.exec(signature ?? "");
|
|
48
|
-
if (!match) return false;
|
|
49
|
-
const at = Number(match[1]);
|
|
50
|
-
if (Math.abs((options.now ?? Date.now()) - at) > (options.toleranceMs ?? 3e5)) return false;
|
|
51
|
-
const key = await crypto.subtle.importKey("raw", encoder.encode(secret), {
|
|
52
|
-
name: "HMAC",
|
|
53
|
-
hash: "SHA-256"
|
|
54
|
-
}, false, ["verify"]);
|
|
55
|
-
const given = new Uint8Array(match[2].match(/../g).map((pair) => parseInt(pair, 16)));
|
|
56
|
-
return crypto.subtle.verify("HMAC", key, given, encoder.encode(`${at}.${body}`));
|
|
57
|
-
}
|
|
58
|
-
//#endregion
|
|
1
|
+
import { a as verifyWebhook, i as email, n as MAX_RECIPIENTS, r as addressOf, t as EmailError } from "../email-DXtpr7pT.mjs";
|
|
59
2
|
export { EmailError, MAX_RECIPIENTS, addressOf, email, verifyWebhook };
|
package/email/smtp.mjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { client } from "./client.mjs";
|
|
1
|
+
import { c as client, i as email, t as EmailError } from "../email-DXtpr7pT.mjs";
|
|
3
2
|
import { createServer } from "node:net";
|
|
4
3
|
import { createServer as createServer$1 } from "node:tls";
|
|
5
4
|
//#region src/email/smtp.ts
|
package/email/stand-in.mjs
CHANGED
|
@@ -0,0 +1,214 @@
|
|
|
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 };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
//#region src/here.ts
|
|
2
|
+
/** The current request's handles, or undefined outside a deployment's request. */
|
|
3
|
+
function here() {
|
|
4
|
+
const read = globalThis[Symbol.for("midsummer.context")];
|
|
5
|
+
return read?.();
|
|
6
|
+
}
|
|
7
|
+
/** The process's environment, where `midsummer dev` puts its Stand-ins' addresses; empty where there is none. */
|
|
8
|
+
function processEnv() {
|
|
9
|
+
return globalThis.process?.env ?? {};
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* An engine whose every call goes to the one `current` gives at that moment: the request's own, made
|
|
13
|
+
* when the call is. `current` throws the product's own error when there is none. It may give the
|
|
14
|
+
* engine as a promise, for one whose every call returns a promise, as the queues engine does.
|
|
15
|
+
*/
|
|
16
|
+
function late(current) {
|
|
17
|
+
const call = (engine, key, args) => {
|
|
18
|
+
const method = engine[key];
|
|
19
|
+
if (typeof method !== "function") throw new TypeError(`${String(key)} is not a call this engine takes.`);
|
|
20
|
+
return method.apply(engine, args);
|
|
21
|
+
};
|
|
22
|
+
return new Proxy({}, { get: (_target, key) => (...args) => {
|
|
23
|
+
const engine = current();
|
|
24
|
+
return engine instanceof Promise ? engine.then((found) => call(found, key, args)) : call(engine, key, args);
|
|
25
|
+
} });
|
|
26
|
+
}
|
|
27
|
+
//#endregion
|
|
28
|
+
export { late as n, processEnv as r, here as t };
|
|
@@ -396,6 +396,11 @@ interface VectorControl {
|
|
|
396
396
|
}): Promise<void>;
|
|
397
397
|
drop(namespace: string): Promise<void>;
|
|
398
398
|
}
|
|
399
|
+
/**
|
|
400
|
+
* The namespace the environment binds under `name`, from code in a deployment in any framework.
|
|
401
|
+
* Tests use `vectorDb(standIn())` instead.
|
|
402
|
+
*/
|
|
403
|
+
declare function namespace(name: string): Namespace;
|
|
399
404
|
/** Vector DB over `engine`. */
|
|
400
405
|
declare function vectorDb(engine: VectorEngine): VectorDb;
|
|
401
406
|
/**
|
|
@@ -407,4 +412,4 @@ declare function vectorDb(engine: VectorEngine): VectorDb;
|
|
|
407
412
|
*/
|
|
408
413
|
declare function embedded(engine: ValuesEngine, embed: (namespace: string, texts: ReadonlyArray<string>) => Promise<ReadonlyArray<ReadonlyArray<number>> | undefined>): VectorEngine;
|
|
409
414
|
//#endregion
|
|
410
|
-
export { VectorControl as A,
|
|
415
|
+
export { VectorControl as A, MAX_QUERIES as B, QueryOptions as C, ValuesEngine as D, TextVector as E, vectorDb as F, VectorErrorCode as G, MAX_TOP_K as H, MAX_BATCH as I, vectorErrorCodes as K, MAX_DIMENSION as L, VectorEngine as M, embedded as N, ValuesMultiQuery as O, namespace as P, MAX_FULL_TEXT as R, NamespaceInfo as S, TextSearchOptions as T, MAX_VALUE as U, MAX_TEXT as V, VectorError as W, Metric as _, Consistency as a, MultiQueryResult as b, EngineMultiQuery as c, EngineTextSearch as d, Filter as f, MetadataValue as g, Match as h, Condition as i, VectorDb as j, Vector as k, EnginePart as l, Listed as m, AggregateGroup as n, EngineAggregate as o, ListOptions as p, AggregateOptions as r, EngineList as s, Aggregate as t, EngineQuery as u, MultiQueryOptions as v, Scalar as w, Namespace as x, MultiQueryPart as y, MAX_METADATA_BYTES as z };
|
|
@@ -206,7 +206,13 @@ interface EngineList {
|
|
|
206
206
|
readonly after?: string;
|
|
207
207
|
readonly limit: number;
|
|
208
208
|
}
|
|
209
|
+
/**
|
|
210
|
+
* The bucket the environment binds under `name`, from code in a deployment in any framework, or
|
|
211
|
+
* `midsummer dev`'s bucket of that name. Code that runs outside Midsummer connects with a storage
|
|
212
|
+
* key instead: `storage(client(origin, { key, secret }))`.
|
|
213
|
+
*/
|
|
214
|
+
declare function bucket(name: string): Bucket;
|
|
209
215
|
/** Storage over `engine`. */
|
|
210
216
|
declare function storage(engine: StorageEngine): Storage;
|
|
211
217
|
//#endregion
|
|
212
|
-
export {
|
|
218
|
+
export { StorageErrorReason as S, bucket as _, ListOptions as a, StorageError as b, ObjectStream as c, PutOptions as d, Range as f, Usage as g, StoredObject as h, EnginePut as i, Operations as l, StorageEngine as m, Bucket as n, Listing as o, Storage as p, EngineList as r, ObjectRead as s, Body as t, Part as u, storage as v, StorageErrorCode as x, streamsOf as y };
|
package/index.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { _ as
|
|
2
|
-
export { Body, Bucket, EngineList, EnginePut, ListOptions, Listing, ObjectRead, ObjectStream, Operations, Part, PutOptions, Range, Storage, StorageEngine, StorageError, type StorageErrorCode, type StorageErrorReason, StoredObject, Usage, storage, streamsOf };
|
|
1
|
+
import { S as StorageErrorReason, _ as bucket, a as ListOptions, b as StorageError, c as ObjectStream, d as PutOptions, f as Range, g as Usage, h as StoredObject, i as EnginePut, l as Operations, m as StorageEngine, n as Bucket, o as Listing, p as Storage, r as EngineList, s as ObjectRead, t as Body, u as Part, v as storage, x as StorageErrorCode, y as streamsOf } from "./index-Ci6mGXqz.mjs";
|
|
2
|
+
export { Body, Bucket, EngineList, EnginePut, ListOptions, Listing, ObjectRead, ObjectStream, Operations, Part, PutOptions, Range, Storage, StorageEngine, StorageError, type StorageErrorCode, type StorageErrorReason, StoredObject, Usage, bucket, storage, streamsOf };
|