twilio-labs / serverless-toolkit

CLI tool to develop, debug and deploy Twilio Functions

Home Page:https://www.twilio.com/docs/labs/serverless-toolkit

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issues with Runtime.getAssets() returning empty

asfo opened this issue · comments

Hello guys, I just did the update to the latest version of everything related to Twilio Functions, but seems like something is broken or I'm missing something:

I have the .twilioserverlessrc file with the following settings:

{
  "commands": {},
  "environments": {},
  "projects": {},
  "assets": true,
  "functions": true,
  "live": true,
  "port": "9000",
  "runtime": "node12",
}

And if I start my project, it returns the assets available:

│   Twilio assets available:                                                                     │
│   ├── /phrases.json | http://localhost:9000/phrases.json                 │
│   └── /twilio-logo.png | http://localhost:9000/twilio-logo.png       │

The problem is that if I add the code to get that asset, the function returns empty.

console.log(Runtime.getAssets())

Returns: {} and it should return the array with the assets.

So I can't access that asset inside the code, but if I open the local URL it works and it is detected.

Thanks in advance.

Hello! So, it looks like currently you have phrases.json and twilio-logo.png set as public Assets, so they won't be returned when you run Runtime.getAssets (which only returns private Assets).

In order to make them private, and accessible via Runtime.getAssets, you'd want to modify their names to instead be phrases.private.json and twilio-logo.private.png, respectively. If you rename those files and then start your project, you should see something along these lines in your terminal now:

Twilio assets available:
└── [private] /phrases.json | Runtime.getAssets()['/phrases.json']

I'm the docs maintainer for Runtime, so please let me know if there's anything I could do with the structure of the docs to help make this clearer! 😃

Hello! So, it looks like currently you have phrases.json and twilio-logo.png set as public Assets, so they won't be returned when you run Runtime.getAssets (which only returns private Assets).

In order to make them private, and accessible via Runtime.getAssets, you'd want to modify their names to instead be phrases.private.json and twilio-logo.private.png, respectively. If you rename those files and then start your project, you should see something along these lines in your terminal now:

Twilio assets available:
└── [private] /phrases.json | Runtime.getAssets()['/phrases.json']

I'm the docs maintainer for Runtime, so please let me know if there's anything I could do with the structure of the docs to help make this clearer! 😃

Oh, I see!

I was checking against the code example and I read that about "privates" but I was like "🤔 it should be private if it's in my assets folder..." since I thought I had to use the twilio assets command to create a public bucket or something like that. So you're totally right!

Just maybe link the "private" word to another section on the doc itself and include something like this (I just found it):
image

It could be easier :) than just watch the source and try to understand what "private" in this case means

Thank you for your help!

Good point! I can work on some of the internal linking for sure to help future devs out! 😄

As far as the assets folder goes, yes those files are "privately" on your local machine, but once you deploy the project to Twilio (twilio serverless:deploy), their visibility levels will be determined by the file names (and the same for any Functions under functions).

Ex. a plain phrases.json file would be accessible by any machine at example-1234.twil.io/phrases.json, phrases.protected.json would be accessible as well but 403 unless the request has a specially encoded x-twilio-signature header, and phrases.private.json is actually bundled up with your deployed code and only able to be imported with the Runtime.getAssets helper, with no access from the wider web 😀

I hope that helps, and I'll add some todos to refine those explanations :)

Good point! I can work on some of the internal linking for sure to help future devs out! 😄

As far as the assets folder goes, yes those files are "privately" on your local machine, but once you deploy the project to Twilio (twilio serverless:deploy), their visibility levels will be determined by the file names (and the same for any Functions under functions).

Ex. a plain phrases.json file would be accessible by any machine at example-1234.twil.io/phrases.json, phrases.protected.json would be accessible as well but 403 unless the request has a specially encoded x-twilio-signature header, and phrases.private.json is actually bundled up with your deployed code and only able to be imported with the Runtime.getAssets helper, with no access from the wider web 😀

I hope that helps, and I'll add some todos to refine those explanations :)

Perfect!, I really appreciate your help.

I close the issue now 😄

Hi @asfo ! I tossed around a few revisions and landed on this new format which should help emphasize the naming conventions <-> visibility relationship. The Runtime Client getAssets doc already mentioned that it only returns private assets, but I've added some crosslinking to make it easier to find that explanation!

Hi @asfo ! I tossed around a few revisions and landed on this new format which should help emphasize the naming conventions <-> visibility relationship. The Runtime Client getAssets doc already mentioned that it only returns private assets, but I've added some crosslinking to make it easier to find that explanation!

Amazing! I really appreciate it!. Thanks!