Skip to content

Commit

Permalink
feat: 实现 RPC 服务,并从 vite 迁移到 rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
LokiSharp committed Oct 22, 2023
1 parent e3ba2a0 commit 0e53677
Show file tree
Hide file tree
Showing 34 changed files with 1,269 additions and 1,176 deletions.
2 changes: 1 addition & 1 deletion common/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"ignorePatterns": [
"dist",
"coverage",
"vite.config.ts"
"rollup.config.ts"
],
"rules": {
"@typescript-eslint/explicit-function-return-type": "warn",
Expand Down
33 changes: 18 additions & 15 deletions common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,43 @@
"files": [
"dist"
],
"main": "./dist/common.cjs",
"module": "./dist/common.js",
"main": "./dist/index.js",
"module": "./dist/index.js",
"exports": {
"types": "./dist/index.d.ts",
"import": "./dist/common.js",
"require": "./dist/common.cjs"
"import": "./dist/index.js",
"require": "./dist/index.js"
},
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"build": "rollup --config rollup.config.ts --configPlugin typescript",
"lint": "eslint . --ext ts,tsx",
"test": "jest",
"coverage": "jest --coverage"
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.5",
"@types/jest": "^29.5.5",
"@types/lodash": "^4.14.199",
"@types/node": "^20.8.3",
"@types/lodash": "^4.14.200",
"@types/node": "^20.8.7",
"@typescript-eslint/eslint-plugin": "^6.7.4",
"eslint": "^8.51.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.7.0",
"lodash": "^4.17.21",
"prettier": "^3.0.3",
"rollup": "^4.1.4",
"rollup-plugin-dts": "^6.1.0",
"ts-jest": "^29.1.1",
"typescript": "^5.1.3",
"vite": "^4.3.9",
"vite-plugin-dts": "^3.6.0"
"ts-node": "^10.9.1",
"tsc-alias": "^1.8.8",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
},
"dependencies": {
"lodash": "^4.17.21"
},
"_moduleAliases": {
"@": "src"
},
"dependencies": {
"q": "^1.5.1"
}
}
46 changes: 46 additions & 0 deletions common/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import typescript from "@rollup/plugin-typescript";
import { dts } from "rollup-plugin-dts";
import alias from '@rollup/plugin-alias';
import * as path from "path";
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';

const config = [
{
input: 'src/index.ts',
output: [
{
file: './dist/index.js',
format: 'cjs',
sourcemap: true,
}
],
plugins: [
typescript({
tsconfig: './tsconfig.json'
}),
resolve(),
commonjs(),
alias({
entries: [
{ find: '@', replacement: path.join(path.resolve(), "src") }
]
}),
]
},
{
input: "src/index.ts",
output: [{ file: "./dist/index.d.ts", format: "es" }],
plugins: [
dts(),
alias({
entries: [
{ find: '@', replacement: path.join(path.resolve(), "src") }
]
}),
resolve(),
commonjs()
]
}
]
export default config;
66 changes: 66 additions & 0 deletions common/src/Rpc/JSONFrameStream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Writable, WritableOptions } from "stream";

export class JSONFrameStream extends Writable {
public handler: (obj: object) => void;

Check warning on line 4 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'obj' is defined but never used

Check warning on line 4 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

'obj' is defined but never used

Check warning on line 4 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.x)

'obj' is defined but never used

Check warning on line 4 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, latest)

'obj' is defined but never used

Check warning on line 4 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 16.x)

'obj' is defined but never used

Check warning on line 4 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 16.x)

'obj' is defined but never used

Check warning on line 4 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20.x)

'obj' is defined but never used

Check warning on line 4 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20.x)

'obj' is defined but never used

Check warning on line 4 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18.x)

'obj' is defined but never used

Check warning on line 4 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, latest)

'obj' is defined but never used

Check warning on line 4 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

'obj' is defined but never used

Check warning on line 4 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

'obj' is defined but never used
public frame: Frame | null;

public constructor(
handler: (obj: object) => void,

Check warning on line 8 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'obj' is defined but never used

Check warning on line 8 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

'obj' is defined but never used

Check warning on line 8 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.x)

'obj' is defined but never used

Check warning on line 8 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, latest)

'obj' is defined but never used

Check warning on line 8 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 16.x)

'obj' is defined but never used

Check warning on line 8 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 16.x)

'obj' is defined but never used

Check warning on line 8 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20.x)

'obj' is defined but never used

Check warning on line 8 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20.x)

