Skip to content

Commit 32be4db

Browse files
feat(cli): add --ws-port option (#719)
1 parent dacf8ad commit 32be4db

14 files changed

Lines changed: 71 additions & 7 deletions

File tree

.changeset/clean-melons-shine.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nodesecure/server": minor
3+
---
4+
5+
Add buildServer wsPort option

bin/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ prog
7373
.command("open [json]")
7474
.describe(i18n.getTokenSync("cli.commands.open.desc"))
7575
.option("-p, --port", i18n.getTokenSync("cli.commands.open.option_port"), process.env.PORT)
76-
.option("-f, --fresh-start", i18n.getTokenSync("cli.commands.open.option_fresh_start"), process.env.PORT)
76+
.option("--ws-port", i18n.getTokenSync("cli.commands.open.option_ws_port"), process.env.WS_PORT)
77+
.option("-f, --fresh-start", i18n.getTokenSync("cli.commands.open.option_fresh_start"), false)
7778
.option("-d, --developer", i18n.getTokenSync("cli.commands.open.option_developer"), false)
7879
.action(commands.http.start);
7980

docs/cli/open.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ $ nsecure open [json]
1717
| Name | Shortcut | Default Value | Description |
1818
|---|---|---|---|
1919
| `--port` | `-p` | `process.env.PORT` | Specify the port on which the HTTP server should run. |
20+
| `--ws-port` | N/A | `process.env.WS_PORT` | Specify the port on which the WebSocket server should run. |
2021
| `--fresh-start` | `-f` | `false` | Open the UI with no initial package. Also, the app will use a dedicated cache. |
2122
| `--developer` | `-d` | `false` | Launch the server in developer mode, enabling automatic HTML component refresh. |

i18n/english.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const cli = {
4343
open: {
4444
desc: "Run an HTTP Server with a given nsecure JSON file",
4545
option_port: "Define the running port",
46+
option_ws_port: "Define the WebSocket server port",
4647
option_fresh_start: "Launch the server from scratch, ignoring any existing payload file",
4748
option_developer: "Launch the server in developer mode, enabling automatic HTML component refresh"
4849
},

i18n/french.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const cli = {
4343
open: {
4444
desc: "Démarre un serveur HTTP avec un fichier .json nsecure donné",
4545
option_port: "Port à utiliser",
46+
option_ws_port: "Port du serveur WebSocket",
4647
option_fresh_start: "Lance le serveur à partir de zéro, en ignorant tout fichier de payload existant",
4748
option_developer: "Lance le serveur en mode développeur, permettant le rafraîchissement automatique des composants HTML"
4849
},

public/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ document.addEventListener("DOMContentLoaded", async() => {
160160
);
161161
onSettingsSaved(window.settings.config);
162162

163-
const socket = new WebSocketClient(`ws://${window.location.hostname}:1338`);
163+
const socket = new WebSocketClient(`ws://${window.location.hostname}:${window.__WS_PORT__}`);
164164
socket.addEventListener("PAYLOAD", onSocketPayload);
165165
socket.addEventListener("INIT", onSocketInitOrReload);
166166
socket.addEventListener("RELOAD", onSocketInitOrReload);

src/commands/http.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export async function start(
2828
) {
2929
const port = Number(options.port);
3030
const httpPort = Number.isNaN(port) ? 0 : port;
31+
const wsPort = Number(options["ws-port"]) || void 0;
3132
const freshStart = Boolean(options.f);
3233
const enableDeveloperMode = Boolean(options.developer);
3334

@@ -59,6 +60,7 @@ export async function start(
5960
scanType: options.scanType,
6061
projectRootDir: kProjectRootDir,
6162
componentsDir: kComponentsDir,
63+
wsPort,
6264
i18n: {
6365
english,
6466
french
@@ -74,7 +76,8 @@ export async function start(
7476

7577
new WebSocketServerInstanciator({
7678
cache,
77-
logger
79+
logger,
80+
port: wsPort
7881
});
7982

8083
for (const eventName of ["SIGINT", "SIGTERM"]) {

views/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<link rel="stylesheet" href="public/main.css" />
1010
<link rel="stylesheet" href="workspaces/documentation-ui/src/css/main.css" />
1111
<link rel="stylesheet" href="node_modules/highlight.js/styles/github.css" />
12+
<script>window.__WS_PORT__ = Number("[[=z.wsPort]]");</script>
1213
<script src="https://cdn.socket.io/4.7.5/socket.io.min.js"></script>
1314
<script src="public/main.js" defer></script>
1415
<title>NodeSecure</title>

workspaces/server/src/ALS.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface AsyncStoreContext {
1919
french: NestedStringRecord;
2020
};
2121
viewBuilder: ViewBuilder;
22+
wsPort: number;
2223
reporter?: typeof report;
2324
}
2425

workspaces/server/src/ViewBuilder.class.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,14 @@ export class ViewBuilder {
7272
return HTMLStr;
7373
}
7474

75-
async render(): Promise<string> {
75+
async render(wsPort: number): Promise<string> {
7676
const i18nLangName = await i18n.getLocalLang();
7777

7878
const HTMLStr = await this.#build();
7979
const templateStr = zup(HTMLStr)({
8080
lang: i18n.getTokenSync("lang"),
8181
i18nLangName,
82+
wsPort,
8283
token: (tokenName: string) => i18n.getTokenSync(`ui.${tokenName}`)
8384
});
8485

0 commit comments

Comments
 (0)