Skip to content

Commit abc259c

Browse files
author
Ace Nassri
committed
Fix busboy + GCF race condition
1 parent e30436f commit abc259c

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

functions/http/index.js

+26-8
Original file line numberDiff line numberDiff line change
@@ -166,24 +166,42 @@ exports.uploadFile = (req, res) => {
166166
fields[fieldname] = val;
167167
});
168168

169+
let fileWrites = [];
170+
169171
// This code will process each file uploaded.
170172
busboy.on('file', (fieldname, file, filename) => {
171173
// Note: os.tmpdir() points to an in-memory file system on GCF
172174
// Thus, any files in it must fit in the instance's memory.
173175
console.log(`Processed file ${filename}`);
174176
const filepath = path.join(tmpdir, filename);
175177
uploads[fieldname] = filepath;
176-
file.pipe(fs.createWriteStream(filepath));
178+
179+
const writeStream = fs.createWriteStream(filepath);
180+
file.pipe(writeStream);
181+
182+
// File was processed by Busboy; wait for it to be written to disk.
183+
const promise = new Promise((resolve, reject) => {
184+
file.on('end', () => {
185+
writeStream.end();
186+
});
187+
writeStream.on('finish', resolve);
188+
writeStream.on('error', reject);
189+
});
190+
fileWrites.push(promise);
177191
});
178192

179-
// This event will be triggered after all uploaded files are saved.
193+
// Triggered once all uploaded files are processed by Busboy.
194+
// We still need to wait for the disk writes (saves) to complete.
180195
busboy.on('finish', () => {
181-
// TODO(developer): Process uploaded files here
182-
for (const name in uploads) {
183-
const file = uploads[name];
184-
fs.unlinkSync(file);
185-
}
186-
res.send();
196+
Promise.all(fileWrites)
197+
.then(() => {
198+
// TODO(developer): Process saved files here
199+
for (const name in uploads) {
200+
const file = uploads[name];
201+
fs.unlinkSync(file);
202+
}
203+
res.send();
204+
});
187205
});
188206

189207
busboy.end(req.rawBody);

0 commit comments

Comments
 (0)