@naisys/hub 3.0.0-beta.32 → 3.0.0-beta.33

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.
@@ -3,7 +3,7 @@
3
3
  * Each connected NAISYS instance gets its own NaisysConnection instance.
4
4
  */
5
5
  export function createNaisysConnection(socket, connectionInfo, raiseEvent, logService) {
6
- const { hostId, hostName, connectedAt, hostType, clientVersion } = connectionInfo;
6
+ const { hostId, hostName, connectedAt, instanceId, processStartedAt, hostType, clientVersion, } = connectionInfo;
7
7
  logService.log(`[Hub:Connection] NAISYS instance connected: ${hostName} (${hostId})`);
8
8
  // Forward all socket events to hub's emit function
9
9
  // Note: Socket.IO passes (eventName, ...args) where last arg may be an ack callback
@@ -38,6 +38,8 @@ export function createNaisysConnection(socket, connectionInfo, raiseEvent, logSe
38
38
  getHostId: () => hostId,
39
39
  getHostName: () => hostName,
40
40
  getConnectedAt: () => connectedAt,
41
+ getInstanceId: () => instanceId,
42
+ getProcessStartedAt: () => processStartedAt,
41
43
  getSocketId: () => socket.id,
42
44
  getHostType: () => hostType,
43
45
  getClientVersion: () => clientVersion,
@@ -58,16 +58,21 @@ export function createNaisysServer(nsp, initialHubAccessKey, logService, hostReg
58
58
  connection.sendMessage(event, payload, ack);
59
59
  return true;
60
60
  }
61
+ function createConnectError(message, code, fatal = true) {
62
+ const error = new Error(message);
63
+ error.data = { code, fatal };
64
+ return error;
65
+ }
61
66
  // Authentication middleware
62
67
  nsp.use(async (socket, next) => {
63
- const { hubAccessKey: clientAccessKey, hostName, machineId: rawMachineId, hostType: rawHostType, clientVersion, environment: rawEnvironment, } = socket.handshake.auth;
68
+ const { hubAccessKey: clientAccessKey, hostName, machineId: rawMachineId, instanceId: rawInstanceId, startedAt: rawStartedAt, hostType: rawHostType, clientVersion, environment: rawEnvironment, } = socket.handshake.auth;
64
69
  if (!clientAccessKey || clientAccessKey !== hubAccessKey) {
65
70
  logService.log(`[Hub] Connection rejected: invalid access key from ${socket.handshake.address}`);
66
- return next(new Error("Invalid access key"));
71
+ return next(createConnectError("Invalid access key", "invalid_access_key"));
67
72
  }
68
73
  if (!hostName) {
69
74
  logService.log(`[Hub] Connection rejected: missing hostName`);
70
- return next(new Error("Missing hostName"));
75
+ return next(createConnectError("Missing hostName", "missing_host_name"));
71
76
  }
72
77
  try {
73
78
  const hostType = (typeof rawHostType === "string" ? rawHostType : "naisys");
@@ -75,6 +80,12 @@ export function createNaisysServer(nsp, initialHubAccessKey, logService, hostReg
75
80
  const machineId = typeof rawMachineId === "string" && rawMachineId
76
81
  ? rawMachineId
77
82
  : undefined;
83
+ const instanceId = typeof rawInstanceId === "string" && rawInstanceId
84
+ ? rawInstanceId
85
+ : socket.id;
86
+ const processStartedAt = typeof rawStartedAt === "number" && Number.isFinite(rawStartedAt)
87
+ ? rawStartedAt
88
+ : Date.now();
78
89
  const environment = rawEnvironment && typeof rawEnvironment === "object"
79
90
  ? rawEnvironment
80
91
  : undefined;
@@ -87,25 +98,37 @@ export function createNaisysServer(nsp, initialHubAccessKey, logService, hostReg
87
98
  if (hostType === "naisys") {
88
99
  const existing = naisysConnections.get(result.hostId);
89
100
  if (existing) {
90
- logService.log(`[Hub] Superseding existing connection for host '${result.hostName}' — disconnecting old socket`);
91
- existing.disconnect();
101
+ if (existing.getInstanceId() === instanceId) {
102
+ logService.log(`[Hub] Replacing existing socket for host '${result.hostName}' from the same process instance`);
103
+ existing.disconnect();
104
+ }
105
+ else if (processStartedAt >= existing.getProcessStartedAt()) {
106
+ logService.log(`[Hub] Superseding existing connection for host '${result.hostName}' — newer process instance connected`);
107
+ existing.disconnect();
108
+ }
109
+ else {
110
+ logService.log(`[Hub] Connection rejected: older process attempted to reclaim host '${result.hostName}'`);
111
+ return next(createConnectError("Superseded by newer NAISYS instance", "superseded_by_newer_instance"));
112
+ }
92
113
  }
93
114
  }
94
115
  socket.data.hostId = result.hostId;
95
116
  socket.data.hostName = result.hostName;
96
117
  socket.data.machineId = result.machineId;
118
+ socket.data.instanceId = instanceId;
119
+ socket.data.processStartedAt = processStartedAt;
97
120
  socket.data.hostType = hostType;
98
121
  socket.data.clientVersion = resolvedVersion;
99
122
  next();
100
123
  }
101
124
  catch (err) {
102
125
  logService.error(`[Hub] Connection rejected: failed to register host ${hostName}: ${err}`);
103
- return next(new Error("NAISYS instance registration failed"));
126
+ return next(createConnectError("NAISYS instance registration failed", "registration_failed"));
104
127
  }
105
128
  });
106
129
  // Handle new connections
107
130
  nsp.on("connection", (socket) => {
108
- const { hostId, hostName, machineId, hostType, clientVersion } = socket.data;
131
+ const { hostId, hostName, machineId, instanceId, processStartedAt, hostType, clientVersion, } = socket.data;
109
132
  // Send the client its assigned machineId and authoritative hostname
110
133
  const registered = { machineId, hostName };
111
134
  socket.emit(HubEvents.HOST_REGISTERED, registered);
@@ -114,6 +137,8 @@ export function createNaisysServer(nsp, initialHubAccessKey, logService, hostReg
114
137
  hostId,
115
138
  hostName,
116
139
  connectedAt: new Date(),
140
+ instanceId,
141
+ processStartedAt,
117
142
  hostType,
118
143
  clientVersion,
119
144
  }, raiseEvent, logService);
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@naisys/hub",
3
- "version": "3.0.0-beta.32",
3
+ "version": "3.0.0-beta.33",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@naisys/hub",
9
- "version": "3.0.0-beta.32",
9
+ "version": "3.0.0-beta.33",
10
10
  "dependencies": {
11
- "@naisys/common": "3.0.0-beta.32",
12
- "@naisys/common-node": "3.0.0-beta.32",
13
- "@naisys/hub-database": "3.0.0-beta.32",
14
- "@naisys/hub-protocol": "3.0.0-beta.32",
11
+ "@naisys/common": "3.0.0-beta.33",
12
+ "@naisys/common-node": "3.0.0-beta.33",
13
+ "@naisys/hub-database": "3.0.0-beta.33",
14
+ "@naisys/hub-protocol": "3.0.0-beta.33",
15
15
  "commander": "^14.0.3",
16
16
  "dotenv": "^17.3.1",
17
17
  "fastify": "^5.8.2",
@@ -24,7 +24,7 @@
24
24
  "node": ">=22.0.0"
25
25
  },
26
26
  "peerDependencies": {
27
- "@naisys/supervisor": "3.0.0-beta.32"
27
+ "@naisys/supervisor": "3.0.0-beta.33"
28
28
  },
29
29
  "peerDependenciesMeta": {
30
30
  "@naisys/supervisor": {
@@ -189,32 +189,32 @@
189
189
  "license": "MIT"
190
190
  },
191
191
  "node_modules/@naisys/common": {
192
- "version": "3.0.0-beta.32",
193
- "resolved": "https://registry.npmjs.org/@naisys/common/-/common-3.0.0-beta.32.tgz",
194
- "integrity": "sha512-f9guURs/iCeATRFP7E6rzg4f4Faw4cSoO/JtE54AFWlOeZVDgDC16lK1PlyoSJ5t5PtIY+a+Cu/5TaZrmDYNzg==",
192
+ "version": "3.0.0-beta.33",
193
+ "resolved": "https://registry.npmjs.org/@naisys/common/-/common-3.0.0-beta.33.tgz",
194
+ "integrity": "sha512-htoTTNNQkcW276Z+ca6uVxlmF6nAoxlTVCCGpC4oEmAftxIu8Uf5s0IHJ2Pc8gXZTrW28/85Ec8P0d+nsvzblg==",
195
195
  "dependencies": {
196
196
  "semver": "^7.7.4",
197
197
  "zod": "^4.3.6"
198
198
  }
199
199
  },
200
200
  "node_modules/@naisys/common-node": {
201
- "version": "3.0.0-beta.32",
202
- "resolved": "https://registry.npmjs.org/@naisys/common-node/-/common-node-3.0.0-beta.32.tgz",
203
- "integrity": "sha512-gdGe1IiZ4c+lc50MImIFhfYhvEdV/DJ5MBRoINmOAfk0SuzyVQeHEDlCPFWhRnZd82J2OMcf/qIw55RbCAvsIw==",
201
+ "version": "3.0.0-beta.33",
202
+ "resolved": "https://registry.npmjs.org/@naisys/common-node/-/common-node-3.0.0-beta.33.tgz",
203
+ "integrity": "sha512-U5M+rqs9Ap44Bs+eZ5vNmpJKRYkM0ca3Lq+aWP5goXHXTuRg0XCGFCc5e6/ugwzWOf2XJkBHnkgYIn1DmeP2qg==",
204
204
  "dependencies": {
205
- "@naisys/common": "3.0.0-beta.32",
205
+ "@naisys/common": "3.0.0-beta.33",
206
206
  "better-sqlite3": "^12.6.2",
207
207
  "js-yaml": "^4.1.1",
208
208
  "pino": "^10.3.1"
209
209
  }
210
210
  },
211
211
  "node_modules/@naisys/hub-database": {
212
- "version": "3.0.0-beta.32",
213
- "resolved": "https://registry.npmjs.org/@naisys/hub-database/-/hub-database-3.0.0-beta.32.tgz",
214
- "integrity": "sha512-YVTLL8n0tWRZcSxNxQCvNejYST6EBl9Uoj/H0AUH0+zc7YPVrsBxJR5DPU1lMGQ9siN/fCUkXl1n9oyI4Gz56g==",
212
+ "version": "3.0.0-beta.33",
213
+ "resolved": "https://registry.npmjs.org/@naisys/hub-database/-/hub-database-3.0.0-beta.33.tgz",
214
+ "integrity": "sha512-dWr4pVodZhB5c55CksVEDPtxNkLddryNnYs6kvf6vg9v0coMQRemprQb3z4bUDjMSHXBsrVForefnVvct2N7qQ==",
215
215
  "dependencies": {
216
- "@naisys/common": "3.0.0-beta.32",
217
- "@naisys/common-node": "3.0.0-beta.32",
216
+ "@naisys/common": "3.0.0-beta.33",
217
+ "@naisys/common-node": "3.0.0-beta.33",
218
218
  "@prisma/adapter-better-sqlite3": "^7.5.0",
219
219
  "@prisma/client": "^7.5.0",
220
220
  "better-sqlite3": "^12.6.2",
@@ -222,11 +222,11 @@
222
222
  }
223
223
  },
224
224
  "node_modules/@naisys/hub-protocol": {
225
- "version": "3.0.0-beta.32",
226
- "resolved": "https://registry.npmjs.org/@naisys/hub-protocol/-/hub-protocol-3.0.0-beta.32.tgz",
227
- "integrity": "sha512-eMXjiifY/DSXIlVLbDEmqddwryTl68ednV3wTqiRzgCVWQpsDHdBLWxR5hb0SNTagqk513qELeNvRIz4sE5Gow==",
225
+ "version": "3.0.0-beta.33",
226
+ "resolved": "https://registry.npmjs.org/@naisys/hub-protocol/-/hub-protocol-3.0.0-beta.33.tgz",
227
+ "integrity": "sha512-vfGw/pe+Zb8lfAmBH/eB3CO2J4NwdMMkD1b8COHxy1ckWZbiUwaV1xbafP8P1qRRdj48BniYMg9sqrCNP5GsNw==",
228
228
  "dependencies": {
229
- "@naisys/common": "3.0.0-beta.32",
229
+ "@naisys/common": "3.0.0-beta.33",
230
230
  "zod": "^4.3.6"
231
231
  }
232
232
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naisys/hub",
3
- "version": "3.0.0-beta.32",
3
+ "version": "3.0.0-beta.33",
4
4
  "description": "NAISYS Hub - Adds persistence and multi-instance coordination to NAISYS",
5
5
  "type": "module",
6
6
  "main": "dist/naisysHub.js",
@@ -31,7 +31,7 @@
31
31
  "!dist/**/*.d.ts.map"
32
32
  ],
33
33
  "peerDependencies": {
34
- "@naisys/supervisor": "3.0.0-beta.32"
34
+ "@naisys/supervisor": "3.0.0-beta.33"
35
35
  },
36
36
  "peerDependenciesMeta": {
37
37
  "@naisys/supervisor": {
@@ -39,10 +39,10 @@
39
39
  }
40
40
  },
41
41
  "dependencies": {
42
- "@naisys/common": "3.0.0-beta.32",
43
- "@naisys/common-node": "3.0.0-beta.32",
44
- "@naisys/hub-database": "3.0.0-beta.32",
45
- "@naisys/hub-protocol": "3.0.0-beta.32",
42
+ "@naisys/common": "3.0.0-beta.33",
43
+ "@naisys/common-node": "3.0.0-beta.33",
44
+ "@naisys/hub-database": "3.0.0-beta.33",
45
+ "@naisys/hub-protocol": "3.0.0-beta.33",
46
46
  "commander": "^14.0.3",
47
47
  "dotenv": "^17.3.1",
48
48
  "fastify": "^5.8.2",