'obj' is defined but never used

Check warning on line 8 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18.x)

'obj' is defined but never used

Check warning on line 8 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, latest)

'obj' is defined but never used

Check warning on line 8 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

'obj' is defined but never used

Check warning on line 8 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

'obj' is defined but never used
options?: WritableOptions,
) {
super(options);
this.handler = handler;
this.frame = null;
}

public _write(
chunk: Buffer,
encoding: BufferEncoding,
callback: (error?: Error | null) => void,

Check warning on line 19 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'error' is defined but never used

Check warning on line 19 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

'error' is defined but never used

Check warning on line 19 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.x)

'error' is defined but never used

Check warning on line 19 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, latest)

'error' is defined but never used

Check warning on line 19 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 16.x)

'error' is defined but never used

Check warning on line 19 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 16.x)

'error' is defined but never used

Check warning on line 19 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20.x)

'error' is defined but never used

Check warning on line 19 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20.x)

'error' is defined but never used

Check warning on line 19 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18.x)

'error' is defined but never used

Check warning on line 19 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, latest)

'error' is defined but never used

Check warning on line 19 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

'error' is defined but never used

Check warning on line 19 in common/src/Rpc/JSONFrameStream.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

'error' is defined but never used
): void {
this._parse(chunk);
callback();
}

public _parse(buffer: Buffer): unknown | undefined {
if (!buffer.length) {
return;
}
if (!this.frame) {
this.frame = {
data: Buffer.alloc(4),
pointer: 0,
};
}
if (!this.frame.size) {
const length = Math.min(buffer.length, 4 - this.frame.pointer);
buffer.copy(this.frame.data, this.frame.pointer, 0, length);
this.frame.pointer += length;
if (this.frame.pointer === 4) {
this.frame.size = this.frame.data.readUInt32BE();
this.frame.data = Buffer.alloc(this.frame.size);
this.frame.pointer = 0;
}
return this._parse(buffer.slice(length));
} else {
const length = Math.min(
buffer.length,
this.frame.size - this.frame.pointer,
);
buffer.copy(this.frame.data, this.frame.pointer, 0, length);
this.frame.pointer += length;
if (this.frame.pointer == this.frame.size) {
this.handler(JSON.parse(this.frame.data.toString("utf8")));
this.frame = null;
}
return this._parse(buffer.slice(length));
}
}

public static makeFrame(obj: object): Buffer {
const data = Buffer.from(JSON.stringify(obj), "utf8");
const length = Buffer.alloc(4);
length.writeUInt32BE(data.length);
return Buffer.concat([length, data]);
}
}
88 changes: 88 additions & 0 deletions common/src/Rpc/RpcClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { Socket } from "net";
import { EventEmitter } from "events";
import { JSONFrameStream } from "@/Rpc/JSONFrameStream";

export class RpcClient {
public socket: Socket;
public requestId: number;
public defers: Map<
string,
{
defer: Promise<unknown>;
reject: (reason?: Error | undefined) => void;

Check warning on line 12 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'reason' is defined but never used

Check warning on line 12 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

'reason' is defined but never used

Check warning on line 12 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.x)

'reason' is defined but never used

Check warning on line 12 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, latest)

'reason' is defined but never used

Check warning on line 12 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 16.x)

'reason' is defined but never used

Check warning on line 12 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 16.x)

'reason' is defined but never used

Check warning on line 12 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20.x)

'reason' is defined but never used

Check warning on line 12 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20.x)

'reason' is defined but never used

Check warning on line 12 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18.x)

'reason' is defined but never used

Check warning on line 12 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, latest)

'reason' is defined but never used

Check warning on line 12 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

'reason' is defined but never used

Check warning on line 12 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

'reason' is defined but never used
resolve: (value?: object) => void;

Check warning on line 13 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'value' is defined but never used

Check warning on line 13 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

'value' is defined but never used

Check warning on line 13 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.x)

'value' is defined but never used

Check warning on line 13 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, latest)

'value' is defined but never used

Check warning on line 13 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 16.x)

'value' is defined but never used

Check warning on line 13 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 16.x)

'value' is defined but never used

Check warning on line 13 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20.x)

'value' is defined but never used

Check warning on line 13 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20.x)

'value' is defined but never used

Check warning on line 13 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18.x)

'value' is defined but never used

Check warning on line 13 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, latest)

'value' is defined but never used

Check warning on line 13 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

'value' is defined but never used

