Control File Extension for Buffer downloads

We started using runkit for creating reproducible examples and it has worked reasonably well. We found one major stumbling block: there’s no way to specify the filename or even the extension for buffer output.

Example: https://runkit.com/sheetjs/58f5c48e958121001283c8fe

This case builds up an XLSX file. Normally in NPM we would use XLSX.writeFile but that ultimately calls fs.writeFileSync which, based on other issues in the forum, is apparently not supported. That’s fine, our workaround is to return a Buffer that you can download. The problem with this approach is that you can’t specify an extension. In this case, since XLSX files are ultimately ZIP containers, runkit guesses that the output is a zip file.

Is there a way to configure the buffer display to force a file extension?

Hi Sheetjs,

We’ve been looking into this the past couple of weeks. We’ve found that we can actually auto-detect that the file is xlsx, so at least for this case, we would be able to just show it that way. We’re still investigating the best way to implement this, and will see how long it would take to get in there.

Thanks!

Francisco

The simplest approach would just check if the Buffer has a filename key and use that as the output filename. It wouldn’t disrupt any standard usecases.

The ideal solution would be a fake filesystem so that the entire fs family would run as expected: fs.readFile / fs.readFileSync would read from the fake filesystem and fs.writeFile / fs.writeFileSync would write to this filesystem.

We’ve found that the file command correctly identifies zip files as excel files (when they are excel files of course). Just storing the buffer in a file and then using that command would correctly tell us the file type, we’re trying to figure out if we can avoid the filesystem round trip.