rsocket-browser
Version:
Browser-first RSocket client over WebSocket or WebTransport with reconnect and Resume.
61 lines (60 loc) • 1.95 kB
JavaScript
//#region src/rsocket/options.ts
/** Resolves both browser constructor forms into canonical client options. */
function browserClientOptions(urlOrOptions, fallback) {
const { url, options } = resolveConstructor(urlOrOptions, fallback);
const { setup: browserSetup, webTransport, reconnect = true, ...rest } = options;
const { transport, protocols, ...setup } = browserSetup ?? {};
if (isWebTransportEndpoint(url)) {
if (protocols !== void 0 || transport !== void 0) throw new TypeError("WebTransport URLs use options.webTransport; setup.protocols and setup.transport are WebSocket-only");
return {
...rest,
reconnect,
transport: {
type: "webtransport",
url,
...webTransport
},
...browserSetup === void 0 ? {} : { setup }
};
}
if (webTransport !== void 0) throw new TypeError("options.webTransport requires an https: WebTransport URL");
return {
...rest,
reconnect,
transport: {
type: "websocket",
url,
...protocols === void 0 ? {} : { protocols },
...transport === void 0 ? {} : { factory: transport }
},
...browserSetup === void 0 ? {} : { setup }
};
}
/** Selects WebTransport only for an unambiguous HTTPS endpoint. */
function isWebTransportEndpoint(input) {
try {
const href = isUrl(input) ? input.href : String(input);
return new URL(href).protocol === "https:";
} catch {
return false;
}
}
/** Separates the URL from either supported constructor form. */
function resolveConstructor(input, fallback) {
if (typeof input === "string" || isUrl(input)) return {
url: input,
options: fallback
};
const { url, ...options } = input;
return {
url,
options
};
}
/** Recognizes URL objects created by the current window or another browser realm. */
function isUrl(value) {
return typeof value === "object" && value !== null && Object.prototype.toString.call(value) === "[object URL]";
}
//#endregion
export { browserClientOptions };
//# sourceMappingURL=options.js.map