HttpConnection.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. "use strict";
  2. // Copyright (c) .NET Foundation. All rights reserved.
  3. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  4. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  5. return new (P || (P = Promise))(function (resolve, reject) {
  6. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  7. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  8. function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
  9. step((generator = generator.apply(thisArg, _arguments || [])).next());
  10. });
  11. };
  12. var __generator = (this && this.__generator) || function (thisArg, body) {
  13. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  14. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  15. function verb(n) { return function (v) { return step([n, v]); }; }
  16. function step(op) {
  17. if (f) throw new TypeError("Generator is already executing.");
  18. while (_) try {
  19. 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;
  20. if (y = 0, t) op = [op[0] & 2, t.value];
  21. switch (op[0]) {
  22. case 0: case 1: t = op; break;
  23. case 4: _.label++; return { value: op[1], done: false };
  24. case 5: _.label++; y = op[1]; op = [0]; continue;
  25. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  26. default:
  27. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  28. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  29. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  30. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  31. if (t[2]) _.ops.pop();
  32. _.trys.pop(); continue;
  33. }
  34. op = body.call(thisArg, _);
  35. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  36. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  37. }
  38. };
  39. Object.defineProperty(exports, "__esModule", { value: true });
  40. var DefaultHttpClient_1 = require("./DefaultHttpClient");
  41. var ILogger_1 = require("./ILogger");
  42. var ITransport_1 = require("./ITransport");
  43. var LongPollingTransport_1 = require("./LongPollingTransport");
  44. var ServerSentEventsTransport_1 = require("./ServerSentEventsTransport");
  45. var Utils_1 = require("./Utils");
  46. var WebSocketTransport_1 = require("./WebSocketTransport");
  47. var MAX_REDIRECTS = 100;
  48. var WebSocketModule = null;
  49. var EventSourceModule = null;
  50. if (typeof window === "undefined" && typeof require !== "undefined") {
  51. // In order to ignore the dynamic require in webpack builds we need to do this magic
  52. // @ts-ignore: TS doesn't know about these names
  53. var requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
  54. WebSocketModule = requireFunc("ws");
  55. EventSourceModule = requireFunc("eventsource");
  56. }
  57. /** @private */
  58. var HttpConnection = /** @class */ (function () {
  59. function HttpConnection(url, options) {
  60. if (options === void 0) { options = {}; }
  61. this.features = {};
  62. Utils_1.Arg.isRequired(url, "url");
  63. this.logger = Utils_1.createLogger(options.logger);
  64. this.baseUrl = this.resolveUrl(url);
  65. options = options || {};
  66. options.logMessageContent = options.logMessageContent || false;
  67. var isNode = typeof window === "undefined";
  68. if (!isNode && typeof WebSocket !== "undefined" && !options.WebSocket) {
  69. options.WebSocket = WebSocket;
  70. }
  71. else if (isNode && !options.WebSocket) {
  72. if (WebSocketModule) {
  73. options.WebSocket = WebSocketModule;
  74. }
  75. }
  76. if (!isNode && typeof EventSource !== "undefined" && !options.EventSource) {
  77. options.EventSource = EventSource;
  78. }
  79. else if (isNode && !options.EventSource) {
  80. if (typeof EventSourceModule !== "undefined") {
  81. options.EventSource = EventSourceModule;
  82. }
  83. }
  84. this.httpClient = options.httpClient || new DefaultHttpClient_1.DefaultHttpClient(this.logger);
  85. this.connectionState = 2 /* Disconnected */;
  86. this.options = options;
  87. this.onreceive = null;
  88. this.onclose = null;
  89. }
  90. HttpConnection.prototype.start = function (transferFormat) {
  91. transferFormat = transferFormat || ITransport_1.TransferFormat.Binary;
  92. Utils_1.Arg.isIn(transferFormat, ITransport_1.TransferFormat, "transferFormat");
  93. this.logger.log(ILogger_1.LogLevel.Debug, "Starting connection with transfer format '" + ITransport_1.TransferFormat[transferFormat] + "'.");
  94. if (this.connectionState !== 2 /* Disconnected */) {
  95. return Promise.reject(new Error("Cannot start a connection that is not in the 'Disconnected' state."));
  96. }
  97. this.connectionState = 0 /* Connecting */;
  98. this.startPromise = this.startInternal(transferFormat);
  99. return this.startPromise;
  100. };
  101. HttpConnection.prototype.send = function (data) {
  102. if (this.connectionState !== 1 /* Connected */) {
  103. throw new Error("Cannot send data if the connection is not in the 'Connected' State.");
  104. }
  105. // Transport will not be null if state is connected
  106. return this.transport.send(data);
  107. };
  108. HttpConnection.prototype.stop = function (error) {
  109. return __awaiter(this, void 0, void 0, function () {
  110. var e_1;
  111. return __generator(this, function (_a) {
  112. switch (_a.label) {
  113. case 0:
  114. this.connectionState = 2 /* Disconnected */;
  115. // Set error as soon as possible otherwise there is a race between
  116. // the transport closing and providing an error and the error from a close message
  117. // We would prefer the close message error.
  118. this.stopError = error;
  119. _a.label = 1;
  120. case 1:
  121. _a.trys.push([1, 3, , 4]);
  122. return [4 /*yield*/, this.startPromise];
  123. case 2:
  124. _a.sent();
  125. return [3 /*break*/, 4];
  126. case 3:
  127. e_1 = _a.sent();
  128. return [3 /*break*/, 4];
  129. case 4:
  130. if (!this.transport) return [3 /*break*/, 6];
  131. return [4 /*yield*/, this.transport.stop()];
  132. case 5:
  133. _a.sent();
  134. this.transport = undefined;
  135. _a.label = 6;
  136. case 6: return [2 /*return*/];
  137. }
  138. });
  139. });
  140. };
  141. HttpConnection.prototype.startInternal = function (transferFormat) {
  142. return __awaiter(this, void 0, void 0, function () {
  143. var url, negotiateResponse, redirects, _loop_1, this_1, state_1, e_2;
  144. var _this = this;
  145. return __generator(this, function (_a) {
  146. switch (_a.label) {
  147. case 0:
  148. url = this.baseUrl;
  149. this.accessTokenFactory = this.options.accessTokenFactory;
  150. _a.label = 1;
  151. case 1:
  152. _a.trys.push([1, 12, , 13]);
  153. if (!this.options.skipNegotiation) return [3 /*break*/, 5];
  154. if (!(this.options.transport === ITransport_1.HttpTransportType.WebSockets)) return [3 /*break*/, 3];
  155. // No need to add a connection ID in this case
  156. this.transport = this.constructTransport(ITransport_1.HttpTransportType.WebSockets);
  157. // We should just call connect directly in this case.
  158. // No fallback or negotiate in this case.
  159. return [4 /*yield*/, this.transport.connect(url, transferFormat)];
  160. case 2:
  161. // We should just call connect directly in this case.
  162. // No fallback or negotiate in this case.
  163. _a.sent();
  164. return [3 /*break*/, 4];
  165. case 3: throw Error("Negotiation can only be skipped when using the WebSocket transport directly.");
  166. case 4: return [3 /*break*/, 11];
  167. case 5:
  168. negotiateResponse = null;
  169. redirects = 0;
  170. _loop_1 = function () {
  171. var accessToken_1;
  172. return __generator(this, function (_a) {
  173. switch (_a.label) {
  174. case 0: return [4 /*yield*/, this_1.getNegotiationResponse(url)];
  175. case 1:
  176. negotiateResponse = _a.sent();
  177. // the user tries to stop the connection when it is being started
  178. if (this_1.connectionState === 2 /* Disconnected */) {
  179. return [2 /*return*/, { value: void 0 }];
  180. }
  181. if (negotiateResponse.error) {
  182. throw Error(negotiateResponse.error);
  183. }
  184. if (negotiateResponse.ProtocolVersion) {
  185. 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.");
  186. }
  187. if (negotiateResponse.url) {
  188. url = negotiateResponse.url;
  189. }
  190. if (negotiateResponse.accessToken) {
  191. accessToken_1 = negotiateResponse.accessToken;
  192. this_1.accessTokenFactory = function () { return accessToken_1; };
  193. }
  194. redirects++;
  195. return [2 /*return*/];
  196. }
  197. });
  198. };
  199. this_1 = this;
  200. _a.label = 6;
  201. case 6: return [5 /*yield**/, _loop_1()];
  202. case 7:
  203. state_1 = _a.sent();
  204. if (typeof state_1 === "object")
  205. return [2 /*return*/, state_1.value];
  206. _a.label = 8;
  207. case 8:
  208. if (negotiateResponse.url && redirects < MAX_REDIRECTS) return [3 /*break*/, 6];
  209. _a.label = 9;
  210. case 9:
  211. if (redirects === MAX_REDIRECTS && negotiateResponse.url) {
  212. throw Error("Negotiate redirection limit exceeded.");
  213. }
  214. return [4 /*yield*/, this.createTransport(url, this.options.transport, negotiateResponse, transferFormat)];
  215. case 10:
  216. _a.sent();
  217. _a.label = 11;
  218. case 11:
  219. if (this.transport instanceof LongPollingTransport_1.LongPollingTransport) {
  220. this.features.inherentKeepAlive = true;
  221. }
  222. this.transport.onreceive = this.onreceive;
  223. this.transport.onclose = function (e) { return _this.stopConnection(e); };
  224. // only change the state if we were connecting to not overwrite
  225. // the state if the connection is already marked as Disconnected
  226. this.changeState(0 /* Connecting */, 1 /* Connected */);
  227. return [3 /*break*/, 13];
  228. case 12:
  229. e_2 = _a.sent();
  230. this.logger.log(ILogger_1.LogLevel.Error, "Failed to start the connection: " + e_2);
  231. this.connectionState = 2 /* Disconnected */;
  232. this.transport = undefined;
  233. throw e_2;
  234. case 13: return [2 /*return*/];
  235. }
  236. });
  237. });
  238. };
  239. HttpConnection.prototype.getNegotiationResponse = function (url) {
  240. return __awaiter(this, void 0, void 0, function () {
  241. var _a, headers, token, negotiateUrl, response, e_3;
  242. return __generator(this, function (_b) {
  243. switch (_b.label) {
  244. case 0:
  245. if (!this.accessTokenFactory) return [3 /*break*/, 2];
  246. return [4 /*yield*/, this.accessTokenFactory()];
  247. case 1:
  248. token = _b.sent();
  249. if (token) {
  250. headers = (_a = {},
  251. _a["Authorization"] = "Bearer " + token,
  252. _a);
  253. }
  254. _b.label = 2;
  255. case 2:
  256. negotiateUrl = this.resolveNegotiateUrl(url);
  257. this.logger.log(ILogger_1.LogLevel.Debug, "Sending negotiation request: " + negotiateUrl + ".");
  258. _b.label = 3;
  259. case 3:
  260. _b.trys.push([3, 5, , 6]);
  261. return [4 /*yield*/, this.httpClient.post(negotiateUrl, {
  262. content: "",
  263. headers: headers,
  264. })];
  265. case 4:
  266. response = _b.sent();
  267. if (response.statusCode !== 200) {
  268. throw Error("Unexpected status code returned from negotiate " + response.statusCode);
  269. }
  270. return [2 /*return*/, JSON.parse(response.content)];
  271. case 5:
  272. e_3 = _b.sent();
  273. this.logger.log(ILogger_1.LogLevel.Error, "Failed to complete negotiation with the server: " + e_3);
  274. throw e_3;
  275. case 6: return [2 /*return*/];
  276. }
  277. });
  278. });
  279. };
  280. HttpConnection.prototype.createConnectUrl = function (url, connectionId) {
  281. if (!connectionId) {
  282. return url;
  283. }
  284. return url + (url.indexOf("?") === -1 ? "?" : "&") + ("id=" + connectionId);
  285. };
  286. HttpConnection.prototype.createTransport = function (url, requestedTransport, negotiateResponse, requestedTransferFormat) {
  287. return __awaiter(this, void 0, void 0, function () {
  288. var connectUrl, transports, _i, transports_1, endpoint, transport, ex_1;
  289. return __generator(this, function (_a) {
  290. switch (_a.label) {
  291. case 0:
  292. connectUrl = this.createConnectUrl(url, negotiateResponse.connectionId);
  293. if (!this.isITransport(requestedTransport)) return [3 /*break*/, 2];
  294. this.logger.log(ILogger_1.LogLevel.Debug, "Connection was provided an instance of ITransport, using that directly.");
  295. this.transport = requestedTransport;
  296. return [4 /*yield*/, this.transport.connect(connectUrl, requestedTransferFormat)];
  297. case 1:
  298. _a.sent();
  299. // only change the state if we were connecting to not overwrite
  300. // the state if the connection is already marked as Disconnected
  301. this.changeState(0 /* Connecting */, 1 /* Connected */);
  302. return [2 /*return*/];
  303. case 2:
  304. transports = negotiateResponse.availableTransports || [];
  305. _i = 0, transports_1 = transports;
  306. _a.label = 3;
  307. case 3:
  308. if (!(_i < transports_1.length)) return [3 /*break*/, 9];
  309. endpoint = transports_1[_i];
  310. this.connectionState = 0 /* Connecting */;
  311. transport = this.resolveTransport(endpoint, requestedTransport, requestedTransferFormat);
  312. if (!(typeof transport === "number")) return [3 /*break*/, 8];
  313. this.transport = this.constructTransport(transport);
  314. if (!!negotiateResponse.connectionId) return [3 /*break*/, 5];
  315. return [4 /*yield*/, this.getNegotiationResponse(url)];
  316. case 4:
  317. negotiateResponse = _a.sent();
  318. connectUrl = this.createConnectUrl(url, negotiateResponse.connectionId);
  319. _a.label = 5;
  320. case 5:
  321. _a.trys.push([5, 7, , 8]);
  322. return [4 /*yield*/, this.transport.connect(connectUrl, requestedTransferFormat)];
  323. case 6:
  324. _a.sent();
  325. this.changeState(0 /* Connecting */, 1 /* Connected */);
  326. return [2 /*return*/];
  327. case 7:
  328. ex_1 = _a.sent();
  329. this.logger.log(ILogger_1.LogLevel.Error, "Failed to start the transport '" + ITransport_1.HttpTransportType[transport] + "': " + ex_1);
  330. this.connectionState = 2 /* Disconnected */;
  331. negotiateResponse.connectionId = undefined;
  332. return [3 /*break*/, 8];
  333. case 8:
  334. _i++;
  335. return [3 /*break*/, 3];
  336. case 9: throw new Error("Unable to initialize any of the available transports.");
  337. }
  338. });
  339. });
  340. };
  341. HttpConnection.prototype.constructTransport = function (transport) {
  342. switch (transport) {
  343. case ITransport_1.HttpTransportType.WebSockets:
  344. if (!this.options.WebSocket) {
  345. throw new Error("'WebSocket' is not supported in your environment.");
  346. }
  347. return new WebSocketTransport_1.WebSocketTransport(this.httpClient, this.accessTokenFactory, this.logger, this.options.logMessageContent || false, this.options.WebSocket);
  348. case ITransport_1.HttpTransportType.ServerSentEvents:
  349. if (!this.options.EventSource) {
  350. throw new Error("'EventSource' is not supported in your environment.");
  351. }
  352. return new ServerSentEventsTransport_1.ServerSentEventsTransport(this.httpClient, this.accessTokenFactory, this.logger, this.options.logMessageContent || false, this.options.EventSource);
  353. case ITransport_1.HttpTransportType.LongPolling:
  354. return new LongPollingTransport_1.LongPollingTransport(this.httpClient, this.accessTokenFactory, this.logger, this.options.logMessageContent || false);
  355. default:
  356. throw new Error("Unknown transport: " + transport + ".");
  357. }
  358. };
  359. HttpConnection.prototype.resolveTransport = function (endpoint, requestedTransport, requestedTransferFormat) {
  360. var transport = ITransport_1.HttpTransportType[endpoint.transport];
  361. if (transport === null || transport === undefined) {
  362. this.logger.log(ILogger_1.LogLevel.Debug, "Skipping transport '" + endpoint.transport + "' because it is not supported by this client.");
  363. }
  364. else {
  365. var transferFormats = endpoint.transferFormats.map(function (s) { return ITransport_1.TransferFormat[s]; });
  366. if (transportMatches(requestedTransport, transport)) {
  367. if (transferFormats.indexOf(requestedTransferFormat) >= 0) {
  368. if ((transport === ITransport_1.HttpTransportType.WebSockets && !this.options.WebSocket) ||
  369. (transport === ITransport_1.HttpTransportType.ServerSentEvents && !this.options.EventSource)) {
  370. this.logger.log(ILogger_1.LogLevel.Debug, "Skipping transport '" + ITransport_1.HttpTransportType[transport] + "' because it is not supported in your environment.'");
  371. }
  372. else {
  373. this.logger.log(ILogger_1.LogLevel.Debug, "Selecting transport '" + ITransport_1.HttpTransportType[transport] + "'.");
  374. return transport;
  375. }
  376. }
  377. else {
  378. this.logger.log(ILogger_1.LogLevel.Debug, "Skipping transport '" + ITransport_1.HttpTransportType[transport] + "' because it does not support the requested transfer format '" + ITransport_1.TransferFormat[requestedTransferFormat] + "'.");
  379. }
  380. }
  381. else {
  382. this.logger.log(ILogger_1.LogLevel.Debug, "Skipping transport '" + ITransport_1.HttpTransportType[transport] + "' because it was disabled by the client.");
  383. }
  384. }
  385. return null;
  386. };
  387. HttpConnection.prototype.isITransport = function (transport) {
  388. return transport && typeof (transport) === "object" && "connect" in transport;
  389. };
  390. HttpConnection.prototype.changeState = function (from, to) {
  391. if (this.connectionState === from) {
  392. this.connectionState = to;
  393. return true;
  394. }
  395. return false;
  396. };
  397. HttpConnection.prototype.stopConnection = function (error) {
  398. this.transport = undefined;
  399. // If we have a stopError, it takes precedence over the error from the transport
  400. error = this.stopError || error;
  401. if (error) {
  402. this.logger.log(ILogger_1.LogLevel.Error, "Connection disconnected with error '" + error + "'.");
  403. }
  404. else {
  405. this.logger.log(ILogger_1.LogLevel.Information, "Connection disconnected.");
  406. }
  407. this.connectionState = 2 /* Disconnected */;
  408. if (this.onclose) {
  409. this.onclose(error);
  410. }
  411. };
  412. HttpConnection.prototype.resolveUrl = function (url) {
  413. // startsWith is not supported in IE
  414. if (url.lastIndexOf("https://", 0) === 0 || url.lastIndexOf("http://", 0) === 0) {
  415. return url;
  416. }
  417. if (typeof window === "undefined" || !window || !window.document) {
  418. throw new Error("Cannot resolve '" + url + "'.");
  419. }
  420. // Setting the url to the href propery of an anchor tag handles normalization
  421. // for us. There are 3 main cases.
  422. // 1. Relative path normalization e.g "b" -> "http://localhost:5000/a/b"
  423. // 2. Absolute path normalization e.g "/a/b" -> "http://localhost:5000/a/b"
  424. // 3. Networkpath reference normalization e.g "//localhost:5000/a/b" -> "http://localhost:5000/a/b"
  425. var aTag = window.document.createElement("a");
  426. aTag.href = url;
  427. this.logger.log(ILogger_1.LogLevel.Information, "Normalizing '" + url + "' to '" + aTag.href + "'.");
  428. return aTag.href;
  429. };
  430. HttpConnection.prototype.resolveNegotiateUrl = function (url) {
  431. var index = url.indexOf("?");
  432. var negotiateUrl = url.substring(0, index === -1 ? url.length : index);
  433. if (negotiateUrl[negotiateUrl.length - 1] !== "/") {
  434. negotiateUrl += "/";
  435. }
  436. negotiateUrl += "negotiate";
  437. negotiateUrl += index === -1 ? "" : url.substring(index);
  438. return negotiateUrl;
  439. };
  440. return HttpConnection;
  441. }());
  442. exports.HttpConnection = HttpConnection;
  443. function transportMatches(requestedTransport, actualTransport) {
  444. return !requestedTransport || ((actualTransport & requestedTransport) !== 0);
  445. }
  446. //# sourceMappingURL=HttpConnection.js.map