UNPKG

actionhero

Version:

actionhero.js is a multi-transport API Server with integrated cluster capabilities and delayed tasks

155 lines (154 loc) 6.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SpecHelper = void 0; const uuid = require("uuid"); const index_1 = require("../index"); /** * A special "mock" server which enables you to test actions and tasks in a simple way. Only available in the TEST environment. */ class SpecHelper extends index_1.Initializer { constructor() { super(); this.name = "specHelper"; this.loadPriority = 900; this.startPriority = 901; this.enabled = false; } async initialize(config) { if (index_1.env === "test" || String(process.env.SPECHELPER) === "true") { this.enabled = true; } if (!this.enabled) { return; } class TestServer extends index_1.Server { constructor() { super(); this.type = "testServer"; this.attributes = { canChat: true, logConnections: false, logExits: false, sendWelcomeMessage: true, verbs: index_1.api.connections.allowedVerbs, }; } async initialize() { } async start() { index_1.log("loading the testServer", "info"); this.on("connection", (connection) => { this.handleConnection(connection); }); this.on("actionComplete", (data) => { this.actionComplete(data); }); } async stop() { } async sendMessage(connection, message, messageId) { process.nextTick(() => { connection.messages.push(message); if (typeof connection.actionCallbacks[messageId] === "function") { connection.actionCallbacks[messageId](message, connection); delete connection.actionCallbacks[messageId]; } }); } async sendFile(connection, error, fileStream, mime, length) { let content = ""; const messageId = connection.messageId; const response = { content: null, mime: mime, length: length, error: undefined, }; if (error) { response.error = error; } try { if (!error) { fileStream.on("data", (d) => { content += d; }); fileStream.on("end", () => { response.content = content; this.sendMessage(connection, response, messageId); }); } else { this.sendMessage(connection, response, messageId); } } catch (e) { this.log(e, "warning"); this.sendMessage(connection, response, messageId); } } handleConnection(connection) { connection.messages = []; connection.actionCallbacks = {}; } async actionComplete(data) { if (typeof data.response === "string" || Array.isArray(data.response)) { if (data.response.error) { data.response = await config.errors.serializers.servers.specHelper(data.response.error); } } else { if (data.response.error) { data.response.error = await config.errors.serializers.servers.specHelper(data.response.error); } if (index_1.api.specHelper.returnMetadata) { data.response.messageId = data.messageId; data.response.serverInformation = { serverName: config.general.serverName, apiVersion: config.general.apiVersion, }; data.response.requesterInformation = { id: data.connection.id, remoteIP: data.connection.remoteIP, receivedParams: {}, }; for (const k in data.params) { data.response.requesterInformation.receivedParams[k] = data.params[k]; } } } if (data.toRender === true) { this.sendMessage(data.connection, data.response, data.messageId); } } } index_1.api.specHelper = { returnMetadata: true, Server: TestServer, }; /** * A special connection usable in tests. Create via `await api.specHelper.Connection.createAsync()` */ index_1.api.specHelper.Connection = class { static async createAsync(data) { const id = uuid.v4(); await index_1.api.servers.servers.testServer.buildConnection({ id: id, fingerprint: id, rawConnection: {}, remoteAddress: "testServer", remotePort: 0, }); return index_1.api.connections.connections[id]; } }; } async start() { if (!this.enabled) { return; } const server = new index_1.api.specHelper.Server(); server.config = { enabled: true }; await server.start(index_1.api); index_1.api.servers.servers.testServer = server; } } exports.SpecHelper = SpecHelper;