@aria-framework/db-worker 0.27.0 → 0.27.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/README.md CHANGED
@@ -437,6 +437,17 @@ Against 0.25.0's code, 6 of its 10 checks fail.
437
437
 
438
438
  ## Changelog
439
439
 
440
+ - **0.27.1** — the UTC pin is WAITED FOR. 0.27.0 fired `SET TIME ZONE 'UTC'` from the pool's
441
+ `connect` event without awaiting it, so the first query on every new connection was issued
442
+ while the SET was still running: pg 8 prints "Calling client.query() when the client is already
443
+ executing a query is deprecated" (seen on an app at startup), and pg 9 makes it a failed query.
444
+ The SET now runs in pg-pool's awaited `onConnect` hook — installed on the pool's options, so
445
+ `pinPoolUtc` covers a pool the app built too; an `onConnect` already there still runs, after the
446
+ pin. A pg-pool without the hook falls back to the old `connect` path. **Behaviour change:** a SET
447
+ that fails now fails that checkout (the query rejects, the client is discarded, the error is
448
+ logged) instead of handing out an unpinned session. `test/pinAwaited.pg.js` (N concurrent
449
+ queries on N fresh connections: no overlap, every connection UTC) and `test/pinPoolUtc.js`
450
+ (wiring) — against 0.27.0, 4 of 15 and 5 of 6 fail.
440
451
  - **0.27.0** — "Time is UTC" (repo README): a Postgres session and a Date on the wire no longer
441
452
  depend on the server's, the role's, the connection string's or the process's timezone.
442
453
  - **Every connection the driver opens runs `SET TIME ZONE 'UTC'` first** — the app pool and the
package/driver/pgUtc.js CHANGED
@@ -21,10 +21,23 @@
21
21
  * SET issued after the session starts overrides the server default, the database default, the
22
22
  * role default and the startup packet alike.
23
23
  *
24
- * WHY IT IS IN TIME. pg-pool emits 'connect' synchronously, BEFORE handing the new client to
25
- * whoever asked for it, and a client runs its queries in the order they were queued. The SET is
26
- * therefore the first statement on every connection, ahead of the caller's first query.
27
- * test/timezone.pg.js proves it against a pool whose connection string asks for another zone.
24
+ * WHY IT IS IN TIME — AND WAITED FOR. pg-pool awaits its `onConnect(client)` option after a
25
+ * connection opens and BEFORE the client is handed to whoever asked for it, so the SET has
26
+ * COMPLETED before the caller's first query is issued. Until 0.27.1 the SET was fired from the
27
+ * 'connect' event and not awaited: still first in the client's queue, but the caller's query was
28
+ * then issued while it ran — pg 8 prints "Calling client.query() when the client is already
29
+ * executing a query is deprecated", and pg 9 removes that queue, which makes it a failed query
30
+ * on a fresh connection. The hook is installed on the pool's own options, so it covers a pool
31
+ * the CALLER built too; a hook already there still runs, after the pin.
32
+ * test/timezone.pg.js proves the zone against a pool whose connection string asks for another;
33
+ * test/pinAwaited.pg.js proves that no query overlaps the SET.
34
+ *
35
+ * A pg-pool too old to know `onConnect` ignores it, and the 'connect' listener below then sees
36
+ * a client the hook never pinned and issues the SET the old way: pinned, with the warning.
37
+ *
38
+ * A SET THAT FAILS FAILS THE CHECKOUT: pg-pool ends that client and the caller's query rejects
39
+ * with the error (logged here too). An unpinned session is exactly the silent failure this
40
+ * exists to prevent, so it is no longer handed out.
28
41
  *
29
42
  * LIMIT, stated: only connections opened AFTER the call are pinned. Call it on a pool before
30
43
  * its first query — which is what the driver and the session-store factory both do.
@@ -32,7 +45,9 @@
32
45
 
33
46
  'use strict';
34
47
 
48
+ const PIN_SQL = "SET TIME ZONE 'UTC'";
35
49
  const PINNED = new WeakSet();
