index.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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-miniapp/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 Miniapp v3.4.16
  13. * -----------------------------------------
  14. * Copyright (c) King Wang.
  15. * MIT License
  16. * https://github.com/k8w/tsrpc-miniapp
  17. */
  18. 'use strict';
  19. Object.defineProperty(exports, '__esModule', { value: true });
  20. require('k8w-extend-native');
  21. var tslib = require('tslib');
  22. var tsrpcBaseClient = require('tsrpc-base-client');
  23. var tsrpcProto = require('tsrpc-proto');
  24. var HttpProxy = /** @class */ (function () {
  25. function HttpProxy() {
  26. }
  27. HttpProxy.prototype.fetch = function (options) {
  28. var _a;
  29. if (!this.miniappObj) {
  30. return {
  31. abort: function () { },
  32. promise: Promise.resolve({
  33. isSucc: false,
  34. err: new tsrpcProto.TsrpcError('miniappObj is not set, please check if this is miniapp environment.', { type: tsrpcProto.TsrpcError.Type.ClientError })
  35. })
  36. };
  37. }
  38. var rs;
  39. var promise = new Promise(function (_rs) {
  40. rs = _rs;
  41. });
  42. var data;
  43. if (typeof options.data === 'string') {
  44. data = options.data;
  45. }
  46. else {
  47. var buf = options.data;
  48. if (buf.byteOffset === 0 && buf.byteLength === buf.buffer.byteLength) {
  49. data = buf.buffer;
  50. }
  51. else {
  52. data = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
  53. }
  54. }
  55. var reqTask = this.miniappObj.request({
  56. url: options.url,
  57. data: data,
  58. method: options.method,
  59. header: (_a = options.headers) !== null && _a !== void 0 ? _a : {
  60. 'content-type': 'application/octet-stream'
  61. },
  62. dataType: '其他',
  63. responseType: options.responseType,
  64. success: function (res) {
  65. if (res.statusCode === 200 || res.statusCode === 500) {
  66. rs({
  67. isSucc: true,
  68. res: typeof res.data === 'string' ? res.data : new Uint8Array(res.data)
  69. });
  70. }
  71. else {
  72. rs({
  73. isSucc: false,
  74. err: new tsrpcProto.TsrpcError({
  75. message: 'HTTP Error ' + res.statusCode,
  76. type: tsrpcProto.TsrpcError.Type.ServerError,
  77. httpCode: res.statusCode
  78. })
  79. });
  80. }
  81. },
  82. fail: function (res) {
  83. rs({
  84. isSucc: false,
  85. err: new tsrpcProto.TsrpcError({
  86. message: 'Network Error',
  87. type: tsrpcProto.TsrpcError.Type.NetworkError,
  88. innerErr: res
  89. })
  90. });
  91. }
  92. });
  93. var abort = reqTask.abort.bind(reqTask);
  94. return {
  95. promise: promise,
  96. abort: abort
  97. };
  98. };
  99. return HttpProxy;
  100. }());
  101. /**
  102. * Client for TSRPC HTTP Server.
  103. * It uses native http module of NodeJS.
  104. * @typeParam ServiceType - `ServiceType` from generated `proto.ts`
  105. */
  106. var HttpClient = /** @class */ (function (_super) {
  107. tslib.__extends(HttpClient, _super);
  108. function HttpClient(proto, options) {
  109. var _this = this;
  110. var httpProxy = new HttpProxy;
  111. _this = _super.call(this, proto, httpProxy, tslib.__assign(tslib.__assign({}, defaultHttpClientOptions), options)) || this;
  112. httpProxy.miniappObj = _this.options.miniappObj;
  113. return _this;
  114. }
  115. return HttpClient;
  116. }(tsrpcBaseClient.BaseHttpClient));
  117. var defaultHttpClientOptions = tslib.__assign(tslib.__assign({}, tsrpcBaseClient.defaultBaseHttpClientOptions), { miniappObj: typeof wx !== 'undefined' ? wx : undefined, customObjectIdClass: String });
  118. var WebSocketProxy = /** @class */ (function () {
  119. function WebSocketProxy() {
  120. }
  121. WebSocketProxy.prototype.connect = function (server, protocols) {
  122. var _this = this;
  123. var ws = this.miniappObj.connectSocket(tslib.__assign(tslib.__assign({}, this.client.options.connectSocketOptions), { url: server, protocols: protocols }));
  124. this._ws = ws;
  125. this._ws.onOpen(function (header) {
  126. if (_this._ws !== ws) {
  127. return;
  128. }
  129. _this.options.onOpen();
  130. });
  131. this._ws.onError(function (res) {
  132. if (_this._ws !== ws) {
  133. return;
  134. }
  135. ws.close();
  136. _this._ws = undefined;
  137. _this.options.onError(res);
  138. });
  139. this._ws.onClose(function (e) {
  140. if (_this._ws !== ws) {
  141. return;
  142. }
  143. _this._ws = undefined;
  144. _this.options.onClose(e.code, e.reason);
  145. });
  146. this._ws.onMessage(function (e) {
  147. if (_this._ws !== ws) {
  148. return;
  149. }
  150. if (typeof e.data === 'string') {
  151. _this.options.onMessage(e.data);
  152. }
  153. else {
  154. _this.options.onMessage(new Uint8Array(e.data));
  155. }
  156. });
  157. };
  158. WebSocketProxy.prototype.close = function (code, reason) {
  159. var _this = this;
  160. var _a;
  161. (_a = this._ws) === null || _a === void 0 ? void 0 : _a.close({
  162. code: code,
  163. reason: reason,
  164. fail: function (res) {
  165. var _a;
  166. // 重试一次
  167. console.error('WebSocket closed failed', res);
  168. (_a = _this._ws) === null || _a === void 0 ? void 0 : _a.close();
  169. }
  170. });
  171. this._ws = undefined;
  172. };
  173. WebSocketProxy.prototype.send = function (data) {
  174. var _this = this;
  175. var sendData;
  176. if (typeof data === 'string') {
  177. sendData = data;
  178. }
  179. else {
  180. var buf = data;
  181. if (buf.byteOffset === 0 && buf.byteLength === buf.buffer.byteLength) {
  182. sendData = buf.buffer;
  183. }
  184. else {
  185. sendData = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
  186. }
  187. }
  188. return new Promise(function (rs) {
  189. _this._ws.send({
  190. data: sendData,
  191. success: function () { rs({}); },
  192. fail: function (res) {
  193. rs({
  194. err: new tsrpcProto.TsrpcError({
  195. message: 'Network Error',
  196. type: tsrpcProto.TsrpcError.Type.NetworkError,
  197. innerErr: res
  198. })
  199. });
  200. }
  201. });
  202. });
  203. };
  204. return WebSocketProxy;
  205. }());
  206. /**
  207. * Client for TSRPC WebSocket Server.
  208. * @typeParam ServiceType - `ServiceType` from generated `proto.ts`
  209. */
  210. var WsClient = /** @class */ (function (_super) {
  211. tslib.__extends(WsClient, _super);
  212. function WsClient(proto, options) {
  213. var _this = this;
  214. var wsp = new WebSocketProxy();
  215. _this = _super.call(this, proto, wsp, tslib.__assign(tslib.__assign({}, defaultWsClientOptions), options)) || this;
  216. if (!_this.options.miniappObj) {
  217. throw new Error('options.miniappObj is not set');
  218. }
  219. wsp.miniappObj = _this.options.miniappObj;
  220. wsp.client = _this;
  221. return _this;
  222. }
  223. return WsClient;
  224. }(tsrpcBaseClient.BaseWsClient));
  225. var defaultWsClientOptions = tslib.__assign(tslib.__assign({}, tsrpcBaseClient.defaultBaseWsClientOptions), { miniappObj: typeof wx !== 'undefined' ? wx : undefined, customObjectIdClass: String });
  226. exports.HttpClient = HttpClient;
  227. exports.WsClient = WsClient;
  228. Object.keys(tsrpcProto).forEach(function (k) {
  229. if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
  230. enumerable: true,
  231. get: function () { return tsrpcProto[k]; }
  232. });
  233. });
  234. }
  235. if (nodeEnv) {
  236. __define(__module.exports, __require, __module);
  237. }
  238. else {
  239. __quick_compile_project__.registerModuleFunc(__filename, function () {
  240. __define(__module.exports, __require, __module);
  241. });
  242. }
  243. })();