@telorun/starlark 0.1.5 → 0.1.7
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/script.d.ts +12 -0
- package/dist/script.js +73 -0
- package/package.json +9 -5
- package/src/script.ts +5 -42
- package/tsconfig.json +1 -1
- package/LICENSE +0 -17
package/dist/script.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ControllerContext, ResourceContext, ResourceInstance, RuntimeResource } from "@telorun/sdk";
|
|
2
|
+
declare global {
|
|
3
|
+
var run_starlark_code: (code: string, context?: Record<string, any>) => any;
|
|
4
|
+
}
|
|
5
|
+
type StarlarkScriptResource = RuntimeResource & {
|
|
6
|
+
code?: string;
|
|
7
|
+
inputSchema?: Record<string, any>;
|
|
8
|
+
outputSchema?: Record<string, any>;
|
|
9
|
+
};
|
|
10
|
+
export declare function register(ctx: ControllerContext): Promise<void>;
|
|
11
|
+
export declare function create(resource: StarlarkScriptResource, ctx: ResourceContext): Promise<ResourceInstance>;
|
|
12
|
+
export {};
|
package/dist/script.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { initialize } from "starlark-webasm";
|
|
2
|
+
let initialized = false;
|
|
3
|
+
export async function register(ctx) {
|
|
4
|
+
if (!initialized) {
|
|
5
|
+
// Suppress the "run_starlark_code has been added to the javascript globals" message
|
|
6
|
+
const originalLog = console.log;
|
|
7
|
+
console.log = () => { };
|
|
8
|
+
try {
|
|
9
|
+
await initialize();
|
|
10
|
+
}
|
|
11
|
+
finally {
|
|
12
|
+
console.log = originalLog;
|
|
13
|
+
}
|
|
14
|
+
initialized = true;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
class StarlarkScript {
|
|
18
|
+
ctx;
|
|
19
|
+
manifest;
|
|
20
|
+
name;
|
|
21
|
+
code;
|
|
22
|
+
constructor(ctx, manifest) {
|
|
23
|
+
this.ctx = ctx;
|
|
24
|
+
this.manifest = manifest;
|
|
25
|
+
this.name = manifest.metadata.name;
|
|
26
|
+
this.code = manifest.code || "";
|
|
27
|
+
}
|
|
28
|
+
async init() {
|
|
29
|
+
// Starlark code is compiled on-demand during execution
|
|
30
|
+
}
|
|
31
|
+
async invoke(input) {
|
|
32
|
+
this.ctx.validateSchema(input, this.manifest.inputSchema);
|
|
33
|
+
const result = await executeStarlark(this.code, input);
|
|
34
|
+
this.ctx.validateSchema(result, this.manifest.outputSchema);
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export async function create(resource, ctx) {
|
|
39
|
+
const name = resource.metadata.name;
|
|
40
|
+
if (!resource.code) {
|
|
41
|
+
throw new Error(`StarlarkScript "${name}" is missing code`);
|
|
42
|
+
}
|
|
43
|
+
const instance = new StarlarkScript(ctx, resource);
|
|
44
|
+
await instance.init();
|
|
45
|
+
return instance;
|
|
46
|
+
}
|
|
47
|
+
async function executeStarlark(code, input) {
|
|
48
|
+
try {
|
|
49
|
+
const result = globalThis.run_starlark_code(`${code}\ndef main():\n print(str(run(${JSON.stringify(input)})))`);
|
|
50
|
+
if (result.error) {
|
|
51
|
+
throw new Error(result.error);
|
|
52
|
+
}
|
|
53
|
+
let cleanJson = result.message
|
|
54
|
+
.replace(/'/g, '"')
|
|
55
|
+
.replace(/True/g, "true")
|
|
56
|
+
.replace(/False/g, "false")
|
|
57
|
+
.replace(/None/g, "null");
|
|
58
|
+
const trimmed = cleanJson.trim();
|
|
59
|
+
if ((trimmed.startsWith("{") && trimmed.endsWith("}")) ||
|
|
60
|
+
(trimmed.startsWith("[") && trimmed.endsWith("]"))) {
|
|
61
|
+
try {
|
|
62
|
+
return JSON.parse(trimmed);
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
throw new Error("Invalid output from Starlark code");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return cleanJson;
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
throw new Error(`StarlarkScript execution failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
72
|
+
}
|
|
73
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@telorun/starlark",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"type": "module",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "tsc -p tsconfig.lib.json"
|
|
7
|
+
},
|
|
5
8
|
"exports": {
|
|
6
9
|
"./script": {
|
|
7
10
|
"bun": "./src/script.ts",
|
|
@@ -10,11 +13,12 @@
|
|
|
10
13
|
}
|
|
11
14
|
},
|
|
12
15
|
"dependencies": {
|
|
16
|
+
"@telorun/sdk": "workspace:*",
|
|
13
17
|
"ajv": "^8.17.1",
|
|
14
|
-
"starlark-webasm": "^1.0.3"
|
|
15
|
-
"@telorun/sdk": "0.2.6"
|
|
18
|
+
"starlark-webasm": "^1.0.3"
|
|
16
19
|
},
|
|
17
20
|
"devDependencies": {
|
|
18
|
-
"@types/node": "^20.0.0"
|
|
21
|
+
"@types/node": "^20.0.0",
|
|
22
|
+
"typescript": "^5.0.0"
|
|
19
23
|
}
|
|
20
|
-
}
|
|
24
|
+
}
|
package/src/script.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
ControllerContext,
|
|
3
|
+
ResourceContext,
|
|
4
|
+
ResourceInstance,
|
|
5
|
+
ResourceManifest,
|
|
6
|
+
RuntimeResource,
|
|
7
7
|
} from "@telorun/sdk";
|
|
8
|
-
import Ajv, { ErrorObject, ValidateFunction } from "ajv";
|
|
9
8
|
import { initialize } from "starlark-webasm";
|
|
10
9
|
|
|
11
10
|
declare global {
|
|
@@ -19,10 +18,6 @@ type StarlarkScriptResource = RuntimeResource & {
|
|
|
19
18
|
};
|
|
20
19
|
|
|
21
20
|
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
21
|
export async function register(ctx: ControllerContext): Promise<void> {
|
|
27
22
|
if (!initialized) {
|
|
28
23
|
// Suppress the "run_starlark_code has been added to the javascript globals" message
|
|
@@ -59,11 +54,6 @@ class StarlarkScript implements ResourceInstance {
|
|
|
59
54
|
this.ctx.validateSchema(result, this.manifest.outputSchema);
|
|
60
55
|
return result;
|
|
61
56
|
}
|
|
62
|
-
|
|
63
|
-
async teardown(): Promise<void> {
|
|
64
|
-
inputValidators.delete(`${this.name}:input`);
|
|
65
|
-
outputValidators.delete(`${this.name}:output`);
|
|
66
|
-
}
|
|
67
57
|
}
|
|
68
58
|
|
|
69
59
|
export async function create(
|
|
@@ -113,30 +103,3 @@ async function executeStarlark(code: string, input: any): Promise<any> {
|
|
|
113
103
|
);
|
|
114
104
|
}
|
|
115
105
|
}
|
|
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
|
-
}
|
package/tsconfig.json
CHANGED
package/LICENSE
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
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.
|