apcore-js 0.5.0 → 0.7.0
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 +1 -1
- package/dist/acl.d.ts +27 -0
- package/dist/acl.d.ts.map +1 -0
- package/dist/acl.js +175 -0
- package/dist/acl.js.map +1 -0
- package/dist/approval.d.ts +85 -0
- package/dist/approval.d.ts.map +1 -0
- package/dist/approval.js +73 -0
- package/dist/approval.js.map +1 -0
- package/dist/async-task.d.ts +90 -0
- package/dist/async-task.d.ts.map +1 -0
- package/dist/async-task.js +215 -0
- package/dist/async-task.js.map +1 -0
- package/dist/bindings.d.ts +12 -0
- package/dist/bindings.d.ts.map +1 -0
- package/dist/bindings.js +185 -0
- package/dist/bindings.js.map +1 -0
- package/dist/cancel.d.ts +14 -0
- package/dist/cancel.d.ts.map +1 -0
- package/dist/cancel.js +27 -0
- package/dist/cancel.js.map +1 -0
- package/dist/config.d.ts +9 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +23 -0
- package/dist/config.js.map +1 -0
- package/dist/context.d.ts +50 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +87 -0
- package/dist/context.js.map +1 -0
- package/dist/decorator.d.ts +57 -0
- package/dist/decorator.d.ts.map +1 -0
- package/dist/decorator.js +74 -0
- package/dist/decorator.js.map +1 -0
- package/dist/errors.d.ts +204 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +364 -0
- package/dist/errors.js.map +1 -0
- package/dist/executor.d.ts +82 -0
- package/dist/executor.d.ts.map +1 -0
- package/dist/executor.js +489 -0
- package/dist/executor.js.map +1 -0
- package/dist/extensions.d.ts +58 -0
- package/dist/extensions.d.ts.map +1 -0
- package/dist/extensions.js +239 -0
- package/dist/extensions.js.map +1 -0
- package/{src/index.ts → dist/index.d.ts} +6 -63
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +45 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware/adapters.d.ts +18 -0
- package/dist/middleware/adapters.d.ts.map +1 -0
- package/dist/middleware/adapters.js +25 -0
- package/dist/middleware/adapters.js.map +1 -0
- package/dist/middleware/base.d.ts +10 -0
- package/dist/middleware/base.d.ts.map +1 -0
- package/dist/middleware/base.js +15 -0
- package/dist/middleware/base.js.map +1 -0
- package/{src/middleware/index.ts → dist/middleware/index.d.ts} +1 -0
- package/dist/middleware/index.d.ts.map +1 -0
- package/dist/middleware/index.js +5 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/middleware/logging.d.ts +25 -0
- package/dist/middleware/logging.d.ts.map +1 -0
- package/dist/middleware/logging.js +64 -0
- package/dist/middleware/logging.js.map +1 -0
- package/dist/middleware/manager.d.ts +21 -0
- package/dist/middleware/manager.d.ts.map +1 -0
- package/dist/middleware/manager.js +77 -0
- package/dist/middleware/manager.js.map +1 -0
- package/dist/module.d.ts +31 -0
- package/dist/module.d.ts.map +1 -0
- package/dist/module.js +12 -0
- package/dist/module.js.map +1 -0
- package/dist/observability/context-logger.d.ts +54 -0
- package/dist/observability/context-logger.d.ts.map +1 -0
- package/dist/observability/context-logger.js +151 -0
- package/dist/observability/context-logger.js.map +1 -0
- package/{src/observability/index.ts → dist/observability/index.d.ts} +1 -0
- package/dist/observability/index.d.ts.map +1 -0
- package/dist/observability/index.js +4 -0
- package/dist/observability/index.js.map +1 -0
- package/dist/observability/metrics.d.ts +30 -0
- package/dist/observability/metrics.d.ts.map +1 -0
- package/dist/observability/metrics.js +177 -0
- package/dist/observability/metrics.js.map +1 -0
- package/dist/observability/tracing.d.ts +62 -0
- package/dist/observability/tracing.d.ts.map +1 -0
- package/dist/observability/tracing.js +184 -0
- package/dist/observability/tracing.js.map +1 -0
- package/dist/registry/dependencies.d.ts +6 -0
- package/dist/registry/dependencies.d.ts.map +1 -0
- package/dist/registry/dependencies.js +83 -0
- package/dist/registry/dependencies.js.map +1 -0
- package/dist/registry/entry-point.d.ts +6 -0
- package/dist/registry/entry-point.d.ts.map +1 -0
- package/dist/registry/entry-point.js +55 -0
- package/dist/registry/entry-point.js.map +1 -0
- package/{src/registry/index.ts → dist/registry/index.d.ts} +1 -0
- package/dist/registry/index.d.ts.map +1 -0
- package/dist/registry/index.js +8 -0
- package/dist/registry/index.js.map +1 -0
- package/dist/registry/metadata.d.ts +9 -0
- package/dist/registry/metadata.d.ts.map +1 -0
- package/dist/registry/metadata.js +105 -0
- package/dist/registry/metadata.js.map +1 -0
- package/dist/registry/registry.d.ts +102 -0
- package/dist/registry/registry.d.ts.map +1 -0
- package/dist/registry/registry.js +534 -0
- package/dist/registry/registry.js.map +1 -0
- package/dist/registry/scanner.d.ts +7 -0
- package/dist/registry/scanner.d.ts.map +1 -0
- package/dist/registry/scanner.js +164 -0
- package/dist/registry/scanner.js.map +1 -0
- package/dist/registry/schema-export.d.ts +9 -0
- package/dist/registry/schema-export.d.ts.map +1 -0
- package/dist/registry/schema-export.js +132 -0
- package/dist/registry/schema-export.js.map +1 -0
- package/dist/registry/types.d.ts +29 -0
- package/dist/registry/types.d.ts.map +1 -0
- package/dist/registry/types.js +5 -0
- package/dist/registry/types.js.map +1 -0
- package/dist/registry/validation.d.ts +9 -0
- package/dist/registry/validation.d.ts.map +1 -0
- package/dist/registry/validation.js +33 -0
- package/dist/registry/validation.js.map +1 -0
- package/dist/schema/annotations.d.ts +8 -0
- package/dist/schema/annotations.d.ts.map +1 -0
- package/dist/schema/annotations.js +52 -0
- package/dist/schema/annotations.js.map +1 -0
- package/dist/schema/exporter.d.ts +13 -0
- package/dist/schema/exporter.d.ts.map +1 -0
- package/dist/schema/exporter.js +71 -0
- package/dist/schema/exporter.js.map +1 -0
- package/dist/schema/index.d.ts +9 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/{src/schema/index.ts → dist/schema/index.js} +1 -7
- package/dist/schema/index.js.map +1 -0
- package/dist/schema/loader.d.ts +30 -0
- package/dist/schema/loader.d.ts.map +1 -0
- package/dist/schema/loader.js +260 -0
- package/dist/schema/loader.js.map +1 -0
- package/dist/schema/ref-resolver.d.ts +19 -0
- package/dist/schema/ref-resolver.d.ts.map +1 -0
- package/dist/schema/ref-resolver.js +212 -0
- package/dist/schema/ref-resolver.js.map +1 -0
- package/dist/schema/strict.d.ts +7 -0
- package/dist/schema/strict.d.ts.map +1 -0
- package/dist/schema/strict.js +127 -0
- package/dist/schema/strict.js.map +1 -0
- package/dist/schema/types.d.ts +53 -0
- package/dist/schema/types.d.ts.map +1 -0
- package/dist/schema/types.js +31 -0
- package/dist/schema/types.js.map +1 -0
- package/dist/schema/validator.d.ts +16 -0
- package/dist/schema/validator.d.ts.map +1 -0
- package/dist/schema/validator.js +71 -0
- package/dist/schema/validator.js.map +1 -0
- package/dist/trace-context.d.ts +35 -0
- package/dist/trace-context.d.ts.map +1 -0
- package/dist/trace-context.js +86 -0
- package/dist/trace-context.js.map +1 -0
- package/dist/utils/index.d.ts +11 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +32 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/pattern.d.ts +5 -0
- package/dist/utils/pattern.d.ts.map +1 -0
- package/dist/utils/pattern.js +31 -0
- package/dist/utils/pattern.js.map +1 -0
- package/package.json +24 -3
- package/.claude/settings.local.json +0 -12
- package/.github/workflows/ci.yml +0 -39
- package/.gitmessage +0 -60
- package/.pre-commit-config.yaml +0 -28
- package/CHANGELOG.md +0 -214
- package/CLAUDE.md +0 -68
- package/apcore-logo.svg +0 -79
- package/planning/acl-system/overview.md +0 -54
- package/planning/acl-system/plan.md +0 -92
- package/planning/acl-system/state.json +0 -76
- package/planning/acl-system/tasks/acl-core.md +0 -226
- package/planning/acl-system/tasks/acl-rule.md +0 -92
- package/planning/acl-system/tasks/conditional-rules.md +0 -259
- package/planning/acl-system/tasks/pattern-matching.md +0 -152
- package/planning/acl-system/tasks/yaml-loading.md +0 -271
- package/planning/core-executor/overview.md +0 -53
- package/planning/core-executor/plan.md +0 -88
- package/planning/core-executor/state.json +0 -76
- package/planning/core-executor/tasks/async-support.md +0 -106
- package/planning/core-executor/tasks/execution-pipeline.md +0 -113
- package/planning/core-executor/tasks/redaction.md +0 -85
- package/planning/core-executor/tasks/safety-checks.md +0 -65
- package/planning/core-executor/tasks/setup.md +0 -75
- package/planning/decorator-bindings/overview.md +0 -62
- package/planning/decorator-bindings/plan.md +0 -104
- package/planning/decorator-bindings/state.json +0 -87
- package/planning/decorator-bindings/tasks/binding-directory.md +0 -79
- package/planning/decorator-bindings/tasks/binding-loader.md +0 -148
- package/planning/decorator-bindings/tasks/explicit-schemas.md +0 -85
- package/planning/decorator-bindings/tasks/function-module.md +0 -127
- package/planning/decorator-bindings/tasks/module-factory.md +0 -89
- package/planning/decorator-bindings/tasks/schema-modes.md +0 -142
- package/planning/middleware-system/overview.md +0 -48
- package/planning/middleware-system/plan.md +0 -102
- package/planning/middleware-system/state.json +0 -65
- package/planning/middleware-system/tasks/adapters.md +0 -170
- package/planning/middleware-system/tasks/base.md +0 -115
- package/planning/middleware-system/tasks/logging-middleware.md +0 -304
- package/planning/middleware-system/tasks/manager.md +0 -313
- package/planning/observability/overview.md +0 -53
- package/planning/observability/plan.md +0 -119
- package/planning/observability/state.json +0 -98
- package/planning/observability/tasks/context-logger.md +0 -201
- package/planning/observability/tasks/exporters.md +0 -121
- package/planning/observability/tasks/metrics-collector.md +0 -162
- package/planning/observability/tasks/metrics-middleware.md +0 -141
- package/planning/observability/tasks/obs-logging-middleware.md +0 -179
- package/planning/observability/tasks/span-model.md +0 -120
- package/planning/observability/tasks/tracing-middleware.md +0 -179
- package/planning/overview.md +0 -81
- package/planning/registry-system/overview.md +0 -57
- package/planning/registry-system/plan.md +0 -114
- package/planning/registry-system/state.json +0 -109
- package/planning/registry-system/tasks/dependencies.md +0 -157
- package/planning/registry-system/tasks/entry-point.md +0 -148
- package/planning/registry-system/tasks/metadata.md +0 -198
- package/planning/registry-system/tasks/registry-core.md +0 -323
- package/planning/registry-system/tasks/scanner.md +0 -172
- package/planning/registry-system/tasks/schema-export.md +0 -261
- package/planning/registry-system/tasks/types.md +0 -124
- package/planning/registry-system/tasks/validation.md +0 -177
- package/planning/schema-system/overview.md +0 -56
- package/planning/schema-system/plan.md +0 -121
- package/planning/schema-system/state.json +0 -98
- package/planning/schema-system/tasks/exporter.md +0 -153
- package/planning/schema-system/tasks/loader.md +0 -106
- package/planning/schema-system/tasks/ref-resolver.md +0 -133
- package/planning/schema-system/tasks/strict-mode.md +0 -140
- package/planning/schema-system/tasks/typebox-generation.md +0 -133
- package/planning/schema-system/tasks/types-and-annotations.md +0 -160
- package/planning/schema-system/tasks/validator.md +0 -149
- package/src/acl.ts +0 -200
- package/src/async-task.ts +0 -267
- package/src/bindings.ts +0 -207
- package/src/cancel.ts +0 -32
- package/src/config.ts +0 -24
- package/src/context.ts +0 -160
- package/src/decorator.ts +0 -110
- package/src/errors.ts +0 -429
- package/src/executor.ts +0 -493
- package/src/extensions.ts +0 -265
- package/src/middleware/adapters.ts +0 -54
- package/src/middleware/base.ts +0 -33
- package/src/middleware/logging.ts +0 -103
- package/src/middleware/manager.ts +0 -105
- package/src/module.ts +0 -43
- package/src/observability/context-logger.ts +0 -203
- package/src/observability/metrics.ts +0 -214
- package/src/observability/tracing.ts +0 -252
- package/src/registry/dependencies.ts +0 -99
- package/src/registry/entry-point.ts +0 -64
- package/src/registry/metadata.ts +0 -111
- package/src/registry/registry.ts +0 -580
- package/src/registry/scanner.ts +0 -168
- package/src/registry/schema-export.ts +0 -181
- package/src/registry/types.ts +0 -32
- package/src/registry/validation.ts +0 -38
- package/src/schema/annotations.ts +0 -68
- package/src/schema/exporter.ts +0 -90
- package/src/schema/loader.ts +0 -273
- package/src/schema/ref-resolver.ts +0 -244
- package/src/schema/strict.ts +0 -136
- package/src/schema/types.ts +0 -73
- package/src/schema/validator.ts +0 -82
- package/src/trace-context.ts +0 -102
- package/src/utils/index.ts +0 -5
- package/src/utils/pattern.ts +0 -30
- package/tests/async-task.test.ts +0 -335
- package/tests/helpers.ts +0 -30
- package/tests/integration/test-acl-safety.test.ts +0 -269
- package/tests/integration/test-binding-executor.test.ts +0 -194
- package/tests/integration/test-e2e-flow.test.ts +0 -117
- package/tests/integration/test-error-propagation.test.ts +0 -259
- package/tests/integration/test-middleware-chain.test.ts +0 -120
- package/tests/integration/test-observability-integration.test.ts +0 -438
- package/tests/observability/test-context-logger.test.ts +0 -123
- package/tests/observability/test-metrics.test.ts +0 -186
- package/tests/observability/test-tracing.test.ts +0 -303
- package/tests/registry/test-dependencies.test.ts +0 -70
- package/tests/registry/test-entry-point.test.ts +0 -133
- package/tests/registry/test-metadata.test.ts +0 -265
- package/tests/registry/test-registry.test.ts +0 -1397
- package/tests/registry/test-scanner.test.ts +0 -257
- package/tests/registry/test-schema-export.test.ts +0 -355
- package/tests/registry/test-validation.test.ts +0 -75
- package/tests/schema/test-annotations.test.ts +0 -137
- package/tests/schema/test-exporter.test.ts +0 -172
- package/tests/schema/test-loader.test.ts +0 -461
- package/tests/schema/test-ref-resolver.test.ts +0 -530
- package/tests/schema/test-strict.test.ts +0 -348
- package/tests/schema/test-validator.test.ts +0 -64
- package/tests/test-acl.test.ts +0 -423
- package/tests/test-bindings.test.ts +0 -227
- package/tests/test-cancel.test.ts +0 -71
- package/tests/test-config.test.ts +0 -76
- package/tests/test-context.test.ts +0 -266
- package/tests/test-decorator.test.ts +0 -173
- package/tests/test-errors.test.ts +0 -647
- package/tests/test-executor-stream.test.ts +0 -208
- package/tests/test-executor.test.ts +0 -252
- package/tests/test-extensions.test.ts +0 -310
- package/tests/test-logging-middleware.test.ts +0 -150
- package/tests/test-middleware-manager.test.ts +0 -185
- package/tests/test-middleware.test.ts +0 -86
- package/tests/test-trace-context.test.ts +0 -251
- package/tests/utils/test-pattern.test.ts +0 -109
- package/tsconfig.build.json +0 -8
- package/tsconfig.json +0 -20
- package/vitest.config.ts +0 -18
package/src/acl.ts
DELETED
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ACL (Access Control List) types and implementation for apcore.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { readFileSync, existsSync } from 'node:fs';
|
|
6
|
-
import yaml from 'js-yaml';
|
|
7
|
-
import type { Context } from './context.js';
|
|
8
|
-
import { ACLRuleError, ConfigNotFoundError } from './errors.js';
|
|
9
|
-
import { matchPattern } from './utils/pattern.js';
|
|
10
|
-
|
|
11
|
-
export interface ACLRule {
|
|
12
|
-
callers: string[];
|
|
13
|
-
targets: string[];
|
|
14
|
-
effect: string;
|
|
15
|
-
description: string;
|
|
16
|
-
conditions?: Record<string, unknown> | null;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function parseAclRule(rawRule: unknown, index: number): ACLRule {
|
|
20
|
-
if (typeof rawRule !== 'object' || rawRule === null || Array.isArray(rawRule)) {
|
|
21
|
-
throw new ACLRuleError(`Rule ${index} must be a mapping, got ${typeof rawRule}`);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const ruleObj = rawRule as Record<string, unknown>;
|
|
25
|
-
for (const key of ['callers', 'targets', 'effect']) {
|
|
26
|
-
if (!(key in ruleObj)) {
|
|
27
|
-
throw new ACLRuleError(`Rule ${index} missing required key '${key}'`);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const effect = ruleObj['effect'] as string;
|
|
32
|
-
if (effect !== 'allow' && effect !== 'deny') {
|
|
33
|
-
throw new ACLRuleError(`Rule ${index} has invalid effect '${effect}', must be 'allow' or 'deny'`);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const callers = ruleObj['callers'];
|
|
37
|
-
if (!Array.isArray(callers)) {
|
|
38
|
-
throw new ACLRuleError(`Rule ${index} 'callers' must be a list, got ${typeof callers}`);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const targets = ruleObj['targets'];
|
|
42
|
-
if (!Array.isArray(targets)) {
|
|
43
|
-
throw new ACLRuleError(`Rule ${index} 'targets' must be a list, got ${typeof targets}`);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
callers: callers as string[],
|
|
48
|
-
targets: targets as string[],
|
|
49
|
-
effect,
|
|
50
|
-
description: (ruleObj['description'] as string) ?? '',
|
|
51
|
-
conditions: (ruleObj['conditions'] as Record<string, unknown>) ?? null,
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export class ACL {
|
|
56
|
-
private _rules: ACLRule[];
|
|
57
|
-
private _defaultEffect: string;
|
|
58
|
-
private _yamlPath: string | null = null;
|
|
59
|
-
debug: boolean = false;
|
|
60
|
-
|
|
61
|
-
constructor(rules: ACLRule[], defaultEffect: string = 'deny') {
|
|
62
|
-
if (defaultEffect !== 'allow' && defaultEffect !== 'deny') {
|
|
63
|
-
throw new ACLRuleError(`Invalid default_effect '${defaultEffect}', must be 'allow' or 'deny'`);
|
|
64
|
-
}
|
|
65
|
-
this._rules = [...rules];
|
|
66
|
-
this._defaultEffect = defaultEffect;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
static load(yamlPath: string): ACL {
|
|
70
|
-
if (!existsSync(yamlPath)) {
|
|
71
|
-
throw new ConfigNotFoundError(yamlPath);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
let data: unknown;
|
|
75
|
-
try {
|
|
76
|
-
const content = readFileSync(yamlPath, 'utf-8');
|
|
77
|
-
data = yaml.load(content);
|
|
78
|
-
} catch (e) {
|
|
79
|
-
if (e instanceof ConfigNotFoundError) throw e;
|
|
80
|
-
throw new ACLRuleError(`Invalid YAML in ${yamlPath}: ${e}`);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (typeof data !== 'object' || data === null || Array.isArray(data)) {
|
|
84
|
-
throw new ACLRuleError(`ACL config must be a mapping, got ${typeof data}`);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const dataObj = data as Record<string, unknown>;
|
|
88
|
-
if (!('rules' in dataObj)) {
|
|
89
|
-
throw new ACLRuleError("ACL config missing required 'rules' key");
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const rawRules = dataObj['rules'];
|
|
93
|
-
if (!Array.isArray(rawRules)) {
|
|
94
|
-
throw new ACLRuleError(`'rules' must be a list, got ${typeof rawRules}`);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const defaultEffect = (dataObj['default_effect'] as string) ?? 'deny';
|
|
98
|
-
const rules = rawRules.map((raw, i) => parseAclRule(raw, i));
|
|
99
|
-
|
|
100
|
-
const acl = new ACL(rules, defaultEffect);
|
|
101
|
-
acl._yamlPath = yamlPath;
|
|
102
|
-
return acl;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
check(callerId: string | null, targetId: string, context?: Context | null): boolean {
|
|
106
|
-
const effectiveCaller = callerId === null ? '@external' : callerId;
|
|
107
|
-
|
|
108
|
-
for (const rule of this._rules) {
|
|
109
|
-
if (this._matchesRule(rule, effectiveCaller, targetId, context ?? null)) {
|
|
110
|
-
return rule.effect === 'allow';
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return this._defaultEffect === 'allow';
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
private _matchPattern(pattern: string, value: string, context: Context | null): boolean {
|
|
118
|
-
if (pattern === '@external') return value === '@external';
|
|
119
|
-
if (pattern === '@system') {
|
|
120
|
-
return context !== null && context.identity !== null && context.identity.type === 'system';
|
|
121
|
-
}
|
|
122
|
-
return matchPattern(pattern, value);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
private _matchesRule(rule: ACLRule, caller: string, target: string, context: Context | null): boolean {
|
|
126
|
-
const callerMatch = rule.callers.some((p) => this._matchPattern(p, caller, context));
|
|
127
|
-
if (!callerMatch) return false;
|
|
128
|
-
|
|
129
|
-
const targetMatch = rule.targets.some((p) => this._matchPattern(p, target, context));
|
|
130
|
-
if (!targetMatch) return false;
|
|
131
|
-
|
|
132
|
-
if (rule.conditions != null) {
|
|
133
|
-
if (!this._checkConditions(rule.conditions, context)) return false;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
return true;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
private _checkConditions(conditions: Record<string, unknown>, context: Context | null): boolean {
|
|
140
|
-
if (context === null) return false;
|
|
141
|
-
|
|
142
|
-
if ('identity_types' in conditions) {
|
|
143
|
-
const types = conditions['identity_types'];
|
|
144
|
-
if (!Array.isArray(types)) {
|
|
145
|
-
console.warn('[apcore:acl] identity_types condition must be an array');
|
|
146
|
-
return false;
|
|
147
|
-
}
|
|
148
|
-
if (context.identity === null || !types.includes(context.identity.type)) return false;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
if ('roles' in conditions) {
|
|
152
|
-
const roles = conditions['roles'];
|
|
153
|
-
if (!Array.isArray(roles)) {
|
|
154
|
-
console.warn('[apcore:acl] roles condition must be an array');
|
|
155
|
-
return false;
|
|
156
|
-
}
|
|
157
|
-
if (context.identity === null) return false;
|
|
158
|
-
const identityRoles = new Set(context.identity.roles);
|
|
159
|
-
if (!roles.some((r: string) => identityRoles.has(r))) return false;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if ('max_call_depth' in conditions) {
|
|
163
|
-
const maxDepth = conditions['max_call_depth'];
|
|
164
|
-
if (typeof maxDepth !== 'number') {
|
|
165
|
-
console.warn('[apcore:acl] max_call_depth condition must be a number');
|
|
166
|
-
return false;
|
|
167
|
-
}
|
|
168
|
-
if (context.callChain.length > maxDepth) return false;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return true;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
addRule(rule: ACLRule): void {
|
|
175
|
-
this._rules.unshift(rule);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
removeRule(callers: string[], targets: string[]): boolean {
|
|
179
|
-
for (let i = 0; i < this._rules.length; i++) {
|
|
180
|
-
const rule = this._rules[i];
|
|
181
|
-
if (
|
|
182
|
-
JSON.stringify(rule.callers) === JSON.stringify(callers) &&
|
|
183
|
-
JSON.stringify(rule.targets) === JSON.stringify(targets)
|
|
184
|
-
) {
|
|
185
|
-
this._rules.splice(i, 1);
|
|
186
|
-
return true;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
return false;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
reload(): void {
|
|
193
|
-
if (this._yamlPath === null) {
|
|
194
|
-
throw new ACLRuleError('Cannot reload: ACL was not loaded from a YAML file');
|
|
195
|
-
}
|
|
196
|
-
const reloaded = ACL.load(this._yamlPath);
|
|
197
|
-
this._rules = reloaded._rules;
|
|
198
|
-
this._defaultEffect = reloaded._defaultEffect;
|
|
199
|
-
}
|
|
200
|
-
}
|
package/src/async-task.ts
DELETED
|
@@ -1,267 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Async task manager for background module execution.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
6
|
-
import type { Context } from './context.js';
|
|
7
|
-
import type { Executor } from './executor.js';
|
|
8
|
-
|
|
9
|
-
export enum TaskStatus {
|
|
10
|
-
PENDING = 'pending',
|
|
11
|
-
RUNNING = 'running',
|
|
12
|
-
COMPLETED = 'completed',
|
|
13
|
-
FAILED = 'failed',
|
|
14
|
-
CANCELLED = 'cancelled',
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface TaskInfo {
|
|
18
|
-
readonly taskId: string;
|
|
19
|
-
readonly moduleId: string;
|
|
20
|
-
readonly status: TaskStatus;
|
|
21
|
-
readonly submittedAt: number;
|
|
22
|
-
readonly startedAt: number | null;
|
|
23
|
-
readonly completedAt: number | null;
|
|
24
|
-
readonly result: Record<string, unknown> | null;
|
|
25
|
-
readonly error: string | null;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
type InternalTaskInfo = { -readonly [K in keyof TaskInfo]: TaskInfo[K] };
|
|
29
|
-
|
|
30
|
-
interface InternalTask {
|
|
31
|
-
info: InternalTaskInfo;
|
|
32
|
-
promise: Promise<void>;
|
|
33
|
-
cancelled: boolean;
|
|
34
|
-
resolve: () => void;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Manages background execution of modules via Promises.
|
|
39
|
-
*
|
|
40
|
-
* Uses a simple counter-based concurrency limiter instead of a semaphore.
|
|
41
|
-
*/
|
|
42
|
-
export class AsyncTaskManager {
|
|
43
|
-
private readonly _executor: Executor;
|
|
44
|
-
private readonly _maxConcurrent: number;
|
|
45
|
-
private readonly _maxTasks: number;
|
|
46
|
-
private readonly _tasks: Map<string, InternalTask> = new Map();
|
|
47
|
-
private _runningCount: number = 0;
|
|
48
|
-
private readonly _waitQueue: Array<() => void> = [];
|
|
49
|
-
|
|
50
|
-
constructor(executor: Executor, maxConcurrent: number = 10, maxTasks: number = 1000) {
|
|
51
|
-
this._executor = executor;
|
|
52
|
-
this._maxConcurrent = maxConcurrent;
|
|
53
|
-
this._maxTasks = maxTasks;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Submit a module for background execution.
|
|
58
|
-
*
|
|
59
|
-
* Returns the generated task_id immediately.
|
|
60
|
-
*/
|
|
61
|
-
submit(
|
|
62
|
-
moduleId: string,
|
|
63
|
-
inputs: Record<string, unknown>,
|
|
64
|
-
context?: Context | null,
|
|
65
|
-
): string {
|
|
66
|
-
if (this._tasks.size >= this._maxTasks) {
|
|
67
|
-
throw new Error(`Task limit reached (${this._maxTasks})`);
|
|
68
|
-
}
|
|
69
|
-
const taskId = uuidv4();
|
|
70
|
-
const info: InternalTaskInfo = {
|
|
71
|
-
taskId,
|
|
72
|
-
moduleId,
|
|
73
|
-
status: TaskStatus.PENDING,
|
|
74
|
-
submittedAt: Date.now() / 1000,
|
|
75
|
-
startedAt: null,
|
|
76
|
-
completedAt: null,
|
|
77
|
-
result: null,
|
|
78
|
-
error: null,
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
let resolvePromise!: () => void;
|
|
82
|
-
const promise = new Promise<void>((resolve) => {
|
|
83
|
-
resolvePromise = resolve;
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
const internal: InternalTask = {
|
|
87
|
-
info,
|
|
88
|
-
promise,
|
|
89
|
-
cancelled: false,
|
|
90
|
-
resolve: resolvePromise,
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
this._tasks.set(taskId, internal);
|
|
94
|
-
this._enqueue(taskId, moduleId, inputs, context ?? null);
|
|
95
|
-
return taskId;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Return the TaskInfo for a task, or null if not found.
|
|
100
|
-
*/
|
|
101
|
-
getStatus(taskId: string): TaskInfo | null {
|
|
102
|
-
const internal = this._tasks.get(taskId);
|
|
103
|
-
return internal ? { ...internal.info } : null;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Return the result of a completed task.
|
|
108
|
-
*
|
|
109
|
-
* Throws if the task is not found or not in COMPLETED status.
|
|
110
|
-
*/
|
|
111
|
-
getResult(taskId: string): Record<string, unknown> {
|
|
112
|
-
const internal = this._tasks.get(taskId);
|
|
113
|
-
if (!internal) {
|
|
114
|
-
throw new Error(`Task not found: ${taskId}`);
|
|
115
|
-
}
|
|
116
|
-
if (internal.info.status !== TaskStatus.COMPLETED) {
|
|
117
|
-
throw new Error(`Task ${taskId} is not completed (status=${internal.info.status})`);
|
|
118
|
-
}
|
|
119
|
-
return internal.info.result!;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Cancel a running or pending task.
|
|
124
|
-
*
|
|
125
|
-
* Sets the cancelled flag and updates status immediately.
|
|
126
|
-
* The underlying execution may still be in-flight but its result
|
|
127
|
-
* will be discarded when it completes.
|
|
128
|
-
*
|
|
129
|
-
* Returns true if the task was successfully marked as cancelled.
|
|
130
|
-
*/
|
|
131
|
-
cancel(taskId: string): boolean {
|
|
132
|
-
const internal = this._tasks.get(taskId);
|
|
133
|
-
if (!internal) return false;
|
|
134
|
-
|
|
135
|
-
const { info } = internal;
|
|
136
|
-
if (info.status !== TaskStatus.PENDING && info.status !== TaskStatus.RUNNING) {
|
|
137
|
-
return false;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
internal.cancelled = true;
|
|
141
|
-
info.status = TaskStatus.CANCELLED;
|
|
142
|
-
info.completedAt = Date.now() / 1000;
|
|
143
|
-
return true;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Return all tasks, optionally filtered by status.
|
|
148
|
-
*/
|
|
149
|
-
listTasks(status?: TaskStatus): TaskInfo[] {
|
|
150
|
-
const tasks = [...this._tasks.values()];
|
|
151
|
-
const filtered = status ? tasks.filter(t => t.info.status === status) : tasks;
|
|
152
|
-
return filtered.map(t => ({ ...t.info }));
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Remove terminal-state tasks older than maxAgeSeconds seconds.
|
|
157
|
-
*
|
|
158
|
-
* Terminal states: COMPLETED, FAILED, CANCELLED.
|
|
159
|
-
* Returns the number of tasks removed.
|
|
160
|
-
*/
|
|
161
|
-
cleanup(maxAgeSeconds: number = 3600): number {
|
|
162
|
-
const terminal = new Set([TaskStatus.COMPLETED, TaskStatus.FAILED, TaskStatus.CANCELLED]);
|
|
163
|
-
const now = Date.now() / 1000;
|
|
164
|
-
let removed = 0;
|
|
165
|
-
|
|
166
|
-
for (const [taskId, internal] of this._tasks.entries()) {
|
|
167
|
-
if (!terminal.has(internal.info.status)) continue;
|
|
168
|
-
const refTime = internal.info.completedAt ?? internal.info.submittedAt;
|
|
169
|
-
if ((now - refTime) >= maxAgeSeconds) {
|
|
170
|
-
this._tasks.delete(taskId);
|
|
171
|
-
removed++;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
return removed;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Cancel all pending and running tasks.
|
|
180
|
-
*/
|
|
181
|
-
shutdown(): void {
|
|
182
|
-
for (const [taskId, task] of this._tasks) {
|
|
183
|
-
if (task.info.status === TaskStatus.PENDING || task.info.status === TaskStatus.RUNNING) {
|
|
184
|
-
this.cancel(taskId);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Acquire a concurrency slot. Resolves when a slot is available.
|
|
191
|
-
*/
|
|
192
|
-
private _acquireSlot(): Promise<void> {
|
|
193
|
-
if (this._runningCount < this._maxConcurrent) {
|
|
194
|
-
this._runningCount++;
|
|
195
|
-
return Promise.resolve();
|
|
196
|
-
}
|
|
197
|
-
return new Promise<void>((resolve) => {
|
|
198
|
-
this._waitQueue.push(() => {
|
|
199
|
-
this._runningCount++;
|
|
200
|
-
resolve();
|
|
201
|
-
});
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Release a concurrency slot and notify the next waiter.
|
|
207
|
-
*/
|
|
208
|
-
private _releaseSlot(): void {
|
|
209
|
-
this._runningCount--;
|
|
210
|
-
if (this._waitQueue.length > 0) {
|
|
211
|
-
const next = this._waitQueue.shift()!;
|
|
212
|
-
next();
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Enqueue the task for execution under the concurrency limit.
|
|
218
|
-
*/
|
|
219
|
-
private _enqueue(
|
|
220
|
-
taskId: string,
|
|
221
|
-
moduleId: string,
|
|
222
|
-
inputs: Record<string, unknown>,
|
|
223
|
-
context: Context | null,
|
|
224
|
-
): void {
|
|
225
|
-
const run = async (): Promise<void> => {
|
|
226
|
-
const internal = this._tasks.get(taskId);
|
|
227
|
-
if (!internal) return;
|
|
228
|
-
|
|
229
|
-
try {
|
|
230
|
-
await this._acquireSlot();
|
|
231
|
-
|
|
232
|
-
// Check if cancelled while waiting for a slot
|
|
233
|
-
if (internal.cancelled) {
|
|
234
|
-
return; // finally block handles releaseSlot + resolve
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
internal.info.status = TaskStatus.RUNNING;
|
|
238
|
-
internal.info.startedAt = Date.now() / 1000;
|
|
239
|
-
|
|
240
|
-
const result = await this._executor.call(moduleId, inputs, context);
|
|
241
|
-
|
|
242
|
-
// Check if cancelled during execution
|
|
243
|
-
if (internal.cancelled) {
|
|
244
|
-
return; // finally block handles releaseSlot + resolve
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
internal.info.status = TaskStatus.COMPLETED;
|
|
248
|
-
internal.info.completedAt = Date.now() / 1000;
|
|
249
|
-
internal.info.result = result;
|
|
250
|
-
} catch (err) {
|
|
251
|
-
if (!internal.cancelled) {
|
|
252
|
-
internal.info.status = TaskStatus.FAILED;
|
|
253
|
-
internal.info.completedAt = Date.now() / 1000;
|
|
254
|
-
internal.info.error = err instanceof Error ? err.message : String(err);
|
|
255
|
-
}
|
|
256
|
-
} finally {
|
|
257
|
-
this._releaseSlot();
|
|
258
|
-
internal.resolve();
|
|
259
|
-
}
|
|
260
|
-
};
|
|
261
|
-
|
|
262
|
-
// Fire and forget -- errors are captured inside run()
|
|
263
|
-
run().catch((err) => {
|
|
264
|
-
console.warn('[apcore:async-task] Unexpected error in task runner:', err);
|
|
265
|
-
});
|
|
266
|
-
}
|
|
267
|
-
}
|
package/src/bindings.ts
DELETED
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* YAML binding loader for zero-code-modification module integration.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { readFileSync, existsSync, readdirSync, statSync } from 'node:fs';
|
|
6
|
-
import { resolve, dirname, join, basename, isAbsolute } from 'node:path';
|
|
7
|
-
import { Type, type TSchema } from '@sinclair/typebox';
|
|
8
|
-
import yaml from 'js-yaml';
|
|
9
|
-
import { FunctionModule } from './decorator.js';
|
|
10
|
-
import {
|
|
11
|
-
BindingCallableNotFoundError,
|
|
12
|
-
BindingFileInvalidError,
|
|
13
|
-
BindingInvalidTargetError,
|
|
14
|
-
BindingModuleNotFoundError,
|
|
15
|
-
BindingNotCallableError,
|
|
16
|
-
BindingSchemaMissingError,
|
|
17
|
-
} from './errors.js';
|
|
18
|
-
import type { Registry } from './registry/registry.js';
|
|
19
|
-
import { jsonSchemaToTypeBox } from './schema/loader.js';
|
|
20
|
-
|
|
21
|
-
export class BindingLoader {
|
|
22
|
-
async loadBindings(filePath: string, registry: Registry): Promise<FunctionModule[]> {
|
|
23
|
-
const bindingFileDir = dirname(filePath);
|
|
24
|
-
|
|
25
|
-
let content: string;
|
|
26
|
-
try {
|
|
27
|
-
content = readFileSync(filePath, 'utf-8');
|
|
28
|
-
} catch (e) {
|
|
29
|
-
throw new BindingFileInvalidError(filePath, String(e));
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
let data: unknown;
|
|
33
|
-
try {
|
|
34
|
-
data = yaml.load(content);
|
|
35
|
-
} catch (e) {
|
|
36
|
-
throw new BindingFileInvalidError(filePath, `YAML parse error: ${e}`);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (data === null || data === undefined) {
|
|
40
|
-
throw new BindingFileInvalidError(filePath, 'File is empty');
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const dataObj = data as Record<string, unknown>;
|
|
44
|
-
if (!('bindings' in dataObj)) {
|
|
45
|
-
throw new BindingFileInvalidError(filePath, "Missing 'bindings' key");
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const bindings = dataObj['bindings'];
|
|
49
|
-
if (!Array.isArray(bindings)) {
|
|
50
|
-
throw new BindingFileInvalidError(filePath, "'bindings' must be a list");
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const results: FunctionModule[] = [];
|
|
54
|
-
for (const entry of bindings) {
|
|
55
|
-
const entryObj = entry as Record<string, unknown>;
|
|
56
|
-
if (!('module_id' in entryObj)) {
|
|
57
|
-
throw new BindingFileInvalidError(filePath, "Binding entry missing 'module_id'");
|
|
58
|
-
}
|
|
59
|
-
if (!('target' in entryObj)) {
|
|
60
|
-
throw new BindingFileInvalidError(filePath, "Binding entry missing 'target'");
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const fm = await this._createModuleFromBinding(entryObj, bindingFileDir);
|
|
64
|
-
registry.register(entryObj['module_id'] as string, fm);
|
|
65
|
-
results.push(fm);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return results;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async loadBindingDir(
|
|
72
|
-
dirPath: string,
|
|
73
|
-
registry: Registry,
|
|
74
|
-
pattern: string = '*.binding.yaml',
|
|
75
|
-
): Promise<FunctionModule[]> {
|
|
76
|
-
if (!existsSync(dirPath) || !statSync(dirPath).isDirectory()) {
|
|
77
|
-
throw new BindingFileInvalidError(dirPath, 'Directory does not exist');
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const files = readdirSync(dirPath)
|
|
81
|
-
.filter((f) => {
|
|
82
|
-
// Simple glob matching for *.binding.yaml
|
|
83
|
-
const suffix = pattern.replace('*', '');
|
|
84
|
-
return f.endsWith(suffix);
|
|
85
|
-
})
|
|
86
|
-
.sort();
|
|
87
|
-
|
|
88
|
-
const results: FunctionModule[] = [];
|
|
89
|
-
for (const f of files) {
|
|
90
|
-
const fms = await this.loadBindings(join(dirPath, f), registry);
|
|
91
|
-
results.push(...fms);
|
|
92
|
-
}
|
|
93
|
-
return results;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
async resolveTarget(targetString: string): Promise<(...args: unknown[]) => unknown> {
|
|
97
|
-
if (!targetString.includes(':')) {
|
|
98
|
-
throw new BindingInvalidTargetError(targetString);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const [modulePath, callableName] = targetString.split(':', 2);
|
|
102
|
-
|
|
103
|
-
if (modulePath.includes('..')) {
|
|
104
|
-
throw new BindingInvalidTargetError(
|
|
105
|
-
`Module path '${modulePath}' must not contain '..' segments`,
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (modulePath.startsWith('file:')) {
|
|
110
|
-
throw new BindingInvalidTargetError(
|
|
111
|
-
`Module path '${modulePath}' must not use file: URLs`,
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
let mod: Record<string, unknown>;
|
|
116
|
-
try {
|
|
117
|
-
mod = await import(modulePath);
|
|
118
|
-
} catch (e) {
|
|
119
|
-
throw new BindingModuleNotFoundError(modulePath);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (callableName.includes('.')) {
|
|
123
|
-
const [className, methodName] = callableName.split('.', 2);
|
|
124
|
-
const cls = mod[className];
|
|
125
|
-
if (cls == null) {
|
|
126
|
-
throw new BindingCallableNotFoundError(className, modulePath);
|
|
127
|
-
}
|
|
128
|
-
let instance: Record<string, unknown>;
|
|
129
|
-
try {
|
|
130
|
-
instance = new (cls as new () => Record<string, unknown>)();
|
|
131
|
-
} catch {
|
|
132
|
-
throw new BindingCallableNotFoundError(callableName, modulePath);
|
|
133
|
-
}
|
|
134
|
-
const method = instance[methodName];
|
|
135
|
-
if (method == null) {
|
|
136
|
-
throw new BindingCallableNotFoundError(callableName, modulePath);
|
|
137
|
-
}
|
|
138
|
-
if (typeof method !== 'function') {
|
|
139
|
-
throw new BindingNotCallableError(targetString);
|
|
140
|
-
}
|
|
141
|
-
return method.bind(instance) as (...args: unknown[]) => unknown;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const result = mod[callableName];
|
|
145
|
-
if (result == null) {
|
|
146
|
-
throw new BindingCallableNotFoundError(callableName, modulePath);
|
|
147
|
-
}
|
|
148
|
-
if (typeof result !== 'function') {
|
|
149
|
-
throw new BindingNotCallableError(targetString);
|
|
150
|
-
}
|
|
151
|
-
return result as (...args: unknown[]) => unknown;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
private async _createModuleFromBinding(
|
|
155
|
-
binding: Record<string, unknown>,
|
|
156
|
-
bindingFileDir: string,
|
|
157
|
-
): Promise<FunctionModule> {
|
|
158
|
-
const func = await this.resolveTarget(binding['target'] as string);
|
|
159
|
-
const moduleId = binding['module_id'] as string;
|
|
160
|
-
|
|
161
|
-
let inputSchema: TSchema;
|
|
162
|
-
let outputSchema: TSchema;
|
|
163
|
-
|
|
164
|
-
if ('input_schema' in binding || 'output_schema' in binding) {
|
|
165
|
-
const inputSchemaDict = (binding['input_schema'] as Record<string, unknown>) ?? {};
|
|
166
|
-
const outputSchemaDict = (binding['output_schema'] as Record<string, unknown>) ?? {};
|
|
167
|
-
inputSchema = jsonSchemaToTypeBox(inputSchemaDict);
|
|
168
|
-
outputSchema = jsonSchemaToTypeBox(outputSchemaDict);
|
|
169
|
-
} else if ('schema_ref' in binding) {
|
|
170
|
-
const refPath = resolve(bindingFileDir, binding['schema_ref'] as string);
|
|
171
|
-
if (!existsSync(refPath)) {
|
|
172
|
-
throw new BindingFileInvalidError(refPath, 'Schema reference file not found');
|
|
173
|
-
}
|
|
174
|
-
let refData: Record<string, unknown>;
|
|
175
|
-
try {
|
|
176
|
-
refData = (yaml.load(readFileSync(refPath, 'utf-8')) as Record<string, unknown>) ?? {};
|
|
177
|
-
} catch (e) {
|
|
178
|
-
throw new BindingFileInvalidError(refPath, `YAML parse error: ${e}`);
|
|
179
|
-
}
|
|
180
|
-
inputSchema = jsonSchemaToTypeBox(
|
|
181
|
-
(refData['input_schema'] as Record<string, unknown>) ?? {},
|
|
182
|
-
);
|
|
183
|
-
outputSchema = jsonSchemaToTypeBox(
|
|
184
|
-
(refData['output_schema'] as Record<string, unknown>) ?? {},
|
|
185
|
-
);
|
|
186
|
-
} else {
|
|
187
|
-
// No schema, use permissive
|
|
188
|
-
inputSchema = Type.Record(Type.String(), Type.Unknown());
|
|
189
|
-
outputSchema = Type.Record(Type.String(), Type.Unknown());
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
return new FunctionModule({
|
|
193
|
-
execute: async (inputs, context) => {
|
|
194
|
-
const result = await func(inputs, context);
|
|
195
|
-
if (result === null || result === undefined) return {};
|
|
196
|
-
if (typeof result === 'object' && !Array.isArray(result)) return result as Record<string, unknown>;
|
|
197
|
-
return { result };
|
|
198
|
-
},
|
|
199
|
-
moduleId,
|
|
200
|
-
inputSchema,
|
|
201
|
-
outputSchema,
|
|
202
|
-
description: (binding['description'] as string) ?? undefined,
|
|
203
|
-
tags: (binding['tags'] as string[]) ?? null,
|
|
204
|
-
version: (binding['version'] as string) ?? '1.0.0',
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
}
|
package/src/cancel.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Cooperative cancellation support for apcore module execution.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export class ExecutionCancelledError extends Error {
|
|
6
|
-
constructor(message: string = "Execution was cancelled") {
|
|
7
|
-
super(message);
|
|
8
|
-
this.name = "ExecutionCancelledError";
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export class CancelToken {
|
|
13
|
-
private _cancelled: boolean = false;
|
|
14
|
-
|
|
15
|
-
get isCancelled(): boolean {
|
|
16
|
-
return this._cancelled;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
cancel(): void {
|
|
20
|
-
this._cancelled = true;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
check(): void {
|
|
24
|
-
if (this._cancelled) {
|
|
25
|
-
throw new ExecutionCancelledError();
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
reset(): void {
|
|
30
|
-
this._cancelled = false;
|
|
31
|
-
}
|
|
32
|
-
}
|