"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseWebSocketRequest = exports.generateWebSocketUrl = void 0;
const BadRequestHttpError_1 = require("../../../util/errors/BadRequestHttpError");
/**
 * Generates a WebSocket URL by converting an HTTP(S) URL into a WS(S) URL.
 * @param id - The identifier of the channel. Needs to be a URL.
 */
function generateWebSocketUrl(id) {
    return `ws${id.slice('http'.length)}`;
}
exports.generateWebSocketUrl = generateWebSocketUrl;
/**
 * Parses a {@link IncomingMessage} to extract its path used for authentication.
 *
 * @param baseUrl - The base URL of the server.
 * @param request - The request to parse.
 */
function parseWebSocketRequest(baseUrl, request) {
    const path = request.url;
    if (!path) {
        throw new BadRequestHttpError_1.BadRequestHttpError('Missing url parameter in WebSocket request');
    }
    // Use dummy base and then explicitly set the host and protocol from the base URL.
    const id = new URL(path, 'http://example.com');
    const base = new URL(baseUrl);
    id.host = base.host;
    id.protocol = base.protocol;
    return id.href;
}
exports.parseWebSocketRequest = parseWebSocketRequest;
//# sourceMappingURL=WebSocket2023Util.js.map