soma-core 2.2.5 → 2.2.6
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/README.md +5 -6
- package/bin/soma.cjs +2 -2
- package/core/build/compiler/flat-page.ts +16 -0
- package/core/build/compiler/full-manifest-builder.ts +0 -3
- package/core/build/compiler/page-assets.ts +0 -1
- package/core/build/compiler/page-closure.ts +245 -0
- package/core/build/compiler/page-declaration-compiler.ts +2 -5
- package/core/build/compiler/soma-precompile.js +1848 -3244
- package/core/build/compiler/soma-precompile.ts +4 -7
- package/core/build/component-tree/artifact-codec.ts +4 -8
- package/core/build/lsp/soma-declaration.cjs +1 -1
- package/core/build/lsp/std-completion.generated.json +3 -3
- package/core/client/a11y/a11y-config.ts +2 -1
- package/core/client/a11y/dom-id.ts +1 -1
- package/core/client/base/SomaHttpClient.ts +11 -3
- package/core/client/base/SomaPageRootBase.ts +6 -0
- package/core/client/base/defineSomaView.ts +178 -0
- package/core/client/base/index.ts +13 -0
- package/core/client/base/view-context.ts +328 -0
- package/core/client/behavior/adapter-collection.ts +2 -2
- package/core/client/compiler/manifest-compiler.ts +5 -36
- package/core/client/component/component-definition.types.ts +2 -1
- package/core/client/component/component-manager.ts +110 -20
- package/core/client/component/structure-adapter.ts +3 -7
- package/core/client/container/SomaContainerAdapter.ts +9 -1
- package/core/client/data/data-object-resolver.ts +516 -181
- package/core/client/data/data-write-actions.ts +79 -15
- package/core/client/data/format-config.ts +3 -1
- package/core/client/data/region-scope-port.ts +11 -0
- package/core/client/engine/asset-resolver.ts +2 -5
- package/core/client/engine/dom-renderer.ts +7 -40
- package/core/client/engine/event-engine.ts +1 -105
- package/core/client/engine/property-watcher.ts +1 -1
- package/core/client/engine/reactive-proxy.ts +78 -17
- package/core/client/engine/runtime-builder.ts +0 -14
- package/core/client/engine/scope-resolver.ts +18 -0
- package/core/client/engine/soma-bootstrap.ts +385 -98
- package/core/client/engine/style-binder.ts +49 -106
- package/core/client/engine/timer-engine.ts +11 -1
- package/core/client/host-bridge/load-host-domain-class.ts +2 -1
- package/core/client/infra/http/SomaHttpService.ts +15 -4
- package/core/client/infra/i18n/pack-locales.ts +2 -2
- package/core/client/mount/domain-rule.ts +7 -3
- package/core/client/mount/mount-page.ts +38 -5
- package/core/client/mount/types.ts +2 -0
- package/core/client/parser/confirm-spec.ts +1 -1
- package/core/client/parser/data-config-parser.ts +27 -210
- package/core/client/parser/pipeline-parser.ts +14 -59
- package/core/client/parser/style-config-parser.ts +101 -95
- package/core/client/parser/timer-parser.ts +21 -0
- package/core/client/parser/ui-config-parser.ts +64 -12
- package/core/client/registry/declaration-renderer.ts +1 -1
- package/core/client/runtime/SomaRuntimeServices.ts +2 -1
- package/core/client/runtime/SomaSnapshotStore.ts +5 -0
- package/core/client/security/url-guard.ts +71 -11
- package/core/client/validation/async-check-registry.ts +2 -1
- package/core/client/validation/contract-validator.ts +2 -6
- package/core/client/validation/field-rules.ts +3 -13
- package/core/client/validation/object-rules.ts +2 -10
- package/core/index.ts +1 -35
- package/core/inspect/check-product.ts +5 -5
- package/core/inspect/runtime.ts +2 -1
- package/core/server/api/dispatch-service-api.ts +35 -36
- package/core/server/api/load-host-service.ts +5 -4
- package/core/server/compile-port.ts +1 -1
- package/core/server/host/app-config-composer.ts +2 -1
- package/core/server/host/host-assembly-entry.ts +1 -0
- package/core/server/host/host-assembly.ts +47 -3
- package/core/server/host/host-caller.ts +2 -1
- package/core/server/host/host-dir.ts +9 -0
- package/core/server/host/host-response-headers.ts +2 -1
- package/core/server/host/invoke-host-request-hook.ts +28 -4
- package/core/server/host/published-page-assets.ts +2 -1
- package/core/server/host/resolve-host-request-hook.ts +25 -7
- package/core/server/host/server-page-assets.ts +4 -4
- package/core/server/host/virtual-pages.ts +3 -2
- package/core/server/infra/cache/ServerFileCache.ts +79 -9
- package/core/server/registry/api-registry-store.ts +3 -1
- package/core/server/registry/declaration-precompiler.ts +0 -2
- package/core/server/registry/page-registry-repository.ts +2 -1
- package/core/server/registry/page-registry-store.ts +2 -1
- package/core/server/registry/registry-error.ts +6 -1
- package/core/server/registry/registry-file-lock.ts +296 -23
- package/core/server/registry/request-body-limit.ts +3 -2
- package/core/server/registry/validation/declaration-safety-scan.ts +151 -27
- package/core/server/runtime-space/access-gate.ts +21 -7
- package/core/server/runtime-space/bindings.ts +3 -6
- package/core/server/runtime-space/dynamic-pages.ts +181 -49
- package/core/server/runtime-space/errors.ts +3 -0
- package/core/server/runtime-space/inject.ts +74 -15
- package/core/server/runtime-space/page-grants.ts +7 -1
- package/core/server/runtime-space/resolve.ts +66 -19
- package/core/server/runtime-space/store.ts +32 -3
- package/core/server/runtime-space/submit-steps.ts +2 -1
- package/core/server/runtime-space/types.ts +3 -1
- package/core/shared/component-tree-errors.ts +2 -1
- package/core/shared/contract-a11y.ts +1 -1
- package/core/shared/contract-sections.ts +2 -2
- package/core/shared/core-diagnostic-codes.ts +8 -0
- package/core/shared/declaration-keys.ts +1 -1
- package/core/shared/engines/engine-error.ts +2 -1
- package/core/shared/generation-error.ts +2 -1
- package/core/shared/page-envelope.ts +1 -1
- package/core/shared/pure-binding.ts +88 -0
- package/core/shared/registry-generated/diagnostic-registry-index.ts +16 -19
- package/core/shared/registry-generated/diagnostic-registry.ts +135 -177
- package/core/shared/registry-generated/limits.ts +49 -13
- package/core/shared/registry-generated/std-grammar-completion.ts +0 -4
- package/core/shared/registry-generated/std-grammar-views.ts +40 -10
- package/core/shared/registry-generated/std-grammar.ts +42 -20
- package/core/shared/soma-error.ts +36 -0
- package/core/shared/types/compile-port.types.ts +1 -1
- package/core/shared/types/data-config.types.ts +17 -17
- package/core/shared/types/engine.types.ts +0 -3
- package/core/shared/types/event.types.ts +1 -11
- package/core/shared/types/generation-error.types.ts +5 -1
- package/core/shared/types/host-assembly.types.ts +7 -1
- package/core/shared/types/manifest.types.ts +0 -3
- package/core/shared/types/region-scope.types.ts +5 -1
- package/core/shared/types/scope-manifest.types.ts +0 -2
- package/core/shared/types/style-config.types.ts +21 -21
- package/core/shared/types/ui-config.types.ts +4 -5
- package/core/shared/types/watcher.types.ts +1 -1
- package/dist/browser/runtime-bundle.manifest.json +11 -13
- package/dist/browser/soma-runtime.js +2567 -2696
- package/dist/core/index.cjs +5446 -5486
- package/dist/host-native/aot-builder.cjs +46 -1
- package/dist/host-native/native-server.cjs +4879 -4505
- package/dist/lsp/component-tree.cjs +5814 -7042
- package/dist/types/build/compiler/full-manifest-builder.d.ts +0 -2
- package/dist/types/build/compiler/page-closure.d.ts +21 -0
- package/dist/types/build/compiler/page-declaration-compiler.d.ts +2 -4
- package/dist/types/build/compiler/soma-precompile.d.ts +1 -3
- package/dist/types/build/component-tree/artifact-codec.d.ts +0 -2
- package/dist/types/client/a11y/a11y-config.d.ts +2 -1
- package/dist/types/client/base/SomaHttpClient.d.ts +1 -1
- package/dist/types/client/base/SomaPageRootBase.d.ts +5 -0
- package/dist/types/client/base/defineSomaView.d.ts +46 -0
- package/dist/types/client/base/index.d.ts +2 -0
- package/dist/types/client/base/view-context.d.ts +136 -0
- package/dist/types/client/behavior/adapter-collection.d.ts +1 -1
- package/dist/types/client/compiler/manifest-compiler.d.ts +0 -3
- package/dist/types/client/component/component-definition.types.d.ts +2 -1
- package/dist/types/client/component/component-manager.d.ts +29 -0
- package/dist/types/client/data/data-object-resolver.d.ts +91 -31
- package/dist/types/client/data/format-config.d.ts +2 -1
- package/dist/types/client/engine/dom-renderer.d.ts +1 -2
- package/dist/types/client/engine/event-engine.d.ts +0 -7
- package/dist/types/client/engine/reactive-proxy.d.ts +11 -4
- package/dist/types/client/engine/runtime-builder.d.ts +0 -4
- package/dist/types/client/engine/soma-bootstrap.d.ts +44 -6
- package/dist/types/client/engine/style-binder.d.ts +8 -4
- package/dist/types/client/host-bridge/load-host-domain-class.d.ts +2 -1
- package/dist/types/client/mount/domain-rule.d.ts +2 -0
- package/dist/types/client/mount/mount-page.d.ts +2 -1
- package/dist/types/client/mount/types.d.ts +2 -0
- package/dist/types/client/parser/data-config-parser.d.ts +6 -5
- package/dist/types/client/parser/style-config-parser.d.ts +7 -3
- package/dist/types/client/parser/timer-parser.d.ts +5 -0
- package/dist/types/client/parser/ui-config-parser.d.ts +6 -2
- package/dist/types/client/runtime/SomaSnapshotStore.d.ts +2 -0
- package/dist/types/client/security/url-guard.d.ts +14 -0
- package/dist/types/client/validation/async-check-registry.d.ts +2 -1
- package/dist/types/client/validation/object-rules.d.ts +0 -5
- package/dist/types/index.d.ts +2 -7
- package/dist/types/inspect/runtime.d.ts +2 -1
- package/dist/types/server/api/dispatch-service-api.d.ts +5 -0
- package/dist/types/server/api/load-host-service.d.ts +1 -1
- package/dist/types/server/compile-port.d.ts +1 -1
- package/dist/types/server/host/host-assembly-entry.d.ts +1 -1
- package/dist/types/server/host/host-assembly.d.ts +2 -0
- package/dist/types/server/host/host-dir.d.ts +2 -0
- package/dist/types/server/host/invoke-host-request-hook.d.ts +10 -3
- package/dist/types/server/host/published-page-assets.d.ts +2 -1
- package/dist/types/server/host/resolve-host-request-hook.d.ts +18 -5
- package/dist/types/server/host/server-page-assets.d.ts +2 -1
- package/dist/types/server/infra/cache/ServerFileCache.d.ts +8 -0
- package/dist/types/server/registry/declaration-precompiler.d.ts +0 -1
- package/dist/types/server/registry/registry-error.d.ts +5 -1
- package/dist/types/server/registry/registry-file-lock.d.ts +30 -0
- package/dist/types/server/registry/request-body-limit.d.ts +3 -2
- package/dist/types/server/registry/validation/declaration-safety-scan.d.ts +3 -0
- package/dist/types/server/runtime-space/access-gate.d.ts +7 -0
- package/dist/types/server/runtime-space/bindings.d.ts +3 -2
- package/dist/types/server/runtime-space/dynamic-pages.d.ts +6 -1
- package/dist/types/server/runtime-space/errors.d.ts +1 -1
- package/dist/types/server/runtime-space/resolve.d.ts +2 -0
- package/dist/types/server/runtime-space/store.d.ts +8 -1
- package/dist/types/server/runtime-space/submit-steps.d.ts +2 -1
- package/dist/types/server/runtime-space/types.d.ts +3 -1
- package/dist/types/shared/component-tree-errors.d.ts +2 -1
- package/dist/types/shared/core-diagnostic-codes.d.ts +1 -1
- package/dist/types/shared/declaration-keys.d.ts +1 -1
- package/dist/types/shared/engines/engine-error.d.ts +2 -1
- package/dist/types/shared/generation-error.d.ts +2 -1
- package/dist/types/shared/pure-binding.d.ts +48 -0
- package/dist/types/shared/registry-generated/diagnostic-registry.d.ts +119 -158
- package/dist/types/shared/registry-generated/limits.d.ts +40 -10
- package/dist/types/shared/registry-generated/std-grammar-completion.d.ts +0 -30
- package/dist/types/shared/registry-generated/std-grammar-views.d.ts +97 -23
- package/dist/types/shared/registry-generated/std-grammar.d.ts +109 -241
- package/dist/types/shared/soma-error.d.ts +15 -0
- package/dist/types/shared/types/data-config.types.d.ts +14 -16
- package/dist/types/shared/types/engine.types.d.ts +0 -3
- package/dist/types/shared/types/event.types.d.ts +1 -7
- package/dist/types/shared/types/generation-error.types.d.ts +1 -1
- package/dist/types/shared/types/host-assembly.types.d.ts +2 -0
- package/dist/types/shared/types/manifest.types.d.ts +0 -3
- package/dist/types/shared/types/region-scope.types.d.ts +4 -1
- package/dist/types/shared/types/scope-manifest.types.d.ts +0 -2
- package/dist/types/shared/types/style-config.types.d.ts +17 -19
- package/dist/types/shared/types/ui-config.types.d.ts +4 -5
- package/host-native/bin/host-native.cjs +22 -1
- package/host-native/client/browser-entry.ts +21 -172
- package/host-native/src/aot-builder.ts +52 -0
- package/host-native/src/host-prepare.ts +39 -5
- package/host-native/src/manifest-http.ts +2 -3
- package/host-native/src/native-server.ts +812 -174
- package/host-native/src/page-http.ts +3 -3
- package/host-native/src/public-packages.cjs +0 -1
- package/host-native/src/registry-handler.ts +12 -5
- package/host-native/src/static-file.ts +203 -53
- package/package.json +3 -2
- package/core/client/engine/step-runner.ts +0 -212
- package/core/client/engine/trigger-engine.ts +0 -55
- package/core/client/format/zoned.ts +0 -10
- package/core/client/parser/step-parser.ts +0 -359
- package/core/client/parser/trigger-parser.ts +0 -90
- package/core/client/parser/value-lang.ts +0 -340
- package/core/client/time/instant.ts +0 -2
- package/core/shared/calc/coerce.ts +0 -2
- package/core/shared/calc/errors.ts +0 -40
- package/core/shared/calc/evaluator.ts +0 -113
- package/core/shared/calc/index.ts +0 -11
- package/core/shared/calc/lexer.ts +0 -189
- package/core/shared/calc/parser.ts +0 -193
- package/core/shared/calc/typer.ts +0 -171
- package/core/shared/date-parts.ts +0 -2
- package/core/shared/show-when.ts +0 -77
- package/core/shared/types/step.types.ts +0 -91
- package/core/shared/types/trigger.types.ts +0 -14
- package/dist/types/client/engine/step-runner.d.ts +0 -42
- package/dist/types/client/engine/trigger-engine.d.ts +0 -16
- package/dist/types/client/format/zoned.d.ts +0 -1
- package/dist/types/client/parser/step-parser.d.ts +0 -23
- package/dist/types/client/parser/trigger-parser.d.ts +0 -13
- package/dist/types/client/parser/value-lang.d.ts +0 -63
- package/dist/types/client/time/instant.d.ts +0 -1
- package/dist/types/shared/calc/coerce.d.ts +0 -1
- package/dist/types/shared/calc/errors.d.ts +0 -14
- package/dist/types/shared/calc/evaluator.d.ts +0 -2
- package/dist/types/shared/calc/index.d.ts +0 -6
- package/dist/types/shared/calc/lexer.d.ts +0 -19
- package/dist/types/shared/calc/parser.d.ts +0 -43
- package/dist/types/shared/calc/typer.d.ts +0 -11
- package/dist/types/shared/date-parts.d.ts +0 -1
- package/dist/types/shared/show-when.d.ts +0 -21
- package/dist/types/shared/types/step.types.d.ts +0 -76
- package/dist/types/shared/types/trigger.types.d.ts +0 -12
|
@@ -116,8 +116,9 @@ export function pageSuccessResponse(
|
|
|
116
116
|
cacheKey?: string,
|
|
117
117
|
): Response {
|
|
118
118
|
const jsonType = 'application/json; charset=utf-8';
|
|
119
|
+
const isPrefix = (page as { binding?: { kind?: string } }).binding?.kind === 'prefix';
|
|
119
120
|
|
|
120
|
-
if (!page.cacheable || page.purpose === 'preview') {
|
|
121
|
+
if (!page.cacheable || page.purpose === 'preview' || isPrefix) {
|
|
121
122
|
const text = typeof body === 'string' ? body : JSON.stringify(body);
|
|
122
123
|
return new Response(text, {
|
|
123
124
|
status: 200,
|
|
@@ -215,9 +216,8 @@ export function pageErrorResponse(
|
|
|
215
216
|
{ status: error.status, headers },
|
|
216
217
|
);
|
|
217
218
|
}
|
|
218
|
-
const detail = error instanceof Error ? error.message : String(error);
|
|
219
219
|
return Response.json(
|
|
220
|
-
{ error:
|
|
220
|
+
{ error: 'Internal server error', code: 'SOMA_PAGE_ASSET_UNKNOWN' },
|
|
221
221
|
{ status: 500, headers },
|
|
222
222
|
);
|
|
223
223
|
}
|
|
@@ -118,7 +118,6 @@ function readAllSomaPublicAliases(hostDir, repoRoot, extraRoots = []) {
|
|
|
118
118
|
path.join(repoRoot, 'soma-core'),
|
|
119
119
|
path.join(repoRoot, 'soma-foundation'),
|
|
120
120
|
path.join(repoRoot, 'soma-ui'),
|
|
121
|
-
path.join(repoRoot, 'soma-lite'),
|
|
122
121
|
path.join(repoRoot, 'soma-kit'),
|
|
123
122
|
path.join(repoRoot, 'soma-ai'),
|
|
124
123
|
];
|
|
@@ -17,11 +17,17 @@ function createHandler(method: HttpMethod) {
|
|
|
17
17
|
const segments = resolvedParams.path || [];
|
|
18
18
|
const url = new URL(req.url, 'http://soma.local');
|
|
19
19
|
const pathname = req.nextUrl?.pathname || url.pathname;
|
|
20
|
+
const hostDir = req.hostDir ?? process.env.SOMA_HOST_DIR;
|
|
20
21
|
|
|
21
|
-
const rejected = await invokeHostRequestHook(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
const rejected = await invokeHostRequestHook(
|
|
23
|
+
req,
|
|
24
|
+
{
|
|
25
|
+
kind: 'api',
|
|
26
|
+
path: pathname,
|
|
27
|
+
},
|
|
28
|
+
undefined,
|
|
29
|
+
{ hostDir },
|
|
30
|
+
);
|
|
25
31
|
if (rejected) {
|
|
26
32
|
return rejected;
|
|
27
33
|
}
|
|
@@ -63,10 +69,11 @@ function createHandler(method: HttpMethod) {
|
|
|
63
69
|
): Promise<Response> {
|
|
64
70
|
const url = new URL(req.url, 'http://soma.local');
|
|
65
71
|
const pathname = req.nextUrl?.pathname || url.pathname;
|
|
72
|
+
const hostDir = req.hostDir ?? process.env.SOMA_HOST_DIR;
|
|
66
73
|
return withHostResponseHeaders(await dispatch(req, context), {
|
|
67
74
|
method: req.method || method,
|
|
68
75
|
pathname,
|
|
69
|
-
});
|
|
76
|
+
}, hostDir);
|
|
70
77
|
};
|
|
71
78
|
}
|
|
72
79
|
|
|
@@ -2,43 +2,79 @@ import fs from 'node:fs';
|
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { createHash } from 'node:crypto';
|
|
4
4
|
import zlib from 'node:zlib';
|
|
5
|
+
import { Readable } from 'node:stream';
|
|
6
|
+
import { createRequire } from 'node:module';
|
|
5
7
|
import { resolveSomaRuntimeLayout } from '../../core/server/runtime-space/root';
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
'
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
'.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const
|
|
9
|
+
// esbuild format:'cjs' 产物里 import.meta.url 为 undefined,此时回落到原生 require。
|
|
10
|
+
const mimeTypes = (typeof import.meta.url === 'string'
|
|
11
|
+
? createRequire(import.meta.url)('mime-types')
|
|
12
|
+
: require('mime-types')) as { lookup(extension: string): string | false };
|
|
13
|
+
|
|
14
|
+
/** 仅构建输出目录中的内容寻址产物可 immutable(esbuild `[hash]` 为 8 位大写字母数字,区分大小写);public 与可配置 runtime 目录一律保守缓存。 */
|
|
15
|
+
const HASHED_NAME = /-[A-Z0-9]{8}\.(?:js|css)(?:\.map)?$/;
|
|
16
|
+
|
|
17
|
+
const EXECUTABLE_DOC_EXTENSIONS: ReadonlySet<string> = new Set([
|
|
18
|
+
'.html', '.htm', '.xhtml', '.xht', '.shtml', '.mht', '.mhtml',
|
|
19
|
+
]);
|
|
20
|
+
const TYPESCRIPT_EXTENSIONS: ReadonlySet<string> = new Set(['.ts', '.tsx', '.mts', '.cts']);
|
|
21
|
+
|
|
22
|
+
export function contentTypeForExtension(extension: string): string {
|
|
23
|
+
// public/ 只承诺图片与字体等静态素材;避免把 HTML 从下载型变成可执行文档。
|
|
24
|
+
if (EXECUTABLE_DOC_EXTENSIONS.has(extension)) return 'application/octet-stream';
|
|
25
|
+
// mime-types 会把 .ts 当成视频(MPEG-TS),源码文件固定为纯文本。
|
|
26
|
+
if (TYPESCRIPT_EXTENSIONS.has(extension)) return 'text/plain; charset=utf-8';
|
|
27
|
+
const lookedUp = mimeTypes.lookup(extension);
|
|
28
|
+
if (!lookedUp) return 'application/octet-stream';
|
|
29
|
+
const normalized = extension === '.ico'
|
|
30
|
+
? 'image/x-icon'
|
|
31
|
+
: lookedUp === 'application/javascript'
|
|
32
|
+
? 'text/javascript'
|
|
33
|
+
: lookedUp;
|
|
34
|
+
return normalized.startsWith('text/') || normalized === 'application/json' || normalized === 'text/javascript'
|
|
35
|
+
? `${normalized}; charset=utf-8`
|
|
36
|
+
: normalized;
|
|
37
|
+
}
|
|
26
38
|
|
|
27
39
|
export type StaticResolution =
|
|
28
40
|
| { readonly ok: true; readonly file: string; readonly size: number; readonly mtimeMs: number }
|
|
29
41
|
| { readonly ok: false };
|
|
30
42
|
|
|
31
43
|
export function isSafeSegments(segments: readonly string[]): boolean {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
44
|
+
if (segments.length === 0) return false;
|
|
45
|
+
return segments.every((segment) => {
|
|
46
|
+
if (
|
|
47
|
+
segment === '' ||
|
|
48
|
+
segment.startsWith('.') ||
|
|
49
|
+
/[\\/]/.test(segment) ||
|
|
50
|
+
/[\x00-\x1f]/.test(segment)
|
|
51
|
+
) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
const decoded = decodeURIComponent(segment);
|
|
56
|
+
if (
|
|
57
|
+
decoded === '' ||
|
|
58
|
+
decoded.startsWith('.') ||
|
|
59
|
+
/[\\/]/.test(decoded) ||
|
|
60
|
+
/[\x00-\x1f]/.test(decoded)
|
|
61
|
+
) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
} catch {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
return true;
|
|
68
|
+
});
|
|
35
69
|
}
|
|
36
70
|
|
|
37
71
|
export function resolveStaticFile(staticRoot: string, segments: readonly string[]): StaticResolution {
|
|
38
72
|
if (!isSafeSegments(segments)) return { ok: false };
|
|
39
73
|
try {
|
|
40
74
|
const rootReal = fs.realpathSync(staticRoot);
|
|
41
|
-
const candidate = path.join(rootReal, ...segments)
|
|
75
|
+
const candidate = path.join(rootReal, ...segments.map((s) => {
|
|
76
|
+
try { return decodeURIComponent(s); } catch { return s; }
|
|
77
|
+
}));
|
|
42
78
|
const real = fs.realpathSync(candidate);
|
|
43
79
|
if (real !== rootReal && !real.startsWith(rootReal + path.sep)) return { ok: false };
|
|
44
80
|
const stat = fs.statSync(real);
|
|
@@ -69,6 +105,11 @@ export function clearStaticBufferCache(): void {
|
|
|
69
105
|
currentStaticCacheBytes = 0;
|
|
70
106
|
}
|
|
71
107
|
|
|
108
|
+
/** 该大小的静态文件是否存在 gzip 表示:只看大小(≤单文件缓存上限且 ≥1KB),不读文件;缓存函数与响应头(Vary)共用这一组条件 */
|
|
109
|
+
function hasGzipRepresentation(size: number): boolean {
|
|
110
|
+
return size <= MAX_SINGLE_FILE_BYTES && size >= 1024;
|
|
111
|
+
}
|
|
112
|
+
|
|
72
113
|
function getOrCacheStaticFile(filePath: string, size: number, mtimeMs: number): CachedStaticPayload | null {
|
|
73
114
|
if (size > MAX_SINGLE_FILE_BYTES) return null;
|
|
74
115
|
const key = `${filePath}:${size}:${mtimeMs}`;
|
|
@@ -84,7 +125,7 @@ function getOrCacheStaticFile(filePath: string, size: number, mtimeMs: number):
|
|
|
84
125
|
const buffer = fs.readFileSync(filePath);
|
|
85
126
|
const raw = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
86
127
|
let gzip: Uint8Array | null = null;
|
|
87
|
-
if (raw.byteLength
|
|
128
|
+
if (hasGzipRepresentation(raw.byteLength)) {
|
|
88
129
|
try {
|
|
89
130
|
gzip = zlib.gzipSync(raw);
|
|
90
131
|
} catch {
|
|
@@ -111,58 +152,141 @@ function getOrCacheStaticFile(filePath: string, size: number, mtimeMs: number):
|
|
|
111
152
|
}
|
|
112
153
|
}
|
|
113
154
|
|
|
114
|
-
|
|
155
|
+
/** 按逗号拆分并解析 q 值:gzip(或 `*`)且 q>0 才算接受;q=0 明确拒绝,显式 gzip 优先于 `*`。 */
|
|
156
|
+
export function acceptsGzip(header: string | null | undefined): boolean {
|
|
115
157
|
if (!header) return false;
|
|
116
|
-
|
|
158
|
+
let gzip: number | null = null;
|
|
159
|
+
let star: number | null = null;
|
|
160
|
+
for (const part of header.split(',')) {
|
|
161
|
+
const [rawName, ...params] = part.split(';');
|
|
162
|
+
const name = rawName.trim().toLowerCase();
|
|
163
|
+
if (name !== 'gzip' && name !== 'x-gzip' && name !== '*') continue;
|
|
164
|
+
let q = 1;
|
|
165
|
+
for (const param of params) {
|
|
166
|
+
const m = /^\s*q\s*=\s*([0-9.]+)\s*$/i.exec(param);
|
|
167
|
+
if (m) {
|
|
168
|
+
const parsed = Number(m[1]);
|
|
169
|
+
q = Number.isFinite(parsed) ? parsed : 0;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (name === '*') star = q;
|
|
173
|
+
else gzip = q;
|
|
174
|
+
}
|
|
175
|
+
return (gzip ?? star ?? 0) > 0;
|
|
117
176
|
}
|
|
118
177
|
|
|
119
|
-
|
|
178
|
+
/** 解析单区间 Range:合法返回闭区间;越界 'unsatisfiable';多区间 / 非法语法返回 null(忽略 Range)。 */
|
|
179
|
+
function parseSingleRange(
|
|
180
|
+
header: string,
|
|
181
|
+
size: number,
|
|
182
|
+
): { start: number; end: number } | 'unsatisfiable' | null {
|
|
183
|
+
const m = /^\s*bytes\s*=\s*(\d*)\s*-\s*(\d*)\s*$/i.exec(header);
|
|
184
|
+
if (!m || (m[1] === '' && m[2] === '')) return null;
|
|
185
|
+
let start: number;
|
|
186
|
+
let end: number;
|
|
187
|
+
if (m[1] === '') {
|
|
188
|
+
const suffix = Number(m[2]);
|
|
189
|
+
if (!Number.isSafeInteger(suffix)) return null;
|
|
190
|
+
if (suffix === 0 || size === 0) return 'unsatisfiable';
|
|
191
|
+
start = Math.max(0, size - suffix);
|
|
192
|
+
end = size - 1;
|
|
193
|
+
} else {
|
|
194
|
+
start = Number(m[1]);
|
|
195
|
+
if (!Number.isSafeInteger(start)) return null;
|
|
196
|
+
if (m[2] === '') {
|
|
197
|
+
end = size - 1;
|
|
198
|
+
} else {
|
|
199
|
+
end = Number(m[2]);
|
|
200
|
+
if (!Number.isSafeInteger(end) || end < start) return null;
|
|
201
|
+
}
|
|
202
|
+
if (start >= size) return 'unsatisfiable';
|
|
203
|
+
end = Math.min(end, size - 1);
|
|
204
|
+
}
|
|
205
|
+
return { start, end };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function serveStaticFromRoot(
|
|
209
|
+
root: string,
|
|
120
210
|
segments: readonly string[],
|
|
121
211
|
request: { readonly method: string; readonly headers: { get(name: string): string | null } },
|
|
122
|
-
|
|
123
|
-
): Response {
|
|
124
|
-
|
|
125
|
-
const hostDir = env.SOMA_HOST_DIR;
|
|
126
|
-
if (!hostDir) return notFound();
|
|
127
|
-
let root: string;
|
|
128
|
-
try {
|
|
129
|
-
root = staticRootOf(hostDir, env);
|
|
130
|
-
} catch {
|
|
131
|
-
return notFound();
|
|
132
|
-
}
|
|
212
|
+
allowImmutableContentAddressing = false,
|
|
213
|
+
): Response | null {
|
|
214
|
+
if (!isSafeSegments(segments)) return null;
|
|
133
215
|
const resolved = resolveStaticFile(root, segments);
|
|
134
|
-
if (!resolved.ok) return
|
|
216
|
+
if (!resolved.ok) return null;
|
|
135
217
|
|
|
218
|
+
const ext = path.extname(resolved.file).toLowerCase();
|
|
136
219
|
const etag = `W/"${createHash('sha1').update(`${resolved.size}:${resolved.mtimeMs}`).digest('hex').slice(0, 20)}"`;
|
|
137
|
-
const hashed = HASHED_NAME.test(path.basename(resolved.file));
|
|
220
|
+
const hashed = allowImmutableContentAddressing && HASHED_NAME.test(path.basename(resolved.file));
|
|
138
221
|
const headers: Record<string, string> = {
|
|
139
|
-
'content-type':
|
|
222
|
+
'content-type': contentTypeForExtension(ext),
|
|
140
223
|
'cache-control': hashed ? 'public, max-age=31536000, immutable' : 'public, max-age=300, must-revalidate',
|
|
141
224
|
etag,
|
|
142
225
|
'x-content-type-options': 'nosniff',
|
|
143
226
|
};
|
|
144
227
|
|
|
228
|
+
if (ext === '.svg') {
|
|
229
|
+
headers['content-security-policy'] = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// 是否存在 gzip 表示只看大小(不读文件):存在则 200 / 304 / 206 一律带 Vary: Accept-Encoding;304 / 206 / 416 不取缓存
|
|
233
|
+
if (hasGzipRepresentation(resolved.size)) headers['vary'] = 'Accept-Encoding';
|
|
234
|
+
|
|
145
235
|
if (request.headers.get('if-none-match')?.split(',').some((value) => value.trim() === etag)) {
|
|
146
236
|
return new Response(null, { status: 304, headers });
|
|
147
237
|
}
|
|
148
238
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
239
|
+
const isHead = request.method.toUpperCase() === 'HEAD';
|
|
240
|
+
|
|
241
|
+
// 单区间 Range:独占身份编码(不 gzip),流式只读该区间;多区间 / 非法语法忽略 Range 走完整 200。
|
|
242
|
+
const rangeHeader = request.headers.get('range');
|
|
243
|
+
const ifRange = request.headers.get('if-range');
|
|
244
|
+
// If-Range 要求强比较:本服务只产弱 ETag,ETag 形式一律忽略 Range;响应不带 Last-Modified,日期形式同样忽略。
|
|
245
|
+
if (rangeHeader && ifRange === null) {
|
|
246
|
+
const range = parseSingleRange(rangeHeader, resolved.size);
|
|
247
|
+
if (range === 'unsatisfiable') {
|
|
248
|
+
const { etag: _etag, ...rest } = headers;
|
|
249
|
+
return new Response(null, {
|
|
250
|
+
status: 416,
|
|
251
|
+
headers: { ...rest, 'cache-control': 'no-store', 'content-range': `bytes */${resolved.size}` },
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
if (range) {
|
|
255
|
+
const length = range.end - range.start + 1;
|
|
256
|
+
const rangeHeaders = {
|
|
257
|
+
...headers,
|
|
258
|
+
'accept-ranges': 'bytes',
|
|
259
|
+
'content-range': `bytes ${range.start}-${range.end}/${resolved.size}`,
|
|
260
|
+
'content-length': String(length),
|
|
261
|
+
};
|
|
262
|
+
if (isHead) return new Response(null, { status: 206, headers: rangeHeaders });
|
|
263
|
+
const fileStream = fs.createReadStream(resolved.file, { start: range.start, end: range.end });
|
|
264
|
+
// 文件在 stat 之后被删 / 无权限 / IO 错误:先挂监听留下现场,再交给 Web 流(流随即以该错误终止响应体)
|
|
265
|
+
fileStream.on('error', (error: NodeJS.ErrnoException) => {
|
|
266
|
+
console.error(
|
|
267
|
+
`[soma-core][静态文件] 区间读取失败:文件=${resolved.file} 区间=${range.start}-${range.end} 错误码=${error.code ?? 'UNKNOWN'}`,
|
|
268
|
+
);
|
|
269
|
+
});
|
|
270
|
+
const stream = Readable.toWeb(fileStream);
|
|
271
|
+
return new Response(stream as unknown as BodyInit, { status: 206, headers: rangeHeaders });
|
|
272
|
+
}
|
|
152
273
|
}
|
|
153
274
|
|
|
275
|
+
// 200:GET 与 HEAD 共用同一段「决定表示与响应头」,HEAD 只是不带响应体;只有这里才需要表示内容,才取缓存
|
|
154
276
|
const cached = getOrCacheStaticFile(resolved.file, resolved.size, resolved.mtimeMs);
|
|
155
|
-
const
|
|
277
|
+
const useGzip = !!cached?.gzip && acceptsGzip(request.headers.get('accept-encoding'));
|
|
278
|
+
if (useGzip && cached?.gzip) {
|
|
279
|
+
headers['content-encoding'] = 'gzip';
|
|
280
|
+
headers['content-length'] = String(cached.gzip.byteLength);
|
|
281
|
+
} else {
|
|
282
|
+
headers['accept-ranges'] = 'bytes';
|
|
283
|
+
headers['content-length'] = String(cached ? cached.raw.byteLength : resolved.size);
|
|
284
|
+
}
|
|
285
|
+
if (isHead) return new Response(null, { status: 200, headers });
|
|
156
286
|
|
|
157
287
|
if (cached) {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
headers['vary'] = 'Accept-Encoding';
|
|
161
|
-
headers['content-length'] = String(cached.gzip.byteLength);
|
|
162
|
-
return new Response(cached.gzip as unknown as BodyInit, { status: 200, headers });
|
|
163
|
-
}
|
|
164
|
-
headers['content-length'] = String(cached.raw.byteLength);
|
|
165
|
-
return new Response(cached.raw as unknown as BodyInit, { status: 200, headers });
|
|
288
|
+
const body = useGzip && cached.gzip ? cached.gzip : cached.raw;
|
|
289
|
+
return new Response(body as unknown as BodyInit, { status: 200, headers });
|
|
166
290
|
}
|
|
167
291
|
|
|
168
292
|
// 大于 4MB 的文件回退到直接读取
|
|
@@ -170,3 +294,29 @@ export function staticResponse(
|
|
|
170
294
|
headers['content-length'] = String(buf.length);
|
|
171
295
|
return new Response(buf, { status: 200, headers });
|
|
172
296
|
}
|
|
297
|
+
|
|
298
|
+
export function staticResponse(
|
|
299
|
+
segments: readonly string[],
|
|
300
|
+
request: { readonly method: string; readonly headers: { get(name: string): string | null } },
|
|
301
|
+
env: Readonly<Record<string, string | undefined>> = process.env,
|
|
302
|
+
): Response {
|
|
303
|
+
const notFound = () =>
|
|
304
|
+
new Response('Not found', {
|
|
305
|
+
status: 404,
|
|
306
|
+
headers: {
|
|
307
|
+
'cache-control': 'no-store',
|
|
308
|
+
'content-type': 'text/plain; charset=utf-8',
|
|
309
|
+
'x-content-type-options': 'nosniff',
|
|
310
|
+
},
|
|
311
|
+
});
|
|
312
|
+
const hostDir = env.SOMA_HOST_DIR;
|
|
313
|
+
if (!hostDir) return notFound();
|
|
314
|
+
let root: string;
|
|
315
|
+
try {
|
|
316
|
+
root = staticRootOf(hostDir, env);
|
|
317
|
+
} catch {
|
|
318
|
+
return notFound();
|
|
319
|
+
}
|
|
320
|
+
const res = serveStaticFromRoot(root, segments, request);
|
|
321
|
+
return res ?? notFound();
|
|
322
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "soma-core",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.6",
|
|
4
4
|
"description": "SOMA — Semantic Oriented Modular Architecture. A declarative frontend framework runtime with no UI framework dependency; native host shell ships in host-native/.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"soma",
|
|
@@ -317,10 +317,11 @@
|
|
|
317
317
|
"esbuild": "0.25.12",
|
|
318
318
|
"jiti": "1.21.7",
|
|
319
319
|
"js-yaml": "4.3.2",
|
|
320
|
+
"mime-types": "2.1.35",
|
|
320
321
|
"parse5": "7.3.0",
|
|
321
322
|
"qrcode-generator": "2.0.4",
|
|
322
323
|
"server-only": "0.0.1",
|
|
323
|
-
"soma-foundation": "^0.1.
|
|
324
|
+
"soma-foundation": "^0.1.3",
|
|
324
325
|
"temporal-polyfill": "0.3.2"
|
|
325
326
|
},
|
|
326
327
|
"peerDependencies": {
|
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
// 动作编排运行时(REQ-010 S4)——按编译好的图走一遍。
|
|
2
|
-
//
|
|
3
|
-
// 这里刻意很短:难的部分(可终止性、槽的可见性、并行分支不互引)在解析期就判完了,
|
|
4
|
-
// 运行期只需要照着走。这正是「编排图是纯数据」的收益——
|
|
5
|
-
// 如果编排是一段脚本,这些判断就只能在运行时做,也就是只能在出事之后做。
|
|
6
|
-
//
|
|
7
|
-
// 一条纪律:**输出槽只活在本次事件里**。事件一结束就销毁,绝不写进页面状态。
|
|
8
|
-
// 写进去的话,「这个值是谁放的」就取决于用户最近点过什么——那正是 amis 的运行时数据链
|
|
9
|
-
// 难以排查的根源。
|
|
10
|
-
|
|
11
|
-
import { evaluatePredicate } from '../../shared/predicates';
|
|
12
|
-
import type { SomaFieldPredicate } from '../../shared/predicates';
|
|
13
|
-
import type { ParsedEvent, SomaConfirmSpec } from '../../shared/types/event.types';
|
|
14
|
-
import type { SomaCompiledStep, SomaStepGraph } from '../../shared/types/step.types';
|
|
15
|
-
|
|
16
|
-
export interface StepRunContext {
|
|
17
|
-
/** 事件载荷:`when` 的字段来源之一 */
|
|
18
|
-
payload: Record<string, unknown>;
|
|
19
|
-
/** 执行一条已解析的管道;返回值进输出槽 */
|
|
20
|
-
dispatch(pipeline: Omit<ParsedEvent, 'somaId' | 'domEvent'>, scope?: Record<string, unknown>): Promise<unknown>;
|
|
21
|
-
/** bootstrap 注册的具名动作 */
|
|
22
|
-
actions?: Record<string, (payload: Record<string, unknown>) => Promise<unknown> | unknown>;
|
|
23
|
-
/** 出错落点;不给就 console.error */
|
|
24
|
-
onError?(message: string, detail: { step: number; cause: unknown }): void;
|
|
25
|
-
isValid?(): boolean;
|
|
26
|
-
/**
|
|
27
|
-
* 确认分级的执行口(R-23):步骤管道带 `confirm`(plain 文案或 typed / consent 规格)时,执行前先过它;
|
|
28
|
-
* 返回 false = 用户取消,整条编排中止(`stopped` + `cancelled`),与单管道事件「取消即中止」同口径。
|
|
29
|
-
* 不给就不确认。步骤类型不限:ACTION 步骤与其余步骤一样先过它。
|
|
30
|
-
*/
|
|
31
|
-
confirm?(spec: SomaConfirmSpec): boolean | Promise<boolean>;
|
|
32
|
-
/**
|
|
33
|
-
* 权限码判据(管道尾段 `permission:code`):返回 false = 拒绝,整条编排中止(`stopped` + `denied: 'permission'`),
|
|
34
|
-
* 与单管道事件被权限中间件拦下同口径;提示由实现方负责。不给就不判。
|
|
35
|
-
*/
|
|
36
|
-
permission?(code: string): boolean | Promise<boolean>;
|
|
37
|
-
/** 角色判据(管道尾段 `roles:a,b`):返回 false = 拒绝,整条编排中止(`denied: 'roles'`),与单管道的角色门同口径。不给就不判。 */
|
|
38
|
-
roles?(roles: readonly string[]): boolean;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export interface StepRunResult {
|
|
42
|
-
/** 实际执行过的步骤序号(顺序即执行序,`goto` 会让它跳号) */
|
|
43
|
-
executed: number[];
|
|
44
|
-
/** 事件结束时的槽(仅用于测试与诊断;运行期不外泄) */
|
|
45
|
-
slots: Record<string, unknown>;
|
|
46
|
-
/** 是否被 on_error: stop 中断 */
|
|
47
|
-
stopped: boolean;
|
|
48
|
-
/** 因某步确认被取消而中止(`stopped` 同为 true) */
|
|
49
|
-
cancelled?: boolean;
|
|
50
|
-
/** 因某步权限码或角色不通过而中止(`stopped` 同为 true) */
|
|
51
|
-
denied?: 'permission' | 'roles';
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export async function runSteps(
|
|
55
|
-
graph: SomaStepGraph,
|
|
56
|
-
ctx: StepRunContext,
|
|
57
|
-
): Promise<StepRunResult> {
|
|
58
|
-
const slots: Record<string, unknown> = {};
|
|
59
|
-
const executed: number[] = [];
|
|
60
|
-
let stopped = false;
|
|
61
|
-
let invalidated = false;
|
|
62
|
-
let cancelled = false;
|
|
63
|
-
let denied: 'permission' | 'roles' | undefined;
|
|
64
|
-
|
|
65
|
-
const isValid = (): boolean => {
|
|
66
|
-
if (!invalidated) {
|
|
67
|
-
try {
|
|
68
|
-
invalidated = ctx.isValid?.() === false;
|
|
69
|
-
} catch {
|
|
70
|
-
invalidated = true;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
if (invalidated) stopped = true;
|
|
74
|
-
return !invalidated;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
const report = (message: string, step: number, cause: unknown): void => {
|
|
78
|
-
if (ctx.onError) ctx.onError(message, { step, cause });
|
|
79
|
-
else console.error(`[soma-core][引擎] 第 ${step + 1} 步失败:${message}`, cause);
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
/** `when` 的取值面:事件载荷 ⊕ 当前槽。槽在后,同名时槽赢(它更晚、更具体) */
|
|
83
|
-
const scope = (extra?: Record<string, unknown>): Record<string, unknown> =>
|
|
84
|
-
({ ...ctx.payload, ...slots, ...extra });
|
|
85
|
-
|
|
86
|
-
const holds = (when: SomaFieldPredicate | undefined, extra?: Record<string, unknown>): boolean => {
|
|
87
|
-
if (!isValid()) return false;
|
|
88
|
-
if (when === undefined) return true;
|
|
89
|
-
const values = scope(extra);
|
|
90
|
-
if (!isValid()) return false;
|
|
91
|
-
const matches = evaluatePredicate(when, values, {});
|
|
92
|
-
return isValid() && matches;
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
const call = async (
|
|
96
|
-
pipeline: Omit<ParsedEvent, 'somaId' | 'domEvent'>,
|
|
97
|
-
extra?: Record<string, unknown>,
|
|
98
|
-
): Promise<unknown> => {
|
|
99
|
-
if (!isValid()) return undefined;
|
|
100
|
-
// 尾段闸门,所有步骤类型同一份(含 ACTION),顺序与单管道一致:权限 -> 确认 -> 角色
|
|
101
|
-
if (pipeline.permission && ctx.permission) {
|
|
102
|
-
const allowed = await ctx.permission(pipeline.permission);
|
|
103
|
-
if (!isValid()) return undefined;
|
|
104
|
-
if (!allowed) {
|
|
105
|
-
denied = 'permission';
|
|
106
|
-
stopped = true;
|
|
107
|
-
return undefined;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
if (pipeline.confirm && ctx.confirm) {
|
|
111
|
-
const spec: SomaConfirmSpec = pipeline.confirmSpec ?? { level: 'plain', text: pipeline.confirm };
|
|
112
|
-
const accepted = await ctx.confirm(spec);
|
|
113
|
-
if (!isValid()) return undefined;
|
|
114
|
-
if (!accepted) {
|
|
115
|
-
cancelled = true;
|
|
116
|
-
stopped = true;
|
|
117
|
-
return undefined;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
if (pipeline.roles && pipeline.roles.length > 0 && ctx.roles) {
|
|
121
|
-
if (!ctx.roles(pipeline.roles)) {
|
|
122
|
-
denied = 'roles';
|
|
123
|
-
stopped = true;
|
|
124
|
-
return undefined;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
if (pipeline.type === 'ACTION') {
|
|
128
|
-
const name = String(pipeline.funcPath ?? '');
|
|
129
|
-
const action = ctx.actions?.[name];
|
|
130
|
-
if (typeof action !== 'function') {
|
|
131
|
-
// 解析期与 bootstrap 期都查过一遍,走到这里说明注册表在运行中被换过。
|
|
132
|
-
// 抛而不是静默跳过:一个"没执行但也没报错"的动作是排查不出来的。
|
|
133
|
-
throw new Error(`具名动作 "${name}" 未注册`);
|
|
134
|
-
}
|
|
135
|
-
const payload = scope(extra);
|
|
136
|
-
if (!isValid()) return undefined;
|
|
137
|
-
return action(payload);
|
|
138
|
-
}
|
|
139
|
-
return ctx.dispatch(pipeline, scope(extra));
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* 走一串步骤。
|
|
144
|
-
*
|
|
145
|
-
* `goto` 只在同一层内跳(解析期保证它只向后),所以这里用一个下标游标而不是递归,
|
|
146
|
-
* 跳转就是把游标挪过去。
|
|
147
|
-
*/
|
|
148
|
-
const walk = async (list: SomaCompiledStep[], extra?: Record<string, unknown>): Promise<void> => {
|
|
149
|
-
let cursor = 0;
|
|
150
|
-
while (cursor < list.length) {
|
|
151
|
-
if (stopped || !isValid()) return;
|
|
152
|
-
const step = list[cursor];
|
|
153
|
-
|
|
154
|
-
if (!holds(step.when, extra)) {
|
|
155
|
-
if (!isValid()) return;
|
|
156
|
-
cursor += 1;
|
|
157
|
-
continue;
|
|
158
|
-
}
|
|
159
|
-
executed.push(step.index);
|
|
160
|
-
|
|
161
|
-
try {
|
|
162
|
-
if (step.kind === 'do') {
|
|
163
|
-
const value = await call(step.pipeline!, extra);
|
|
164
|
-
if (!isValid() || cancelled || denied) return;
|
|
165
|
-
if (step.output !== undefined) slots[step.output] = value;
|
|
166
|
-
} else if (step.kind === 'switch') {
|
|
167
|
-
// 取**第一个**成立的分支。otherwise 解析期已保证在末尾,所以顺序扫即可。
|
|
168
|
-
const branch = (step.branches ?? []).find((item) => isValid() && (item.otherwise || holds(item.when, extra)));
|
|
169
|
-
if (branch !== undefined) {
|
|
170
|
-
const value = await call(branch.pipeline, extra);
|
|
171
|
-
if (!isValid() || cancelled || denied) return;
|
|
172
|
-
if (branch.output !== undefined) slots[branch.output] = value;
|
|
173
|
-
}
|
|
174
|
-
} else if (step.kind === 'parallel') {
|
|
175
|
-
// 分支之间不互引槽(解析期已判),所以真并行是安全的
|
|
176
|
-
await Promise.all((step.children ?? []).map((child) => walk([child], extra)));
|
|
177
|
-
if (!isValid()) return;
|
|
178
|
-
} else if (step.kind === 'for_each') {
|
|
179
|
-
const source = scope(extra)[step.over!];
|
|
180
|
-
const items = Array.isArray(source) ? source : [];
|
|
181
|
-
for (const item of items) {
|
|
182
|
-
if (stopped || !isValid()) break;
|
|
183
|
-
// 项名只在体内可见:出了循环体就没了,不写进 slots
|
|
184
|
-
await walk(step.children ?? [], { ...extra, [step.as!]: item });
|
|
185
|
-
if (!isValid()) return;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
} catch (cause) {
|
|
189
|
-
if (!isValid()) return;
|
|
190
|
-
const message = cause instanceof Error ? cause.message : String(cause);
|
|
191
|
-
report(message, step.index, cause);
|
|
192
|
-
if (!isValid()) return;
|
|
193
|
-
if (step.onError === 'continue') {
|
|
194
|
-
cursor += 1;
|
|
195
|
-
continue;
|
|
196
|
-
}
|
|
197
|
-
if (step.onError.startsWith('goto:')) {
|
|
198
|
-
// 声明里的序号是**从 1 起**的人类序号,游标是从 0 起的下标
|
|
199
|
-
cursor = Number(step.onError.slice(5)) - 1;
|
|
200
|
-
continue;
|
|
201
|
-
}
|
|
202
|
-
stopped = true;
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
cursor += 1;
|
|
206
|
-
}
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
await walk(graph.steps);
|
|
210
|
-
isValid();
|
|
211
|
-
return { executed, slots, stopped, ...(cancelled ? { cancelled: true } : {}), ...(denied ? { denied } : {}) };
|
|
212
|
-
}
|