transport.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.perMessageDeflate = exports.WebSocketTransport = void 0;
  6. var _utilsBundle = require("../utilsBundle");
  7. var _utils = require("../utils");
  8. var _happyEyeballs = require("../utils/happy-eyeballs");
  9. /**
  10. * Copyright 2018 Google Inc. All rights reserved.
  11. * Modifications copyright (c) Microsoft Corporation.
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License");
  14. * you may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. */
  25. const perMessageDeflate = exports.perMessageDeflate = {
  26. zlibDeflateOptions: {
  27. level: 3
  28. },
  29. zlibInflateOptions: {
  30. chunkSize: 10 * 1024
  31. },
  32. threshold: 10 * 1024
  33. };
  34. class WebSocketTransport {
  35. static async connect(progress, url, headers, followRedirects, debugLogHeader) {
  36. return await WebSocketTransport._connect(progress, url, headers || {}, {
  37. follow: !!followRedirects,
  38. hadRedirects: false
  39. }, debugLogHeader);
  40. }
  41. static async _connect(progress, url, headers, redirect, debugLogHeader) {
  42. const logUrl = stripQueryParams(url);
  43. progress === null || progress === void 0 ? void 0 : progress.log(`<ws connecting> ${logUrl}`);
  44. const transport = new WebSocketTransport(progress, url, logUrl, headers, redirect.follow && redirect.hadRedirects, debugLogHeader);
  45. let success = false;
  46. progress === null || progress === void 0 ? void 0 : progress.cleanupWhenAborted(async () => {
  47. if (!success) await transport.closeAndWait().catch(e => null);
  48. });
  49. const result = await new Promise((fulfill, reject) => {
  50. transport._ws.on('open', async () => {
  51. progress === null || progress === void 0 ? void 0 : progress.log(`<ws connected> ${logUrl}`);
  52. fulfill({
  53. transport
  54. });
  55. });
  56. transport._ws.on('error', event => {
  57. progress === null || progress === void 0 ? void 0 : progress.log(`<ws connect error> ${logUrl} ${event.message}`);
  58. reject(new Error('WebSocket error: ' + event.message));
  59. transport._ws.close();
  60. });
  61. transport._ws.on('unexpected-response', (request, response) => {
  62. if (redirect.follow && !redirect.hadRedirects && (response.statusCode === 301 || response.statusCode === 302 || response.statusCode === 307 || response.statusCode === 308)) {
  63. fulfill({
  64. redirect: response
  65. });
  66. transport._ws.close();
  67. return;
  68. }
  69. for (let i = 0; i < response.rawHeaders.length; i += 2) {
  70. if (debugLogHeader && response.rawHeaders[i] === debugLogHeader) progress === null || progress === void 0 ? void 0 : progress.log(response.rawHeaders[i + 1]);
  71. }
  72. const chunks = [];
  73. const errorPrefix = `${logUrl} ${response.statusCode} ${response.statusMessage}`;
  74. response.on('data', chunk => chunks.push(chunk));
  75. response.on('close', () => {
  76. const error = chunks.length ? `${errorPrefix}\n${Buffer.concat(chunks)}` : errorPrefix;
  77. progress === null || progress === void 0 ? void 0 : progress.log(`<ws unexpected response> ${error}`);
  78. reject(new Error('WebSocket error: ' + error));
  79. transport._ws.close();
  80. });
  81. });
  82. });
  83. if (result.redirect) {
  84. // Strip access key headers from the redirected request.
  85. const newHeaders = Object.fromEntries(Object.entries(headers || {}).filter(([name]) => !name.includes('access-key')));
  86. return WebSocketTransport._connect(progress, result.redirect.headers.location, newHeaders, {
  87. follow: true,
  88. hadRedirects: true
  89. }, debugLogHeader);
  90. }
  91. success = true;
  92. return transport;
  93. }
  94. constructor(progress, url, logUrl, headers, followRedirects, debugLogHeader) {
  95. var _progress$timeUntilDe;
  96. this._ws = void 0;
  97. this._progress = void 0;
  98. this._logUrl = void 0;
  99. this.onmessage = void 0;
  100. this.onclose = void 0;
  101. this.wsEndpoint = void 0;
  102. this.headers = [];
  103. this.wsEndpoint = url;
  104. this._logUrl = logUrl;
  105. this._ws = new _utilsBundle.ws(url, [], {
  106. maxPayload: 256 * 1024 * 1024,
  107. // 256Mb,
  108. // Prevent internal http client error when passing negative timeout.
  109. handshakeTimeout: Math.max((_progress$timeUntilDe = progress === null || progress === void 0 ? void 0 : progress.timeUntilDeadline()) !== null && _progress$timeUntilDe !== void 0 ? _progress$timeUntilDe : 30_000, 1),
  110. headers,
  111. followRedirects,
  112. agent: /^(https|wss):\/\//.test(url) ? _happyEyeballs.httpsHappyEyeballsAgent : _happyEyeballs.httpHappyEyeballsAgent,
  113. perMessageDeflate
  114. });
  115. this._ws.on('upgrade', response => {
  116. for (let i = 0; i < response.rawHeaders.length; i += 2) {
  117. this.headers.push({
  118. name: response.rawHeaders[i],
  119. value: response.rawHeaders[i + 1]
  120. });
  121. if (debugLogHeader && response.rawHeaders[i] === debugLogHeader) progress === null || progress === void 0 ? void 0 : progress.log(response.rawHeaders[i + 1]);
  122. }
  123. });
  124. this._progress = progress;
  125. // The 'ws' module in node sometimes sends us multiple messages in a single task.
  126. // In Web, all IO callbacks (e.g. WebSocket callbacks)
  127. // are dispatched into separate tasks, so there's no need
  128. // to do anything extra.
  129. const messageWrap = (0, _utils.makeWaitForNextTask)();
  130. this._ws.addEventListener('message', event => {
  131. messageWrap(() => {
  132. const eventData = event.data;
  133. let parsedJson;
  134. try {
  135. parsedJson = JSON.parse(eventData);
  136. } catch (e) {
  137. var _this$_progress;
  138. (_this$_progress = this._progress) === null || _this$_progress === void 0 ? void 0 : _this$_progress.log(`<closing ws> Closing websocket due to malformed JSON. eventData=${eventData} e=${e === null || e === void 0 ? void 0 : e.message}`);
  139. this._ws.close();
  140. return;
  141. }
  142. try {
  143. if (this.onmessage) this.onmessage.call(null, parsedJson);
  144. } catch (e) {
  145. var _this$_progress2;
  146. (_this$_progress2 = this._progress) === null || _this$_progress2 === void 0 ? void 0 : _this$_progress2.log(`<closing ws> Closing websocket due to failed onmessage callback. eventData=${eventData} e=${e === null || e === void 0 ? void 0 : e.message}`);
  147. this._ws.close();
  148. }
  149. });
  150. });
  151. this._ws.addEventListener('close', event => {
  152. var _this$_progress3;
  153. (_this$_progress3 = this._progress) === null || _this$_progress3 === void 0 ? void 0 : _this$_progress3.log(`<ws disconnected> ${logUrl} code=${event.code} reason=${event.reason}`);
  154. if (this.onclose) this.onclose.call(null);
  155. });
  156. // Prevent Error: read ECONNRESET.
  157. this._ws.addEventListener('error', error => {
  158. var _this$_progress4;
  159. return (_this$_progress4 = this._progress) === null || _this$_progress4 === void 0 ? void 0 : _this$_progress4.log(`<ws error> ${logUrl} ${error.type} ${error.message}`);
  160. });
  161. }
  162. send(message) {
  163. this._ws.send(JSON.stringify(message));
  164. }
  165. close() {
  166. var _this$_progress5;
  167. (_this$_progress5 = this._progress) === null || _this$_progress5 === void 0 ? void 0 : _this$_progress5.log(`<ws disconnecting> ${this._logUrl}`);
  168. this._ws.close();
  169. }
  170. async closeAndWait() {
  171. if (this._ws.readyState === _utilsBundle.ws.CLOSED) return;
  172. const promise = new Promise(f => this._ws.once('close', f));
  173. this.close();
  174. await promise; // Make sure to await the actual disconnect.
  175. }
  176. }
  177. exports.WebSocketTransport = WebSocketTransport;
  178. function stripQueryParams(url) {
  179. try {
  180. const u = new URL(url);
  181. u.search = '';
  182. u.hash = '';
  183. return u.toString();
  184. } catch {
  185. return url;
  186. }
  187. }