twilio-labs / runtime-helpers Goto Github PK
View Code? Open in Web Editor NEWA set of common utility code for use in Twilio Functions.
License: MIT License
A set of common utility code for use in Twilio Functions.
License: MIT License
Today a customer might be writing some unintentionally "leaky" code where they call callback(...)
before all asynchronous operations they started have completed. This could be because they ran a method that returns a Promise and they haven't awaited it or they ran a function that receives it's own callback method and they haven't adjusted their code accordingly.
As a result this can create unintended behavior in a variety of ways. Some of these operations might only complete on subsequent executions of the Function and in other cases the result might not be received or the logging will be off.
A very basic example for testing:
exports.handler = function (context, event, callback) {
console.log('1')
setTimeout(() => {
console.log('3');
}, 1000);
console.log('2');
callback(null, {});
};
A customer should have to refactor this code by moving the callback(null, {})
into the setTimeout
after console.log('3')
in order to make sure all things get appropriately logged.
Create a higher order function that can be used to be warned about any unfinished asynchronous code in the debugger:
const { warnOnUnfinishedCode } = require('@twilio-labs/runtime-helpers');
exports.handler = warnOnUnfinishedCode(function (context, event, callback) {
console.log('1')
setTimeout(() => {
console.log('3');
}, 1000);
console.log('2');
callback(null, {});
});
In this case the library would log a warning to the debugger (using console.warn
) to inform the customer that on line 5 they created a timeout that has not yet completed and that the callback(null, {})
needs to be moved into it.
Once we've proven the usefulness of this and it's stability we could decide to directly offer this as part of the Runtime Handler for all customers. In the meantime this should help customers (and support) to debug those issues.
In order to detect those open handles we need to use async_hooks
(see documentation) to track init
and destroy
events, filter out any that are not relevant to customers (either because they are Node.js or Functions internal) and at the end of the callback execution trigger a warning if any async events were created in init
but never destroyed in destroy
.
Each event in the init
should come with a type for better filtering and we should be able to use Error stacks (i.e. create an error object and inspect the error.stack
) to determine the line that the async handle was created on.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.