"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.events = exports.EventRegistry = void 0;
/**
* Contains event handler registrations for a script.
*
* Access the registry through the global `events` object.
*/
class EventRegistry {
constructor() {
this.handlers = {};
}
/**
* Registers a handler to be run when a particular kind of event occurs.
* When Brigade receives a matching event, it will run the specified
* handler.
*
* Possible event sources depend on the gateways configured in your Brigade
* system, and possible event types depend on the gateway. See the Brigade
* and gateway documentation for details.
*
* @param eventSource The event source (gateway) to register the handler for
* @param eventType The event type to register the handler for
* @param eventHandler The handler to run when an event with the given source and
* type occurs
*/
on(eventSource, eventType, eventHandler) {
this.handlers[`${eventSource}:${eventType}`] = eventHandler;
return this;
}
process() {
let event;
if (process.env.BRIGADE_EVENT_FILE) {
console.log(`Loading dummy event from file ${process.env.BRIGADE_EVENT_FILE}`);
event = require(process.env.BRIGADE_EVENT_FILE);
}
else {
console.log("No dummy event file provided");
console.log("Generating a dummy event");
let source;
let type;
if (process.env.BRIGADE_EVENT) {
let eventTokens = [];
const match = process.env.BRIGADE_EVENT.match(/^(.+?):(.+)$/);
if (match) {
eventTokens = Array.from(match);
}
if (eventTokens.length != 3) {
throw new Error(`${process.env.BRIGADE_EVENT} is not a valid event of the form <source>:<type>`);
}
source = eventTokens[1];
type = eventTokens[2];
console.log(`Using specified dummy event with source "${source}" and type "${type}"`);
}
else {
console.log("No dummy event type provided");
source = "brigade.sh/cli";
type = "exec";
console.log(`Using default dummy event with source "${source}" and type "${type}"`);
}
event = {
id: this.newUUID(),
source: source,
type: type,
project: {
id: this.newUUID(),
secrets: {}
},
worker: {
apiAddress: "https://brigade2.example.com",
apiToken: this.newUUID(),
configFilesDirectory: ".brigade",
defaultConfigFiles: {}
}
};
}
console.log("Processing the following dummy event:");
console.log(event);
this.fire(event);
}
fire(event) {
let handlerFn = this.handlers[`${event.source}:${event.type}`];
if (!handlerFn) {
handlerFn = this.handlers[`${event.source}:*`];
}
if (handlerFn) {
return handlerFn(event);
}
}
newUUID() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0, v = c == "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
}
exports.EventRegistry = EventRegistry;
/** Contains event handler registrations for a script. */
exports.events = new EventRegistry();
//# sourceMappingURL=events.js.map