HttpConnection.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. import { AccessTokenHttpClient } from "./AccessTokenHttpClient";
  4. import { DefaultHttpClient } from "./DefaultHttpClient";
  5. import { AggregateErrors, DisabledTransportError, FailedToNegotiateWithServerError, FailedToStartTransportError, HttpError, UnsupportedTransportError, AbortError } from "./Errors";
  6. import { LogLevel } from "./ILogger";
  7. import { HttpTransportType, TransferFormat } from "./ITransport";
  8. import { LongPollingTransport } from "./LongPollingTransport";
  9. import { ServerSentEventsTransport } from "./ServerSentEventsTransport";
  10. import { Arg, createLogger, getUserAgentHeader, Platform } from "./Utils";
  11. import { WebSocketTransport } from "./WebSocketTransport";
  12. const MAX_REDIRECTS = 100;
  13. /** @private */
  14. export class HttpConnection {
  15. constructor(url, options = {}) {
  16. this._stopPromiseResolver = () => { };
  17. this.features = {};
  18. this._negotiateVersion = 1;
  19. Arg.isRequired(url, "url");
  20. this._logger = createLogger(options.logger);
  21. this.baseUrl = this._resolveUrl(url);
  22. options = options || {};
  23. options.logMessageContent = options.logMessageContent === undefined ? false : options.logMessageContent;
  24. if (typeof options.withCredentials === "boolean" || options.withCredentials === undefined) {
  25. options.withCredentials = options.withCredentials === undefined ? true : options.withCredentials;
  26. }
  27. else {
  28. throw new Error("withCredentials option was not a 'boolean' or 'undefined' value");
  29. }
  30. options.timeout = options.timeout === undefined ? 100 * 1000 : options.timeout;
  31. let webSocketModule = null;
  32. let eventSourceModule = null;
  33. if (Platform.isNode && typeof require !== "undefined") {
  34. // In order to ignore the dynamic require in webpack builds we need to do this magic
  35. // @ts-ignore: TS doesn't know about these names
  36. const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
  37. webSocketModule = requireFunc("ws");
  38. eventSourceModule = requireFunc("eventsource");
  39. }
  40. if (!Platform.isNode && typeof WebSocket !== "undefined" && !options.WebSocket) {
  41. options.WebSocket = WebSocket;
  42. }
  43. else if (Platform.isNode && !options.WebSocket) {
  44. if (webSocketModule) {
  45. options.WebSocket = webSocketModule;
  46. }
  47. }
  48. if (!Platform.isNode && typeof EventSource !== "undefined" && !options.EventSource) {
  49. options.EventSource = EventSource;
  50. }
  51. else if (Platform.isNode && !options.EventSource) {
  52. if (typeof eventSourceModule !== "undefined") {
  53. options.EventSource = eventSourceModule;
  54. }
  55. }
  56. this._httpClient = new AccessTokenHttpClient(options.httpClient || new DefaultHttpClient(this._logger), options.accessTokenFactory);
  57. this._connectionState = "Disconnected" /* Disconnected */;
  58. this._connectionStarted = false;
  59. this._options = options;
  60. this.onreceive = null;
  61. this.onclose = null;
  62. }
  63. async start(transferFormat) {
  64. transferFormat = transferFormat || TransferFormat.Binary;
  65. Arg.isIn(transferFormat, TransferFormat, "transferFormat");
  66. this._logger.log(LogLevel.Debug, `Starting connection with transfer format '${TransferFormat[transferFormat]}'.`);
  67. if (this._connectionState !== "Disconnected" /* Disconnected */) {
  68. return Promise.reject(new Error("Cannot start an HttpConnection that is not in the 'Disconnected' state."));
  69. }
  70. this._connectionState = "Connecting" /* Connecting */;
  71. this._startInternalPromise = this._startInternal(transferFormat);
  72. await this._startInternalPromise;
  73. // The TypeScript compiler thinks that connectionState must be Connecting here. The TypeScript compiler is wrong.
  74. if (this._connectionState === "Disconnecting" /* Disconnecting */) {
  75. // stop() was called and transitioned the client into the Disconnecting state.
  76. const message = "Failed to start the HttpConnection before stop() was called.";
  77. this._logger.log(LogLevel.Error, message);
  78. // We cannot await stopPromise inside startInternal since stopInternal awaits the startInternalPromise.
  79. await this._stopPromise;
  80. return Promise.reject(new AbortError(message));
  81. }
  82. else if (this._connectionState !== "Connected" /* Connected */) {
  83. // stop() was called and transitioned the client into the Disconnecting state.
  84. const message = "HttpConnection.startInternal completed gracefully but didn't enter the connection into the connected state!";
  85. this._logger.log(LogLevel.Error, message);
  86. return Promise.reject(new AbortError(message));
  87. }
  88. this._connectionStarted = true;
  89. }
  90. send(data) {
  91. if (this._connectionState !== "Connected" /* Connected */) {
  92. return Promise.reject(new Error("Cannot send data if the connection is not in the 'Connected' State."));
  93. }
  94. if (!this._sendQueue) {
  95. this._sendQueue = new TransportSendQueue(this.transport);
  96. }
  97. // Transport will not be null if state is connected
  98. return this._sendQueue.send(data);
  99. }
  100. async stop(error) {
  101. if (this._connectionState === "Disconnected" /* Disconnected */) {
  102. this._logger.log(LogLevel.Debug, `Call to HttpConnection.stop(${error}) ignored because the connection is already in the disconnected state.`);
  103. return Promise.resolve();
  104. }
  105. if (this._connectionState === "Disconnecting" /* Disconnecting */) {
  106. this._logger.log(LogLevel.Debug, `Call to HttpConnection.stop(${error}) ignored because the connection is already in the disconnecting state.`);
  107. return this._stopPromise;
  108. }
  109. this._connectionState = "Disconnecting" /* Disconnecting */;
  110. this._stopPromise = new Promise((resolve) => {
  111. // Don't complete stop() until stopConnection() completes.
  112. this._stopPromiseResolver = resolve;
  113. });
  114. // stopInternal should never throw so just observe it.
  115. await this._stopInternal(error);
  116. await this._stopPromise;
  117. }
  118. async _stopInternal(error) {
  119. // Set error as soon as possible otherwise there is a race between
  120. // the transport closing and providing an error and the error from a close message
  121. // We would prefer the close message error.
  122. this._stopError = error;
  123. try {
  124. await this._startInternalPromise;
  125. }
  126. catch (e) {
  127. // This exception is returned to the user as a rejected Promise from the start method.
  128. }
  129. // The transport's onclose will trigger stopConnection which will run our onclose event.
  130. // The transport should always be set if currently connected. If it wasn't set, it's likely because
  131. // stop was called during start() and start() failed.
  132. if (this.transport) {
  133. try {
  134. await this.transport.stop();
  135. }
  136. catch (e) {
  137. this._logger.log(LogLevel.Error, `HttpConnection.transport.stop() threw error '${e}'.`);
  138. this._stopConnection();
  139. }
  140. this.transport = undefined;
  141. }
  142. else {
  143. this._logger.log(LogLevel.Debug, "HttpConnection.transport is undefined in HttpConnection.stop() because start() failed.");
  144. }
  145. }
  146. async _startInternal(transferFormat) {
  147. // Store the original base url and the access token factory since they may change
  148. // as part of negotiating
  149. let url = this.baseUrl;
  150. this._accessTokenFactory = this._options.accessTokenFactory;
  151. this._httpClient._accessTokenFactory = this._accessTokenFactory;
  152. try {
  153. if (this._options.skipNegotiation) {
  154. if (this._options.transport === HttpTransportType.WebSockets) {
  155. // No need to add a connection ID in this case
  156. this.transport = this._constructTransport(HttpTransportType.WebSockets);
  157. // We should just call connect directly in this case.
  158. // No fallback or negotiate in this case.
  159. await this._startTransport(url, transferFormat);
  160. }
  161. else {
  162. throw new Error("Negotiation can only be skipped when using the WebSocket transport directly.");
  163. }
  164. }
  165. else {
  166. let negotiateResponse = null;
  167. let redirects = 0;
  168. do {
  169. negotiateResponse = await this._getNegotiationResponse(url);
  170. // the user tries to stop the connection when it is being started
  171. if (this._connectionState === "Disconnecting" /* Disconnecting */ || this._connectionState === "Disconnected" /* Disconnected */) {
  172. throw new AbortError("The connection was stopped during negotiation.");
  173. }
  174. if (negotiateResponse.error) {
  175. throw new Error(negotiateResponse.error);
  176. }
  177. if (negotiateResponse.ProtocolVersion) {
  178. throw new 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.");
  179. }
  180. if (negotiateResponse.url) {
  181. url = negotiateResponse.url;
  182. }
  183. if (negotiateResponse.accessToken) {
  184. // Replace the current access token factory with one that uses
  185. // the returned access token
  186. const accessToken = negotiateResponse.accessToken;
  187. this._accessTokenFactory = () => accessToken;
  188. // set the factory to undefined so the AccessTokenHttpClient won't retry with the same token, since we know it won't change until a connection restart
  189. this._httpClient._accessToken = accessToken;
  190. this._httpClient._accessTokenFactory = undefined;
  191. }
  192. redirects++;
  193. } while (negotiateResponse.url && redirects < MAX_REDIRECTS);
  194. if (redirects === MAX_REDIRECTS && negotiateResponse.url) {
  195. throw new Error("Negotiate redirection limit exceeded.");
  196. }
  197. await this._createTransport(url, this._options.transport, negotiateResponse, transferFormat);
  198. }
  199. if (this.transport instanceof LongPollingTransport) {
  200. this.features.inherentKeepAlive = true;
  201. }
  202. if (this._connectionState === "Connecting" /* Connecting */) {
  203. // Ensure the connection transitions to the connected state prior to completing this.startInternalPromise.
  204. // start() will handle the case when stop was called and startInternal exits still in the disconnecting state.
  205. this._logger.log(LogLevel.Debug, "The HttpConnection connected successfully.");
  206. this._connectionState = "Connected" /* Connected */;
  207. }
  208. // stop() is waiting on us via this.startInternalPromise so keep this.transport around so it can clean up.
  209. // This is the only case startInternal can exit in neither the connected nor disconnected state because stopConnection()
  210. // will transition to the disconnected state. start() will wait for the transition using the stopPromise.
  211. }
  212. catch (e) {
  213. this._logger.log(LogLevel.Error, "Failed to start the connection: " + e);
  214. this._connectionState = "Disconnected" /* Disconnected */;
  215. this.transport = undefined;
  216. // if start fails, any active calls to stop assume that start will complete the stop promise
  217. this._stopPromiseResolver();
  218. return Promise.reject(e);
  219. }
  220. }
  221. async _getNegotiationResponse(url) {
  222. const headers = {};
  223. const [name, value] = getUserAgentHeader();
  224. headers[name] = value;
  225. const negotiateUrl = this._resolveNegotiateUrl(url);
  226. this._logger.log(LogLevel.Debug, `Sending negotiation request: ${negotiateUrl}.`);
  227. try {
  228. const response = await this._httpClient.post(negotiateUrl, {
  229. content: "",
  230. headers: { ...headers, ...this._options.headers },
  231. timeout: this._options.timeout,
  232. withCredentials: this._options.withCredentials,
  233. });
  234. if (response.statusCode !== 200) {
  235. return Promise.reject(new Error(`Unexpected status code returned from negotiate '${response.statusCode}'`));
  236. }
  237. const negotiateResponse = JSON.parse(response.content);
  238. if (!negotiateResponse.negotiateVersion || negotiateResponse.negotiateVersion < 1) {
  239. // Negotiate version 0 doesn't use connectionToken
  240. // So we set it equal to connectionId so all our logic can use connectionToken without being aware of the negotiate version
  241. negotiateResponse.connectionToken = negotiateResponse.connectionId;
  242. }
  243. return negotiateResponse;
  244. }
  245. catch (e) {
  246. let errorMessage = "Failed to complete negotiation with the server: " + e;
  247. if (e instanceof HttpError) {
  248. if (e.statusCode === 404) {
  249. errorMessage = errorMessage + " Either this is not a SignalR endpoint or there is a proxy blocking the connection.";
  250. }
  251. }
  252. this._logger.log(LogLevel.Error, errorMessage);
  253. return Promise.reject(new FailedToNegotiateWithServerError(errorMessage));
  254. }
  255. }
  256. _createConnectUrl(url, connectionToken) {
  257. if (!connectionToken) {
  258. return url;
  259. }
  260. return url + (url.indexOf("?") === -1 ? "?" : "&") + `id=${connectionToken}`;
  261. }
  262. async _createTransport(url, requestedTransport, negotiateResponse, requestedTransferFormat) {
  263. let connectUrl = this._createConnectUrl(url, negotiateResponse.connectionToken);
  264. if (this._isITransport(requestedTransport)) {
  265. this._logger.log(LogLevel.Debug, "Connection was provided an instance of ITransport, using that directly.");
  266. this.transport = requestedTransport;
  267. await this._startTransport(connectUrl, requestedTransferFormat);
  268. this.connectionId = negotiateResponse.connectionId;
  269. return;
  270. }
  271. const transportExceptions = [];
  272. const transports = negotiateResponse.availableTransports || [];
  273. let negotiate = negotiateResponse;
  274. for (const endpoint of transports) {
  275. const transportOrError = this._resolveTransportOrError(endpoint, requestedTransport, requestedTransferFormat);
  276. if (transportOrError instanceof Error) {
  277. // Store the error and continue, we don't want to cause a re-negotiate in these cases
  278. transportExceptions.push(`${endpoint.transport} failed:`);
  279. transportExceptions.push(transportOrError);
  280. }
  281. else if (this._isITransport(transportOrError)) {
  282. this.transport = transportOrError;
  283. if (!negotiate) {
  284. try {
  285. negotiate = await this._getNegotiationResponse(url);
  286. }
  287. catch (ex) {
  288. return Promise.reject(ex);
  289. }
  290. connectUrl = this._createConnectUrl(url, negotiate.connectionToken);
  291. }
  292. try {
  293. await this._startTransport(connectUrl, requestedTransferFormat);
  294. this.connectionId = negotiate.connectionId;
  295. return;
  296. }
  297. catch (ex) {
  298. this._logger.log(LogLevel.Error, `Failed to start the transport '${endpoint.transport}': ${ex}`);
  299. negotiate = undefined;
  300. transportExceptions.push(new FailedToStartTransportError(`${endpoint.transport} failed: ${ex}`, HttpTransportType[endpoint.transport]));
  301. if (this._connectionState !== "Connecting" /* Connecting */) {
  302. const message = "Failed to select transport before stop() was called.";
  303. this._logger.log(LogLevel.Debug, message);
  304. return Promise.reject(new AbortError(message));
  305. }
  306. }
  307. }
  308. }
  309. if (transportExceptions.length > 0) {
  310. return Promise.reject(new AggregateErrors(`Unable to connect to the server with any of the available transports. ${transportExceptions.join(" ")}`, transportExceptions));
  311. }
  312. return Promise.reject(new Error("None of the transports supported by the client are supported by the server."));
  313. }
  314. _constructTransport(transport) {
  315. switch (transport) {
  316. case HttpTransportType.WebSockets:
  317. if (!this._options.WebSocket) {
  318. throw new Error("'WebSocket' is not supported in your environment.");
  319. }
  320. return new WebSocketTransport(this._httpClient, this._accessTokenFactory, this._logger, this._options.logMessageContent, this._options.WebSocket, this._options.headers || {});
  321. case HttpTransportType.ServerSentEvents:
  322. if (!this._options.EventSource) {
  323. throw new Error("'EventSource' is not supported in your environment.");
  324. }
  325. return new ServerSentEventsTransport(this._httpClient, this._httpClient._accessToken, this._logger, this._options);
  326. case HttpTransportType.LongPolling:
  327. return new LongPollingTransport(this._httpClient, this._logger, this._options);
  328. default:
  329. throw new Error(`Unknown transport: ${transport}.`);
  330. }
  331. }
  332. _startTransport(url, transferFormat) {
  333. this.transport.onreceive = this.onreceive;
  334. this.transport.onclose = (e) => this._stopConnection(e);
  335. return this.transport.connect(url, transferFormat);
  336. }
  337. _resolveTransportOrError(endpoint, requestedTransport, requestedTransferFormat) {
  338. const transport = HttpTransportType[endpoint.transport];
  339. if (transport === null || transport === undefined) {
  340. this._logger.log(LogLevel.Debug, `Skipping transport '${endpoint.transport}' because it is not supported by this client.`);
  341. return new Error(`Skipping transport '${endpoint.transport}' because it is not supported by this client.`);
  342. }
  343. else {
  344. if (transportMatches(requestedTransport, transport)) {
  345. const transferFormats = endpoint.transferFormats.map((s) => TransferFormat[s]);
  346. if (transferFormats.indexOf(requestedTransferFormat) >= 0) {
  347. if ((transport === HttpTransportType.WebSockets && !this._options.WebSocket) ||
  348. (transport === HttpTransportType.ServerSentEvents && !this._options.EventSource)) {
  349. this._logger.log(LogLevel.Debug, `Skipping transport '${HttpTransportType[transport]}' because it is not supported in your environment.'`);
  350. return new UnsupportedTransportError(`'${HttpTransportType[transport]}' is not supported in your environment.`, transport);
  351. }
  352. else {
  353. this._logger.log(LogLevel.Debug, `Selecting transport '${HttpTransportType[transport]}'.`);
  354. try {
  355. return this._constructTransport(transport);
  356. }
  357. catch (ex) {
  358. return ex;
  359. }
  360. }
  361. }
  362. else {
  363. this._logger.log(LogLevel.Debug, `Skipping transport '${HttpTransportType[transport]}' because it does not support the requested transfer format '${TransferFormat[requestedTransferFormat]}'.`);
  364. return new Error(`'${HttpTransportType[transport]}' does not support ${TransferFormat[requestedTransferFormat]}.`);
  365. }
  366. }
  367. else {
  368. this._logger.log(LogLevel.Debug, `Skipping transport '${HttpTransportType[transport]}' because it was disabled by the client.`);
  369. return new DisabledTransportError(`'${HttpTransportType[transport]}' is disabled by the client.`, transport);
  370. }
  371. }
  372. }
  373. _isITransport(transport) {
  374. return transport && typeof (transport) === "object" && "connect" in transport;
  375. }
  376. _stopConnection(error) {
  377. this._logger.log(LogLevel.Debug, `HttpConnection.stopConnection(${error}) called while in state ${this._connectionState}.`);
  378. this.transport = undefined;
  379. // If we have a stopError, it takes precedence over the error from the transport
  380. error = this._stopError || error;
  381. this._stopError = undefined;
  382. if (this._connectionState === "Disconnected" /* Disconnected */) {
  383. this._logger.log(LogLevel.Debug, `Call to HttpConnection.stopConnection(${error}) was ignored because the connection is already in the disconnected state.`);
  384. return;
  385. }
  386. if (this._connectionState === "Connecting" /* Connecting */) {
  387. this._logger.log(LogLevel.Warning, `Call to HttpConnection.stopConnection(${error}) was ignored because the connection is still in the connecting state.`);
  388. throw new Error(`HttpConnection.stopConnection(${error}) was called while the connection is still in the connecting state.`);
  389. }
  390. if (this._connectionState === "Disconnecting" /* Disconnecting */) {
  391. // A call to stop() induced this call to stopConnection and needs to be completed.
  392. // Any stop() awaiters will be scheduled to continue after the onclose callback fires.
  393. this._stopPromiseResolver();
  394. }
  395. if (error) {
  396. this._logger.log(LogLevel.Error, `Connection disconnected with error '${error}'.`);
  397. }
  398. else {
  399. this._logger.log(LogLevel.Information, "Connection disconnected.");
  400. }
  401. if (this._sendQueue) {
  402. this._sendQueue.stop().catch((e) => {
  403. this._logger.log(LogLevel.Error, `TransportSendQueue.stop() threw error '${e}'.`);
  404. });
  405. this._sendQueue = undefined;
  406. }
  407. this.connectionId = undefined;
  408. this._connectionState = "Disconnected" /* Disconnected */;
  409. if (this._connectionStarted) {
  410. this._connectionStarted = false;
  411. try {
  412. if (this.onclose) {
  413. this.onclose(error);
  414. }
  415. }
  416. catch (e) {
  417. this._logger.log(LogLevel.Error, `HttpConnection.onclose(${error}) threw error '${e}'.`);
  418. }
  419. }
  420. }
  421. _resolveUrl(url) {
  422. // startsWith is not supported in IE
  423. if (url.lastIndexOf("https://", 0) === 0 || url.lastIndexOf("http://", 0) === 0) {
  424. return url;
  425. }
  426. if (!Platform.isBrowser) {
  427. throw new Error(`Cannot resolve '${url}'.`);
  428. }
  429. // Setting the url to the href propery of an anchor tag handles normalization
  430. // for us. There are 3 main cases.
  431. // 1. Relative path normalization e.g "b" -> "http://localhost:5000/a/b"
  432. // 2. Absolute path normalization e.g "/a/b" -> "http://localhost:5000/a/b"
  433. // 3. Networkpath reference normalization e.g "//localhost:5000/a/b" -> "http://localhost:5000/a/b"
  434. const aTag = window.document.createElement("a");
  435. aTag.href = url;
  436. this._logger.log(LogLevel.Information, `Normalizing '${url}' to '${aTag.href}'.`);
  437. return aTag.href;
  438. }
  439. _resolveNegotiateUrl(url) {
  440. const index = url.indexOf("?");
  441. let negotiateUrl = url.substring(0, index === -1 ? url.length : index);
  442. if (negotiateUrl[negotiateUrl.length - 1] !== "/") {
  443. negotiateUrl += "/";
  444. }
  445. negotiateUrl += "negotiate";
  446. negotiateUrl += index === -1 ? "" : url.substring(index);
  447. if (negotiateUrl.indexOf("negotiateVersion") === -1) {
  448. negotiateUrl += index === -1 ? "?" : "&";
  449. negotiateUrl += "negotiateVersion=" + this._negotiateVersion;
  450. }
  451. return negotiateUrl;
  452. }
  453. }
  454. function transportMatches(requestedTransport, actualTransport) {
  455. return !requestedTransport || ((actualTransport & requestedTransport) !== 0);
  456. }
  457. /** @private */
  458. export class TransportSendQueue {
  459. constructor(_transport) {
  460. this._transport = _transport;
  461. this._buffer = [];
  462. this._executing = true;
  463. this._sendBufferedData = new PromiseSource();
  464. this._transportResult = new PromiseSource();
  465. this._sendLoopPromise = this._sendLoop();
  466. }
  467. send(data) {
  468. this._bufferData(data);
  469. if (!this._transportResult) {
  470. this._transportResult = new PromiseSource();
  471. }
  472. return this._transportResult.promise;
  473. }
  474. stop() {
  475. this._executing = false;
  476. this._sendBufferedData.resolve();
  477. return this._sendLoopPromise;
  478. }
  479. _bufferData(data) {
  480. if (this._buffer.length && typeof (this._buffer[0]) !== typeof (data)) {
  481. throw new Error(`Expected data to be of type ${typeof (this._buffer)} but was of type ${typeof (data)}`);
  482. }
  483. this._buffer.push(data);
  484. this._sendBufferedData.resolve();
  485. }
  486. async _sendLoop() {
  487. while (true) {
  488. await this._sendBufferedData.promise;
  489. if (!this._executing) {
  490. if (this._transportResult) {
  491. this._transportResult.reject("Connection stopped.");
  492. }
  493. break;
  494. }
  495. this._sendBufferedData = new PromiseSource();
  496. const transportResult = this._transportResult;
  497. this._transportResult = undefined;
  498. const data = typeof (this._buffer[0]) === "string" ?
  499. this._buffer.join("") :
  500. TransportSendQueue._concatBuffers(this._buffer);
  501. this._buffer.length = 0;
  502. try {
  503. await this._transport.send(data);
  504. transportResult.resolve();
  505. }
  506. catch (error) {
  507. transportResult.reject(error);
  508. }
  509. }
  510. }
  511. static _concatBuffers(arrayBuffers) {
  512. const totalLength = arrayBuffers.map((b) => b.byteLength).reduce((a, b) => a + b);
  513. const result = new Uint8Array(totalLength);
  514. let offset = 0;
  515. for (const item of arrayBuffers) {
  516. result.set(new Uint8Array(item), offset);
  517. offset += item.byteLength;
  518. }
  519. return result.buffer;
  520. }
  521. }
  522. class PromiseSource {
  523. constructor() {
  524. this.promise = new Promise((resolve, reject) => [this._resolver, this._rejecter] = [resolve, reject]);
  525. }
  526. resolve() {
  527. this._resolver();
  528. }
  529. reject(reason) {
  530. this._rejecter(reason);
  531. }
  532. }
  533. //# sourceMappingURL=HttpConnection.js.map