@midsummer-ai/sdk 0.1.0-staging.3677 → 0.1.0-staging.3683

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/contract.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { c as LIMIT_WINDOW_MS, l as SETTINGS_TTL_MS, n as AuthError, s as LIMITS } from "../auth-D50wCpNm.mjs";
1
+ import { c as LIMIT_WINDOW_MS, l as SETTINGS_TTL_MS, n as AuthError, s as LIMITS } from "../auth-BxXrC_JJ.mjs";
2
2
  import { IMPERSONATION_TOKEN_MS, startImpersonation } from "./impersonate.mjs";
3
3
  //#region src/auth/contract.ts
4
4
  /**
package/auth/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { a as auth, i as SESSION_CACHE_S, n as AuthError, o as createAuth, r as IMPERSONATION_S, t as AUTH_PATH, u as databaseStore } from "../auth-D50wCpNm.mjs";
1
+ import { a as auth, i as SESSION_CACHE_S, n as AuthError, o as createAuth, r as IMPERSONATION_S, t as AUTH_PATH, u as databaseStore } from "../auth-BxXrC_JJ.mjs";
2
2
  import { checkSignInMail, signInWords } from "./mail.mjs";
3
3
  import { n as authFiles } from "../files-DAFOuID_.mjs";
4
4
  export { AUTH_PATH, AuthError, IMPERSONATION_S, SESSION_CACHE_S, auth, authFiles, checkSignInMail, createAuth, databaseStore, signInWords };
package/auth/mail.d.mts CHANGED
@@ -1,3 +1,4 @@
1
+ import { a as EmailError } from "../index-m0wtx22n.mjs";
1
2
  //#region src/auth/mail.d.ts
2
3
  type SignInMailKind = "code" | "link" | "verify" | "reset" | "invite" | "device";
3
4
  /** One sign-in mail: whom to, which kind, the app's name, and its code or its address. */
