@fakehost/signalr
Version:
A Fake Signalr Service for faking/mocking signalr hub services for testing, prototyping, and demoing
72 lines (69 loc) • 2.36 kB
JavaScript
// src/createServer.ts
import { HttpRestService, enableLogger as restLogger } from "@fakehost/fake-rest/server";
import { WsHost, enableLogger as wsLogger } from "@fakehost/exchange";
import { URL as URL2 } from "url";
// src/restHandshakeRouter.ts
import { createRouter, cors } from "@fakehost/fake-rest";
import { v4 as uuid } from "uuid";
var restRouter = createRouter().use(cors()).use((_, res) => {
const connectionId = uuid();
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
import Url from "url";
var isFakeSignalrHub = (hub) => {
return "constructor" in hub && "name" in hub.constructor && hub.constructor.name === "FakeSignalrHub";
};
var URL = globalThis.URL || Url.URL;
// src/createServer.ts
var objectKeys = (x) => Object.keys(x);
var createServerSignalr = async (options) => {
const rest = new HttpRestService(restRouter, {
name: `http://${options == null ? void 0 : options.name}`,
port: options == null ? void 0 : options.port,
silent: true
});
const wsHost = new 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) && wsLogger();
(options == null ? void 0 : options.debug) && restLogger();
const hostUrl = await wsHost.url;
const url = new URL2(`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()]);
}
};
};
export {
createServerSignalr
};
//# sourceMappingURL=createServer.js.map