javascript - Node.js ReadStream 在管道到 Hash 时卡在 Electron 上
问题描述
我正在使用vue-cli和vue-cli-plugin-electron-builder创建这个应用程序,我遇到了这个奇怪的问题,特定于电子,一旦应用程序启动,第一个ReadStream
创建的应用程序不会将其内容通过管道传输到给定的流。
现在,通过刷新应用程序CTRL + F5
将使流再次工作,从此不再有任何问题。
我当前的代码类似于应用程序启动后在渲染线程上调用:
public async run() {
await this.scanFolder("/some/path/to/a/folder");
}
private async scanFolder(path: string){
const entries = readdirSync(path, { withFileTypes: true });
for (const entry of entries){
if (!entry.isDirectory()){
const md5 = await calculateMD5(path + "/" + entry.name);
}
}
}
public static async calculateFileMD5(path: string) {
const md5 = createHash("md5");
md5.setEncoding("hex");
console.log("Creating promise");
const promise = new Promise<string>((resolve, reject) => {
const fileStream = createReadStream(path, {
autoClose: true,
emitClose: true
});
fileStream.on("open", () => console.log("STREAM OPEN"));
fileStream.on("ready", () => console.log("STREAM READY"));
fileStream.on("close", () => {
console.log("Stream closed");
resolve(md5.read());
});
fileStream.on("data", data => {
console.log(data);
});
fileStream.on("end", () => {
console.log("Stream ENDED");
resolve(md5.read());
});
fileStream.on("error", error => {
console.error(error);
reject(error);
});
console.log("Piping to MD5");
fileStream.pipe(md5, { end: true });
fileStream.resume();
console.log("Is paused?: " + fileStream.isPaused());
});
console.log("Returning promise");
return promise;
}
启动应用程序npm run electron:serve
并调用该run
函数将输出以下内容:
Creating promise
Piping to MD5
Is paused?: false
Returning promise
STREAM OPEN
STREAM READY
现在,如果通过CRTL + F5
流重新加载应用程序,则将其内容正确地通过管道传递给 Hash。
一旦应用程序启动以使流正常工作,我能做些什么使它不需要刷新?
解决方案
推荐阅读
- php - CakePHP 2 会话突然/过早结束
- javascript - 如何在 React 的函数中获取 useEffect?
- java - ./gradlew 在 docker 容器中找不到
- python - 用于查找矩阵和的顶部特征值的 Lanczos 算法
- node.js - npm audit fix 命令无法解决漏洞
- sql-server - 寻找 Linq 查询
- node.js - sveltekit 中未定义的 process.env 环境变量
- deep-learning - 梯度累积问题
- java - 字符串未显示在输出中
- python - 如何在 Python 中的密度颜色图上绘制密度线图