Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Cloud Functions: How do you share source code?

I have a Node server and multiple controllers that perform DB operations and helpers (For e-mail, for example) within that directory.

I'd like to use source from that directory within my functions. Assuming the following directory structure:

src/
  server/
    /app/controllers/email_helper.js
  fns/
    send-confirm/

What's the best way to use email_helper within the send-confirm function?

I've tried:

  • Symbolically linking the 'server' directory
  • Adding a local repo to send-confirm/package.json

Neither of the above work.

like image 631
haberdasher Avatar asked Mar 16 '17 18:03

haberdasher


People also ask

How do I give permission to Cloud Functions?

Access control for users Cloud Functions supports the basic roles of Editor, Owner, and Viewer, which give the following permissions: Editor and Owner: Read and write access to all functions-related resources. Lets users deploy, update, and delete functions. Additional access to other resources in the project.

What language would you like to use to write Cloud Functions?

Cloud Functions current supports the Node. js 8 JavaScript Runtime, and the Node. js 10 Engine is in beta. You can choose a Node version it from your package.

How do I access secrets from cloud function?

Click Security to open the security tab. Click Reference a secret to set a secret for the function. Select the secret to make accessible. If you need to create a secret, see Creating and accessing secrets.

What is the difference between on call and on request function?

onRequest creates a standard API endpoint, and you'll use whatever methods your client-side code normally uses to make. HTTP requests to interact with them. onCall creates a callable. Once you get used to them, onCall is less effort to write, but you don't have all the flexibility you might be used to.


1 Answers

In principle, your Cloud Functions can use any other Node.js module, the same way any standard Node.js server would. However, since Cloud Functions needs to build your module in the cloud, it needs to be able to locate those dependency modules from the cloud. This is where the issue lies.

Cloud Functions can load modules from any one of these places:

  • Any public npm repository.
  • Any web-visible URL.
  • Anywhere in the functions/ directory that firebase init generates for you, and which gets uploaded on firebase deploy.

In your case, from the perspective of functions/package.json, the ../server/ directory doesn't fall under any of those categories, and so Cloud Functions can't use your module. Unfortunately, firebase deploy doesn't follow symlinks, which is why that solution doesn't work.

I see two possible immediate fixes:

  • Move your server/ directory to be under functions/. I realize this isn't the prettiest directory layout, but it's the easiest fix while hacking. In functions/package.json you can then have a local dependency on ./server.
  • Expose your code behind a URL somewhere. For example, you could package up a .tar and put that on Google Drive, or on Firebase Cloud Storage. Alternatively, you can use a public git repository.

In the future, I'd love it if firebase deploy followed symlinks. I've filed a feature request for that in Firebase's internal bug tracker.

like image 116
Robert-Jan Huijsman Avatar answered Oct 10 '22 23:10

Robert-Jan Huijsman