@onekeyfe/hd-transport-http 0.1.44 → 0.1.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAGA,oBAAY,kBAAkB,GAAG;IAC/B,IAAI,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC;IACrD,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,GAAG,KAAK,CAAC;IACvB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAwBF,wBAAsB,OAAO,CAAC,OAAO,EAAE,kBAAkB,gBAuBxD"}
1
+ {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAGA,oBAAY,kBAAkB,GAAG;IAC/B,IAAI,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC;IACrD,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,GAAG,KAAK,CAAC;IACvB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAwBF,wBAAsB,OAAO,CAAC,OAAO,EAAE,kBAAkB,gBA0BxD"}
package/dist/index.js CHANGED
@@ -1,6 +1,13 @@
1
- import transport from '@onekeyfe/hd-transport';
2
- import { HardwareError, HardwareErrorCode, ERRORS } from '@onekeyfe/hd-shared';
3
- import axios from 'axios';
1
+ 'use strict';
2
+
3
+ var transport = require('@onekeyfe/hd-transport');
4
+ var hdShared = require('@onekeyfe/hd-shared');
5
+ var axios = require('axios');
6
+
7
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
+
9
+ var transport__default = /*#__PURE__*/_interopDefaultLegacy(transport);
10
+ var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
4
11
 
5
12
  /******************************************************************************
6
13
  Copyright (c) Microsoft Corporation.
@@ -60,23 +67,39 @@ function request(options) {
60
67
  },
61
68
  timeout: (_a = options.timeout) !== null && _a !== void 0 ? _a : undefined,
62
69
  };
63
- const res = yield axios.request(fetchOptions);
70
+ const res = yield axios__default["default"].request(fetchOptions);
64
71
  if (+res.status === 200) {
65
72
  return parseResult(res.data);
66
73
  }
67
74
  const resJson = parseResult(res.data);
68
75
  if (typeof resJson === 'object' && resJson != null && resJson.error != null) {
69
- throw new HardwareError({ errorCode: HardwareErrorCode.NetworkError, message: resJson.error });
76
+ throw new hdShared.HardwareError({
77
+ errorCode: hdShared.HardwareErrorCode.NetworkError,
78
+ message: resJson.error,
79
+ });
70
80
  }
71
81
  else {
72
- throw new HardwareError({ errorCode: HardwareErrorCode.NetworkError, message: res.data });
82
+ throw new hdShared.HardwareError({ errorCode: hdShared.HardwareErrorCode.NetworkError, message: res.data });
73
83
  }
74
84
  });
75
85
  }
86
+ axios__default["default"].interceptors.request.use(config => {
87
+ var _a, _b;
88
+ if (typeof window !== 'undefined') {
89
+ return config;
90
+ }
91
+ if ((_a = config.url) === null || _a === void 0 ? void 0 : _a.startsWith('http://localhost:21320')) {
92
+ if (!((_b = config === null || config === void 0 ? void 0 : config.headers) === null || _b === void 0 ? void 0 : _b.Origin)) {
93
+ console.log('set node request origin');
94
+ config.headers = Object.assign(Object.assign({}, config.headers), { Origin: 'https://jssdk.onekey.so' });
95
+ }
96
+ }
97
+ return config;
98
+ });
76
99
 
77
100
  const DEFAULT_URL = 'http://localhost:21320';
78
101
 
79
- const { check, buildOne, receiveOne, parseConfigure } = transport;
102
+ const { check, buildOne, receiveOne, parseConfigure } = transport__default["default"];
80
103
  class HttpTransport {
81
104
  constructor(url) {
82
105
  this.configured = false;
@@ -85,7 +108,7 @@ class HttpTransport {
85
108
  }
86
109
  _post(options) {
87
110
  if (this.stopped) {
88
- return Promise.reject(ERRORS.TypedError('Transport stopped.'));
111
+ return Promise.reject(hdShared.ERRORS.TypedError('Transport stopped.'));
89
112
  }
90
113
  return request(Object.assign(Object.assign({}, options), { method: 'POST', url: this.url + options.url }));
91
114
  }
@@ -115,7 +138,7 @@ class HttpTransport {
115
138
  listen(old) {
116
139
  return __awaiter(this, void 0, void 0, function* () {
117
140
  if (old === null) {
118
- throw ERRORS.TypedError('Http-Transport does not support listen without previous.');
141
+ throw hdShared.ERRORS.TypedError('Http-Transport does not support listen without previous.');
119
142
  }
120
143
  const devicesS = yield this._post({
121
144
  url: '/listen',
@@ -157,7 +180,7 @@ class HttpTransport {
157
180
  call(session, name, data) {
158
181
  return __awaiter(this, void 0, void 0, function* () {
159
182
  if (this._messages == null) {
160
- throw ERRORS.TypedError(HardwareErrorCode.TransportNotConfigured);
183
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportNotConfigured);
161
184
  }
162
185
  const messages = this._messages;
163
186
  this.Log.debug('call-', ' name: ', name, ' data: ', data);
@@ -169,7 +192,7 @@ class HttpTransport {
169
192
  timeout: name === 'Initialize' ? 10000 : undefined,
170
193
  });
171
194
  if (typeof resData !== 'string') {
172
- throw ERRORS.TypedError(HardwareErrorCode.NetworkError, 'Returning data is not string.');
195
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.NetworkError, 'Returning data is not string.');
173
196
  }
174
197
  const jsonData = receiveOne(messages, resData);
175
198
  return check.call(jsonData);
@@ -178,7 +201,7 @@ class HttpTransport {
178
201
  post(session, name, data) {
179
202
  return __awaiter(this, void 0, void 0, function* () {
180
203
  if (this._messages == null) {
181
- throw ERRORS.TypedError(HardwareErrorCode.TransportNotConfigured);
204
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportNotConfigured);
182
205
  }
183
206
  const messages = this._messages;
184
207
  const outData = buildOne(messages, name, data).toString('hex');
@@ -191,14 +214,14 @@ class HttpTransport {
191
214
  read(session) {
192
215
  return __awaiter(this, void 0, void 0, function* () {
193
216
  if (this._messages == null) {
194
- throw ERRORS.TypedError(HardwareErrorCode.TransportNotConfigured);
217
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportNotConfigured);
195
218
  }
196
219
  const messages = this._messages;
197
220
  const resData = yield this._post({
198
221
  url: `/read/${session}`,
199
222
  });
200
223
  if (typeof resData !== 'string') {
201
- throw ERRORS.TypedError(HardwareErrorCode.NetworkError, 'Returning data is not string.');
224
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.NetworkError, 'Returning data is not string.');
202
225
  }
203
226
  const jsonData = receiveOne(messages, resData);
204
227
  return check.call(jsonData);
@@ -215,4 +238,4 @@ class HttpTransport {
215
238
  }
216
239
  }
217
240
 
218
- export { HttpTransport as default };
241
+ module.exports = HttpTransport;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-transport-http",
3
- "version": "0.1.44",
3
+ "version": "0.1.45",
4
4
  "description": "hardware http transport",
5
5
  "author": "OneKey",
6
6
  "homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
@@ -25,9 +25,9 @@
25
25
  "url": "https://github.com/OneKeyHQ/hardware-js-sdk/issues"
26
26
  },
27
27
  "dependencies": {
28
- "@onekeyfe/hd-shared": "^0.1.44",
29
- "@onekeyfe/hd-transport": "^0.1.44",
28
+ "@onekeyfe/hd-shared": "^0.1.45",
29
+ "@onekeyfe/hd-transport": "^0.1.45",
30
30
  "axios": "^0.27.2"
31
31
  },
32
- "gitHead": "fb673918587877267cbbeca6e0c6abbc56a0d0fa"
32
+ "gitHead": "0436f8ff26494933124c1c6f4586d4aeb5526c30"
33
33
  }
package/src/http.ts CHANGED
@@ -49,8 +49,29 @@ export async function request(options: HttpRequestOptions) {
49
49
  }
50
50
  const resJson = parseResult(res.data);
51
51
  if (typeof resJson === 'object' && resJson != null && resJson.error != null) {
52
- throw new HardwareError({ errorCode: HardwareErrorCode.NetworkError, message: resJson.error });
52
+ throw new HardwareError({
53
+ errorCode: HardwareErrorCode.NetworkError,
54
+ message: resJson.error,
55
+ });
53
56
  } else {
54
57
  throw new HardwareError({ errorCode: HardwareErrorCode.NetworkError, message: res.data });
55
58
  }
56
59
  }
60
+
61
+ axios.interceptors.request.use(config => {
62
+ if (typeof window !== 'undefined') {
63
+ return config;
64
+ }
65
+ // node environment
66
+ if (config.url?.startsWith('http://localhost:21320')) {
67
+ if (!config?.headers?.Origin) {
68
+ console.log('set node request origin');
69
+ // add Origin field for request headers
70
+ config.headers = {
71
+ ...config.headers,
72
+ Origin: 'https://jssdk.onekey.so',
73
+ };
74
+ }
75
+ }
76
+ return config;
77
+ });