Skip to content

Commit

Permalink
Fix busboy + GCF race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace Nassri committed Aug 17, 2018
1 parent b967c6f commit 960efb9
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions functions/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,24 +166,42 @@ exports.uploadFile = (req, res) => {
fields[fieldname] = val;
});

let fileWrites = [];

// This code will process each file uploaded.
busboy.on('file', (fieldname, file, filename) => {
// Note: os.tmpdir() points to an in-memory file system on GCF
// Thus, any files in it must fit in the instance's memory.
console.log(`Processed file ${filename}`);
const filepath = path.join(tmpdir, filename);
uploads[fieldname] = filepath;
file.pipe(fs.createWriteStream(filepath));

const writeStream = fs.createWriteStream(filepath);
file.pipe(writeStream);

// File was processed by Busboy; wait for it to be written to disk.
const promise = new Promise((resolve, reject) => {
file.on('end', () => {
writeStream.end();
});
writeStream.on('finish', resolve);
writeStream.on('error', reject);
});
fileWrites.push(promise);
});

// This event will be triggered after all uploaded files are saved.
// Triggered once all uploaded files are processed by Busboy.
// We still need to wait for the disk writes (saves) to complete.
busboy.on('finish', () => {
// TODO(developer): Process uploaded files here
for (const name in uploads) {
const file = uploads[name];
fs.unlinkSync(file);
}
res.send();
Promise.all(fileWrites)
.then(() => {
// TODO(developer): Process saved files here
for (const name in uploads) {
const file = uploads[name];
fs.unlinkSync(file);
}
res.send();
});
});

busboy.end(req.rawBody);
Expand Down

0 comments on commit 960efb9

Please sign in to comment.