Refactor: tsconfig strict: true

This commit is contained in:
unitwk 2024-03-14 14:31:34 +08:00
parent ae4d2d0516
commit e55f93e8dd
5 changed files with 46 additions and 29 deletions

View File

@ -9,6 +9,7 @@ export default class InstanceStreamListener {
public requestForward(socket: Socket, instanceUuid: string) {
if (this.listenMap.has(instanceUuid)) {
const sockets = this.listenMap.get(instanceUuid);
if (!sockets) return;
for (const iterator of sockets)
if (iterator.id === socket.id)
throw new Error(
@ -24,14 +25,14 @@ export default class InstanceStreamListener {
if (!this.listenMap.has(instanceUuid))
throw new Error(`The specified ${instanceUuid} does not exist in the listening table`);
const socketList = this.listenMap.get(instanceUuid);
socketList.forEach((v, index) => {
if (v.id === socket.id) socketList.splice(index, 1);
socketList?.forEach((v, index) => {
if (v.id === socket.id) socketList?.splice(index, 1);
});
}
public forward(instanceUuid: string, data: any) {
const sockets = this.listenMap.get(instanceUuid);
sockets.forEach((socket) => {
sockets?.forEach((socket) => {
if (socket && socket.connected) socket.emit("instance/stdout", data);
});
}
@ -39,13 +40,13 @@ export default class InstanceStreamListener {
public forwardViaCallback(instanceUuid: string, callback: (socket: Socket) => void) {
if (this.listenMap.has(instanceUuid)) {
const sockets = this.listenMap.get(instanceUuid);
sockets.forEach((socket) => {
sockets?.forEach((socket) => {
if (socket && socket.connected) callback(socket);
});
}
}
public hasListenInstance(instanceUuid: string) {
return this.listenMap.has(instanceUuid) && this.listenMap.get(instanceUuid).length > 0;
return this.listenMap.has(instanceUuid) && this.listenMap?.get(instanceUuid)!.length > 0;
}
}

View File

@ -8,8 +8,8 @@ import iconv from "iconv-lite";
export class StartError extends Error {}
export class ProcessWrapper extends EventEmitter {
public process: ChildProcess;
public pid: number;
public process?: ChildProcess;
public pid?: number;
public errMsg = {
timeoutErr: "task timeout!",
@ -21,7 +21,7 @@ export class ProcessWrapper extends EventEmitter {
public readonly file: string,
public readonly args: string[],
public readonly cwd: string,
public readonly timeout: number = null,
public readonly timeout: number = 0,
public readonly code = "utf-8",
public readonly option: SpawnOptionsWithoutStdio = {}
) {
@ -34,7 +34,7 @@ export class ProcessWrapper extends EventEmitter {
public start(): Promise<boolean> {
return new Promise((resolve, reject) => {
let timeTask: NodeJS.Timeout = null;
let timeTask: NodeJS.Timeout;
const subProcess = child_process.spawn(this.file, this.args, {
stdio: "pipe",
windowsHide: true,
@ -62,7 +62,7 @@ export class ProcessWrapper extends EventEmitter {
// timeout, terminate the task
if (this.timeout) {
timeTask = setTimeout(() => {
if (!subProcess.exitCode && subProcess.exitCode !== 0) {
if (subProcess?.pid && !subProcess.exitCode && subProcess.exitCode !== 0) {
killProcess(subProcess.pid, subProcess);
reject(new Error(this.errMsg.timeoutErr));
} else {
@ -74,32 +74,32 @@ export class ProcessWrapper extends EventEmitter {
}
public getPid() {
return this.process.pid;
return this.process?.pid;
}
public write(data?: string) {
return this.process.stdin.write(iconv.encode(data, this.code));
public write(data?: any) {
return this.process?.stdin?.write(iconv.encode(data, this.code));
}
public kill() {
killProcess(this.process.pid, this.process);
if (this.process?.pid) killProcess(this.process?.pid, this.process);
}
public status() {
return this.process.exitCode == null;
return !!this.process?.exitCode;
}
public exitCode() {
return this.process.exitCode;
return this.process?.exitCode;
}
private async destroy() {
try {
for (const n of this.eventNames()) this.removeAllListeners(n);
if (this.process.stdout)
if (this.process?.stdout)
for (const eventName of this.process.stdout.eventNames())
this.process.stdout.removeAllListeners(eventName);
if (this.process.stderr)
if (this.process?.stderr)
for (const eventName of this.process.stderr.eventNames())
this.process.stderr.removeAllListeners(eventName);
if (this.process)
@ -114,7 +114,7 @@ export class ProcessWrapper extends EventEmitter {
} catch (error: any) {
console.log("[ProcessWrapper destroy() Error]", error);
} finally {
this.process = null;
this.process = undefined;
}
}
}

View File

@ -52,11 +52,22 @@ export interface IDataSource<T> {
// MYSQL data source
export class MySqlSource<T> implements IDataSource<T> {
selectPage: (condition: any, page: number, pageSize: number) => Page<T>;
select: (condition: any) => any[];
update: (condition: any, data: any) => void;
delete: (condition: any) => void;
insert: (data: any) => void;
constructor(public data: any) {}
selectPage(condition: any, page: number, pageSize: number) {
return {
page,
pageSize,
maxPage: 0,
total: 0,
data: []
};
}
select(condition: any) {
return [];
}
update(condition: any, data: any) {}
delete(condition: any) {}
insert(data: any) {}
}
// local file data source (embedded microdatabase)
@ -99,7 +110,7 @@ export class LocalFileSource<T> implements IDataSource<T> {
}
select(condition: any): any[] {
return null;
return [];
}
update(condition: any, data: any) {}
delete(condition: any) {}

View File

@ -60,7 +60,7 @@ export function isEmpty(v: any): boolean {
return v === null || v === undefined;
}
export function supposeValue<T>(v: any, def: T = null): T {
export function supposeValue<T>(v: any, def?: T) {
if (isEmpty(v)) return def;
return v;
}

View File

@ -1,18 +1,23 @@
{
"compilerOptions": {
"declaration": true,
"skipLibCheck": true,
"strictNullChecks": true,
"noImplicitAny": true,
"noImplicitThis": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"allowJs": true,
"module": "commonjs",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"target": "ES2018",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"@languages/*": ["../languages/*"],
"*": ["node_modules/*", "src/types/*"]
}
},