@abtnode/util 1.7.26 → 1.8.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.
package/lib/axios.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // set proxy to false to fix bug with HTTPS_PROXY environment
2
2
  // https://github.com/axios/axios/issues/3384
3
- const axios = require('axios');
3
+ const { default: axios } = require('axios');
4
4
 
5
5
  axios.defaults.proxy = false;
6
6
 
@@ -7,18 +7,56 @@ const axios = require('./axios');
7
7
  const CANCEL = '__cancel__';
8
8
 
9
9
  /**
10
- * download file
11
- * @param {string} url
12
- * @param {string} dest directory
13
- * @param {Number} timeout unit: ms
14
- * @param {CancelCtrl} cancelCtrl
10
+ *
11
+ *
12
+ * @param {Stream} stream
13
+ * @returns {Promise<string>}
15
14
  */
16
- const downloadFile = async (url, dest, { timeout = 600 * 1000, cancelCtrl } = {}) => {
15
+ async function getErrorMessageFromStream(stream) {
16
+ const str = await streamToString(stream);
17
+
18
+ try {
19
+ const json = JSON.parse(str);
20
+ return json.error;
21
+ } catch (error) {
22
+ return str;
23
+ }
24
+ }
25
+
26
+ /**
27
+ *
28
+ *
29
+ * @param {*} stream
30
+ * @return {Promise<string>}
31
+ */
32
+ async function streamToString(stream) {
33
+ const chunks = [];
34
+
35
+ for await (const chunk of stream) {
36
+ chunks.push(Buffer.from(chunk));
37
+ }
38
+
39
+ return Buffer.concat(chunks).toString('utf-8');
40
+ }
41
+
42
+ /**
43
+ *
44
+ *
45
+ * @param {*} url
46
+ * @param {*} dest directory
47
+ * @param {{
48
+ * timeout: number; // unit: ms
49
+ * cancelCtrl: any;
50
+ * }} [{ timeout = 600 * 1000, cancelCtrl, data }={}]
51
+ * @param {{}} context
52
+ * @return {*}
53
+ */
54
+ const downloadFile = async (url, dest, { timeout = 600 * 1000, cancelCtrl } = {}, context = {}) => {
17
55
  const CONNECTION_TIMEOUT = 20 * 1000;
18
56
  const source = CancelToken.source();
19
57
 
20
58
  try {
21
- const result = await tryWithTimeout(async () => {
59
+ return await tryWithTimeout(async () => {
22
60
  const timer = setTimeout(() => {
23
61
  source.cancel(`Connection timeout: ${Math.ceil(CONNECTION_TIMEOUT / 1000)}s`);
24
62
  }, CONNECTION_TIMEOUT);
@@ -39,6 +77,7 @@ const downloadFile = async (url, dest, { timeout = 600 * 1000, cancelCtrl } = {}
39
77
 
40
78
  const response = await axios({
41
79
  url,
80
+ headers: context?.headers,
42
81
  method: 'GET',
43
82
  responseType: 'stream',
44
83
  cancelToken: source.token,
@@ -61,8 +100,6 @@ const downloadFile = async (url, dest, { timeout = 600 * 1000, cancelCtrl } = {}
61
100
 
62
101
  return dest;
63
102
  }, timeout);
64
-
65
- return result;
66
103
  } catch (err) {
67
104
  if (fs.existsSync(dest)) {
68
105
  fs.unlinkSync(dest);
@@ -76,6 +113,11 @@ const downloadFile = async (url, dest, { timeout = 600 * 1000, cancelCtrl } = {}
76
113
  }
77
114
 
78
115
  source.cancel();
116
+
117
+ if (err?.response?.data) {
118
+ throw new Error(await getErrorMessageFromStream(err.response.data));
119
+ }
120
+
79
121
  throw err;
80
122
  }
81
123
  };
@@ -0,0 +1,81 @@
1
+ const { sign } = require('@arcblock/jwt');
2
+ const { BlockletSource } = require('@blocklet/meta/lib/constants');
3
+ const { WHO_CAN_ACCESS } = require('@abtnode/constant');
4
+ const logger = require('@abtnode/logger')('@abtnode/util:public-to-store');
5
+
6
+ const getWallet = require('./get-app-wallet');
7
+ const axios = require('./axios');
8
+
9
+ const getAppToken = (blocklet) =>
10
+ sign(blocklet.environmentObj?.BLOCKLET_APP_ID, blocklet.environmentObj?.BLOCKLET_APP_SK);
11
+
12
+ const getAppId = (blocklet) => blocklet.environmentObj?.BLOCKLET_APP_ID;
13
+
14
+ const getAppSK = (blocklet) => blocklet.environmentObj?.BLOCKLET_APP_SK;
15
+
16
+ const getBlockletDid = (blocklet) => blocklet.meta?.bundleDid;
17
+
18
+ const getAppUrl = (blocklet) => blocklet.environmentObj?.BLOCKLET_APP_URL;
19
+
20
+ /**
21
+ * verify manages the permissions of blocklet public instance
22
+ * @param {*} blocklet
23
+ * @returns bool
24
+ */
25
+ const verifyPublicInstance = (blocklet) => {
26
+ const { whoCanAccess = WHO_CAN_ACCESS.ALL } = blocklet.settings;
27
+ return blocklet.source === BlockletSource.registry && whoCanAccess === WHO_CAN_ACCESS.ALL;
28
+ };
29
+
30
+ /**
31
+ *
32
+ * @param {*} blocklet
33
+ * @param {*} {userDid publicToStore}
34
+ */
35
+ async function handleInstanceInStore(blocklet, { userDid = null, publicToStore = true } = {}) {
36
+ if (!blocklet) {
37
+ throw new Error('blocklet argument is required');
38
+ }
39
+
40
+ const ownerDid = userDid || blocklet.settings?.owner?.did;
41
+ if (!verifyPublicInstance(blocklet)) {
42
+ throw new Error('no permission to set publicInstance ');
43
+ }
44
+
45
+ if (!ownerDid) {
46
+ return false;
47
+ }
48
+
49
+ const appToken = getAppToken(blocklet);
50
+ const blockletDid = getBlockletDid(blocklet);
51
+ const appId = getAppId(blocklet);
52
+ const appSK = getAppSK(blocklet);
53
+ const wallet = getWallet(appSK);
54
+ const body = {
55
+ appToken,
56
+ appId,
57
+ appPK: wallet.publicKey,
58
+ ownerDid,
59
+ appUrl: getAppUrl(blocklet),
60
+ blockletDid,
61
+ };
62
+
63
+ const api = axios.create({ baseURL: blocklet.deployedFrom, timeout: 1000 * 10 });
64
+
65
+ if (!publicToStore) {
66
+ try {
67
+ await api.delete(`/api/blocklet-instances/${appId}`, { data: body });
68
+ } catch (error) {
69
+ logger.error('failed to delete blocklet instance', { error });
70
+ }
71
+ } else {
72
+ try {
73
+ await api.put(`/api/blocklet-instances/${appId}`, body);
74
+ } catch (error) {
75
+ logger.error('failed to update blocklet instance', { error });
76
+ }
77
+ }
78
+ return true;
79
+ }
80
+
81
+ module.exports = handleInstanceInStore;
package/lib/sleep.js CHANGED
@@ -1 +1,2 @@
1
+ // eslint-disable-next-line no-promise-executor-return
1
2
  module.exports = (timeout = 0) => new Promise((resolve) => setTimeout(resolve, timeout));
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.7.26",
6
+ "version": "1.8.1",
7
7
  "description": "ArcBlock's JavaScript utility",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -18,7 +18,10 @@
18
18
  "author": "polunzh <polunzh@gmail.com> (http://github.com/polunzh)",
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
- "@abtnode/constant": "1.7.26",
21
+ "@abtnode/constant": "1.8.1",
22
+ "@abtnode/logger": "1.8.1",
23
+ "@arcblock/jwt": "^1.17.0",
24
+ "@blocklet/meta": "1.8.1",
22
25
  "@ocap/mcrypto": "1.17.0",
23
26
  "@ocap/util": "1.17.0",
24
27
  "@ocap/wallet": "1.17.0",
@@ -30,6 +33,7 @@
30
33
  "find-up": "^5.0.0",
31
34
  "flush-write-stream": "^2.0.0",
32
35
  "folder-walker": "^3.2.0",
36
+ "form-data": "^4.0.0",
33
37
  "hasha": "^5.2.0",
34
38
  "internal-ip": "^6.1.0",
35
39
  "is-docker": "^2.1.1",
@@ -55,5 +59,5 @@
55
59
  "fs-extra": "^10.0.1",
56
60
  "jest": "^27.4.5"
57
61
  },
58
- "gitHead": "b7ef9b4ddb18f7a0c3898177fe06d9cefe966566"
62
+ "gitHead": "c970b8a386bebd7fe6dbc8b8eedf8bd8328b4bb5"
59
63
  }