index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*!
  2. * TSRPC Browser v3.4.16
  3. * -----------------------------------------
  4. * Copyright (c) King Wang.
  5. * MIT License
  6. * https://github.com/k8w/tsrpc-browser
  7. */
  8. 'use strict';
  9. Object.defineProperty(exports, '__esModule', { value: true });
  10. require('k8w-extend-native');
  11. require('core-js/modules/es.object.to-string.js');
  12. require('core-js/modules/es.regexp.to-string.js');
  13. require('core-js/modules/es.number.max-safe-integer.js');
  14. require('core-js/modules/es.number.constructor.js');
  15. require('core-js/modules/es.regexp.exec.js');
  16. require('core-js/modules/es.regexp.test.js');
  17. require('core-js/modules/es.array.iterator.js');
  18. require('core-js/modules/es.array-buffer.slice.js');
  19. require('core-js/modules/es.typed-array.uint8-array.js');
  20. require('core-js/modules/esnext.typed-array.at.js');
  21. require('core-js/modules/es.typed-array.copy-within.js');
  22. require('core-js/modules/es.typed-array.every.js');
  23. require('core-js/modules/es.typed-array.fill.js');
  24. require('core-js/modules/es.typed-array.filter.js');
  25. require('core-js/modules/es.typed-array.find.js');
  26. require('core-js/modules/es.typed-array.find-index.js');
  27. require('core-js/modules/es.typed-array.for-each.js');
  28. require('core-js/modules/es.typed-array.includes.js');
  29. require('core-js/modules/es.typed-array.index-of.js');
  30. require('core-js/modules/es.typed-array.iterator.js');
  31. require('core-js/modules/es.typed-array.join.js');
  32. require('core-js/modules/es.typed-array.last-index-of.js');
  33. require('core-js/modules/es.typed-array.map.js');
  34. require('core-js/modules/es.typed-array.reduce.js');
  35. require('core-js/modules/es.typed-array.reduce-right.js');
  36. require('core-js/modules/es.typed-array.reverse.js');
  37. require('core-js/modules/es.typed-array.set.js');
  38. require('core-js/modules/es.typed-array.slice.js');
  39. require('core-js/modules/es.typed-array.some.js');
  40. require('core-js/modules/es.typed-array.sort.js');
  41. require('core-js/modules/es.typed-array.subarray.js');
  42. require('core-js/modules/es.typed-array.to-locale-string.js');
  43. require('core-js/modules/es.typed-array.to-string.js');
  44. require('core-js/modules/es.array.from.js');
  45. require('core-js/modules/es.string.iterator.js');
  46. require('core-js/modules/es.number.parse-int.js');
  47. require('core-js/modules/es.array.join.js');
  48. require('core-js/modules/es.string.match.js');
  49. require('core-js/modules/es.function.name.js');
  50. require('core-js/modules/es.json.stringify.js');
  51. require('core-js/modules/es.promise.js');
  52. require('core-js/modules/es.regexp.constructor.js');
  53. require('core-js/modules/es.regexp.sticky.js');
  54. require('core-js/modules/web.dom-collections.for-each.js');
  55. require('core-js/modules/es.array.filter.js');
  56. require('core-js/modules/es.object.keys.js');
  57. require('core-js/modules/es.array.find-index.js');
  58. require('core-js/modules/es.array.splice.js');
  59. require('core-js/modules/es.array.slice.js');
  60. require('core-js/modules/es.array.find.js');
  61. require('core-js/modules/es.string.ends-with.js');
  62. require('core-js/modules/es.array.includes.js');
  63. require('core-js/modules/es.string.includes.js');
  64. require('core-js/modules/es.string.starts-with.js');
  65. require('core-js/modules/es.typed-array.int8-array.js');
  66. require('core-js/modules/es.typed-array.int16-array.js');
  67. require('core-js/modules/es.typed-array.int32-array.js');
  68. require('core-js/modules/es.typed-array.uint16-array.js');
  69. require('core-js/modules/es.typed-array.uint32-array.js');
  70. require('core-js/modules/es.typed-array.float32-array.js');
  71. require('core-js/modules/es.typed-array.float64-array.js');
  72. require('core-js/modules/es.array.map.js');
  73. require('core-js/modules/es.object.assign.js');
  74. require('core-js/modules/es.array.index-of.js');
  75. require('core-js/modules/web.dom-collections.iterator.js');
  76. require('core-js/modules/es.array-buffer.constructor.js');
  77. require('core-js/modules/es.array-buffer.is-view.js');
  78. require('core-js/modules/web.url.to-json.js');
  79. require('core-js/modules/es.object.entries.js');
  80. require('core-js/modules/es.number.is-integer.js');
  81. require('core-js/modules/es.object.get-prototype-of.js');
  82. require('core-js/modules/es.parse-int.js');
  83. var tsrpcProto = require('tsrpc-proto');
  84. var tslib = require('tslib');
  85. var tsrpcBaseClient = require('tsrpc-base-client');
  86. /**
  87. * @internal
  88. */
  89. var HttpProxy = /** @class */ (function () {
  90. function HttpProxy() {
  91. }
  92. HttpProxy.prototype.fetch = function (options) {
  93. var _this = this;
  94. var _a;
  95. var rs;
  96. var promise = new Promise(function (_rs) {
  97. rs = _rs;
  98. });
  99. var xhr = new XMLHttpRequest();
  100. if (typeof navigator !== 'undefined' && ((_a = navigator === null || navigator === void 0 ? void 0 : navigator.userAgent) === null || _a === void 0 ? void 0 : _a.indexOf('MSIE 8.0;')) > -1) {
  101. //IE8 不支持onload onabort onerror事件
  102. xhr.onreadystatechange = function () { return tslib.__awaiter(_this, void 0, void 0, function () {
  103. return tslib.__generator(this, function (_a) {
  104. if (xhr.readyState == 4) {
  105. //Network Error
  106. if (xhr.status == 0 || (xhr.response == null && xhr.responseText == null)) {
  107. rs({
  108. isSucc: false,
  109. err: new tsrpcProto.TsrpcError('Network Error', {
  110. type: tsrpcProto.TsrpcError.Type.NetworkError,
  111. httpCode: xhr.status
  112. })
  113. });
  114. return [2 /*return*/];
  115. }
  116. //IE9 wrongURL 会返回12029
  117. if (xhr.status == 12029) {
  118. rs({
  119. isSucc: false,
  120. err: new tsrpcProto.TsrpcError({
  121. message: 'Network Error',
  122. type: tsrpcProto.TsrpcError.Type.NetworkError,
  123. httpCode: xhr.status
  124. })
  125. });
  126. return [2 /*return*/];
  127. }
  128. // Res
  129. rs({
  130. isSucc: true,
  131. res: options.responseType === 'text' ? xhr.responseText : new Uint8Array(xhr.response)
  132. });
  133. }
  134. return [2 /*return*/];
  135. });
  136. }); };
  137. }
  138. else {
  139. xhr.onerror = function () {
  140. rs({
  141. isSucc: false,
  142. err: new tsrpcProto.TsrpcError({
  143. message: 'Network Error',
  144. type: tsrpcProto.TsrpcError.Type.NetworkError,
  145. httpCode: xhr.status
  146. })
  147. });
  148. };
  149. // 有的平台 超时不触发onerror
  150. xhr.ontimeout = function () {
  151. rs({
  152. isSucc: false,
  153. err: new tsrpcProto.TsrpcError({
  154. message: 'Request Timeout',
  155. type: tsrpcProto.TsrpcError.Type.NetworkError,
  156. code: 'TIMEOUT'
  157. })
  158. });
  159. };
  160. // Res
  161. xhr.onload = function () { return tslib.__awaiter(_this, void 0, void 0, function () {
  162. return tslib.__generator(this, function (_a) {
  163. if (xhr.status === 200 || xhr.status === 500) {
  164. rs({
  165. isSucc: true,
  166. res: xhr.response && (options.responseType === 'text' ? xhr.responseText : new Uint8Array(xhr.response))
  167. });
  168. }
  169. else {
  170. rs({
  171. isSucc: false,
  172. err: new tsrpcProto.TsrpcError({
  173. message: 'HTTP Error ' + xhr.status,
  174. type: tsrpcProto.TsrpcError.Type.ServerError,
  175. httpCode: xhr.status
  176. })
  177. });
  178. }
  179. return [2 /*return*/];
  180. });
  181. }); };
  182. var transportOptions_1 = options.transportOptions;
  183. if (!!transportOptions_1.onProgress) {
  184. xhr.upload.onprogress = function (e) {
  185. var _a;
  186. (_a = transportOptions_1.onProgress) === null || _a === void 0 ? void 0 : _a.call(transportOptions_1, e.loaded / e.total);
  187. };
  188. }
  189. }
  190. xhr.open(options.method, options.url, true);
  191. if (options.headers) {
  192. for (var key in options.headers) {
  193. xhr.setRequestHeader(key, options.headers[key]);
  194. }
  195. }
  196. xhr.responseType = options.responseType;
  197. var timeout = options.timeout;
  198. if (timeout) {
  199. xhr.timeout = timeout;
  200. }
  201. xhr.send(options.data);
  202. var abort = xhr.abort.bind(xhr);
  203. return {
  204. promise: promise,
  205. abort: abort
  206. };
  207. };
  208. return HttpProxy;
  209. }());
  210. /**
  211. * HTTP Client for TSRPC.
  212. * It uses XMLHttpRequest to send requests.
  213. * @typeParam ServiceType - `ServiceType` from generated `proto.ts`
  214. */
  215. var HttpClient = /** @class */ (function (_super) {
  216. tslib.__extends(HttpClient, _super);
  217. function HttpClient(proto, options) {
  218. var httpProxy = new HttpProxy;
  219. return _super.call(this, proto, httpProxy, tslib.__assign(tslib.__assign({}, defaultHttpClientOptions), options)) || this;
  220. }
  221. HttpClient.prototype.callApi = function (apiName, req, options) {
  222. if (options === void 0) { options = {}; }
  223. return _super.prototype.callApi.call(this, apiName, req, options);
  224. };
  225. HttpClient.prototype.sendMsg = function (msgName, msg, options) {
  226. if (options === void 0) { options = {}; }
  227. return _super.prototype.sendMsg.call(this, msgName, msg, options);
  228. };
  229. return HttpClient;
  230. }(tsrpcBaseClient.BaseHttpClient));
  231. var defaultHttpClientOptions = tslib.__assign(tslib.__assign({}, tsrpcBaseClient.defaultBaseHttpClientOptions), { customObjectIdClass: String });
  232. /**
  233. * @internal
  234. */
  235. var WebSocketProxy = /** @class */ (function () {
  236. function WebSocketProxy(caUrl) {
  237. this.caUrl = caUrl;
  238. }
  239. WebSocketProxy.prototype.connect = function (server, protocols) {
  240. var _this = this;
  241. if (this.caUrl) {
  242. // @ts-ignore
  243. this._ws = new WebSocket(server, protocols, this.caUrl);
  244. }
  245. else {
  246. this._ws = new WebSocket(server, protocols);
  247. }
  248. this._ws.binaryType = 'arraybuffer';
  249. this._ws.onopen = this.options.onOpen;
  250. this._ws.onerror = this.options.onError;
  251. this._ws.onclose = function (e) {
  252. _this.options.onClose(e.code, e.reason);
  253. _this._ws = undefined;
  254. };
  255. this._ws.onmessage = function (e) {
  256. var _a;
  257. if (e.data instanceof ArrayBuffer) {
  258. _this.options.onMessage(new Uint8Array(e.data));
  259. }
  260. else if (typeof e.data === 'string') {
  261. _this.options.onMessage(e.data);
  262. }
  263. else {
  264. (_a = _this.options.logger) === null || _a === void 0 ? void 0 : _a.warn('[Unresolved Recv]', e.data);
  265. }
  266. };
  267. };
  268. WebSocketProxy.prototype.close = function (code, reason) {
  269. var _a;
  270. (_a = this._ws) === null || _a === void 0 ? void 0 : _a.close(code, reason);
  271. this._ws = undefined;
  272. };
  273. WebSocketProxy.prototype.send = function (data) {
  274. return tslib.__awaiter(this, void 0, void 0, function () {
  275. var sendData, buf;
  276. return tslib.__generator(this, function (_a) {
  277. try {
  278. sendData = void 0;
  279. if (typeof data === 'string') {
  280. sendData = data;
  281. }
  282. else {
  283. buf = data;
  284. if (buf.byteOffset === 0 && buf.byteLength === buf.buffer.byteLength) {
  285. sendData = buf.buffer;
  286. }
  287. else {
  288. sendData = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
  289. }
  290. }
  291. this._ws.send(sendData);
  292. return [2 /*return*/, {}];
  293. }
  294. catch (err) {
  295. return [2 /*return*/, {
  296. err: new tsrpcProto.TsrpcError('Network Error', {
  297. code: 'SEND_BUF_ERR',
  298. type: tsrpcProto.TsrpcError.Type.NetworkError,
  299. innerErr: err
  300. })
  301. }];
  302. }
  303. return [2 /*return*/];
  304. });
  305. });
  306. };
  307. return WebSocketProxy;
  308. }());
  309. /**
  310. * Client for TSRPC WebSocket Server.
  311. * @typeParam ServiceType - `ServiceType` from generated `proto.ts`
  312. */
  313. var WsClient = /** @class */ (function (_super) {
  314. tslib.__extends(WsClient, _super);
  315. function WsClient(proto, options) {
  316. var wsp = new WebSocketProxy(options === null || options === void 0 ? void 0 : options.caUrl);
  317. return _super.call(this, proto, wsp, tslib.__assign(tslib.__assign({}, defaultWsClientOptions), options)) || this;
  318. }
  319. return WsClient;
  320. }(tsrpcBaseClient.BaseWsClient));
  321. var defaultWsClientOptions = tslib.__assign(tslib.__assign({}, tsrpcBaseClient.defaultBaseWsClientOptions), { customObjectIdClass: String });
  322. exports.HttpClient = HttpClient;
  323. exports.WsClient = WsClient;
  324. Object.keys(tsrpcProto).forEach(function (k) {
  325. if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
  326. enumerable: true,
  327. get: function () { return tsrpcProto[k]; }
  328. });
  329. });