velocious 1.0.726 → 1.0.728
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 +2 -2
- package/bin/velocious.js +2 -19
- package/build/application.js +32 -3
- package/build/background-jobs/main.js +9 -1
- package/build/background-jobs/worker.js +69 -6
- package/build/bin/velocious.js +2 -19
- package/build/cli/process-args.js +34 -0
- package/build/configuration-types.js +1 -1
- package/build/configuration.js +43 -2
- package/build/database/advisory-lock-lock-server-backend.js +30 -24
- package/build/database/advisory-lock-lock-server-embedded-client.js +74 -0
- package/build/database/advisory-lock-lock-server-engine.js +544 -0
- package/build/database/advisory-lock-lock-server-local-client.js +260 -0
- package/build/database/advisory-lock-lock-server-socket-path.js +17 -0
- package/build/database/advisory-lock-lock-server-transport.js +30 -0
- package/build/database/drivers/base.js +34 -12
- package/build/database/drivers/mssql/index.js +17 -18
- package/build/database/drivers/mssql/table.js +28 -25
- package/build/database/drivers/mysql/index.js +15 -18
- package/build/database/drivers/mysql/table.js +31 -23
- package/build/database/drivers/pgsql/index.js +15 -18
- package/build/database/drivers/pgsql/table.js +28 -26
- package/build/database/drivers/sqlite/base.js +7 -9
- package/build/database/drivers/sqlite/table.js +32 -26
- package/build/database/pool/async-tracked-multi-connection.js +1 -0
- package/build/database/pool/base.js +14 -4
- package/build/database/pool/single-multi-use.js +1 -0
- package/build/database/record/auditing.js +89 -11
- package/build/database/record/index.js +1 -1
- package/build/environment-handlers/base.js +50 -9
- package/build/environment-handlers/node/advisory-lock-lock-server-local-server.js +206 -0
- package/build/environment-handlers/node/cli/commands/server.js +1 -0
- package/build/environment-handlers/node.js +30 -3
- package/build/http-server/server-lock.js +55 -18
- package/build/src/application.d.ts +7 -4
- package/build/src/application.d.ts.map +1 -1
- package/build/src/application.js +27 -4
- package/build/src/background-jobs/main.d.ts +2 -1
- package/build/src/background-jobs/main.d.ts.map +1 -1
- package/build/src/background-jobs/main.js +10 -2
- package/build/src/background-jobs/worker.d.ts +42 -0
- package/build/src/background-jobs/worker.d.ts.map +1 -1
- package/build/src/background-jobs/worker.js +61 -5
- package/build/src/cli/process-args.d.ts +11 -0
- package/build/src/cli/process-args.d.ts.map +1 -0
- package/build/src/cli/process-args.js +34 -0
- package/build/src/configuration-types.d.ts +3 -1
- package/build/src/configuration-types.d.ts.map +1 -1
- package/build/src/configuration-types.js +2 -2
- package/build/src/configuration.d.ts +16 -1
- package/build/src/configuration.d.ts.map +1 -1
- package/build/src/configuration.js +39 -3
- package/build/src/database/advisory-lock-lock-server-backend.d.ts +10 -3
- package/build/src/database/advisory-lock-lock-server-backend.d.ts.map +1 -1
- package/build/src/database/advisory-lock-lock-server-backend.js +25 -22
- package/build/src/database/advisory-lock-lock-server-embedded-client.d.ts +24 -0
- package/build/src/database/advisory-lock-lock-server-embedded-client.d.ts.map +1 -0
- package/build/src/database/advisory-lock-lock-server-embedded-client.js +64 -0
- package/build/src/database/advisory-lock-lock-server-engine.d.ts +285 -0
- package/build/src/database/advisory-lock-lock-server-engine.d.ts.map +1 -0
- package/build/src/database/advisory-lock-lock-server-engine.js +460 -0
- package/build/src/database/advisory-lock-lock-server-local-client.d.ts +114 -0
- package/build/src/database/advisory-lock-lock-server-local-client.d.ts.map +1 -0
- package/build/src/database/advisory-lock-lock-server-local-client.js +227 -0
- package/build/src/database/advisory-lock-lock-server-socket-path.d.ts +9 -0
- package/build/src/database/advisory-lock-lock-server-socket-path.d.ts.map +1 -0
- package/build/src/database/advisory-lock-lock-server-socket-path.js +16 -0
- package/build/src/database/advisory-lock-lock-server-transport.d.ts +18 -0
- package/build/src/database/advisory-lock-lock-server-transport.d.ts.map +1 -0
- package/build/src/database/advisory-lock-lock-server-transport.js +27 -0
- package/build/src/database/drivers/base.d.ts +15 -2
- package/build/src/database/drivers/base.d.ts.map +1 -1
- package/build/src/database/drivers/base.js +33 -13
- package/build/src/database/drivers/mssql/index.d.ts +2 -2
- package/build/src/database/drivers/mssql/index.d.ts.map +1 -1
- package/build/src/database/drivers/mssql/index.js +17 -18
- package/build/src/database/drivers/mssql/table.d.ts.map +1 -1
- package/build/src/database/drivers/mssql/table.js +25 -23
- package/build/src/database/drivers/mysql/index.d.ts +2 -2
- package/build/src/database/drivers/mysql/index.d.ts.map +1 -1
- package/build/src/database/drivers/mysql/index.js +16 -19
- package/build/src/database/drivers/mysql/table.d.ts.map +1 -1
- package/build/src/database/drivers/mysql/table.js +27 -22
- package/build/src/database/drivers/pgsql/index.d.ts +2 -2
- package/build/src/database/drivers/pgsql/index.d.ts.map +1 -1
- package/build/src/database/drivers/pgsql/index.js +16 -19
- package/build/src/database/drivers/pgsql/table.d.ts.map +1 -1
- package/build/src/database/drivers/pgsql/table.js +25 -23
- package/build/src/database/drivers/sqlite/base.d.ts.map +1 -1
- package/build/src/database/drivers/sqlite/base.js +8 -10
- package/build/src/database/drivers/sqlite/table.d.ts.map +1 -1
- package/build/src/database/drivers/sqlite/table.js +29 -25
- package/build/src/database/pool/async-tracked-multi-connection.d.ts.map +1 -1
- package/build/src/database/pool/async-tracked-multi-connection.js +2 -1
- package/build/src/database/pool/base.d.ts +6 -0
- package/build/src/database/pool/base.d.ts.map +1 -1
- package/build/src/database/pool/base.js +14 -5
- package/build/src/database/pool/single-multi-use.d.ts.map +1 -1
- package/build/src/database/pool/single-multi-use.js +2 -1
- package/build/src/database/record/auditing.d.ts +14 -4
- package/build/src/database/record/auditing.d.ts.map +1 -1
- package/build/src/database/record/auditing.js +77 -12
- package/build/src/database/record/index.d.ts +4 -2
- package/build/src/database/record/index.d.ts.map +1 -1
- package/build/src/database/record/index.js +2 -2
- package/build/src/environment-handlers/base.d.ts +86 -7
- package/build/src/environment-handlers/base.d.ts.map +1 -1
- package/build/src/environment-handlers/base.js +48 -9
- package/build/src/environment-handlers/node/advisory-lock-lock-server-local-server.d.ts +2 -0
- package/build/src/environment-handlers/node/advisory-lock-lock-server-local-server.d.ts.map +1 -0
- package/build/src/environment-handlers/node/advisory-lock-lock-server-local-server.js +179 -0
- package/build/src/environment-handlers/node/cli/commands/server.d.ts.map +1 -1
- package/build/src/environment-handlers/node/cli/commands/server.js +2 -1
- package/build/src/environment-handlers/node.d.ts +10 -4
- package/build/src/environment-handlers/node.d.ts.map +1 -1
- package/build/src/environment-handlers/node.js +30 -4
- package/build/src/http-server/server-lock.d.ts +43 -14
- package/build/src/http-server/server-lock.d.ts.map +1 -1
- package/build/src/http-server/server-lock.js +54 -19
- package/build/src/tenants/default-tenant-database-provisioning.d.ts.map +1 -1
- package/build/src/tenants/default-tenant-database-provisioning.js +7 -1
- package/build/tenants/default-tenant-database-provisioning.js +8 -0
- package/package.json +2 -2
- package/src/application.js +32 -3
- package/src/background-jobs/main.js +9 -1
- package/src/background-jobs/worker.js +69 -6
- package/src/cli/process-args.js +34 -0
- package/src/configuration-types.js +1 -1
- package/src/configuration.js +43 -2
- package/src/database/advisory-lock-lock-server-backend.js +30 -24
- package/src/database/advisory-lock-lock-server-embedded-client.js +74 -0
- package/src/database/advisory-lock-lock-server-engine.js +544 -0
- package/src/database/advisory-lock-lock-server-local-client.js +260 -0
- package/src/database/advisory-lock-lock-server-socket-path.js +17 -0
- package/src/database/advisory-lock-lock-server-transport.js +30 -0
- package/src/database/drivers/base.js +34 -12
- package/src/database/drivers/mssql/index.js +17 -18
- package/src/database/drivers/mssql/table.js +28 -25
- package/src/database/drivers/mysql/index.js +15 -18
- package/src/database/drivers/mysql/table.js +31 -23
- package/src/database/drivers/pgsql/index.js +15 -18
- package/src/database/drivers/pgsql/table.js +28 -26
- package/src/database/drivers/sqlite/base.js +7 -9
- package/src/database/drivers/sqlite/table.js +32 -26
- package/src/database/pool/async-tracked-multi-connection.js +1 -0
- package/src/database/pool/base.js +14 -4
- package/src/database/pool/single-multi-use.js +1 -0
- package/src/database/record/auditing.js +89 -11
- package/src/database/record/index.js +1 -1
- package/src/environment-handlers/base.js +50 -9
- package/src/environment-handlers/node/advisory-lock-lock-server-local-server.js +206 -0
- package/src/environment-handlers/node/cli/commands/server.js +1 -0
- package/src/environment-handlers/node.js +30 -3
- package/src/http-server/server-lock.js +55 -18
- package/src/tenants/default-tenant-database-provisioning.js +8 -0
|
@@ -882,7 +882,7 @@
|
|
|
882
882
|
* behavior; the file backend is disabled by default and only selectable by
|
|
883
883
|
* explicit configuration.
|
|
884
884
|
* @typedef {object} AdvisoryLocksConfiguration
|
|
885
|
-
* @property {{file?: {directory?: string, enabled?: boolean, leaseMs?: number, renewIntervalMs?: number}, lockServer?: {authToken?: string, connectTimeoutMs?: number, enabled?: boolean, host?: string, leaseMs?: number, port?: number, reprobeCooldownMs?: number, url?: string}}} [backends] - Backend enablement. `file.enabled` defaults to false; enabling it is the deployment's explicit single-host declaration. `lockServer` defaults to enabled when a host/port/url or the `VELOCIOUS_LOCK_SERVER_HOST`/`_PORT` env pair is configured.
|
|
885
|
+
* @property {{file?: {directory?: string, enabled?: boolean, leaseMs?: number, renewIntervalMs?: number}, lockServer?: {authToken?: string, connectTimeoutMs?: number, enabled?: boolean, host?: string, leaseMs?: number, mode?: "embedded" | "external" | "local", port?: number, reprobeCooldownMs?: number, socketPath?: string, url?: string}}} [backends] - Backend enablement. `file.enabled` defaults to false; enabling it is the deployment's explicit single-host declaration. `lockServer` defaults to enabled when a host/port/url or the `VELOCIOUS_LOCK_SERVER_HOST`/`_PORT` env pair is configured.
|
|
886
886
|
* @property {import("./database/advisory-lock-backend-policy.js").AdvisoryLockBackendPolicyEntry[]} [policy] - Per-lock-name minimum/allowed/preferred tier rules (design §5).
|
|
887
887
|
*/
|
|
888
888
|
|
package/src/configuration.js
CHANGED
|
@@ -286,6 +286,14 @@ export default class VelociousConfiguration {
|
|
|
286
286
|
/** @type {Map<string, number>} */
|
|
287
287
|
_schemaCacheGenerationsByReuseKey = new Map()
|
|
288
288
|
|
|
289
|
+
/**
|
|
290
|
+
* Schema-metadata cache maps keyed by physical database reuse key. Every
|
|
291
|
+
* connection to one physical database shares a single map, so cached table
|
|
292
|
+
* and column introspection survives pool connection churn instead of being
|
|
293
|
+
* re-run on every re-spawned connection.
|
|
294
|
+
* @type {Map<string, Map<string, Promise<ReturnType<typeof JSON.parse>>>>} */
|
|
295
|
+
_schemaCacheMapsByReuseKey = new Map()
|
|
296
|
+
|
|
289
297
|
/**
|
|
290
298
|
* Runs current.
|
|
291
299
|
* @returns {VelociousConfiguration} - The current.
|
|
@@ -1422,6 +1430,8 @@ export default class VelociousConfiguration {
|
|
|
1422
1430
|
this.schemaCacheGenerationForReuseKey(reuseKey) + 1
|
|
1423
1431
|
)
|
|
1424
1432
|
|
|
1433
|
+
this.schemaCacheMapForReuseKey(reuseKey).clear()
|
|
1434
|
+
|
|
1425
1435
|
for (const pool of Object.values(this.databasePools)) {
|
|
1426
1436
|
if (pool.getConfigurationReuseKey() === reuseKey) {
|
|
1427
1437
|
pool.clearSchemaCache()
|
|
@@ -1438,6 +1448,22 @@ export default class VelociousConfiguration {
|
|
|
1438
1448
|
return this._schemaCacheGenerationsByReuseKey.get(reuseKey) || 0
|
|
1439
1449
|
}
|
|
1440
1450
|
|
|
1451
|
+
/**
|
|
1452
|
+
* Returns the schema-metadata cache map shared by every connection to one physical database, creating it on first use.
|
|
1453
|
+
* @param {string} reuseKey - Connection reuse key identifying the physical database.
|
|
1454
|
+
* @returns {Map<string, Promise<ReturnType<typeof JSON.parse>>>} - Shared schema metadata cache map.
|
|
1455
|
+
*/
|
|
1456
|
+
schemaCacheMapForReuseKey(reuseKey) {
|
|
1457
|
+
let schemaCacheMap = this._schemaCacheMapsByReuseKey.get(reuseKey)
|
|
1458
|
+
|
|
1459
|
+
if (!schemaCacheMap) {
|
|
1460
|
+
schemaCacheMap = new Map()
|
|
1461
|
+
this._schemaCacheMapsByReuseKey.set(reuseKey, schemaCacheMap)
|
|
1462
|
+
}
|
|
1463
|
+
|
|
1464
|
+
return schemaCacheMap
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1441
1467
|
/**
|
|
1442
1468
|
* Invalidates record metadata owned by one closed/deleted physical tenant
|
|
1443
1469
|
* database while preserving every other tenant generation.
|
|
@@ -4234,7 +4260,7 @@ export default class VelociousConfiguration {
|
|
|
4234
4260
|
* Full backward compatibility: an app that configures nothing gets today's
|
|
4235
4261
|
* driver-native-only behavior — the file backend is **disabled by default**
|
|
4236
4262
|
* and only ever selectable by explicit configuration.
|
|
4237
|
-
* @returns {{backends: {file: {directory?: string, enabled: boolean, leaseMs: number, renewIntervalMs: number}, lockServer: {authToken: string, connectTimeoutMs: number, enabled: boolean, leaseMs: number, reprobeCooldownMs: number, url: string}}, policy: import("./database/advisory-lock-backend-policy.js").AdvisoryLockBackendPolicyEntry[]}} - Advisory-lock configuration with defaults applied.
|
|
4263
|
+
* @returns {{backends: {file: {directory?: string, enabled: boolean, leaseMs: number, renewIntervalMs: number}, lockServer: {authToken: string, connectTimeoutMs: number, enabled: boolean, leaseMs: number, mode: "embedded" | "external" | "local", reprobeCooldownMs: number, socketPath: string, url: string}}, policy: import("./database/advisory-lock-backend-policy.js").AdvisoryLockBackendPolicyEntry[]}} - Advisory-lock configuration with defaults applied.
|
|
4238
4264
|
*/
|
|
4239
4265
|
getAdvisoryLockConfig() {
|
|
4240
4266
|
const configured = this._advisoryLocks || {}
|
|
@@ -4249,6 +4275,15 @@ export default class VelociousConfiguration {
|
|
|
4249
4275
|
: (typeof envPort === "number" && Number.isFinite(envPort) ? envPort : 3006)
|
|
4250
4276
|
const lockServerUrl = lockServerConfigured.url
|
|
4251
4277
|
|| `ws://${lockServerHost || "127.0.0.1"}:${lockServerPort}/websocket`
|
|
4278
|
+
// STC-715: `embedded` runs the lock-server engine in this process, `local`
|
|
4279
|
+
// spawns it as a child over stdio JSON-lines. Both default `enabled` to true
|
|
4280
|
+
// when the mode is named explicitly — no host/port/url required. `external`
|
|
4281
|
+
// (the default) keeps today's rule: enabled only via url/host/port config
|
|
4282
|
+
// or the env pair, so an app that configures nothing is unchanged.
|
|
4283
|
+
const configuredMode = lockServerConfigured.mode
|
|
4284
|
+
const lockServerMode = configuredMode === "embedded" || configuredMode === "local" || configuredMode === "external"
|
|
4285
|
+
? configuredMode
|
|
4286
|
+
: "external"
|
|
4252
4287
|
|
|
4253
4288
|
return {
|
|
4254
4289
|
backends: {
|
|
@@ -4265,9 +4300,13 @@ export default class VelociousConfiguration {
|
|
|
4265
4300
|
connectTimeoutMs: typeof lockServerConfigured.connectTimeoutMs === "number" ? lockServerConfigured.connectTimeoutMs : 3000,
|
|
4266
4301
|
enabled: typeof lockServerConfigured.enabled === "boolean"
|
|
4267
4302
|
? lockServerConfigured.enabled
|
|
4268
|
-
:
|
|
4303
|
+
: lockServerMode === "external"
|
|
4304
|
+
? Boolean(lockServerConfigured.url || lockServerConfigured.host || envHost || typeof lockServerConfigured.port === "number" || envPortRaw)
|
|
4305
|
+
: true,
|
|
4269
4306
|
leaseMs: typeof lockServerConfigured.leaseMs === "number" ? lockServerConfigured.leaseMs : 30_000,
|
|
4307
|
+
mode: lockServerMode,
|
|
4270
4308
|
reprobeCooldownMs: typeof lockServerConfigured.reprobeCooldownMs === "number" ? lockServerConfigured.reprobeCooldownMs : 5000,
|
|
4309
|
+
socketPath: typeof lockServerConfigured.socketPath === "string" ? lockServerConfigured.socketPath : "",
|
|
4271
4310
|
url: lockServerUrl
|
|
4272
4311
|
}
|
|
4273
4312
|
},
|
|
@@ -4337,11 +4376,13 @@ export default class VelociousConfiguration {
|
|
|
4337
4376
|
authToken: config.backends.lockServer.authToken,
|
|
4338
4377
|
connectTimeoutMs: config.backends.lockServer.connectTimeoutMs,
|
|
4339
4378
|
leaseMs: config.backends.lockServer.leaseMs,
|
|
4379
|
+
mode: config.backends.lockServer.mode,
|
|
4340
4380
|
onError: (/** @type {Error} */ error) => {
|
|
4341
4381
|
this.getErrorEvents().emit("framework-error", {context: "advisory-lock-lock-server", error})
|
|
4342
4382
|
this.getErrorEvents().emit("all-error", {context: "advisory-lock-lock-server", error, errorType: "framework-error"})
|
|
4343
4383
|
},
|
|
4344
4384
|
reprobeCooldownMs: config.backends.lockServer.reprobeCooldownMs,
|
|
4385
|
+
socketPath: config.backends.lockServer.socketPath,
|
|
4345
4386
|
url: config.backends.lockServer.url
|
|
4346
4387
|
})
|
|
4347
4388
|
|
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
|
-
import WebSocket from "ws"
|
|
4
|
-
|
|
5
|
-
import VelociousWebsocketClient from "../http-client/websocket-client.js"
|
|
6
3
|
import {AdvisoryLockServiceUnavailableError} from "./advisory-lock-errors.js"
|
|
7
4
|
|
|
8
5
|
/**
|
|
@@ -40,6 +37,13 @@ const LOCK_SERVER_CONNECTION_TYPE = "lock"
|
|
|
40
37
|
*
|
|
41
38
|
* Unavailability is cached for `reprobeCooldownMs` so a flapping server cannot
|
|
42
39
|
* turn every waiting caller into a connection attempt (design §2).
|
|
40
|
+
*
|
|
41
|
+
* The transport is injected through `clientFactory` (STC-715): this module no
|
|
42
|
+
* longer imports `ws` itself, so it is safe for browser/Expo bundles. The Node
|
|
43
|
+
* handler injects the websocket transport for `external` mode, the spawned
|
|
44
|
+
* stdio/UDS client for `local` mode, and the in-process engine client for
|
|
45
|
+
* `embedded` mode — all three speak the same protocol surface through this
|
|
46
|
+
* adapter.
|
|
43
47
|
*/
|
|
44
48
|
export default class AdvisoryLockLockServerBackend {
|
|
45
49
|
/**
|
|
@@ -47,14 +51,14 @@ export default class AdvisoryLockLockServerBackend {
|
|
|
47
51
|
* constructing this object never performs I/O.
|
|
48
52
|
* @param {object} args - Backend options.
|
|
49
53
|
* @param {string} [args.authToken] - Shared secret; the server fails closed without a matching token.
|
|
50
|
-
* @param {(args: {authToken: string, url: string}) => LockServerClientLike}
|
|
54
|
+
* @param {(args: {authToken: string, url: string}) => LockServerClientLike} args.clientFactory - Transport factory injected by the environment handler (websocket for external mode, spawned child for local mode, in-process engine for embedded mode).
|
|
51
55
|
* @param {number} [args.connectTimeoutMs] - Handshake timeout.
|
|
52
56
|
* @param {number} [args.leaseMs] - Lease duration used until the server advertises its own.
|
|
53
57
|
* @param {(error: Error) => void} [args.onError] - Async error reporter.
|
|
54
58
|
* @param {number} [args.reprobeCooldownMs] - How long an unavailable server is not re-probed.
|
|
55
|
-
* @param {string} args.url - Lock-server websocket URL.
|
|
59
|
+
* @param {string} args.url - Lock-server endpoint (websocket URL for external mode; informational for embedded/local modes).
|
|
56
60
|
*/
|
|
57
|
-
constructor({authToken = "", clientFactory
|
|
61
|
+
constructor({authToken = "", clientFactory, connectTimeoutMs = 3000, leaseMs = 30_000, onError, reprobeCooldownMs = 5000, url}) {
|
|
58
62
|
this.url = url
|
|
59
63
|
this.authToken = authToken
|
|
60
64
|
this.clientFactory = clientFactory
|
|
@@ -117,6 +121,8 @@ export default class AdvisoryLockLockServerBackend {
|
|
|
117
121
|
this.status.lastProbeAt = new Date().toISOString()
|
|
118
122
|
|
|
119
123
|
try {
|
|
124
|
+
if (typeof this.clientFactory !== "function") throw new Error("The lock-server backend requires a clientFactory transport from the environment handler")
|
|
125
|
+
|
|
120
126
|
const client = this.clientFactory({authToken: this.authToken, url: this.url})
|
|
121
127
|
|
|
122
128
|
this.client = client
|
|
@@ -350,6 +356,24 @@ export default class AdvisoryLockLockServerBackend {
|
|
|
350
356
|
return
|
|
351
357
|
}
|
|
352
358
|
|
|
359
|
+
// Transport-side death (spawned child exited, embedded engine closed):
|
|
360
|
+
// fail every pending request now and start the re-probe cooldown instead
|
|
361
|
+
// of letting each request burn its own timeout. This is the crash-detection
|
|
362
|
+
// half of the local-mode lifecycle (STC-715); the cooldown is the restart
|
|
363
|
+
// backoff and the next acquisition spawns the child again.
|
|
364
|
+
if (body.type === "response" && body.transportClosed === true) {
|
|
365
|
+
const error = new AdvisoryLockServiceUnavailableError(
|
|
366
|
+
`Lock server ${this.url} transport closed: ${String(body.error || "transport closed")}`,
|
|
367
|
+
{lockName: "", reason: "transport-closed"}
|
|
368
|
+
)
|
|
369
|
+
|
|
370
|
+
this.markUnavailable(error)
|
|
371
|
+
this.teardown()
|
|
372
|
+
this.reportError(error)
|
|
373
|
+
|
|
374
|
+
return
|
|
375
|
+
}
|
|
376
|
+
|
|
353
377
|
if (body.type === "lost") {
|
|
354
378
|
const name = String(body.name ?? "")
|
|
355
379
|
const hold = this.holds.get(name)
|
|
@@ -547,21 +571,3 @@ export default class AdvisoryLockLockServerBackend {
|
|
|
547
571
|
if (this.onError) this.onError(normalized)
|
|
548
572
|
}
|
|
549
573
|
}
|
|
550
|
-
|
|
551
|
-
/**
|
|
552
|
-
* Default transport factory: the framework websocket client with the Node
|
|
553
|
-
* `ws` implementation bound explicitly (no global mutation).
|
|
554
|
-
* @param {{authToken: string, url: string}} args - Connection args.
|
|
555
|
-
* @returns {LockServerClientLike} - Websocket client.
|
|
556
|
-
*/
|
|
557
|
-
function defaultClientFactory({url}) {
|
|
558
|
-
// Node's global WebSocket is not guaranteed to exist, so the implementation is
|
|
559
|
-
// bound explicitly instead of mutating `globalThis`.
|
|
560
|
-
const webSocketImplementation = /** @type {typeof globalThis.WebSocket} */ (/** @type {unknown} */ (WebSocket))
|
|
561
|
-
|
|
562
|
-
return /** @type {LockServerClientLike} */ (/** @type {unknown} */ (new VelociousWebsocketClient({
|
|
563
|
-
heartbeatIntervalMs: 10_000,
|
|
564
|
-
url,
|
|
565
|
-
webSocketImplementation
|
|
566
|
-
})))
|
|
567
|
-
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import AdvisoryLockLockServerEngine from "./advisory-lock-lock-server-engine.js"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {import("./advisory-lock-lock-server-backend.js").LockServerClientLike} LockServerClientLike
|
|
7
|
+
* @typedef {import("./advisory-lock-lock-server-backend.js").LockServerConnectionLike} LockServerConnectionLike
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* In-process transport factory for the `embedded` lock-server mode (STC-715).
|
|
12
|
+
* It wires the lock-server backend adapter to an `AdvisoryLockLockServerEngine`
|
|
13
|
+
* living in the same process over plain function calls — no sockets, no `ws`,
|
|
14
|
+
* no Node builtins — so browser/Expo bundles can run the lock-server tier.
|
|
15
|
+
*
|
|
16
|
+
* The engine mints the holder id for the connection exactly like the external
|
|
17
|
+
* daemon does over the websocket transport, so the adapter's lease/epoch/
|
|
18
|
+
* fencing behavior is identical across modes.
|
|
19
|
+
* @param {AdvisoryLockLockServerEngine} engine - Engine this transport connects to.
|
|
20
|
+
* @returns {(args: {authToken: string, url: string}) => LockServerClientLike} - Client factory for the backend adapter.
|
|
21
|
+
*/
|
|
22
|
+
export function createEmbeddedLockServerClientFactory(engine) {
|
|
23
|
+
return () => {
|
|
24
|
+
/** @type {string | null} */
|
|
25
|
+
let connectionId = null
|
|
26
|
+
/** @type {((body: Record<string, unknown>) => void) | null} */
|
|
27
|
+
let onMessage = null
|
|
28
|
+
|
|
29
|
+
/** @type {LockServerClientLike} */
|
|
30
|
+
const client = {
|
|
31
|
+
async connect() {},
|
|
32
|
+
|
|
33
|
+
close() {
|
|
34
|
+
if (connectionId !== null) engine.connectionClose(connectionId)
|
|
35
|
+
|
|
36
|
+
connectionId = null
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
openConnection(_type, {onMessage: messageHandler}) {
|
|
40
|
+
onMessage = messageHandler
|
|
41
|
+
|
|
42
|
+
connectionId = engine.openConnection({
|
|
43
|
+
onClose: () => {
|
|
44
|
+
onMessage?.({error: "Embedded lock server engine is closed", status: "transport-closed", transportClosed: true, type: "response"})
|
|
45
|
+
},
|
|
46
|
+
send: (/** @type {Record<string, unknown>} */ body) => messageHandler(body)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
// The engine answers the connection-open frame with a hello-ack, just
|
|
50
|
+
// like the external daemon does after verifying auth and protocol.
|
|
51
|
+
queueMicrotask(() => messageHandler({leaseMs: engine.leaseMs, protocolVersion: 2, type: "hello-ack"}))
|
|
52
|
+
|
|
53
|
+
/** @type {LockServerConnectionLike} */
|
|
54
|
+
const connection = {
|
|
55
|
+
close: () => {
|
|
56
|
+
if (connectionId !== null) engine.connectionClose(connectionId)
|
|
57
|
+
|
|
58
|
+
connectionId = null
|
|
59
|
+
},
|
|
60
|
+
ready: Promise.resolve(),
|
|
61
|
+
sendMessage: (/** @type {Record<string, unknown>} */ body) => {
|
|
62
|
+
if (connectionId === null) throw new Error("Embedded lock server connection is not open")
|
|
63
|
+
|
|
64
|
+
engine.handleMessage(connectionId, body)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return connection
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return client
|
|
73
|
+
}
|
|
74
|
+
}
|