@midsummer-ai/sdk 0.1.0-staging.3695 → 0.1.0-staging.3698

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.
@@ -1,6 +1,6 @@
1
- import { n as AuthStore } from "../store-CeIGY-xT.mjs";
1
+ import { n as AuthStore } from "../store-DsWS_MpS.mjs";
2
2
  import { SignInMail } from "./mail.mjs";
3
- import { i as Auth } from "../index-CsgQ_ouh.mjs";
3
+ import { i as Auth } from "../index-Cc5gCPfV.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/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
- import { n as AuthStore, r as databaseStore, t as AuthCall } from "../store-CeIGY-xT.mjs";
1
+ import { n as AuthStore, r as databaseStore, t as AuthCall } from "../store-DsWS_MpS.mjs";
2
2
  import { i as SignUpPolicy, n as CaptchaProvider, r as PublicSettings, t as AuthSettings } from "../settings-si8iBtwc.mjs";
3
3
  import { SignInMail, SignInMailKind, checkSignInMail, signInWords } from "./mail.mjs";
4
- import { _ as AuthEventBatch, a as AuthError, c as AuthUser, d as SESSION_CACHE_S, f as SignedIn, g as AuthEvent, h as createAuth, i as Auth, l as AuthUserRecord, m as auth, n as AgentGrant, o as AuthErrorCode, p as SignedInAgent, r as AgentKey, s as AuthOptions, t as AUTH_PATH, u as IMPERSONATION_S, v as AuthEventType, y as EventSend } from "../index-CsgQ_ouh.mjs";
4
+ import { _ as AuthEventBatch, a as AuthError, c as AuthUser, d as SESSION_CACHE_S, f as SignedIn, g as AuthEvent, h as createAuth, i as Auth, l as AuthUserRecord, m as auth, n as AgentGrant, o as AuthErrorCode, p as SignedInAgent, r as AgentKey, s as AuthOptions, t as AUTH_PATH, u as IMPERSONATION_S, v as AuthEventType, y as EventSend } from "../index-Cc5gCPfV.mjs";
5
5
  import { i as authFiles } from "../files-C-FvBxT4.mjs";
6
6
  export { AUTH_PATH, 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 };
@@ -1,4 +1,4 @@
1
- import { n as AuthStore } from "../store-CeIGY-xT.mjs";
1
+ import { n as AuthStore } from "../store-DsWS_MpS.mjs";
2
2
  import { SignInMail } from "./mail.mjs";
3
3
  //#region src/auth/stand-in.d.ts
4
4
  interface StandIn {
@@ -1,4 +1,4 @@
1
- import { t as DatabaseError } from "./errors-x2vXKi0u.mjs";
1
+ import { t as DatabaseError } from "./errors-CnQduDOt.mjs";
2
2
  //#region src/database/client.d.ts
3
3
  /** A function's reference, as `api.<file>.<export>` holds it, typed by its arguments and its answer. */
4
4
  interface Reference<Kind extends "query" | "mutation" | "action", Args = any, Returns = any> {
@@ -1,3 +1,3 @@
1
- import { n as DatabaseErrorCode, t as DatabaseError } from "../errors-x2vXKi0u.mjs";
2
- import { a as Database, c as Reference, d as clerk, f as connect, i as ConnectOptions, l as ReturnsOf, m as userToken, n as AuthState, o as DatabaseClient, p as oidc, r as BoundDatabases, s as FetchToken, t as ArgsOf, u as auth0 } from "../client-BshDU1b2.mjs";
1
+ import { n as DatabaseErrorCode, t as DatabaseError } from "../errors-CnQduDOt.mjs";
2
+ import { a as Database, c as Reference, d as clerk, f as connect, i as ConnectOptions, l as ReturnsOf, m as userToken, n as AuthState, o as DatabaseClient, p as oidc, r as BoundDatabases, s as FetchToken, t as ArgsOf, u as auth0 } from "../client-D3BZs3LA.mjs";
3
3
  export { ArgsOf, AuthState, BoundDatabases, ConnectOptions, Database, DatabaseClient, DatabaseError, type DatabaseErrorCode, FetchToken, Reference, ReturnsOf, auth0, clerk, connect, oidc, userToken };
@@ -30,7 +30,9 @@ import { t as callKey } from "../key-BrRdFY-n.mjs";
30
30
  * const db = await connect({ key: process.env.DATABASE_KEY! })
31
31
  *
32
32
  * One connection carries every call. A mutation is tagged, so one sent again after a lost
33
- * connection applies once; a watch is given its value now and again after every change. Every
33
+ * connection applies once; a watch is given its value now and again after every change. A query or a
34
+ * watch the Database refuses while it catches up after a restart is read again for a few seconds
35
+ * before it gives `history_unavailable`; a mutation or an action refused so throws at once. Every
34
36
  * refusal is a `DatabaseError` whose `code`, a `DatabaseErrorCode`, tells you why; a connection the
35
37
  * Database refuses is `unauthenticated`, with its reason.
36
38
  *
@@ -110,12 +112,49 @@ async function keyToken(api, key, fresh = false) {
110
112
  });
111
113
  return asked;
112
114
  }
