@midsummer-ai/sdk 0.1.0-staging.3728 → 0.1.0-staging.3734

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.
Files changed (47) hide show
  1. package/ai/contract.mjs +1 -1
  2. package/ai/index.d.mts +3 -2
  3. package/ai/index.mjs +1 -262
  4. package/ai/stand-in.mjs +1 -62
  5. package/ai-2Llbc56l.mjs +328 -0
  6. package/apps.d.mts +0 -15
  7. package/apps.mjs +19 -2
  8. package/auth/contract.d.mts +2 -2
  9. package/auth/contract.mjs +1 -1
  10. package/auth/index.d.mts +2 -2
  11. package/auth/index.mjs +1 -1
  12. package/auth/mail.mjs +2 -2
  13. package/auth/password.mjs +1 -1
  14. package/auth/react.d.mts +7 -2
  15. package/auth/react.mjs +44 -8
  16. package/auth/stand-in.d.mts +1 -1
  17. package/{auth-BA5Xdi-4.mjs → auth-BaGXVRYd.mjs} +38 -8
  18. package/billing/index.mjs +1 -1
  19. package/client-_Al5bpqX.mjs +877 -0
  20. package/{client-D3BZs3LA.d.mts → client-oAVg6vn_.d.mts} +64 -20
  21. package/client.mjs +1 -1
  22. package/contract.mjs +1 -1
  23. package/database/client.d.mts +2 -2
  24. package/database/client.mjs +2 -711
  25. package/database/react.d.mts +1 -1
  26. package/database/stand-in.mjs +1 -1
  27. package/email/client.mjs +1 -1
  28. package/email/contract.mjs +1 -1
  29. package/email/inbox.mjs +1 -1
  30. package/email/index.mjs +1 -1
  31. package/email/smtp.mjs +1 -1
  32. package/email/stand-in.mjs +1 -1
  33. package/{email-ynNo0HqT.mjs → email-DmWG_Ug-.mjs} +16 -2
  34. package/{engine-DDPJ2CET.mjs → engine-BmiPioJ2.mjs} +13 -2
  35. package/{here-1plIxb0D.mjs → here-BKjizr_N.mjs} +9 -1
  36. package/{index-C2Jo3JqT.d.mts → index-C8Ti5raa.d.mts} +2 -2
  37. package/index.mjs +1 -1
  38. package/package.json +1 -1
  39. package/{qr-CBh0LP2_.mjs → qr-Br1sOBmT.mjs} +1 -0
  40. package/queues/index.mjs +1 -1
  41. package/s3.mjs +1 -1
  42. package/socket.mjs +1 -1
  43. package/{src-BKKy9km-.mjs → src-DgX-tCDn.mjs} +2 -2
  44. package/storage/files.d.mts +1 -1
  45. package/storage/files.mjs +2 -2
  46. package/{store-CLGBhEpO.d.mts → store-Bx4dOx66.d.mts} +1 -1
  47. package/vector/index.mjs +1 -1
package/apps.d.mts CHANGED
@@ -1,19 +1,4 @@
1
1
  //#region src/apps.d.ts
2
- /**
3
- * For an app teams install on Midsummer: check that an event or a runner job Midsummer posted is
4
- * genuine, and act in an installation with a token that is renewed before it ends.
5
- *
6
- * import { InstalledApp, verifyEvent } from "@midsummer-ai/sdk/apps"
7
- *
8
- * // In your events handler, with the body exactly as it arrived:
9
- * if (!(await verifyEvent(request.headers, body, process.env.MIDSUMMER_SIGNING_SECRET!))) return new Response(null, { status: 401 })
10
- * const event = JSON.parse(body) as AppEvent
11
- *
12
- * const app = new InstalledApp({ id: "oapp_…", secret: process.env.MIDSUMMER_APP_SECRET! })
13
- * await app.request(event.data.installation.id, "/v1/mr/acme/web/mr-12/checks/style", { method: "PUT", body: JSON.stringify({ candidateId, state: "running" }) })
14
- *
15
- * Events are signed as Standard Webhooks says, so any of its libraries checks them too.
16
- */
17
2
  /** What Midsummer posts to an app: its `type`, when, and `data` with the installation it is for. */
