@blocklet/sdk 1.16.44-beta-20250528-003415-7a5e5d90 → 1.16.44-beta-20250529-223630-10e16ac8
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 +24 -2
- package/lib/config.js +60 -21
- package/lib/middlewares/fallback.d.ts +1 -0
- package/lib/middlewares/fallback.js +57 -1
- package/lib/middlewares/index.d.ts +1 -0
- package/package.json +9 -8
package/lib/config.d.ts
CHANGED
|
@@ -76,9 +76,20 @@ declare const _handleComponentInstalled: (data: {
|
|
|
76
76
|
}) => void;
|
|
77
77
|
declare const _handleConfigUpdate: (data: any) => void;
|
|
78
78
|
declare const _handleComponentConfigUpdate: (data: any) => void;
|
|
79
|
+
declare const getBlockletSettings: () => {
|
|
80
|
+
theme: {
|
|
81
|
+
prefer: "light";
|
|
82
|
+
light: {
|
|
83
|
+
palette: import("@mui/material").PaletteOptions;
|
|
84
|
+
};
|
|
85
|
+
dark: {
|
|
86
|
+
palette: import("@mui/material").PaletteOptions;
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
};
|
|
79
90
|
declare const getBlockletJs: (pageGroup?: string, pathPrefix?: string, source?: string) => string;
|
|
80
91
|
export { logger, setLogger, env, componentStore as components, MountPoint, // @deprecated, for backward compatibility
|
|
81
|
-
TComponent, events, Events, getBlockletJs, _handleComponentUpdateOld, _handleConfigUpdate, _handleComponentInstalled, _handleComponentUpdated, _handleComponentStarted, _handleComponentStopped, _handleComponentRemoved, _handleComponentConfigUpdate, };
|
|
92
|
+
TComponent, events, Events, getBlockletJs, _handleComponentUpdateOld, _handleConfigUpdate, _handleComponentInstalled, _handleComponentUpdated, _handleComponentStarted, _handleComponentStopped, _handleComponentRemoved, _handleComponentConfigUpdate, getBlockletSettings, };
|
|
82
93
|
declare const _default: {
|
|
83
94
|
logger: {
|
|
84
95
|
info: {
|
|
@@ -131,7 +142,18 @@ declare const _default: {
|
|
|
131
142
|
componentRemoved: string;
|
|
132
143
|
envUpdate: string;
|
|
133
144
|
};
|
|
134
|
-
fetchBlockletJs: () => Promise<
|
|
145
|
+
fetchBlockletJs: (type?: "js" | "json") => Promise<any>;
|
|
135
146
|
getBlockletJs: (pageGroup?: string, pathPrefix?: string, source?: string) => string;
|
|
147
|
+
getBlockletSettings: () => {
|
|
148
|
+
theme: {
|
|
149
|
+
prefer: "light";
|
|
150
|
+
light: {
|
|
151
|
+
palette: import("@mui/material").PaletteOptions;
|
|
152
|
+
};
|
|
153
|
+
dark: {
|
|
154
|
+
palette: import("@mui/material").PaletteOptions;
|
|
155
|
+
};
|
|
156
|
+
};
|
|
157
|
+
};
|
|
136
158
|
};
|
|
137
159
|
export default _default;
|
package/lib/config.js
CHANGED
|
@@ -3,7 +3,7 @@ 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._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.setLogger = exports.logger = void 0;
|
|
6
|
+
exports.getBlockletSettings = 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.setLogger = exports.logger = void 0;
|
|
7
7
|
/* eslint-disable prettier/prettier */
|
|
8
8
|
/* eslint-disable no-console */
|
|
9
9
|
const path_1 = __importDefault(require("path"));
|
|
@@ -14,6 +14,7 @@ const axios_1 = __importDefault(require("axios"));
|
|
|
14
14
|
const debug_1 = __importDefault(require("debug"));
|
|
15
15
|
const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
|
|
16
16
|
const throttle_1 = __importDefault(require("lodash/throttle"));
|
|
17
|
+
const theme_1 = require("@blocklet/theme");
|
|
17
18
|
const env_1 = __importDefault(require("@blocklet/env"));
|
|
18
19
|
const constant_1 = require("@blocklet/constant");
|
|
19
20
|
const util_1 = require("@blocklet/env/lib/util");
|
|
@@ -263,29 +264,66 @@ const _handleComponentConfigUpdate = (data) => {
|
|
|
263
264
|
}
|
|
264
265
|
};
|
|
265
266
|
exports._handleComponentConfigUpdate = _handleComponentConfigUpdate;
|
|
267
|
+
const DEFAULT_THEME_SETTINGS = {
|
|
268
|
+
prefer: 'light',
|
|
269
|
+
light: {
|
|
270
|
+
palette: theme_1.BLOCKLET_THEME_LIGHT.palette,
|
|
271
|
+
},
|
|
272
|
+
dark: {
|
|
273
|
+
palette: theme_1.BLOCKLET_THEME_DARK.palette,
|
|
274
|
+
},
|
|
275
|
+
};
|
|
266
276
|
let blockletJs = '';
|
|
267
|
-
|
|
277
|
+
let blockletSettings = { theme: DEFAULT_THEME_SETTINGS };
|
|
278
|
+
const fetchBlockletJs = async (type = 'js') => {
|
|
268
279
|
const componentDid = process.env.BLOCKLET_COMPONENT_DID;
|
|
269
280
|
try {
|
|
270
281
|
const { mountPoint } = componentStore.find((x) => x.did === componentDid);
|
|
271
|
-
const
|
|
282
|
+
const url = (0, ufo_1.joinURL)(env.appUrl, mountPoint === '/' ? '' : mountPoint, `__blocklet__.js?nocache=1&t=${Date.now()}${type === 'json' ? '&type=json' : ''}`);
|
|
283
|
+
const res = await axios_1.default.get(url, {
|
|
272
284
|
timeout: 8000,
|
|
273
285
|
headers: {
|
|
274
286
|
'User-Agent': `BlockletSDK/${version_1.version}`,
|
|
275
287
|
},
|
|
276
288
|
});
|
|
277
|
-
if (
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
289
|
+
if (type === 'js') {
|
|
290
|
+
if (typeof res.data === 'string' && res.data.startsWith('window.blocklet')) {
|
|
291
|
+
blockletJs = res.data;
|
|
292
|
+
logger.info(`Fetch blocklet.js succeed: ${env.appPid}/${componentDid}`);
|
|
293
|
+
return res.data;
|
|
294
|
+
}
|
|
295
|
+
throw new Error('Invalid blocklet.js');
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
let data;
|
|
299
|
+
try {
|
|
300
|
+
data = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
|
|
301
|
+
}
|
|
302
|
+
catch (e) {
|
|
303
|
+
throw new Error('Invalid blocklet.json response');
|
|
304
|
+
}
|
|
305
|
+
if (typeof data === 'object') {
|
|
306
|
+
blockletSettings = {
|
|
307
|
+
theme: (0, theme_1.merge)(DEFAULT_THEME_SETTINGS, data.theme || {}),
|
|
308
|
+
};
|
|
309
|
+
logger.info(`Fetch blocklet.json succeed: ${env.appPid}/${componentDid}`);
|
|
310
|
+
return data;
|
|
311
|
+
}
|
|
312
|
+
throw new Error('Invalid blocklet.json data');
|
|
281
313
|
}
|
|
282
|
-
throw new Error('Invalid blocklet.js');
|
|
283
314
|
}
|
|
284
315
|
catch (err) {
|
|
285
|
-
logger.error(`Fetch blocklet
|
|
286
|
-
return '';
|
|
316
|
+
logger.error(`Fetch blocklet.${type} failed: ${env.appPid}/${componentDid}`, err.message);
|
|
317
|
+
return type === 'js' ? '' : {};
|
|
287
318
|
}
|
|
288
319
|
};
|
|
320
|
+
const getBlockletSettings = () => {
|
|
321
|
+
return blockletSettings;
|
|
322
|
+
};
|
|
323
|
+
exports.getBlockletSettings = getBlockletSettings;
|
|
324
|
+
const refreshBlockletContext = (0, throttle_1.default)(async () => {
|
|
325
|
+
await Promise.all([fetchBlockletJs('js'), fetchBlockletJs('json')]);
|
|
326
|
+
}, 3000, { trailing: true });
|
|
289
327
|
// Page group is dynamic, so we need to create it on the fly
|
|
290
328
|
const normalize = (prefix) => `/${prefix}/`.replace(/\/+/g, '/');
|
|
291
329
|
const getBlockletJs = (pageGroup = '', pathPrefix = '', source = blockletJs) => {
|
|
@@ -303,7 +341,7 @@ const getBlockletJs = (pageGroup = '', pathPrefix = '', source = blockletJs) =>
|
|
|
303
341
|
return copy;
|
|
304
342
|
};
|
|
305
343
|
exports.getBlockletJs = getBlockletJs;
|
|
306
|
-
|
|
344
|
+
// prettier-ignore
|
|
307
345
|
const runInServer = (fn, type) =>
|
|
308
346
|
// eslint-disable-next-line consistent-return
|
|
309
347
|
(...args) => {
|
|
@@ -321,23 +359,23 @@ if (inRuntimeEnv && !process.env.BLOCKLET_HOOK_NAME && process.env.BLOCKLET_MODE
|
|
|
321
359
|
notification_1.default.on(constant_1.BlockletInternalEvents.appConfigChanged, _handleConfigUpdate);
|
|
322
360
|
notification_1.default.on(constant_1.BlockletInternalEvents.componentConfigChanged, _handleComponentConfigUpdate);
|
|
323
361
|
// Reactive fetch
|
|
324
|
-
notification_1.default.on(constant_1.BlockletInternalEvents.appConfigChanged,
|
|
325
|
-
notification_1.default.on(constant_1.BlockletInternalEvents.appSettingChanged,
|
|
326
|
-
notification_1.default.on(constant_1.BlockletInternalEvents.componentConfigChanged,
|
|
362
|
+
notification_1.default.on(constant_1.BlockletInternalEvents.appConfigChanged, refreshBlockletContext);
|
|
363
|
+
notification_1.default.on(constant_1.BlockletInternalEvents.appSettingChanged, refreshBlockletContext);
|
|
364
|
+
notification_1.default.on(constant_1.BlockletInternalEvents.componentConfigChanged, refreshBlockletContext);
|
|
327
365
|
notification_1.default.on(constant_1.BlockletInternalEvents.componentInstalled, runInServer(_handleComponentInstalled, 'new'));
|
|
328
366
|
notification_1.default.on(constant_1.BlockletInternalEvents.componentUpgraded, runInServer(_handleComponentUpdated, 'new'));
|
|
329
367
|
notification_1.default.on(constant_1.BlockletInternalEvents.componentUpdated, runInServer(_handleComponentUpdated, 'new'));
|
|
330
368
|
notification_1.default.on(constant_1.BlockletInternalEvents.componentStarted, runInServer(_handleComponentStarted, 'new'));
|
|
331
369
|
notification_1.default.on(constant_1.BlockletInternalEvents.componentStopped, runInServer(_handleComponentStopped, 'new'));
|
|
332
370
|
notification_1.default.on(constant_1.BlockletInternalEvents.componentRemoved, runInServer(_handleComponentRemoved, 'new'));
|
|
333
|
-
notification_1.default.on(constant_1.BlockletInternalEvents.componentInstalled, runInServer(
|
|
334
|
-
notification_1.default.on(constant_1.BlockletInternalEvents.componentUpgraded, runInServer(
|
|
335
|
-
notification_1.default.on(constant_1.BlockletInternalEvents.componentUpdated, runInServer(
|
|
336
|
-
notification_1.default.on(constant_1.BlockletInternalEvents.componentStarted, runInServer(
|
|
337
|
-
notification_1.default.on(constant_1.BlockletInternalEvents.componentStopped, runInServer(
|
|
338
|
-
notification_1.default.on(constant_1.BlockletInternalEvents.componentRemoved, runInServer(
|
|
371
|
+
notification_1.default.on(constant_1.BlockletInternalEvents.componentInstalled, runInServer(refreshBlockletContext, 'new'));
|
|
372
|
+
notification_1.default.on(constant_1.BlockletInternalEvents.componentUpgraded, runInServer(refreshBlockletContext, 'new'));
|
|
373
|
+
notification_1.default.on(constant_1.BlockletInternalEvents.componentUpdated, runInServer(refreshBlockletContext, 'new'));
|
|
374
|
+
notification_1.default.on(constant_1.BlockletInternalEvents.componentStarted, runInServer(refreshBlockletContext, 'new'));
|
|
375
|
+
notification_1.default.on(constant_1.BlockletInternalEvents.componentStopped, runInServer(refreshBlockletContext, 'new'));
|
|
376
|
+
notification_1.default.on(constant_1.BlockletInternalEvents.componentRemoved, runInServer(refreshBlockletContext, 'new'));
|
|
339
377
|
// Do an initial fetch
|
|
340
|
-
|
|
378
|
+
refreshBlockletContext();
|
|
341
379
|
}
|
|
342
380
|
exports.default = {
|
|
343
381
|
logger,
|
|
@@ -348,4 +386,5 @@ exports.default = {
|
|
|
348
386
|
Events,
|
|
349
387
|
fetchBlockletJs,
|
|
350
388
|
getBlockletJs,
|
|
389
|
+
getBlockletSettings,
|
|
351
390
|
};
|
|
@@ -11,6 +11,7 @@ type FallbackOptions = {
|
|
|
11
11
|
timeout?: number;
|
|
12
12
|
maxLength?: number;
|
|
13
13
|
cacheTtl?: number;
|
|
14
|
+
injectBlockletJs?: boolean;
|
|
14
15
|
};
|
|
15
16
|
declare const fallback: (file: string, options?: FallbackOptions) => (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
16
17
|
export = fallback;
|
|
@@ -8,6 +8,7 @@ const ufo_1 = require("ufo");
|
|
|
8
8
|
const escape_1 = __importDefault(require("lodash/escape"));
|
|
9
9
|
const crypto_1 = __importDefault(require("crypto"));
|
|
10
10
|
const constant_1 = require("@blocklet/constant");
|
|
11
|
+
const theme_1 = require("@blocklet/theme");
|
|
11
12
|
const config_1 = require("../config");
|
|
12
13
|
// Cache configurations
|
|
13
14
|
const DEFAULT_CACHE_TTL = 1 * 60 * 1000; // 1 minute
|
|
@@ -66,6 +67,49 @@ const tryWithTimeout = (asyncFn, timeout) => {
|
|
|
66
67
|
}
|
|
67
68
|
});
|
|
68
69
|
};
|
|
70
|
+
const buildThemeStyles = (theme) => {
|
|
71
|
+
const { light, dark } = theme;
|
|
72
|
+
return `
|
|
73
|
+
<style id="blocklet-theme-style">
|
|
74
|
+
:root {
|
|
75
|
+
--blocklet-background-default-color: ${light.palette?.background?.default ?? '#fff'};
|
|
76
|
+
--blocklet-text-primary-color: ${light.palette?.text?.primary ?? '#000'};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
[data-theme='dark'] {
|
|
80
|
+
--blocklet-background-default-color: ${dark.palette?.background?.default ?? '#000'};
|
|
81
|
+
--blocklet-text-primary-color: ${dark.palette?.text?.primary ?? '#fff'};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
html, body {
|
|
85
|
+
background-color: var(--blocklet-background-default-color);
|
|
86
|
+
color: var(--blocklet-text-primary-color);
|
|
87
|
+
}
|
|
88
|
+
</style>
|
|
89
|
+
`;
|
|
90
|
+
};
|
|
91
|
+
const buildThemeScript = (theme) => {
|
|
92
|
+
return `
|
|
93
|
+
<script id="blocklet-theme-script">
|
|
94
|
+
(function() {
|
|
95
|
+
try {
|
|
96
|
+
const BLOCKLET_THEME_PREFER_KEY = '${theme_1.BLOCKLET_THEME_PREFER_KEY}';
|
|
97
|
+
${theme_1.isValidThemeMode.toString()}
|
|
98
|
+
${theme_1.getDefaultThemePrefer.toString()}
|
|
99
|
+
|
|
100
|
+
const prefer = getDefaultThemePrefer(${JSON.stringify({ theme: { prefer: theme.prefer } })});
|
|
101
|
+
if(prefer === 'dark') {
|
|
102
|
+
document.documentElement.setAttribute('data-theme', 'dark');
|
|
103
|
+
} else {
|
|
104
|
+
document.documentElement.removeAttribute('data-theme');
|
|
105
|
+
}
|
|
106
|
+
} catch (e) {
|
|
107
|
+
console.error('Theme init error:', e);
|
|
108
|
+
}
|
|
109
|
+
})();
|
|
110
|
+
</script>
|
|
111
|
+
`;
|
|
112
|
+
};
|
|
69
113
|
const fallback = (file, options = {}) => {
|
|
70
114
|
const filePath = options.root ? (0, path_1.join)(options.root, file) : file;
|
|
71
115
|
// Check file existence during initialization
|
|
@@ -82,6 +126,7 @@ const fallback = (file, options = {}) => {
|
|
|
82
126
|
const pageGroup = req.headers['x-page-group'] || '';
|
|
83
127
|
const pathPrefix = req.headers['x-path-prefix'] || '';
|
|
84
128
|
const cacheKey = getCacheKey(req.path, filePath, pageGroup, pathPrefix);
|
|
129
|
+
const { theme } = (0, config_1.getBlockletSettings)();
|
|
85
130
|
if (cacheEnabled) {
|
|
86
131
|
// Check cache first
|
|
87
132
|
const cached = cache.get(cacheKey);
|
|
@@ -125,11 +170,22 @@ const fallback = (file, options = {}) => {
|
|
|
125
170
|
source = source.replace(HEAD_END_TAG, `<link rel="blocklet-open-embed" type="application/json" href="${pageData.embed}" />${HEAD_END_TAG}`);
|
|
126
171
|
}
|
|
127
172
|
const blockletJs = (0, config_1.getBlockletJs)(pageGroup, pathPrefix);
|
|
128
|
-
if (blockletJs) {
|
|
173
|
+
if (blockletJs && options.injectBlockletJs !== false) {
|
|
129
174
|
source = source
|
|
130
175
|
.replace('<script src="__blocklet__.js"></script>', `<script>${blockletJs}</script>`)
|
|
131
176
|
.replace('<script src="__meta__.js"></script>', `<script>${blockletJs}</script>`);
|
|
132
177
|
}
|
|
178
|
+
// Inject theme styles and script
|
|
179
|
+
const themeStyles = buildThemeStyles(theme);
|
|
180
|
+
const themeScript = buildThemeScript(theme);
|
|
181
|
+
// 注入主题样式
|
|
182
|
+
if (!source.includes('<style id="blocklet-theme">')) {
|
|
183
|
+
source = source.replace(HEAD_END_TAG, `${themeStyles}${HEAD_END_TAG}`);
|
|
184
|
+
}
|
|
185
|
+
// 注入主题切换脚本
|
|
186
|
+
if (!source.includes('<script id="blocklet-theme-script">')) {
|
|
187
|
+
source = source.replace(HEAD_END_TAG, `${themeScript}${HEAD_END_TAG}`);
|
|
188
|
+
}
|
|
133
189
|
// Cache the processed response
|
|
134
190
|
const etag = generateETag(source);
|
|
135
191
|
cache.set(cacheKey, {
|
|
@@ -41,6 +41,7 @@ declare const _default: {
|
|
|
41
41
|
timeout?: number;
|
|
42
42
|
maxLength?: number;
|
|
43
43
|
cacheTtl?: number;
|
|
44
|
+
injectBlockletJs?: boolean;
|
|
44
45
|
}) => (req: import("express").Request, res: import("express").Response, next: import("express").NextFunction) => Promise<void>;
|
|
45
46
|
sitemap: (generatorFn: (fn: (item: {
|
|
46
47
|
url: string;
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.16.44-beta-
|
|
6
|
+
"version": "1.16.44-beta-20250529-223630-10e16ac8",
|
|
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",
|
|
@@ -27,17 +27,18 @@
|
|
|
27
27
|
"author": "linchen1987 <linchen.1987@foxmail.com> (http://github.com/linchen1987)",
|
|
28
28
|
"license": "Apache-2.0",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@abtnode/client": "1.16.44-beta-
|
|
31
|
-
"@abtnode/constant": "1.16.44-beta-
|
|
32
|
-
"@abtnode/util": "1.16.44-beta-
|
|
30
|
+
"@abtnode/client": "1.16.44-beta-20250529-223630-10e16ac8",
|
|
31
|
+
"@abtnode/constant": "1.16.44-beta-20250529-223630-10e16ac8",
|
|
32
|
+
"@abtnode/util": "1.16.44-beta-20250529-223630-10e16ac8",
|
|
33
33
|
"@arcblock/did": "1.20.11",
|
|
34
34
|
"@arcblock/did-auth": "1.20.11",
|
|
35
35
|
"@arcblock/jwt": "1.20.11",
|
|
36
36
|
"@arcblock/ws": "1.20.11",
|
|
37
|
-
"@blocklet/constant": "1.16.44-beta-
|
|
38
|
-
"@blocklet/env": "1.16.44-beta-
|
|
37
|
+
"@blocklet/constant": "1.16.44-beta-20250529-223630-10e16ac8",
|
|
38
|
+
"@blocklet/env": "1.16.44-beta-20250529-223630-10e16ac8",
|
|
39
39
|
"@blocklet/error": "^0.2.4",
|
|
40
|
-
"@blocklet/meta": "1.16.44-beta-
|
|
40
|
+
"@blocklet/meta": "1.16.44-beta-20250529-223630-10e16ac8",
|
|
41
|
+
"@blocklet/theme": "^2.13.55",
|
|
41
42
|
"@did-connect/authenticator": "^2.2.8",
|
|
42
43
|
"@did-connect/handler": "^2.2.8",
|
|
43
44
|
"@nedb/core": "^2.1.5",
|
|
@@ -83,5 +84,5 @@
|
|
|
83
84
|
"ts-node": "^10.9.1",
|
|
84
85
|
"typescript": "^5.6.3"
|
|
85
86
|
},
|
|
86
|
-
"gitHead": "
|
|
87
|
+
"gitHead": "381ba5459e32dd7bc94f7ea62df65b72644d6d16"
|
|
87
88
|
}
|