HttpConnection.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  4. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  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) : adopt(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. import { LogLevel } from "./ILogger";
  40. import { HttpTransportType, TransferFormat } from "./ITransport";
  41. import { LongPollingTransport } from "./LongPollingTransport";
  42. import { Arg, createLogger } from "./Utils";
  43. import DefaultRequest from "./DefualtRequest";
  44. import { ResponseType } from "./wx-request/model/ResponseType";
  45. import { WxSocketTransport } from "./WxSocketTransport";
  46. var MAX_REDIRECTS = 100;
  47. var WxSocketModule = WxSocketTransport;
  48. var LongPollingModule = LongPollingTransport;
  49. /** @private */
  50. var HttpConnection = /** @class */ (function () {
  51. function HttpConnection(url, options) {
  52. if (options === void 0) { options = {}; }
  53. this.features = {};
  54. Arg.isRequired(url, "url");
  55. this.logger = createLogger(options.logger);
  56. options = options || {};
  57. // ! 这里修改为自定义解析 和 默认传入 全路径方式
  58. this.baseUrl = options.resolveUrl ? options.resolveUrl(url) : this.resolveUrl(url);
  59. options.logMessageContent = options.logMessageContent || false;
  60. // ! 修改 options 参数赋值方式
  61. if (!options.WxSocket && wx) {
  62. options.WxSocket = WxSocketModule;
  63. }
  64. if (!options.LongPolling) {
  65. options.LongPolling = LongPollingModule;
  66. }
  67. this.request = options.request || new DefaultRequest({}, this.logger);
  68. this.connectionState = 2 /* Disconnected */;
  69. this.options = options;
  70. this.onreceive = null;
  71. this.onclose = null;
  72. }
  73. HttpConnection.prototype.start = function (transferFormat) {
  74. transferFormat = transferFormat || TransferFormat.Binary;
  75. Arg.isIn(transferFormat, TransferFormat, "transferFormat");
  76. this.logger.log(LogLevel.Debug, "Starting connection with transfer format '" + TransferFormat[transferFormat] + "'.", TransferFormat);
  77. if (this.connectionState !== 2 /* Disconnected */) {
  78. return Promise.reject(new Error("Cannot start a connection that is not in the 'Disconnected' state. state is " + this.connectionState));
  79. }
  80. this.connectionState = 0 /* Connecting */;
  81. this.startPromise = this.startInternal(transferFormat);
  82. return this.startPromise;
  83. };
  84. HttpConnection.prototype.send = function (data) {
  85. if (this.connectionState !== 1 /* Connected */) {
  86. throw new Error("Cannot send data if the connection is not in the 'Connected' State.");
  87. }
  88. // Transport will not be null if state is connected
  89. return this.transport.send(data);
  90. };
  91. HttpConnection.prototype.stop = function (error) {
  92. return __awaiter(this, void 0, void 0, function () {
  93. var e_1;
  94. return __generator(this, function (_a) {
  95. switch (_a.label) {
  96. case 0:
  97. this.connectionState = 2 /* Disconnected */;
  98. // Set error as soon as possible otherwise there is a race between
  99. // the transport closing and providing an error and the error from a close message
  100. // We would prefer the close message error.
  101. this.stopError = error;
  102. _a.label = 1;
  103. case 1:
  104. _a.trys.push([1, 3, , 4]);
  105. return [4 /*yield*/, this.startPromise];
  106. case 2:
  107. _a.sent();
  108. return [3 /*break*/, 4];
  109. case 3:
  110. e_1 = _a.sent();
  111. return [3 /*break*/, 4];
  112. case 4:
  113. if (!this.transport) return [3 /*break*/, 6];
  114. return [4 /*yield*/, this.transport.stop()];
  115. case 5:
  116. _a.sent();
  117. this.transport = undefined;
  118. _a.label = 6;
  119. case 6: return [2 /*return*/];
  120. }
  121. });
  122. });
  123. };
  124. HttpConnection.prototype.startInternal = function (transferFormat) {
  125. return __awaiter(this, void 0, void 0, function () {
  126. var url, negotiateResponse, redirects, _loop_1, this_1, state_1, e_2;
  127. var _this = this;
  128. return __generator(this, function (_a) {
  129. switch (_a.label) {
  130. case 0:
  131. url = this.baseUrl;
  132. this.accessTokenFactory = this.options.accessTokenFactory;
  133. this.socketUrlFactory = this.options.socketUrlFactory;
  134. _a.label = 1;
  135. case 1:
  136. _a.trys.push([1, 12, , 13]);
  137. if (!this.options.skipNegotiation) return [3 /*break*/, 5];
  138. if (!(this.options.transport === HttpTransportType.WebSockets)) return [3 /*break*/, 3];
  139. // No need to add a connection ID in this case
  140. this.transport = this.constructTransport(HttpTransportType.WebSockets);
  141. // We should just call connect directly in this case.
  142. // No fallback or negotiate in this case.
  143. return [4 /*yield*/, this.transport.connect({
  144. url: url,
  145. header: {},
  146. protocols: [],
  147. tcpNoDelay: true,
  148. transferFormat: transferFormat
  149. })];
  150. case 2:
  151. // We should just call connect directly in this case.
  152. // No fallback or negotiate in this case.
  153. _a.sent();
  154. return [3 /*break*/, 4];
  155. case 3: throw Error("Negotiation can only be skipped when using the WxSocket transport directly.");
  156. case 4: return [3 /*break*/, 11];
  157. case 5:
  158. negotiateResponse = null;
  159. redirects = 0;
  160. _loop_1 = function () {
  161. var accessToken_1;
  162. return __generator(this, function (_a) {
  163. switch (_a.label) {
  164. case 0: return [4 /*yield*/, this_1.getNegotiationResponse(url)];
  165. case 1:
  166. negotiateResponse = _a.sent();
  167. // the user tries to stop the connection when it is being started
  168. if (this_1.connectionState === 2 /* Disconnected */) {
  169. return [2 /*return*/, { value: void 0 }];
  170. }
  171. if (negotiateResponse.error) {
  172. throw Error(negotiateResponse.error);
  173. }
  174. if (negotiateResponse.ProtocolVersion) {
  175. throw Error("检测到尝试连接到一个 非 ASP.NET Core 服务器。此客户端仅支持连接到ASP.NET Core 服务器。. See https://aka.ms/signalr-core-differences for details.");
  176. }
  177. if (negotiateResponse.url) {
  178. url = negotiateResponse.url;
  179. }
  180. if (negotiateResponse.accessToken) {
  181. accessToken_1 = negotiateResponse.accessToken;
  182. // ! 通过 /negotiate 接口返回的assessToken 仅支持 accessTokenFactory(),如果实现了 socketUrlFactory(),会忽略掉这个token
  183. this_1.accessTokenFactory = function () { return accessToken_1; };
  184. }
  185. redirects++;
  186. return [2 /*return*/];
  187. }
  188. });
  189. };
  190. this_1 = this;
  191. _a.label = 6;
  192. case 6: return [5 /*yield**/, _loop_1()];
  193. case 7:
  194. state_1 = _a.sent();
  195. if (typeof state_1 === "object")
  196. return [2 /*return*/, state_1.value];
  197. _a.label = 8;
  198. case 8:
  199. if (negotiateResponse.url && redirects < MAX_REDIRECTS) return [3 /*break*/, 6];
  200. _a.label = 9;
  201. case 9:
  202. if (redirects === MAX_REDIRECTS && negotiateResponse.url) {
  203. throw Error("Negotiate redirection limit exceeded. -fy : 超出协商重定向限制");
  204. }
  205. return [4 /*yield*/, this.createTransport(url, this.options.transport, negotiateResponse, transferFormat)];
  206. case 10:
  207. _a.sent();
  208. _a.label = 11;
  209. case 11:
  210. if (this.transport instanceof LongPollingTransport) {
  211. this.features.inherentKeepAlive = true;
  212. }
  213. this.transport.onreceive = this.onreceive;
  214. this.transport.onclose = function (e) { return _this.stopConnection(e); };
  215. // only change the state if we were connecting to not overwrite
  216. // the state if the connection is already marked as Disconnected
  217. this.changeState(0 /* Connecting */, 1 /* Connected */);
  218. return [2 /*return*/];
  219. case 12:
  220. e_2 = _a.sent();
  221. this.logger.log(LogLevel.Error, "Failed to start the connection: ", e_2);
  222. this.connectionState = 2 /* Disconnected */;
  223. this.transport = undefined;
  224. throw e_2;
  225. case 13: return [2 /*return*/];
  226. }
  227. });
  228. });
  229. };
  230. HttpConnection.prototype.getNegotiationResponse = function (url) {
  231. return __awaiter(this, void 0, void 0, function () {
  232. var headers, token, negotiateUrl, response, e_3;
  233. var _a;
  234. return __generator(this, function (_b) {
  235. switch (_b.label) {
  236. case 0:
  237. if (!this.accessTokenFactory) return [3 /*break*/, 2];
  238. return [4 /*yield*/, this.accessTokenFactory()];
  239. case 1:
  240. token = _b.sent();
  241. if (token) {
  242. headers = (_a = {},
  243. _a["Authorization"] = "Bearer " + token,
  244. _a);
  245. }
  246. _b.label = 2;
  247. case 2:
  248. negotiateUrl = this.resolveNegotiateUrl(url);
  249. this.logger.log(LogLevel.Debug, "Sending negotiation request: " + negotiateUrl + ".");
  250. _b.label = 3;
  251. case 3:
  252. _b.trys.push([3, 5, , 6]);
  253. return [4 /*yield*/, this.request.post(negotiateUrl, {}, {
  254. headers: headers,
  255. responseType: ResponseType.TEXT
  256. })];
  257. case 4:
  258. response = _b.sent();
  259. if (response.statusCode !== 200) {
  260. throw Error("Unexpected status code returned from negotiate " + response.statusCode);
  261. }
  262. return [2 /*return*/, JSON.parse(response.data)];
  263. case 5:
  264. e_3 = _b.sent();
  265. this.logger.log(LogLevel.Error, "Failed to complete negotiation with the server: ", e_3);
  266. throw e_3;
  267. case 6: return [2 /*return*/];
  268. }
  269. });
  270. });
  271. };
  272. HttpConnection.prototype.createConnectUrl = function (url, connectionId) {
  273. if (!connectionId) {
  274. return url;
  275. }
  276. return url + (url.indexOf("?") === -1 ? "?" : "&") + ("id=" + connectionId);
  277. };
  278. HttpConnection.prototype.createTransport = function (url, requestedTransport, negotiateResponse, requestedTransferFormat) {
  279. return __awaiter(this, void 0, void 0, function () {
  280. var connectUrl, transports, _i, transports_1, endpoint, transport, ex_1;
  281. return __generator(this, function (_a) {
  282. switch (_a.label) {
  283. case 0:
  284. connectUrl = this.createConnectUrl(url, negotiateResponse.connectionId);
  285. if (!this.isITransport(requestedTransport)) return [3 /*break*/, 2];
  286. this.logger.log(LogLevel.Debug, "Connection was provided an instance of ITransport, using that directly.");
  287. this.transport = requestedTransport;
  288. return [4 /*yield*/, this.transport.connect({
  289. url: connectUrl,
  290. transferFormat: requestedTransferFormat
  291. })];
  292. case 1:
  293. _a.sent();
  294. // only change the state if we were connecting to not overwrite
  295. // the state if the connection is already marked as Disconnected
  296. this.changeState(0 /* Connecting */, 1 /* Connected */);
  297. return [2 /*return*/];
  298. case 2:
  299. transports = negotiateResponse.availableTransports || [];
  300. _i = 0, transports_1 = transports;
  301. _a.label = 3;
  302. case 3:
  303. if (!(_i < transports_1.length)) return [3 /*break*/, 9];
  304. endpoint = transports_1[_i];
  305. this.connectionState = 0 /* Connecting */;
  306. transport = this.resolveTransport(endpoint, requestedTransport, requestedTransferFormat);
  307. if (!(typeof transport === "number")) return [3 /*break*/, 8];
  308. this.transport = this.constructTransport(transport);
  309. if (!!negotiateResponse.connectionId) return [3 /*break*/, 5];
  310. return [4 /*yield*/, this.getNegotiationResponse(url)];
  311. case 4:
  312. negotiateResponse = _a.sent();
  313. connectUrl = this.createConnectUrl(url, negotiateResponse.connectionId);
  314. _a.label = 5;
  315. case 5:
  316. _a.trys.push([5, 7, , 8]);
  317. return [4 /*yield*/, this.transport.connect({
  318. url: connectUrl,
  319. transferFormat: requestedTransferFormat
  320. })];
  321. case 6:
  322. _a.sent();
  323. this.changeState(0 /* Connecting */, 1 /* Connected */);
  324. return [2 /*return*/];
  325. case 7:
  326. ex_1 = _a.sent();
  327. this.logger.log(LogLevel.Error, "Failed to start the transport '" + HttpTransportType[transport] + "':", ex_1);
  328. this.connectionState = 2 /* Disconnected */;
  329. negotiateResponse.connectionId = undefined;
  330. return [3 /*break*/, 8];
  331. case 8:
  332. _i++;
  333. return [3 /*break*/, 3];
  334. case 9: throw new Error("Unable to initialize any of the available transports.");
  335. }
  336. });
  337. });
  338. };
  339. /**
  340. *
  341. * @description 这里对原来的实例化方式进行了改写,如果传入的是实例化完成的 Transport ,将直接返回
  342. * 如果是传入继承 Transport的 class,将执行 new Transport(options)
  343. * - 这里对原生的多项入参合并成了options(这点差异需要注意)
  344. * @private
  345. * @param {HttpTransportType} transport
  346. * @returns
  347. * @memberof HttpConnection
  348. */
  349. HttpConnection.prototype.constructTransport = function (transport) {
  350. var _a = this.options, WxSocket = _a.WxSocket, LongPolling = _a.LongPolling, wxSocketTransportOptions = _a.wxSocketTransportOptions, longPollingTransportOptions = _a.longPollingTransportOptions;
  351. switch (transport) {
  352. case HttpTransportType.WebSockets: // wx socket 方式
  353. if (WxSocket instanceof WxSocketTransport) {
  354. return WxSocket;
  355. }
  356. else {
  357. return new WxSocket(wxSocketTransportOptions
  358. ? wxSocketTransportOptions
  359. : {
  360. // token 工厂
  361. accessTokenFactory: this.accessTokenFactory,
  362. // socket 单独实现一个socket url factory(用于后端改了 accecc_token 参数名的场景)
  363. socketUrlFactory: this.socketUrlFactory,
  364. // logger
  365. logger: this.logger,
  366. logMessageContent: this.options.logMessageContent || false,
  367. /** 是否允许替换socket连接
  368. *
  369. * 小程序 版本 < 1.7.0 时, 最多允许存在一个socket连接, 此参数用于是否允许在这个情况下,替换这个打开的socket
  370. */
  371. allowReplaceSocket: true,
  372. /** 是否启用消息队列缓存连接建立前消息,并在建立连接后发送 */
  373. enableMessageQueue: this.options.enableMessageQueue == undefined ? true : this.options.enableMessageQueue,
  374. /** 重连设置 */
  375. reconnect: {
  376. enable: true,
  377. max: 3
  378. }
  379. });
  380. }
  381. case HttpTransportType.LongPolling: // 长轮询方式
  382. if (LongPolling instanceof LongPollingTransport) {
  383. return LongPolling;
  384. }
  385. else {
  386. return new LongPolling(longPollingTransportOptions
  387. ? longPollingTransportOptions
  388. : {
  389. request: this.request,
  390. accessTokenFactory: this.accessTokenFactory,
  391. logger: this.logger,
  392. logMessageContent: this.options.logMessageContent || false
  393. });
  394. }
  395. default:
  396. throw new Error("Unknown transport: " + transport + ".");
  397. }
  398. };
  399. HttpConnection.prototype.resolveTransport = function (endpoint, requestedTransport, requestedTransferFormat) {
  400. var transport = HttpTransportType[endpoint.transport];
  401. if (transport === null || transport === undefined) {
  402. this.logger.log(LogLevel.Debug, "Skipping transport '" + endpoint.transport + "' because it is not supported by this client.");
  403. }
  404. else {
  405. var transferFormats = endpoint.transferFormats.map(function (s) { return TransferFormat[s]; });
  406. if (transportMatches(requestedTransport, transport)) {
  407. if (transferFormats.indexOf(requestedTransferFormat) >= 0) {
  408. if (transport === HttpTransportType.WebSockets && !this.options.WxSocket) {
  409. this.logger.log(LogLevel.Debug, "Skipping transport '" + HttpTransportType[transport] + "' because it is not supported in your environment.'");
  410. }
  411. else {
  412. this.logger.log(LogLevel.Debug, "Selecting transport '" + HttpTransportType[transport] + "'.");
  413. return transport;
  414. }
  415. }
  416. else {
  417. this.logger.log(LogLevel.Debug, "Skipping transport '" + HttpTransportType[transport] + "' because it does not support the requested transfer format '" + TransferFormat[requestedTransferFormat] + "'.");
  418. }
  419. }
  420. else {
  421. this.logger.log(LogLevel.Debug, "Skipping transport '" + HttpTransportType[transport] + "' because it was disabled by the client.");
  422. }
  423. }
  424. return null;
  425. };
  426. HttpConnection.prototype.isITransport = function (transport) {
  427. return transport && typeof transport === "object" && "connect" in transport;
  428. };
  429. HttpConnection.prototype.changeState = function (from, to) {
  430. if (this.connectionState === from) {
  431. this.connectionState = to;
  432. return true;
  433. }
  434. return false;
  435. };
  436. HttpConnection.prototype.stopConnection = function (error) {
  437. this.transport = undefined;
  438. // If we have a stopError, it takes precedence over the error from the transport
  439. error = this.stopError || error;
  440. if (error) {
  441. this.logger.log(LogLevel.Error, "Connection disconnected with error '" + error + "'.");
  442. }
  443. else {
  444. this.logger.log(LogLevel.Information, "Connection disconnected.");
  445. }
  446. this.connectionState = 2 /* Disconnected */;
  447. if (this.onclose) {
  448. this.onclose(error);
  449. }
  450. };
  451. /**
  452. * ! 由于小程序内必须指定 BaseUrl 关系,这里如果不是全路径的话,暂时直接抛出异常
  453. * @param url
  454. */
  455. HttpConnection.prototype.resolveUrl = function (url) {
  456. // startsWith is not supported in IE
  457. if (url.lastIndexOf("https://", 0) === 0 || url.lastIndexOf("http://", 0) === 0) {
  458. return url;
  459. }
  460. else {
  461. throw new Error("HttpConnection. \u89E3\u6790url\u9519\u8BEF,\u5C0F\u7A0B\u5E8F\u5185\u9700\u8981\u4F20\u5165\u5168\u8DEF\u5F84 ->link: " + url);
  462. }
  463. };
  464. HttpConnection.prototype.resolveNegotiateUrl = function (url) {
  465. var index = url.indexOf("?");
  466. var negotiateUrl = url.substring(0, index === -1 ? url.length : index);
  467. if (negotiateUrl[negotiateUrl.length - 1] !== "/") {
  468. negotiateUrl += "/";
  469. }
  470. negotiateUrl += "negotiate";
  471. negotiateUrl += index === -1 ? "" : url.substring(index);
  472. return negotiateUrl;
  473. };
  474. return HttpConnection;
  475. }());
  476. export { HttpConnection };
  477. function transportMatches(requestedTransport, actualTransport) {
  478. return !requestedTransport || (actualTransport & requestedTransport) !== 0;
  479. }