@abtnode/util 1.7.27 → 1.8.2

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
@@ -2,6 +2,4 @@
2
2
  // https://github.com/axios/axios/issues/3384
3
3
  const { default: axios } = require('axios');
4
4
 
5
- axios.defaults.proxy = false;
6
-
7
5
  module.exports = axios;
@@ -6,6 +6,39 @@ const axios = require('./axios');
6
6
 
7
7
  const CANCEL = '__cancel__';
8
8
 
9
+ /**
10
+ *
11
+ *
12
+ * @param {Stream} stream
13
+ * @returns {Promise<string>}
14
+ */
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
+
9
42
  /**
10
43
  *
11
44
  *
@@ -80,6 +113,11 @@ const downloadFile = async (url, dest, { timeout = 600 * 1000, cancelCtrl } = {}
80
113
  }
81
114
 
82
115
  source.cancel();
116
+
117
+ if (err?.response?.data) {
118
+ throw new Error(await getErrorMessageFromStream(err.response.data));
119
+ }
120
+
83
121
  throw err;
84
122
  }
85
123
  };
@@ -0,0 +1,85 @@
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
+ * @returns bool
35
+ */
36
+ async function handleInstanceInStore(blocklet, { userDid = null, publicToStore = false } = {}) {
37
+ if (!blocklet) {
38
+ logger.error('blocklet argument is required');
39
+ throw new Error('blocklet argument is required');
40
+ }
41
+
42
+ const ownerDid = userDid || blocklet.settings?.owner?.did;
43
+ if (!ownerDid) {
44
+ return false;
45
+ }
46
+
47
+ if (!verifyPublicInstance(blocklet)) {
48
+ logger.error('no permission to set publicInstance');
49
+ throw new Error('no permission to set publicInstance');
50
+ }
51
+
52
+ const appToken = getAppToken(blocklet);
53
+ const blockletDid = getBlockletDid(blocklet);
54
+ const appId = getAppId(blocklet);
55
+ const appSK = getAppSK(blocklet);
56
+ const wallet = getWallet(appSK);
57
+ const body = {
58
+ appToken,
59
+ appId,
60
+ appPK: wallet.publicKey,
61
+ ownerDid,
62
+ appUrl: getAppUrl(blocklet),
63
+ blockletDid,
64
+ };
65
+
66
+ const api = axios.create({ baseURL: blocklet.deployedFrom, timeout: 1000 * 10 });
67
+ if (!publicToStore) {
68
+ try {
69
+ await api.delete(`/api/blocklet-instances/${appId}`, { data: body });
70
+ } catch (error) {
71
+ logger.error('failed to delete blocklet instance', { error });
72
+ throw new Error('failed to delete blocklet instance');
73
+ }
74
+ } else {
75
+ try {
76
+ await api.put(`/api/blocklet-instances/${appId}`, body);
77
+ } catch (error) {
78
+ logger.error('failed to update blocklet instance', { error });
79
+ throw new Error('failed to delete blocklet instance');
80
+ }
81
+ }
82
+ return true;
83
+ }
84
+
85
+ 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.27",
6
+ "version": "1.8.2",
7
7
  "description": "ArcBlock's JavaScript utility",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -18,10 +18,13 @@
18
18
  "author": "polunzh <polunzh@gmail.com> (http://github.com/polunzh)",
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
- "@abtnode/constant": "1.7.27",
22
- "@ocap/mcrypto": "1.17.0",
23
- "@ocap/util": "1.17.0",
24
- "@ocap/wallet": "1.17.0",
21
+ "@abtnode/constant": "1.8.2",
22
+ "@abtnode/logger": "1.8.2",
23
+ "@arcblock/jwt": "^1.17.2",
24
+ "@blocklet/meta": "1.8.2",
25
+ "@ocap/mcrypto": "1.17.2",
26
+ "@ocap/util": "1.17.2",
27
+ "@ocap/wallet": "1.17.2",
25
28
  "axios": "^0.27.2",
26
29
  "axios-mock-adapter": "^1.20.0",
27
30
  "axon": "^2.0.3",
@@ -56,5 +59,5 @@
56
59
  "fs-extra": "^10.0.1",
57
60
  "jest": "^27.4.5"
58
61
  },
59
- "gitHead": "81a5492df66389b0aede13f033d1e493450833bc"
62
+ "gitHead": "fcbe3c97f3825c507ee16714f49bbf8f58c5b59f"
60
63
  }