febs 1.3.0 → 1.3.1

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.
Files changed (2) hide show
  1. package/package.json +1 -3
  2. package/server/net.js +22 -10
package/package.json CHANGED
@@ -4,8 +4,6 @@
4
4
  "assert": "^1.3.0",
5
5
  "bignumber.js": "^4.0.1",
6
6
  "co-busboy": "^1.4.0",
7
- "node-fetch": "^2.6.0",
8
- "promise": "^7.1.1",
9
7
  "raw-body": "^2.2.0",
10
8
  "url": "^0.10.3"
11
9
  },
@@ -59,5 +57,5 @@
59
57
  }
60
58
  ],
61
59
  "name": "febs",
62
- "version": "1.3.0"
60
+ "version": "1.3.1"
63
61
  }
package/server/net.js CHANGED
@@ -6,20 +6,32 @@
6
6
 
7
7
  'use strict';
8
8
 
9
- var nodeFetch = require('node-fetch');
10
9
  var exception = require('../browser/common/exception');
11
10
 
12
- exports.fetch = function (url, init) {
13
- return nodeFetch(url, init)
11
+ exports.fetch = function(url, init) {
12
+ var options = Object.assign({}, init);
13
+
14
+ // 原生 fetch 不支持 timeout 选项, 需要转换为 AbortSignal.
15
+ // 调用方已传入 signal 时以其为准.
16
+ if (options.timeout > 0 && !options.signal) {
17
+ options.signal = AbortSignal.timeout(options.timeout);
18
+ }
19
+ delete options.timeout;
20
+
21
+ return fetch(url, options)
14
22
  .catch(e => {
15
- if (e instanceof nodeFetch.FetchError) {
16
- if (e.type === 'request-timeout') {
17
- throw new exception(e.message, 'NetworkTimeout', __filename, __line, __column);
18
- }
19
- else {
20
- throw new exception(e.message, 'NetworkFailed', __filename, __line, __column);
21
- }
23
+ // 超时:
24
+ // 原生 fetch(AbortSignal.timeout) -> DOMException, name 为 'TimeoutError';
25
+ // node-fetch v2 -> FetchError, type 为 'request-timeout'.
26
+ if (e && (e.name === 'TimeoutError' || e.type === 'request-timeout')) {
27
+ throw new exception(e.message, 'NetworkTimeout', __filename, __line, __column);
22
28
  }
29
+
30
+ // 网络错误: 原生 fetch 统一抛出 TypeError('fetch failed').
31
+ if (e instanceof TypeError) {
32
+ throw new exception(e.message, 'NetworkFailed', __filename, __line, __column);
33
+ }
34
+
23
35
  throw e;
24
36
  });
25
37
  }