113
- const refusal = (answer) => new DatabaseError(answer?.why ?? answer?.err ?? "refused", answer?.error ?? answer?.why ?? "refused");
115
+ const isRefusal = (value) => !!value && typeof value === "object" && !Array.isArray(value) && value.ok === false;
116
+ /**
117
+ * Whether the database refused a call because it could not read its rows as of the moment the call reads at: it is
118
+ * rebuilding what it keeps after a restart, and says `reads below lsn <n> are no longer kept` in the refusal's error
119
+ * (or, once it codes it, `history_unavailable`). It clears within seconds, so a read refused so is tried again.
120
+ */
121
+ const HISTORY_GONE = /reads below lsn \d+ are no longer kept/u;
122
+ const historyGone = (answer) => (answer.why ?? answer.err) === "history_unavailable" || (answer.why ?? answer.err) === "unavailable" && HISTORY_GONE.test(answer.error ?? "");
123
+ /** How long a read refused `history_unavailable` waits before each try again: 3 seconds in all. */
124
+ const HISTORY_RETRY_MS = [
125
+ 100,
126
+ 200,
127
+ 400,
128
+ 800,
129
+ 1500
130
+ ];
131
+ const HISTORY_DOCS = "https://docs.midsummer.new/guides/database/troubleshooting#history-unavailable";
132
+ /** The `DatabaseError` for a refused call; `retried` is set for a read the client already tried again. */
133
+ const refusal = (answer, retried = false) => {
134
+ if (answer && historyGone(answer)) {
135
+ const tried = retried ? `The client tried again for ${HISTORY_RETRY_MS.reduce((sum, ms) => sum + ms, 0) / 1e3} seconds. ` : "";
136
+ return new DatabaseError("history_unavailable", `The database couldn't read the rows this call needs because it's catching up after a restart. This is temporary. ${tried}Try again in a moment; a mutation sent again with the same tag applies once. If it keeps happening, see ${HISTORY_DOCS}`, { why: answer.error });
137
+ }
138
+ return new DatabaseError(answer?.why ?? answer?.err ?? "refused", answer?.error ?? answer?.why ?? "refused");
139
+ };
114
140
  const caught = (error) => error?.answer ?? {
115
141
  ok: false,
116
142
  why: error?.code ?? "refused",
117
143
  error: error?.message
118
144
  };
145
+ /**
146
+ * A read's value, or its refusal as an `Answer`, tried again while the database refuses it `history_unavailable`,
147
+ * at the waits of `HISTORY_RETRY_MS`. Only reads come here: a mutation or an action refused so is thrown at once.
148
+ */
149
+ async function readAgain(read) {
150
+ let value = await read().catch(caught);
151
+ for (const wait of HISTORY_RETRY_MS) {
152
+ if (!isRefusal(value) || !historyGone(value)) return value;
153
+ await new Promise((resolve) => setTimeout(resolve, wait));
154
+ value = await read().catch(caught);
155
+ }
156
+ return value;
157
+ }
119
158
  const told = (listener, outcome) => "error" in outcome ? listener.onError?.(outcome.error) : listener.onValue(outcome.value);
120
159
  /** Why the Database refused a connection, from the reason it gives at the socket's address over HTTP; null when it gave none. */
