Open rob-ubnt opened 6 years ago
I think I've discovered the problem, but I'm not sure how to solve it. I added a log statement after calling the file()
method...
createTopFile().then(() => {
return readTopFile().then(data => {
console.log('Content of /tmp/top.txt is:', data) // ...this prints "Content of /tmp/top.txt is: <Buffer 74 6f 70 20 ... >"
systemZip.file('top.txt', data)
console.log('Added top.txt to system folder') // ...new log statement
})
})
...and another one in the onUpdate()
function:
zip.generateAsync({
type: 'nodebuffer',
compression: 'DEFLATE',
compressionOptions: {level: 1},
platform: process.platform
}, (metadata) => {
if (metadata.currentFile) {
this.verbose(`generateAsync() is processing ${metadata.currentFile}`) // ...new log statement
}
}).then(buffer => {
res.status(200).set('Content-Type', 'application/zip').send(buffer)
}).catch(err => {
res.status(500).json({error: err.message})
})
I never see generateAsync() is processing system/top.txt
in the logs. In fact, here you can see that generateAsync()
starts processing my other files before top.txt is added to the system folder:
...
Added /tmp/foo.txt to system folder
Added /tmp/bar.txt to system folder
generateAsync() is processing system/
generateAsync() is processing system/foo.txt
generateAsync() is processing system/bar.txt
Added /tmp/top.txt to system folder
...
So for my top.txt file, how can I force the file()
method to complete before the call to generateAsync()
is executed?
I fell upon this issue recently, and It does not seem to be an issue with jszip
itself.
The problem here seems to be a race condition due to incorrect promise chaining at the end of the code snippet given above. It was probably meant to be written as:
createTopFile().then(() => {
return readTopFile();
}).then(data => {
systemZip.file('top.txt', data);
return zip.generateAsync({
type: 'nodebuffer',
compression: 'DEFLATE',
compressionOptions: {level: 1},
platform: process.platform
});
}).then(buffer => {
res.status(200).set('Content-Type', 'application/zip').send(buffer)
}).catch(err => {
res.status(500).json({error: err.message})
});
Did you fix it @rob-ubnt? I am having the same problem.
I'm developing an API that allows users to download a zip archive containing log files and various other text files. For some reason, jszip randomly leaves files out of the archive. Here's an example of a file that often, but not always, gets left out:
The
/tmp/top.txt
file is always created successfully (just like other files before it), and there are no errors thrown, but for some reason thesystem
folder in the zip archive doesn't always have atop.txt
file.Any ideas why?