1

Goal

I would like a specific JavaScript file to be referenced by multiple JavaScript Azure Functions that are managed by different users in different Azure subscriptions. All the subscriptions are within an enterprise subscription. I would also like these multiple users to be able to alter this single-sourced file.

Proposed solution

My best guess on how to accomplish this is to store the file in a public GitHub repository and have each Function reference it.

Problem

Referencing an external script (in my case, the script hosted in GitHub) in standard ways (as I understand them) does not seem to work when implemented in a JavaScript Azure Function.

Code

Structure:

var abc = require('<external reference>');

Sample external JS file:

https://github.com/<username>/<repo>/blob/master/file.js

Failed attempts

There seem to be many outdated methods documented on SO (e.g., rawgit) but I am looking at the answers last edited in 2018.

  • https://github.com/<username>/<repo>/blob/master/file.js
  • https://raw.githubusercontent.com/<username>/<repo>/master/file.js
  • http://cdn.jsdelivr.net/gh/<username>/<repo>/file.js

Alternatives

I know that in the case that multiple Functions within a single Azure Function (and therefore within the same Azure subscription) can reference a centrally managed file by:

  • Placing it in a Shared folder, adding "watchDirectories": [ "Shared" ] to host.json, and referencing it with require('../Shared/file.js')
  • Referencing code in one Function from another (e.g., in Function-2 I can use require('../Function-1/file.js'))

However, unless I misunderstand, neither meets the requirements that the Functions can reside in different Functions in different Azure subscriptions.

I am new to JS. Thank you for any guidance. There may be other ways to satisfy these requirements that I am not thinking of.

1
  • According to the rawgit website, it's no longer available. Commented Dec 4, 2019 at 14:11

1 Answer 1

2

You won't be able to require the js files in the typical sense from inside a function, as require uses the filesystem to resolve files, which wouldn't really exist inside of a function. If the js you want to run is available on github you could download the source from github. And use the JS Function constructor to run the create a function you can call. Very roughly it might look something like:

let myfunc;
fetchCodeToRun().then(codeString=>myfunc=new Function(codeString)); //untested

Please note that I'm not specifically too familiar with Azure, and I only have a tiny amount of experience with serverless infrastructure so it's very possible that there's a better way of accomplishing this.

Function constructor docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.