Share JavaScript code between Twilio functions - twilio

I have a Twilio serverless app that contains several functions. Several of the functions have code that is similar that I have extracted out into a separate file that can be used by the loaded and used by the functions.
This works if I run things locally with twilio serverless:start, but fails when I deploy to Twilio and try using the endpoints from their. On Twilio the functions fail with the message Cannot find module '<path to module>' \nRequire stack ...
functions (two files like this):
const share = require('shared-code');
shared-code.js:
exports.helperFn = function() {}
How can I easily share JavaScript code between Twilio functions?

Twilio developer evangelist here.
You can find the path of a Function here. Then use that path in the file in which you want to reference code from another file:
let path = Runtime.getFunctions()['function-path'].path; //example: Runtime.getFunctions()['api/identity'].path;
where the Function path is the Function name after the / in the URL.
Then to use that code from a Function in a different Function, you could require this
let module = require(path);
Let me know if this helps at all!

Related

How to add extensions to Microsoft Edge

I an trying to figure out how to add webRequest extension to Microsoft Edge. Can someone provide some assistance? I have gone though a number of documents, but when I go to Microsoft online store I don't see it there.
Test code:
<html>
<script>
browser.webRequest.onBeforeRequest.addListener(
logURL,
{ urls: ["<all_urls>"] }
);
function logURL(requestDetails) {
console.log("Loading: " + requestDetails.url);
};
</script>
</html>
In the doc of webRequest in MDN, we can see that:
To use the webRequest API for a given host, an extension must have the "webRequest" API permissions and the host permission for that host.
Where can we add the permissions? The answer is the manifest.json file. It is the necessary part of an extension. You could see the Anatomy of an extension to learn the compositions of an extension.
Besides, browser.webRequest isn't in the list of content scripts APIs, so we can only use it in background scripts.
In a conclusion, we can't just use the browser.webRequest in a script of a html file. If we want to test the event browser.webRequest.onBeforeRequest, we need to have a manifest.json file, put permisssions in it:
"permissions": [
"*://learn.microsoft.com/*",
"webRequest"
]
Then put the scripts you gave in the background scripts. Then you could try to debug the extension in Edge, there will be no error. Here is an article about creating a Microsoft Edge extension, you could refer to it if you need.

Electron-mksnapshot snapshotResult.setGlobals location?

I've created a script using electron-link. Within the links are basic requires needed for electron i.e:
function get_app() {
return app = app || require("./node_modules/electron/index.js").app;
}
when running the script through electron I get the below error:
"To use Node's require you need to call snapshotResult.setGlobals first!"
I've attempted to add this line to many areas without much affect. is there a correct area to place this?
Figured it out. I needed to wrap my main.js file in a function and export that function. then I can call the function after calling the .setGlobals function.

How to call C# Web Service in Corona (Lua)?

I am trying to call a web service written in C#. Below is client side in corona:
local client = require "soap.client"
local ns, meth, ent = client.call {
url = "http://172.16.1.162:7878/BaksetBilgi.svc",
soapaction = "doubler",
method = "http://titck.kara.com/bask",
entries = {
......}
When I run this code, it gives error :
module 'soap.client' not found:resource (soap.client.lu) does not exist in archive..
How can I solve it?
That module is not part of corona. Looks like it's only 3 files, you could copy them into your Corona project. Note that the language that SOAP service is written in is irrelevant to the client: SOAP is a protocall, if you know how to format your data in HTML then you can do SOAP. Look at any good SOAP tutorial. But I don't know why you wouldn't want to use that library, just copy the files into your project.

"document" in mozilla extension js modules?

I am building Firefox extension, that creates single XMPP chat connection, that can be accessed from all tabs and windows, so I figured, that only way to to this, is to create connection in javascript module and include it on every browser window. Correct me if I am wrong...
EDIT: I am building traditional extension with xul overlays, not using sdk, and talking about those modules: https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules
So I copied Strophe.js into js module. Strophe.js uses code like this:
/*_Private_ function that creates a dummy XML DOM document to serve as
* an element and text node generator.
*/
[---]
if (document.implementation.createDocument === undefined) {
doc = this._getIEXmlDom();
doc.appendChild(doc.createElement('strophe'));
} else {
doc = document.implementation
.createDocument('jabber:client', 'strophe', null);
}
and later uses doc.createElement() to create xml(or html?) nodes.
All worked fine, but in module I got error "Error: ReferenceError: document is not defined".
How to get around this?
(Larger piece of exact code: http://pastebin.com/R64gYiKC )
Use the hiddenDOMwindow
Cu.import("resource://gre/modules/Services.jsm");
var doc = Services.appShell.hiddenDOMWindow.document;
It sounds like you might not be correctly attaching your content script to the worker page. Make sure that you're using something like tabs.attach() to attach one or more content scripts to the worker page (see documentation here).
Otherwise you may need to wait for the DOM to load, waiting for the entire page to load
window.onload = function ()
{
Javascript code goes here
}
Should take at least diagnose that issue (even if the above isn't the best method to use in production). But if I had to wager, I'd say that you're not attaching the content script.

How do I share javascript code between background and content scripts in Firefox Jetpack add-ons?

I've written a library to mimic Chrome's request/response API for my Firefox add-on. Obviously I need to use this code in both my background process (main.js) and my content script. Surely there must be a better way than stringifying the imported module.
There is a semi-documented way of getting the URLs of the SDK code modules. This involves low-level modules which aren't guaranteed to stay stable. In fact, it is very likely that this part of the SDK will change and this solution won't work any more (like the solution originally presented here).
That said, the SDK allows you to access the loader module. So first you need to construct a loader with the same options as the one used by the SDK. Then you need to use the resolveURI utility function to resolve the module name using the mapping of this loader:
var {Loader, resolveURI} = require('toolkit/loader');
var options = require('#loader/options');
var loader = Loader(options);
var fooURI = resolveURI("./foo", loader.mapping);
The code above generates the URL for the module foo. Note that you need to use the module foo somewhere, otherwise it won't be included in your extension. If you don't use it yet then a dummy function will be sufficient:
function dummy()
{
require("foo");
}
Using the resulting URL as content script works just fine:
var pageMod = require("page-mod");
pageMod.PageMod({
include: "*.google.com",
contentScriptWhen: 'end',
contentScriptFile: [fooURI, ...]
});

Resources