@@ -31,9 +32,15 @@ declare function signInWords(mail: SignInMail): {
31
32
  /**
32
33
  * Sends a sign-in mail where the code runs: from `sender` (the settings' address) or `AUTH_EMAIL_FROM` when one is set, else from the shared
33
34
  * sign-in sender. Under `midsummer dev`, with no deployment to send from, its words are printed instead,
34
- * so the code or the link is there to use. Throws `EmailError` when Email refuses it.
35
+ * so the code or the link is there to use. Throws `EmailError` saying why when Email refuses it or the mail service doesn't send it.
35
36
  */
36
37
  declare function sendSignInMail(mail: SignInMail, env?: Readonly<Record<string, string | undefined>>, sender?: string | null, remote?: RemoteMail): Promise<void>;
38
+ /**
39
+ * The error a sign-in mail the mail service refused throws, with Email's words for why. A deployment's sign-in throws it
40
+ * itself; the platform's mail Action (`auth.mail.create`) returns 400 with its message, which an app hosted elsewhere
41
+ * throws as the same error.
42
+ */
43
+ declare function unsentMail(to: string, why: string | undefined): EmailError;
37
44
  /** Where a sign-in mail goes from outside Midsummer: the platform's mail Action, with the database's key or a run's workload token. */
38
45
  interface RemoteMail {
39
46
  readonly url: string;
@@ -46,4 +53,4 @@ interface RemoteMail {
46
53
  readonly fetch?: typeof fetch;
47
54
  }
48
55
  //#endregion
49
- export { RemoteMail, SignInMail, SignInMailKind, checkSignInMail, sendSignInMail, signInWords };
56
+ export { RemoteMail, SignInMail, SignInMailKind, checkSignInMail, sendSignInMail, signInWords, unsentMail };
package/auth/mail.mjs CHANGED
@@ -138,28 +138,27 @@ async function keyOf(mail) {
138
138
  /**
139
139
  * Sends a sign-in mail where the code runs: from `sender` (the settings' address) or `AUTH_EMAIL_FROM` when one is set, else from the shared
140
140
  * sign-in sender. Under `midsummer dev`, with no deployment to send from, its words are printed instead,
141
- * so the code or the link is there to use. Throws `EmailError` when Email refuses it.
141
+ * so the code or the link is there to use. Throws `EmailError` saying why when Email refuses it or the mail service doesn't send it.
142
142
  */
143
143
  async function sendSignInMail(mail, env = processEnv(), sender, remote) {
144
144
  const checked = checkSignInMail(mail);
145
145
  const from = sender || env.AUTH_EMAIL_FROM;
146
146
  const handle = here()?.email;
147
147
  if (from && handle) {
148
- await email().send({
148
+ sent(await email().send({
149
149
  from,
150
150
  to: checked.to,
151
151
  ...signInWords(checked),
152
152
  key: await keyOf(checked),
153
153
  tag: "sign-in"
154
- });
154
+ }), checked.to);
155
155
  return;
156
156
  }
157
157
  if (handle) {
158
- const answer = await handle.send({
158
+ sent(await handle.send({
159
159
  signIn: checked,
160
160
  key: await keyOf(checked)
161
- });
162
- if (answer?.error) throw new EmailError(answer.error.code, answer.error.message);
161
+ }), checked.to);
163
162
  return;
164
163
  }
165
164
  if (remote) {
@@ -188,5 +187,31 @@ async function sendSignInMail(mail, env = processEnv(), sender, remote) {
188
187
  }
189
188
  throw new EmailError("forbidden", "This code runs outside a deployment, so sign-in has nowhere to send mail: set DATABASE_KEY. In tests, give createAuth a mail function: createAuth({ mail }).");
190
189
  }
190
+ const CODES = [
191
+ "invalid",
192
+ "unverified",
193
+ "forbidden",
194
+ "unavailable"
195
+ ];
196
+ /**
197
+ * Throws why a sign-in mail wasn't sent, given what the platform's handle returned: its refusal (`{ error: { code,
198
+ * message } }`), or the message as Email kept it, `failed` with Email's words as `error` when the mail service refused it.
199
+ */
200
+ function sent(answer, to) {
201
+ const given = typeof answer === "object" && answer !== null ? answer : {};
202
+ if (typeof given.error === "object" && given.error !== null) {
203
+ const { code, message } = given.error;
204
+ throw new EmailError(CODES.includes(code) ? code : "unavailable", typeof message === "string" && message ? message : `The sign-in mail to ${to} wasn't sent.`);
205
+ }
206
+ if (given.state === "failed") throw unsentMail(to, typeof given.error === "string" ? given.error : void 0);
207
+ }
208
+ /**
209
+ * The error a sign-in mail the mail service refused throws, with Email's words for why. A deployment's sign-in throws it
210
+ * itself; the platform's mail Action (`auth.mail.create`) returns 400 with its message, which an app hosted elsewhere
211
+ * throws as the same error.
212
+ */
213
+ function unsentMail(to, why) {
214
+ return new EmailError("invalid", `The sign-in mail to ${to} wasn't sent: ${why || "the mail service refused it."}`);
215
+ }
191
216
  //#endregion
192
- export { checkSignInMail, sendSignInMail, signInWords };
217
+ export { checkSignInMail, sendSignInMail, signInWords, unsentMail };
package/auth/password.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { i as hashPassword$1, o as verifyPassword$1 } from "../engine-BhA0XgI7.mjs";
1
+ import { i as hashPassword$1, o as verifyPassword$1 } from "../engine-DsgRC8SR.mjs";
2
2
  import { isBcrypt as isBcrypt$1 } from "./import.mjs";
3
3
  //#region src/auth/password.ts
4
4
  /**
@@ -3,7 +3,7 @@ import { connect } from "./database/client.mjs";
3
3
  import { sendSignInMail } from "./auth/mail.mjs";
4
4
  import "./files-DAFOuID_.mjs";
5
5
  import { standIn } from "./auth/stand-in.mjs";
6
- import { a as ourWords, c as agentOfToken, d as credentialOf, f as remembering, l as challenge, n as REFUSALS, p as resourceOf, r as buildEngine, s as agentOfKey, t as JoinRefused, u as consentPage } from "./engine-BhA0XgI7.mjs";
6
+ import { a as ourWords, c as agentOfToken, d as credentialOf, f as remembering, l as challenge, n as REFUSALS, p as resourceOf, r as buildEngine, s as agentOfKey, t as JoinRefused, u as consentPage } from "./engine-DsgRC8SR.mjs";
7
7
  import { a as WORDS, c as setupKeyOf, i as CAPTCHA_WIDGETS, n as qrSvg, o as ago, r as AUTH_CSS, s as deviceOf } from "./qr-C1mfuCT9.mjs";
8
8
  //#region src/auth/store.ts
9
9
  const KIND = {
@@ -48163,6 +48163,11 @@ async function verifyPassword(input) {
48163
48163
  function ourWords(text) {
48164
48164
  return text.replace(/\[better[- ]?auth\]:?\s*/gi, "").replace(/BETTER_AUTH_/g, "AUTH_").replace(/better[- ]?auth/gi, "sign-in");
48165
48165
  }
48166
+ /** An error as one log line: its name, its `code` when it has one, and its message. */
48167
+ function errorLine(error) {
48168
+ const code = error.code;
48169
+ return `${error.name}${typeof code === "string" && code ? ` (${code})` : ""}${error.message ? `: ${error.message}` : ""}`;
48170
+ }
48166
48171
  /** Midsummer's words for the engine's refusals a person sees most, by their code. Any other keeps its message, in our words. */
48167
48172
  const REFUSALS = {
48168
48173
  INVALID_EMAIL_OR_PASSWORD: "That email and password don't match.",
@@ -48346,7 +48351,7 @@ function optionsFor(config, store) {
48346
48351
  advanced: { cookiePrefix: "midsummer" },
48347
48352
  logger: {
48348
48353
  level: "warn",
48349
- log: (level, message, ...args) => (level === "error" ? console.error : console.warn)(`[auth] ${ourWords(String(message))}`, ...args.map((arg) => typeof arg === "string" ? ourWords(arg) : arg))
48354
+ log: (level, message, ...args) => (level === "error" ? console.error : console.warn)(`[auth] ${ourWords(String(message))}`, ...args.map((arg) => typeof arg === "string" ? ourWords(arg) : arg instanceof Error ? ourWords(errorLine(arg)) : arg))
48350
48355
  },
48351
48356
  telemetry: { enabled: false },
48352
48357
  database: engineAdapter(store, config.changed),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midsummer-ai/sdk",
3
- "version": "0.1.0-staging.3677",
3
+ "version": "0.1.0-staging.3683",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "description": "Midsummer's handles for an app's code: its Database, Storage, Queues, Vector DB, AI, Email and Agents, with stand-ins for tests.",