UNPKG

actionhero

Version:

The reusable, scalable, and quick node.js API server for stateless and stateful applications

151 lines (150 loc) 5.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SpecHelper = void 0; const uuid = require("uuid"); const connection_1 = require("../classes/connection"); 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() { if (index_1.env === "test") 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: connection_1.connectionVerbs, }; } async initialize() { } async start() { (0, 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) { data.response.error; if (typeof data.response === "string" || Array.isArray(data.response)) { // nothing to do... } else { if (data.response.error) { data.response.error = await index_1.config.errors.serializers.servers.specHelper(data.response.error); } if (index_1.api.specHelper.returnMetadata) { data.response.messageId = data.messageId; data.response.serverInformation = { serverName: index_1.config.general.serverName, apiVersion: index_1.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() { 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;