nodio-cli 1.0.3 → 1.0.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodio-cli",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Nodio distributed storage network",
5
5
  "main": "src/server/index.js",
6
6
  "type": "commonjs",
package/src/node/index.js CHANGED
@@ -31,7 +31,7 @@ async function isPortFree(port) {
31
31
  server.close(() => resolve(true));
32
32
  });
33
33
 
34
- server.listen(port, '127.0.0.1');
34
+ server.listen({ port, exclusive: true });
35
35
  });
36
36
  }
37
37
 
@@ -46,13 +46,35 @@ async function findFreePort(startPort, endPort) {
46
46
  throw new Error(`no free port found in range ${startPort}-${endPort}`);
47
47
  }
48
48
 
49
+ function detectAdvertisedHost() {
50
+ const interfaces = os.networkInterfaces();
51
+ for (const addresses of Object.values(interfaces)) {
52
+ if (!Array.isArray(addresses)) {
53
+ continue;
54
+ }
55
+
56
+ for (const address of addresses) {
57
+ if (address.family === 'IPv4' && !address.internal) {
58
+ return address.address;
59
+ }
60
+ }
61
+ }
62
+
63
+ return '127.0.0.1';
64
+ }
65
+
66
+ function isLoopbackHost(host) {
67
+ const normalized = String(host || '').trim().toLowerCase();
68
+ return normalized === '127.0.0.1' || normalized === 'localhost' || normalized === '::1';
69
+ }
70
+
49
71
  program
50
72
  .name('nodio-node')
51
73
  .description('Nodio donor node CLI')
52
74
  .argument('[capacity]', 'optional capacity shorthand, e.g. 10gb')
53
75
  .option('--node-id <id>', 'unique node ID (optional; auto-assigned and persisted if omitted)')
54
76
  .option('--server <url>', 'central server URL', 'https://api.nodio.me')
55
- .option('--host <host>', 'host/IP exposed to network', '127.0.0.1')
77
+ .option('--host <host>', 'host/IP exposed to network (default: auto-detected LAN IP)', 'auto')
56
78
  .option('--port <port>', 'port to expose shard API (auto-picked when omitted)')
57
79
  .option('--storage-dir <path>', 'local shard storage directory (defaults to ~/.nodio-nodes/node-<port>)')
58
80
  .option('--capacity-gb <gb>', 'donated capacity in GB', '10')
