@vlian/framework 1.2.25 → 1.2.38
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/dist/analytics.umd.js +208 -2395
- package/dist/analytics.umd.js.map +1 -1
- package/dist/core/Test.cjs +2 -2
- package/dist/core/Test.cjs.map +1 -1
- package/dist/core/Test.js +1 -1
- package/dist/core/Test.js.map +1 -1
- package/dist/core/config/ConfigLoader.cjs +7 -7
- package/dist/core/config/ConfigLoader.cjs.map +1 -1
- package/dist/core/config/ConfigLoader.js +1 -1
- package/dist/core/config/ConfigLoader.js.map +1 -1
- package/dist/core/error/ErrorBoundary.cjs +6 -6
- package/dist/core/error/ErrorBoundary.cjs.map +1 -1
- package/dist/core/error/ErrorBoundary.d.ts +1 -1
- package/dist/core/error/ErrorBoundary.js +2 -2
- package/dist/core/error/ErrorBoundary.js.map +1 -1
- package/dist/core/error/ErrorHandler.cjs +19 -19
- package/dist/core/error/ErrorHandler.cjs.map +1 -1
- package/dist/core/error/ErrorHandler.d.ts +2 -2
- package/dist/core/error/ErrorHandler.js +2 -2
- package/dist/core/error/ErrorHandler.js.map +1 -1
- package/dist/core/event/AppEventBus.cjs +5 -5
- package/dist/core/event/AppEventBus.cjs.map +1 -1
- package/dist/core/event/AppEventBus.js +1 -1
- package/dist/core/event/AppEventBus.js.map +1 -1
- package/dist/core/initialization/InitializationErrorThrower.cjs.map +1 -1
- package/dist/core/initialization/InitializationErrorThrower.js.map +1 -1
- package/dist/core/initialization/initialization.cjs +3 -3
- package/dist/core/initialization/initialization.cjs.map +1 -1
- package/dist/core/initialization/initialization.d.ts +1 -1
- package/dist/core/initialization/initialization.js +1 -1
- package/dist/core/initialization/initialization.js.map +1 -1
- package/dist/core/initialization/initializationErrorState.cjs +2 -2
- package/dist/core/initialization/initializationErrorState.cjs.map +1 -1
- package/dist/core/initialization/initializationErrorState.d.ts +1 -1
- package/dist/core/initialization/initializationErrorState.js +1 -1
- package/dist/core/initialization/initializationErrorState.js.map +1 -1
- package/dist/core/kernel/defaultAdapters.cjs +14 -13
- package/dist/core/kernel/defaultAdapters.cjs.map +1 -1
- package/dist/core/kernel/defaultAdapters.js +2 -1
- package/dist/core/kernel/defaultAdapters.js.map +1 -1
- package/dist/core/kernel/types.d.ts +1 -1
- package/dist/core/kernel/types.js.map +1 -1
- package/dist/core/router/RouterManager.cjs +9 -9
- package/dist/core/router/RouterManager.cjs.map +1 -1
- package/dist/core/router/RouterManager.js +1 -1
- package/dist/core/router/RouterManager.js.map +1 -1
- package/dist/core/router/adapter/AdapterManager.cjs +10 -10
- package/dist/core/router/adapter/AdapterManager.cjs.map +1 -1
- package/dist/core/router/adapter/AdapterManager.js +1 -1
- package/dist/core/router/adapter/AdapterManager.js.map +1 -1
- package/dist/core/router/adapter/react-router/ReactRouterAdapter.cjs +4 -4
- package/dist/core/router/adapter/react-router/ReactRouterAdapter.cjs.map +1 -1
- package/dist/core/router/adapter/react-router/ReactRouterAdapter.js +1 -1
- package/dist/core/router/adapter/react-router/ReactRouterAdapter.js.map +1 -1
- package/dist/core/router/dynamic/DynamicRouteManager.cjs +8 -8
- package/dist/core/router/dynamic/DynamicRouteManager.cjs.map +1 -1
- package/dist/core/router/dynamic/DynamicRouteManager.js +1 -1
- package/dist/core/router/dynamic/DynamicRouteManager.js.map +1 -1
- package/dist/core/router/errors/RouterError.cjs +4 -4
- package/dist/core/router/errors/RouterError.cjs.map +1 -1
- package/dist/core/router/errors/RouterError.d.ts +1 -1
- package/dist/core/router/errors/RouterError.js +1 -1
- package/dist/core/router/errors/RouterError.js.map +1 -1
- package/dist/core/router/lifecycle/RouterLifecycleManager.cjs +8 -8
- package/dist/core/router/lifecycle/RouterLifecycleManager.cjs.map +1 -1
- package/dist/core/router/lifecycle/RouterLifecycleManager.js +1 -1
- package/dist/core/router/lifecycle/RouterLifecycleManager.js.map +1 -1
- package/dist/core/router/middleware/RouterMiddlewareManager.cjs +11 -11
- package/dist/core/router/middleware/RouterMiddlewareManager.cjs.map +1 -1
- package/dist/core/router/middleware/RouterMiddlewareManager.js +1 -1
- package/dist/core/router/middleware/RouterMiddlewareManager.js.map +1 -1
- package/dist/core/router/middleware/auth.cjs +4 -4
- package/dist/core/router/middleware/auth.cjs.map +1 -1
- package/dist/core/router/middleware/auth.js +1 -1
- package/dist/core/router/middleware/auth.js.map +1 -1
- package/dist/core/router/monitoring/RouterMonitoring.cjs +2 -2
- package/dist/core/router/monitoring/RouterMonitoring.cjs.map +1 -1
- package/dist/core/router/monitoring/RouterMonitoring.js +2 -2
- package/dist/core/router/monitoring/RouterMonitoring.js.map +1 -1
- package/dist/core/router/navigation/RouterNavigation.cjs +7 -7
- package/dist/core/router/navigation/RouterNavigation.cjs.map +1 -1
- package/dist/core/router/navigation/RouterNavigation.js +1 -1
- package/dist/core/router/navigation/RouterNavigation.js.map +1 -1
- package/dist/core/router/performance/RouteCache.cjs +7 -7
- package/dist/core/router/performance/RouteCache.cjs.map +1 -1
- package/dist/core/router/performance/RouteCache.js +1 -1
- package/dist/core/router/performance/RouteCache.js.map +1 -1
- package/dist/core/router/performance/RoutePreloader.cjs +6 -6
- package/dist/core/router/performance/RoutePreloader.cjs.map +1 -1
- package/dist/core/router/performance/RoutePreloader.js +1 -1
- package/dist/core/router/performance/RoutePreloader.js.map +1 -1
- package/dist/core/router/plugin/RouterPluginManager.cjs +8 -8
- package/dist/core/router/plugin/RouterPluginManager.cjs.map +1 -1
- package/dist/core/router/plugin/RouterPluginManager.js +1 -1
- package/dist/core/router/plugin/RouterPluginManager.js.map +1 -1
- package/dist/core/router/utils/adapters/react-router/transform.cjs +6 -2
- package/dist/core/router/utils/adapters/react-router/transform.cjs.map +1 -1
- package/dist/core/router/utils/adapters/react-router/transform.js +5 -1
- package/dist/core/router/utils/adapters/react-router/transform.js.map +1 -1
- package/dist/core/router/utils/transform.cjs +13 -12
- package/dist/core/router/utils/transform.cjs.map +1 -1
- package/dist/core/router/utils/transform.js +2 -1
- package/dist/core/router/utils/transform.js.map +1 -1
- package/dist/core/router/validation/RouterConfigValidator.cjs +2 -2
- package/dist/core/router/validation/RouterConfigValidator.cjs.map +1 -1
- package/dist/core/router/validation/RouterConfigValidator.js +1 -1
- package/dist/core/router/validation/RouterConfigValidator.js.map +1 -1
- package/dist/core/router/version/RouteVersionManager.cjs +6 -6
- package/dist/core/router/version/RouteVersionManager.cjs.map +1 -1
- package/dist/core/router/version/RouteVersionManager.js +1 -1
- package/dist/core/router/version/RouteVersionManager.js.map +1 -1
- package/dist/core/splash/SplashScreen.cjs +4 -4
- package/dist/core/splash/SplashScreen.cjs.map +1 -1
- package/dist/core/splash/SplashScreen.js +1 -1
- package/dist/core/splash/SplashScreen.js.map +1 -1
- package/dist/core/startup/initializeServices.cjs +15 -15
- package/dist/core/startup/initializeServices.cjs.map +1 -1
- package/dist/core/startup/initializeServices.d.ts +2 -2
- package/dist/core/startup/initializeServices.js +3 -3
- package/dist/core/startup/initializeServices.js.map +1 -1
- package/dist/core/startup/performanceTracker.cjs.map +1 -1
- package/dist/core/startup/performanceTracker.d.ts +1 -1
- package/dist/core/startup/performanceTracker.js.map +1 -1
- package/dist/core/startup/renderApp.cjs +3 -3
- package/dist/core/startup/renderApp.cjs.map +1 -1
- package/dist/core/startup/renderApp.d.ts +1 -1
- package/dist/core/startup/renderApp.js +2 -2
- package/dist/core/startup/renderApp.js.map +1 -1
- package/dist/core/startup/startApp.cjs +24 -24
- package/dist/core/startup/startApp.cjs.map +1 -1
- package/dist/core/startup/startApp.js +5 -5
- package/dist/core/startup/startApp.js.map +1 -1
- package/dist/core/types.d.ts +3 -3
- package/dist/core/types.js.map +1 -1
- package/dist/index.cjs +15 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +6 -4
- package/dist/index.js +5 -4
- package/dist/index.js.map +1 -1
- package/dist/index.umd.js +2150 -23125
- package/dist/index.umd.js.map +1 -1
- package/dist/kernel/constants.cjs +67 -0
- package/dist/kernel/constants.cjs.map +1 -0
- package/dist/kernel/constants.d.ts +5 -0
- package/dist/kernel/constants.js +43 -0
- package/dist/kernel/constants.js.map +1 -0
- package/dist/kernel/index.cjs +40 -0
- package/dist/kernel/index.cjs.map +1 -0
- package/dist/kernel/index.d.ts +3 -0
- package/dist/kernel/index.js +4 -0
- package/dist/kernel/index.js.map +1 -0
- package/dist/kernel/kernel.cjs +296 -0
- package/dist/kernel/kernel.cjs.map +1 -0
- package/dist/kernel/kernel.d.ts +40 -0
- package/dist/kernel/kernel.js +272 -0
- package/dist/kernel/kernel.js.map +1 -0
- package/dist/kernel/manager/cacheManager.cjs +46 -0
- package/dist/kernel/manager/cacheManager.cjs.map +1 -0
- package/dist/kernel/manager/cacheManager.d.ts +6 -0
- package/dist/kernel/manager/cacheManager.js +36 -0
- package/dist/kernel/manager/cacheManager.js.map +1 -0
- package/dist/kernel/manager/i18nManager.cjs +68 -0
- package/dist/kernel/manager/i18nManager.cjs.map +1 -0
- package/dist/kernel/manager/i18nManager.d.ts +8 -0
- package/dist/kernel/manager/i18nManager.js +58 -0
- package/dist/kernel/manager/i18nManager.js.map +1 -0
- package/dist/kernel/manager/index.cjs +30 -0
- package/dist/kernel/manager/index.cjs.map +1 -0
- package/dist/kernel/manager/index.d.ts +4 -0
- package/dist/kernel/manager/index.js +6 -0
- package/dist/kernel/manager/index.js.map +1 -0
- package/dist/kernel/manager/loggerManager.cjs +70 -0
- package/dist/kernel/manager/loggerManager.cjs.map +1 -0
- package/dist/kernel/manager/loggerManager.d.ts +14 -0
- package/dist/kernel/manager/loggerManager.js +60 -0
- package/dist/kernel/manager/loggerManager.js.map +1 -0
- package/dist/kernel/manager/persistence.cjs +93 -0
- package/dist/kernel/manager/persistence.cjs.map +1 -0
- package/dist/kernel/manager/persistence.d.ts +3 -0
- package/dist/kernel/manager/persistence.js +75 -0
- package/dist/kernel/manager/persistence.js.map +1 -0
- package/dist/kernel/manager/themeManager.cjs +85 -0
- package/dist/kernel/manager/themeManager.cjs.map +1 -0
- package/dist/kernel/manager/themeManager.d.ts +9 -0
- package/dist/kernel/manager/themeManager.js +75 -0
- package/dist/kernel/manager/themeManager.js.map +1 -0
- package/dist/kernel/types.cjs +6 -0
- package/dist/kernel/types.cjs.map +1 -0
- package/dist/kernel/types.d.ts +72 -0
- package/dist/kernel/types.js +3 -0
- package/dist/kernel/types.js.map +1 -0
- package/dist/library/storage/encryption.cjs +12 -13
- package/dist/library/storage/encryption.cjs.map +1 -1
- package/dist/library/storage/encryption.js +1 -2
- package/dist/library/storage/encryption.js.map +1 -1
- package/dist/request/adapter.d.ts +1 -0
- package/dist/request/core.d.ts +1 -0
- package/dist/request/index.d.ts +1 -42
- package/dist/request/plugin/csrfPlugin.d.ts +2 -2
- package/dist/request/plugin/queue.d.ts +2 -2
- package/dist/request/plugin.d.ts +1 -0
- package/dist/request/runtime.d.ts +1 -0
- package/dist/request/types.d.ts +1 -394
- package/dist/request/utils.d.ts +1 -0
- package/dist/state.umd.js +1 -1
- package/dist/utils/csrf.cjs +13 -152
- package/dist/utils/csrf.cjs.map +1 -1
- package/dist/utils/csrf.d.ts +1 -72
- package/dist/utils/csrf.js +1 -142
- package/dist/utils/csrf.js.map +1 -1
- package/dist/utils/errors/ErrorCodes.cjs +6 -76
- package/dist/utils/errors/ErrorCodes.cjs.map +1 -1
- package/dist/utils/errors/ErrorCodes.d.ts +1 -45
- package/dist/utils/errors/ErrorCodes.js +1 -84
- package/dist/utils/errors/ErrorCodes.js.map +1 -1
- package/dist/utils/errors.cjs +53 -326
- package/dist/utils/errors.cjs.map +1 -1
- package/dist/utils/errors.d.ts +19 -172
- package/dist/utils/errors.js +16 -352
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/logger.cjs +5 -374
- package/dist/utils/logger.cjs.map +1 -1
- package/dist/utils/logger.d.ts +2 -189
- package/dist/utils/logger.js +1 -379
- package/dist/utils/logger.js.map +1 -1
- package/dist/utils/logger.types.cjs +3 -12
- package/dist/utils/logger.types.cjs.map +1 -1
- package/dist/utils/logger.types.d.ts +2 -57
- package/dist/utils/logger.types.js +1 -10
- package/dist/utils/logger.types.js.map +1 -1
- package/dist/utils/monitoring.cjs +11 -302
- package/dist/utils/monitoring.cjs.map +1 -1
- package/dist/utils/monitoring.d.ts +1 -163
- package/dist/utils/monitoring.js +1 -294
- package/dist/utils/monitoring.js.map +1 -1
- package/dist/utils/performance.cjs +5 -352
- package/dist/utils/performance.cjs.map +1 -1
- package/dist/utils/performance.d.ts +2 -246
- package/dist/utils/performance.js +1 -354
- package/dist/utils/performance.js.map +1 -1
- package/dist/utils/resourceLoader.cjs +5 -303
- package/dist/utils/resourceLoader.cjs.map +1 -1
- package/dist/utils/resourceLoader.d.ts +2 -130
- package/dist/utils/resourceLoader.js +1 -305
- package/dist/utils/resourceLoader.js.map +1 -1
- package/dist/utils/runtimeSecurity.cjs +2 -140
- package/dist/utils/runtimeSecurity.cjs.map +1 -1
- package/dist/utils/runtimeSecurity.d.ts +2 -104
- package/dist/utils/runtimeSecurity.js +1 -141
- package/dist/utils/runtimeSecurity.js.map +1 -1
- package/dist/utils/security.cjs +3 -314
- package/dist/utils/security.cjs.map +1 -1
- package/dist/utils/security.d.ts +2 -80
- package/dist/utils/security.js +1 -311
- package/dist/utils/security.js.map +1 -1
- package/dist/utils/traceId.cjs +10 -111
- package/dist/utils/traceId.cjs.map +1 -1
- package/dist/utils/traceId.d.ts +1 -63
- package/dist/utils/traceId.js +1 -116
- package/dist/utils/traceId.js.map +1 -1
- package/dist/utils/validation.cjs +3 -173
- package/dist/utils/validation.cjs.map +1 -1
- package/dist/utils/validation.d.ts +2 -110
- package/dist/utils/validation.js +1 -175
- package/dist/utils/validation.js.map +1 -1
- package/package.json +15 -24
- package/dist/lazy/index.cjs +0 -104
- package/dist/lazy/index.cjs.map +0 -1
- package/dist/lazy/index.d.ts +0 -19
- package/dist/lazy/index.js +0 -24
- package/dist/lazy/index.js.map +0 -1
- package/dist/request/adapter/RequestAdapter.cjs +0 -78
- package/dist/request/adapter/RequestAdapter.cjs.map +0 -1
- package/dist/request/adapter/axiosAdapter.cjs +0 -164
- package/dist/request/adapter/axiosAdapter.cjs.map +0 -1
- package/dist/request/adapter/fetchAdapter.cjs +0 -134
- package/dist/request/adapter/fetchAdapter.cjs.map +0 -1
- package/dist/request/adapter/index.cjs +0 -80
- package/dist/request/adapter/index.cjs.map +0 -1
- package/dist/request/adapter/kyAdapter.cjs +0 -191
- package/dist/request/adapter/kyAdapter.cjs.map +0 -1
- package/dist/request/adapter/undiciAdapter.cjs +0 -213
- package/dist/request/adapter/undiciAdapter.cjs.map +0 -1
- package/dist/request/core/RequestClient.cjs +0 -558
- package/dist/request/core/RequestClient.cjs.map +0 -1
- package/dist/request/core/index.cjs +0 -15
- package/dist/request/core/index.cjs.map +0 -1
- package/dist/request/index.cjs +0 -149
- package/dist/request/index.cjs.map +0 -1
- package/dist/request/plugin/RequestPlugin.cjs +0 -218
- package/dist/request/plugin/RequestPlugin.cjs.map +0 -1
- package/dist/request/plugin/cache.cjs +0 -269
- package/dist/request/plugin/cache.cjs.map +0 -1
- package/dist/request/plugin/csrfPlugin.cjs +0 -40
- package/dist/request/plugin/csrfPlugin.cjs.map +0 -1
- package/dist/request/plugin/index.cjs +0 -53
- package/dist/request/plugin/index.cjs.map +0 -1
- package/dist/request/plugin/monitoring.cjs +0 -216
- package/dist/request/plugin/monitoring.cjs.map +0 -1
- package/dist/request/plugin/queue.cjs +0 -140
- package/dist/request/plugin/queue.cjs.map +0 -1
- package/dist/request/plugin/retry.cjs +0 -98
- package/dist/request/plugin/retry.cjs.map +0 -1
- package/dist/request/plugin/validation.cjs +0 -121
- package/dist/request/plugin/validation.cjs.map +0 -1
- package/dist/request/runtime/RequestContext.cjs +0 -77
- package/dist/request/runtime/RequestContext.cjs.map +0 -1
- package/dist/request/runtime/index.cjs +0 -32
- package/dist/request/runtime/index.cjs.map +0 -1
- package/dist/request/types.cjs +0 -112
- package/dist/request/types.cjs.map +0 -1
- package/dist/request/utils/RequestQueueManager.cjs +0 -168
- package/dist/request/utils/RequestQueueManager.cjs.map +0 -1
- package/dist/request/utils/dependencyCheck.cjs +0 -237
- package/dist/request/utils/dependencyCheck.cjs.map +0 -1
- package/dist/request/utils/index.cjs +0 -30
- package/dist/request/utils/index.cjs.map +0 -1
- package/dist/request.umd.js +0 -5392
- package/dist/request.umd.js.map +0 -1
|
@@ -1,307 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
* 资源类型
|
|
3
|
-
*/ function _define_property(obj, key, value) {
|
|
4
|
-
if (key in obj) {
|
|
5
|
-
Object.defineProperty(obj, key, {
|
|
6
|
-
value: value,
|
|
7
|
-
enumerable: true,
|
|
8
|
-
configurable: true,
|
|
9
|
-
writable: true
|
|
10
|
-
});
|
|
11
|
-
} else {
|
|
12
|
-
obj[key] = value;
|
|
13
|
-
}
|
|
14
|
-
return obj;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* 资源优先级
|
|
18
|
-
*/ export var ResourcePriority = /*#__PURE__*/ function(ResourcePriority) {
|
|
19
|
-
/**
|
|
20
|
-
* 高优先级 - 关键资源
|
|
21
|
-
*/ ResourcePriority["HIGH"] = "high";
|
|
22
|
-
/**
|
|
23
|
-
* 中优先级 - 重要资源
|
|
24
|
-
*/ ResourcePriority["MEDIUM"] = "medium";
|
|
25
|
-
/**
|
|
26
|
-
* 低优先级 - 非关键资源
|
|
27
|
-
*/ ResourcePriority["LOW"] = "low";
|
|
28
|
-
/**
|
|
29
|
-
* 自动 - 由浏览器决定
|
|
30
|
-
*/ ResourcePriority["AUTO"] = "auto";
|
|
31
|
-
return ResourcePriority;
|
|
32
|
-
}({});
|
|
33
|
-
/**
|
|
34
|
-
* 资源加载器类
|
|
35
|
-
*/ export class ResourceLoader {
|
|
36
|
-
/**
|
|
37
|
-
* 获取动态并发数(基于网络状况)
|
|
38
|
-
*/ getDynamicConcurrency() {
|
|
39
|
-
if (typeof navigator === 'undefined' || !('connection' in navigator)) {
|
|
40
|
-
return this.maxConcurrentLoads;
|
|
41
|
-
}
|
|
42
|
-
const connection = navigator.connection;
|
|
43
|
-
if (!connection) {
|
|
44
|
-
return this.maxConcurrentLoads;
|
|
45
|
-
}
|
|
46
|
-
// 根据网络类型调整并发数
|
|
47
|
-
const effectiveType = connection.effectiveType;
|
|
48
|
-
switch(effectiveType){
|
|
49
|
-
case 'slow-2g':
|
|
50
|
-
case '2g':
|
|
51
|
-
return 2;
|
|
52
|
-
case '3g':
|
|
53
|
-
return 4;
|
|
54
|
-
case '4g':
|
|
55
|
-
default:
|
|
56
|
-
return this.maxConcurrentLoads;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* 加载脚本
|
|
61
|
-
*/ async loadScript(options) {
|
|
62
|
-
const { url, ...rest } = options;
|
|
63
|
-
return this.loadResource({
|
|
64
|
-
...rest,
|
|
65
|
-
type: 'script',
|
|
66
|
-
url
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* 加载样式
|
|
71
|
-
*/ async loadStyle(options) {
|
|
72
|
-
const { url, ...rest } = options;
|
|
73
|
-
return this.loadResource({
|
|
74
|
-
...rest,
|
|
75
|
-
type: 'style',
|
|
76
|
-
url
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* 预加载资源
|
|
81
|
-
*/ preload(options) {
|
|
82
|
-
if (typeof document === 'undefined') {
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
const link = document.createElement('link');
|
|
86
|
-
link.rel = 'preload';
|
|
87
|
-
link.href = options.url;
|
|
88
|
-
link.as = this.getResourceAs(options.type);
|
|
89
|
-
if (options.priority) {
|
|
90
|
-
link.setAttribute('fetchpriority', options.priority);
|
|
91
|
-
}
|
|
92
|
-
if (options.crossOrigin) {
|
|
93
|
-
link.crossOrigin = options.crossOrigin;
|
|
94
|
-
}
|
|
95
|
-
if (options.integrity) {
|
|
96
|
-
link.integrity = options.integrity;
|
|
97
|
-
}
|
|
98
|
-
document.head.appendChild(link);
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* 预取资源
|
|
102
|
-
*/ prefetch(options) {
|
|
103
|
-
if (typeof document === 'undefined') {
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
const link = document.createElement('link');
|
|
107
|
-
link.rel = 'prefetch';
|
|
108
|
-
link.href = options.url;
|
|
109
|
-
if (options.priority) {
|
|
110
|
-
link.setAttribute('fetchpriority', options.priority);
|
|
111
|
-
}
|
|
112
|
-
document.head.appendChild(link);
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* 加载资源
|
|
116
|
-
*/ async loadResource(options) {
|
|
117
|
-
const { url, type, onLoad, onError } = options;
|
|
118
|
-
// 如果已经加载过,直接返回
|
|
119
|
-
if (this.loadedResources.has(url)) {
|
|
120
|
-
onLoad?.();
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
// 如果正在加载,等待加载完成
|
|
124
|
-
if (this.loadingResources.has(url)) {
|
|
125
|
-
await this.loadingResources.get(url);
|
|
126
|
-
onLoad?.();
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
// 创建加载 Promise
|
|
130
|
-
const loadPromise = this.doLoadResource({
|
|
131
|
-
...options,
|
|
132
|
-
type,
|
|
133
|
-
url
|
|
134
|
-
});
|
|
135
|
-
// 保存加载 Promise
|
|
136
|
-
this.loadingResources.set(url, loadPromise);
|
|
137
|
-
try {
|
|
138
|
-
await loadPromise;
|
|
139
|
-
this.loadedResources.add(url);
|
|
140
|
-
onLoad?.();
|
|
141
|
-
} catch (error) {
|
|
142
|
-
const err = error instanceof Error ? error : new Error(String(error));
|
|
143
|
-
onError?.(err);
|
|
144
|
-
throw err;
|
|
145
|
-
} finally{
|
|
146
|
-
this.loadingResources.delete(url);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* 执行资源加载
|
|
151
|
-
*/ doLoadResource(options) {
|
|
152
|
-
return new Promise((resolve, reject)=>{
|
|
153
|
-
if (typeof document === 'undefined') {
|
|
154
|
-
reject(new Error('资源加载需要在浏览器环境中执行'));
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
const { url, type, async = true, defer, crossOrigin, integrity } = options;
|
|
158
|
-
if (type === 'script') {
|
|
159
|
-
const script = document.createElement('script');
|
|
160
|
-
script.src = url;
|
|
161
|
-
script.async = async;
|
|
162
|
-
if (defer !== undefined) {
|
|
163
|
-
script.defer = defer;
|
|
164
|
-
}
|
|
165
|
-
if (crossOrigin) {
|
|
166
|
-
script.crossOrigin = crossOrigin;
|
|
167
|
-
}
|
|
168
|
-
if (integrity) {
|
|
169
|
-
script.integrity = integrity;
|
|
170
|
-
}
|
|
171
|
-
script.onload = ()=>resolve();
|
|
172
|
-
script.onerror = ()=>reject(new Error(`Failed to load script: ${url}`));
|
|
173
|
-
document.head.appendChild(script);
|
|
174
|
-
} else if (type === 'style') {
|
|
175
|
-
const link = document.createElement('link');
|
|
176
|
-
link.rel = 'stylesheet';
|
|
177
|
-
link.href = url;
|
|
178
|
-
if (crossOrigin) {
|
|
179
|
-
link.crossOrigin = crossOrigin;
|
|
180
|
-
}
|
|
181
|
-
if (integrity) {
|
|
182
|
-
link.integrity = integrity;
|
|
183
|
-
}
|
|
184
|
-
link.onload = ()=>resolve();
|
|
185
|
-
link.onerror = ()=>reject(new Error(`Failed to load stylesheet: ${url}`));
|
|
186
|
-
document.head.appendChild(link);
|
|
187
|
-
} else {
|
|
188
|
-
reject(new Error(`Unsupported resource type: ${type}`));
|
|
189
|
-
}
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* 获取资源 as 属性值
|
|
194
|
-
*/ getResourceAs(type) {
|
|
195
|
-
const asMap = {
|
|
196
|
-
script: 'script',
|
|
197
|
-
style: 'style',
|
|
198
|
-
image: 'image',
|
|
199
|
-
font: 'font',
|
|
200
|
-
prefetch: 'fetch',
|
|
201
|
-
preload: 'fetch'
|
|
202
|
-
};
|
|
203
|
-
return asMap[type] || 'fetch';
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* 使用 requestIdleCallback 加载低优先级资源
|
|
207
|
-
*/ loadWhenIdle(options, timeout) {
|
|
208
|
-
if (typeof requestIdleCallback !== 'undefined') {
|
|
209
|
-
requestIdleCallback(()=>{
|
|
210
|
-
this.loadResource(options).catch((error)=>{
|
|
211
|
-
console.warn('Idle resource load failed:', error);
|
|
212
|
-
});
|
|
213
|
-
}, {
|
|
214
|
-
timeout
|
|
215
|
-
});
|
|
216
|
-
} else {
|
|
217
|
-
// 降级方案:延迟加载
|
|
218
|
-
setTimeout(()=>{
|
|
219
|
-
this.loadResource(options).catch((error)=>{
|
|
220
|
-
console.warn('Delayed resource load failed:', error);
|
|
221
|
-
});
|
|
222
|
-
}, 1000);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* 批量加载资源(按优先级)
|
|
227
|
-
*/ async loadResources(resources) {
|
|
228
|
-
// 按优先级排序
|
|
229
|
-
const sortedResources = this.sortByPriority(resources);
|
|
230
|
-
// 按优先级分组
|
|
231
|
-
const highPriority = sortedResources.filter((r)=>r.priority === "high" || !r.priority);
|
|
232
|
-
const mediumPriority = sortedResources.filter((r)=>r.priority === "medium");
|
|
233
|
-
const lowPriority = sortedResources.filter((r)=>r.priority === "low");
|
|
234
|
-
const results = [];
|
|
235
|
-
// 先加载高优先级资源(并行,但限制并发数)
|
|
236
|
-
if (highPriority.length > 0) {
|
|
237
|
-
const highResults = await this.loadResourcesWithConcurrencyLimit(highPriority);
|
|
238
|
-
results.push(...highResults);
|
|
239
|
-
}
|
|
240
|
-
// 然后加载中优先级资源
|
|
241
|
-
if (mediumPriority.length > 0) {
|
|
242
|
-
const mediumResults = await this.loadResourcesWithConcurrencyLimit(mediumPriority);
|
|
243
|
-
results.push(...mediumResults);
|
|
244
|
-
}
|
|
245
|
-
// 最后加载低优先级资源(使用空闲时间)
|
|
246
|
-
if (lowPriority.length > 0) {
|
|
247
|
-
// 低优先级资源在空闲时加载
|
|
248
|
-
for (const resource of lowPriority){
|
|
249
|
-
this.loadWhenIdle(resource);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
return results;
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* 按优先级排序资源
|
|
256
|
-
*/ sortByPriority(resources) {
|
|
257
|
-
const priorityOrder = {
|
|
258
|
-
["high"]: 0,
|
|
259
|
-
["medium"]: 1,
|
|
260
|
-
["low"]: 2,
|
|
261
|
-
["auto"]: 3
|
|
262
|
-
};
|
|
263
|
-
return [
|
|
264
|
-
...resources
|
|
265
|
-
].sort((a, b)=>{
|
|
266
|
-
const aPriority = priorityOrder[a.priority || "auto"] ?? 3;
|
|
267
|
-
const bPriority = priorityOrder[b.priority || "auto"] ?? 3;
|
|
268
|
-
return aPriority - bPriority;
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
/**
|
|
272
|
-
* 限制并发数的资源加载(优化版本)
|
|
273
|
-
*/ async loadResourcesWithConcurrencyLimit(resources, concurrency) {
|
|
274
|
-
const maxConcurrency = concurrency ?? this.getDynamicConcurrency();
|
|
275
|
-
const results = [];
|
|
276
|
-
const executing = new Set();
|
|
277
|
-
for (const resource of resources){
|
|
278
|
-
// 如果达到并发限制,等待一个任务完成
|
|
279
|
-
if (executing.size >= maxConcurrency) {
|
|
280
|
-
await Promise.race(executing);
|
|
281
|
-
}
|
|
282
|
-
const promise = this.loadResource(resource).finally(()=>{
|
|
283
|
-
executing.delete(promise);
|
|
284
|
-
});
|
|
285
|
-
executing.add(promise);
|
|
286
|
-
results.push(promise);
|
|
287
|
-
}
|
|
288
|
-
// 等待所有任务完成
|
|
289
|
-
await Promise.all(results);
|
|
290
|
-
return [];
|
|
291
|
-
}
|
|
292
|
-
/**
|
|
293
|
-
* 检查资源是否已加载
|
|
294
|
-
*/ isLoaded(url) {
|
|
295
|
-
return this.loadedResources.has(url);
|
|
296
|
-
}
|
|
297
|
-
constructor(){
|
|
298
|
-
_define_property(this, "loadedResources", new Set());
|
|
299
|
-
_define_property(this, "loadingResources", new Map());
|
|
300
|
-
_define_property(this, "maxConcurrentLoads", 6); // 最大并发加载数(默认值)
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
/**
|
|
304
|
-
* 默认资源加载器实例
|
|
305
|
-
*/ export const resourceLoader = new ResourceLoader();
|
|
1
|
+
export { ResourceLoader, ResourcePriority, resourceLoader } from "@vlian/utils";
|
|
306
2
|
|
|
307
3
|
//# sourceMappingURL=resourceLoader.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/resourceLoader.ts"],"sourcesContent":["/**\n * 资源类型\n */\nexport type ResourceType = 'script' | 'style' | 'image' | 'font' | 'prefetch' | 'preload';\n\n/**\n * 资源优先级\n */\nexport enum ResourcePriority {\n /**\n * 高优先级 - 关键资源\n */\n HIGH = 'high',\n /**\n * 中优先级 - 重要资源\n */\n MEDIUM = 'medium',\n /**\n * 低优先级 - 非关键资源\n */\n LOW = 'low',\n /**\n * 自动 - 由浏览器决定\n */\n AUTO = 'auto',\n}\n\n/**\n * 资源加载选项\n */\nexport interface ResourceLoadOptions {\n /**\n * 资源类型\n */\n type: ResourceType;\n /**\n * 资源 URL\n */\n url: string;\n /**\n * 优先级\n */\n priority?: ResourcePriority;\n /**\n * 是否异步加载\n */\n async?: boolean;\n /**\n * 是否延迟加载\n */\n defer?: boolean;\n /**\n * 跨域设置\n */\n crossOrigin?: 'anonymous' | 'use-credentials';\n /**\n * 完整性校验(SRI)\n */\n integrity?: string;\n /**\n * 加载完成回调\n */\n onLoad?: () => void;\n /**\n * 加载失败回调\n */\n onError?: (error: Error) => void;\n}\n\n/**\n * 资源加载器类\n */\nexport class ResourceLoader {\n private loadedResources: Set<string> = new Set();\n private loadingResources: Map<string, Promise<void>> = new Map();\n private maxConcurrentLoads: number = 6; // 最大并发加载数(默认值)\n\n /**\n * 获取动态并发数(基于网络状况)\n */\n private getDynamicConcurrency(): number {\n if (typeof navigator === 'undefined' || !('connection' in navigator)) {\n return this.maxConcurrentLoads;\n }\n\n const connection = (navigator as any).connection;\n if (!connection) {\n return this.maxConcurrentLoads;\n }\n\n // 根据网络类型调整并发数\n const effectiveType = connection.effectiveType;\n switch (effectiveType) {\n case 'slow-2g':\n case '2g':\n return 2;\n case '3g':\n return 4;\n case '4g':\n default:\n return this.maxConcurrentLoads;\n }\n }\n\n /**\n * 加载脚本\n */\n async loadScript(options: Omit<ResourceLoadOptions, 'type'>): Promise<void> {\n const { url, ...rest } = options;\n return this.loadResource({ ...rest, type: 'script', url });\n }\n\n /**\n * 加载样式\n */\n async loadStyle(options: Omit<ResourceLoadOptions, 'type'>): Promise<void> {\n const { url, ...rest } = options;\n return this.loadResource({ ...rest, type: 'style', url });\n }\n\n /**\n * 预加载资源\n */\n preload(options: ResourceLoadOptions): void {\n if (typeof document === 'undefined') {\n return;\n }\n\n const link = document.createElement('link');\n link.rel = 'preload';\n link.href = options.url;\n link.as = this.getResourceAs(options.type);\n\n if (options.priority) {\n link.setAttribute('fetchpriority', options.priority);\n }\n\n if (options.crossOrigin) {\n link.crossOrigin = options.crossOrigin;\n }\n\n if (options.integrity) {\n link.integrity = options.integrity;\n }\n\n document.head.appendChild(link);\n }\n\n /**\n * 预取资源\n */\n prefetch(options: Omit<ResourceLoadOptions, 'type'>): void {\n if (typeof document === 'undefined') {\n return;\n }\n\n const link = document.createElement('link');\n link.rel = 'prefetch';\n link.href = options.url;\n\n if (options.priority) {\n link.setAttribute('fetchpriority', options.priority);\n }\n\n document.head.appendChild(link);\n }\n\n /**\n * 加载资源\n */\n async loadResource(options: ResourceLoadOptions): Promise<void> {\n const { url, type, onLoad, onError } = options;\n\n // 如果已经加载过,直接返回\n if (this.loadedResources.has(url)) {\n onLoad?.();\n return;\n }\n\n // 如果正在加载,等待加载完成\n if (this.loadingResources.has(url)) {\n await this.loadingResources.get(url);\n onLoad?.();\n return;\n }\n\n // 创建加载 Promise\n const loadPromise = this.doLoadResource({ ...options, type, url });\n\n // 保存加载 Promise\n this.loadingResources.set(url, loadPromise);\n\n try {\n await loadPromise;\n this.loadedResources.add(url);\n onLoad?.();\n } catch (error) {\n const err = error instanceof Error ? error : new Error(String(error));\n onError?.(err);\n throw err;\n } finally {\n this.loadingResources.delete(url);\n }\n }\n\n /**\n * 执行资源加载\n */\n private doLoadResource(options: ResourceLoadOptions): Promise<void> {\n return new Promise((resolve, reject) => {\n if (typeof document === 'undefined') {\n reject(new Error('资源加载需要在浏览器环境中执行'));\n return;\n }\n\n const { url, type, async = true, defer, crossOrigin, integrity } = options;\n\n if (type === 'script') {\n const script = document.createElement('script');\n script.src = url;\n script.async = async;\n if (defer !== undefined) {\n script.defer = defer;\n }\n if (crossOrigin) {\n script.crossOrigin = crossOrigin;\n }\n if (integrity) {\n script.integrity = integrity;\n }\n script.onload = () => resolve();\n script.onerror = () => reject(new Error(`Failed to load script: ${url}`));\n document.head.appendChild(script);\n } else if (type === 'style') {\n const link = document.createElement('link');\n link.rel = 'stylesheet';\n link.href = url;\n if (crossOrigin) {\n link.crossOrigin = crossOrigin;\n }\n if (integrity) {\n link.integrity = integrity;\n }\n link.onload = () => resolve();\n link.onerror = () => reject(new Error(`Failed to load stylesheet: ${url}`));\n document.head.appendChild(link);\n } else {\n reject(new Error(`Unsupported resource type: ${type}`));\n }\n });\n }\n\n /**\n * 获取资源 as 属性值\n */\n private getResourceAs(type: ResourceType): string {\n const asMap: Record<ResourceType, string> = {\n script: 'script',\n style: 'style',\n image: 'image',\n font: 'font',\n prefetch: 'fetch',\n preload: 'fetch',\n };\n return asMap[type] || 'fetch';\n }\n\n /**\n * 使用 requestIdleCallback 加载低优先级资源\n */\n loadWhenIdle(options: ResourceLoadOptions, timeout?: number): void {\n if (typeof requestIdleCallback !== 'undefined') {\n requestIdleCallback(\n () => {\n this.loadResource(options).catch((error) => {\n console.warn('Idle resource load failed:', error);\n });\n },\n { timeout }\n );\n } else {\n // 降级方案:延迟加载\n setTimeout(() => {\n this.loadResource(options).catch((error) => {\n console.warn('Delayed resource load failed:', error);\n });\n }, 1000);\n }\n }\n\n /**\n * 批量加载资源(按优先级)\n */\n async loadResources(resources: ResourceLoadOptions[]): Promise<void[]> {\n // 按优先级排序\n const sortedResources = this.sortByPriority(resources);\n \n // 按优先级分组\n const highPriority = sortedResources.filter(r => r.priority === ResourcePriority.HIGH || !r.priority);\n const mediumPriority = sortedResources.filter(r => r.priority === ResourcePriority.MEDIUM);\n const lowPriority = sortedResources.filter(r => r.priority === ResourcePriority.LOW);\n\n const results: void[] = [];\n\n // 先加载高优先级资源(并行,但限制并发数)\n if (highPriority.length > 0) {\n const highResults = await this.loadResourcesWithConcurrencyLimit(highPriority);\n results.push(...highResults);\n }\n\n // 然后加载中优先级资源\n if (mediumPriority.length > 0) {\n const mediumResults = await this.loadResourcesWithConcurrencyLimit(mediumPriority);\n results.push(...mediumResults);\n }\n\n // 最后加载低优先级资源(使用空闲时间)\n if (lowPriority.length > 0) {\n // 低优先级资源在空闲时加载\n for (const resource of lowPriority) {\n this.loadWhenIdle(resource);\n }\n }\n\n return results;\n }\n\n /**\n * 按优先级排序资源\n */\n private sortByPriority(resources: ResourceLoadOptions[]): ResourceLoadOptions[] {\n const priorityOrder = {\n [ResourcePriority.HIGH]: 0,\n [ResourcePriority.MEDIUM]: 1,\n [ResourcePriority.LOW]: 2,\n [ResourcePriority.AUTO]: 3,\n };\n\n return [...resources].sort((a, b) => {\n const aPriority = priorityOrder[a.priority || ResourcePriority.AUTO] ?? 3;\n const bPriority = priorityOrder[b.priority || ResourcePriority.AUTO] ?? 3;\n return aPriority - bPriority;\n });\n }\n\n /**\n * 限制并发数的资源加载(优化版本)\n */\n private async loadResourcesWithConcurrencyLimit(\n resources: ResourceLoadOptions[],\n concurrency?: number\n ): Promise<void[]> {\n const maxConcurrency = concurrency ?? this.getDynamicConcurrency();\n const results: Promise<void>[] = [];\n const executing = new Set<Promise<void>>();\n\n for (const resource of resources) {\n // 如果达到并发限制,等待一个任务完成\n if (executing.size >= maxConcurrency) {\n await Promise.race(executing);\n }\n\n const promise = this.loadResource(resource).finally(() => {\n executing.delete(promise);\n });\n\n executing.add(promise);\n results.push(promise);\n }\n\n // 等待所有任务完成\n await Promise.all(results);\n return [];\n }\n\n /**\n * 检查资源是否已加载\n */\n isLoaded(url: string): boolean {\n return this.loadedResources.has(url);\n }\n}\n\n/**\n * 默认资源加载器实例\n */\nexport const resourceLoader = new ResourceLoader();\n"],"names":["ResourcePriority","ResourceLoader","getDynamicConcurrency","navigator","maxConcurrentLoads","connection","effectiveType","loadScript","options","url","rest","loadResource","type","loadStyle","preload","document","link","createElement","rel","href","as","getResourceAs","priority","setAttribute","crossOrigin","integrity","head","appendChild","prefetch","onLoad","onError","loadedResources","has","loadingResources","get","loadPromise","doLoadResource","set","add","error","err","Error","String","delete","Promise","resolve","reject","async","defer","script","src","undefined","onload","onerror","asMap","style","image","font","loadWhenIdle","timeout","requestIdleCallback","catch","console","warn","setTimeout","loadResources","resources","sortedResources","sortByPriority","highPriority","filter","r","mediumPriority","lowPriority","results","length","highResults","loadResourcesWithConcurrencyLimit","push","mediumResults","resource","priorityOrder","sort","a","b","aPriority","bPriority","concurrency","maxConcurrency","executing","Set","size","race","promise","finally","all","isLoaded","Map","resourceLoader"],"mappings":"AAAA;;CAEC;;;;;;;;;;;;;AAGD;;CAEC,GACD,OAAO,IAAA,AAAKA,0CAAAA;IACV;;GAEC;IAED;;GAEC;IAED;;GAEC;IAED;;GAEC;WAfSA;MAiBX;AA4CD;;CAEC,GACD,OAAO,MAAMC;IAKX;;GAEC,GACD,AAAQC,wBAAgC;QACtC,IAAI,OAAOC,cAAc,eAAe,CAAE,CAAA,gBAAgBA,SAAQ,GAAI;YACpE,OAAO,IAAI,CAACC,kBAAkB;QAChC;QAEA,MAAMC,aAAa,AAACF,UAAkBE,UAAU;QAChD,IAAI,CAACA,YAAY;YACf,OAAO,IAAI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,EAAyB;QAC9E,MAAMe,gBAAgB;YACpB,QAAuB,EAAE;YACzB,UAAyB,EAAE;YAC3B,OAAsB,EAAE;YACxB,QAAuB,EAAE;QAC3B;QAEA,OAAO;eAAIf;SAAU,CAACgB,IAAI,CAAC,CAACC,GAAGC;YAC7B,MAAMC,YAAYJ,aAAa,CAACE,EAAE7D,QAAQ,WAA0B,IAAI;YACxE,MAAMgE,YAAYL,aAAa,CAACG,EAAE9D,QAAQ,WAA0B,IAAI;YACxE,OAAO+D,YAAYC;QACrB;IACF;IAEA;;GAEC,GACD,MAAcT,kCACZX,SAAgC,EAChCqB,WAAoB,EACH;QACjB,MAAMC,iBAAiBD,eAAe,IAAI,CAACrF,qBAAqB;QAChE,MAAMwE,UAA2B,EAAE;QACnC,MAAMe,YAAY,IAAIC;QAEtB,KAAK,MAAMV,YAAYd,UAAW;YAChC,oBAAoB;YACpB,IAAIuB,UAAUE,IAAI,IAAIH,gBAAgB;gBACpC,MAAM5C,QAAQgD,IAAI,CAACH;YACrB;YAEA,MAAMI,UAAU,IAAI,CAAClF,YAAY,CAACqE,UAAUc,OAAO,CAAC;gBAClDL,UAAU9C,MAAM,CAACkD;YACnB;YAEAJ,UAAUnD,GAAG,CAACuD;YACdnB,QAAQI,IAAI,CAACe;QACf;QAEA,WAAW;QACX,MAAMjD,QAAQmD,GAAG,CAACrB;QAClB,OAAO,EAAE;IACX;IAEA;;GAEC,GACDsB,SAASvF,GAAW,EAAW;QAC7B,OAAO,IAAI,CAACsB,eAAe,CAACC,GAAG,CAACvB;IAClC;;QAnTA,uBAAQsB,mBAA+B,IAAI2D;QAC3C,uBAAQzD,oBAA+C,IAAIgE;QAC3D,uBAAQ7F,sBAA6B,IAAG,eAAe;;AAkTzD;AAEA;;CAEC,GACD,OAAO,MAAM8F,iBAAiB,IAAIjG,iBAAiB"}
|
|
1
|
+
{"version":3,"sources":["../../src/utils/resourceLoader.ts"],"sourcesContent":["export { ResourceLoader, ResourcePriority, resourceLoader } from '@vlian/utils';\nexport type { ResourceLoadOptions, ResourceType } from '@vlian/utils';\n"],"names":["ResourceLoader","ResourcePriority","resourceLoader"],"mappings":"AAAA,SAASA,cAAc,EAAEC,gBAAgB,EAAEC,cAAc,QAAQ,eAAe"}
|
|
@@ -5,147 +5,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
Object.defineProperty(exports, "RuntimeSecurity", {
|
|
6
6
|
enumerable: true,
|
|
7
7
|
get: function() {
|
|
8
|
-
return RuntimeSecurity;
|
|
8
|
+
return _utils.RuntimeSecurity;
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
|
-
const
|
|
12
|
-
let RuntimeSecurity = class RuntimeSecurity {
|
|
13
|
-
/**
|
|
14
|
-
* 应用 CSP
|
|
15
|
-
*
|
|
16
|
-
* 注意:通过 meta 标签设置 CSP 不如 HTTP 头安全,建议在服务器端设置 CSP 头。
|
|
17
|
-
* 此方法仅作为补充措施。
|
|
18
|
-
*/ static applyCSP(config) {
|
|
19
|
-
if (typeof document === 'undefined') {
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
try {
|
|
23
|
-
const cspHeader = _security.SecurityUtils.generateCSP(config);
|
|
24
|
-
// 创建或更新 meta 标签
|
|
25
|
-
let metaCSP = document.querySelector('meta[http-equiv="Content-Security-Policy"]');
|
|
26
|
-
if (!metaCSP) {
|
|
27
|
-
metaCSP = document.createElement('meta');
|
|
28
|
-
metaCSP.setAttribute('http-equiv', 'Content-Security-Policy');
|
|
29
|
-
// 插入到 head 的开头,确保优先级
|
|
30
|
-
document.head.insertBefore(metaCSP, document.head.firstChild);
|
|
31
|
-
}
|
|
32
|
-
metaCSP.setAttribute('content', cspHeader);
|
|
33
|
-
// 添加 CSP 违规报告(如果支持)
|
|
34
|
-
if (typeof window !== 'undefined' && 'reporting' in window) {
|
|
35
|
-
// 可以添加 CSP 违规监听器
|
|
36
|
-
window.addEventListener('securitypolicyviolation', (event)=>{
|
|
37
|
-
console.warn('CSP 违规:', {
|
|
38
|
-
violatedDirective: event.violatedDirective,
|
|
39
|
-
blockedURI: event.blockedURI
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
} catch (error) {
|
|
44
|
-
console.error('应用 CSP 失败:', error);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* 应用安全头(通过 meta 标签,实际应该由服务器设置)
|
|
49
|
-
*/ static applySecurityHeaders(config) {
|
|
50
|
-
if (typeof document === 'undefined') {
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
// X-Frame-Options
|
|
54
|
-
if (config.xFrameOptions) {
|
|
55
|
-
let meta = document.querySelector('meta[http-equiv="X-Frame-Options"]');
|
|
56
|
-
if (!meta) {
|
|
57
|
-
meta = document.createElement('meta');
|
|
58
|
-
meta.setAttribute('http-equiv', 'X-Frame-Options');
|
|
59
|
-
document.head.appendChild(meta);
|
|
60
|
-
}
|
|
61
|
-
meta.setAttribute('content', config.xFrameOptions);
|
|
62
|
-
}
|
|
63
|
-
// X-Content-Type-Options
|
|
64
|
-
if (config.xContentTypeOptions !== false) {
|
|
65
|
-
let meta = document.querySelector('meta[http-equiv="X-Content-Type-Options"]');
|
|
66
|
-
if (!meta) {
|
|
67
|
-
meta = document.createElement('meta');
|
|
68
|
-
meta.setAttribute('http-equiv', 'X-Content-Type-Options');
|
|
69
|
-
document.head.appendChild(meta);
|
|
70
|
-
}
|
|
71
|
-
meta.setAttribute('content', 'nosniff');
|
|
72
|
-
}
|
|
73
|
-
// X-XSS-Protection
|
|
74
|
-
if (config.xXSSProtection !== false) {
|
|
75
|
-
let meta = document.querySelector('meta[http-equiv="X-XSS-Protection"]');
|
|
76
|
-
if (!meta) {
|
|
77
|
-
meta = document.createElement('meta');
|
|
78
|
-
meta.setAttribute('http-equiv', 'X-XSS-Protection');
|
|
79
|
-
document.head.appendChild(meta);
|
|
80
|
-
}
|
|
81
|
-
meta.setAttribute('content', '1; mode=block');
|
|
82
|
-
}
|
|
83
|
-
// Referrer-Policy
|
|
84
|
-
if (config.referrerPolicy) {
|
|
85
|
-
let meta = document.querySelector('meta[name="referrer"]');
|
|
86
|
-
if (!meta) {
|
|
87
|
-
meta = document.createElement('meta');
|
|
88
|
-
meta.setAttribute('name', 'referrer');
|
|
89
|
-
document.head.appendChild(meta);
|
|
90
|
-
}
|
|
91
|
-
meta.setAttribute('content', config.referrerPolicy);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* 检查运行时安全
|
|
96
|
-
*/ static checkRuntimeSecurity() {
|
|
97
|
-
const issues = [];
|
|
98
|
-
if (typeof window === 'undefined') {
|
|
99
|
-
return {
|
|
100
|
-
safe: true,
|
|
101
|
-
issues: []
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
// 检查是否在 HTTPS 环境下
|
|
105
|
-
if (window.location.protocol !== 'https:' && window.location.hostname !== 'localhost') {
|
|
106
|
-
issues.push('应用未在 HTTPS 环境下运行');
|
|
107
|
-
}
|
|
108
|
-
// 检查是否有危险的全局对象
|
|
109
|
-
if (window.eval) {
|
|
110
|
-
issues.push('检测到危险的 eval 函数');
|
|
111
|
-
}
|
|
112
|
-
// 检查是否有危险的 innerHTML 使用
|
|
113
|
-
// 这个检查需要在代码层面进行,这里只是示例
|
|
114
|
-
return {
|
|
115
|
-
safe: issues.length === 0,
|
|
116
|
-
issues
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* 初始化运行时安全
|
|
121
|
-
*/ static initialize(options = {}) {
|
|
122
|
-
const { enableCSP = false, enableSecurityHeaders = false, enableRuntimeChecks = false, csp, securityHeaders } = options;
|
|
123
|
-
// 应用 CSP
|
|
124
|
-
if (enableCSP && csp) {
|
|
125
|
-
this.applyCSP(csp);
|
|
126
|
-
}
|
|
127
|
-
// 应用安全头
|
|
128
|
-
if (enableSecurityHeaders && securityHeaders) {
|
|
129
|
-
this.applySecurityHeaders(securityHeaders);
|
|
130
|
-
}
|
|
131
|
-
// 运行时检查
|
|
132
|
-
if (enableRuntimeChecks) {
|
|
133
|
-
const checkResult = this.checkRuntimeSecurity();
|
|
134
|
-
if (!checkResult.safe) {
|
|
135
|
-
console.warn('运行时安全检查发现问题:', checkResult.issues);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* 验证 URL 安全性
|
|
141
|
-
*/ static validateUrl(url) {
|
|
142
|
-
return _security.SecurityUtils.isSafeUrl(url);
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* 验证并清理用户输入
|
|
146
|
-
*/ static sanitizeUserInput(input) {
|
|
147
|
-
return _security.SecurityUtils.validateInput(input);
|
|
148
|
-
}
|
|
149
|
-
};
|
|
11
|
+
const _utils = require("@vlian/utils");
|
|
150
12
|
|
|
151
13
|
//# sourceMappingURL=runtimeSecurity.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/runtimeSecurity.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"sources":["../../src/utils/runtimeSecurity.ts"],"sourcesContent":["export { RuntimeSecurity } from '@vlian/utils';\nexport type { RuntimeSecurityOptions, CSPConfig, SecurityHeadersConfig } from '@vlian/utils';\n"],"names":["RuntimeSecurity"],"mappings":";;;;+BAASA;;;eAAAA,sBAAe;;;uBAAQ"}
|
|
@@ -1,104 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*/
|
|
4
|
-
export interface CSPConfig {
|
|
5
|
-
defaultSrc?: string[];
|
|
6
|
-
scriptSrc?: string[];
|
|
7
|
-
styleSrc?: string[];
|
|
8
|
-
imgSrc?: string[];
|
|
9
|
-
connectSrc?: string[];
|
|
10
|
-
fontSrc?: string[];
|
|
11
|
-
frameSrc?: string[];
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* 安全头配置
|
|
15
|
-
*/
|
|
16
|
-
export interface SecurityHeadersConfig {
|
|
17
|
-
/**
|
|
18
|
-
* Content Security Policy
|
|
19
|
-
*/
|
|
20
|
-
csp?: CSPConfig | string;
|
|
21
|
-
/**
|
|
22
|
-
* X-Frame-Options
|
|
23
|
-
*/
|
|
24
|
-
xFrameOptions?: 'DENY' | 'SAMEORIGIN' | 'ALLOW-FROM';
|
|
25
|
-
/**
|
|
26
|
-
* X-Content-Type-Options
|
|
27
|
-
*/
|
|
28
|
-
xContentTypeOptions?: boolean;
|
|
29
|
-
/**
|
|
30
|
-
* X-XSS-Protection
|
|
31
|
-
*/
|
|
32
|
-
xXSSProtection?: boolean;
|
|
33
|
-
/**
|
|
34
|
-
* Referrer-Policy
|
|
35
|
-
*/
|
|
36
|
-
referrerPolicy?: 'no-referrer' | 'no-referrer-when-downgrade' | 'origin' | 'origin-when-cross-origin' | 'same-origin' | 'strict-origin' | 'strict-origin-when-cross-origin' | 'unsafe-url';
|
|
37
|
-
/**
|
|
38
|
-
* Permissions-Policy
|
|
39
|
-
*/
|
|
40
|
-
permissionsPolicy?: Record<string, string[]>;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* 运行时安全检查选项
|
|
44
|
-
*/
|
|
45
|
-
export interface RuntimeSecurityOptions {
|
|
46
|
-
/**
|
|
47
|
-
* 是否启用 CSP
|
|
48
|
-
*/
|
|
49
|
-
enableCSP?: boolean;
|
|
50
|
-
/**
|
|
51
|
-
* 是否启用安全头
|
|
52
|
-
*/
|
|
53
|
-
enableSecurityHeaders?: boolean;
|
|
54
|
-
/**
|
|
55
|
-
* 是否启用运行时检查
|
|
56
|
-
*/
|
|
57
|
-
enableRuntimeChecks?: boolean;
|
|
58
|
-
/**
|
|
59
|
-
* CSP 配置
|
|
60
|
-
*/
|
|
61
|
-
csp?: CSPConfig;
|
|
62
|
-
/**
|
|
63
|
-
* 安全头配置
|
|
64
|
-
*/
|
|
65
|
-
securityHeaders?: SecurityHeadersConfig;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* 运行时安全工具类
|
|
69
|
-
*/
|
|
70
|
-
export declare class RuntimeSecurity {
|
|
71
|
-
/**
|
|
72
|
-
* 应用 CSP
|
|
73
|
-
*
|
|
74
|
-
* 注意:通过 meta 标签设置 CSP 不如 HTTP 头安全,建议在服务器端设置 CSP 头。
|
|
75
|
-
* 此方法仅作为补充措施。
|
|
76
|
-
*/
|
|
77
|
-
static applyCSP(config: CSPConfig): void;
|
|
78
|
-
/**
|
|
79
|
-
* 应用安全头(通过 meta 标签,实际应该由服务器设置)
|
|
80
|
-
*/
|
|
81
|
-
static applySecurityHeaders(config: SecurityHeadersConfig): void;
|
|
82
|
-
/**
|
|
83
|
-
* 检查运行时安全
|
|
84
|
-
*/
|
|
85
|
-
static checkRuntimeSecurity(): {
|
|
86
|
-
safe: boolean;
|
|
87
|
-
issues: string[];
|
|
88
|
-
};
|
|
89
|
-
/**
|
|
90
|
-
* 初始化运行时安全
|
|
91
|
-
*/
|
|
92
|
-
static initialize(options?: RuntimeSecurityOptions): void;
|
|
93
|
-
/**
|
|
94
|
-
* 验证 URL 安全性
|
|
95
|
-
*/
|
|
96
|
-
static validateUrl(url: string): boolean;
|
|
97
|
-
/**
|
|
98
|
-
* 验证并清理用户输入
|
|
99
|
-
*/
|
|
100
|
-
static sanitizeUserInput(input: unknown): {
|
|
101
|
-
safe: boolean;
|
|
102
|
-
sanitized?: string;
|
|
103
|
-
};
|
|
104
|
-
}
|
|
1
|
+
export { RuntimeSecurity } from '@vlian/utils';
|
|
2
|
+
export type { RuntimeSecurityOptions, CSPConfig, SecurityHeadersConfig } from '@vlian/utils';
|