Skip to content
This repository has been archived by the owner on Jul 2, 2020. It is now read-only.

Commit

Permalink
fix: using ws instead of websocket (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
echosoar committed Mar 19, 2020
1 parent 1416f97 commit 01c0b38
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 69 deletions.
5 changes: 2 additions & 3 deletions packages/faas-cli-command-core/benchmark/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ const CorefuncTestInvoke = async () => {
await core.invoke('invoke');
};
const contenders = {
'core help no plugin': Corefunc,
'core help test plugin': CorefuncTest,
'core invoke test plugin': CorefuncTestInvoke,
'pure core': Corefunc,
'core help': CorefuncTest,
};

console.log('\nBenchmark:');
Expand Down
3 changes: 1 addition & 2 deletions packages/serverless-invoke/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
"@midwayjs/serverless-spec-builder": "^0.2.49",
"fs-extra": "^8.1.0",
"globby": "^10.0.1",
"ts-node": "^8.5.2",
"urllib": "^2.34.1",
"websocket": "^1.0.30"
"ws": "^7.2.3"
},
"devDependencies": {
"@midwayjs/faas": "^0.2.49",
Expand Down
58 changes: 35 additions & 23 deletions packages/serverless-invoke/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
*/
import { FaaSStarterClass, cleanTarget } from './utils';
import { join, resolve, relative } from 'path';
import { existsSync, move, writeFileSync, ensureFileSync } from 'fs-extra';
import {
existsSync,
move,
writeFileSync,
ensureFileSync,
remove,
} from 'fs-extra';
import { loadSpec, getSpecFile } from '@midwayjs/fcli-command-core';
import { writeWrapper } from '@midwayjs/serverless-spec-builder';
import { AnalyzeResult, Locator } from '@midwayjs/locate';
Expand Down Expand Up @@ -148,30 +154,36 @@ export abstract class InvokeCore implements IInvoke {
await cleanTarget(this.buildDir);
}
const opts = this.options.incremental ? { overwrite: true } : {};
if (this.codeAnalyzeResult.integrationProject) {
// 一体化调整目录
await tsIntegrationProjectCompile(baseDir, {
buildRoot: this.buildDir,
tsCodeRoot: this.codeAnalyzeResult.tsCodeRoot,
incremental: this.options.incremental,
tsConfig: {
compilerOptions: {
sourceRoot: this.codeAnalyzeResult.tsCodeRoot, // for sourceMap
try {
if (this.codeAnalyzeResult.integrationProject) {
// 一体化调整目录
await tsIntegrationProjectCompile(baseDir, {
buildRoot: this.buildDir,
tsCodeRoot: this.codeAnalyzeResult.tsCodeRoot,
incremental: this.options.incremental,
tsConfig: {
compilerOptions: {
sourceRoot: this.codeAnalyzeResult.tsCodeRoot, // for sourceMap
},
},
},
clean: this.options.clean,
});
} else {
await tsCompile(baseDir, {
tsConfigName: 'tsconfig.json',
tsConfig: {
compilerOptions: {
sourceRoot: resolve(baseDir, 'src'), // for sourceMap
clean: this.options.clean,
});
} else {
await tsCompile(baseDir, {
tsConfigName: 'tsconfig.json',
tsConfig: {
compilerOptions: {
sourceRoot: resolve(baseDir, 'src'), // for sourceMap
},
},
},
clean: this.options.clean,
});
await move(join(baseDir, 'dist'), join(this.buildDir, 'dist'), opts);
clean: this.options.clean,
});
await move(join(baseDir, 'dist'), join(this.buildDir, 'dist'), opts);
}
} catch (e) {
await remove(buildLogPath);
lockMap[buildLogPath] = false;
throw new Error(`Typescript Build Error, Please Check Your FaaS Code!`);
}
lockMap[buildLogPath] = true;
// 针对多次调用清理缓存
Expand Down
45 changes: 4 additions & 41 deletions packages/serverless-invoke/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const urllib = require('urllib');
const WebSocketClient = require('websocket').client;
const WebSocket = require('ws');
import { tmpdir } from 'os';
import {
existsSync,
Expand Down Expand Up @@ -72,53 +72,16 @@ export function getWssUrl(port, type?: string, isThrowErr?: boolean) {
);
}
});
}, 300);
}, 100);
});
}

function debugWs(addr) {
let currentId = 0;
const cbMap = {};
return new Promise(resolve => {
const client = new WebSocketClient();
client.on('connect', function(connection) {
connection.on('message', message => {
if (message.utf8Data) {
const data = JSON.parse(message.utf8Data);
if (data.id) {
if (data.id > currentId) {
currentId = data.id - 0;
}
if (cbMap[data.id]) {
cbMap[data.id](data);
}
}
}
});

const send = (method, params?) => {
return new Promise(resolve => {
const curId = currentId + 1;
currentId = curId;
cbMap[curId] = data => {
resolve(data);
};

const param: any = { id: curId, method };
if (params) {
param.params = params;
}
connection.sendUTF(JSON.stringify(param));
});
};

send('Profiler.enable');
send('Runtime.enable');
send('Debugger.enable', { maxScriptsCacheSize: 10000000 });
send('Debugger.setBlackboxPatterns', { patterns: ['internal'] });
const ws = new WebSocket(addr);
ws.on('open', () => {
resolve(send);
});
client.connect(addr);
});
}

Expand Down

0 comments on commit 01c0b38

Please sign in to comment.