@abtnode/util 1.16.16-beta-d8a9fb09 → 1.16.16-beta-e038cde7

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,5 +1,6 @@
1
1
  const fs = require('fs');
2
2
  const { CancelToken } = require('axios');
3
+ const throttle = require('lodash/throttle');
3
4
  const streamToPromise = require('stream-to-promise');
4
5
  const tryWithTimeout = require('./try-with-timeout');
5
6
  const axios = require('./axios');
@@ -45,13 +46,20 @@ async function streamToString(stream) {
45
46
  * @param {*} url
46
47
  * @param {*} dest directory
47
48
  * @param {{
48
- * timeout: number; // unit: ms
49
- * cancelCtrl: any;
49
+ * timeout?: number; // unit: ms
50
+ * cancelCtrl?: any;
51
+ * onProgress?: any
52
+ * minProgressInterval?: number
50
53
  * }} [{ timeout = 600 * 1000, cancelCtrl, data }={}]
51
54
  * @param {{}} context
52
55
  * @return {*}
53
56
  */
54
- const downloadFile = async (url, dest, { timeout = 600 * 1000, cancelCtrl, onProgress } = {}, context = {}) => {
57
+ const downloadFile = async (
58
+ url,
59
+ dest,
60
+ { timeout = 600 * 1000, minProgressInterval = 400, cancelCtrl, onProgress } = {},
61
+ context = {}
62
+ ) => {
55
63
  const CONNECTION_TIMEOUT = 20 * 1000;
56
64
  const source = CancelToken.source();
57
65
 
@@ -85,12 +93,13 @@ const downloadFile = async (url, dest, { timeout = 600 * 1000, cancelCtrl, onPro
85
93
  });
86
94
 
87
95
  if (typeof onProgress === 'function') {
96
+ const throttled = throttle(onProgress, minProgressInterval, { leading: false, trailing: true });
88
97
  const total = (response.headers || {})['content-length'] || 0;
89
98
  let current = 0;
90
99
  response.data.on('data', (chunk) => {
91
100
  current += chunk.length;
92
101
  try {
93
- onProgress({ total, current });
102
+ throttled({ total, current });
94
103
  } catch {
95
104
  // do nothing
96
105
  }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Fork from https://github.com/tkellen/js-express-bearer-token
3
3
  */
4
-
4
+ const toLower = require('lodash/toLower');
5
5
  const parseCookie = require('cookie').parse;
6
6
  const decodeCookie = require('cookie-parser').signedCookie;
7
7
 
@@ -51,17 +51,6 @@ module.exports = (opts) => {
51
51
 
52
52
  // headers
53
53
  if (req.headers) {
54
- if (req.headers[headerName]) {
55
- const parts = req.headers[headerName].split(' ');
56
- if (parts.length === 2 && parts[0] === headerKey) {
57
- if (token) {
58
- error = true;
59
- }
60
- // eslint-disable-next-line prefer-destructuring
61
- token = parts[1];
62
- }
63
- }
64
-
65
54
  // cookie
66
55
  if (cookie && req.headers.cookie) {
67
56
  const plainCookie = getCookie(req.headers.cookie || '', cookie.key); // seeks the key
@@ -76,6 +65,21 @@ module.exports = (opts) => {
76
65
  }
77
66
  }
78
67
  }
68
+
69
+ // header 中携带的 token 应该是等级最高的
70
+ if (req.headers[headerName]) {
71
+ const parts = req.headers[headerName].split(' ');
72
+ if (parts.length === 2 && toLower(parts[0]) === toLower(headerKey)) {
73
+ if (token) {
74
+ // HACK: 在统一登录的切换账户场景中,会同时携带 cookie 和 header bearerToken,但这两个值是不一样的,需要以 bearerToken 为准
75
+ if (token === parts[1]) {
76
+ error = true;
77
+ }
78
+ }
79
+ // eslint-disable-next-line prefer-destructuring
80
+ token = parts[1];
81
+ }
82
+ }
79
83
  }
80
84
 
81
85
  // RFC6750 states the access_token MUST NOT be provided
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.16.16-beta-d8a9fb09",
6
+ "version": "1.16.16-beta-e038cde7",
7
7
  "description": "ArcBlock's JavaScript utility",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -18,9 +18,9 @@
18
18
  "author": "polunzh <polunzh@gmail.com> (http://github.com/polunzh)",
19
19
  "license": "Apache-2.0",
20
20
  "dependencies": {
21
- "@abtnode/constant": "1.16.16-beta-d8a9fb09",
22
- "@abtnode/logger": "1.16.16-beta-d8a9fb09",
23
- "@blocklet/constant": "1.16.16-beta-d8a9fb09",
21
+ "@abtnode/constant": "1.16.16-beta-e038cde7",
22
+ "@abtnode/logger": "1.16.16-beta-e038cde7",
23
+ "@blocklet/constant": "1.16.16-beta-e038cde7",
24
24
  "@ocap/client": "1.18.89",
25
25
  "@ocap/mcrypto": "1.18.89",
26
26
  "@ocap/util": "1.18.89",
@@ -71,5 +71,5 @@
71
71
  "fs-extra": "^10.1.0",
72
72
  "jest": "^27.5.1"
73
73
  },
74
- "gitHead": "a03d8972fd0fafe810369efa89b03ccd0647dce5"
74
+ "gitHead": "688b1c7ae0f6652094100c101f5d218266dd86ac"
75
75
  }