fs.createWriteStream正在流式传输但没有写入数据。

huangapple go评论64阅读模式
英文:

fs.createWriteStream is streaming but not writing

问题

I only recently noticed that the following code is not doing what it was doing earlier… actually writing the file to the disk.

const download = async (archive) => {
    const d = await new Promise((resolve) => {      
        const opts = { method: 'GET' };
        const req = https.request(url, opts, (res) => {
            const file = fs.createWriteStream(`path/to/${archive}`);

            res.on('data', (chunk) => {
                    file.write(chunk);
                    //process.stdout.write(chunk);
                })
                .on('end', () => file.end());
    
            resolve(archive);
        });
        
        req.on('error', (error) => console.error(error));
        req.end();
    });
}

the file is getting created on disk, but is only 0B. If I uncomment process.stdout.write(chunk), it happily spits out gobbledegook of the binary file to the console. But nothing is actually written to the disk.

Where am I goofing up? or has something changed in node recently that is silently screwing up my code? Very happy for any guidance.

英文:

I only recently noticed that the following code is not doing what it was doing earlier… actually writing the file to the disk.

const download = async (archive) => {
    const d = await new Promise((resolve) => {      
        const opts = { method: 'GET' };
        const req = https.request(url, opts, (res) => {
            const file = fs.createWriteStream(`path/to/${archive}`);

            res.on('data', (chunk) => {
                    file.write(chunk);
                    //process.stdout.write(chunk);
                })
                .on('end', () => file.end());
    
            resolve(archive);
        });
        
        req.on('error', (error) => console.error(error));
        req.end();
    });
}

the file is getting created on disk, but is only 0B. If I uncomment process.stdout.write(chunk), it happily spits out gobbledegook of the binary file to the console. But nothing is actually written to the disk.

Where am I goofing up? or has something changed in node recently that is silently screwing up my code? Very happy for any guidance.

答案1

得分: 0

更新: 我说得太快了。请参阅下面的评论
更新2: 这对我有帮助。现在完整的 zip 文件可以正确下载并解压缩

感谢 @jfriend00 的评论,以下编辑修复了问题

const download = async (archive) => {
    const d = await new Promise((resolve) => {      
        const opts = { method: 'GET' };
        const req = https.request(url, opts, (res) => {
            const file = fs.createWriteStream(`path/to/${archive}`);

            res.on('data', (chunk) => {
                    file.write(chunk);
                })
                .on('end', () => {
                    file.end());
                    // 将 resolve() 移到这里
                    resolve(archive);
                });
    
            //resolve(archive);
        });
        
        req.on('error', (error) => console.error(error));
        req.end();
    });
}

我本以为原始代码没问题,突然就不工作了,但我很高兴我没有发誓。

更新:

上述编辑没有帮助。虽然现在文件确实被下载,但下载不正确。它是一个 zip 文件,但当我尝试解压缩时,出现错误

错误: 命令失败: …  
  未找到中央目录签名。这个  
  文件不是一个 zip 文件,或者它构成了一个多部分归档的一个磁盘。在后一种情况下,  
  中央目录和 zip 文件的注释将在这个  
  归档的最后一个磁盘上找到。  

但是当我直接下载文件时,解压缩正常。所以我的代码没有正确下载文件。

英文:

update: I spoke to soon. See comment below
update2: this helped me. The full zip file is downloading and unzipping properly now


thanks to the comment from @jfriend00, the following edit fixed the problem

const download = async (archive) => {
    const d = await new Promise((resolve) => {      
        const opts = { method: 'GET' };
        const req = https.request(url, opts, (res) => {
            const file = fs.createWriteStream(`path/to/${archive}`);

            res.on('data', (chunk) => {
                    file.write(chunk);
                })
                .on('end', () => {
                    file.end());
                    // moving resolve() here
                    resolve(archive);
                });
    
            //resolve(archive);
        });
        
        req.on('error', (error) => console.error(error));
        req.end();
    });
}

I could have sworn the original code was working fine and then stopped working, but I am glad I didn't swear.

update:

The edits above do not help. While now the file does get downloaded, it is not downloaded properly. It is a zip file, but when I try to unzip it, I get an error that

Error: Command failed: … 
  End-of-central-directory signature not found.  Either this 
  file is not a zipfile, or it constitutes one disk of a 
  multi-part archive.  In the latter case the central directory 
  and zipfile comment will be found on the last disk(s) of this 
  archive.

But when I download the file directly, it unzips just fine. So my code is not downloading the file properly.

huangapple
  • 本文由 发表于 2023年5月14日 05:29:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定