beddel 0.1.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 +297 -0
- package/dist/agents/agentRegistry.d.ts +68 -0
- package/dist/agents/agentRegistry.d.ts.map +1 -0
- package/dist/agents/agentRegistry.js +222 -0
- package/dist/agents/agentRegistry.js.map +1 -0
- package/dist/agents/formatter-agent.d.ts +10 -0
- package/dist/agents/formatter-agent.d.ts.map +1 -0
- package/dist/agents/formatter-agent.js +49 -0
- package/dist/agents/formatter-agent.js.map +1 -0
- package/dist/agents/genkit-agent.d.ts +12 -0
- package/dist/agents/genkit-agent.d.ts.map +1 -0
- package/dist/agents/genkit-agent.js +119 -0
- package/dist/agents/genkit-agent.js.map +1 -0
- package/dist/agents/i18n-messages.d.ts +17 -0
- package/dist/agents/i18n-messages.d.ts.map +1 -0
- package/dist/agents/i18n-messages.js +92 -0
- package/dist/agents/i18n-messages.js.map +1 -0
- package/dist/agents/index.d.ts +10 -0
- package/dist/agents/index.d.ts.map +1 -0
- package/dist/agents/index.js +26 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/pipeline.d.ts +15 -0
- package/dist/agents/pipeline.d.ts.map +1 -0
- package/dist/agents/pipeline.js +45 -0
- package/dist/agents/pipeline.js.map +1 -0
- package/dist/agents/schema-factory.d.ts +40 -0
- package/dist/agents/schema-factory.d.ts.map +1 -0
- package/dist/agents/schema-factory.js +121 -0
- package/dist/agents/schema-factory.js.map +1 -0
- package/dist/agents/translation-validators.d.ts +26 -0
- package/dist/agents/translation-validators.d.ts.map +1 -0
- package/dist/agents/translation-validators.js +77 -0
- package/dist/agents/translation-validators.js.map +1 -0
- package/dist/agents/translator-agents.d.ts +184 -0
- package/dist/agents/translator-agents.d.ts.map +1 -0
- package/dist/agents/translator-agents.js +613 -0
- package/dist/agents/translator-agents.js.map +1 -0
- package/dist/agents/types/translation.types.d.ts +100 -0
- package/dist/agents/types/translation.types.d.ts.map +1 -0
- package/dist/agents/types/translation.types.js +3 -0
- package/dist/agents/types/translation.types.js.map +1 -0
- package/dist/agents/validator-agent.d.ts +42 -0
- package/dist/agents/validator-agent.d.ts.map +1 -0
- package/dist/agents/validator-agent.js +122 -0
- package/dist/agents/validator-agent.js.map +1 -0
- package/dist/audit/auditTrail.d.ts +55 -0
- package/dist/audit/auditTrail.d.ts.map +1 -0
- package/dist/audit/auditTrail.js +93 -0
- package/dist/audit/auditTrail.js.map +1 -0
- package/dist/compliance/gdprEngine.d.ts +44 -0
- package/dist/compliance/gdprEngine.d.ts.map +1 -0
- package/dist/compliance/gdprEngine.js +178 -0
- package/dist/compliance/gdprEngine.js.map +1 -0
- package/dist/compliance/lgpdEngine.d.ts +51 -0
- package/dist/compliance/lgpdEngine.d.ts.map +1 -0
- package/dist/compliance/lgpdEngine.js +221 -0
- package/dist/compliance/lgpdEngine.js.map +1 -0
- package/dist/config.d.ts +78 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +77 -0
- package/dist/config.js.map +1 -0
- package/dist/errors.d.ts +17 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +40 -0
- package/dist/errors.js.map +1 -0
- package/dist/firebase/tenantManager.d.ts +84 -0
- package/dist/firebase/tenantManager.d.ts.map +1 -0
- package/dist/firebase/tenantManager.js +378 -0
- package/dist/firebase/tenantManager.js.map +1 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +118 -0
- package/dist/index.js.map +1 -0
- package/dist/integration/secure-yaml-runtime.d.ts +68 -0
- package/dist/integration/secure-yaml-runtime.d.ts.map +1 -0
- package/dist/integration/secure-yaml-runtime.js +245 -0
- package/dist/integration/secure-yaml-runtime.js.map +1 -0
- package/dist/parser/secure-yaml-parser.d.ts +62 -0
- package/dist/parser/secure-yaml-parser.d.ts.map +1 -0
- package/dist/parser/secure-yaml-parser.js +234 -0
- package/dist/parser/secure-yaml-parser.js.map +1 -0
- package/dist/performance/autoscaling.d.ts +100 -0
- package/dist/performance/autoscaling.d.ts.map +1 -0
- package/dist/performance/autoscaling.js +339 -0
- package/dist/performance/autoscaling.js.map +1 -0
- package/dist/performance/benchmark.d.ts +104 -0
- package/dist/performance/benchmark.d.ts.map +1 -0
- package/dist/performance/benchmark.js +514 -0
- package/dist/performance/benchmark.js.map +1 -0
- package/dist/performance/index.d.ts +14 -0
- package/dist/performance/index.d.ts.map +1 -0
- package/dist/performance/index.js +35 -0
- package/dist/performance/index.js.map +1 -0
- package/dist/performance/monitor.d.ts +126 -0
- package/dist/performance/monitor.d.ts.map +1 -0
- package/dist/performance/monitor.js +324 -0
- package/dist/performance/monitor.js.map +1 -0
- package/dist/performance/streaming.d.ts +82 -0
- package/dist/performance/streaming.d.ts.map +1 -0
- package/dist/performance/streaming.js +287 -0
- package/dist/performance/streaming.js.map +1 -0
- package/dist/runtime/audit.d.ts +240 -0
- package/dist/runtime/audit.d.ts.map +1 -0
- package/dist/runtime/audit.js +641 -0
- package/dist/runtime/audit.js.map +1 -0
- package/dist/runtime/declarativeAgentRuntime.d.ts +123 -0
- package/dist/runtime/declarativeAgentRuntime.d.ts.map +1 -0
- package/dist/runtime/declarativeAgentRuntime.js +576 -0
- package/dist/runtime/declarativeAgentRuntime.js.map +1 -0
- package/dist/runtime/isolatedRuntime.d.ts +119 -0
- package/dist/runtime/isolatedRuntime.d.ts.map +1 -0
- package/dist/runtime/isolatedRuntime.js +425 -0
- package/dist/runtime/isolatedRuntime.js.map +1 -0
- package/dist/runtime/schemaCompiler.d.ts +35 -0
- package/dist/runtime/schemaCompiler.d.ts.map +1 -0
- package/dist/runtime/schemaCompiler.js +151 -0
- package/dist/runtime/schemaCompiler.js.map +1 -0
- package/dist/runtime/simpleRuntime.d.ts +57 -0
- package/dist/runtime/simpleRuntime.d.ts.map +1 -0
- package/dist/runtime/simpleRuntime.js +187 -0
- package/dist/runtime/simpleRuntime.js.map +1 -0
- package/dist/security/dashboard.d.ts +89 -0
- package/dist/security/dashboard.d.ts.map +1 -0
- package/dist/security/dashboard.js +300 -0
- package/dist/security/dashboard.js.map +1 -0
- package/dist/security/hardening.d.ts +130 -0
- package/dist/security/hardening.d.ts.map +1 -0
- package/dist/security/hardening.js +414 -0
- package/dist/security/hardening.js.map +1 -0
- package/dist/security/index.d.ts +128 -0
- package/dist/security/index.d.ts.map +1 -0
- package/dist/security/index.js +353 -0
- package/dist/security/index.js.map +1 -0
- package/dist/security/monitor.d.ts +88 -0
- package/dist/security/monitor.d.ts.map +1 -0
- package/dist/security/monitor.js +356 -0
- package/dist/security/monitor.js.map +1 -0
- package/dist/security/scanner.d.ts +104 -0
- package/dist/security/scanner.d.ts.map +1 -0
- package/dist/security/scanner.js +298 -0
- package/dist/security/scanner.js.map +1 -0
- package/dist/security/score.d.ts +150 -0
- package/dist/security/score.d.ts.map +1 -0
- package/dist/security/score.js +983 -0
- package/dist/security/score.js.map +1 -0
- package/dist/security/test-security.d.ts +22 -0
- package/dist/security/test-security.d.ts.map +1 -0
- package/dist/security/test-security.js +154 -0
- package/dist/security/test-security.js.map +1 -0
- package/dist/security/threatDetector.d.ts +39 -0
- package/dist/security/threatDetector.d.ts.map +1 -0
- package/dist/security/threatDetector.js +354 -0
- package/dist/security/threatDetector.js.map +1 -0
- package/dist/security/validation.d.ts +69 -0
- package/dist/security/validation.d.ts.map +1 -0
- package/dist/security/validation.js +286 -0
- package/dist/security/validation.js.map +1 -0
- package/dist/server/api/clientsRoute.d.ts +9 -0
- package/dist/server/api/clientsRoute.d.ts.map +1 -0
- package/dist/server/api/clientsRoute.js +71 -0
- package/dist/server/api/clientsRoute.js.map +1 -0
- package/dist/server/api/endpointsRoute.d.ts +8 -0
- package/dist/server/api/endpointsRoute.d.ts.map +1 -0
- package/dist/server/api/endpointsRoute.js +76 -0
- package/dist/server/api/endpointsRoute.js.map +1 -0
- package/dist/server/api/graphql.d.ts +9 -0
- package/dist/server/api/graphql.d.ts.map +1 -0
- package/dist/server/api/graphql.js +180 -0
- package/dist/server/api/graphql.js.map +1 -0
- package/dist/server/errors.d.ts +19 -0
- package/dist/server/errors.d.ts.map +1 -0
- package/dist/server/errors.js +42 -0
- package/dist/server/errors.js.map +1 -0
- package/dist/server/index.d.ts +7 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +24 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/kvStore.d.ts +27 -0
- package/dist/server/kvStore.d.ts.map +1 -0
- package/dist/server/kvStore.js +128 -0
- package/dist/server/kvStore.js.map +1 -0
- package/dist/server/runtimeSecurity.d.ts +28 -0
- package/dist/server/runtimeSecurity.d.ts.map +1 -0
- package/dist/server/runtimeSecurity.js +85 -0
- package/dist/server/runtimeSecurity.js.map +1 -0
- package/dist/server/types.d.ts +53 -0
- package/dist/server/types.d.ts.map +1 -0
- package/dist/server/types.js +8 -0
- package/dist/server/types.js.map +1 -0
- package/dist/types/executionContext.d.ts +16 -0
- package/dist/types/executionContext.d.ts.map +1 -0
- package/dist/types/executionContext.js +3 -0
- package/dist/types/executionContext.js.map +1 -0
- package/package.json +77 -0
- package/src/agents/agentRegistry.ts +272 -0
- package/src/agents/image-agent.yaml +86 -0
- package/src/agents/joker-agent.yaml +47 -0
- package/src/agents/translator-agent.yaml +80 -0
- package/src/audit/auditTrail.ts +134 -0
- package/src/compliance/gdprEngine.ts +209 -0
- package/src/compliance/lgpdEngine.ts +268 -0
- package/src/config.ts +179 -0
- package/src/errors.ts +35 -0
- package/src/firebase/tenantManager.ts +443 -0
- package/src/index.ts +125 -0
- package/src/integration/secure-yaml-runtime.ts +341 -0
- package/src/parser/secure-yaml-parser.ts +273 -0
- package/src/performance/autoscaling.ts +495 -0
- package/src/performance/benchmark.ts +644 -0
- package/src/performance/index.ts +34 -0
- package/src/performance/monitor.ts +469 -0
- package/src/performance/streaming.ts +317 -0
- package/src/runtime/audit.ts +907 -0
- package/src/runtime/declarativeAgentRuntime.ts +836 -0
- package/src/runtime/isolatedRuntime.ts +572 -0
- package/src/runtime/schemaCompiler.ts +228 -0
- package/src/runtime/simpleRuntime.ts +201 -0
- package/src/security/dashboard.ts +462 -0
- package/src/security/hardening.ts +560 -0
- package/src/security/index.ts +439 -0
- package/src/security/monitor.ts +490 -0
- package/src/security/scanner.ts +368 -0
- package/src/security/score.ts +1138 -0
- package/src/security/threatDetector.ts +481 -0
- package/src/security/validation.ts +365 -0
- package/src/server/api/clientsRoute.ts +92 -0
- package/src/server/api/endpointsRoute.ts +97 -0
- package/src/server/api/graphql.ts +249 -0
- package/src/server/errors.ts +38 -0
- package/src/server/index.ts +6 -0
- package/src/server/kvStore.ts +152 -0
- package/src/server/runtimeSecurity.ts +102 -0
- package/src/server/types.ts +60 -0
- package/src/types/executionContext.ts +16 -0
- package/tools/seed.ts +365 -0
- package/tools/test-endpoints.ts +174 -0
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { z, type ZodIssue, type ZodLiteral, type ZodTypeAny } from "zod";
|
|
3
|
+
|
|
4
|
+
export type DeclarativeSchemaDefinition = {
|
|
5
|
+
type?: string;
|
|
6
|
+
properties?: Record<string, DeclarativeSchemaDefinition>;
|
|
7
|
+
items?: DeclarativeSchemaDefinition;
|
|
8
|
+
required?: string[];
|
|
9
|
+
enum?: Array<string | number | boolean>;
|
|
10
|
+
minLength?: number;
|
|
11
|
+
maxLength?: number;
|
|
12
|
+
minItems?: number;
|
|
13
|
+
maxItems?: number;
|
|
14
|
+
additionalProperties?: boolean;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type DeclarativeSchemaPhase = "input" | "output";
|
|
18
|
+
|
|
19
|
+
export class SchemaCompilationError extends Error {
|
|
20
|
+
constructor(message: string) {
|
|
21
|
+
super(message);
|
|
22
|
+
this.name = "SchemaCompilationError";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class DeclarativeSchemaValidationError extends Error {
|
|
27
|
+
constructor(
|
|
28
|
+
message: string,
|
|
29
|
+
public readonly phase: DeclarativeSchemaPhase,
|
|
30
|
+
public readonly issues: ZodIssue[]
|
|
31
|
+
) {
|
|
32
|
+
super(message);
|
|
33
|
+
this.name = "DeclarativeSchemaValidationError";
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export class DeclarativeSchemaCompiler {
|
|
38
|
+
private readonly cache = new Map<string, ZodTypeAny>();
|
|
39
|
+
|
|
40
|
+
public compile(definition: unknown, path: string): ZodTypeAny {
|
|
41
|
+
const cacheKey = this.createCacheKey(definition, path);
|
|
42
|
+
const cached = this.cache.get(cacheKey);
|
|
43
|
+
if (cached) {
|
|
44
|
+
return cached;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const schema = this.buildSchema(definition, path);
|
|
48
|
+
this.cache.set(cacheKey, schema);
|
|
49
|
+
return schema;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public clear(): void {
|
|
53
|
+
this.cache.clear();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public get size(): number {
|
|
57
|
+
return this.cache.size;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private createCacheKey(definition: unknown, path: string): string {
|
|
61
|
+
const serialized = JSON.stringify(definition) ?? "undefined";
|
|
62
|
+
const signature = createHash("sha256").update(serialized).digest("hex");
|
|
63
|
+
return `${path}:${signature}`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private buildSchema(
|
|
67
|
+
definition: unknown,
|
|
68
|
+
path: string
|
|
69
|
+
): ZodTypeAny {
|
|
70
|
+
if (
|
|
71
|
+
!definition ||
|
|
72
|
+
typeof definition !== "object" ||
|
|
73
|
+
Array.isArray(definition)
|
|
74
|
+
) {
|
|
75
|
+
throw new SchemaCompilationError(
|
|
76
|
+
`Invalid schema at ${path}: expected object definition`
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const typedDefinition = definition as DeclarativeSchemaDefinition;
|
|
81
|
+
if (!typedDefinition.type || typeof typedDefinition.type !== "string") {
|
|
82
|
+
throw new SchemaCompilationError(
|
|
83
|
+
`Schema at ${path} must declare a string 'type'`
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
switch (typedDefinition.type) {
|
|
88
|
+
case "object":
|
|
89
|
+
return this.buildObjectSchema(typedDefinition, path);
|
|
90
|
+
case "array":
|
|
91
|
+
return this.buildArraySchema(typedDefinition, path);
|
|
92
|
+
case "string":
|
|
93
|
+
return this.buildStringSchema(typedDefinition, path);
|
|
94
|
+
case "number":
|
|
95
|
+
return z.number();
|
|
96
|
+
case "integer":
|
|
97
|
+
return z.number().int();
|
|
98
|
+
case "boolean":
|
|
99
|
+
return z.boolean();
|
|
100
|
+
case "any":
|
|
101
|
+
return z.any();
|
|
102
|
+
case "unknown":
|
|
103
|
+
return z.unknown();
|
|
104
|
+
default:
|
|
105
|
+
if (typedDefinition.enum) {
|
|
106
|
+
return this.buildEnumSchema(typedDefinition.enum, path);
|
|
107
|
+
}
|
|
108
|
+
throw new SchemaCompilationError(
|
|
109
|
+
`Unsupported schema type '${typedDefinition.type}' at ${path}`
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private buildObjectSchema(
|
|
115
|
+
definition: DeclarativeSchemaDefinition,
|
|
116
|
+
path: string
|
|
117
|
+
): ZodTypeAny {
|
|
118
|
+
const properties = definition.properties || {};
|
|
119
|
+
if (typeof properties !== "object") {
|
|
120
|
+
throw new SchemaCompilationError(
|
|
121
|
+
`Object schema at ${path} must define 'properties' as an object`
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const requiredFields = new Set(definition.required || []);
|
|
126
|
+
const shape: Record<string, ZodTypeAny> = {};
|
|
127
|
+
|
|
128
|
+
for (const [key, childDefinition] of Object.entries(properties)) {
|
|
129
|
+
const childPath = `${path}.properties.${key}`;
|
|
130
|
+
const childSchema = this.buildSchema(childDefinition, childPath);
|
|
131
|
+
shape[key] = requiredFields.has(key)
|
|
132
|
+
? childSchema
|
|
133
|
+
: childSchema.optional();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
let objectSchema = z.object(shape);
|
|
137
|
+
if (definition.additionalProperties) {
|
|
138
|
+
objectSchema = objectSchema.catchall(z.any());
|
|
139
|
+
} else {
|
|
140
|
+
objectSchema = objectSchema.strict();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return objectSchema;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private buildArraySchema(
|
|
147
|
+
definition: DeclarativeSchemaDefinition,
|
|
148
|
+
path: string
|
|
149
|
+
): ZodTypeAny {
|
|
150
|
+
if (!definition.items) {
|
|
151
|
+
throw new SchemaCompilationError(
|
|
152
|
+
`Array schema at ${path} must define 'items'`
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const itemSchema = this.buildSchema(definition.items, `${path}.items`);
|
|
157
|
+
let arraySchema = z.array(itemSchema);
|
|
158
|
+
|
|
159
|
+
if (typeof definition.minItems === "number") {
|
|
160
|
+
arraySchema = arraySchema.min(definition.minItems);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (typeof definition.maxItems === "number") {
|
|
164
|
+
arraySchema = arraySchema.max(definition.maxItems);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return arraySchema;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
private buildStringSchema(
|
|
171
|
+
definition: DeclarativeSchemaDefinition,
|
|
172
|
+
path: string
|
|
173
|
+
): ZodTypeAny {
|
|
174
|
+
let stringSchema = z.string();
|
|
175
|
+
|
|
176
|
+
if (typeof definition.minLength === "number") {
|
|
177
|
+
stringSchema = stringSchema.min(definition.minLength);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (typeof definition.maxLength === "number") {
|
|
181
|
+
stringSchema = stringSchema.max(definition.maxLength);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (definition.enum) {
|
|
185
|
+
return this.buildEnumSchema(definition.enum, path);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return stringSchema;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private buildEnumSchema(
|
|
192
|
+
values: Array<string | number | boolean>,
|
|
193
|
+
path: string
|
|
194
|
+
): ZodTypeAny {
|
|
195
|
+
if (!Array.isArray(values) || values.length === 0) {
|
|
196
|
+
throw new SchemaCompilationError(
|
|
197
|
+
`Enum at ${path} must be a non-empty array`
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const literals = values.map((value) => {
|
|
202
|
+
if (
|
|
203
|
+
typeof value === "string" ||
|
|
204
|
+
typeof value === "number" ||
|
|
205
|
+
typeof value === "boolean"
|
|
206
|
+
) {
|
|
207
|
+
return z.literal(value);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
throw new SchemaCompilationError(
|
|
211
|
+
`Enum at ${path} only supports string, number, or boolean values`
|
|
212
|
+
);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
if (literals.length === 1) {
|
|
216
|
+
return literals[0];
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const [first, second, ...rest] = literals;
|
|
220
|
+
return z.union(
|
|
221
|
+
[first, second, ...rest] as [
|
|
222
|
+
ZodLiteral<string | number | boolean>,
|
|
223
|
+
ZodLiteral<string | number | boolean>,
|
|
224
|
+
...ZodLiteral<string | number | boolean>[]
|
|
225
|
+
]
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Simple Isolated Runtime - Isolated VM v5 Implementation
|
|
3
|
+
* Provides ultra-secure isolated execution environment with zero-trust architecture
|
|
4
|
+
* Simplified version with core functionality
|
|
5
|
+
*/
|
|
6
|
+
import * as ivm from "isolated-vm";
|
|
7
|
+
import { runtimeConfig, securityProfiles, RuntimeConfig } from "../config";
|
|
8
|
+
|
|
9
|
+
export interface ExecutionResult<T = any> {
|
|
10
|
+
success: boolean;
|
|
11
|
+
result?: T;
|
|
12
|
+
error?: string;
|
|
13
|
+
executionTime: number;
|
|
14
|
+
memoryUsed: number;
|
|
15
|
+
timestamp: Date;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ExecutionOptions {
|
|
19
|
+
code: string;
|
|
20
|
+
context?: Record<string, any>;
|
|
21
|
+
securityProfile?: string;
|
|
22
|
+
timeout?: number;
|
|
23
|
+
memoryLimit?: number;
|
|
24
|
+
tenantId?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class IsolatedRuntimeError extends Error {
|
|
28
|
+
constructor(message: string, public readonly code: string) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.name = "IsolatedRuntimeError";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Simple Isolated Runtime Manager
|
|
36
|
+
* Provides basic isolated execution functionality
|
|
37
|
+
*/
|
|
38
|
+
export class SimpleIsolatedRuntimeManager {
|
|
39
|
+
private metrics: Map<string, number[]> = new Map();
|
|
40
|
+
|
|
41
|
+
constructor(private config: RuntimeConfig = runtimeConfig) {}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Execute code in isolated environment
|
|
45
|
+
*/
|
|
46
|
+
public async execute<T = any>(
|
|
47
|
+
options: ExecutionOptions
|
|
48
|
+
): Promise<ExecutionResult<T>> {
|
|
49
|
+
const startTime = Date.now();
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
// Validate input
|
|
53
|
+
this.validateExecutionOptions(options);
|
|
54
|
+
|
|
55
|
+
// Get security profile
|
|
56
|
+
const profileName =
|
|
57
|
+
options.securityProfile || this.config.defaultSecurityProfile;
|
|
58
|
+
const securityProfile = securityProfiles[profileName];
|
|
59
|
+
|
|
60
|
+
// Create isolated environment
|
|
61
|
+
const result = await this.executeInIsolate<T>(options, securityProfile);
|
|
62
|
+
|
|
63
|
+
const executionTime = Date.now() - startTime;
|
|
64
|
+
result.executionTime = executionTime;
|
|
65
|
+
|
|
66
|
+
this.updateMetrics("executionTime", executionTime);
|
|
67
|
+
this.updateMetrics("successRate", result.success ? 1 : 0);
|
|
68
|
+
|
|
69
|
+
return result;
|
|
70
|
+
} catch (error) {
|
|
71
|
+
const executionTime = Date.now() - startTime;
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
success: false,
|
|
75
|
+
error: error instanceof Error ? error.message : String(error),
|
|
76
|
+
executionTime,
|
|
77
|
+
memoryUsed: 0,
|
|
78
|
+
timestamp: new Date(),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Execute code in isolated context
|
|
85
|
+
*/
|
|
86
|
+
private async executeInIsolate<T>(
|
|
87
|
+
options: ExecutionOptions,
|
|
88
|
+
securityProfile: any
|
|
89
|
+
): Promise<ExecutionResult<T>> {
|
|
90
|
+
const startTime = Date.now();
|
|
91
|
+
|
|
92
|
+
try {
|
|
93
|
+
// Create isolate with memory limit
|
|
94
|
+
const isolate = new ivm.Isolate({
|
|
95
|
+
memoryLimit: securityProfile.memoryLimit,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// Create context
|
|
99
|
+
const context = await isolate.createContext();
|
|
100
|
+
|
|
101
|
+
try {
|
|
102
|
+
// Setup execution
|
|
103
|
+
const script = await isolate.compileScript(options.code);
|
|
104
|
+
|
|
105
|
+
// Execute script
|
|
106
|
+
const result = await script.run(context, {
|
|
107
|
+
timeout: options.timeout || securityProfile.timeout,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Get memory usage
|
|
111
|
+
const memoryUsed = await this.getMemoryUsage(isolate);
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
success: true,
|
|
115
|
+
result: result as T,
|
|
116
|
+
executionTime: Date.now() - startTime,
|
|
117
|
+
memoryUsed,
|
|
118
|
+
timestamp: new Date(),
|
|
119
|
+
};
|
|
120
|
+
} finally {
|
|
121
|
+
// Always dispose isolate
|
|
122
|
+
isolate.dispose();
|
|
123
|
+
}
|
|
124
|
+
} catch (error) {
|
|
125
|
+
return {
|
|
126
|
+
success: false,
|
|
127
|
+
error: error instanceof Error ? error.message : String(error),
|
|
128
|
+
executionTime: Date.now() - startTime,
|
|
129
|
+
memoryUsed: 0,
|
|
130
|
+
timestamp: new Date(),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Get memory usage for isolate
|
|
137
|
+
*/
|
|
138
|
+
private async getMemoryUsage(isolate: ivm.Isolate): Promise<number> {
|
|
139
|
+
try {
|
|
140
|
+
const stats = await isolate.getHeapStatistics();
|
|
141
|
+
return (stats.used_heap_size || 0) / (1024 * 1024); // MB
|
|
142
|
+
} catch (error) {
|
|
143
|
+
return 0;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Validate execution options
|
|
149
|
+
*/
|
|
150
|
+
private validateExecutionOptions(options: ExecutionOptions): void {
|
|
151
|
+
if (!options.code || typeof options.code !== "string") {
|
|
152
|
+
throw new IsolatedRuntimeError(
|
|
153
|
+
"Code must be a non-empty string",
|
|
154
|
+
"INVALID_CODE"
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (options.code.length > 1024 * 1024) {
|
|
159
|
+
throw new IsolatedRuntimeError(
|
|
160
|
+
"Code exceeds maximum size limit (1MB)",
|
|
161
|
+
"CODE_TOO_LARGE"
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const memoryLimit = options.memoryLimit || this.config.memoryLimit;
|
|
166
|
+
if (memoryLimit > 8) {
|
|
167
|
+
throw new IsolatedRuntimeError(
|
|
168
|
+
"Memory limit exceeds maximum allowed (8MB)",
|
|
169
|
+
"MEMORY_LIMIT_EXCEEDED"
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Update metrics tracking
|
|
176
|
+
*/
|
|
177
|
+
private updateMetrics(metric: string, value: number): void {
|
|
178
|
+
if (!this.metrics.has(metric)) {
|
|
179
|
+
this.metrics.set(metric, []);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const values = this.metrics.get(metric)!;
|
|
183
|
+
values.push(value);
|
|
184
|
+
|
|
185
|
+
// Keep only last 100 values
|
|
186
|
+
if (values.length > 100) {
|
|
187
|
+
values.shift();
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Get current metrics
|
|
193
|
+
*/
|
|
194
|
+
public getMetrics(): Record<string, number[]> {
|
|
195
|
+
return Object.fromEntries(this.metrics);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Singleton instance
|
|
200
|
+
export const runtimeManager = new SimpleIsolatedRuntimeManager();
|
|
201
|
+
export default SimpleIsolatedRuntimeManager;
|