@midsummer-ai/sdk 0.1.0-next.1305 → 0.1.0-next.1979
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.
|
@@ -164,12 +164,15 @@ var Aimd = class {
|
|
|
164
164
|
return this.v;
|
|
165
165
|
}
|
|
166
166
|
};
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
167
|
+
//#endregion
|
|
168
|
+
//#region src/database/client/connection.mjs
|
|
169
|
+
/** How long a socket's open may take (its upgrade answered), ms; a reconnect's try still unopened at each of HEDGE_AT
|
|
170
|
+
* (ms from its start) gets another socket beside it, at most three at once. */
|
|
171
|
+
const OPEN_WAIT = 1e4;
|
|
172
|
+
const HEDGE_AT = [100, 200];
|
|
173
|
+
/** A reconnect's waits between tries after the first (which is at once): RETRY_FIRST ms, doubling to RETRY_MAX. */
|
|
174
|
+
const RETRY_FIRST = 50;
|
|
175
|
+
const RETRY_MAX = 1e3;
|
|
173
176
|
const TAG_TTL = 864e5;
|
|
174
177
|
const groupOf = (id) => {
|
|
175
178
|
const t = String(id ?? ""), k = t.indexOf("/");
|
|
@@ -237,11 +240,13 @@ const SUBSCRIBE_WINDOW_MS = breakEvenMs({
|
|
|
237
240
|
holdUsdPerSec: PUSH_USD / 60,
|
|
238
241
|
eachUsd: READ_USD
|
|
239
242
|
});
|
|
240
|
-
/** Whether a token says server code: ours with `ro: "server"`, or the platform's for a Database Key (`
|
|
241
|
-
* `
|
|
243
|
+
/** Whether a token says server code: ours with `ro: "server"`, or the platform's for a Database Key (`key:<id>`), a
|
|
244
|
+
* Deployment (`site:<project>:<environment>`) or a CI or Agent run (`workload:<project>:<environment>`), each with
|
|
245
|
+
* `iss: "midsummer"`; a person's, `user:<handle>`, is not. */
|
|
246
|
+
const SERVER_SUBJECTS = /^(?:key|site|workload):/u;
|
|
242
247
|
const serverToken = (token) => {
|
|
243
248
|
const p = payloadOf(token);
|
|
244
|
-
return p?.ro === "server" || p?.iss === "midsummer" && String(p?.sub ?? "")
|
|
249
|
+
return p?.ro === "server" || p?.iss === "midsummer" && SERVER_SUBJECTS.test(String(p?.sub ?? ""));
|
|
245
250
|
};
|
|
246
251
|
/** The shape `Connection.connect` takes: "binding" (a server object binding in `opts.env.DATABASE_EDGE` or
|
|
247
252
|
* `opts.binding`), "server" (a runtime with no `window` / `document`, and a server token), else "browser". `g`: the
|
|
@@ -403,8 +408,8 @@ var Connection = class Connection {
|
|
|
403
408
|
...options,
|
|
404
409
|
shape
|
|
405
410
|
};
|
|
406
|
-
if (o.store === void 0 && typeof indexedDB !== "undefined" && typeof document !== "undefined") o.store = (await import("./store-
|
|
407
|
-
if (o.tabs !== false && typeof document !== "undefined") return (await import("./tabs-
|
|
411
|
+
if (o.store === void 0 && typeof indexedDB !== "undefined" && typeof document !== "undefined") o.store = (await import("./store-CfQLVX-u.mjs")).idbStore(new URL(url).host);
|
|
412
|
+
if (o.tabs !== false && typeof document !== "undefined") return (await import("./tabs-C5uIkZzN.mjs")).ConnectionTabs.connect(url, o);
|
|
408
413
|
return Connection.open(url, o);
|
|
409
414
|
}
|
|
410
415
|
/** A Connection over one socket, of the shape given (default "browser"), no detection (tabs.mjs opens its own so). */
|
|
@@ -412,6 +417,10 @@ var Connection = class Connection {
|
|
|
412
417
|
const g = new Connection(url, options);
|
|
413
418
|
if (g.store) await g.store.attach(g);
|
|
414
419
|
if (options.functions) g.predictor = new (await (import("./predict-Drfm_8pO.mjs"))).Predictor(g, options.functions);
|
|
420
|
+
if (options.shape === "server" && options.http !== false) {
|
|
421
|
+
g.http = true;
|
|
422
|
+
return g;
|
|
423
|
+
}
|
|
415
424
|
await g.open();
|
|
416
425
|
if (!g.dir) await Promise.race([g.hello, new Promise((r) => setTimeout(r, HELLO_WAIT_MS))]);
|
|
417
426
|
return g;
|
|
@@ -589,7 +598,7 @@ var Connection = class Connection {
|
|
|
589
598
|
try {
|
|
590
599
|
ws?.close(1e3);
|
|
591
600
|
} catch {}
|
|
592
|
-
},
|
|
601
|
+
}, OPEN_WAIT);
|
|
593
602
|
const opened = (ws) => {
|
|
594
603
|
clearTimeout(timer);
|
|
595
604
|
if (this.closed) {
|
|
@@ -599,6 +608,13 @@ var Connection = class Connection {
|
|
|
599
608
|
ready();
|
|
600
609
|
return;
|
|
601
610
|
}
|
|
611
|
+
if (this.ws && this.ws !== ws && this.ws.readyState === 1) {
|
|
612
|
+
try {
|
|
613
|
+
ws.close(1e3);
|
|
614
|
+
} catch {}
|
|
615
|
+
ready();
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
602
618
|
this.ws = ws;
|
|
603
619
|
this.heard = this.answeredAt = this.openedAt = performance.now();
|
|
604
620
|
this.probe = 0;
|
|
@@ -648,22 +664,39 @@ var Connection = class Connection {
|
|
|
648
664
|
}
|
|
649
665
|
});
|
|
650
666
|
}
|
|
651
|
-
/** Opens a socket again until one opens (one loop at a time)
|
|
652
|
-
*
|
|
653
|
-
|
|
667
|
+
/** Opens a socket again until one opens (one loop at a time): the first try at once, then after 50 ms doubling to
|
|
668
|
+
* 1 s, each wait jittered (clients a dead server dropped together do not come back together). A try still unopened
|
|
669
|
+
* at 100 ms gets a second socket beside it and at 200 ms a third, and the first to open serves (the others are
|
|
670
|
+
* closed): a socket the network sent to a replica that is gone costs at most 100 ms, while a slow open (a wake, a
|
|
671
|
+
* long replay, a takeover waiting for a dead replica's lease) is never given up. */
|
|
672
|
+
async reconnect(delay = 0) {
|
|
654
673
|
if (this.reopening || this.closed) return;
|
|
655
674
|
this.reopening = true;
|
|
656
675
|
this.stats.reconnects += 1;
|
|
676
|
+
const opens = () => this.open().then(() => true, () => false);
|
|
677
|
+
const firstOf = (tries) => new Promise((done) => {
|
|
678
|
+
let n = 0;
|
|
679
|
+
for (const p of tries) p.then((ok) => {
|
|
680
|
+
if (ok) done(true);
|
|
681
|
+
else if (++n === tries.length) done(false);
|
|
682
|
+
});
|
|
683
|
+
});
|
|
657
684
|
try {
|
|
658
|
-
for (;;) {
|
|
659
|
-
await new Promise((r) => setTimeout(r, delay));
|
|
685
|
+
for (let step = RETRY_FIRST;;) {
|
|
686
|
+
if (delay > 0) await new Promise((r) => setTimeout(r, delay));
|
|
660
687
|
if (this.closed || this.ws?.readyState === 1) return;
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
688
|
+
const tries = [opens()];
|
|
689
|
+
let opened = null;
|
|
690
|
+
for (let k = 0, at = 0; k <= HEDGE_AT.length; k += 1) {
|
|
691
|
+
const wait = k < HEDGE_AT.length ? new Promise((r) => setTimeout(() => r(null), HEDGE_AT[k] - at)) : new Promise(() => {});
|
|
692
|
+
opened = await Promise.race([firstOf(tries), wait]);
|
|
693
|
+
if (opened !== null) break;
|
|
694
|
+
at = HEDGE_AT[k];
|
|
695
|
+
tries.push(opens());
|
|
666
696
|
}
|
|
697
|
+
if (opened) return;
|
|
698
|
+
delay = step * (.5 + Math.random() / 2);
|
|
699
|
+
step = Math.min(RETRY_MAX, step * 2);
|
|
667
700
|
}
|
|
668
701
|
} finally {
|
|
669
702
|
this.reopening = false;
|
|
@@ -779,6 +812,7 @@ var Connection = class Connection {
|
|
|
779
812
|
* same tag, and its answer is still read here); "b" or "e": never waits for durable (server shapes), and its round
|
|
780
813
|
* trip tunes the conflation window. */
|
|
781
814
|
send(unit, op, keep = false, now = null, route = void 0, relax = null) {
|
|
815
|
+
if (this.http && !this.ws && !this.reopening && !this.closed) this.reconnect(0);
|
|
782
816
|
const i = op.i ?? this.next++;
|
|
783
817
|
const o = {
|
|
784
818
|
i,
|
|
@@ -1379,12 +1413,15 @@ var Connection = class Connection {
|
|
|
1379
1413
|
this.notifyRow(key);
|
|
1380
1414
|
}
|
|
1381
1415
|
const guess = optimistic || as != null ? null : this.predictor?.begin(name, args, route);
|
|
1382
|
-
const
|
|
1416
|
+
const op = {
|
|
1383
1417
|
k: "m",
|
|
1384
1418
|
f: name,
|
|
1385
1419
|
a: args,
|
|
1386
1420
|
t: tag ?? this.tag(),
|
|
1387
|
-
...asOf(as)
|
|
1421
|
+
...asOf(as)
|
|
1422
|
+
};
|
|
1423
|
+
const a = this.http && this.ws?.readyState !== 1 ? await this.overHttp(op) : await this.send(this.unitOf(route), {
|
|
1424
|
+
...op,
|
|
1388
1425
|
...this.urgent()
|
|
1389
1426
|
}, true, null, route);
|
|
1390
1427
|
guess?.end(a);
|
|
@@ -1395,6 +1432,68 @@ var Connection = class Connection {
|
|
|
1395
1432
|
if (refused(a)) throw coded(a);
|
|
1396
1433
|
return a;
|
|
1397
1434
|
}
|
|
1435
|
+
/** One query or mutation over HTTP (`POST <db>/call`, the token in its header), with no socket: its answer as a
|
|
1436
|
+
* socket's. A mutation's tag rides with it, so one sent again (after a failure the network gave no answer to)
|
|
1437
|
+
* applies once; a query, or a tagged mutation, is sent again on a 5xx or no answer, twice at most; a 401 renews the
|
|
1438
|
+
* token once. */
|
|
1439
|
+
async overHttp(op) {
|
|
1440
|
+
const body = JSON.stringify([{
|
|
1441
|
+
i: 1,
|
|
1442
|
+
...op
|
|
1443
|
+
}]), again = op.k === "q" || !!op.t;
|
|
1444
|
+
for (let tries = 0;; tries += 1) {
|
|
1445
|
+
let r;
|
|
1446
|
+
try {
|
|
1447
|
+
r = await fetchAt(`${this.base}/call`, {
|
|
1448
|
+
method: "POST",
|
|
1449
|
+
headers: {
|
|
1450
|
+
authorization: `Bearer ${this.token}`,
|
|
1451
|
+
"content-type": "application/json"
|
|
1452
|
+
},
|
|
1453
|
+
body
|
|
1454
|
+
});
|
|
1455
|
+
} catch (e) {
|
|
1456
|
+
if (again && tries < 2) continue;
|
|
1457
|
+
return {
|
|
1458
|
+
i: 1,
|
|
1459
|
+
ok: false,
|
|
1460
|
+
why: "unavailable",
|
|
1461
|
+
error: String(e?.message ?? e)
|
|
1462
|
+
};
|
|
1463
|
+
}
|
|
1464
|
+
if (r.status === 401 && this.refresh && tries === 0) {
|
|
1465
|
+
await this.renew(false);
|
|
1466
|
+
continue;
|
|
1467
|
+
}
|
|
1468
|
+
const text = await r.text();
|
|
1469
|
+
if (!r.ok) {
|
|
1470
|
+
if (r.status >= 500 && again && tries < 2) continue;
|
|
1471
|
+
let j = {};
|
|
1472
|
+
try {
|
|
1473
|
+
j = JSON.parse(text);
|
|
1474
|
+
} catch {}
|
|
1475
|
+
return {
|
|
1476
|
+
i: 1,
|
|
1477
|
+
ok: false,
|
|
1478
|
+
why: r.status === 401 ? "denied" : j.error ?? "refused",
|
|
1479
|
+
error: j.why ?? text.slice(0, 200)
|
|
1480
|
+
};
|
|
1481
|
+
}
|
|
1482
|
+
for (const line of text.split("\n")) try {
|
|
1483
|
+
const v = JSON.parse(line);
|
|
1484
|
+
if (Array.isArray(v)) {
|
|
1485
|
+
const a = v.find((x) => x?.i === 1);
|
|
1486
|
+
if (a) return a;
|
|
1487
|
+
}
|
|
1488
|
+
} catch {}
|
|
1489
|
+
return {
|
|
1490
|
+
i: 1,
|
|
1491
|
+
ok: false,
|
|
1492
|
+
why: "unavailable",
|
|
1493
|
+
error: "no answer in the call's reply"
|
|
1494
|
+
};
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1398
1497
|
/** An ACTION of a the database app (it may do I/O; not a transaction): called as a mutation is, by reference or
|
|
1399
1498
|
* name, tagged (a resend after a reconnect is answered from its tag). Resolves its answer. */
|
|
1400
1499
|
async action(name, args, options = {}) {
|
|
@@ -1403,12 +1502,13 @@ var Connection = class Connection {
|
|
|
1403
1502
|
/** A query function's value, once, on unit `unit` (default "0"). */
|
|
1404
1503
|
async query(name, args, { unit = "0", as } = {}) {
|
|
1405
1504
|
name = fnName(name);
|
|
1406
|
-
const
|
|
1505
|
+
const op = {
|
|
1407
1506
|
k: "q",
|
|
1408
1507
|
f: name,
|
|
1409
1508
|
a: args,
|
|
1410
1509
|
...asOf(as)
|
|
1411
|
-
}
|
|
1510
|
+
};
|
|
1511
|
+
const a = this.http && this.ws?.readyState !== 1 ? await this.overHttp(op) : await this.send(String(unit), op);
|
|
1412
1512
|
if (!a.ok) throw coded(a);
|
|
1413
1513
|
return a.value;
|
|
1414
1514
|
}
|
package/database/client.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DatabaseError } from "./errors.mjs";
|
|
2
|
-
import { t as Connection } from "../connection-
|
|
2
|
+
import { t as Connection } from "../connection-CCeSgaOV.mjs";
|
|
3
3
|
//#region src/database/client.ts
|
|
4
4
|
/**
|
|
5
5
|
* `@midsummer-ai/sdk/database`: a Project's Database, from its app or its server.
|
|
@@ -54,6 +54,7 @@ const caught = (error) => ({
|
|
|
54
54
|
});
|
|
55
55
|
async function connect(options) {
|
|
56
56
|
let url, token, refresh;
|
|
57
|
+
const server = !("url" in options);
|
|
57
58
|
if ("database" in options) {
|
|
58
59
|
const first = await options.database.token(options.binding);
|
|
59
60
|
url = first.url;
|
|
@@ -97,7 +98,8 @@ async function connect(options) {
|
|
|
97
98
|
}
|
|
98
99
|
const connection = await Connection.connect(url, {
|
|
99
100
|
...token !== void 0 ? { token } : {},
|
|
100
|
-
...refresh ? { refresh } : {}
|
|
101
|
+
...refresh ? { refresh } : {},
|
|
102
|
+
...server ? { shape: "server" } : {}
|
|
101
103
|
});
|
|
102
104
|
const call = (fn) => ({
|
|
103
105
|
name: fn.name,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midsummer-ai/sdk",
|
|
3
|
-
"version": "0.1.0-next.
|
|
3
|
+
"version": "0.1.0-next.1979",
|
|
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 { t as Connection } from "./connection-
|
|
1
|
+
import { t as Connection } from "./connection-CCeSgaOV.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 = {
|