index.js 15 KB

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