Skip to content

Commit

Permalink
Merge branch 'feat/v0.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
scarqin committed Mar 15, 2023
2 parents f5b5edf + d67d739 commit 4bfb442
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 95 deletions.
13 changes: 13 additions & 0 deletions src/node/extensions-manage/ecosystem.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
apps: [
{
name: 'extension-runtime',
script: 'main.js',
watch: '.'
}
],

deploy: {
production: {}
}
};
4 changes: 2 additions & 2 deletions src/node/extensions-manage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"scripts": {
"prepare": "node tools/install.cjs",
"dev": "nodemon main",
"start": "pm2 start main.js",
"stop": "pm2 stop main.js",
"start": "pm2 start ecosystem.config.cjs",
"stop": "pm2 stop ecosystem.config.cjs",
"log": "pm2 logs"
},
"type": "module",
Expand Down
17 changes: 17 additions & 0 deletions src/node/test-server/ecosystem.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
apps: [
{
name: 'http-server',
script: 'server/main.js',
watch: '.'
},
{
name: 'websocket-server',
script: 'server/socketio.js',
watch: '.'
}
],
deploy: {
production: {}
}
};
7 changes: 2 additions & 5 deletions src/node/test-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
},
"scripts": {
"dev": "node ./server/main.js",
"start": "pm2-runtime start ./server/main.js",
"start:io": "pm2-runtime start ./server/socketio.js",
"start:all": "yarn start && yarn start:io",
"start:all:pm2": "pm2 start ./server/main.js",
"start": "pm2 start ecosystem.config.js",
"watch": "pm2 list",
"stop": "pm2 stop all",
"stop": "pm2 stop ecosystem.config.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Postcat",
Expand Down
2 changes: 0 additions & 2 deletions src/node/test-server/server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const _LibsCommon = require('../request/libs/common.js');
const koaBody = require('koa-body');
const Koa = require('koa');
const cors = require('@koa/cors');
const socketio = require('./socketio.js');

const app = new Koa();
const port = process.env.TEST_SERVER_PORT || 4201;
Expand Down Expand Up @@ -46,6 +45,5 @@ app.use(async (ctx, next) => {
next();
});

socketio();
app.listen(port);
console.log(`Server is running at port ${port} ...`);
166 changes: 82 additions & 84 deletions src/node/test-server/server/socketio.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,100 +8,98 @@ process.on('uncaughtException', err => {

const _post = process.env.EOAPI_WEBSOCKET_PORT || 13928;

const socket = (port = _post) => {
const io = new IO.Server(port, {
transports: ['websocket']
});
io.on('connection', socket => {
// * send a message to the client
socket.emit('ws-client', 'link success');
let ws = null;
const io = new IO.Server(port, {
transports: ['websocket']
});
io.on('connection', socket => {
// * send a message to the client
socket.emit('ws-client', 'link success');
let ws = null;

const unlisten = () => {
if (!ws) return null;
ws.on('close', () => null);
ws.on('upgrade', () => null);
ws.on('message', () => null);
const unlisten = () => {
if (!ws) return null;
ws.on('close', () => null);
ws.on('upgrade', () => null);
ws.on('message', () => null);
ws = null;
};
socket.on('grpc-server', async data => {
// * 创建 grpc 客户端发起请求
// 端口管理、编译文件、运行插件代码、销毁服务
const [res, err] = await grpcClient(data);
socket.emit('grpc-client', [res, err]);
});
// receive a message from the client
socket.on('ws-server', ({ type, content }) => {
if (type === 'connect') {
return;
}
if (type === 'ws-disconnect') {
ws.close();
ws = null;
};
socket.on('grpc-server', async data => {
// * 创建 grpc 客户端发起请求
// 端口管理、编译文件、运行插件代码、销毁服务
const [res, err] = await grpcClient(data);
socket.emit('grpc-client', [res, err]);
});
// receive a message from the client
socket.on('ws-server', ({ type, content }) => {
if (type === 'connect') {
return;
}
if (type === 'ws-disconnect') {
ws.close();
ws = null;
return;
return;
}
if (type === 'ws-connect') {
const { request } = content;
const link = /^(wss:\/{2})|(ws:\/{2})\S+$/m.test(request.uri.trim())
? request.uri.trim()
: request.protocol + '://' + request.uri.trim().replace('//', '');
try {
ws = new WebSocket(link, {
headers: request?.requestParams.headerParams
?.filter(it => it.name && it.paramAttr?.example)
.reduce(
(total, { name, paramAttr }) => ({
...total,
[name]: paramAttr.example
}),
{}
)
});
} catch (error) {
socket.emit('ws-client', { type: 'ws-connect-back', status: -1, content: error });
}
if (type === 'ws-connect') {
const { request } = content;
const link = /^(wss:\/{2})|(ws:\/{2})\S+$/m.test(request.uri.trim())
? request.uri.trim()
: request.protocol + '://' + request.uri.trim().replace('//', '');
try {
ws = new WebSocket(link, {
headers: request?.requestParams.headerParams
?.filter(it => it.name && it.paramAttr?.example)
.reduce(
(total, { name, paramAttr }) => ({
...total,
[name]: paramAttr.example
}),
{}
)
});
} catch (error) {
socket.emit('ws-client', { type: 'ws-connect-back', status: -1, content: error });
}

ws.on('error', err => {
socket.emit('ws-client', { type: 'ws-connect-back', status: -1, content: err });
unlisten();
});
ws.on('error', err => {
socket.emit('ws-client', { type: 'ws-connect-back', status: -1, content: err });
unlisten();
});

const reqHeader = ws._req.getHeaders();
const reqHeader = ws._req.getHeaders();

// 打开WebSocket连接后立刻发送一条消息:
ws.on('open', () => {
// console.log(`[CLIENT] open()`);
});
ws.on('upgrade', ({ headers: resHeader }) => {
socket.emit('ws-client', { type: 'ws-connect-back', status: 0, content: { reqHeader, resHeader } });
});
// 打开WebSocket连接后立刻发送一条消息:
ws.on('open', () => {
// console.log(`[CLIENT] open()`);
});
ws.on('upgrade', ({ headers: resHeader }) => {
socket.emit('ws-client', { type: 'ws-connect-back', status: 0, content: { reqHeader, resHeader } });
});

ws.on('message', message => {
socket.emit('ws-client', {
type: 'ws-message-back',
status: 0,
content: message?.toString() || message
});
ws.on('message', message => {
socket.emit('ws-client', {
type: 'ws-message-back',
status: 0,
content: message?.toString() || message
});
});

// ws.on('close', () => {
// socket.emit('ws-client', {
// type: 'ws-connect-back',
// status: -1,
// content: 'Server disconnected'
// });
// unlisten();
// });
// ws.on('close', () => {
// socket.emit('ws-client', {
// type: 'ws-connect-back',
// status: -1,
// content: 'Server disconnected'
// });
// unlisten();
// });
}
if (type === 'ws-message') {
const { message } = content;
if (!message) {
console.log('发送内容为空');
}
if (type === 'ws-message') {
const { message } = content;
if (!message) {
console.log('发送内容为空');
}
ws.send(message);
}
});
ws.send(message);
}
});
};
});

module.exports = socket;
2 changes: 1 addition & 1 deletion src/platform/node/extension-manager/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import path from 'node:path';
const extServerMap = new Map<string, SidebarView>();

// * npm pkg name
const defaultExtension = [{ name: 'postcat-export-openapi' }, { name: 'postcat-import-openapi' }];
const defaultExtension = [{ name: 'postcat-export-openapi' }, { name: 'postcat-import-openapi' }, { name: 'postcat-basic-auth' }];
const isExists = async filePath =>
await promises
.access(filePath)
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"scripts/build.js",
"test/e2e/test.ts",
"test/e2e/test.ts",
"src/node/extensions-manage/extension-manage.js"
"src/node/extensions-manage/extension-manage.js",
"src/node/test-server/ecosystem.config.js"
],
"exclude": ["node_modules", "**/*.spec.ts", "**/browser/**/*.js", "**/browser/**/*.ts", "out"],
"angularCompilerOptions": {
Expand Down

0 comments on commit 4bfb442

Please sign in to comment.