Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify comments + add field handler to multipart sample #609

Merged
merged 3 commits into from
May 24, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions functions/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,31 @@ exports.uploadFile = (req, res) => {
if (req.method === 'POST') {
const busboy = new Busboy({ headers: req.headers });

// This object will accumulate all the fields, keyed by their name
const fields = {};

// This object will accumulate all the uploaded files, keyed by their name.
const uploads = {};
const tmpdir = os.tmpdir();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add an empty line before:
const tmpdir = os.tmpdir();

Otherwise it looks like the comment "This object will accumulate all the uploaded files, keyed by their name" is for the two lines.

You can group this "const tmpdir" together with "const busboy"


// This callback will be invoked for each file uploaded.
// This event will be triggered for each non-file field in the form.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit afraid that users may confuse events described above, with event that trigger a function. For instance, users may think that there are many function executions here.

I think the previous form ("This callback will be invoked") was a bit better

Also, busboy docs say that events are emitted, not triggered.

How about something like this:

  • "This code is executed on..."
  • "This code handles busboy events emitted on..."
  • "This busboy events are emitted on..."

busboy.on('field', (fieldname, val) => {
// TODO(developer): Process submitted field values here
console.log(`Processed field ${fieldname}: ${val}.`);
fields[fieldname] = val;
});

// This event will be triggered for 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));
});

// This callback will be invoked after all uploaded files are saved.
// This event will be triggered after all uploaded files are saved.
busboy.on('finish', () => {
// TODO(developer): Process uploaded files here
for (const name in uploads) {
Expand Down