121
160
  async function refusalAt(url, token) {
@@ -276,8 +315,8 @@ async function opened(url, token, refresh, server, key) {
276
315
  const watching = /* @__PURE__ */ new Map();
277
316
  return {
278
317
  async query(fn, ...[args]) {
279
- const value = await connection.query(call(fn), sent(args)).catch(caught);
280
- if (value && typeof value === "object" && !Array.isArray(value) && value.ok === false) throw refusal(value);
318
+ const value = await readAgain(() => connection.query(call(fn), sent(args)));
319
+ if (isRefusal(value)) throw refusal(value, true);
281
320
  return got(value);
282
321
  },
283
322
  async mutation(fn, ...[args, { tag } = {}]) {
@@ -312,14 +351,14 @@ async function opened(url, token, refresh, server, key) {
312
351
  return;
313
352
  }
314
353
  const held = item?.ok === false ? item : meta?.error;
315
- if (held) {
354
+ if (held && !historyGone(held)) {
316
355
  tell({ error: refusal(held) });
317
356
  return;
318
357
  }
319
358
  const n = asked += 1;
320
- connection.query(call(fn), sent(args)).catch(caught).then((answer) => {
359
+ readAgain(() => connection.query(call(fn), sent(args))).then((answer) => {
321
360
  if (n !== asked || watching.get(key) !== it) return;
322
- if (answer && typeof answer === "object" && !Array.isArray(answer) && answer.ok === false) tell({ error: refusal(answer) });
361
+ if (isRefusal(answer)) tell({ error: refusal(answer, true) });
323
362
  else tell({ value: got(answer ?? null) });
324
363
  });
325
364
  });
@@ -1,2 +1,2 @@
1
- import { n as DatabaseErrorCode, t as DatabaseError } from "../errors-x2vXKi0u.mjs";
1
+ import { n as DatabaseErrorCode, t as DatabaseError } from "../errors-CnQduDOt.mjs";
2
2
  export { DatabaseError, DatabaseErrorCode };
@@ -1,5 +1,5 @@
1
- import { t as DatabaseError } from "../errors-x2vXKi0u.mjs";
2
- import { a as Database, c as Reference } from "../client-BshDU1b2.mjs";
1
+ import { t as DatabaseError } from "../errors-CnQduDOt.mjs";
2
+ import { a as Database, c as Reference } from "../client-D3BZs3LA.mjs";
3
3
  import { ReactNode } from "react";
4
4
  //#region src/database/react.d.ts
5
5
  type ArgsOf<F> = F extends Reference<any, infer A, any> ? A : never;
@@ -1,4 +1,4 @@
1
- import { t as DatabaseError } from "../errors-x2vXKi0u.mjs";
1
+ import { t as DatabaseError } from "../errors-CnQduDOt.mjs";
2
2
  import { p as Storage } from "../index-CXiY5knc.mjs";
3
3
  import { t as AuthConfig } from "../auth-B7rCtXFQ.mjs";
4
4
  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";
@@ -14,8 +14,11 @@
14
14
  * - `unauthenticated`: the Database took no caller from the connection: its token did not verify (expired, forged, for
15
15
  * another audience, from an issuer it doesn't trust), or there was none and the Database admits no anonymous callers.
16
16
  * - `unavailable`: the Database could not be reached, or could not finish the call.
17
+ * - `history_unavailable`: the database could not read the rows the call needs as of the moment it reads at, because
18
+ * it is catching up after a restart. It is temporary: a query is tried again for a few seconds before this is
19
+ * thrown, and a mutation may be sent again with the same tag.
17
20
  */
18
- type DatabaseErrorCode = "refused" | "short" | "notfound" | "invalid" | "internal" | "no function" | "busy" | "quota" | "paused" | "denied" | "unauthenticated" | "unavailable";
21
+ type DatabaseErrorCode = "refused" | "short" | "notfound" | "invalid" | "internal" | "no function" | "busy" | "quota" | "paused" | "denied" | "unauthenticated" | "unavailable" | "history_unavailable";
19
22
  declare class DatabaseError extends Error {
20
23
  readonly code: DatabaseErrorCode | (string & {});
21
24
  readonly detail?: unknown;
@@ -1,4 +1,4 @@
1
- import { n as AuthStore } from "./store-CeIGY-xT.mjs";
1
+ import { n as AuthStore } from "./store-DsWS_MpS.mjs";
2
2
  import { n as CaptchaProvider, t as AuthSettings } from "./settings-si8iBtwc.mjs";
3
3
  import { SignInMail } from "./auth/mail.mjs";
4
4
  //#region src/auth/events.d.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midsummer-ai/sdk",
3
- "version": "0.1.0-staging.3695",
3
+ "version": "0.1.0-staging.3698",
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.",
@@ -1,4 +1,4 @@
1
- import { a as Database } from "./client-BshDU1b2.mjs";
1
+ import { a as Database } from "./client-D3BZs3LA.mjs";
2
2
  //#region src/auth/store.d.ts
3
3
  /** One of `db/auth.ts`'s functions. */
4
4
  type AuthCall = "create" | "findOne" | "findMany" | "count" | "update" | "updateMany" | "deleteMany" | "consume" | "increment" | "page";