@midsummer-ai/sdk 0.1.0-staging.3775 → 0.1.0-staging.3778
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.d.mts +2 -2
- package/auth/contract.mjs +1 -1
- package/auth/index.d.mts +2 -2
- package/auth/index.mjs +1 -1
- package/auth/react.mjs +1 -1
- package/auth/stand-in.d.mts +1 -1
- package/{auth-Bu4FIk-C.mjs → auth-DBp5FHvK.mjs} +1 -1
- package/{client-COPmSTKM.d.mts → client-BDjPQa7G.d.mts} +21 -2
- package/{client-G20sA352.mjs → client-CIuYlRWZ.mjs} +559 -51
- package/{connection-C_JSWUrt.mjs → connection-CpT0x1EE.mjs} +3 -3
- package/database/client.d.mts +2 -2
- package/database/client.mjs +1 -1
- package/database/react.d.mts +1 -1
- package/{index-q1gV7MyO.d.mts → index-C0ykY_s9.d.mts} +1 -1
- package/package.json +1 -1
- package/{store-BpA6Q5Z_.d.mts → store-CRqU5WCe.d.mts} +1 -1
- package/{store-CsBhnabU.mjs → store-Ch931wwA.mjs} +1 -1
- package/{tabs-gHJHkhm2.mjs → tabs-C4NDJzfK.mjs} +1 -1
package/auth/contract.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { n as AuthStore } from "../store-
|
|
1
|
+
import { n as AuthStore } from "../store-CRqU5WCe.mjs";
|
|
2
2
|
import { SignInMail } from "./mail.mjs";
|
|
3
|
-
import { a as Auth } from "../index-
|
|
3
|
+
import { a as Auth } from "../index-C0ykY_s9.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-
|
|
1
|
+
import { c as LIMITS, l as LIMIT_WINDOW_MS, r as AuthError, u as SETTINGS_TTL_MS } from "../auth-DBp5FHvK.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-
|
|
1
|
+
import { n as AuthStore, r as databaseStore, t as AuthCall } from "../store-CRqU5WCe.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-
|
|
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-C0ykY_s9.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-
|
|
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-DBp5FHvK.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/react.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as DATABASE_TOKEN_PATH, u as signedInDatabase } from "../client-
|
|
1
|
+
import { t as DATABASE_TOKEN_PATH, u as signedInDatabase } from "../client-CIuYlRWZ.mjs";
|
|
2
2
|
import { DatabaseProvider } from "../database/react.mjs";
|
|
3
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";
|
|
4
4
|
import { AuthClientError, authClient } from "./client.mjs";
|
package/auth/stand-in.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { i as processEnv, r as platformApi, t as here } from "./here-BKjizr_N.mjs";
|
|
2
|
-
import { d as userToken, o as connect, s as databaseUrl } from "./client-
|
|
2
|
+
import { d as userToken, o as connect, s as databaseUrl } from "./client-CIuYlRWZ.mjs";
|
|
3
3
|
import { sendSignInMail } from "./auth/mail.mjs";
|
|
4
4
|
import "./files-DhPeWwcT.mjs";
|
|
5
5
|
import { standIn } from "./auth/stand-in.mjs";
|
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import { t as DatabaseError } from "./errors-CnQduDOt.mjs";
|
|
2
|
+
//#region src/database/door-socket.d.ts
|
|
3
|
+
/** A TCP + TLS socket as the runtime opens one: its two streams, when it opened and closed, and `close`. */
|
|
4
|
+
interface StreamSocket {
|
|
5
|
+
readonly readable: ReadableStream<Uint8Array>;
|
|
6
|
+
readonly writable: WritableStream<Uint8Array>;
|
|
7
|
+
readonly opened?: Promise<unknown>;
|
|
8
|
+
readonly closed?: Promise<unknown>;
|
|
9
|
+
close(): unknown;
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
2
12
|
//#region src/database/client.d.ts
|
|
3
13
|
/** A function's reference, as `api.<file>.<export>` holds it, typed by its arguments and its answer. */
|
|
4
14
|
interface Reference<Kind extends "query" | "mutation" | "action", Args = any, Returns = any> {
|
|
@@ -60,6 +70,10 @@ interface DatabaseToken {
|
|
|
60
70
|
readonly url: string;
|
|
61
71
|
readonly expiresAt: number;
|
|
62
72
|
}
|
|
73
|
+
/** A door a request hands its server: the Database's address and token, and where its socket is (`host:port`), where it has one. */
|
|
74
|
+
interface DoorToken extends DatabaseToken {
|
|
75
|
+
readonly socket?: string;
|
|
76
|
+
}
|
|
63
77
|
/**
|
|
64
78
|
* What Midsummer hands a Deployment as `context.database`: a token and address for a Database its Environment binds,
|
|
65
79
|
* as the app's server, and (`userToken`) as one of the app's users, for that user's browser.
|
|
@@ -71,7 +85,12 @@ interface BoundDatabases {
|
|
|
71
85
|
* Where the app's server calls the Database bound as `binding`, with its token and when that runs out, handed in
|
|
72
86
|
* with the request, so connecting asks for nothing. Undefined where the request was handed none: `token` serves.
|
|
73
87
|
*/
|
|
74
|
-
door?(binding: string):
|
|
88
|
+
door?(binding: string): DoorToken | undefined;
|
|
89
|
+
/**
|
|
90
|
+
* Opens a TCP + TLS socket to `address` (`host:port`, a door's `socket`) for the request being served, which lives
|
|
91
|
+
* only as long as that request. Undefined where the runtime has none: every call goes over HTTP.
|
|
92
|
+
*/
|
|
93
|
+
socket?(address: string): StreamSocket | Promise<StreamSocket>;
|
|
75
94
|
/**
|
|
76
95
|
* Has the Database bound as `binding` made again after its door returned that it has none for a call sent at `since`
|
|
77
96
|
* (as after a move, its rows gone): true when that call should be sent once more.
|
|
@@ -220,4 +239,4 @@ declare function clerk(getToken: (options: {
|
|
|
220
239
|
*/
|
|
221
240
|
declare function oidc(getToken: (forceRefresh: boolean) => Promise<string | null | undefined> | string | null | undefined): FetchToken;
|
|
222
241
|
//#endregion
|
|
223
|
-
export { userToken as S,
|
|
242
|
+
export { userToken as C, signedInDatabase as S, clerk as _, DATABASE_TOKEN_PATH as a, oidc as b, DatabaseToken as c, PROFILE_MOST as d, Reference as f, auth0 as g, UserProfile as h, ConnectOptions as i, DoorToken as l, SignedInDatabase as m, AuthState as n, Database as o, ReturnsOf as p, BoundDatabases as r, DatabaseClient as s, ArgsOf as t, FetchToken as u, connect as v, profileClaims as x, databaseUrl as y };
|
|
@@ -1,8 +1,373 @@
|
|
|
1
1
|
import { r as platformApi } from "./here-BKjizr_N.mjs";
|
|
2
2
|
import { n as bytesTagged, r as bytesUntagged } from "./bytes-C2pcyl9u.mjs";
|
|
3
3
|
import { DatabaseError } from "./database/errors.mjs";
|
|
4
|
-
import { i as payloadOf, r as fetchAt, t as Connection } from "./connection-
|
|
4
|
+
import { a as socketAt, i as payloadOf, r as fetchAt, t as Connection } from "./connection-CpT0x1EE.mjs";
|
|
5
5
|
import { t as callKey } from "./key-BrRdFY-n.mjs";
|
|
6
|
+
//#region src/database/door-socket.ts
|
|
7
|
+
/**
|
|
8
|
+
* The Database's socket protocol: binary frames on one TCP + TLS connection a server opens itself, many calls in flight
|
|
9
|
+
* and watches beside them. A Deployment's server opens one per request (`DoorSocket`) at the socket address its door
|
|
10
|
+
* names; `client.ts` decides what goes on it. All integers are little-endian.
|
|
11
|
+
*
|
|
12
|
+
* const socket = new DoorSocket(handle.socket("host:port"), "db-<id>", "Bearer <token>")
|
|
13
|
+
* if (socket.ready("Bearer <token>", body.length)) const { status, text } = await socket.call(body)
|
|
14
|
+
*
|
|
15
|
+
* - **Hello**, the client's first bytes, once: `MAGIC`, a u32 of the bytes after it, a u8 form (`FORM_JSON`), a u16 and
|
|
16
|
+
* the Database (`db-<id>`), a u16 and the token (the Authorization value). Frames may follow it at once; it is answered
|
|
17
|
+
* `HELLO_OK` (id 0: u32 calls allowed in flight, u32 the largest call), or `REFUSED` (id 0: u16 status, a JSON body)
|
|
18
|
+
* and the socket closes.
|
|
19
|
+
* - **Frames**, both ways: a u32 of the bytes after it (5 + the payload), a u32 id, a u8 kind, the payload. The client
|
|
20
|
+
* picks ids, unique among its unanswered calls and open watches; 0 is the hello's.
|
|
21
|
+
* - A `CALL`'s payload is exactly a `POST <door>/call` body, answered `ANSWER`: u16 HTTP status, u8 form, the body, in
|
|
22
|
+
* whatever order calls finish. A `WATCH` opens a watch, answered `OPENED` (u16 status, 101 open), then `DATA` for each
|
|
23
|
+
* message the Database pushes; `DATA` from the client is a message on it; `CLOSE` either way ends it.
|
|
24
|
+
*/
|
|
25
|
+
/** A hello's first 4 bytes, by which the address knows the socket protocol from HTTP. */
|
|
26
|
+
const MAGIC = Uint8Array.of(0, 71, 72, 1);
|
|
27
|
+
/** The most a hello may hold after its magic and length. */
|
|
28
|
+
const MAX_HELLO = 16384;
|
|
29
|
+
/** The largest frame the Database sends: a push of 64 MiB, its id and kind. */
|
|
30
|
+
const MAX_FRAME = 67108869;
|
|
31
|
+
/** Until the hello is answered: calls in flight and a call's bytes, the Database's bounds. */
|
|
32
|
+
const IN_FLIGHT = 1024;
|
|
33
|
+
const MAX_CALL = 2 << 20;
|
|
34
|
+
const encoder$1 = new TextEncoder();
|
|
35
|
+
const decoder = new TextDecoder();
|
|
36
|
+
/** The client's hello for `database` (`db-<id>`) with `authorization` (`Bearer <token>`), bodies as JSON. */
|
|
37
|
+
function hello(database, authorization, form = 0) {
|
|
38
|
+
const db = encoder$1.encode(database), auth = encoder$1.encode(authorization);
|
|
39
|
+
const n = 5 + db.length + auth.length;
|
|
40
|
+
if (n > MAX_HELLO) throw new RangeError(`a hello holds at most ${MAX_HELLO} bytes; this one is ${n}`);
|
|
41
|
+
const out = new Uint8Array(8 + n), view = new DataView(out.buffer);
|
|
42
|
+
out.set(MAGIC, 0);
|
|
43
|
+
view.setUint32(4, n, true);
|
|
44
|
+
out[8] = form;
|
|
45
|
+
view.setUint16(9, db.length, true);
|
|
46
|
+
out.set(db, 11);
|
|
47
|
+
view.setUint16(11 + db.length, auth.length, true);
|
|
48
|
+
out.set(auth, 13 + db.length);
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
/** A frame either way: its length, `id`, `kind`, then `payload`. */
|
|
52
|
+
function frame(id, kind, payload = /* @__PURE__ */ new Uint8Array(0)) {
|
|
53
|
+
const out = new Uint8Array(9 + payload.length), view = new DataView(out.buffer);
|
|
54
|
+
view.setUint32(0, 5 + payload.length, true);
|
|
55
|
+
view.setUint32(4, id, true);
|
|
56
|
+
out[8] = kind;
|
|
57
|
+
out.set(payload, 9);
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
/** Frames out of the bytes a socket reads, however they are split. */
|
|
61
|
+
var FrameReader = class {
|
|
62
|
+
#chunks = [];
|
|
63
|
+
#size = 0;
|
|
64
|
+
#length;
|
|
65
|
+
/** The frames `chunk` completes, in order. A length no frame can have throws: the socket is not speaking the protocol. */
|
|
66
|
+
push(chunk) {
|
|
67
|
+
if (chunk.length) {
|
|
68
|
+
this.#chunks.push(chunk);
|
|
69
|
+
this.#size += chunk.length;
|
|
70
|
+
}
|
|
71
|
+
const frames = [];
|
|
72
|
+
for (;;) {
|
|
73
|
+
if (this.#length === void 0) {
|
|
74
|
+
if (this.#size < 9) break;
|
|
75
|
+
const head = this.#peek(4);
|
|
76
|
+
const length = new DataView(head.buffer, head.byteOffset, 4).getUint32(0, true);
|
|
77
|
+
if (length < 5 || length > MAX_FRAME) throw new RangeError(`a frame of ${length} bytes`);
|
|
78
|
+
this.#length = length;
|
|
79
|
+
}
|
|
80
|
+
if (this.#size < 4 + this.#length) break;
|
|
81
|
+
const bytes = this.#take(4 + this.#length);
|
|
82
|
+
this.#length = void 0;
|
|
83
|
+
frames.push({
|
|
84
|
+
id: new DataView(bytes.buffer, bytes.byteOffset, 8).getUint32(4, true),
|
|
85
|
+
kind: bytes[8],
|
|
86
|
+
body: bytes.subarray(9)
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
return frames;
|
|
90
|
+
}
|
|
91
|
+
#peek(n) {
|
|
92
|
+
const first = this.#chunks[0];
|
|
93
|
+
if (first.length >= n) return first;
|
|
94
|
+
const out = new Uint8Array(n);
|
|
95
|
+
let at = 0;
|
|
96
|
+
for (const chunk of this.#chunks) {
|
|
97
|
+
const part = chunk.subarray(0, n - at);
|
|
98
|
+
out.set(part, at);
|
|
99
|
+
at += part.length;
|
|
100
|
+
if (at === n) break;
|
|
101
|
+
}
|
|
102
|
+
return out;
|
|
103
|
+
}
|
|
104
|
+
#take(n) {
|
|
105
|
+
const first = this.#chunks[0];
|
|
106
|
+
let out;
|
|
107
|
+
if (first.length >= n) {
|
|
108
|
+
out = first.subarray(0, n);
|
|
109
|
+
if (first.length === n) this.#chunks.shift();
|
|
110
|
+
else this.#chunks[0] = first.subarray(n);
|
|
111
|
+
} else {
|
|
112
|
+
out = new Uint8Array(n);
|
|
113
|
+
let at = 0;
|
|
114
|
+
while (at < n) {
|
|
115
|
+
const chunk = this.#chunks[0], part = chunk.subarray(0, n - at);
|
|
116
|
+
out.set(part, at);
|
|
117
|
+
at += part.length;
|
|
118
|
+
if (part.length === chunk.length) this.#chunks.shift();
|
|
119
|
+
else this.#chunks[0] = chunk.subarray(part.length);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
this.#size -= n;
|
|
123
|
+
return out;
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
/** What a call or watch on a socket that ended rejects with: nothing reached the Database that it answered, so send it another way. */
|
|
127
|
+
var SocketClosed = class extends Error {
|
|
128
|
+
closed = true;
|
|
129
|
+
constructor() {
|
|
130
|
+
super("the database's socket closed");
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
/** A watch the Database refused to open, with its status and why: the same refusal a WebSocket's open would get. */
|
|
134
|
+
var WatchRefused = class extends Error {
|
|
135
|
+
status;
|
|
136
|
+
constructor(status, why) {
|
|
137
|
+
super(why || `the database refused the watch (${status})`);
|
|
138
|
+
this.status = status;
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
/**
|
|
142
|
+
* One watch on a `DoorSocket`, in the shape of a WebSocket, so a `Connection` runs on it (its `dial`). The Database's
|
|
143
|
+
* own first message on a socket is its hello, which the socket's `HELLO_OK` stands in for and which never reaches the
|
|
144
|
+
* watch: an empty one is told to the watch's first listener, so the connection sends at once to the one unit a
|
|
145
|
+
* Database has.
|
|
146
|
+
*/
|
|
147
|
+
var SocketWatch = class {
|
|
148
|
+
id;
|
|
149
|
+
socket;
|
|
150
|
+
readyState = 0;
|
|
151
|
+
binaryType = "arraybuffer";
|
|
152
|
+
#messages = /* @__PURE__ */ new Set();
|
|
153
|
+
#closes = /* @__PURE__ */ new Set();
|
|
154
|
+
#helloTold = false;
|
|
155
|
+
constructor(id, socket) {
|
|
156
|
+
this.id = id;
|
|
157
|
+
this.socket = socket;
|
|
158
|
+
}
|
|
159
|
+
addEventListener(type, listener) {
|
|
160
|
+
if (type === "message") {
|
|
161
|
+
this.#messages.add(listener);
|
|
162
|
+
if (!this.#helloTold) {
|
|
163
|
+
this.#helloTold = true;
|
|
164
|
+
queueMicrotask(() => this.message("{\"hello\":{}}"));
|
|
165
|
+
}
|
|
166
|
+
} else if (type === "close") this.#closes.add(listener);
|
|
167
|
+
}
|
|
168
|
+
removeEventListener(type, listener) {
|
|
169
|
+
if (type === "message") this.#messages.delete(listener);
|
|
170
|
+
else if (type === "close") this.#closes.delete(listener);
|
|
171
|
+
}
|
|
172
|
+
send(text) {
|
|
173
|
+
if (this.readyState !== 1) throw new SocketClosed();
|
|
174
|
+
this.socket.send(frame(this.id, 3, encoder$1.encode(text)));
|
|
175
|
+
}
|
|
176
|
+
/** Ends the watch: the Database is told, and its close heard here once. */
|
|
177
|
+
close() {
|
|
178
|
+
if (this.readyState === 3) return;
|
|
179
|
+
if (this.socket.state !== "closed") this.socket.send(frame(this.id, 4));
|
|
180
|
+
this.ended();
|
|
181
|
+
}
|
|
182
|
+
/** @internal The Database's message on it. */
|
|
183
|
+
message(data) {
|
|
184
|
+
if (this.readyState === 1) for (const listener of [...this.#messages]) listener({ data });
|
|
185
|
+
}
|
|
186
|
+
/** @internal It ended: its close is heard once. */
|
|
187
|
+
ended() {
|
|
188
|
+
if (this.readyState === 3) return;
|
|
189
|
+
this.readyState = 3;
|
|
190
|
+
this.socket.forget(this.id);
|
|
191
|
+
queueMicrotask(() => {
|
|
192
|
+
for (const listener of [...this.#closes]) listener();
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
/**
|
|
197
|
+
* One request's socket to its Database: the hello sent at once, calls and watches sent behind it without waiting, each
|
|
198
|
+
* answered by its id. It is `opening` until the hello is answered, `open` once it is, and `closed` once anything ends
|
|
199
|
+
* it (a refusal, a frame it can't read, the stream ending): every call and watch still unanswered then rejects with
|
|
200
|
+
* `SocketClosed`, and nothing more is sent. Nothing about it is ever waited on to make a call.
|
|
201
|
+
*/
|
|
202
|
+
var DoorSocket = class {
|
|
203
|
+
authorization;
|
|
204
|
+
state = "opening";
|
|
205
|
+
/** How long until the hello was answered, in ms, once it is. */
|
|
206
|
+
openMs;
|
|
207
|
+
/** Why it closed, once it has. */
|
|
208
|
+
why;
|
|
209
|
+
#started = performance.now();
|
|
210
|
+
#raw;
|
|
211
|
+
#writer;
|
|
212
|
+
#queued = [];
|
|
213
|
+
#next = 1;
|
|
214
|
+
#inFlight = IN_FLIGHT;
|
|
215
|
+
#maxCall = MAX_CALL;
|
|
216
|
+
#calls = /* @__PURE__ */ new Map();
|
|
217
|
+
#watches = /* @__PURE__ */ new Map();
|
|
218
|
+
/** Opens the protocol on `made` for `database` (`db-<id>`) as `authorization` (`Bearer <token>`); a hello too long for it throws. */
|
|
219
|
+
constructor(made, database, authorization) {
|
|
220
|
+
this.authorization = authorization;
|
|
221
|
+
this.#queued.push(hello(database, authorization));
|
|
222
|
+
Promise.resolve(made).then((raw) => this.#run(raw), (error) => this.end(`it could not be opened (${error instanceof Error ? error.message : String(error)})`));
|
|
223
|
+
}
|
|
224
|
+
/** Whether a call of `bytes` as `authorization` goes on it now: open, its hello's token, within its bounds. */
|
|
225
|
+
ready(authorization, bytes) {
|
|
226
|
+
return this.state === "open" && authorization === this.authorization && bytes <= this.#maxCall && this.#calls.size < this.#inFlight;
|
|
227
|
+
}
|
|
228
|
+
/** One call (a `POST <door>/call` body): its answer, or `SocketClosed` once the socket ends first. */
|
|
229
|
+
call(body) {
|
|
230
|
+
if (this.state === "closed") return Promise.reject(new SocketClosed());
|
|
231
|
+
const id = this.#id();
|
|
232
|
+
return new Promise((done, failed) => {
|
|
233
|
+
this.#calls.set(id, {
|
|
234
|
+
done,
|
|
235
|
+
failed
|
|
236
|
+
});
|
|
237
|
+
this.send(frame(id, 1, body));
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
/** A watch opened with `first` its first message (may be none): resolves once the Database opens it. */
|
|
241
|
+
watch(first = "") {
|
|
242
|
+
if (this.state === "closed") return Promise.reject(new SocketClosed());
|
|
243
|
+
const id = this.#id(), watch = new SocketWatch(id, this);
|
|
244
|
+
return new Promise((done, failed) => {
|
|
245
|
+
this.#watches.set(id, {
|
|
246
|
+
watch,
|
|
247
|
+
opening: {
|
|
248
|
+
done,
|
|
249
|
+
failed
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
this.send(frame(id, 2, encoder$1.encode(first)));
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
/** @internal Sends a frame, behind the hello; a write that fails ends the socket. */
|
|
256
|
+
send(bytes) {
|
|
257
|
+
if (this.state === "closed") return;
|
|
258
|
+
if (!this.#writer) {
|
|
259
|
+
this.#queued.push(bytes);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
this.#writer.write(bytes).catch(() => this.end("a write failed"));
|
|
263
|
+
}
|
|
264
|
+
/** @internal A watch ended here: its id may be used again. */
|
|
265
|
+
forget(id) {
|
|
266
|
+
this.#watches.delete(id);
|
|
267
|
+
}
|
|
268
|
+
/** Ends it: what is unanswered rejects with `SocketClosed`, and the connection is closed. */
|
|
269
|
+
end(why = "it was closed") {
|
|
270
|
+
if (this.state === "closed") return;
|
|
271
|
+
this.state = "closed";
|
|
272
|
+
this.why = why;
|
|
273
|
+
for (const call of this.#calls.values()) call.failed(new SocketClosed());
|
|
274
|
+
this.#calls.clear();
|
|
275
|
+
for (const { watch, opening } of [...this.#watches.values()]) {
|
|
276
|
+
if (opening) opening.failed(new SocketClosed());
|
|
277
|
+
watch.ended();
|
|
278
|
+
}
|
|
279
|
+
this.#watches.clear();
|
|
280
|
+
try {
|
|
281
|
+
Promise.resolve(this.#raw?.close()).catch(() => {});
|
|
282
|
+
} catch {}
|
|
283
|
+
}
|
|
284
|
+
async #run(raw) {
|
|
285
|
+
this.#raw = raw;
|
|
286
|
+
if (this.state === "closed") {
|
|
287
|
+
try {
|
|
288
|
+
Promise.resolve(raw.close()).catch(() => {});
|
|
289
|
+
} catch {}
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
raw.opened?.catch(() => {});
|
|
293
|
+
raw.closed?.then(() => this.end("the database closed it"), () => this.end("the connection failed"));
|
|
294
|
+
let reader;
|
|
295
|
+
try {
|
|
296
|
+
this.#writer = raw.writable.getWriter();
|
|
297
|
+
reader = raw.readable.getReader();
|
|
298
|
+
} catch {
|
|
299
|
+
this.end("its streams could not be read");
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
for (const bytes of this.#queued.splice(0)) this.send(bytes);
|
|
303
|
+
const frames = new FrameReader();
|
|
304
|
+
try {
|
|
305
|
+
for (;;) {
|
|
306
|
+
const { value, done } = await reader.read();
|
|
307
|
+
if (done) break;
|
|
308
|
+
for (const each of frames.push(value)) this.#took(each);
|
|
309
|
+
if (this.state === "closed") return;
|
|
310
|
+
}
|
|
311
|
+
this.end("the database closed it");
|
|
312
|
+
} catch (error) {
|
|
313
|
+
this.end(error instanceof RangeError ? `the database sent ${error.message}` : "the connection failed");
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
#took({ id, kind, body }) {
|
|
317
|
+
const view = new DataView(body.buffer, body.byteOffset, body.byteLength);
|
|
318
|
+
if (id === 0 && kind === 0 && body.length >= 8) {
|
|
319
|
+
this.#inFlight = Math.min(IN_FLIGHT, view.getUint32(0, true));
|
|
320
|
+
this.#maxCall = Math.min(MAX_CALL, view.getUint32(4, true));
|
|
321
|
+
this.openMs = performance.now() - this.#started;
|
|
322
|
+
if (this.state === "opening") this.state = "open";
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
if (id === 0 && kind === 5) {
|
|
326
|
+
this.end(body.length >= 2 ? `the database refused it (${view.getUint16(0, true)}: ${decoder.decode(body.subarray(2))})` : "the database refused it");
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
if (kind === 1 && body.length >= 3) {
|
|
330
|
+
const call = this.#calls.get(id);
|
|
331
|
+
if (!call) return;
|
|
332
|
+
this.#calls.delete(id);
|
|
333
|
+
call.done({
|
|
334
|
+
status: view.getUint16(0, true),
|
|
335
|
+
text: decoder.decode(body.subarray(3))
|
|
336
|
+
});
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
const held = this.#watches.get(id);
|
|
340
|
+
if (!held) return;
|
|
341
|
+
if (kind === 2 && body.length >= 2 && held.opening) {
|
|
342
|
+
const status = view.getUint16(0, true), opening = held.opening;
|
|
343
|
+
delete held.opening;
|
|
344
|
+
if (status === 101) {
|
|
345
|
+
held.watch.readyState = 1;
|
|
346
|
+
opening.done(held.watch);
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
this.#watches.delete(id);
|
|
350
|
+
let why = decoder.decode(body.subarray(2));
|
|
351
|
+
try {
|
|
352
|
+
const refused = JSON.parse(why);
|
|
353
|
+
if (typeof refused.why === "string") why = refused.why;
|
|
354
|
+
} catch {}
|
|
355
|
+
opening.failed(new WatchRefused(status, why));
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
if (kind === 3) held.watch.message(decoder.decode(body));
|
|
359
|
+
else if (kind === 4) held.watch.ended();
|
|
360
|
+
}
|
|
361
|
+
/** The next id no unanswered call or open watch has; never 0. */
|
|
362
|
+
#id() {
|
|
363
|
+
for (;;) {
|
|
364
|
+
const id = this.#next;
|
|
365
|
+
this.#next = this.#next >= 4294967295 ? 1 : this.#next + 1;
|
|
366
|
+
if (!this.#calls.has(id) && !this.#watches.has(id)) return id;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
//#endregion
|
|
6
371
|
//#region src/database/dev-token.ts
|
|
7
372
|
/**
|
|
8
373
|
* The development server's tokens (`./dev-server.ts`): an unsigned JWT naming its caller, which only the development
|
|
@@ -48,7 +413,8 @@ function devToken(subject, profile = {}) {
|
|
|
48
413
|
* const db = await connect({ binding: "main" })
|
|
49
414
|
*
|
|
50
415
|
* In a Deployment that waits on nothing: the request hands its server the Database's address and a token, and the
|
|
51
|
-
* calls the server makes at once go to the Database together, in one request.
|
|
416
|
+
* calls the server makes at once go to the Database together, in one request. The request's later calls and its
|
|
417
|
+
* watches go on one socket it opens beside its first call, where its Database has one.
|
|
52
418
|
*
|
|
53
419
|
* A server on Midsummer's own contract is also handed the handle as `context.database`:
|
|
54
420
|
*
|
|
@@ -360,7 +726,7 @@ async function isolation(url, key, token) {
|
|
|
360
726
|
return {
|
|
361
727
|
...own,
|
|
362
728
|
name,
|
|
363
|
-
store: (await import("./store-
|
|
729
|
+
store: (await import("./store-Ch931wwA.mjs")).idbStore(name)
|
|
364
730
|
};
|
|
365
731
|
}
|
|
366
732
|
const inPage = () => typeof globalThis.document !== "undefined";
|
|
@@ -446,6 +812,22 @@ async function opened(url, token, refresh, server, key) {
|
|
|
446
812
|
const DOOR_RENEW_MS = 3e4;
|
|
447
813
|
/** How long one request to a door may take, its answer read included, before it counts as no answer and is sent again. */
|
|
448
814
|
const DOOR_CALL_MS = 1e4;
|
|
815
|
+
/**
|
|
816
|
+
* A socket call's answer, or `SocketClosed` once it has held the call past `DOOR_CALL_MS`, with the socket closed so
|
|
817
|
+
* the request goes on by fetch: no call waits on a socket that stalled.
|
|
818
|
+
*/
|
|
819
|
+
function answeredWithin(socket, call) {
|
|
820
|
+
let timer;
|
|
821
|
+
const stalled = new Promise((_, reject) => {
|
|
822
|
+
timer = setTimeout(() => {
|
|
823
|
+
try {
|
|
824
|
+
socket.end(`a call waited past ${DOOR_CALL_MS} ms`);
|
|
825
|
+
} catch {}
|
|
826
|
+
reject(new SocketClosed());
|
|
827
|
+
}, DOOR_CALL_MS);
|
|
828
|
+
});
|
|
829
|
+
return Promise.race([call, stalled]).finally(() => clearTimeout(timer));
|
|
830
|
+
}
|
|
449
831
|
/** The most calls one request to a door carries; more made at once go in as many requests as they need. */
|
|
450
832
|
const DOOR_BATCH = 256;
|
|
451
833
|
/**
|
|
@@ -454,15 +836,86 @@ const DOOR_BATCH = 256;
|
|
|
454
836
|
* an ask that ends with another request.
|
|
455
837
|
*/
|
|
456
838
|
const remaking = /* @__PURE__ */ new WeakMap();
|
|
839
|
+
const lanes = /* @__PURE__ */ new WeakMap();
|
|
840
|
+
function laneOf(handle, door) {
|
|
841
|
+
let byDoor = lanes.get(handle);
|
|
842
|
+
if (!byDoor) {
|
|
843
|
+
byDoor = /* @__PURE__ */ new Map();
|
|
844
|
+
lanes.set(handle, byDoor);
|
|
845
|
+
}
|
|
846
|
+
let lane = byDoor.get(door);
|
|
847
|
+
if (!lane) {
|
|
848
|
+
lane = {
|
|
849
|
+
tried: false,
|
|
850
|
+
timed: false
|
|
851
|
+
};
|
|
852
|
+
byDoor.set(door, lane);
|
|
853
|
+
}
|
|
854
|
+
return lane;
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* Starts the connection to `door`'s address as soon as a request connects to it, without waiting on it: a request to
|
|
858
|
+
* the address's own health, which no Database answers, so the request's first call finds the connection made (a new
|
|
859
|
+
* one costs that call 50-160 ms; a kept one about 10). Once a request.
|
|
860
|
+
*/
|
|
861
|
+
function warmUp(lane, door) {
|
|
862
|
+
if (lane.warm || lane.tried) return;
|
|
863
|
+
const warm = { at: performance.now() };
|
|
864
|
+
lane.warm = warm;
|
|
865
|
+
const ended = (status) => {
|
|
866
|
+
warm.ended = performance.now();
|
|
867
|
+
warm.status = status;
|
|
868
|
+
};
|
|
869
|
+
try {
|
|
870
|
+
fetchAt(new URL("/health", door).href).then((response) => {
|
|
871
|
+
ended(response.status);
|
|
872
|
+
response.body?.cancel().catch(() => {});
|
|
873
|
+
}, () => ended(0));
|
|
874
|
+
} catch {
|
|
875
|
+
ended(0);
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
const encoder = new TextEncoder();
|
|
879
|
+
/** A `Server-Timing` entry's number of milliseconds. */
|
|
880
|
+
const ms = (value) => value.toFixed(1);
|
|
457
881
|
/**
|
|
458
|
-
*
|
|
459
|
-
*
|
|
460
|
-
*
|
|
461
|
-
*
|
|
462
|
-
*
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
882
|
+
* The `db-first` entry of a request's first call to its door, which says where its time went: `token` (having the
|
|
883
|
+
* token: none when the request handed one), `call` (the call itself, `by fetch` or `by socket`), `warm` (the door's
|
|
884
|
+
* connection started at connect, ended before the call or still going beside it; `none` where none was started) and
|
|
885
|
+
* `socket` (open, and how long its hello took, or still opening, or none). A slow call beside a quick warm-up was the
|
|
886
|
+
* Database (waking, or the engine); a slow warm-up the connection.
|
|
887
|
+
*/
|
|
888
|
+
function firstEntry(lane, tokenMs, callMs, started, status, bySocket) {
|
|
889
|
+
const warm = lane.warm;
|
|
890
|
+
const warmed = !warm ? "none" : warm.ended === void 0 ? `${ms(started - warm.at)} so far, beside the call` : `${ms(warm.ended - warm.at)} ${warm.ended <= started ? "before" : "beside"} the call (${warm.status})`;
|
|
891
|
+
const socket = lane.socket;
|
|
892
|
+
const opened = !socket ? "none" : socket.state === "open" ? `open in ${ms(socket.openMs ?? 0)}` : socket.state === "opening" ? "opening" : `closed (${socket.why})`;
|
|
893
|
+
return `db-first;dur=${ms(tokenMs + callMs)};desc="token ${ms(tokenMs)}, call ${ms(callMs)} by ${bySocket ? "socket" : "fetch"} (${status}), warm ${warmed}, socket ${opened}"`;
|
|
894
|
+
}
|
|
895
|
+
/** A WebSocket to `url` once it opens, as a `Connection`'s `dial` gives one. */
|
|
896
|
+
function openedAt(url) {
|
|
897
|
+
return new Promise((done, failed) => {
|
|
898
|
+
const ws = socketAt(url);
|
|
899
|
+
ws.addEventListener("open", () => done(ws), { once: true });
|
|
900
|
+
ws.addEventListener("error", () => failed(/* @__PURE__ */ new Error("socket error")), { once: true });
|
|
901
|
+
ws.addEventListener("close", () => failed(/* @__PURE__ */ new Error("socket closed")), { once: true });
|
|
902
|
+
});
|
|
903
|
+
}
|
|
904
|
+
/**
|
|
905
|
+
* A Deployment's server's calls to its Database, sent straight to the Database's door with the token its request
|
|
906
|
+
* handed in, with no hop through Midsummer. The calls made in one turn go together in one call (`DOOR_BATCH` at most),
|
|
907
|
+
* each result matched by its `i`, and a mutation is tagged, so one sent again applies once. A call that gets no answer,
|
|
908
|
+
* or a 5xx, is sent again twice at most; a 401 renews the token once. A token with under `DOOR_RENEW_MS` left is
|
|
909
|
+
* renewed before it is sent: from the door the request being served was handed, else the handle's `token`. The door
|
|
910
|
+
* answering that it has no Database by its id (404 `not_found`, as after a move with its rows gone) asks the handle to
|
|
911
|
+
* make it again (`remake`), once, and sends the calls once more.
|
|
912
|
+
*
|
|
913
|
+
* How a call goes, in each request (`Lane`): connecting starts the door's connection (`warmUp`). The request's first
|
|
914
|
+
* call goes by fetch (`POST <door>/call`), and beside it the request opens a socket to the door's socket address
|
|
915
|
+
* (`DoorSocket`), where the door has one and the runtime can open one; later calls of the request go on the socket once
|
|
916
|
+
* its hello is answered, by fetch until then. A watch goes on the socket too. Nothing waits on the socket: one that
|
|
917
|
+
* fails or closes leaves every call it held, and every later one of that request, to fetch, with no error; a watch
|
|
918
|
+
* opens a WebSocket to the door instead. A renewed token goes by fetch: a socket's token is its hello's.
|
|
466
919
|
*/
|
|
467
920
|
function doorCalls(handle, binding, first) {
|
|
468
921
|
let door = first;
|
|
@@ -473,7 +926,8 @@ function doorCalls(handle, binding, first) {
|
|
|
473
926
|
const handed = force ? void 0 : now().door?.(binding);
|
|
474
927
|
const next = handed && handed.url === door.url && handed.expiresAt - Date.now() > DOOR_RENEW_MS ? handed : {
|
|
475
928
|
...await now().token(binding),
|
|
476
|
-
url: door.url
|
|
929
|
+
url: door.url,
|
|
930
|
+
...door.socket ? { socket: door.socket } : {}
|
|
477
931
|
};
|
|
478
932
|
door = next;
|
|
479
933
|
return next;
|
|
@@ -487,11 +941,23 @@ function doorCalls(handle, binding, first) {
|
|
|
487
941
|
...answer,
|
|
488
942
|
i
|
|
489
943
|
});
|
|
490
|
-
/**
|
|
944
|
+
/** The request's socket, opened beside its first call or watch, once: where the door names an address and the runtime opens sockets. */
|
|
945
|
+
const begin = (lane, asker, token) => {
|
|
946
|
+
if (lane.tried) return;
|
|
947
|
+
lane.tried = true;
|
|
948
|
+
const database = new URL(door.url).pathname.split("/").filter(Boolean).at(-1);
|
|
949
|
+
if (!door.socket || !asker.socket || !database) return;
|
|
950
|
+
try {
|
|
951
|
+
lane.socket = new DoorSocket(asker.socket(door.socket), database, `Bearer ${token}`);
|
|
952
|
+
} catch {}
|
|
953
|
+
};
|
|
954
|
+
/** One call of `ops` to the door, and how to find each one's answer by its `i`. */
|
|
491
955
|
const post = async (ops) => {
|
|
492
|
-
const body = JSON.stringify(ops), since = Date.now();
|
|
956
|
+
const body = JSON.stringify(ops), bytes = encoder.encode(body), since = Date.now();
|
|
957
|
+
const asker = now(), lane = laneOf(asker, door.url);
|
|
493
958
|
let remade = false;
|
|
494
959
|
for (let tries = 0;; tries += 1) {
|
|
960
|
+
const asked = performance.now();
|
|
495
961
|
let token;
|
|
496
962
|
try {
|
|
497
963
|
token = await live();
|
|
@@ -502,41 +968,61 @@ function doorCalls(handle, binding, first) {
|
|
|
502
968
|
error: `no new token for the database bound as ${binding} (${error instanceof Error ? error.message : String(error)})`
|
|
503
969
|
});
|
|
504
970
|
}
|
|
971
|
+
const tokenMs = performance.now() - asked;
|
|
972
|
+
begin(lane, asker, token);
|
|
973
|
+
const socket = lane.socket?.ready(`Bearer ${token}`, bytes.length) ? lane.socket : void 0;
|
|
505
974
|
const started = performance.now();
|
|
506
|
-
let
|
|
507
|
-
try {
|
|
508
|
-
|
|
509
|
-
method: "POST",
|
|
510
|
-
headers: {
|
|
511
|
-
authorization: `Bearer ${token}`,
|
|
512
|
-
"content-type": "application/json"
|
|
513
|
-
},
|
|
514
|
-
body,
|
|
515
|
-
signal: AbortSignal.timeout(DOOR_CALL_MS)
|
|
516
|
-
});
|
|
517
|
-
text = await response.text();
|
|
975
|
+
let status, text;
|
|
976
|
+
if (socket) try {
|
|
977
|
+
({status, text} = await answeredWithin(socket, socket.call(bytes)));
|
|
518
978
|
} catch (error) {
|
|
519
|
-
if (
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
});
|
|
979
|
+
if (error instanceof SocketClosed) {
|
|
980
|
+
tries -= 1;
|
|
981
|
+
continue;
|
|
982
|
+
}
|
|
983
|
+
throw error;
|
|
525
984
|
}
|
|
526
|
-
|
|
527
|
-
|
|
985
|
+
else {
|
|
986
|
+
let response;
|
|
987
|
+
try {
|
|
988
|
+
response = await fetchAt(`${door.url}/call`, {
|
|
989
|
+
method: "POST",
|
|
990
|
+
headers: {
|
|
991
|
+
authorization: `Bearer ${token}`,
|
|
992
|
+
"content-type": "application/json"
|
|
993
|
+
},
|
|
994
|
+
body,
|
|
995
|
+
signal: AbortSignal.timeout(DOOR_CALL_MS)
|
|
996
|
+
});
|
|
997
|
+
text = await response.text();
|
|
998
|
+
} catch (error) {
|
|
999
|
+
if (tries < 2) continue;
|
|
1000
|
+
return all({
|
|
1001
|
+
ok: false,
|
|
1002
|
+
why: "unavailable",
|
|
1003
|
+
error: `couldn't reach the database bound as ${binding} (${error instanceof Error ? error.message : String(error)})`
|
|
1004
|
+
});
|
|
1005
|
+
}
|
|
1006
|
+
status = response.status;
|
|
1007
|
+
}
|
|
1008
|
+
const callMs = performance.now() - started;
|
|
1009
|
+
asker.note?.(`db-call;dur=${ms(callMs)};desc="${status}, ${ops.length} call${ops.length === 1 ? "" : "s"}${socket ? ", socket" : ""}"`);
|
|
1010
|
+
if (!lane.timed) {
|
|
1011
|
+
lane.timed = true;
|
|
1012
|
+
asker.note?.(firstEntry(lane, tokenMs, callMs, started, status, !!socket));
|
|
1013
|
+
}
|
|
1014
|
+
if (status === 401 && tries === 0) {
|
|
528
1015
|
try {
|
|
529
1016
|
await renew(true);
|
|
530
1017
|
} catch {}
|
|
531
1018
|
continue;
|
|
532
1019
|
}
|
|
533
|
-
if (
|
|
1020
|
+
if (status < 200 || status > 299) {
|
|
534
1021
|
let refused = {};
|
|
535
1022
|
try {
|
|
536
1023
|
refused = JSON.parse(text);
|
|
537
1024
|
} catch {}
|
|
538
|
-
|
|
539
|
-
if (response.status === 404 && refused.error === "not_found" && !remade && asker.remake) {
|
|
1025
|
+
if (status === 404 && refused.error === "not_found" && !remade && asker.remake) {
|
|
540
1026
|
remade = true;
|
|
541
1027
|
const at = door.url;
|
|
542
1028
|
let asked = remaking.get(asker);
|
|
@@ -555,10 +1041,10 @@ function doorCalls(handle, binding, first) {
|
|
|
555
1041
|
}
|
|
556
1042
|
if (await asking) continue;
|
|
557
1043
|
}
|
|
558
|
-
if (
|
|
1044
|
+
if (status >= 500 && tries < 2) continue;
|
|
559
1045
|
return all({
|
|
560
1046
|
ok: false,
|
|
561
|
-
why:
|
|
1047
|
+
why: status === 401 ? "denied" : typeof refused.error === "string" ? refused.error : "refused",
|
|
562
1048
|
error: typeof refused.why === "string" ? refused.why : text.slice(0, 200)
|
|
563
1049
|
});
|
|
564
1050
|
}
|
|
@@ -577,7 +1063,7 @@ function doorCalls(handle, binding, first) {
|
|
|
577
1063
|
};
|
|
578
1064
|
}
|
|
579
1065
|
};
|
|
580
|
-
/** Sends what this turn queued, `DOOR_BATCH` calls a
|
|
1066
|
+
/** Sends what this turn queued, `DOOR_BATCH` calls at a time. */
|
|
581
1067
|
const flush = () => {
|
|
582
1068
|
const batch = queued;
|
|
583
1069
|
queued = [];
|
|
@@ -605,7 +1091,22 @@ function doorCalls(handle, binding, first) {
|
|
|
605
1091
|
done
|
|
606
1092
|
});
|
|
607
1093
|
});
|
|
608
|
-
|
|
1094
|
+
/**
|
|
1095
|
+
* A watch's socket for the request it opens in: a watch on the request's socket while that is open with the
|
|
1096
|
+
* connection's token, else a WebSocket to the door. A watch the Database refuses fails as a WebSocket's open would.
|
|
1097
|
+
*/
|
|
1098
|
+
const dial = (lane) => async (connection) => {
|
|
1099
|
+
const socket = lane.socket;
|
|
1100
|
+
if (socket && socket.state !== "closed" && socket.authorization === `Bearer ${connection.token}`) try {
|
|
1101
|
+
return await socket.watch();
|
|
1102
|
+
} catch (error) {
|
|
1103
|
+
if (!(error instanceof SocketClosed)) throw error;
|
|
1104
|
+
}
|
|
1105
|
+
return openedAt(connection.url);
|
|
1106
|
+
};
|
|
1107
|
+
/** Each request's watches, on one connection that request opened. */
|
|
1108
|
+
const watchers = /* @__PURE__ */ new WeakMap();
|
|
1109
|
+
warmUp(laneOf(now(), door.url), door.url);
|
|
609
1110
|
return {
|
|
610
1111
|
async query(fn, args) {
|
|
611
1112
|
const answer = await call({
|
|
@@ -629,18 +1130,25 @@ function doorCalls(handle, binding, first) {
|
|
|
629
1130
|
},
|
|
630
1131
|
watchQuery(fn, args, options, onValue) {
|
|
631
1132
|
let stopped = false, stop = () => {};
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
1133
|
+
const asker = now(), lane = laneOf(asker, door.url);
|
|
1134
|
+
let watcher = watchers.get(lane);
|
|
1135
|
+
if (!watcher) {
|
|
1136
|
+
const opening = live().then((token) => {
|
|
1137
|
+
begin(lane, asker, token);
|
|
1138
|
+
return Connection.open(door.url, {
|
|
1139
|
+
token,
|
|
1140
|
+
refresh: async () => (await renew(true)).token,
|
|
1141
|
+
shape: "server",
|
|
1142
|
+
dial: dial(lane)
|
|
1143
|
+
});
|
|
1144
|
+
});
|
|
638
1145
|
opening.catch(() => {
|
|
639
|
-
if (
|
|
1146
|
+
if (watchers.get(lane) === opening) watchers.delete(lane);
|
|
640
1147
|
});
|
|
641
|
-
|
|
1148
|
+
watchers.set(lane, opening);
|
|
1149
|
+
watcher = opening;
|
|
642
1150
|
}
|
|
643
|
-
|
|
1151
|
+
watcher.then((open) => {
|
|
644
1152
|
if (!stopped) stop = open.watchQuery(fn, args, options, onValue);
|
|
645
1153
|
}, (error) => {
|
|
646
1154
|
if (!stopped) onValue(void 0, { error: caught(error) });
|
|
@@ -651,7 +1159,7 @@ function doorCalls(handle, binding, first) {
|
|
|
651
1159
|
};
|
|
652
1160
|
},
|
|
653
1161
|
close() {
|
|
654
|
-
|
|
1162
|
+
watchers.get(laneOf(now(), door.url))?.then((open) => open.close(), () => {});
|
|
655
1163
|
}
|
|
656
1164
|
};
|
|
657
1165
|
}
|
|
@@ -409,8 +409,8 @@ var Connection = class Connection {
|
|
|
409
409
|
...options,
|
|
410
410
|
shape
|
|
411
411
|
};
|
|
412
|
-
if (o.store === void 0 && typeof indexedDB !== "undefined" && typeof document !== "undefined") o.store = (await import("./store-
|
|
413
|
-
if (o.tabs !== false && typeof document !== "undefined") return (await import("./tabs-
|
|
412
|
+
if (o.store === void 0 && typeof indexedDB !== "undefined" && typeof document !== "undefined") o.store = (await import("./store-Ch931wwA.mjs")).idbStore(new URL(url).host);
|
|
413
|
+
if (o.tabs !== false && typeof document !== "undefined") return (await import("./tabs-C4NDJzfK.mjs")).ConnectionTabs.connect(url, o);
|
|
414
414
|
return Connection.open(url, o);
|
|
415
415
|
}
|
|
416
416
|
/** A Connection over one socket, of the shape given (default "browser"), no detection (tabs.mjs opens its own so). */
|
|
@@ -2009,4 +2009,4 @@ var Connection = class Connection {
|
|
|
2009
2009
|
}
|
|
2010
2010
|
};
|
|
2011
2011
|
//#endregion
|
|
2012
|
-
export { payloadOf as i, TAG_TTL as n, fetchAt as r, Connection as t };
|
|
2012
|
+
export { socketAt as a, payloadOf as i, TAG_TTL as n, fetchAt as r, Connection as t };
|
package/database/client.d.mts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { n as DatabaseErrorCode, t as DatabaseError } from "../errors-CnQduDOt.mjs";
|
|
2
|
-
import {
|
|
3
|
-
export { ArgsOf, AuthState, BoundDatabases, ConnectOptions, DATABASE_TOKEN_PATH, Database, DatabaseClient, DatabaseError, type DatabaseErrorCode, DatabaseToken, FetchToken, PROFILE_MOST, Reference, ReturnsOf, SignedInDatabase, UserProfile, auth0, clerk, connect, databaseUrl, oidc, profileClaims, signedInDatabase, userToken };
|
|
2
|
+
import { C as userToken, S as signedInDatabase, _ as clerk, a as DATABASE_TOKEN_PATH, b as oidc, c as DatabaseToken, d as PROFILE_MOST, f as Reference, g as auth0, h as UserProfile, i as ConnectOptions, l as DoorToken, m as SignedInDatabase, n as AuthState, o as Database, p as ReturnsOf, r as BoundDatabases, s as DatabaseClient, t as ArgsOf, u as FetchToken, v as connect, x as profileClaims, y as databaseUrl } from "../client-BDjPQa7G.mjs";
|
|
3
|
+
export { ArgsOf, AuthState, BoundDatabases, ConnectOptions, DATABASE_TOKEN_PATH, Database, DatabaseClient, DatabaseError, type DatabaseErrorCode, DatabaseToken, DoorToken, FetchToken, PROFILE_MOST, Reference, ReturnsOf, SignedInDatabase, UserProfile, auth0, clerk, connect, databaseUrl, oidc, profileClaims, signedInDatabase, userToken };
|
package/database/client.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { DatabaseError } from "./errors.mjs";
|
|
2
|
-
import { a as clerk, c as oidc, d as userToken, i as auth0, l as profileClaims, n as DatabaseClient, o as connect, r as PROFILE_MOST, s as databaseUrl, t as DATABASE_TOKEN_PATH, u as signedInDatabase } from "../client-
|
|
2
|
+
import { a as clerk, c as oidc, d as userToken, i as auth0, l as profileClaims, n as DatabaseClient, o as connect, r as PROFILE_MOST, s as databaseUrl, t as DATABASE_TOKEN_PATH, u as signedInDatabase } from "../client-CIuYlRWZ.mjs";
|
|
3
3
|
export { DATABASE_TOKEN_PATH, DatabaseClient, DatabaseError, PROFILE_MOST, auth0, clerk, connect, databaseUrl, oidc, profileClaims, signedInDatabase, userToken };
|
package/database/react.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as DatabaseError } from "../errors-CnQduDOt.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { f as Reference, o as Database } from "../client-BDjPQa7G.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 { n as AuthStore } from "./store-
|
|
1
|
+
import { n as AuthStore } from "./store-CRqU5WCe.mjs";
|
|
2
2
|
import { n as CaptchaProvider, t as AuthSettings } from "./settings-Dz3irtlu.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.
|
|
3
|
+
"version": "0.1.0-staging.3778",
|
|
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 { o as Database } from "./client-
|
|
1
|
+
import { o as Database } from "./client-BDjPQa7G.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" | "upsert" | "deleteMany" | "consume" | "increment" | "page";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as Connection } from "./connection-
|
|
1
|
+
import { t as Connection } from "./connection-CpT0x1EE.mjs";
|
|
2
2
|
//#region src/database/client/tabs.mjs
|
|
3
3
|
/** Argument index of the options that take a `tag` (a tab tags its own writes: the same tag after a handover). */
|
|
4
4
|
const TAGGED = {
|