@blocklet/sdk 1.16.15-beta-9318a201 → 1.16.15-beta-ed0db59e
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/config.d.ts +16 -15
- package/lib/config.js +86 -6
- package/lib/security/index.d.ts +4 -4
- package/lib/security/index.js +10 -6
- package/lib/service/notification.js +2 -1
- package/lib/util/login.js +15 -8
- package/package.json +7 -7
package/lib/config.d.ts
CHANGED
|
@@ -25,7 +25,12 @@ declare const logger: {
|
|
|
25
25
|
(message?: any, ...optionalParams: any[]): void;
|
|
26
26
|
};
|
|
27
27
|
};
|
|
28
|
-
declare const env:
|
|
28
|
+
declare const env: {
|
|
29
|
+
componentDid: string;
|
|
30
|
+
dataDir: string;
|
|
31
|
+
cacheDir: string;
|
|
32
|
+
mode: string;
|
|
33
|
+
preferences: any;
|
|
29
34
|
appId: string;
|
|
30
35
|
appPid: string;
|
|
31
36
|
appIds: string[];
|
|
@@ -33,18 +38,13 @@ declare const env: Readonly<{
|
|
|
33
38
|
appDescription: string;
|
|
34
39
|
appUrl: string;
|
|
35
40
|
isComponent: boolean;
|
|
36
|
-
dataDir: string;
|
|
37
|
-
cacheDir: string;
|
|
38
|
-
mode: string;
|
|
39
41
|
appStorageEndpoint: string;
|
|
40
42
|
serverVersion: string;
|
|
41
43
|
languages: {
|
|
42
44
|
code: string;
|
|
43
45
|
name: string;
|
|
44
46
|
}[];
|
|
45
|
-
|
|
46
|
-
componentDid: string;
|
|
47
|
-
}>;
|
|
47
|
+
};
|
|
48
48
|
type MountPoint = TComponentInternalInfo & {
|
|
49
49
|
webEndpoint?: string;
|
|
50
50
|
};
|
|
@@ -70,9 +70,10 @@ declare const _handleComponentInstalled: (data: {
|
|
|
70
70
|
components: TComponents;
|
|
71
71
|
}) => void;
|
|
72
72
|
declare const _handleConfigUpdate: (data: any) => void;
|
|
73
|
+
declare const _handleComponentConfigUpdate: (data: any) => void;
|
|
73
74
|
declare const getBlockletJs: (pageGroup?: string, pathPrefix?: string, source?: string) => string;
|
|
74
75
|
export { logger, env, componentStore as components, MountPoint, // @deprecated, for backward compatibility
|
|
75
|
-
TComponent, events, Events, getBlockletJs, _handleComponentUpdateOld, _handleConfigUpdate, _handleComponentInstalled, _handleComponentUpdated, _handleComponentStarted, _handleComponentStopped, _handleComponentRemoved, };
|
|
76
|
+
TComponent, events, Events, getBlockletJs, _handleComponentUpdateOld, _handleConfigUpdate, _handleComponentInstalled, _handleComponentUpdated, _handleComponentStarted, _handleComponentStopped, _handleComponentRemoved, _handleComponentConfigUpdate, };
|
|
76
77
|
declare const _default: {
|
|
77
78
|
logger: {
|
|
78
79
|
info: {
|
|
@@ -89,7 +90,12 @@ declare const _default: {
|
|
|
89
90
|
(message?: any, ...optionalParams: any[]): void;
|
|
90
91
|
};
|
|
91
92
|
};
|
|
92
|
-
env:
|
|
93
|
+
env: {
|
|
94
|
+
componentDid: string;
|
|
95
|
+
dataDir: string;
|
|
96
|
+
cacheDir: string;
|
|
97
|
+
mode: string;
|
|
98
|
+
preferences: any;
|
|
93
99
|
appId: string;
|
|
94
100
|
appPid: string;
|
|
95
101
|
appIds: string[];
|
|
@@ -97,18 +103,13 @@ declare const _default: {
|
|
|
97
103
|
appDescription: string;
|
|
98
104
|
appUrl: string;
|
|
99
105
|
isComponent: boolean;
|
|
100
|
-
dataDir: string;
|
|
101
|
-
cacheDir: string;
|
|
102
|
-
mode: string;
|
|
103
106
|
appStorageEndpoint: string;
|
|
104
107
|
serverVersion: string;
|
|
105
108
|
languages: {
|
|
106
109
|
code: string;
|
|
107
110
|
name: string;
|
|
108
111
|
}[];
|
|
109
|
-
|
|
110
|
-
componentDid: string;
|
|
111
|
-
}>;
|
|
112
|
+
};
|
|
112
113
|
components: TComponents;
|
|
113
114
|
events: EventEmitter;
|
|
114
115
|
Events: {
|
package/lib/config.js
CHANGED
|
@@ -3,8 +3,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports._handleComponentRemoved = exports._handleComponentStopped = exports._handleComponentStarted = exports._handleComponentUpdated = exports._handleComponentInstalled = exports._handleConfigUpdate = exports._handleComponentUpdateOld = exports.getBlockletJs = exports.Events = exports.events = exports.components = exports.env = exports.logger = void 0;
|
|
6
|
+
exports._handleComponentConfigUpdate = exports._handleComponentRemoved = exports._handleComponentStopped = exports._handleComponentStarted = exports._handleComponentUpdated = exports._handleComponentInstalled = exports._handleConfigUpdate = exports._handleComponentUpdateOld = exports.getBlockletJs = exports.Events = exports.events = exports.components = exports.env = exports.logger = void 0;
|
|
7
7
|
/* eslint-disable prettier/prettier */
|
|
8
|
+
/* eslint-disable no-console */
|
|
9
|
+
const path_1 = __importDefault(require("path"));
|
|
10
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
11
|
const url_join_1 = __importDefault(require("url-join"));
|
|
9
12
|
const events_1 = __importDefault(require("events"));
|
|
10
13
|
const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
|
|
@@ -14,6 +17,7 @@ const constant_1 = require("@blocklet/constant");
|
|
|
14
17
|
const util_1 = require("@blocklet/env/lib/util");
|
|
15
18
|
const util_2 = require("@blocklet/meta/lib/util");
|
|
16
19
|
const axios_1 = __importDefault(require("axios"));
|
|
20
|
+
const security_1 = require("./security");
|
|
17
21
|
const version_1 = require("./version");
|
|
18
22
|
const notification_1 = __importDefault(require("./service/notification"));
|
|
19
23
|
const server_version_1 = __importDefault(require("./util/server-version"));
|
|
@@ -38,11 +42,9 @@ const AppConfigKeyMap = {
|
|
|
38
42
|
ABT_NODE: 'serverVersion', // for backup compatibility
|
|
39
43
|
};
|
|
40
44
|
const logger = {
|
|
41
|
-
// eslint-disable-next-line no-console
|
|
42
45
|
info: console.info,
|
|
43
46
|
debug: (...args) => {
|
|
44
47
|
if (process.env.NODE_ENV === 'development') {
|
|
45
|
-
// eslint-disable-next-line no-console
|
|
46
48
|
console.debug(...args);
|
|
47
49
|
}
|
|
48
50
|
},
|
|
@@ -50,7 +52,55 @@ const logger = {
|
|
|
50
52
|
error: console.error,
|
|
51
53
|
};
|
|
52
54
|
exports.logger = logger;
|
|
53
|
-
const
|
|
55
|
+
const appDataDir = process.env.BLOCKLET_APP_DATA_DIR;
|
|
56
|
+
let appEnvFromDisk = {};
|
|
57
|
+
let envFromDisk = {};
|
|
58
|
+
let componentsFromDisk;
|
|
59
|
+
if (appDataDir) {
|
|
60
|
+
try {
|
|
61
|
+
const configFile = path_1.default.join(appDataDir, constant_1.APP_CONFIG_FILE_PATH);
|
|
62
|
+
if (fs_1.default.existsSync(configFile)) {
|
|
63
|
+
const config = JSON.parse(fs_1.default.readFileSync(configFile).toString());
|
|
64
|
+
appEnvFromDisk = config.env || {};
|
|
65
|
+
componentsFromDisk = config.components;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
console.error(error);
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
const envFile = path_1.default.join(appDataDir, constant_1.APP_CONFIG_DIR, process.env.BLOCKLET_COMPONENT_DID, constant_1.COMPONENT_ENV_FILE_NAME);
|
|
73
|
+
if (fs_1.default.existsSync(envFile)) {
|
|
74
|
+
const raw = fs_1.default.readFileSync(envFile).toString();
|
|
75
|
+
const decrypted = (0, security_1.decrypt)(raw, process.env.BLOCKLET_COMPONENT_API_KEY, process.env.BLOCKLET_COMPONENT_DID);
|
|
76
|
+
const configObj = JSON.parse(decrypted);
|
|
77
|
+
envFromDisk = Object.keys(configObj)
|
|
78
|
+
.filter((key) => !(0, util_2.isPreferenceKey)({ key }))
|
|
79
|
+
.reduce((o, key) => {
|
|
80
|
+
o[key] = configObj[key];
|
|
81
|
+
return o;
|
|
82
|
+
}, {});
|
|
83
|
+
envFromDisk.preferences = (0, util_1.getBlockletPreferences)(configObj);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
console.error(error);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const env = {
|
|
91
|
+
...env_1.default,
|
|
92
|
+
...appEnvFromDisk,
|
|
93
|
+
...envFromDisk,
|
|
94
|
+
componentDid: process.env.BLOCKLET_COMPONENT_DID,
|
|
95
|
+
dataDir: process.env.BLOCKLET_DATA_DIR,
|
|
96
|
+
cacheDir: process.env.BLOCKLET_CACHE_DIR,
|
|
97
|
+
mode: process.env.BLOCKLET_MODE,
|
|
98
|
+
preferences: {
|
|
99
|
+
...env_1.default.preferences,
|
|
100
|
+
...appEnvFromDisk.preferences,
|
|
101
|
+
...envFromDisk.preferences,
|
|
102
|
+
}
|
|
103
|
+
};
|
|
54
104
|
exports.env = env;
|
|
55
105
|
const _fillWebEndpoint = (components) => {
|
|
56
106
|
components.forEach((x) => {
|
|
@@ -60,7 +110,9 @@ const _fillWebEndpoint = (components) => {
|
|
|
60
110
|
});
|
|
61
111
|
};
|
|
62
112
|
const initComponentStore = () => {
|
|
63
|
-
const components =
|
|
113
|
+
const components = componentsFromDisk
|
|
114
|
+
? (0, cloneDeep_1.default)(componentsFromDisk)
|
|
115
|
+
: JSON.parse(process.env.BLOCKLET_MOUNT_POINTS || '[]') || [];
|
|
64
116
|
_fillWebEndpoint(components);
|
|
65
117
|
return components;
|
|
66
118
|
};
|
|
@@ -150,6 +202,33 @@ const _handleConfigUpdate = (data) => {
|
|
|
150
202
|
events.emit(Events.envUpdate, updates);
|
|
151
203
|
};
|
|
152
204
|
exports._handleConfigUpdate = _handleConfigUpdate;
|
|
205
|
+
const _handleComponentConfigUpdate = (data) => {
|
|
206
|
+
try {
|
|
207
|
+
const decrypted = (0, security_1.decrypt)(data.configs || '[]', process.env.BLOCKLET_COMPONENT_API_KEY, process.env.BLOCKLET_COMPONENT_DID);
|
|
208
|
+
const configs = JSON.parse(decrypted);
|
|
209
|
+
const updates = configs
|
|
210
|
+
.filter((x) => !(0, util_2.isPreferenceKey)(x))
|
|
211
|
+
.reduce((o, { key, value }) => {
|
|
212
|
+
o[key] = value;
|
|
213
|
+
return o;
|
|
214
|
+
}, {});
|
|
215
|
+
Object.assign(env, updates);
|
|
216
|
+
const preferenceUpdates = (0, util_1.getBlockletPreferences)(configs.reduce((acc, x) => {
|
|
217
|
+
acc[x.key] = x.value;
|
|
218
|
+
return acc;
|
|
219
|
+
}, {}));
|
|
220
|
+
env.preferences = env.preferences || {};
|
|
221
|
+
Object.assign(env.preferences, preferenceUpdates);
|
|
222
|
+
events.emit(Events.envUpdate, {
|
|
223
|
+
...updates,
|
|
224
|
+
preferences: preferenceUpdates,
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
catch (error) {
|
|
228
|
+
logger.error('Failed to update component config', error.message);
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
exports._handleComponentConfigUpdate = _handleComponentConfigUpdate;
|
|
153
232
|
let blockletJs = '';
|
|
154
233
|
const fetchBlockletJs = async () => {
|
|
155
234
|
try {
|
|
@@ -162,7 +241,6 @@ const fetchBlockletJs = async () => {
|
|
|
162
241
|
},
|
|
163
242
|
});
|
|
164
243
|
blockletJs = res.data;
|
|
165
|
-
// eslint-disable-next-line no-console
|
|
166
244
|
logger.info('Fetch blocklet.js succeed', { componentDid });
|
|
167
245
|
return res.data;
|
|
168
246
|
}
|
|
@@ -202,9 +280,11 @@ const runInServer = (fn, type) =>
|
|
|
202
280
|
const inRuntimeEnv = !!process.env.BLOCKLET_APP_SK;
|
|
203
281
|
if (inRuntimeEnv && !process.env.BLOCKLET_HOOK_NAME) {
|
|
204
282
|
notification_1.default.on(constant_1.BlockletInternalEvents.appConfigChanged, _handleConfigUpdate);
|
|
283
|
+
notification_1.default.on(constant_1.BlockletInternalEvents.componentConfigChanged, _handleComponentConfigUpdate);
|
|
205
284
|
// Reactive fetch
|
|
206
285
|
notification_1.default.on(constant_1.BlockletInternalEvents.appConfigChanged, refreshBlockletJs);
|
|
207
286
|
notification_1.default.on(constant_1.BlockletInternalEvents.appSettingChanged, refreshBlockletJs);
|
|
287
|
+
notification_1.default.on(constant_1.BlockletInternalEvents.componentConfigChanged, refreshBlockletJs);
|
|
208
288
|
notification_1.default.on(constant_1.BlockletInternalEvents.componentInstalled, runInServer(_handleComponentInstalled, 'new'));
|
|
209
289
|
notification_1.default.on(constant_1.BlockletInternalEvents.componentUpgraded, runInServer(_handleComponentUpdated, 'new'));
|
|
210
290
|
notification_1.default.on(constant_1.BlockletInternalEvents.componentUpdated, runInServer(_handleComponentUpdated, 'new'));
|
package/lib/security/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
declare const encrypt: (message: string) => string;
|
|
2
|
-
declare const decrypt: (message: string) => string;
|
|
1
|
+
declare const encrypt: (message: string, password?: string, salt?: string) => string;
|
|
2
|
+
declare const decrypt: (message: string, password?: string, salt?: string) => string;
|
|
3
3
|
export { encrypt };
|
|
4
4
|
export { decrypt };
|
|
5
5
|
declare const _default: {
|
|
6
|
-
encrypt: (message: string) => string;
|
|
7
|
-
decrypt: (message: string) => string;
|
|
6
|
+
encrypt: (message: string, password?: string, salt?: string) => string;
|
|
7
|
+
decrypt: (message: string, password?: string, salt?: string) => string;
|
|
8
8
|
};
|
|
9
9
|
export default _default;
|
package/lib/security/index.js
CHANGED
|
@@ -7,18 +7,22 @@ exports.decrypt = exports.encrypt = void 0;
|
|
|
7
7
|
const crypto_1 = __importDefault(require("crypto"));
|
|
8
8
|
const aes_legacy_1 = __importDefault(require("@ocap/mcrypto/lib/crypter/aes-legacy"));
|
|
9
9
|
const AES = { default: aes_legacy_1.default }.default;
|
|
10
|
-
const encrypt = (message) => {
|
|
11
|
-
|
|
10
|
+
const encrypt = (message, password, salt) => {
|
|
11
|
+
const _password = password || process.env.BLOCKLET_APP_EK;
|
|
12
|
+
const _salt = salt || process.env.BLOCKLET_DID;
|
|
13
|
+
if (!_password || !_salt) {
|
|
12
14
|
return message;
|
|
13
15
|
}
|
|
14
|
-
return
|
|
16
|
+
return AES.encrypt(message, crypto_1.default.pbkdf2Sync(_password, _salt, 256, 32, 'sha512').toString('hex'));
|
|
15
17
|
};
|
|
16
18
|
exports.encrypt = encrypt;
|
|
17
|
-
const decrypt = (message) => {
|
|
18
|
-
|
|
19
|
+
const decrypt = (message, password, salt) => {
|
|
20
|
+
const _password = password || process.env.BLOCKLET_APP_EK;
|
|
21
|
+
const _salt = salt || process.env.BLOCKLET_DID;
|
|
22
|
+
if (!_password || !_salt) {
|
|
19
23
|
return message;
|
|
20
24
|
}
|
|
21
|
-
return
|
|
25
|
+
return AES.decrypt(message, crypto_1.default.pbkdf2Sync(_password, _salt, 256, 32, 'sha512').toString('hex'));
|
|
22
26
|
};
|
|
23
27
|
exports.decrypt = decrypt;
|
|
24
28
|
exports.default = {
|
|
@@ -165,7 +165,8 @@ const initClient = () => {
|
|
|
165
165
|
return;
|
|
166
166
|
}
|
|
167
167
|
// verify sender is server
|
|
168
|
-
|
|
168
|
+
const tolerance = 600;
|
|
169
|
+
if (!Jwt.verify(sender.token, process.env.ABT_NODE_PK, { tolerance })) {
|
|
169
170
|
const message = `verify sender failed in internal events. event: ${event}, sender: ${JSON.stringify(sender)}`;
|
|
170
171
|
emitError({ message });
|
|
171
172
|
console.error(message);
|
package/lib/util/login.js
CHANGED
|
@@ -5,15 +5,22 @@ exports.getLoginProvider = void 0;
|
|
|
5
5
|
const constant_1 = require("@blocklet/constant");
|
|
6
6
|
function getLoginProvider(request) {
|
|
7
7
|
let provider;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if (
|
|
11
|
-
|
|
12
|
-
provider = request.headers['x-user-provider'];
|
|
8
|
+
const extraParams = request?.context?.store?.extraParams || request.query || {};
|
|
9
|
+
// NOTICE: 某些情况下,需要优先读取参数中的 provider,其余情况均以当前登录用户的 provider 来决定
|
|
10
|
+
if (extraParams.providerMode === 'paramsFirst') {
|
|
11
|
+
provider = extraParams.provider;
|
|
13
12
|
}
|
|
14
|
-
|
|
15
|
-
//
|
|
16
|
-
|
|
13
|
+
else {
|
|
14
|
+
// 1. 优先读取当前登录用户的 provider
|
|
15
|
+
provider = request.user?.provider;
|
|
16
|
+
if (!provider) {
|
|
17
|
+
// 2. 读取在 headers 中指定的 provider
|
|
18
|
+
provider = request.headers['x-user-provider'];
|
|
19
|
+
}
|
|
20
|
+
if (!provider) {
|
|
21
|
+
// 3. 读取在 extraParams 中指定的 provider
|
|
22
|
+
provider = extraParams.provider;
|
|
23
|
+
}
|
|
17
24
|
}
|
|
18
25
|
return provider || constant_1.LOGIN_PROVIDER.WALLET;
|
|
19
26
|
}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.16.15-beta-
|
|
6
|
+
"version": "1.16.15-beta-ed0db59e",
|
|
7
7
|
"description": "graphql client to read/write data on abt node",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"typings": "lib/index.d.ts",
|
|
@@ -26,15 +26,15 @@
|
|
|
26
26
|
"author": "linchen1987 <linchen.1987@foxmail.com> (http://github.com/linchen1987)",
|
|
27
27
|
"license": "Apache-2.0",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@abtnode/client": "1.16.15-beta-
|
|
30
|
-
"@abtnode/constant": "1.16.15-beta-
|
|
29
|
+
"@abtnode/client": "1.16.15-beta-ed0db59e",
|
|
30
|
+
"@abtnode/constant": "1.16.15-beta-ed0db59e",
|
|
31
31
|
"@arcblock/did": "1.18.89",
|
|
32
32
|
"@arcblock/did-auth": "1.18.89",
|
|
33
33
|
"@arcblock/jwt": "1.18.89",
|
|
34
34
|
"@arcblock/ws": "1.18.89",
|
|
35
|
-
"@blocklet/constant": "1.16.15-beta-
|
|
36
|
-
"@blocklet/env": "1.16.15-beta-
|
|
37
|
-
"@blocklet/meta": "1.16.15-beta-
|
|
35
|
+
"@blocklet/constant": "1.16.15-beta-ed0db59e",
|
|
36
|
+
"@blocklet/env": "1.16.15-beta-ed0db59e",
|
|
37
|
+
"@blocklet/meta": "1.16.15-beta-ed0db59e",
|
|
38
38
|
"@did-connect/authenticator": "^2.2.0",
|
|
39
39
|
"@did-connect/handler": "^2.2.0",
|
|
40
40
|
"@nedb/core": "^2.1.5",
|
|
@@ -74,5 +74,5 @@
|
|
|
74
74
|
"ts-node": "^10.9.1",
|
|
75
75
|
"typescript": "^5.0.4"
|
|
76
76
|
},
|
|
77
|
-
"gitHead": "
|
|
77
|
+
"gitHead": "647ebe45f6214ea5c284c9234f1319b6ff72f915"
|
|
78
78
|
}
|