Taskcluster Component Loader
This library provides a means of loading application "components", each of which can depend on other components. This makes application startup more modular and flexible. It also enables dependency injection during tests.
It is used to run all Taskcluster microservices.
Components
Each component definition specifies:
- Name of the component,
- Required components to be instantiated first, and,
- How to asynchronously load the component.
All of this is specified as properties of the components object. A server component might be defined like this:
// Definition of 'server' component, notice that components listed in // `required` are destructured in the argument to `setup`. server: required: 'port' setup: async { let server = http; await { server; server; server; }; return server; }
Loading
The loader components are all handed to this library, which returns a
load(componentname, overwrites)
function. While creating this function, the
library will also ensure that definitions are valid and that the components
form a directed acylic graph (DAG).
Calling the load
function with a component name will return the result of
that component's setup function (after recursively setting up any of the
component's requirements). All components are loaded asynchronously: a
component's setup
function may return a Promise which will be resolved before
setting up components that depend on it.
The following example creates a server, where the port number is provided by
another component. Note that the server
component's setup
method is
asynchronous, and that await
is used with the load
method invocation.
let loader = ; // Create loaderlet load = ; // Create serverlet server = await ;
Overwrites and Virtual Components
With overwrites
you can replace a component. This is particularly useful in
tests where you may want to inject a mock component, but still load the same
end result. In the example we could overwrite port
using:
// Create server overwriting the 'port' componentlet server = await ;
Finally, you can specify virtual components, for example you may wish to force
the caller of load
to always specify a port.
let loader = ; // Create loaderlet load = ; // Create express (here we're forced to specify port)let server = await ;
As a neat little treat, the load has a default target graphviz
which returns
a representation of the dependency graph in graphviz's dot format. This
representation can be rendered using the graphviz tool.
Remark the load
function doesn't have any side-effects on its own, which
means that if you call load('server')
twice you'll get two different
instantiations of the server
component and all of its dependencies. This is
particularly useful for getting a fresh component between tests.
Advice
We generally recommend one component loader per project, and that you expose
it in an executable such that you do node server.js <component>
to start a
process running the specified component. A handy way to run the loader on startup,
given both the profile ($NODE_ENV) and process (target component):
// If this file is executed launch component from first argumentif !moduleparent ;