50
+ const PINNED_CLIENTS = new WeakSet();
36
51
 
37
52
  /**
38
53
  * @param {object} pool a node-postgres Pool
@@ -43,12 +58,22 @@ function pinPoolUtc(pool, opts = {}) {
43
58
  if (!pool || typeof pool.on !== 'function') throw new Error('pinPoolUtc(pool): a pg Pool is required');
44
59
  if (PINNED.has(pool)) return pool;
45
60
  PINNED.add(pool);
61
+ const say = (err) => {
62
+ // Loud, because an unpinned session is exactly the silent failure this exists to prevent.
63
+ try { (opts.logger || console).error(`postgres: could not pin a session to UTC (${err.message})`); }
64
+ catch (_) { /* a logging failure must not become an unhandled rejection */ }
65
+ };
66
+ if (!pool.options) pool.options = {};
67
+ const prior = pool.options.onConnect;
68
+ pool.options.onConnect = async (client) => {
69
+ try { await client.query(PIN_SQL); } catch (err) { say(err); throw err; }
70
+ PINNED_CLIENTS.add(client);
71
+ if (typeof prior === 'function') await prior(client);
72
+ };
73
+ // Fallback for a pg-pool without the hook; a no-op for every client the hook pinned.
46
74
  pool.on('connect', (client) => {
47
- client.query("SET TIME ZONE 'UTC'").catch((err) => {
48
- // Loud, because an unpinned session is exactly the silent failure this exists to prevent.
49
- try { (opts.logger || console).error(`postgres: could not pin a session to UTC (${err.message})`); }
50
- catch (_) { /* a logging failure must not become an unhandled rejection */ }
51
- });
75
+ if (PINNED_CLIENTS.has(client)) return;
76
+ client.query(PIN_SQL).catch(say);
52
77
  });
53
78
  return pool;
54
79
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aria-framework/db-worker",
3
- "version": "0.27.0",
3
+ "version": "0.27.1",
4
4
  "description": "Aria App Framework — db-worker module. Async RPC bridge over synchronous better-sqlite3: a worker-thread dispatcher (runWorker), a main-thread client with per-call timeouts and crash handling (DbClient), and a SQLite layer with WAL pragmas + a numbered-migration runner (database).",
5
5
  "license": "UNLICENSED",
6
6
  "private": false,
@@ -40,7 +40,7 @@
40
40
  "pg": "^8.23.0"
41
41
  },
42
42
  "scripts": {
43
- "test": "node test/driverContract.js && node test/migrations.js && node test/migrations-notx.js && node test/migrationDialect.js && node test/regressions.js && node test/transactions.js && node test/tenantContext.js && node test/tenancyMigration.js && node test/tenancyAttribution.pglite.js && node test/tenancyNullable.pg.js && node test/tenancyPartnerFk.pg.js && node test/tenancyOnUpdate.pg.js && node test/dialectIntrospection.js && node test/tenantRls.pg.js && node test/nestedTransaction.pg.js && node test/clientError.pg.js && node test/migrationRole.pg.js && node test/smoke.js && node test/tier2.js && node test/timezone.pg.js",
43
+ "test": "node test/driverContract.js && node test/migrations.js && node test/migrations-notx.js && node test/migrationDialect.js && node test/regressions.js && node test/transactions.js && node test/tenantContext.js && node test/tenancyMigration.js && node test/tenancyAttribution.pglite.js && node test/tenancyNullable.pg.js && node test/tenancyPartnerFk.pg.js && node test/tenancyOnUpdate.pg.js && node test/dialectIntrospection.js && node test/tenantRls.pg.js && node test/nestedTransaction.pg.js && node test/clientError.pg.js && node test/migrationRole.pg.js && node test/smoke.js && node test/tier2.js && node test/timezone.pg.js && node test/pinPoolUtc.js && node test/pinAwaited.pg.js",
44
44
  "prepublishOnly": "node ../../test/packaging.js db-worker"
45
45
  }
46
46
  }