UNPKG

@fakehost/signalr

Version:

A Fake Signalr Service for faking/mocking signalr hub services for testing, prototyping, and demoing

107 lines (103 loc) 4.14 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/createServer.ts var createServer_exports = {}; __export(createServer_exports, { createServerSignalr: () => createServerSignalr }); module.exports = __toCommonJS(createServer_exports); var import_server = require("@fakehost/fake-rest/server"); var import_exchange = require("@fakehost/exchange"); var import_url2 = require("url"); // src/restHandshakeRouter.ts var import_fake_rest = require("@fakehost/fake-rest"); var import_uuid = require("uuid"); var restRouter = (0, import_fake_rest.createRouter)().use((0, import_fake_rest.cors)()).use((_, res) => { const connectionId = (0, import_uuid.v4)(); res.json(signalrHandshake(connectionId)); }); var signalrHandshake = (connectionId) => ({ negotiateVersion: 1, connectionId, connectionToken: connectionId, availableTransports: [ { transport: "WebSockets", transferFormats: ["Text", "Binary"] }, { transport: "ServerSentEvents", transferFormats: ["Text"] }, { transport: "LongPolling", transferFormats: ["Text", "Binary"] } ] }); // src/types.ts var import_url = __toESM(require("url"), 1); var isFakeSignalrHub = (hub) => { return "constructor" in hub && "name" in hub.constructor && hub.constructor.name === "FakeSignalrHub"; }; var URL = globalThis.URL || import_url.default.URL; // src/createServer.ts var objectKeys = (x) => Object.keys(x); var createServerSignalr = async (options) => { const rest = new import_server.HttpRestService(restRouter, { name: `http://${options == null ? void 0 : options.name}`, port: options == null ? void 0 : options.port, silent: true }); const wsHost = new import_exchange.WsHost({ server: rest.server, name: `ws://${options == null ? void 0 : options.name}`, debug: options.debug }); const hubResult = objectKeys(options.hubs).reduce((acc, hubName) => { const hub = options.hubs[hubName]; if (isFakeSignalrHub(hub)) { hub.setHost(wsHost); acc[hubName] = { disconnect: () => wsHost.disconnect({ path: hub.path }) }; } return acc; }, {}); (options == null ? void 0 : options.debug) && (0, import_exchange.enableLogger)(); (options == null ? void 0 : options.debug) && (0, import_server.enableLogger)(); const hostUrl = await wsHost.url; const url = new import_url2.URL(`http://${hostUrl.hostname}${hostUrl.port ? ":" + hostUrl.port : ""}`); return { host: wsHost, disconnect: (hub, options2) => { return hubResult[hub].disconnect(options2); }, url, dispose: async () => { await Promise.all([rest.dispose(), wsHost.dispose()]); } }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createServerSignalr }); //# sourceMappingURL=createServer.cjs.map