@darlean/webgateways-suite
Version:
Web Gateways Suite that acts as Web/API Gateway that invokes actors to serve HTTP requests
122 lines (121 loc) • 5.86 kB
JavaScript
;
/**
* Suite that provides the WebGateways service that makes it possible to have actors handle HTTP requests.
*
* @packageDocumentation
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createWebGatewaysSuiteFromConfig = exports.createWebGatewaysSuite = exports.WEBGATEWAY_HOST_ACTOR = void 0;
const base_1 = require("@darlean/base");
const actor_impl_1 = require("./actor.impl");
exports.WEBGATEWAY_HOST_ACTOR = 'io.darlean.WebGatewayHostActor';
__exportStar(require("./actor.impl"), exports);
__exportStar(require("./intf"), exports);
// Set to 2.5 minutes, as caddy has a default of 2 minutes and nginx 75 seconds
// It is expected that darlean runs behind such a proxy so this is a reasonable default.
const DEFAULT_KEEPALIVETIMEOUT = 150 * 1000;
function createWebGatewaysSuite(config, appId) {
const startActions = [];
for (const host of config.gateways ?? []) {
startActions.push({ name: `Web Gateway ${host.id ?? 'default'}`, id: [host.id ?? 'default', appId], action: 'touch' });
}
return new base_1.ActorSuite([
{
type: exports.WEBGATEWAY_HOST_ACTOR,
kind: 'singular',
placement: {
version: '20230202',
bindIdx: -1
},
creator: (context) => {
const id = context.id[0];
const gatewaycfg = config.gateways?.find((x) => x.id === id);
if (gatewaycfg) {
const cfg = {
name: gatewaycfg.id ?? 'default',
port: gatewaycfg.port ?? 80,
handlers: [],
keepAliveTimeout: gatewaycfg.keepAliveTimeout ?? config.keepAliveTimeout ?? DEFAULT_KEEPALIVETIMEOUT,
maxContentLength: gatewaycfg.maxContentLength
};
for (const handler of gatewaycfg.handlers ?? []) {
const actorType = handler.actorType ?? gatewaycfg.actorType;
const actionName = handler.actionName ?? gatewaycfg.actionName;
if (!handler.flow) {
if (!actorType) {
throw new Error(`No actor type configured for Web Gateway handler`);
}
if (!actionName) {
throw new Error('No action name configured for WebGateway handler');
}
}
const actorId = handler.actorId ?? gatewaycfg.actorId ?? [];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (handler.placeholders) {
throw new Error('Placeholders are not supported anymore in web gateways');
}
const h = {
method: handler.method,
path: handler.path,
action: async (req) => {
if (actorType && actionName) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const actor = context.portal.retrieve(actorType, actorId);
return await actor[actionName](req);
}
},
flow: handler.flow,
maxContentLength: handler.maxContentLength
};
cfg.handlers?.push(h);
}
return new actor_impl_1.WebGatewayActor(cfg);
}
else
throw new Error(`Host [${id}] is not configured`);
},
startActions
}
]);
}
exports.createWebGatewaysSuite = createWebGatewaysSuite;
function createWebGatewaysSuiteFromConfig(config, runtimeEnabled, appId) {
if (config.fetchBoolean('enabled') ?? runtimeEnabled) {
const options = {
gateways: [],
keepAliveTimeout: config.fetchNumber('keepAliveTimeout')
};
const gateways = config.fetchRaw('gateways');
let first = true;
for (const gateway of gateways ?? []) {
const port = first ? config.fetchNumber('port') ?? gateway.port : gateway.port;
options.gateways?.push({
id: gateway.id,
port,
actorId: gateway.actorId,
actorType: gateway.actorType,
actionName: gateway.actionName,
handlers: gateway.handlers,
keepAliveTimeout: gateway.keepAliveTimeout,
maxContentLength: gateway.maxContentLength
});
first = false;
}
return createWebGatewaysSuite(options, appId);
}
}
exports.createWebGatewaysSuiteFromConfig = createWebGatewaysSuiteFromConfig;