From ca9151d2b107236a92a81d23f8f2a35ce61d064d Mon Sep 17 00:00:00 2001 From: Fernando Cappi Date: Sat, 12 May 2018 00:15:11 -0300 Subject: [PATCH] Update code to work with the current google cloud function structure --- functions/gcs/index.js | 24 +++++++++--------------- functions/gcs/test/index.test.js | 14 +++++--------- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/functions/gcs/index.js b/functions/gcs/index.js index e2424591f8..29d71df125 100644 --- a/functions/gcs/index.js +++ b/functions/gcs/index.js @@ -39,22 +39,16 @@ function getFileStream (file) { * Reads file and responds with the number of words in the file. * * @example - * gcloud alpha functions call wordCount --data '{"bucket":"YOUR_BUCKET_NAME","name":"sample.txt"}' + * gcloud functions call wordCount --data '{"bucket":"YOUR_BUCKET_NAME","name":"sample.txt"}' * - * @param {object} event The Cloud Functions event. - * @param {object} event.data A Google Cloud Storage File object. - * @param {string} event.data.bucket Name of a Cloud Storage bucket. - * @param {string} event.data.name Name of a file in the Cloud Storage bucket. - * @param {function} callback The callback function. + * @param {Object} req Cloud Function request context. + * @param {object} req.body A Google Cloud Storage File object. + * @param {string} req.body.bucket Name of a Cloud Storage bucket. + * @param {string} req.body.name Name of a file in the Cloud Storage bucket. + * @param {Object} res Cloud Function response context. */ -exports.wordCount = (event, callback) => { - const file = event.data; - - if (file.resourceState === 'not_exists') { - // This is a file deletion event, so skip it - callback(); - return; - } +exports.wordCount = (req, res) => { + const file = req.body; let count = 0; const options = { @@ -67,7 +61,7 @@ exports.wordCount = (event, callback) => { count += line.trim().split(/\s+/).length; }) .on('close', () => { - callback(null, `File ${file.name} has ${count} words`); + res.send(`File ${file.name} has ${count} words`); }); }; // [END functions_word_count_read] diff --git a/functions/gcs/test/index.test.js b/functions/gcs/test/index.test.js index 16ee43d934..f22839e2af 100644 --- a/functions/gcs/test/index.test.js +++ b/functions/gcs/test/index.test.js @@ -53,7 +53,7 @@ test.serial(`Fails without a bucket`, (t) => { const expectedMsg = `Bucket not provided. Make sure you have a "bucket" property in your request`; t.throws( - () => getSample().program.wordCount({ data: { name: `file` } }), + () => getSample().program.wordCount({ name: `file` }), Error, expectedMsg ); @@ -63,7 +63,7 @@ test.serial(`Fails without a file`, (t) => { const expectedMsg = `Filename not provided. Make sure you have a "file" property in your request`; t.throws( - () => getSample().program.wordCount({ data: { bucket: `bucket` } }), + () => getSample().program.wordCount({ bucket: `bucket` }), Error, expectedMsg ); @@ -71,9 +71,7 @@ test.serial(`Fails without a file`, (t) => { test.cb.serial(`Does nothing for deleted files`, (t) => { const event = { - data: { - resourceState: `not_exists` - } + resourceState: `not_exists` }; const sample = getSample(); @@ -89,10 +87,8 @@ test.cb.serial(`Does nothing for deleted files`, (t) => { test.cb.serial(`Reads the file line by line`, (t) => { const expectedMsg = `File ${filename} has 114 words`; const event = { - data: { - bucket: `bucket`, - name: `sample.txt` - } + bucket: `bucket`, + name: `sample.txt` }; const sample = getSample();