18
3
  interface AppEvent<Data extends Record<string, unknown> = Record<string, unknown>> {
19
4
  readonly type: string;
package/apps.mjs CHANGED
@@ -1,4 +1,21 @@
1
+ import { r as platformApi } from "./here-BKjizr_N.mjs";
1
2
  //#region src/apps.ts
3
+ /**
4
+ * For an app teams install on Midsummer: check that an event or a runner job Midsummer posted is
5
+ * genuine, and act in an installation with a token that is renewed before it ends.
6
+ *
7
+ * import { InstalledApp, verifyEvent } from "@midsummer-ai/sdk/apps"
8
+ *
9
+ * // In your events handler, with the body exactly as it arrived:
10
+ * if (!(await verifyEvent(request.headers, body, process.env.MIDSUMMER_SIGNING_SECRET!))) return new Response(null, { status: 401 })
11
+ * const event = JSON.parse(body) as AppEvent
12
+ *
13
+ * const app = new InstalledApp({ id: "oapp_…", secret: process.env.MIDSUMMER_APP_SECRET! })
14
+ * await app.request(event.data.installation.id, "/v1/mr/acme/web/mr-12/checks/style", { method: "PUT", body: JSON.stringify({ candidateId, state: "running" }) })
15
+ *
16
+ * Events are signed as Standard Webhooks says, so any of its libraries checks them too. Calls go to `origin`, or to
17
+ * `MIDSUMMER_API` when left out, else the API of the site this package was published for.
18
+ */
2
19
  /** How old a signed event may be, and how far ahead of the clock: five minutes, as Standard Webhooks says. */
3
20
  const TOLERANCE_SECONDS = 300;
4
21
  /**
@@ -36,7 +53,7 @@ var InstalledApp = class {
36
53
  constructor(options) {
37
54
  this.#id = options.id;
38
55
  this.#secret = options.secret;
39
- this.#origin = (options.origin ?? "https://api.midsummer.new").replace(/\/+$/, "");
56
+ this.#origin = (options.origin ?? platformApi()).replace(/\/+$/, "");
40
57
  this.#fetch = options.fetch ?? fetch;
41
58
  }
42
59
  /** An installation's token (`msinst_…`), from the one kept while it has over a minute left. Throws when Midsummer refuses the app's secret or the installation. */
@@ -73,7 +90,7 @@ var InstalledApp = class {
73
90
  }
74
91
  /** Trades the one-time code a manifest's app was made with for its id, secret and signing secret, shown this once. */
75
92
  static async fromManifestCode(code, options = {}) {
76
- const origin = (options.origin ?? "https://api.midsummer.new").replace(/\/+$/, "");
93
+ const origin = (options.origin ?? platformApi()).replace(/\/+$/, "");
77
94
  const response = await (options.fetch ?? fetch)(`${origin}/v1/apps/conversions/${encodeURIComponent(code)}`, { method: "POST" });
78
95
  if (!response.ok) throw new Error(`Midsummer refused the code: HTTP ${response.status} ${await response.text()}`);
79
96
  return await response.json();
@@ -1,6 +1,6 @@
1
- import { n as AuthStore } from "../store-CLGBhEpO.mjs";
1
+ import { n as AuthStore } from "../store-Bx4dOx66.mjs";
2
2
  import { SignInMail } from "./mail.mjs";
3
- import { a as Auth } from "../index-C2Jo3JqT.mjs";
3
+ import { a as Auth } from "../index-C8Ti5raa.mjs";
4
4
  //#region src/auth/contract.d.ts
5
5
  interface AuthHarness {
6
6
  /** Sign-in over a store with no users, made with `scopes: CONTRACT_SCOPES`. */
package/auth/contract.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { c as LIMITS, l as LIMIT_WINDOW_MS, r as AuthError, u as SETTINGS_TTL_MS } from "../auth-BA5Xdi-4.mjs";
1
+ import { c as LIMITS, l as LIMIT_WINDOW_MS, r as AuthError, u as SETTINGS_TTL_MS } from "../auth-BaGXVRYd.mjs";
2
2
  import { IMPERSONATION_TOKEN_MS, startImpersonation } from "./impersonate.mjs";
3
3
  //#region src/auth/contract.ts
4
4
  /**
package/auth/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
- import { n as AuthStore, r as databaseStore, t as AuthCall } from "../store-CLGBhEpO.mjs";
1
+ import { n as AuthStore, r as databaseStore, t as AuthCall } from "../store-Bx4dOx66.mjs";
2
2
  import { i as SignUpPolicy, n as CaptchaProvider, r as PublicSettings, t as AuthSettings } from "../settings-Dz3irtlu.mjs";
3
3
  import { SignInMail, SignInMailKind, checkSignInMail, signInWords } from "./mail.mjs";
4
- import { _ as AuthEvent, a as Auth, b as EventSend, c as AuthOptions, d as IMPERSONATION_S, f as SESSION_CACHE_S, g as createAuth, h as auth, i as AgentKey, l as AuthUser, m as SignedInAgent, n as AUTH_ROUTES, o as AuthError, p as SignedIn, r as AgentGrant, s as AuthErrorCode, t as AUTH_PATH, u as AuthUserRecord, v as AuthEventBatch, y as AuthEventType } from "../index-C2Jo3JqT.mjs";
4
+ import { _ as AuthEvent, a as Auth, b as EventSend, c as AuthOptions, d as IMPERSONATION_S, f as SESSION_CACHE_S, g as createAuth, h as auth, i as AgentKey, l as AuthUser, m as SignedInAgent, n as AUTH_ROUTES, o as AuthError, p as SignedIn, r as AgentGrant, s as AuthErrorCode, t as AUTH_PATH, u as AuthUserRecord, v as AuthEventBatch, y as AuthEventType } from "../index-C8Ti5raa.mjs";
5
5
  import { i as authFiles } from "../files-B553U-d6.mjs";
6
6
  export { AUTH_PATH, AUTH_ROUTES, AgentGrant, AgentKey, Auth, type AuthCall, AuthError, AuthErrorCode, type AuthEvent, type AuthEventBatch, type AuthEventType, AuthOptions, type AuthSettings, type AuthStore, AuthUser, AuthUserRecord, type CaptchaProvider, type EventSend, IMPERSONATION_S, type PublicSettings, SESSION_CACHE_S, type SignInMail, type SignInMailKind, type SignUpPolicy, SignedIn, SignedInAgent, auth, authFiles, checkSignInMail, createAuth, databaseStore, signInWords };
package/auth/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { a as SESSION_CACHE_S, d as databaseStore, i as IMPERSONATION_S, n as AUTH_ROUTES, o as auth, r as AuthError, s as createAuth, t as AUTH_PATH } from "../auth-BA5Xdi-4.mjs";
1
+ import { a as SESSION_CACHE_S, d as databaseStore, i as IMPERSONATION_S, n as AUTH_ROUTES, o as auth, r as AuthError, s as createAuth, t as AUTH_PATH } from "../auth-BaGXVRYd.mjs";
2
2
  import { checkSignInMail, signInWords } from "./mail.mjs";
3
3
  import { n as authFiles } from "../files-DhPeWwcT.mjs";
4
4
  export { AUTH_PATH, AUTH_ROUTES, AuthError, IMPERSONATION_S, SESSION_CACHE_S, auth, authFiles, checkSignInMail, createAuth, databaseStore, signInWords };
package/auth/mail.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { r as processEnv, t as here } from "../here-1plIxb0D.mjs";
2
- import { l as email, t as EmailError } from "../email-ynNo0HqT.mjs";
1
+ import { i as processEnv, t as here } from "../here-BKjizr_N.mjs";
2
+ import { l as email, t as EmailError } from "../email-DmWG_Ug-.mjs";
3
3
  //#region src/auth/mail.ts
4
4
  /**
5
5
  * The mail sign-in sends: a code, a sign-in link, an email check, a password reset, an invitation or a sign-in from
package/auth/password.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { i as hashPassword$1, o as verifyPassword$1 } from "../engine-DDPJ2CET.mjs";
1
+ import { i as hashPassword$1, o as verifyPassword$1 } from "../engine-BmiPioJ2.mjs";
2
2
  import { isBcrypt as isBcrypt$1 } from "./import.mjs";
3
3
  //#region src/auth/password.ts
4
4
  /**
package/auth/react.d.mts CHANGED
@@ -12,11 +12,16 @@ interface AuthState {
12
12
  /** Reads who is signed in again. */
13
13
  refresh(): Promise<void>;
14
14
  }
15
- /** Gives the screens inside it the client and who is signed in; `baseURL` when the app's server is on another address. */
15
+ /**
16
+ * Gives the screens inside it the client and who is signed in; `baseURL` when the app's server is on another address.
17
+ * With `database`, the hooks of `@midsummer-ai/sdk/react` inside it (`useQuery`, `useMutation`) reach the app's
18
+ * database as whoever is signed in (`signedInDatabase`), and connect again as someone signs in or out.
19
+ */
16
20
  declare function AuthProvider(props: {
17
21
  readonly children?: ReactNode;
18
22
  readonly client?: AuthClient;
19
23
  readonly baseURL?: string;
24
+ readonly database?: boolean;
20
25
  }): import("react").FunctionComponentElement<import("react").ProviderProps<AuthState | null>>;
21
26
  /** Who is signed in, the client and the app's settings: inside an `AuthProvider`. */
22
27
  declare function useAuth(): AuthState;
@@ -31,7 +36,7 @@ declare function SignIn(props: {
31
36
  readonly wide?: boolean;
32
37
  readonly appear?: boolean;
33
38
  readonly children?: ReactNode;
34
- }> | null;
39
+ }> | import("react").FunctionComponentElement<{}>;
35
40
  /** Sign-up with a name, an email and a password; under a waitlist, joining it; when the app wants emails confirmed, asking for that. */
36
41
  declare function SignUp(props: {
37
42
  readonly redirectTo?: string;
package/auth/react.mjs CHANGED
@@ -1,4 +1,6 @@
1
- import { a as WORDS, c as deviceOf, i as CAPTCHA_WIDGETS, l as setupKeyOf, o as ago, r as AUTH_CSS, t as qrPath } from "../qr-CBh0LP2_.mjs";
1
+ import { c as signedInDatabase, t as DATABASE_TOKEN_PATH } from "../client-_Al5bpqX.mjs";
2
+ import { DatabaseProvider } from "../database/react.mjs";
3
+ import { a as WORDS, c as deviceOf, i as CAPTCHA_WIDGETS, l as setupKeyOf, o as ago, r as AUTH_CSS, t as qrPath } from "../qr-Br1sOBmT.mjs";
2
4
  import { AuthClientError, authClient } from "./client.mjs";
3
5
  import { Fragment, createContext, createElement, useCallback, useContext, useEffect, useId, useMemo, useRef, useState } from "react";
4
6
  //#region src/auth/react.ts
@@ -17,10 +19,15 @@ import { Fragment, createContext, createElement, useCallback, useContext, useEff
17
19
  * roles and invitations). They show only the ways in the app's settings turn on, in the app's name, logo and colour,
18
20
  * with the app's own font; set `--msa-accent`, `--msa-radius` and the other variables in `./look.ts` on any parent to
19
21
  * match the app's look. `useAuth()` gives any component who is signed in. The hosted pages under `/api/auth/ui` are
20
- * the same screens for apps that don't use React.
22
+ * the same screens for apps that don't use React. `<AuthProvider database>` also gives the components inside it the
23
+ * app's database as whoever is signed in, for `useQuery` and the other hooks of `@midsummer-ai/sdk/react`.
21
24
  */
22
25
  const Context = createContext(null);
23
- /** Gives the screens inside it the client and who is signed in; `baseURL` when the app's server is on another address. */
26
+ /**
27
+ * Gives the screens inside it the client and who is signed in; `baseURL` when the app's server is on another address.
28
+ * With `database`, the hooks of `@midsummer-ai/sdk/react` inside it (`useQuery`, `useMutation`) reach the app's
29
+ * database as whoever is signed in (`signedInDatabase`), and connect again as someone signs in or out.
30
+ */
24
31
  function AuthProvider(props) {
25
32
  const client = useMemo(() => props.client ?? authClient(props.baseURL ? { baseURL: props.baseURL } : {}), [props.client, props.baseURL]);
26
33
  const [signedIn, setSignedIn] = useState(void 0);
@@ -28,11 +35,19 @@ function AuthProvider(props) {
28
35
  const refresh = useCallback(async () => {
29
36
  setSignedIn(await client.session().catch(() => null));
30
37
  }, [client]);
38
+ const db = useMemo(() => props.database ? signedInDatabase({ url: `${props.baseURL?.replace(/\/+$/u, "") ?? ""}${DATABASE_TOKEN_PATH}` }) : null, [props.database, props.baseURL]);
31
39
  useEffect(() => {
32
40
  refresh();
33
41
  client.settings().then(setSettings, () => {});
34
- return client.onChange(() => void refresh());
35
- }, [client, refresh]);
42
+ return client.onChange(() => {
43
+ refresh();
44
+ db?.signedInChanged();
45
+ });
46
+ }, [
47
+ client,
48
+ refresh,
49
+ db
50
+ ]);
36
51
  const state = useMemo(() => ({
37
52
  client,
38
53
  signedIn,
@@ -44,7 +59,7 @@ function AuthProvider(props) {
44
59
  settings,
45
60
  refresh
46
61
  ]);
47
- return createElement(Context.Provider, { value: state }, createElement("style", { "data-msa": "" }, AUTH_CSS), props.children);
62
+ return createElement(Context.Provider, { value: state }, createElement("style", { "data-msa": "" }, AUTH_CSS), db ? createElement(DatabaseProvider, { client: db }, props.children) : props.children);
48
63
  }
49
64
  /** Who is signed in, the client and the app's settings: inside an `AuthProvider`. */
50
65
  function useAuth() {
@@ -215,6 +230,23 @@ function Confirm(props) {
215
230
  const go = (to) => {
216
231
  globalThis.location.href = to;
217
232
  };
233
+ /**
234
+ * The sign-in screen while the app's settings load: its title, the email and password fields, button and links as bones
235
+ * the size of each, so the form takes their place without anything moving. It shows only after a moment, so a quick
236
+ * load shows none.
237
+ */
238
+ function SignInLoading() {
239
+ const bone = (width, className = "msa-bone") => createElement("span", {
240
+ className,
241
+ style: { width }
242
+ });
243
+ return createElement("div", {
244
+ className: "msa",
245
+ role: "status",
246
+ "aria-busy": true,
247
+ "aria-label": WORDS.signIn.loading
248
+ }, createElement("div", { className: "msa-card msa-loading" }, createElement("h1", null, bone("60%")), createElement("div", { className: "msa-bones" }, ...[WORDS.fields.email, WORDS.fields.password].map((label) => createElement("label", { key: label }, bone("30%"), bone("100%", "msa-bone msa-bone-control"))), createElement("span", null), bone("100%", "msa-bone msa-bone-control")), createElement("p", { className: "msa-foot" }, bone("70%", "msa-bone msa-bone-inline"))));
249
+ }
218
250
  /** Sign-in: a password, a code or a link by mail, a passkey, Google or GitHub, as the app's settings turn them on. */
219
251
  function SignIn(props) {
220
252
  const { client, settings } = useAuth();
@@ -222,7 +254,8 @@ function SignIn(props) {
222
254
  const [email, setEmail] = useState(""), [password, setPassword] = useState(""), [code, setCode] = useState(""), [captcha, setCaptcha] = useState("");
223
255
  const call = useCall();
224
256
  const done = () => go(props.redirectTo ?? "/");
225
- if (!settings) return null;
257
+ const [shaped] = useState(() => !settings);
258
+ if (!settings) return createElement(SignInLoading);
226
259
  const methods = settings.methods, app = settings.branding.name;
227
260
  const withCaptcha = captcha ? { captcha } : {};
228
261
  if (mode === "two-factor" || mode === "mail-code" || mode === "backup") return createElement(Card, {
@@ -287,7 +320,10 @@ function SignIn(props) {
287
320
  }, WORDS.confirm.back)));
288
321
  const social = ["google", "github"].filter((one) => methods[one]);
289
322
  const byMail = methods.code ? "code" : methods.link ? "link" : null;
290
- return createElement(Card, { title: WORDS.signIn.title(app) }, social.length || methods.passkey ? createElement("div", { className: "msa-stack" }, ...social.map((provider) => createElement("button", {
323
+ return createElement(Card, {
324
+ title: WORDS.signIn.title(app),
325
+ appear: shaped
326
+ }, social.length || methods.passkey ? createElement("div", { className: "msa-stack" }, ...social.map((provider) => createElement("button", {
291
327
  key: provider,
292
328
  type: "button",
293
329
  onClick: () => void call.run(() => client.signIn.social(provider, { callbackURL: props.redirectTo ?? "/" }))
@@ -1,4 +1,4 @@
1
- import { n as AuthStore } from "../store-CLGBhEpO.mjs";
1
+ import { n as AuthStore } from "../store-Bx4dOx66.mjs";
2
2
  import { SignInMail } from "./mail.mjs";
3
3
  //#region src/auth/stand-in.d.ts
4
4
  interface StandIn {
@@ -1,10 +1,10 @@
1
- import { r as processEnv, t as here } from "./here-1plIxb0D.mjs";
2
- import { connect } from "./database/client.mjs";
1
+ import { i as processEnv, r as platformApi, t as here } from "./here-BKjizr_N.mjs";
2
+ import { a as connect, l as userToken, o as databaseUrl } from "./client-_Al5bpqX.mjs";
3
3
  import { sendSignInMail } from "./auth/mail.mjs";
4
4
  import "./files-DhPeWwcT.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-DDPJ2CET.mjs";
7
- import { a as WORDS, c as deviceOf, i as CAPTCHA_WIDGETS, l as setupKeyOf, n as qrSvg, o as ago, r as AUTH_CSS, s as defaultAppName } from "./qr-CBh0LP2_.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-BmiPioJ2.mjs";
7
+ import { a as WORDS, c as deviceOf, i as CAPTCHA_WIDGETS, l as setupKeyOf, n as qrSvg, o as ago, r as AUTH_CSS, s as defaultAppName } from "./qr-Br1sOBmT.mjs";
8
8
  //#region src/auth/store.ts
9
9
  const KIND = {
10
10
  create: "mutation",
@@ -1119,6 +1119,8 @@ ${kind === "sign-in" ? `const o=await p("/passkey/generate-authenticate-options"
1119
1119
  * or (signing in) `{ twoFactor: true }`; `POST /api/auth/sign-out` returns `{ signedOut: true }`; `GET /api/auth/session`
1120
1120
  * returns `{ user, session }` or `null`. The session's cookie is `midsummer.session_token` (`__Secure-midsummer.…` over
1121
1121
  * https). Signing up signs the person in at once unless the settings' `confirmEmail` is on (`./settings.ts`).
1122
+ * `GET /api/auth/database-token` returns `{ url, token, expiresAt }`: the app's database and a token for the person
1123
+ * signed in, which the page's `signedInDatabase()` (`<AuthProvider database>`) connects with, or `{ url }` for no one.
1122
1124
  *
1123
1125
  * Email and password, codes and sign-in links by mail, Google and GitHub when their keys are set, organizations with
1124
1126
  * roles and invitations, two-factor codes, passkeys and bans. Browsers talk to it with `@midsummer-ai/sdk/auth/client`,
@@ -1194,7 +1196,6 @@ const AUTH_ROUTES = {
1194
1196
  };
1195
1197
  /** How many addresses one process keeps sign-in ready for; past it the oldest is made again when asked. */
1196
1198
  const KEPT_ORIGINS = 16;
1197
- const API = "https://api.midsummer.new";
1198
1199
  const METADATA_BYTES = 8192;
1199
1200
  /** The origin a request came to, as the dispatcher routed it. */
1200
1201
  function originOf(request) {
@@ -1232,7 +1233,7 @@ function storeFor(options, env) {
1232
1233
  const key = options.key ?? env.DATABASE_KEY;
1233
1234
  if (key) return databaseStore(requestDatabase(() => connect({
1234
1235
  key,
1235
- api: options.api ?? API
1236
+ api: options.api ?? platformApi()
1236
1237
  }), "its database with DATABASE_KEY"));
1237
1238
  if (env.MIDSUMMER_DEV === "1") return devStore();
1238
1239
  throw new AuthError("forbidden", "Sign-in has no database here. Outside Midsummer, set DATABASE_KEY to a database key (midsummer db key create). In tests, give createAuth the Stand-in: createAuth({ store: standIn().store }).");
@@ -1250,7 +1251,7 @@ function eventSendFor(options, env) {
1250
1251
  };
1251
1252
  if (here() || options.store) return null;
1252
1253
  const key = options.key ?? env.DATABASE_KEY;
1253
- if (key) return httpSend(`${options.api ?? API}/v1/db/auth/events`, { key });
1254
+ if (key) return httpSend(`${options.api ?? platformApi()}/v1/db/auth/events`, { key });
1254
1255
  const workload = env.MIDSUMMER_IDENTITY_REQUEST_TOKEN, tokenUrl = env.MIDSUMMER_IDENTITY_TOKEN_URL;
1255
1256
  if (workload && tokenUrl) return httpSend(`${new URL(tokenUrl).origin}/v1/db/auth/events`, {
1256
1257
  workload,
@@ -1263,7 +1264,7 @@ function remoteMailFor(options, env) {
1263
1264
  if (here() || options.store) return void 0;
1264
1265
  const key = options.key ?? env.DATABASE_KEY;
1265
1266
  if (key) return {
1266
- url: `${options.api ?? API}/v1/db/auth/mail`,
1267
+ url: `${options.api ?? platformApi()}/v1/db/auth/mail`,
1267
1268
  proof: { key }
1268
1269
  };
1269
1270
  const workload = env.MIDSUMMER_IDENTITY_REQUEST_TOKEN, tokenUrl = env.MIDSUMMER_IDENTITY_TOKEN_URL;
@@ -1788,6 +1789,34 @@ function createAuth(options = {}) {
1788
1789
  message: "There's nothing here."
1789
1790
  });
1790
1791
  }
1792
+ /**
1793
+ * `GET /api/auth/database-token`, for the page's `signedInDatabase()`: the address of the database sign-in keeps its
1794
+ * rows in, and for someone signed in a ten-minute token that runs the app's public functions as them (`userToken`),
1795
+ * never internal ones. Someone not signed in gets the address alone, to connect as no one. On Midsummer it needs no
1796
+ * key, previews and `midsummer dev` included; elsewhere it uses the database key sign-in uses.
1797
+ */
1798
+ async function databaseToken(request) {
1799
+ const answer = (status, body) => Response.json(body, {
1800
+ status,
1801
+ headers: { "Cache-Control": "no-store" }
1802
+ });
1803
+ const { env } = base();
1804
+ const key = options.key ?? env.DATABASE_KEY;
1805
+ const where = options.store ? void 0 : here() || env.MIDSUMMER_DATABASE_URL || env.MIDSUMMER_IDENTITY_REQUEST_TOKEN ? { binding: options.database ?? "main" } : key ? {
1806
+ key,
1807
+ api: options.api ?? platformApi()
1808
+ } : void 0;
1809
+ if (!where) return answer(503, {
1810
+ code: "UNAVAILABLE",
1811
+ message: "Sign-in keeps no database here, so there's no database token to give. Run under midsummer dev with a db/ folder, or set DATABASE_KEY outside Midsummer."
1812
+ });
1813
+ const signedIn = await person(request);
1814
+ if (!signedIn) return answer(200, { url: await databaseUrl(where) });
1815
+ return answer(200, await userToken({
1816
+ ...where,
1817
+ as: signedIn.user.id
1818
+ }));
1819
+ }
1791
1820
  /** Serves one request under `/api/auth`: settings, account calls, hosted pages, the guard, then the engine. */
1792
1821
  async function serve(request) {
1793
1822
  const origin = originOf(request), { pathname } = new URL(request.url);
@@ -1810,6 +1839,7 @@ function createAuth(options = {}) {
1810
1839
  ...offered,
1811
1840
  scopes: settingsOfAgents.scopes
1812
1841
  }, { headers: { "Cache-Control": "no-store" } });
1842
+ if (`/api/auth${path}` === "/api/auth/database-token" && request.method === "GET") return databaseToken(request);
1813
1843
  if (path.startsWith("/account/")) return account(request, path.slice(8), origin);
1814
1844
  if (pathname === CONSENT_PATH && settingsOfAgents.consentPage === CONSENT_PATH) return consent(request, origin, engine, appName);
1815
1845
  if (path.startsWith("/ui/")) return hostedPage(request, path.slice(3), {
package/billing/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { n as late, r as processEnv, t as here } from "../here-1plIxb0D.mjs";
1
+ import { i as processEnv, n as late, t as here } from "../here-BKjizr_N.mjs";
2
2
  //#region src/billing/index.ts
3
3
  /**
4
4
  * Billing from a Project's code: send a customer to a checkout, and ask what they have bought. Each customer is your