@telorun/sdk 0.2.7 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/evaluation-context.d.ts +11 -102
- package/dist/evaluation-context.d.ts.map +1 -1
- package/dist/evaluation-context.js +0 -390
- package/dist/module-context.d.ts +12 -36
- package/dist/module-context.d.ts.map +1 -1
- package/dist/module-context.js +1 -177
- package/package.json +19 -1
- package/src/evaluation-context.ts +34 -461
- package/src/module-context.ts +22 -203
- package/src/execution-context.ts +0 -21
package/dist/module-context.js
CHANGED
|
@@ -1,177 +1 @@
|
|
|
1
|
-
|
|
2
|
-
/** Wraps process.env so that missing keys return null instead of throwing in CEL.
|
|
3
|
-
* cel-js uses Object.hasOwn(obj, key) before accessing obj[key], so we must
|
|
4
|
-
* intercept getOwnPropertyDescriptor to report every string key as "own". */
|
|
5
|
-
function lenientEnv(env) {
|
|
6
|
-
return new Proxy(env, {
|
|
7
|
-
get(target, key) {
|
|
8
|
-
if (typeof key !== "string")
|
|
9
|
-
return target[key];
|
|
10
|
-
return key in target ? (target[key] ?? null) : null;
|
|
11
|
-
},
|
|
12
|
-
has() {
|
|
13
|
-
return true;
|
|
14
|
-
},
|
|
15
|
-
getOwnPropertyDescriptor(target, key) {
|
|
16
|
-
if (typeof key !== "string")
|
|
17
|
-
return Object.getOwnPropertyDescriptor(target, key);
|
|
18
|
-
const value = key in target ? (target[key] ?? null) : null;
|
|
19
|
-
return { configurable: true, enumerable: true, writable: true, value };
|
|
20
|
-
},
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
function collectSecretValues(secrets) {
|
|
24
|
-
const values = new Set();
|
|
25
|
-
for (const value of Object.values(secrets)) {
|
|
26
|
-
if (typeof value === "string" && value.length > 0) {
|
|
27
|
-
values.add(value);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
return values;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Persistent, module-scoped context. Three reserved CEL namespaces:
|
|
34
|
-
* variables, secrets, resources.
|
|
35
|
-
*
|
|
36
|
-
* Unlike the base EvaluationContext, ModuleContext is stateful and mutable:
|
|
37
|
-
* variables/secrets/resources accumulate during multi-pass initialization and
|
|
38
|
-
* the context record is rebuilt on each mutation. Import aliases are tracked
|
|
39
|
-
* here for alias-prefixed kind resolution (e.g. MyImport.Http.Route).
|
|
40
|
-
*
|
|
41
|
-
* Imported modules are surfaced under resources.<alias> alongside local
|
|
42
|
-
* resources — no separate imports namespace needed.
|
|
43
|
-
*/
|
|
44
|
-
export class ModuleContext extends EvaluationContext {
|
|
45
|
-
targets;
|
|
46
|
-
_hostEnv;
|
|
47
|
-
_variables;
|
|
48
|
-
_secrets;
|
|
49
|
-
_resources;
|
|
50
|
-
/** Maps import alias → real module name for kind resolution. */
|
|
51
|
-
importAliases = new Map();
|
|
52
|
-
/** Maps import alias → allowed kind names. Absent entry = unrestricted (e.g. Kernel). */
|
|
53
|
-
importedKinds = new Map();
|
|
54
|
-
constructor(source, variables = {}, secrets = {}, resources = {}, targets = [], createInstance = async () => null, emit, _hostEnv) {
|
|
55
|
-
super(source, {}, createInstance, new Set(), emit);
|
|
56
|
-
this.targets = targets;
|
|
57
|
-
this._hostEnv = _hostEnv;
|
|
58
|
-
this._variables = variables;
|
|
59
|
-
this._secrets = secrets;
|
|
60
|
-
this._resources = resources;
|
|
61
|
-
this._rebuildContext();
|
|
62
|
-
}
|
|
63
|
-
get variables() {
|
|
64
|
-
return this._variables;
|
|
65
|
-
}
|
|
66
|
-
get secrets() {
|
|
67
|
-
return this._secrets;
|
|
68
|
-
}
|
|
69
|
-
get resources() {
|
|
70
|
-
return this._resources;
|
|
71
|
-
}
|
|
72
|
-
setVariables(vars) {
|
|
73
|
-
this._variables = vars;
|
|
74
|
-
this._rebuildContext();
|
|
75
|
-
}
|
|
76
|
-
setTargets(vars) {
|
|
77
|
-
this.targets = vars;
|
|
78
|
-
}
|
|
79
|
-
setSecrets(secrets) {
|
|
80
|
-
this._secrets = secrets;
|
|
81
|
-
this._rebuildContext();
|
|
82
|
-
}
|
|
83
|
-
setResource(name, props) {
|
|
84
|
-
this._resources = { ...this._resources, [name]: props };
|
|
85
|
-
this._rebuildContext();
|
|
86
|
-
}
|
|
87
|
-
onResourceSnapshotted(name, snap) {
|
|
88
|
-
this.setResource(name, snap);
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Register an imported module under the given alias, with the list of kind names
|
|
92
|
-
* it exports. An empty kinds array means no restriction (used for built-ins like Kernel).
|
|
93
|
-
*/
|
|
94
|
-
registerImport(alias, targetModule, kinds) {
|
|
95
|
-
this.importAliases.set(alias, targetModule);
|
|
96
|
-
if (kinds.length > 0) {
|
|
97
|
-
this.importedKinds.set(alias, new Set(kinds));
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
getInstance(name) {
|
|
101
|
-
const entry = this.resourceInstances.get(name);
|
|
102
|
-
if (!entry) {
|
|
103
|
-
throw new Error(`Resource '${name}' not found in module context. Available resources: ${[...this.resourceInstances.keys()].join(", ")}`);
|
|
104
|
-
}
|
|
105
|
-
return entry?.instance;
|
|
106
|
-
}
|
|
107
|
-
getInvocable(name) {
|
|
108
|
-
const instance = this.getInstance(name);
|
|
109
|
-
if (instance &&
|
|
110
|
-
typeof instance === "object" &&
|
|
111
|
-
"invoke" in instance &&
|
|
112
|
-
typeof instance.invoke !== "function") {
|
|
113
|
-
throw new Error(`Resource '${name}' does not have an invoke() method.`);
|
|
114
|
-
}
|
|
115
|
-
return instance;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Resolve a fully-qualified kind like "Http.Server" to its real kind "http-server.Server".
|
|
119
|
-
* Splits on the first dot, looks up the prefix in importAliases, validates against
|
|
120
|
-
* importedKinds (if set), and reconstructs the resolved kind.
|
|
121
|
-
* Throws with a clear message if the alias is unknown or the kind is not exported.
|
|
122
|
-
*/
|
|
123
|
-
resolveKind(kind) {
|
|
124
|
-
const dot = kind.indexOf(".");
|
|
125
|
-
if (dot === -1) {
|
|
126
|
-
throw new Error(`Kind '${kind}' must be fully qualified (e.g. 'Module.KindName')`);
|
|
127
|
-
}
|
|
128
|
-
const prefix = kind.slice(0, dot);
|
|
129
|
-
const suffix = kind.slice(dot + 1);
|
|
130
|
-
const realModule = this.importAliases.get(prefix);
|
|
131
|
-
if (!realModule) {
|
|
132
|
-
const known = [...this.importAliases.keys()].join(", ") || "(none)";
|
|
133
|
-
throw new Error(`Kind '${kind}': no module imported with alias '${prefix}'. Known aliases: ${known}`);
|
|
134
|
-
}
|
|
135
|
-
const allowed = this.importedKinds.get(prefix);
|
|
136
|
-
if (allowed !== undefined && !allowed.has(suffix)) {
|
|
137
|
-
throw new Error(`Kind '${suffix}' is not exported by module '${realModule}' (imported as '${prefix}'). ` +
|
|
138
|
-
`Exported kinds: ${[...allowed].join(", ")}`);
|
|
139
|
-
}
|
|
140
|
-
return `${realModule}.${suffix}`;
|
|
141
|
-
}
|
|
142
|
-
_rebuildContext() {
|
|
143
|
-
this._context = {
|
|
144
|
-
variables: this._variables,
|
|
145
|
-
secrets: this._secrets,
|
|
146
|
-
resources: this._resources,
|
|
147
|
-
...(this._hostEnv ? { env: lenientEnv(this._hostEnv) } : {}),
|
|
148
|
-
};
|
|
149
|
-
this._secretValues = collectSecretValues(this._secrets);
|
|
150
|
-
}
|
|
151
|
-
async invoke(kind, name, inputs) {
|
|
152
|
-
const result = await super.invoke(kind, name, inputs);
|
|
153
|
-
const entry = this.resourceInstances.get(name);
|
|
154
|
-
if (entry && typeof entry.instance.snapshot === "function") {
|
|
155
|
-
const snap = await Promise.resolve(entry.instance.snapshot());
|
|
156
|
-
this.setResource(name, snap);
|
|
157
|
-
}
|
|
158
|
-
return result;
|
|
159
|
-
}
|
|
160
|
-
async run(name) {
|
|
161
|
-
const resource = this.resourceInstances.get(name);
|
|
162
|
-
if (!resource) {
|
|
163
|
-
throw new Error(`Target resource ${name} not found in module context. Available resources: ${[...this.resourceInstances.keys()].join(", ")}`);
|
|
164
|
-
}
|
|
165
|
-
if (typeof resource.instance.run === "function") {
|
|
166
|
-
await resource.instance.run();
|
|
167
|
-
}
|
|
168
|
-
else {
|
|
169
|
-
throw new Error(`Target resource ${name} does not have a run() method.`);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
async runTargets() {
|
|
173
|
-
for (const target of this.targets) {
|
|
174
|
-
await this.run(target);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@telorun/sdk",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.8",
|
|
4
|
+
"description": "Telo SDK - Public API for Telo module authors.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"telo",
|
|
7
|
+
"sdk",
|
|
8
|
+
"runtime",
|
|
9
|
+
"modules"
|
|
10
|
+
],
|
|
11
|
+
"author": "Bartosz Pasiński <bartosz.pasinski@codenet.pl>",
|
|
12
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/telorun/telo.git",
|
|
16
|
+
"directory": "sdk/nodejs"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/telorun/telo#readme",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/telorun/telo/issues"
|
|
21
|
+
},
|
|
4
22
|
"type": "module",
|
|
5
23
|
"main": "./dist/index.js",
|
|
6
24
|
"exports": {
|