@contrast/core
IOC container for Contrast agent services. core itself provides only the
container, an in-process message bus, and perf instrumentation. Every other
service (config, logger, patcher, depHooks, rewriter, ...) is a
component that attaches itself to the core instance at startup.
Usage
@contrast/agent creates the single instance in agent/lib/start-agent.js:
const { Core } = require('@contrast/core');
const core = new Core({ agentName, agentVersion, nodeEngines, npmVersionRange, startupValidation });
Class-based components
New components extend ComponentBase and declare their name with
kComponentName. Constructing one sets it on core at that name (dotted names
nest, e.g. 'cveShield.rules') and then calls its init().
const { ComponentBase, kComponentName } = require('@contrast/core');
class MyComponent extends ComponentBase {
static [kComponentName] = 'myComponent';
init() {
const { config, logger } = this.core;
}
async install() {
// called when @contrast/agentify reaches 'myComponent' in its installOrder
}
}
core.initComponentSync(MyComponent);
Older packages export a factory instead, require('@contrast/assess')(core),
or are built with Core.makeComponent({ name, factory }).
Messages
core.messages is an EventEmitter for communication between components. Event
names are the Event enum from @contrast/common.
const { Event } = require('@contrast/common');
core.messages.on(Event.SERVER_SETTINGS_UPDATE, (settings) => { /* ... */ });
Related
@contrast/agentify: composes components and installs them in order. See more here.