UNPKG

@fakehost/signalr

Version:

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

109 lines (105 loc) 4.1 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/browser.ts var browser_exports = {}; __export(browser_exports, { MockedSocket: () => import_exchange.MockedSocket, createInBrowserSignalr: () => createInBrowserSignalr }); module.exports = __toCommonJS(browser_exports); var import_browser = require("@fakehost/fake-rest/browser"); var import_exchange = require("@fakehost/exchange"); // 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/browser.ts var objectKeys = (x) => Object.keys(x); var createInBrowserSignalr = async (options) => { if (options.debug) { (0, import_browser.enableLogger)(); (0, import_exchange.enableLogger)(); } const httpRest = new import_browser.HijackedRestService(options.url, restRouter, { name: options.name, silent: false }); const wsUrl = new URL(options.url); wsUrl.protocol = "ws:"; const hosts = objectKeys(options.hubs).filter((hubName) => isFakeSignalrHub(options.hubs[hubName])).reduce((acc, hubName) => { const hub = options.hubs[hubName]; if (isFakeSignalrHub(hub)) { const host = new import_exchange.BrowserWsHost({ url: new URL(hub.path, wsUrl), silent: options.silent, name: `${options.name || "fake"}:${hubName}` }); hub.setHost(host); acc[hubName] = { disconnect: host.disconnect }; } return acc; }, {}); return { MockedSocket: import_exchange.MockedSocket, mockedFetch: import_browser.mockedFetch, url: options.url, disconnect: (key) => hosts[key].disconnect(), dispose: async () => { const wshosts = Array.from(Object.values(hosts)); await Promise.all([...wshosts, httpRest.dispose()]); } }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { MockedSocket, createInBrowserSignalr }); //# sourceMappingURL=browser.cjs.map