Check warning on line 13 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

'value' is defined but never used
}
>;
public pubSub: EventEmitter;

public constructor(socket: Socket) {
this.socket = socket;
this.socket.pipe(
new JSONFrameStream(
(this._processFrame as (obj: object) => void).bind(this),

Check warning on line 22 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'obj' is defined but never used

Check warning on line 22 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

'obj' is defined but never used

Check warning on line 22 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.x)

'obj' is defined but never used

Check warning on line 22 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, latest)

'obj' is defined but never used

Check warning on line 22 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 16.x)

'obj' is defined but never used

Check warning on line 22 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 16.x)

'obj' is defined but never used

Check warning on line 22 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20.x)

'obj' is defined but never used

Check warning on line 22 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20.x)

'obj' is defined but never used

Check warning on line 22 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18.x)

'obj' is defined but never used

Check warning on line 22 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, latest)

'obj' is defined but never used

Check warning on line 22 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

'obj' is defined but never used

Check warning on line 22 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

'obj' is defined but never used
),
);
this.requestId = 0;
this.defers = new Map();
this.pubSub = new EventEmitter();
}

public _processFrame(obj: RpcClientFrameObj): void {
if (obj.pubSub) {
this.pubSub.emit(obj.pubSub.channel, obj.pubSub.channel, obj.pubSub.data);
this.pubSub.emit("*", obj.pubSub.channel, obj.pubSub.data);
return;
}
if (!this.defers.has(String(obj.id))) {
console.error("invalid request id", obj.id);
return;
}
if (obj.error) {
this.defers.get(String(obj.id))!.reject(obj.error);
} else {
this.defers.get(String(obj.id))!.resolve(obj.result);
}
this.defers.delete(String(obj.id));
}

public request(
method: string,
...args: unknown[]
): {
defer: Promise<unknown>;
reject: (reason?: Error) => void;

Check warning on line 53 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'reason' is defined but never used

Check warning on line 53 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

'reason' is defined but never used

Check warning on line 53 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.x)

'reason' is defined but never used

Check warning on line 53 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, latest)

'reason' is defined but never used

Check warning on line 53 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 16.x)

'reason' is defined but never used

Check warning on line 53 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 16.x)

'reason' is defined but never used

Check warning on line 53 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20.x)

'reason' is defined but never used

Check warning on line 53 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20.x)

'reason' is defined but never used

Check warning on line 53 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18.x)

'reason' is defined but never used

Check warning on line 53 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, latest)

'reason' is defined but never used

Check warning on line 53 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

'reason' is defined but never used

Check warning on line 53 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

'reason' is defined but never used
resolve: (value?: object) => void;

Check warning on line 54 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'value' is defined but never used

Check warning on line 54 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

'value' is defined but never used

Check warning on line 54 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.x)

'value' is defined but never used

Check warning on line 54 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, latest)

'value' is defined but never used

Check warning on line 54 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 16.x)

'value' is defined but never used

Check warning on line 54 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 16.x)

'value' is defined but never used

Check warning on line 54 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20.x)

'value' is defined but never used

Check warning on line 54 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20.x)

'value' is defined but never used

Check warning on line 54 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18.x)

'value' is defined but never used

Check warning on line 54 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, latest)

'value' is defined but never used

Check warning on line 54 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

'value' is defined but never used

Check warning on line 54 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

'value' is defined but never used
} {
this.requestId++;
const request = {
id: this.requestId,
method,
args,
};
let resolve = ((): void => {}) as (value?: object) => void,

Check warning on line 62 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'value' is defined but never used

Check warning on line 62 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

'value' is defined but never used

Check warning on line 62 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.x)

'value' is defined but never used

Check warning on line 62 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, latest)

'value' is defined but never used

Check warning on line 62 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 16.x)

'value' is defined but never used

Check warning on line 62 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 16.x)

'value' is defined but never used

Check warning on line 62 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20.x)

'value' is defined but never used

Check warning on line 62 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20.x)

'value' is defined but never used

Check warning on line 62 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18.x)

'value' is defined but never used

Check warning on line 62 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, latest)

'value' is defined but never used

Check warning on line 62 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

'value' is defined but never used

Check warning on line 62 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

'value' is defined but never used
reject = ((): void => {}) as (reason?: Error) => void;

Check warning on line 63 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'reason' is defined but never used

Check warning on line 63 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

'reason' is defined but never used

Check warning on line 63 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.x)

'reason' is defined but never used

