Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion Node-1st-gen/delete-unused-accounts-cron/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
'use strict';

const functions = require('firebase-functions/v1');
const { onSchedule } = require("firebase-functions/v2/scheduler");

const admin = require('firebase-admin');
admin.initializeApp();
const PromisePool = require('es6-promise-pool').default;
Expand All @@ -26,7 +28,8 @@ const MAX_CONCURRENT = 3;
* Run once a day at midnight, to cleanup the users
* Manually run the task here https://console.cloud.google.com/cloudscheduler
*/
exports.accountcleanup = functions.pubsub.schedule('every day 00:00').onRun(async context => {
exports.accountcleanup = onSchedule('every day 00:00', async (event) => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Doesn't this work with {context}?


// Fetch all user details.
const inactiveUsers = await getInactiveUsers();

Expand Down
6 changes: 4 additions & 2 deletions Node-1st-gen/presence-firestore/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

// [START presence_sync_function]
const functions = require('firebase-functions/v1');
const { onValueUpdated } = require("firebase-functions/v2/database");

const admin = require('firebase-admin');
admin.initializeApp();

Expand All @@ -26,8 +28,8 @@ const firestore = admin.firestore();

// Create a new function which is triggered on changes to /status/{uid}
// Note: This is a Realtime Database trigger, *not* Firestore.
exports.onUserStatusChanged = functions.database.ref('/status/{uid}').onUpdate(
async (change, context) => {
exports.onUserStatusChanged = onValueUpdated('/status/{uid}', async ({ change, context }) => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

In Cloud Functions v2, the trigger handler receives a single event object. For Realtime Database triggers, the Change object is available in the data property of this event. The current destructuring will result in change and context being undefined, causing the function to fail when accessing change.after or context.params.

Suggested change
exports.onUserStatusChanged = onValueUpdated('/status/{uid}', async ({ change, context }) => {
exports.onUserStatusChanged = onValueUpdated('/status/{uid}', async ({ data: change, ...context }) => {


// Get the data written to Realtime Database
const eventStatus = change.after.val();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/

const {functions} = require("firebase-functions/v1");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here and elsewhere, isn't functions no longer needed?

const { beforeUserCreated, beforeUserSignedIn } = require("firebase-functions/v2/identity");

const {admin} = require("firebase-admin");

admin.initializeApp();
Expand All @@ -23,9 +25,8 @@ const db = admin.firestore();
// [START v1ValidateNewUser]
// [START v1beforeCreateFunctionTrigger]
// Block account creation with any non-acme email address.
exports.validateNewUser = functions.auth
.user()
.beforeCreate((user, context) => {
exports.validateNewUser = beforeUserCreated(({ data: user }) => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should just be {user} right?


// [END v1beforeCreateFunctionTrigger]
// [START v1readEmailData]
// Email passed from the User object.
Expand All @@ -48,9 +49,8 @@ exports.validateNewUser = functions.auth
// [START v1CheckForBan]
// [START v1beforeSignInFunctionTrigger]
// Block account sign in with any banned account.
exports.checkForBan = functions.auth
.user()
.beforeSignIn(async (user, context) => {
exports.checkForBan = beforeUserSignedIn(async ({ data: user }) => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Again {user}


// [END v1beforeSignInFunctionTrigger]
// [START v1readEmailData]
// Email passed from the User object.
Expand Down
11 changes: 8 additions & 3 deletions Node-1st-gen/quickstarts/pubsub-helloworld/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

// [START import]
const functions = require('firebase-functions/v1');
const { onMessagePublished } = require("firebase-functions/v2/pubsub");

// [END import]

// [START helloWorld]
Expand All @@ -25,7 +27,8 @@ const functions = require('firebase-functions/v1');
* topic.
*/
// [START trigger]
exports.helloPubSub = functions.pubsub.topic('topic-name').onPublish((message) => {
exports.helloPubSub = onMessagePublished('topic-name', ({ message, context }) => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

In Cloud Functions v2, the Pub/Sub handler receives a single event object. The message data is located at event.data.message. The current destructuring will result in message being undefined.

Suggested change
exports.helloPubSub = onMessagePublished('topic-name', ({ message, context }) => {
exports.helloPubSub = onMessagePublished('topic-name', ({ data: { message } }) => {


// [END trigger]
// [START readBase64]
// Decode the PubSub Message body.
Expand All @@ -41,7 +44,8 @@ exports.helloPubSub = functions.pubsub.topic('topic-name').onPublish((message) =
* Cloud Function to be triggered by Pub/Sub that logs a message using the data published to the
* topic as JSON.
*/
exports.helloPubSubJson = functions.pubsub.topic('another-topic-name').onPublish((message) => {
exports.helloPubSubJson = onMessagePublished('another-topic-name', ({ message, context }) => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

In Cloud Functions v2, the Pub/Sub handler receives a single event object. The message data is located at event.data.message. Additionally, the v2 Message object no longer includes the .json helper property found in v1. You will need to parse the JSON from message.data manually (e.g., JSON.parse(Buffer.from(message.data, 'base64').toString())) in the function body.

Suggested change
exports.helloPubSubJson = onMessagePublished('another-topic-name', ({ message, context }) => {
exports.helloPubSubJson = onMessagePublished('another-topic-name', ({ data: { message } }) => {


// [START readJson]
// Get the `name` attribute of the PubSub message JSON body.
let name = null;
Expand All @@ -60,7 +64,8 @@ exports.helloPubSubJson = functions.pubsub.topic('another-topic-name').onPublish
* Cloud Function to be triggered by Pub/Sub that logs a message using the data attributes
* published to the topic.
*/
exports.helloPubSubAttributes = functions.pubsub.topic('yet-another-topic-name').onPublish((message) => {
exports.helloPubSubAttributes = onMessagePublished('yet-another-topic-name', ({ message, context }) => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

In Cloud Functions v2, the Pub/Sub handler receives a single event object. The message data is located at event.data.message.

Suggested change
exports.helloPubSubAttributes = onMessagePublished('yet-another-topic-name', ({ message, context }) => {
exports.helloPubSubAttributes = onMessagePublished('yet-another-topic-name', ({ data: { message } }) => {


// [START readAttributes]
// Get the `name` attribute of the message.
const name = message.attributes.name;
Expand Down
5 changes: 4 additions & 1 deletion Node-1st-gen/quickstarts/thumbnails/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

// [START import]
const functions = require('firebase-functions/v1');
const { onObjectFinalized } = require("firebase-functions/v2/storage");

const admin = require('firebase-admin');
admin.initializeApp()
const path = require('path');
Expand All @@ -31,7 +33,8 @@ const sharp = require('sharp');
* generate a thumbnail automatically using sharp.
*/
// [START generateThumbnailTrigger]
exports.firstGenGenerateThumbnail = functions.storage.object().onFinalize(async (object) => {
exports.firstGenGenerateThumbnail = onObjectFinalized(async ({ object, context }) => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

In Cloud Functions v2, the Storage handler receives a single event object. The object metadata is located in the data property of this event.

Suggested change
exports.firstGenGenerateThumbnail = onObjectFinalized(async ({ object, context }) => {
exports.firstGenGenerateThumbnail = onObjectFinalized(async ({ data: object }) => {


// [END generateThumbnailTrigger]
// [START eventAttributes]
const fileBucket = object.bucket; // The Storage bucket that contains the file.
Expand Down
11 changes: 7 additions & 4 deletions Node-1st-gen/quickstarts/uppercase-firestore/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
// [START import]
// The Cloud Functions for Firebase SDK to create Cloud Functions and set up triggers.
const functions = require('firebase-functions/v1');
const { onRequest } = require("firebase-functions/v2/https");
const { onDocumentCreated } = require("firebase-functions/v2/firestore");


// The Firebase Admin SDK to access Firestore.
const admin = require("firebase-admin");
Expand All @@ -29,7 +32,8 @@ admin.initializeApp();
// Take the text parameter passed to this HTTP endpoint and insert it into
// Firestore under the path /messages/:documentId/original
// [START addMessageTrigger]
exports.addMessage = functions.https.onRequest(async (req, res) => {
exports.addMessage = onRequest(async (req, res) => {

// [END addMessageTrigger]
// Grab the text parameter.
const original = req.query.text;
Expand All @@ -49,9 +53,8 @@ exports.addMessage = functions.https.onRequest(async (req, res) => {
// Listens for new messages added to /messages/:documentId/original and creates an
// uppercase version of the message to /messages/:documentId/uppercase
// [START makeUppercaseTrigger]
exports.makeUppercase = functions.firestore
.document("/messages/{documentId}")
.onCreate((snap, context) => {
exports.makeUppercase = onDocumentCreated("/messages/{documentId}", ({ snapshot: snap, context }) => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

In Cloud Functions v2, the Firestore handler receives a single event object. For onDocumentCreated, the document snapshot is available in the data property. The current destructuring will cause the function to fail when accessing snap.data() or context.params.

Suggested change
exports.makeUppercase = onDocumentCreated("/messages/{documentId}", ({ snapshot: snap, context }) => {
exports.makeUppercase = onDocumentCreated("/messages/{documentId}", ({ data: snap, ...context }) => {


// [END makeUppercaseTrigger]
// [START makeUppercaseBody]
// Grab the current value of what was written to Firestore.
Expand Down
10 changes: 7 additions & 3 deletions Node-1st-gen/quickstarts/uppercase-rtdb/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
// [START import]
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions/v1');
const { onRequest } = require("firebase-functions/v2/https");
const { onValueCreated } = require("firebase-functions/v2/database");


// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
Expand All @@ -29,7 +32,8 @@ admin.initializeApp();
// Take the text parameter passed to this HTTP endpoint and insert it into the
// Realtime Database under the path /messages/:pushId/original
// [START addMessageTrigger]
exports.addMessage = functions.https.onRequest(async (req, res) => {
exports.addMessage = onRequest(async (req, res) => {

// [END addMessageTrigger]
// Grab the text parameter.
const original = req.query.text;
Expand All @@ -45,8 +49,8 @@ exports.addMessage = functions.https.onRequest(async (req, res) => {
// [START makeUppercase]
// Listens for new messages added to /messages/:pushId/original and creates an
// uppercase version of the message to /messages/:pushId/uppercase
exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
.onCreate((snapshot, context) => {
exports.makeUppercase = onValueCreated('/messages/{pushId}/original', ({ snapshot, context }) => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

In Cloud Functions v2, the Realtime Database handler receives a single event object. The data snapshot is available in the data property. The current destructuring will cause the function to fail when accessing snapshot.val() or context.params.

Suggested change
exports.makeUppercase = onValueCreated('/messages/{pushId}/original', ({ snapshot, context }) => {
exports.makeUppercase = onValueCreated('/messages/{pushId}/original', ({ data: snapshot, ...context }) => {


// Grab the current value of what was written to the Realtime Database.
const original = snapshot.val();
functions.logger.log('Uppercasing', context.params.pushId, original);
Expand Down
5 changes: 4 additions & 1 deletion Node-1st-gen/remote-config-diff/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
*/

const functions = require('firebase-functions/v1');
const { onConfigUpdated } = require("firebase-functions/v2/remoteConfig");

const admin = require('firebase-admin');
const jsonDiff = require('json-diff');

admin.initializeApp();

// [START remote_config_function]
exports.showConfigDiff = functions.remoteConfig.onUpdate(versionMetadata => {
exports.showConfigDiff = onConfigUpdated(({ version: versionMetadata, context }) => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

In Cloud Functions v2, the Remote Config handler receives a single event object. The version metadata is available in the data property. The current destructuring will result in versionMetadata being undefined.

Suggested change
exports.showConfigDiff = onConfigUpdated(({ version: versionMetadata, context }) => {
exports.showConfigDiff = onConfigUpdated(({ data: versionMetadata }) => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is actually correct I assume (renaming the parameter) and is the kind of neat thing using a skill gives us


return admin.credential.applicationDefault().getAccessToken()
.then(accessTokenObj => {
return accessTokenObj.access_token;
Expand Down
Loading