// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; import { DefaultHttpClient } from "./DefaultHttpClient"; import { LogLevel } from "./ILogger"; import { HttpTransportType, TransferFormat } from "./ITransport"; import { LongPollingTransport } from "./LongPollingTransport"; import { ServerSentEventsTransport } from "./ServerSentEventsTransport"; import { Arg, createLogger } from "./Utils"; import { WebSocketTransport } from "./WebSocketTransport"; var MAX_REDIRECTS = 100; var WebSocketModule = null; var EventSourceModule = null; if (typeof window === "undefined" && typeof require !== "undefined") { // In order to ignore the dynamic require in webpack builds we need to do this magic // @ts-ignore: TS doesn't know about these names var requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require; WebSocketModule = requireFunc("ws"); EventSourceModule = requireFunc("eventsource"); } /** @private */ var HttpConnection = /** @class */ (function () { function HttpConnection(url, options) { if (options === void 0) { options = {}; } this.features = {}; Arg.isRequired(url, "url"); this.logger = createLogger(options.logger); this.baseUrl = this.resolveUrl(url); options = options || {}; options.logMessageContent = options.logMessageContent || false; var isNode = typeof window === "undefined"; if (!isNode && typeof WebSocket !== "undefined" && !options.WebSocket) { options.WebSocket = WebSocket; } else if (isNode && !options.WebSocket) { if (WebSocketModule) { options.WebSocket = WebSocketModule; } } if (!isNode && typeof EventSource !== "undefined" && !options.EventSource) { options.EventSource = EventSource; } else if (isNode && !options.EventSource) { if (typeof EventSourceModule !== "undefined") { options.EventSource = EventSourceModule; } } this.httpClient = options.httpClient || new DefaultHttpClient(this.logger); this.connectionState = 2 /* Disconnected */; this.options = options; this.onreceive = null; this.onclose = null; } HttpConnection.prototype.start = function (transferFormat) { transferFormat = transferFormat || TransferFormat.Binary; Arg.isIn(transferFormat, TransferFormat, "transferFormat"); this.logger.log(LogLevel.Debug, "Starting connection with transfer format '" + TransferFormat[transferFormat] + "'."); if (this.connectionState !== 2 /* Disconnected */) { return Promise.reject(new Error("Cannot start a connection that is not in the 'Disconnected' state.")); } this.connectionState = 0 /* Connecting */; this.startPromise = this.startInternal(transferFormat); return this.startPromise; }; HttpConnection.prototype.send = function (data) { if (this.connectionState !== 1 /* Connected */) { throw new Error("Cannot send data if the connection is not in the 'Connected' State."); } // Transport will not be null if state is connected return this.transport.send(data); }; HttpConnection.prototype.stop = function (error) { return __awaiter(this, void 0, void 0, function () { var e_1; return __generator(this, function (_a) { switch (_a.label) { case 0: this.connectionState = 2 /* Disconnected */; // Set error as soon as possible otherwise there is a race between // the transport closing and providing an error and the error from a close message // We would prefer the close message error. this.stopError = error; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, this.startPromise]; case 2: _a.sent(); return [3 /*break*/, 4]; case 3: e_1 = _a.sent(); return [3 /*break*/, 4]; case 4: if (!this.transport) return [3 /*break*/, 6]; return [4 /*yield*/, this.transport.stop()]; case 5: _a.sent(); this.transport = undefined; _a.label = 6; case 6: return [2 /*return*/]; } }); }); }; HttpConnection.prototype.startInternal = function (transferFormat) { return __awaiter(this, void 0, void 0, function () { var url, negotiateResponse, redirects, _loop_1, this_1, state_1, e_2; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: url = this.baseUrl; this.accessTokenFactory = this.options.accessTokenFactory; _a.label = 1; case 1: _a.trys.push([1, 12, , 13]); if (!this.options.skipNegotiation) return [3 /*break*/, 5]; if (!(this.options.transport === HttpTransportType.WebSockets)) return [3 /*break*/, 3]; // No need to add a connection ID in this case this.transport = this.constructTransport(HttpTransportType.WebSockets); // We should just call connect directly in this case. // No fallback or negotiate in this case. return [4 /*yield*/, this.transport.connect(url, transferFormat)]; case 2: // We should just call connect directly in this case. // No fallback or negotiate in this case. _a.sent(); return [3 /*break*/, 4]; case 3: throw Error("Negotiation can only be skipped when using the WebSocket transport directly."); case 4: return [3 /*break*/, 11]; case 5: negotiateResponse = null; redirects = 0; _loop_1 = function () { var accessToken_1; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this_1.getNegotiationResponse(url)]; case 1: negotiateResponse = _a.sent(); // the user tries to stop the connection when it is being started if (this_1.connectionState === 2 /* Disconnected */) { return [2 /*return*/, { value: void 0 }]; } if (negotiateResponse.error) { throw Error(negotiateResponse.error); } if (negotiateResponse.ProtocolVersion) { throw Error("Detected a connection attempt to an ASP.NET SignalR Server. This client only supports connecting to an ASP.NET Core SignalR Server. See https://aka.ms/signalr-core-differences for details."); } if (negotiateResponse.url) { url = negotiateResponse.url; } if (negotiateResponse.accessToken) { accessToken_1 = negotiateResponse.accessToken; this_1.accessTokenFactory = function () { return accessToken_1; }; } redirects++; return [2 /*return*/]; } }); }; this_1 = this; _a.label = 6; case 6: return [5 /*yield**/, _loop_1()]; case 7: state_1 = _a.sent(); if (typeof state_1 === "object") return [2 /*return*/, state_1.value]; _a.label = 8; case 8: if (negotiateResponse.url && redirects < MAX_REDIRECTS) return [3 /*break*/, 6]; _a.label = 9; case 9: if (redirects === MAX_REDIRECTS && negotiateResponse.url) { throw Error("Negotiate redirection limit exceeded."); } return [4 /*yield*/, this.createTransport(url, this.options.transport, negotiateResponse, transferFormat)]; case 10: _a.sent(); _a.label = 11; case 11: if (this.transport instanceof LongPollingTransport) { this.features.inherentKeepAlive = true; } this.transport.onreceive = this.onreceive; this.transport.onclose = function (e) { return _this.stopConnection(e); }; // only change the state if we were connecting to not overwrite // the state if the connection is already marked as Disconnected this.changeState(0 /* Connecting */, 1 /* Connected */); return [3 /*break*/, 13]; case 12: e_2 = _a.sent(); this.logger.log(LogLevel.Error, "Failed to start the connection: " + e_2); this.connectionState = 2 /* Disconnected */; this.transport = undefined; throw e_2; case 13: return [2 /*return*/]; } }); }); }; HttpConnection.prototype.getNegotiationResponse = function (url) { return __awaiter(this, void 0, void 0, function () { var _a, headers, token, negotiateUrl, response, e_3; return __generator(this, function (_b) { switch (_b.label) { case 0: if (!this.accessTokenFactory) return [3 /*break*/, 2]; return [4 /*yield*/, this.accessTokenFactory()]; case 1: token = _b.sent(); if (token) { headers = (_a = {}, _a["Authorization"] = "Bearer " + token, _a); } _b.label = 2; case 2: negotiateUrl = this.resolveNegotiateUrl(url); this.logger.log(LogLevel.Debug, "Sending negotiation request: " + negotiateUrl + "."); _b.label = 3; case 3: _b.trys.push([3, 5, , 6]); return [4 /*yield*/, this.httpClient.post(negotiateUrl, { content: "", headers: headers, })]; case 4: response = _b.sent(); if (response.statusCode !== 200) { throw Error("Unexpected status code returned from negotiate " + response.statusCode); } return [2 /*return*/, JSON.parse(response.content)]; case 5: e_3 = _b.sent(); this.logger.log(LogLevel.Error, "Failed to complete negotiation with the server: " + e_3); throw e_3; case 6: return [2 /*return*/]; } }); }); }; HttpConnection.prototype.createConnectUrl = function (url, connectionId) { if (!connectionId) { return url; } return url + (url.indexOf("?") === -1 ? "?" : "&") + ("id=" + connectionId); }; HttpConnection.prototype.createTransport = function (url, requestedTransport, negotiateResponse, requestedTransferFormat) { return __awaiter(this, void 0, void 0, function () { var connectUrl, transports, _i, transports_1, endpoint, transport, ex_1; return __generator(this, function (_a) { switch (_a.label) { case 0: connectUrl = this.createConnectUrl(url, negotiateResponse.connectionId); if (!this.isITransport(requestedTransport)) return [3 /*break*/, 2]; this.logger.log(LogLevel.Debug, "Connection was provided an instance of ITransport, using that directly."); this.transport = requestedTransport; return [4 /*yield*/, this.transport.connect(connectUrl, requestedTransferFormat)]; case 1: _a.sent(); // only change the state if we were connecting to not overwrite // the state if the connection is already marked as Disconnected this.changeState(0 /* Connecting */, 1 /* Connected */); return [2 /*return*/]; case 2: transports = negotiateResponse.availableTransports || []; _i = 0, transports_1 = transports; _a.label = 3; case 3: if (!(_i < transports_1.length)) return [3 /*break*/, 9]; endpoint = transports_1[_i]; this.connectionState = 0 /* Connecting */; transport = this.resolveTransport(endpoint, requestedTransport, requestedTransferFormat); if (!(typeof transport === "number")) return [3 /*break*/, 8]; this.transport = this.constructTransport(transport); if (!!negotiateResponse.connectionId) return [3 /*break*/, 5]; return [4 /*yield*/, this.getNegotiationResponse(url)]; case 4: negotiateResponse = _a.sent(); connectUrl = this.createConnectUrl(url, negotiateResponse.connectionId); _a.label = 5; case 5: _a.trys.push([5, 7, , 8]); return [4 /*yield*/, this.transport.connect(connectUrl, requestedTransferFormat)]; case 6: _a.sent(); this.changeState(0 /* Connecting */, 1 /* Connected */); return [2 /*return*/]; case 7: ex_1 = _a.sent(); this.logger.log(LogLevel.Error, "Failed to start the transport '" + HttpTransportType[transport] + "': " + ex_1); this.connectionState = 2 /* Disconnected */; negotiateResponse.connectionId = undefined; return [3 /*break*/, 8]; case 8: _i++; return [3 /*break*/, 3]; case 9: throw new Error("Unable to initialize any of the available transports."); } }); }); }; HttpConnection.prototype.constructTransport = function (transport) { switch (transport) { case HttpTransportType.WebSockets: if (!this.options.WebSocket) { throw new Error("'WebSocket' is not supported in your environment."); } return new WebSocketTransport(this.httpClient, this.accessTokenFactory, this.logger, this.options.logMessageContent || false, this.options.WebSocket); case HttpTransportType.ServerSentEvents: if (!this.options.EventSource) { throw new Error("'EventSource' is not supported in your environment."); } return new ServerSentEventsTransport(this.httpClient, this.accessTokenFactory, this.logger, this.options.logMessageContent || false, this.options.EventSource); case HttpTransportType.LongPolling: return new LongPollingTransport(this.httpClient, this.accessTokenFactory, this.logger, this.options.logMessageContent || false); default: throw new Error("Unknown transport: " + transport + "."); } }; HttpConnection.prototype.resolveTransport = function (endpoint, requestedTransport, requestedTransferFormat) { var transport = HttpTransportType[endpoint.transport]; if (transport === null || transport === undefined) { this.logger.log(LogLevel.Debug, "Skipping transport '" + endpoint.transport + "' because it is not supported by this client."); } else { var transferFormats = endpoint.transferFormats.map(function (s) { return TransferFormat[s]; }); if (transportMatches(requestedTransport, transport)) { if (transferFormats.indexOf(requestedTransferFormat) >= 0) { if ((transport === HttpTransportType.WebSockets && !this.options.WebSocket) || (transport === HttpTransportType.ServerSentEvents && !this.options.EventSource)) { this.logger.log(LogLevel.Debug, "Skipping transport '" + HttpTransportType[transport] + "' because it is not supported in your environment.'"); } else { this.logger.log(LogLevel.Debug, "Selecting transport '" + HttpTransportType[transport] + "'."); return transport; } } else { this.logger.log(LogLevel.Debug, "Skipping transport '" + HttpTransportType[transport] + "' because it does not support the requested transfer format '" + TransferFormat[requestedTransferFormat] + "'."); } } else { this.logger.log(LogLevel.Debug, "Skipping transport '" + HttpTransportType[transport] + "' because it was disabled by the client."); } } return null; }; HttpConnection.prototype.isITransport = function (transport) { return transport && typeof (transport) === "object" && "connect" in transport; }; HttpConnection.prototype.changeState = function (from, to) { if (this.connectionState === from) { this.connectionState = to; return true; } return false; }; HttpConnection.prototype.stopConnection = function (error) { this.transport = undefined; // If we have a stopError, it takes precedence over the error from the transport error = this.stopError || error; if (error) { this.logger.log(LogLevel.Error, "Connection disconnected with error '" + error + "'."); } else { this.logger.log(LogLevel.Information, "Connection disconnected."); } this.connectionState = 2 /* Disconnected */; if (this.onclose) { this.onclose(error); } }; HttpConnection.prototype.resolveUrl = function (url) { // startsWith is not supported in IE if (url.lastIndexOf("https://", 0) === 0 || url.lastIndexOf("http://", 0) === 0) { return url; } if (typeof window === "undefined" || !window || !window.document) { throw new Error("Cannot resolve '" + url + "'."); } // Setting the url to the href propery of an anchor tag handles normalization // for us. There are 3 main cases. // 1. Relative path normalization e.g "b" -> "http://localhost:5000/a/b" // 2. Absolute path normalization e.g "/a/b" -> "http://localhost:5000/a/b" // 3. Networkpath reference normalization e.g "//localhost:5000/a/b" -> "http://localhost:5000/a/b" var aTag = window.document.createElement("a"); aTag.href = url; this.logger.log(LogLevel.Information, "Normalizing '" + url + "' to '" + aTag.href + "'."); return aTag.href; }; HttpConnection.prototype.resolveNegotiateUrl = function (url) { var index = url.indexOf("?"); var negotiateUrl = url.substring(0, index === -1 ? url.length : index); if (negotiateUrl[negotiateUrl.length - 1] !== "/") { negotiateUrl += "/"; } negotiateUrl += "negotiate"; negotiateUrl += index === -1 ? "" : url.substring(index); return negotiateUrl; }; return HttpConnection; }()); export { HttpConnection }; function transportMatches(requestedTransport, actualTransport) { return !requestedTransport || ((actualTransport & requestedTransport) !== 0); } //# sourceMappingURL=HttpConnection.js.map