totalum-sdk 1.8.0 → 1.8.1

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/CHANGELOG.md CHANGED
@@ -20,15 +20,24 @@ regenerated), plus the request each call sends to SDK-API (`compat/requests/*.js
20
20
  - **Dist-tags:** `next` is the newest release; `latest` moves only by a decision at launch. `dev` holds the old
21
21
  `0.1.0-dev.N` prereleases.
22
22
 
23
- ## Unreleased
23
+ ## 1.8.1 — 2026-10-04
24
+
25
+ ### Fixed
26
+
27
+ - `withTotalumSession`: a mutation's (`POST`, …) pure read that the primary does not answer in 2 s is asked again on
28
+ the primary, as a stalled replica read already was (B3-AT4: a sign-up waited on its first `SELECT` until the Worker
29
+ was cancelled while the same database answered other requests). Writes are never repeated. Logs one
30
+ `d1_primary_read_timeout` warning. No public type or request changes.
31
+
32
+ ## 1.8.0 — 2026-10-04
33
+
34
+ ### Changed (also in this release)
24
35
 
25
36
  - `ai.video.generate`: `firstFrame`, `lastFrame` and `references` now reach the video model (they were ignored,
26
37
  B2-BI6). `webhook` is removed from its options: it never did anything (no video event exists); poll
27
38
  `ai.video.wait(jobId)`. Removed before launch (`latest` never pointed at a release with it), so no deprecation cycle.
28
39
  - Types regenerated from contracts `develop` (new error codes).
29
40
 
30
- ## 1.8.0 — 2026-10-04
31
-
32
41
  ### Added
33
42
 
34
43
  - `TotalumError.errorCode` also lists `KEY_NOT_FOUND` (an unknown API key id; contracts, swfix).
@@ -102,8 +102,11 @@ declare const READ_YOUR_WRITES_COOKIE = "__tlm_d1_w";
102
102
  * `first-unconstrained` session — the nearest replica — unless the browser wrote in the last 10 s; any other method
103
103
  * uses the plain binding (writes go to the primary; measured faster than any session) and its response sets the short
104
104
  * cookie that sends that browser's next reads to the primary. A replica read that takes over `REPLICA_READ_TIMEOUT_MS`
105
- * is answered by the primary instead, and the rest of that request reads the primary. `run` receives the env to hand to the app, whose
106
- * `totalumD1()` reads `TOTALUM_DB` from it on every statement. No D1 binding (Turso, a laptop): `run(env)` unchanged.
105
+ * is answered by the primary instead, and the rest of that request reads the primary. A mutation's pure read the primary
106
+ * does not answer in that time is asked again on the primary (B3-AT4: one call stalled while the database answered
107
+ * others; a sign-up waited on its first SELECT until the Worker was cancelled). `run` receives the env to hand to the
108
+ * app, whose `totalumD1()` reads `TOTALUM_DB` from it on every statement. No D1 binding (Turso, a laptop): `run(env)`
109
+ * unchanged.
107
110
  */
108
111
  declare function withTotalumSession<E extends object>(request: Request, env: E, run: (env: E) => Promise<Response>): Promise<Response>;
109
112
 
@@ -17,8 +17,11 @@ const LATE = Symbol('late');
17
17
  * `first-unconstrained` session — the nearest replica — unless the browser wrote in the last 10 s; any other method
18
18
  * uses the plain binding (writes go to the primary; measured faster than any session) and its response sets the short
19
19
  * cookie that sends that browser's next reads to the primary. A replica read that takes over `REPLICA_READ_TIMEOUT_MS`
20
- * is answered by the primary instead, and the rest of that request reads the primary. `run` receives the env to hand to the app, whose
21
- * `totalumD1()` reads `TOTALUM_DB` from it on every statement. No D1 binding (Turso, a laptop): `run(env)` unchanged.
20
+ * is answered by the primary instead, and the rest of that request reads the primary. A mutation's pure read the primary
21
+ * does not answer in that time is asked again on the primary (B3-AT4: one call stalled while the database answered
22
+ * others; a sign-up waited on its first SELECT until the Worker was cancelled). `run` receives the env to hand to the
23
+ * app, whose `totalumD1()` reads `TOTALUM_DB` from it on every statement. No D1 binding (Turso, a laptop): `run(env)`
24
+ * unchanged.
22
25
  */
23
26
  export async function withTotalumSession(request, env, run) {
24
27
  const db = env.TOTALUM_DB;
@@ -26,11 +29,11 @@ export async function withTotalumSession(request, env, run) {
26
29
  return run(env);
27
30
  const binding = db;
28
31
  if (!READS.has(request.method))
29
- return withCookie(await run(env));
32
+ return withCookie(await run({ ...env, TOTALUM_DB: bounded(binding, binding, 'primary').db }));
30
33
  const cookies = request.headers.get('cookie') ?? '';
31
34
  if (new RegExp(`(?:^|;\\s*)${READ_YOUR_WRITES_COOKIE}=`).test(cookies))
32
35
  return run(env);
33
- const replica = boundedReplica(binding, binding.withSession('first-unconstrained'));
36
+ const replica = bounded(binding, binding.withSession('first-unconstrained'), 'replica');
34
37
  const res = await run({ ...env, TOTALUM_DB: replica.db });
35
38
  // a GET that wrote (a first visit creating its row, an OAuth callback) sends the browser's next reads to the primary too
36
39
  return replica.wrote() ? withCookie(res) : res;
@@ -43,12 +46,13 @@ function withCookie(res) {
43
46
  return out;
44
47
  }
45
48
  /**
46
- * The request's database on the nearest replica, each pure read bounded by `REPLICA_READ_TIMEOUT_MS`: past it, the read
47
- * runs on the primary (the stalled replica call is abandoned) and so does every later statement of this request. Any
48
- * other statement (a write, even one that fails) runs on the primary and so does the rest of the request: a lost-race
49
- * re-read (better-auth's rate limiter: insert → UNIQUE → read again) must see the row the replica has not received yet.
49
+ * The request's database on `first` (the nearest replica, or the primary itself for a mutation), each pure read bounded
50
+ * by `REPLICA_READ_TIMEOUT_MS`: past it, the read runs again on the primary (the stalled call is abandoned) and so does
51
+ * every later statement of this request. Any other statement (a write, even one that fails) runs on the primary once
52
+ * and so does the rest of the request: a lost-race re-read (better-auth's rate limiter: insert → UNIQUE → read again)
53
+ * must see the row the replica has not received yet.
50
54
  */
51
- function boundedReplica(binding, session) {
55
+ function bounded(binding, first, on) {
52
56
  let primary = false;
53
57
  let wrote = false;
54
58
  async function read(sqls, call) {
@@ -65,7 +69,7 @@ function boundedReplica(binding, session) {
65
69
  }, REPLICA_READ_TIMEOUT_MS);
66
70
  });
67
71
  try {
68
- const out = await Promise.race([call(session), late]);
72
+ const out = await Promise.race([call(first), late]);
69
73
  if (out !== LATE)
70
74
  return out;
71
75
  }
@@ -73,7 +77,7 @@ function boundedReplica(binding, session) {
73
77
  clearTimeout(timer);
74
78
  }
75
79
  primary = true;
76
- console.warn(JSON.stringify({ event: 'd1_replica_read_timeout', ms: REPLICA_READ_TIMEOUT_MS }));
80
+ console.warn(JSON.stringify({ event: `d1_${on}_read_timeout`, ms: REPLICA_READ_TIMEOUT_MS }));
77
81
  return call(binding);
78
82
  }
79
83
  const statement = (sql, params = []) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "totalum-sdk",
3
- "version": "1.8.0",
3
+ "version": "1.8.1",
4
4
  "description": "The SDK generated Totalum apps import: every integration namespace over SDK-API (totalum-sdk, or one subpath each: /files, /ai, /web, \u2026) and totalum-sdk/d1, the D1 driver",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.8.0",
2
+ "version": "1.8.1",
3
3
  "methods": [
4
4
  {
5
5
  "call": "totalum.files.upload(input, { name?, contentType?, private?, folder? })",