我正在try 将函数转换为字符串,以将其发送到工作线程并在那里执行它.然而,在使用导入代码的地方,该字符串包含奇怪的字符.
import { HttpStatus } from '@nestjs/common';
import { access, constants, writeFile } from 'fs/promises';
import path from 'path';
import { Error as CustomError } from 'src/util/Error';
import { AbstractTask } from 'src/util/queue/AbstractTask';
export class CreateFileTask extends AbstractTask<boolean> {
private path: string;
private buffer: Buffer;
constructor(path: string, buffer: Buffer) {
super();
this.path = path;
this.buffer = buffer;
}
public async execute(): Promise<boolean> {
return new Promise(async (resolve, reject) => {
let resolvedPath = path.resolve(this.path);
try {
await access(resolvedPath, constants.F_OK);
} catch (e) {
reject(new CustomError(`file at ${this.path} already exists`, HttpStatus.CONFLICT));
}
try {
await writeFile(resolvedPath, this.buffer);
} catch (e) {
if (e instanceof Error) {
reject(new CustomError(e.message, HttpStatus.INTERNAL_SERVER_ERROR));
} else {
reject(new CustomError(e as string, HttpStatus.INTERNAL_SERVER_ERROR));
}
}
resolve(true);
});
}
}
呼叫new CreateFileTask(...).execute.toString()
的结果是奇怪的字符:
execute() {
return new Promise(async (resolve, reject) => {
let resolvedPath = path.resolve(this.path);
try {
await (0, promises_1.access)(resolvedPath, promises_1.constants.F_OK);
}
catch (e) {
reject(new Error_1.Error(`file at ${this.path} already exists`, common_1.HttpStatus.CONFLICT));
}
try {
await (0, promises_1.writeFile)(resolvedPath, this.buffer);
}
catch (e) {
if (e instanceof Error) {
reject(new Error_1.Error(e.message, common_1.HttpStatus.INTERNAL_SERVER_ERROR));
}
else {
reject(new Error_1.Error(e, common_1.HttpStatus.INTERNAL_SERVER_ERROR));
}
}
resolve(true);
});
}
使用import * as path from "path"
似乎确实可以修复Path模块的问题.为什么会这样呢?有没有其他解决方案,即使不需要也可以用import * as ... from ...
AKA进口everthing?