feat: supports the WS_PATH environment variable (#3384)

This commit is contained in:
chenos 2024-01-13 18:05:22 +08:00 committed by GitHub
parent c44f459756
commit 1adaa53c2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 16 additions and 10 deletions

View File

@ -106,7 +106,9 @@ module.exports = (cli) => {
env: {
PORT: clientPort,
APP_ROOT: `${APP_PACKAGE_ROOT}/client`,
WEBSOCKET_URL: process.env.WEBSOCKET_URL || (serverPort ? `ws://localhost:${serverPort}/ws` : undefined),
WEBSOCKET_URL:
process.env.WEBSOCKET_URL ||
(serverPort ? `ws://localhost:${serverPort}${process.env.WS_PATH}` : undefined),
PROXY_TARGET_URL:
process.env.PROXY_TARGET_URL || (serverPort ? `http://127.0.0.1:${serverPort}` : undefined),
},

View File

@ -271,6 +271,7 @@ exports.initEnv = function initEnv() {
LOCAL_STORAGE_DEST: 'storage/uploads',
PLUGIN_STORAGE_PATH: resolve(process.cwd(), 'storage/plugins'),
MFSU_AD: 'none',
WS_PATH: '/ws',
NODE_MODULES_PATH: resolve(process.cwd(), 'node_modules'),
PM2_HOME: resolve(process.cwd(), './storage/.pm2'),
PLUGIN_PACKAGE_PREFIX: '@nocobase/plugin-,@nocobase/plugin-sample-,@nocobase/preset-',

View File

@ -7,18 +7,20 @@ export const getWebSocketURL = () => {
}
const subApp = getSubAppName();
const queryString = subApp ? `?__appName=${subApp}` : '';
const wsPath = process.env.WS_PATH || '/ws';
if (process.env.WEBSOCKET_URL) {
const url = new URL(process.env.WEBSOCKET_URL);
if (url.hostname === 'localhost') {
return `${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.hostname}:${url.port}/ws${queryString}`;
const protocol = location.protocol === 'https:' ? 'wss' : 'ws';
return `${protocol}://${location.hostname}:${url.port}${wsPath}${queryString}`;
}
return `${process.env.WEBSOCKET_URL}${queryString}`;
}
try {
const url = new URL(process.env.API_BASE_URL);
return `${url.protocol === 'https:' ? 'wss' : 'ws'}://${url.host}/ws${queryString}`;
return `${url.protocol === 'https:' ? 'wss' : 'ws'}://${url.host}${wsPath}${queryString}`;
} catch (error) {
return `${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.host}/ws${queryString}`;
return `${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.host}${wsPath}${queryString}`;
}
};

View File

@ -37,6 +37,7 @@ function getUmiConfig() {
return memo;
}, {}),
define: {
'process.env.WS_PATH': process.env.WS_PATH,
'process.env.API_BASE_URL': API_BASE_URL || API_BASE_PATH,
'process.env.APP_ENV': process.env.APP_ENV,
'process.env.VERSION': packageJson.version,

View File

@ -154,7 +154,7 @@ describe('gateway', () => {
let port;
let messages: Array<string>;
const connectClient = (port) => {
wsClient = new ws(`ws://localhost:${port}/ws`);
wsClient = new ws(`ws://localhost:${port}${process.env.WS_PATH}`);
wsClient.on('message', (data) => {
const message = data.toString();
messages.push(message);

View File

@ -15,7 +15,7 @@ import handler from 'serve-handler';
import { parse } from 'url';
import { AppSupervisor } from '../app-supervisor';
import { ApplicationOptions } from '../application';
import { getPackageDirByExposeUrl, getPackageNameByExposeUrl, PLUGIN_STATICS_PATH } from '../plugin-manager';
import { PLUGIN_STATICS_PATH, getPackageDirByExposeUrl, getPackageNameByExposeUrl } from '../plugin-manager';
import { applyErrorWithArgs, getErrorWithCode } from './errors';
import { IPCSocketClient } from './ipc-socket-client';
import { IPCSocketServer } from './ipc-socket-server';
@ -331,7 +331,7 @@ export class Gateway extends EventEmitter {
from: 'node',
})
.then(async () => {
if (!await mainApp.isStarted()) {
if (!(await mainApp.isStarted())) {
await mainApp.stop();
}
})
@ -396,7 +396,7 @@ export class Gateway extends EventEmitter {
this.server.on('upgrade', (request, socket, head) => {
const { pathname } = parse(request.url);
if (pathname === '/ws') {
if (pathname === process.env.WS_PATH) {
this.wsServer.wss.handleUpgrade(request, socket, head, (ws) => {
this.wsServer.wss.emit('connection', ws, request);
});

View File

@ -32,9 +32,9 @@ export const startServerWithRandomPort = async (startServer) => {
};
export const createWsClient = async ({ serverPort, options = {} }) => {
console.log(`connect to ws://localhost:${serverPort}/ws`, options);
console.log(`connect to ws://localhost:${serverPort}${process.env.WS_PATH}`, options);
const wsc = new ws(`ws://localhost:${serverPort}/ws`, options);
const wsc = new ws(`ws://localhost:${serverPort}${process.env.WS_PATH}`, options);
const messages = [];
wsc.on('message', (data) => {