Check warning on line 63 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, latest)

'reason' is defined but never used

Check warning on line 63 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 16.x)

'reason' is defined but never used

Check warning on line 63 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 16.x)

'reason' is defined but never used

Check warning on line 63 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20.x)

'reason' is defined but never used

Check warning on line 63 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20.x)

'reason' is defined but never used

Check warning on line 63 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18.x)

'reason' is defined but never used

Check warning on line 63 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, latest)

'reason' is defined but never used

Check warning on line 63 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

'reason' is defined but never used

Check warning on line 63 in common/src/Rpc/RpcClient.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

'reason' is defined but never used
this.socket.write(JSONFrameStream.makeFrame(request));
const defer = new Promise((res, rej) => {
[resolve, reject] = [res, rej];
});
this.defers.set(String(this.requestId), { defer, reject, resolve });
return { defer, reject, resolve };
}

public subscribe(
channelToSubscribe: string,
callback: (...args: unknown[]) => void,
): void {
const request = {
method: "subscribe",
channel: channelToSubscribe,
};
this.socket.write(JSONFrameStream.makeFrame(request));
this.pubSub.addListener(
channelToSubscribe,
(channel: string, ...args: unknown[]) => {
callback.apply({ channel }, args);
},
);
}
}
57 changes: 57 additions & 0 deletions common/src/Rpc/RpcServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Socket } from "net";
import { JSONFrameStream } from "@/Rpc/JSONFrameStream";

export class RpcServer {
public socket: Socket;
public methods: RpcMethods;
public channelUnsubscribe: Map<string, () => void>;
public constructor(socket: Socket, methods: RpcMethods) {
this.socket = socket;
this.socket.pipe(
new JSONFrameStream(
(this._processFrame as (obj: object) => void).bind(this),
),
);
this.methods = methods;
this.channelUnsubscribe = new Map();
this.socket.on("close", () => {
this.channelUnsubscribe.forEach((unsubscribe) => unsubscribe());
this.channelUnsubscribe.clear();
});
}

public _processFrame(obj: RpcServerFrameObj): void {
const args = obj.args || [];
if (obj.method === "subscribe") {
if (this.channelUnsubscribe.has("*")) {
return;
}
if (obj.channel === "*") {
this.channelUnsubscribe.forEach((unsubscribe) => unsubscribe());
this.channelUnsubscribe.clear();
}
if (!this.channelUnsubscribe.has(obj.channel)) {
const unsubscribe = this.methods.subscribe(obj.channel, (pubSub) => {
this.socket.write(JSONFrameStream.makeFrame({ pubSub }));
});
this.channelUnsubscribe.set(obj.channel, unsubscribe);
}
this.socket.write("");
return;
}
this.methods[obj.method].apply(
null,
args.concat([
(error: Error, result: object): void => {
const response: RpcResponse = { id: obj.id };
if (error) {
response.error = error;
} else {
response.result = result;
}
this.socket.write(JSONFrameStream.makeFrame(response));
},
]),
);
}
}
2 changes: 2 additions & 0 deletions common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ConfigManager } from "@/ConfigManager";
export { RpcServer } from "@/Rpc/RpcServer";
export { RpcClient } from "@/Rpc/RpcClient";

export const common = {
configManager: new ConfigManager(),
Expand Down
3 changes: 0 additions & 3 deletions common/src/main.ts

This file was deleted.

30 changes: 30 additions & 0 deletions common/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
type Frame = { data: Buffer; pointer: number; size?: number };

type RpcServerFrameObj = {
method: string;
channel: string;
id: string;
args: unknown[];
};

type RpcClientFrameObj = {
id: string;
error?: Error;
result?: object;
pubSub: ChildProcess;
};

type RpcMethods = {
subscribe(
channel: string,
listener: (data: { channel: string; data: unknown }) => void,
): () => void;
publish: (channel: string, data: unknown, cb?: CallBack) => void;
[name: string]: (cb?: CallBack, ...args) => void;
};

type RpcResponse = {
id: string;
error?: Error;
result?: object;
};
7 changes: 7 additions & 0 deletions common/tests/Rpc/JSONFrameStream.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { JSONFrameStream } from "@/Rpc/JSONFrameStream";

test("JSONFrameStream", () => {
const buffer = JSONFrameStream.makeFrame({ Loki: "god" });
console.log(buffer);
expect(buffer.length).toBe(18);
});
Loading

0 comments on commit 0e53677

Please sign in to comment.