File
Blob: samples/nodejs-compat-fs-yazl/worker.js
| 1 | import { default as yazl} from 'yazl'; |
| 2 | import { |
| 3 | createWriteStream, |
| 4 | readFileSync, |
| 5 | readdirSync, |
| 6 | } from 'node:fs'; |
| 7 | |
| 8 | export default { |
| 9 | fetch(request) { |
| 10 | |
| 11 | const { promise, resolve, reject } = Promise.withResolvers(); |
| 12 | |
| 13 | const zipfile = new yazl.ZipFile(); |
| 14 | |
| 15 | const entries = readdirSync("/bundle"); |
| 16 | for (const entry of entries) { |
| 17 | zipfile.addFile(`/bundle/${entry}`, entry); |
| 18 | } |
| 19 | |
| 20 | zipfile.outputStream.pipe(createWriteStream("/tmp/output.zip")) |
| 21 | .on("close", function() { |
| 22 | const buffer = readFileSync("/tmp/output.zip"); |
| 23 | resolve(new Response(buffer)); |
| 24 | }); |
| 25 | |
| 26 | zipfile.end(); |
| 27 | |
| 28 | return promise; |
| 29 | } |
| 30 | }; |