@@ -70,6 +92,7 @@ program.action(async (capacityArg, options) => {
70
92
  const parsedCapacityArg = parseCapacityGb(capacityArg);
71
93
  const capacityGb = parsedCapacityArg ?? Number(options.capacityGb);
72
94
  const heartbeatMs = Number(options.heartbeatMs);
95
+ const advertisedHost = options.host === 'auto' ? detectAdvertisedHost() : options.host;
73
96
  const storageDir = options.storageDir
74
97
  ? path.resolve(options.storageDir)
75
98
  : path.join(os.homedir(), '.nodio-nodes', `node-${port}`);
@@ -87,10 +110,14 @@ program.action(async (capacityArg, options) => {
87
110
  throw new Error('heartbeat-ms must be greater than 0');
88
111
  }
89
112
 
113
+ if (isLoopbackHost(advertisedHost)) {
114
+ console.warn('[nodio-node] warning: loopback host is advertised; only this machine can reach this donor');
115
+ }
116
+
90
117
  const runtime = new NodioNodeRuntime({
91
118
  nodeId: options.nodeId,
92
119
  serverUrl: options.server,
93
- publicUrl: `http://${options.host}:${port}`,
120
+ publicUrl: `http://${advertisedHost}:${port}`,
94
121
  port,
95
122
  storageDir,
96
123
  capacityBytes: Math.floor(capacityGb * 1024 * 1024 * 1024),
@@ -85,11 +85,18 @@ class NodioNodeRuntime {
85
85
  res.json({ ok: true, nodeId: this.nodeId });
86
86
  });
87
87
 
88
- await new Promise((resolve) => {
89
- app.listen(this.port, () => {
88
+ await new Promise((resolve, reject) => {
89
+ const server = app.listen({ port: this.port, exclusive: true }, () => {
90
90
  console.log(`Nodio node ${this.nodeId} listening on port ${this.port}`);
91
91
  resolve();
92
92
  });
93
+
94
+ server.once('error', (error) => {
95
+ if (error && error.code === 'EADDRINUSE') {
96
+ return reject(new Error(`port ${this.port} is already in use`));
97
+ }
98
+ return reject(error);
99
+ });
93
100
  });
94
101
  }
95
102
 
@@ -58,23 +58,53 @@ function buildRoutes(config) {
58
58
  existingByNodeKey = await NodeModel.findOne({ nodeKey }).lean();
59
59
  }
60
60
 
61
+ if (existingByNodeKey && deviceKey && existingByNodeKey.deviceKey && existingByNodeKey.deviceKey !== deviceKey) {
62
+ return res.status(409).json({ error: 'nodeKey belongs to a different device' });
63
+ }
64
+
61
65
  if (existingByNodeKey && nodeId && nodeId !== existingByNodeKey.nodeId) {
62
66
  return res.status(409).json({ error: 'nodeKey is already associated with a different nodeId' });
63
67
  }
64
68
 
65
69
  let effectiveNodeId = nodeId || existingByNodeKey?.nodeId || null;
70
+ let claimedKnownNode = null;
71
+
72
+ if (effectiveNodeId && deviceKey) {
73
+ const existingByNodeId = await NodeModel.findOne({ nodeId: effectiveNodeId }).lean();
74
+ if (
75
+ existingByNodeId
76
+ && existingByNodeId.deviceKey
77
+ && existingByNodeId.deviceKey !== deviceKey
78
+ ) {
79
+ return res.status(409).json({ error: 'nodeId belongs to a different device' });
80
+ }
81
+ }
66
82
 
67
83
  if (!effectiveNodeId && deviceKey && normalizedKnownNodeIds.length > 0) {
68
- const oldestOfflineKnownNode = await NodeModel.findOne({
69
- deviceKey,
70
- status: 'offline',
71
- nodeId: { $in: normalizedKnownNodeIds }
72
- })
73
- .sort({ createdAt: 1 })
74
- .lean();
84
+ claimedKnownNode = await NodeModel.findOneAndUpdate(
85
+ {
86
+ deviceKey,
87
+ status: 'offline',
88
+ nodeId: { $in: normalizedKnownNodeIds }
89
+ },
90
+ {
91
+ $set: {
92
+ url,
93
+ capacityBytes: capacity,
94
+ freeBytes: free,
95
+ status: 'online',
96
+ lastHeartbeatAt: new Date(),
97
+ ...(nodeKey ? { nodeKey } : {})
98
+ }
99
+ },
100
+ {
101
+ new: true,
102
+ sort: { createdAt: 1 }
103
+ }
104
+ );
75
105
 
76
- if (oldestOfflineKnownNode) {
77
- effectiveNodeId = oldestOfflineKnownNode.nodeId;
106
+ if (claimedKnownNode) {
107
+ effectiveNodeId = claimedKnownNode.nodeId;
78
108
  }
79
109
  }
80
110
 
@@ -82,22 +112,23 @@ function buildRoutes(config) {
82
112
  effectiveNodeId = `donor-${uuidv4().slice(0, 8)}`;
83
113
  }
84
114
 
85
- const node = await NodeModel.findOneAndUpdate(
86
- { nodeId: effectiveNodeId },
87
- {
88
- $set: {
89
- nodeId: effectiveNodeId,
90
- ...(deviceKey ? { deviceKey } : {}),
91
- ...(nodeKey ? { nodeKey } : {}),
92
- url,
93
- capacityBytes: capacity,
94
- freeBytes: free,
95
- status: 'online',
96
- lastHeartbeatAt: new Date()
97
- }
98
- },
99
- { upsert: true, new: true, setDefaultsOnInsert: true }
100
- );
115
+ const node = claimedKnownNode
116
+ || await NodeModel.findOneAndUpdate(
117
+ { nodeId: effectiveNodeId },
118
+ {
119
+ $set: {
120
+ nodeId: effectiveNodeId,
121
+ ...(deviceKey ? { deviceKey } : {}),
122
+ ...(nodeKey ? { nodeKey } : {}),
123
+ url,
124
+ capacityBytes: capacity,
125
+ freeBytes: free,
126
+ status: 'online',
127
+ lastHeartbeatAt: new Date()
128
+ }
129
+ },
130
+ { upsert: true, new: true, setDefaultsOnInsert: true }
131
+ );
101
132
 
102
133
  res.json({
103
134
  nodeId: node.nodeId,