@eliware/elera-lib 0.1.3 → 0.1.4

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
@@ -2,7 +2,7 @@
2
2
 
3
3
  The alternative SQL client for Eliware applications. It provides generic
4
4
  primary/balanced MySQL or MariaDB routing without embedding Elera, HAProxy,
5
- backup, or GitOps policy. It is a v0.1.3 alternative to `@eliware/mysql`; the
5
+ backup, or GitOps policy. It is a v0.1.4 alternative to `@eliware/mysql`; the
6
6
  existing package is intentionally unchanged.
7
7
 
8
8
  `primary` is the preferred connection path. `balanced` is an optional alternate
@@ -26,12 +26,17 @@ accounts. Pool settings may be supplied with `MYSQL_CONNECT_TIMEOUT`,
26
26
  `MYSQL_SSL`. Configure primary and balanced routes explicitly; applications
27
27
  should not rely on ambiguous single-endpoint aliases.
28
28
 
29
+ See examples/basic-client.mjs for a complete consumer example using only the
30
+ public package API. Its usage notes are in examples/README.md.
31
+
29
32
  Routing bundles passed to `createDbFromBundle` use the normalized shape
30
33
  `routes.primary` and `routes.balanced`, each containing ordered `{ host, port,
31
- weight }` nodes. A bundle also carries `database`, `identity`, optional
32
- `credentials`, and `expiresAt`; `validateBundle` rejects expired or malformed
33
- route data. The checked-in contract fixture documents the supervisor-facing
34
- wire representation separately.
34
+ weight }` nodes. A bundle may also carry an explicit `writer`, ordered
35
+ `failover`, and `readers` assignment. The bundle carries `database`,
36
+ `identity`, optional `credentials`, and `expiresAt`; `validateBundle` rejects
37
+ expired, malformed, duplicated, or conflicting route data. The checked-in
38
+ contract fixture documents the supervisor-facing wire representation
39
+ separately.
35
40
 
36
41
  The implementation accepts optional routing bundles and injected credential
37
42
  providers, maintains bounded pools per route, supports ordered writer/reader
@@ -44,13 +49,13 @@ CLI commands. Applications provide those integrations through ordinary
44
49
  configuration and callbacks.
45
50
 
46
51
  When a route node is drained, the client immediately stops assigning new work
47
- to that node while existing operations continue. The default drain window is
48
- 45 seconds and can be changed with `drainTimeoutMs`; remaining pool
49
- connections are then force-closed. `client.drain(host)` returns `wait()` and
50
- `forceClose()` operations, while `client.nodeStates()` exposes lifecycle and
51
- active-operation state. Only conservative, single-statement reads are eligible
52
- for automatic retry after a connection failure; uncertain writes are never
53
- retried automatically.
52
+ to that node while existing operations continue. The drain window defaults to
53
+ 45 seconds and is capped at 45 seconds; remaining pool connections are then
54
+ force-closed. `client.drain(host)` returns `wait()` and `forceClose()`
55
+ operations, while `client.nodeStates()` exposes lifecycle and active-operation
56
+ state. Only conservative, single-statement reads are eligible for automatic
57
+ retry after a connection failure; uncertain writes are never retried
58
+ automatically.
54
59
 
55
60
  The public client intentionally exposes SQL operations, health, routing,
56
61
  lifecycle, and optional routing-event synchronization methods. REST and
package/RELEASE_NOTES.md CHANGED
@@ -1,5 +1,42 @@
1
1
  # Release notes
2
2
 
3
+ ## 0.1.4 — Explicit writer and failover routing
4
+
5
+ This release strengthens generic client-side routing for supervisor-provided
6
+ bundles. Applications still use the public library API; supervisor and CLI
7
+ policy remain outside the package.
8
+
9
+ ### Added
10
+
11
+ - Supports explicit `writer`, ordered `failover`, and `readers` assignments in
12
+ routing bundles.
13
+ - Replaces active writer and reader pools immediately when a valid routing
14
+ update arrives through WebSocket or REST resynchronization.
15
+ - Keeps application clients independent so updating one application's bundle
16
+ does not change another client's assignment.
17
+ - Selects the writer first and fails over in the supplied order when a node is
18
+ drained or explicitly unavailable.
19
+ - Completes in-flight work during drain while excluding the node from new work.
20
+ - Enforces a 45-second maximum client-side drain window.
21
+
22
+ ### Fixed and hardened
23
+
24
+ - Compares numeric and string bundle versions numerically and rejects stale
25
+ updates, including versions such as `v10` versus `v9`.
26
+ - Validates routing hosts, ports, weights, duplicate nodes, and writer/failover
27
+ overlap before pool creation.
28
+ - Preserves explicit writer, failover, and reader assignments during stream
29
+ refreshes.
30
+
31
+ ### Validation
32
+
33
+ - Adds integration-style coverage for pool replacement, per-client assignment
34
+ isolation, writer failover, in-flight drain behavior, version ordering,
35
+ WebSocket reconnect, and REST fallback.
36
+ - Maintains 100% statements, branches, functions, and lines coverage with zero
37
+ lint warnings.
38
+ - Diff validation passes.
39
+
3
40
  ## 0.1.3 — Client-side routing drain lifecycle
4
41
 
5
42
  This patch adds generic client-side handling for supervisor-published routing
@@ -0,0 +1,13 @@
1
+ # Elera library example
2
+
3
+ basic-client.mjs is a minimal consumer application. It imports only the
4
+ public @eliware/elera-lib package API, opens a bounded SQL client from
5
+ environment configuration, performs a health check and query, and closes the
6
+ client in a finally block.
7
+
8
+ Run it from a published-package consumer project with:
9
+
10
+ MYSQL_PRIMARY_HOST=127.0.0.1 MYSQL_USER=app MYSQL_PASSWORD=secret MYSQL_DATABASE=app node examples/basic-client.mjs
11
+
12
+ The example is intentionally infrastructure-neutral. It does not contain
13
+ Docker, Kubernetes, Supervisor, CLI, Galera, or test-lab setup.
@@ -0,0 +1,14 @@
1
+ import { createDbFromEnvironment } from '@eliware/elera-lib';
2
+
3
+ // Set MYSQL_PRIMARY_HOST, MYSQL_USER, MYSQL_PASSWORD, and MYSQL_DATABASE
4
+ // before running this example. The same public API works with a routing bundle.
5
+ const db = await createDbFromEnvironment();
6
+
7
+ try {
8
+ const health = await db.health('primary');
9
+ if (!health.ok) throw new Error('primary SQL route is not healthy');
10
+ const [rows] = await db.query('SELECT 1 AS healthy');
11
+ console.log(JSON.stringify({ health, rows }));
12
+ } finally {
13
+ await db.close();
14
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eliware/elera-lib",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Generic MySQL and MariaDB client with resilient routing, client-side drains, and failover",
5
5
  "keywords": [
6
6
  "eliware",
@@ -35,6 +35,7 @@
35
35
  },
36
36
  "files": [
37
37
  "src",
38
+ "examples",
38
39
  "README.md",
39
40
  "RELEASE_NOTES.md",
40
41
  "LICENSE",
package/src/bundle.mjs CHANGED
@@ -1,13 +1,17 @@
1
+ import { validateRoutingNode, validateRoutingNodes } from './routing/node-validation.mjs';
1
2
  const routes = ['primary', 'balanced'];
2
3
 
3
4
  export function validateBundle(bundle) {
4
5
  if (!bundle || typeof bundle !== 'object') throw new TypeError('routing bundle is required');
5
6
  if (!bundle.expiresAt || Number.isNaN(Date.parse(bundle.expiresAt))) throw new TypeError('routing bundle expiresAt is required');
7
+ const writer = bundle.writer !== undefined ? validateRoutingNode(bundle.writer, 'routing bundle writer') : undefined;
8
+ const failover = bundle.failover !== undefined ? validateRoutingNodes(bundle.failover, 'routing bundle failover') : [];
9
+ if (writer && failover.some((node) => node.host === writer.host && node.port === writer.port)) throw new TypeError('routing bundle failover duplicates writer');
10
+ if (bundle.readers !== undefined) validateRoutingNodes(bundle.readers, 'routing bundle readers');
6
11
  for (const route of routes) {
7
12
  if (bundle.routes?.[route] !== undefined && !Array.isArray(bundle.routes[route])) throw new TypeError(`bundle.routes.${route} must be an array`);
8
13
  for (const node of bundle.routes?.[route] ?? []) {
9
- if (!node.host || !Number.isInteger(Number(node.port)) || Number(node.port) < 1 || Number(node.port) > 65535) throw new TypeError(`invalid ${route} bundle node`);
10
- if (node.weight !== undefined && (!Number.isFinite(Number(node.weight)) || Number(node.weight) < 0)) throw new TypeError(`invalid ${route} bundle weight`);
14
+ validateRoutingNode(node, `routing bundle ${route} node`);
11
15
  }
12
16
  }
13
17
  return bundle;
@@ -0,0 +1,7 @@
1
+ import { validateRoutingNode } from '../routing/node-validation.mjs';
2
+
3
+ export function bundleProfiles(bundle, route, base) {
4
+ if (route === 'primary' && bundle.writer?.host) return [bundle.writer, ...(bundle.failover ?? [])].map((node, index) => { const valid = validateRoutingNode(node, `primary assignment[${index}]`); return { ...base, host: valid.host, port: valid.port, weight: valid.weight }; });
5
+ const nodes = route === 'balanced' && bundle.readers?.length ? bundle.readers : bundle.routes?.[route] ?? [];
6
+ return nodes.map((node) => { const valid = validateRoutingNode(node, `${route} route node`); return { ...base, host: valid.host, port: valid.port, weight: valid.weight }; });
7
+ }
@@ -6,11 +6,10 @@ import { resolveCredentials, credentialContext } from '../credential-provider.mj
6
6
  import { validateBundle, bundleExpired, bundleNeedsRefresh } from '../bundle.mjs';
7
7
  import { createRouteFactory } from './route-factory.mjs';
8
8
  import { classifyQuery, routeFor } from '../routing.mjs';
9
+ import { compareBundleVersions } from '../routing/bundle-version.mjs';
10
+ import { clientDrainTimeout } from '../lifecycle/drain-policy.mjs';
9
11
 
10
- const olderVersion = (candidate, current) => {
11
- if (candidate === undefined || current === undefined) return false;
12
- return String(candidate) < String(current);
13
- };
12
+ const olderVersion = (candidate, current) => compareBundleVersions(candidate, current) < 0;
14
13
 
15
14
  export async function createDb({ primary, balanced, bundle, credentialProvider, mysqlLib = mysql, log = defaultLog, routing = 'auto', identity, quarantineMs = 5000, drainTimeoutMs = 45000, now = () => Date.now() } = {}) {
16
15
  if (!primary || typeof primary !== 'object') throw new TypeError('primary connection profile is required');
@@ -32,9 +31,9 @@ export async function createDb({ primary, balanced, bundle, credentialProvider,
32
31
  async execute(sql, values, options) { return choose(sql, options).execute(sql, values); },
33
32
  async transaction(callback) { const node = primaryPool.choose(); const connection = await node.getConnection(); try { await connection.beginTransaction(); const tx = { query: (sql, values) => connection.query(sql, values), execute: (sql, values) => connection.execute(sql, values) }; const result = await callback(tx); await connection.commit(); return result; } catch (error) { await connection.rollback().catch(() => {}); throw asSqlError(error); } finally { connection.release(); } },
34
33
  async health(route = 'primary') { const started = now(); const selected = route === 'balanced' && balancedPool ? balancedPool : primaryPool; const nodes = await selected.health(); return { ok: nodes.some((node) => node.ok), route: selected === balancedPool ? 'balanced' : 'primary', nodes, latencyMs: now() - started }; },
35
- async refresh(nextBundle) { const candidate = validateBundle(nextBundle); if (bundleExpired(candidate, now())) throw new Error('routing bundle is expired'); if (olderVersion(candidate.bundleVersion, activeBundle?.bundleVersion)) return { bundleVersion: activeBundle?.bundleVersion, refreshRequired: bundleNeedsRefresh(activeBundle, now()) }; const previous = [primaryPool, balancedPool]; const credentials = candidate.credentials ?? { username: primaryConfig.user, password: primaryConfig.password }; primaryConfig = validateProfile({ ...primaryConfig, host: candidate.routes.primary[0]?.host, port: candidate.routes.primary[0]?.port, user: credentials.username, password: credentials.password, database: candidate.database }, 'primary'); balancedConfig = candidate.routes.balanced?.[0] ? validateProfile({ ...primaryConfig, host: candidate.routes.balanced[0].host, port: candidate.routes.balanced[0].port }, 'balanced') : undefined; activeBundle = candidate; primaryPool = makeRoute('primary', primaryConfig); balancedPool = balancedConfig ? makeRoute('balanced', balancedConfig) : null; await Promise.all(previous.filter(Boolean).map((pool) => pool.close())); return { bundleVersion: activeBundle.bundleVersion ?? null, refreshRequired: bundleNeedsRefresh(activeBundle, now()) }; },
36
- async attachRoutingStream(stream) { if (!stream?.connect) throw new TypeError('routing stream is required'); stream.setOnUpdate?.(async (event) => { const update = event.type === 'routing.update' ? event : event.type === 'routing.resync' ? event.bundle : undefined; if (update?.routes?.primary?.length) await client.refresh({ ...activeBundle, database: update.database ?? activeBundle?.database ?? primaryConfig.database, credentials: update.credentials ?? activeBundle?.credentials, routes: update.routes, bundleVersion: update.bundleVersion ?? update.version ?? activeBundle?.bundleVersion, expiresAt: update.expiresAt ?? activeBundle?.expiresAt ?? new Date(now() + 60000).toISOString() }); if (event.type === 'routing.drain' || event.type === 'routing.recovery') for (const pool of [primaryPool, balancedPool].filter(Boolean)) (event.type === 'routing.drain' ? pool.drain : pool.recover)(event.node, drainTimeoutMs); }); await stream.connect(); return () => stream.close?.(); },
37
- drain(host, timeoutMs = drainTimeoutMs) { const pools = [primaryPool, balancedPool].filter(Boolean); pools.forEach((pool) => pool.drain(host, timeoutMs)); return { host, timeoutMs, wait: () => Promise.all(pools.map((pool) => pool.waitForIdle(timeoutMs))), forceClose: () => Promise.all(pools.map((pool) => pool.forceClose(host))) }; },
34
+ async refresh(nextBundle) { const candidate = validateBundle(nextBundle); if (bundleExpired(candidate, now())) throw new Error('routing bundle is expired'); if (olderVersion(candidate.bundleVersion, activeBundle?.bundleVersion)) return { bundleVersion: activeBundle?.bundleVersion, refreshRequired: bundleNeedsRefresh(activeBundle, now()) }; const previous = [primaryPool, balancedPool]; const credentials = candidate.credentials ?? { username: primaryConfig.user, password: primaryConfig.password }; const writer = candidate.writer ?? candidate.routes.primary?.[0]; const reader = candidate.readers?.[0] ?? candidate.routes.balanced?.[0]; primaryConfig = validateProfile({ ...primaryConfig, host: writer?.host, port: writer?.port, user: credentials.username, password: credentials.password, database: candidate.database }, 'primary'); balancedConfig = reader ? validateProfile({ ...primaryConfig, host: reader.host, port: reader.port }, 'balanced') : undefined; activeBundle = candidate; primaryPool = makeRoute('primary', primaryConfig); balancedPool = balancedConfig ? makeRoute('balanced', balancedConfig) : null; await Promise.all(previous.filter(Boolean).map((pool) => pool.close())); return { bundleVersion: activeBundle.bundleVersion ?? null, refreshRequired: bundleNeedsRefresh(activeBundle, now()) }; },
35
+ async attachRoutingStream(stream) { if (!stream?.connect) throw new TypeError('routing stream is required'); stream.setOnUpdate?.(async (event) => { const update = event.type === 'routing.update' ? event : event.type === 'routing.resync' ? event.bundle : undefined; if (update && (update.writer || update.routes?.primary?.length)) await client.refresh({ ...activeBundle, ...update, database: update.database ?? activeBundle?.database ?? primaryConfig.database, credentials: update.credentials ?? activeBundle?.credentials, routes: update.routes ?? activeBundle?.routes, bundleVersion: update.bundleVersion ?? update.version ?? activeBundle?.bundleVersion, expiresAt: update.expiresAt ?? activeBundle?.expiresAt ?? new Date(now() + 60000).toISOString() }); if (event.type === 'routing.drain' || event.type === 'routing.recovery') for (const pool of [primaryPool, balancedPool].filter(Boolean)) (event.type === 'routing.drain' ? pool.drain : pool.recover)(event.node, drainTimeoutMs); }); await stream.connect(); return () => stream.close?.(); },
36
+ drain(host, timeoutMs = drainTimeoutMs) { const effectiveTimeout = clientDrainTimeout(timeoutMs); const pools = [primaryPool, balancedPool].filter(Boolean); pools.forEach((pool) => pool.drain(host, effectiveTimeout)); return { host, timeoutMs: effectiveTimeout, wait: () => Promise.all(pools.map((pool) => pool.waitForIdle(effectiveTimeout))), forceClose: () => Promise.all(pools.map((pool) => pool.forceClose(host))) }; },
38
37
  nodeStates() { return [primaryPool, balancedPool].filter(Boolean).flatMap((pool) => pool.nodes.map((node) => ({ host: node.host, port: node.port, route: pool === primaryPool ? 'primary' : 'balanced', state: node.state, active: node.active, available: node.available }))); },
39
38
  setNodeAvailability(route, host, available) { const pool = route === 'balanced' ? balancedPool : primaryPool; pool?.setAvailability(host, available); },
40
39
  bundle: () => activeBundle,
@@ -1,10 +1,6 @@
1
1
  import { bundleExpired } from '../bundle.mjs';
2
2
  import { createNodePool, createRoutePool } from '../pools.mjs';
3
-
4
- const bundleProfiles = (bundle, route, base) => {
5
- const routes = bundle.routes?.[route] ?? [];
6
- return routes.map((node) => ({ ...base, host: node.host, port: node.port, weight: node.weight }));
7
- };
3
+ import { bundleProfiles } from './bundle-profiles.mjs';
8
4
 
9
5
  const routeProfiles = (bundle, route, fallback, now) => {
10
6
  if (!bundle) return [fallback];
@@ -14,5 +10,5 @@ const routeProfiles = (bundle, route, fallback, now) => {
14
10
  };
15
11
 
16
12
  export function createRouteFactory({ bundle, now, mysqlLib, log, quarantineMs }) {
17
- return (route, fallback) => createRoutePool(routeProfiles(bundle, route, fallback, now).map((profile) => createNodePool({ profile, mysqlLib, log, now, quarantineMs })));
13
+ return (route, fallback) => createRoutePool(routeProfiles(bundle, route, fallback, now).map((profile) => createNodePool({ profile, mysqlLib, log, now, quarantineMs })), { preferred: route === 'primary' });
18
14
  }
package/src/index.d.ts CHANGED
@@ -10,6 +10,7 @@ export interface ConnectionProfile {
10
10
  }
11
11
 
12
12
  export interface RoutingNode { host: string; port: number | string; weight?: number; }
13
+ export interface WriterAssignment { host: string; port: number | string; }
13
14
  export interface RoutingBundle {
14
15
  apiVersion?: string;
15
16
  database?: string;
@@ -19,6 +20,9 @@ export interface RoutingBundle {
19
20
  expiresAt: string;
20
21
  refreshAfter?: string;
21
22
  routes: { primary?: RoutingNode[]; balanced?: RoutingNode[] };
23
+ writer?: WriterAssignment;
24
+ failover?: WriterAssignment[];
25
+ readers?: WriterAssignment[];
22
26
  }
23
27
  export interface CredentialProviderResult { user: string; password: string; }
24
28
  export type CredentialProvider = (context: { database: string; identity: string | null; route: string }) => Promise<CredentialProviderResult> | CredentialProviderResult;
@@ -45,7 +49,7 @@ export interface RoutingStream {
45
49
  connect(): Promise<void>;
46
50
  setOnUpdate(handler: (event: unknown) => void | Promise<void>): void;
47
51
  close(): void;
48
- state(): { connected: boolean; mode: 'websocket' | 'rest' | 'disconnected'; expectedVersion: number };
52
+ state(): { connected: boolean; mode: 'websocket' | 'rest' | 'disconnected'; expectedVersion: number | string };
49
53
  }
50
54
 
51
55
  export function createDb(options: { primary: ConnectionProfile; balanced?: Partial<ConnectionProfile>; bundle?: RoutingBundle; credentialProvider?: CredentialProvider; identity?: string; mysqlLib?: unknown; log?: unknown; routing?: 'auto' | 'primary' | 'balanced'; quarantineMs?: number; drainTimeoutMs?: number; now?: () => number }): Promise<DbClient>;
@@ -64,6 +68,11 @@ export function createAdminSql(options: { query: QueryFunction }): { transaction
64
68
  export function createMigrationRunner(options: { query: QueryFunction; migrations?: Array<{ version: number; name: string; statements: string[] }> }): { status(): Promise<{ applied: unknown[] }>; migrate(): Promise<unknown> };
65
69
  export function selectRouteNodes(options: { bundle: RoutingBundle; route?: 'primary' | 'balanced'; now?: number }): RoutingNode[];
66
70
  export function createRoutingStream(options: { endpoint: string; token?: string; application?: string; fetchBundle: (application: string) => Promise<RoutingBundle>; onUpdate?: (event: unknown) => void; onError?: (error: unknown) => void; reconnectMs?: number; maxReconnectMs?: number; heartbeatMs?: number }): RoutingStream;
71
+ export function writerAssignment(bundle: RoutingBundle): WriterAssignment;
72
+ export function failoverNodes(bundle: RoutingBundle): WriterAssignment[];
73
+ export function compareBundleVersions(left: number | string | undefined, right: number | string | undefined): number;
74
+ export const CLIENT_DRAIN_TIMEOUT_MS: 45000;
75
+ export function clientDrainTimeout(timeoutMs?: number): number;
67
76
  export function createQuiesceController(options?: { close?: () => Promise<void>; onChange?: (state: string) => void }): { state(): { state: string; active: number }; enter(): () => void; begin(): Promise<void>; end(): Promise<void>; close(): Promise<void> };
68
77
  export function createSqlVerifier(options: { query: QueryFunction }): { connectivity(): Promise<{ verified: boolean }>; schema(database: string): Promise<{ database: string; verified: boolean }>; account(user: string, host?: string): Promise<{ user: string; host: string; verified: boolean; grants: string[] }>; all(options?: { database?: string; user?: string; host?: string }): Promise<unknown> };
69
78
  export function createMaterializer(options?: Record<string, unknown>): unknown;
package/src/index.mjs CHANGED
@@ -9,6 +9,9 @@ export { createAdminSql } from './admin/sql.mjs';
9
9
  export { createMigrationRunner } from './admin/migrations.mjs';
10
10
  export { selectRouteNodes } from './routing/node-set.mjs';
11
11
  export { createRoutingStream } from './routing/stream-client.mjs';
12
+ export { writerAssignment, failoverNodes } from './routing/assignment.mjs';
13
+ export { compareBundleVersions } from './routing/bundle-version.mjs';
14
+ export { CLIENT_DRAIN_TIMEOUT_MS, clientDrainTimeout } from './lifecycle/drain-policy.mjs';
12
15
  export { createQuiesceController } from './lifecycle/quiesce.mjs';
13
16
  export { createSqlVerifier } from './verification/sql.mjs';
14
17
  export { createMaterializer } from './lifecycle/materializer.mjs';
@@ -0,0 +1,7 @@
1
+ export const CLIENT_DRAIN_TIMEOUT_MS = 45000;
2
+
3
+ export function clientDrainTimeout(timeoutMs = CLIENT_DRAIN_TIMEOUT_MS) {
4
+ const requested = Number(timeoutMs);
5
+ if (!Number.isFinite(requested) || requested < 0) throw new TypeError('drain timeout must be a non-negative number');
6
+ return Math.min(requested, CLIENT_DRAIN_TIMEOUT_MS);
7
+ }
@@ -1,9 +1,10 @@
1
- export function createRoutePool(nodes) {
1
+ export function createRoutePool(nodes, { preferred = false } = {}) {
2
2
  let cursor = 0;
3
3
  const candidates = () => nodes.filter((node) => node.available);
4
4
  const choose = () => {
5
5
  const available = candidates();
6
6
  if (!available.length) throw new Error('no eligible SQL nodes available');
7
+ if (preferred) return available[0];
7
8
  const total = available.reduce((sum, node) => sum + Math.max(0, node.weight), 0);
8
9
  if (!total) return available[cursor++ % available.length];
9
10
  let target = cursor++ % total;
@@ -0,0 +1,9 @@
1
+ export function writerAssignment(bundle) {
2
+ if (!bundle?.writer?.host) throw new TypeError('routing bundle writer is required');
3
+ return { host: bundle.writer.host, port: Number(bundle.writer.port ?? 3306) };
4
+ }
5
+
6
+ export function failoverNodes(bundle) {
7
+ if (!Array.isArray(bundle?.failover)) throw new TypeError('routing bundle failover is required');
8
+ return bundle.failover.map(({ host, port = 3306 }) => ({ host, port: Number(port) }));
9
+ }
@@ -0,0 +1,11 @@
1
+ export function compareBundleVersions(left, right) {
2
+ if (left === undefined || right === undefined) return 0;
3
+ const a = String(left).match(/\d+/g)?.map(Number);
4
+ const b = String(right).match(/\d+/g)?.map(Number);
5
+ if (!a || !b) return String(left).localeCompare(String(right));
6
+ for (let index = 0; index < Math.max(a.length, b.length); index += 1) {
7
+ const difference = (a[index] ?? 0) - (b[index] ?? 0);
8
+ if (difference) return Math.sign(difference);
9
+ }
10
+ return 0;
11
+ }
@@ -0,0 +1,14 @@
1
+ export function validateRoutingNode(node, name = 'routing node') {
2
+ if (!node || typeof node !== 'object' || typeof node.host !== 'string' || node.host.trim() === '') throw new TypeError(`${name} host is required`);
3
+ const port = Number(node.port ?? 3306);
4
+ if (!Number.isInteger(port) || port < 1 || port > 65535) throw new TypeError(`${name} port is invalid`);
5
+ if (node.weight !== undefined && (!Number.isFinite(Number(node.weight)) || Number(node.weight) < 0)) throw new TypeError(`${name} weight is invalid`);
6
+ return { ...node, host: node.host.trim(), port };
7
+ }
8
+
9
+ export function validateRoutingNodes(nodes, name) {
10
+ if (!Array.isArray(nodes)) throw new TypeError(`${name} must be an array`);
11
+ const validated = nodes.map((node, index) => validateRoutingNode(node, `${name}[${index}]`));
12
+ if (new Set(validated.map(({ host, port }) => `${host}:${port}`)).size !== validated.length) throw new TypeError(`${name} contains duplicate nodes`);
13
+ return validated;
14
+ }
@@ -1,4 +1,5 @@
1
1
  import { log as defaultLog } from '@eliware/common';
2
+ import { compareBundleVersions } from './bundle-version.mjs';
2
3
 
3
4
  export function createRoutingStream({ endpoint, token, application = 'default', fetchBundle, WebSocketImpl = globalThis.WebSocket, onUpdate, onError, reconnectMs = 1000, maxReconnectMs = 30000, heartbeatMs = 45000, now = () => Date.now() } = {}) {
4
5
  if (!endpoint || typeof fetchBundle !== 'function') throw new TypeError('endpoint and fetchBundle are required');
@@ -14,10 +15,12 @@ export function createRoutingStream({ endpoint, token, application = 'default',
14
15
  socket.onopen = () => { mode = 'websocket'; delay = reconnectMs; heartbeat = setInterval(() => socket?.send?.(JSON.stringify({ type: 'heartbeat', sentAt: now() })), heartbeatMs); };
15
16
  socket.onmessage = async ({ data }) => {
16
17
  try {
17
- const event = JSON.parse(data); const version = Number(event.version ?? 0);
18
- if (version && expectedVersion && version <= expectedVersion) return;
19
- if (expectedVersion && version > expectedVersion + 1) await fallback();
20
- expectedVersion = Math.max(expectedVersion, version); updateHandler?.(event);
18
+ const event = JSON.parse(data); const version = event.version;
19
+ if (version !== undefined && expectedVersion !== 0 && compareBundleVersions(version, expectedVersion) <= 0) return;
20
+ const numericVersion = Number(version); const numericExpected = Number(expectedVersion);
21
+ if (Number.isInteger(numericVersion) && Number.isInteger(numericExpected) && numericExpected > 0 && numericVersion > numericExpected + 1) await fallback();
22
+ if (version !== undefined && (expectedVersion === 0 || compareBundleVersions(version, expectedVersion) > 0)) expectedVersion = version;
23
+ updateHandler?.(event);
21
24
  } catch (error) { onError?.(error); }
22
25
  };
23
26
  socket.onerror = (error) => { onError?.(error); };