index.mjs 13 KB

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