-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathrclnodejs-web.js
More file actions
executable file
·178 lines (162 loc) · 5.32 KB
/
Copy pathrclnodejs-web.js
File metadata and controls
executable file
·178 lines (162 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env node
// Copyright (c) 2026 RobotWebTools Contributors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// `rclnodejs-web` — the rclnodejs Web launcher.
//
// For frontend developers who don't want to write a Node.js server.
// Run `rclnodejs-web --help` or `npx rclnodejs-web web.json` to start
// the runtime.
import path from 'node:path';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';
import {
parseArgv,
loadConfigFile,
mergeConfig,
validateConfig,
HELP,
} from '../lib/runtime/cli-config.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const argv = process.argv.slice(2);
(async function main() {
let parsed;
try {
parsed = parseArgv(argv);
} catch (e) {
fail(e);
}
if (parsed.help) {
process.stdout.write(HELP);
process.exit(0);
}
if (parsed.version) {
const pkg = JSON.parse(
fs.readFileSync(path.join(__dirname, '..', 'package.json'), 'utf8')
);
process.stdout.write(`rclnodejs ${pkg.version}\n`);
process.exit(0);
}
let cfg;
try {
cfg = mergeConfig(loadConfigFile(parsed.configPath), parsed.partial);
// Re-validate the merged result: loadConfigFile only validates the
// JSON file, so values supplied purely via flags (e.g.
// `--http-sse-keep-alive foo` → NaN, or a non-numeric `--http-port`)
// would otherwise reach the transport unchecked.
validateConfig(cfg, 'options');
} catch (e) {
fail(e);
}
// Defer the rclnodejs import until after argv parsing so --help / --version
// never pay the (slow, native) load cost.
const rclnodejs = (await import('../index.js')).default;
const { createRuntime, WebSocketTransport, HttpTransport } =
await import('../lib/runtime/index.js');
// Track partial init so the catch block can clean up native handles before
// process.exit() — without this, a startup failure (e.g. EADDRINUSE on the
// WS port) leaves rclnodejs's native spin loop running and segfaults on exit.
let rclInitialized = false;
let runtime = null;
try {
await rclnodejs.init();
rclInitialized = true;
const node = rclnodejs.createNode(cfg.node);
rclnodejs.spin(node);
// Always start the WebSocket transport. Add HTTP only when the user
// configured an http.port (via --http-port or in the config file).
const transports = [
new WebSocketTransport({
port: cfg.port,
host: cfg.host,
path: cfg.path,
}),
];
const httpEnabled =
cfg.http && cfg.http.port !== null && cfg.http.port !== undefined;
if (httpEnabled) {
transports.push(
new HttpTransport({
port: cfg.http.port,
host: cfg.http.host || cfg.host,
basePath: cfg.http.basePath || cfg.path,
sse: cfg.http.sse,
sseKeepAliveMs:
cfg.http.sseKeepAliveMs != null
? cfg.http.sseKeepAliveMs
: undefined,
cors: cfg.http.cors,
})
);
}
runtime = createRuntime({ node, transports });
runtime.expose(cfg.expose);
await runtime.start();
// After start(), each transport reports the actual bound port
// (matters for `--port 0` / `--http-port 0` ephemeral modes).
const wsTransport = runtime.transports[0];
const httpTransport = httpEnabled ? runtime.transports[1] : null;
if (!parsed.quiet) {
const displayHost = (h) =>
['0.0.0.0', '::'].includes(h) ? 'localhost' : h;
const list = runtime.registry.list();
const totals =
Object.keys(list.call).length +
Object.keys(list.publish).length +
Object.keys(list.subscribe).length;
const noun = totals === 1 ? 'capability' : 'capabilities';
process.stdout.write(
`rclnodejs/web listening on ws://${displayHost(cfg.host)}:${wsTransport.port}${cfg.path} (${totals} ${noun})\n`
);
if (httpTransport) {
const httpHost = displayHost(cfg.http.host || cfg.host);
const httpBase = cfg.http.basePath || cfg.path;
const httpKinds = cfg.http.sse
? 'call/publish + subscribe (SSE)'
: 'call/publish only';
process.stdout.write(
` also http://${httpHost}:${httpTransport.port}${httpBase} (${httpKinds})\n`
);
}
}
const stop = async () => {
if (!parsed.quiet) process.stdout.write('\nstopping…\n');
await runtime.stop();
rclnodejs.shutdown();
process.exit(0);
};
process.once('SIGINT', stop);
process.once('SIGTERM', stop);
} catch (err) {
// Best-effort native cleanup so rclnodejs doesn't segfault on dirty exit.
if (runtime) {
try {
await runtime.stop();
} catch (_) {
/* ignore — we're already failing */
}
}
if (rclInitialized) {
try {
rclnodejs.shutdown();
} catch (_) {
/* ignore — we're already failing */
}
}
fail(err);
}
})();
function fail(err) {
if (err && err.cli) {
process.stderr.write(`error: ${err.message}\n\n${HELP}`);
process.exit(2);
}
process.stderr.write((err && err.stack) || String(err));
process.stderr.write('\n');
process.exit(1);
}