@telorun/starlark 0.1.1

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/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # @telorun/starlark
2
+
3
+ ## 0.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Automated release.
8
+ - Updated dependencies
9
+ - @telorun/sdk@0.2.2
package/LICENSE ADDED
@@ -0,0 +1,17 @@
1
+ # SUSTAINABLE USE LICENSE (Fair-code)
2
+
3
+ Copyright (c) 2026 DiglyAI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to use, copy, modify, and distribute the Software for any purpose—including commercial purposes—subject to the following conditions:
6
+
7
+ 1. ANTI-COMPETITION RESTRICTION: The Software may not be provided to third parties as a managed service, commercial SaaS (Software-as-a-Service), PaaS (Platform-as-a-Service), BaaS (Backend-as-a-Service), or similar offering where the primary value provided to the user is the functionality of the Software itself, without a separate commercial license from the copyright holder.
8
+
9
+ 2. PERMITTED COMMERCIAL USE: You are free to use the Software to build, host, and monetize your own commercial applications, products, and services, provided such use does not violate Clause 1.
10
+
11
+ 3. ATTRIBUTION: This copyright notice and license must be included in all copies or substantial portions of the Software.
12
+
13
+ 4. CONTRIBUTIONS: Contributions to the Software are welcome and encouraged. By contributing, you agree that your contributions may be incorporated into the Software and distributed under this license.
14
+
15
+ 5. DISCLAIMER: The Software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the Software or the use or other dealings in the Software.
16
+
17
+ For commercial licensing, managed hosting exemptions, or enterprise inquiries, please contact DiglyAI.
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@telorun/starlark",
3
+ "version": "0.1.1",
4
+ "type": "module",
5
+ "main": "./src/index.ts",
6
+ "module": "./src/index.ts",
7
+ "exports": {
8
+ ".": "./src/index.ts"
9
+ },
10
+ "dependencies": {
11
+ "ajv": "^8.17.1",
12
+ "starlark-webasm": "^1.0.3",
13
+ "@telorun/sdk": "0.2.2"
14
+ },
15
+ "devDependencies": {
16
+ "@types/node": "^20.0.0"
17
+ }
18
+ }
package/script.ts ADDED
@@ -0,0 +1,142 @@
1
+ import type {
2
+ ControllerContext,
3
+ ResourceContext,
4
+ ResourceInstance,
5
+ ResourceManifest,
6
+ RuntimeResource,
7
+ } from "@telorun/sdk";
8
+ import Ajv, { ErrorObject, ValidateFunction } from "ajv";
9
+ import { initialize } from "starlark-webasm";
10
+
11
+ declare global {
12
+ var run_starlark_code: (code: string, context?: Record<string, any>) => any;
13
+ }
14
+
15
+ type StarlarkScriptResource = RuntimeResource & {
16
+ code?: string;
17
+ inputSchema?: Record<string, any>;
18
+ outputSchema?: Record<string, any>;
19
+ };
20
+
21
+ let initialized = false;
22
+ const ajv = new Ajv({ allErrors: true, strict: false });
23
+ const inputValidators = new Map<string, ValidateFunction>();
24
+ const outputValidators = new Map<string, ValidateFunction>();
25
+
26
+ export async function register(ctx: ControllerContext): Promise<void> {
27
+ if (!initialized) {
28
+ // Suppress the "run_starlark_code has been added to the javascript globals" message
29
+ const originalLog = console.log;
30
+ console.log = () => {};
31
+ try {
32
+ await initialize();
33
+ } finally {
34
+ console.log = originalLog;
35
+ }
36
+ initialized = true;
37
+ }
38
+ }
39
+
40
+ class StarlarkScript implements ResourceInstance {
41
+ private name: string;
42
+ private code: string;
43
+
44
+ constructor(
45
+ readonly ctx: ResourceContext,
46
+ readonly manifest: ResourceManifest,
47
+ ) {
48
+ this.name = manifest.metadata.name;
49
+ this.code = manifest.code || "";
50
+ }
51
+
52
+ async init(): Promise<void> {
53
+ // Starlark code is compiled on-demand during execution
54
+ }
55
+
56
+ async invoke(input: Record<string, any>): Promise<any> {
57
+ this.ctx.validateSchema(input, this.manifest.inputSchema);
58
+ const result = await executeStarlark(this.code, input);
59
+ this.ctx.validateSchema(result, this.manifest.outputSchema);
60
+ return result;
61
+ }
62
+
63
+ async teardown(): Promise<void> {
64
+ inputValidators.delete(`${this.name}:input`);
65
+ outputValidators.delete(`${this.name}:output`);
66
+ }
67
+ }
68
+
69
+ export async function create(
70
+ resource: StarlarkScriptResource,
71
+ ctx: ResourceContext,
72
+ ): Promise<ResourceInstance> {
73
+ const name = resource.metadata.name;
74
+ if (!resource.code) {
75
+ throw new Error(`StarlarkScript "${name}" is missing code`);
76
+ }
77
+
78
+ const instance = new StarlarkScript(ctx, resource);
79
+ await instance.init();
80
+ return instance;
81
+ }
82
+
83
+ async function executeStarlark(code: string, input: any): Promise<any> {
84
+ try {
85
+ const result = globalThis.run_starlark_code(
86
+ `${code}\ndef main():\n return run(${JSON.stringify(input)})\nmain()`,
87
+ );
88
+ if (result.error) {
89
+ throw new Error(result.error);
90
+ }
91
+
92
+ let cleanJson = result.message
93
+ .replace(/'/g, '"')
94
+ .replace(/True/g, "true")
95
+ .replace(/False/g, "false")
96
+ .replace(/None/g, "null");
97
+ const trimmed = cleanJson.trim();
98
+ if (
99
+ (trimmed.startsWith("{") && trimmed.endsWith("}")) ||
100
+ (trimmed.startsWith("[") && trimmed.endsWith("]"))
101
+ ) {
102
+ try {
103
+ return JSON.parse(trimmed);
104
+ } catch {
105
+ throw new Error("Invalid output from Starlark code");
106
+ }
107
+ }
108
+
109
+ return cleanJson;
110
+ } catch (error) {
111
+ throw new Error(
112
+ `StarlarkScript execution failed: ${error instanceof Error ? error.message : String(error)}`,
113
+ );
114
+ }
115
+ }
116
+
117
+ function getValidator(
118
+ cache: Map<string, ValidateFunction>,
119
+ key: string,
120
+ schema: Record<string, any>,
121
+ ): ValidateFunction {
122
+ const existing = cache.get(key);
123
+ if (existing) {
124
+ return existing;
125
+ }
126
+ const compiled = ajv.compile(schema);
127
+ cache.set(key, compiled);
128
+ return compiled;
129
+ }
130
+
131
+ function formatAjvErrors(errors?: ErrorObject[] | null): string {
132
+ if (!errors || errors.length === 0) {
133
+ return "Validation failed";
134
+ }
135
+ return errors
136
+ .map((err) => {
137
+ const path = err.instancePath && err.instancePath.length > 0 ? err.instancePath : "/";
138
+ const message = err.message || "is invalid";
139
+ return `${path} ${message}`;
140
+ })
141
+ .join("; ");
142
+ }
@@ -0,0 +1,4 @@
1
+ declare module "starlark-webasm" {
2
+ export function initialize(): Promise<void>;
3
+ export function exec(code: string, context?: Record<string, any>): Record<string, any>;
4
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "files": [],
4
+ "include": ["src/**/*.ts", "test/**/*.ts"],
5
+ "compilerOptions": {
6
+ "strict": true,
7
+ "esModuleInterop": true
8
+ },
9
+ "references": [
10
+ {
11
+ "path": "tsconfig.lib.json"
12
+ },
13
+ {
14
+ "path": "tsconfig.spec.json"
15
+ }
16
+ ]
17
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src"
6
+ },
7
+ "exclude": ["node_modules", "test", "dist", "**/*spec.ts"],
8
+ "include": ["src/**/*.ts"]
9
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "strict": true,
5
+ "esModuleInterop": true,
6
+ "types": ["node", "jest"]
7
+ },
8
+ "files": [],
9
+ "include": ["test/**/*.ts"]
10
+ }