scriptable 0.0.6 → 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.
Files changed (40) hide show
  1. package/.claude/settings.local.json +10 -0
  2. package/README.md +139 -0
  3. package/dist/binary-ops.d.ts +2 -0
  4. package/dist/binary-ops.js +16 -0
  5. package/dist/builtin-functions.d.ts +3 -0
  6. package/dist/builtin-functions.js +151 -0
  7. package/dist/environment.d.ts +16 -0
  8. package/dist/environment.js +111 -0
  9. package/dist/evaluate.d.ts +5 -0
  10. package/dist/evaluate.js +232 -0
  11. package/dist/index.d.ts +13 -0
  12. package/dist/index.js +26 -0
  13. package/dist/logic-ops.d.ts +2 -0
  14. package/dist/logic-ops.js +5 -0
  15. package/package.json +17 -50
  16. package/src/__tests__/{break-out.js → break-out.test.ts} +3 -14
  17. package/src/__tests__/{builtin-functions-date.js → builtin-functions-date.test.ts} +30 -11
  18. package/src/__tests__/{builtin-functions.js → builtin-functions.test.ts} +2 -3
  19. package/src/__tests__/{expressions.js → expressions.test.ts} +2 -3
  20. package/src/__tests__/{functions.js → functions.test.ts} +7 -8
  21. package/src/__tests__/{logic.js → logic.test.ts} +2 -3
  22. package/src/__tests__/new-features.test.ts +158 -0
  23. package/src/__tests__/security.test.ts +399 -0
  24. package/src/__tests__/{variables.js → variables.test.ts} +3 -6
  25. package/src/binary-ops.ts +18 -0
  26. package/src/builtin-functions.ts +177 -0
  27. package/src/environment.ts +132 -0
  28. package/src/evaluate.ts +275 -0
  29. package/src/index.ts +42 -0
  30. package/src/logic-ops.ts +6 -0
  31. package/tsconfig.json +16 -0
  32. package/.npmignore +0 -1
  33. package/src/__tests__/index.js +0 -85
  34. package/src/binary-ops.js +0 -13
  35. package/src/builtin-functions.js +0 -58
  36. package/src/environment.js +0 -73
  37. package/src/evaluate.js +0 -174
  38. package/src/index.js +0 -26
  39. package/src/logic-ops.js +0 -6
  40. package/yarn.lock +0 -2632
@@ -0,0 +1,13 @@
1
+ import type { FunctionMap } from "./builtin-functions.js";
2
+ import Environment from "./environment.js";
3
+ export interface RunnerOptions {
4
+ functions?: FunctionMap;
5
+ maxSteps?: number;
6
+ }
7
+ export interface RunResult {
8
+ result: any;
9
+ env: Record<string, any>;
10
+ }
11
+ export default function runner({ functions, maxSteps }?: RunnerOptions): (code: string, env?: Record<string, any>, debug?: boolean) => RunResult;
12
+ export { Environment };
13
+ export type { FunctionMap };
package/dist/index.js ADDED
@@ -0,0 +1,26 @@
1
+ import * as acorn from "acorn";
2
+ import evaluate, { resetStepCounter } from "./evaluate.js";
3
+ import builtinFuncs from "./builtin-functions.js";
4
+ import Environment from "./environment.js";
5
+ function parse(code) {
6
+ return acorn.parse(code, {
7
+ ecmaVersion: "latest",
8
+ sourceType: "script",
9
+ locations: true,
10
+ });
11
+ }
12
+ const DEFAULT_MAX_STEPS = 100000;
13
+ export default function runner({ functions = builtinFuncs, maxSteps = DEFAULT_MAX_STEPS } = {}) {
14
+ return (code, env = {}, debug) => {
15
+ const ast = parse(code);
16
+ debug && console.log(JSON.stringify(ast.body, null, 2));
17
+ resetStepCounter(maxSteps);
18
+ const globalEnv = new Environment();
19
+ Object.keys(env).forEach((key) => {
20
+ globalEnv.def(key, env[key]);
21
+ });
22
+ const result = evaluate(ast, globalEnv, functions);
23
+ return { result, env: globalEnv.vars };
24
+ };
25
+ }
26
+ export { Environment };
@@ -0,0 +1,2 @@
1
+ declare const logicOps: Record<string, (l: unknown, r: unknown) => unknown>;
2
+ export default logicOps;
@@ -0,0 +1,5 @@
1
+ const logicOps = {
2
+ "||": (l, r) => l || r,
3
+ "&&": (l, r) => l && r,
4
+ };
5
+ export default logicOps;
package/package.json CHANGED
@@ -1,64 +1,31 @@
1
1
  {
2
2
  "name": "scriptable",
3
- "version": "0.0.6",
4
- "main": "src/index.js",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js"
11
+ }
12
+ },
5
13
  "author": "jason@lecstor.com",
6
14
  "license": "MIT",
7
- "devDependencies": {
8
- "eslint": "^4.4.1",
9
- "eslint-config-prettier": "^2.3.0",
10
- "eslint-config-standard": "^10.2.1",
11
- "eslint-plugin-import": "^2.7.0",
12
- "eslint-plugin-node": "^5.1.1",
13
- "eslint-plugin-promise": "^3.5.0",
14
- "eslint-plugin-standard": "^3.0.1",
15
- "jest": "^20.0.4"
16
- },
17
15
  "repository": {
18
16
  "type": "git",
19
17
  "url": "https://github.com/lecstor/scriptable.git"
20
18
  },
21
19
  "scripts": {
22
- "test": "jest"
20
+ "build": "tsc",
21
+ "test": "vitest run",
22
+ "test:watch": "vitest"
23
23
  },
24
24
  "dependencies": {
25
- "babylon": "^6.18.0",
26
- "lodash": "^4.17.4",
27
- "moment": "^2.18.1",
28
- "moment-timezone": "^0.5.13"
25
+ "acorn": "^8.14.1"
29
26
  },
30
- "eslintConfig": {
31
- "extends": [
32
- "standard",
33
- "prettier"
34
- ],
35
- "env": {
36
- "jest": true
37
- },
38
- "rules": {
39
- "curly": [
40
- "error",
41
- "all"
42
- ],
43
- "brace-style": [
44
- "error",
45
- "1tbs"
46
- ],
47
- "semi": [
48
- "error",
49
- "always",
50
- {
51
- "omitLastInOneLineBlock": true
52
- }
53
- ]
54
- }
55
- },
56
- "jest": {
57
- "moduleFileExtensions": [
58
- "mjs",
59
- "js"
60
- ],
61
- "testEnvironment": "node",
62
- "verbose": true
27
+ "devDependencies": {
28
+ "typescript": "^5.7.0",
29
+ "vitest": "^3.1.0"
63
30
  }
64
31
  }
@@ -1,24 +1,20 @@
1
- const runner = require("../");
2
-
3
- const DEBUG = true;
1
+ import { describe, it, expect } from "vitest";
2
+ import runner from "../index.js";
4
3
 
5
4
  const run = runner();
6
5
 
7
6
  /*
8
7
  * https://blog.liftsecurity.io/2015/04/27/when-this-is-really-that/
9
- *
10
8
  */
11
9
 
12
10
  describe("try to get require", () => {
13
11
  it("this.constructor.constructor", () => {
14
- // https://gist.github.com/domenic/d15dfd8f06ae5d1109b0
15
12
  const code = `bar = this.constructor.constructor`;
16
13
  const env = {};
17
14
  expect(() => run(code, env)).toThrow("Cannot evaluate ThisExpression");
18
15
  });
19
16
 
20
17
  it("object.constructor", () => {
21
- // https://gist.github.com/domenic/d15dfd8f06ae5d1109b0
22
18
  const code = `
23
19
  bar = { any: "thing" };
24
20
  func = bar.constructor;
@@ -27,12 +23,11 @@ describe("try to get require", () => {
27
23
  `;
28
24
  const env = {};
29
25
  expect(() => run(code, env)).toThrow(
30
- new TypeError("func2 is not a function (5:16)")
26
+ "Access to property 'constructor' is not allowed"
31
27
  );
32
28
  });
33
29
 
34
30
  it("arguments.callee.caller", () => {
35
- // https://gist.github.com/bjoerge/2889278
36
31
  const code = `
37
32
  function getFunc () {
38
33
  return arguments.callee.caller.arguments.callee.caller.arguments.callee.caller.arguments.callee.arguments.callee.caller;
@@ -41,12 +36,6 @@ describe("try to get require", () => {
41
36
  func.call(func, "var fs = process.mainModule.require('fs');");
42
37
  `;
43
38
  const env = {};
44
- // try {
45
- // run(code, env, DEBUG);
46
- // } catch (err) {
47
- // console.error(err);
48
- // }
49
- // console.log(env.args);
50
39
  expect(() => run(code, env)).toThrow(
51
40
  new ReferenceError(`"arguments" is not defined (3:15)`)
52
41
  );
@@ -1,33 +1,32 @@
1
- const runner = require("../");
1
+ import { describe, it, expect } from "vitest";
2
+ import runner from "../index.js";
2
3
 
3
4
  const run = runner();
4
5
 
5
- const DEBUG = true;
6
-
7
6
  describe("builtin functions", () => {
8
7
  describe("date", () => {
9
- it("formats an iso8610 timestamp (+00:00 -> +00:00)", () => {
8
+ it("formats an iso8601 timestamp (+00:00 -> +00:00)", () => {
10
9
  const code = `formatDate("2017-08-14T11:08:19+00:00", "MMM D YYYY h:mma z")`;
11
10
  const { result } = run(code);
12
11
  expect(result).toEqual("Aug 14 2017 11:08am UTC");
13
12
  });
14
13
 
15
- it("formats an iso8610 timestamp (+10:00 -> +00:00)", () => {
14
+ it("formats an iso8601 timestamp (+10:00 -> +00:00)", () => {
16
15
  const code = `formatDate("2017-08-14T11:08:19+10:00", "MMM D YYYY h:mma z")`;
17
16
  const { result } = run(code);
18
17
  expect(result).toEqual("Aug 14 2017 1:08am UTC");
19
18
  });
20
19
 
21
- it("formats an iso8610 timestamp (+10:00 -> Australia/Brisbane)", () => {
20
+ it("formats an iso8601 timestamp (+10:00 -> Australia/Brisbane)", () => {
22
21
  const code = `formatDate("2017-08-14T11:08:19+10:00", "MMM D YYYY h:mma z", "Australia/Brisbane")`;
23
22
  const { result } = run(code);
24
- expect(result).toEqual("Aug 14 2017 11:08am AEST");
23
+ expect(result).toMatch(/^Aug 14 2017 11:08am (AEST|GMT\+10)$/);
25
24
  });
26
25
 
27
- it("formats an iso8610 timestamp (+00:00 -> Australia/Brisbane)", () => {
26
+ it("formats an iso8601 timestamp (+00:00 -> Australia/Brisbane)", () => {
28
27
  const code = `formatDate("2017-08-14T11:08:19+00:00", "MMM D YYYY h:mma z", "Australia/Brisbane")`;
29
28
  const { result } = run(code);
30
- expect(result).toEqual("Aug 14 2017 9:08pm AEST");
29
+ expect(result).toMatch(/^Aug 14 2017 9:08pm (AEST|GMT\+10)$/);
31
30
  });
32
31
 
33
32
  it("formats a Unix timestamp (+00:00 -> +00:00)", () => {
@@ -39,12 +38,12 @@ describe("builtin functions", () => {
39
38
  it("formats a Unix timestamp (+00:00 -> Australia/Brisbane)", () => {
40
39
  const code = `formatDate("1403454068850", "MMM D YYYY h:mma z", "Australia/Brisbane")`;
41
40
  const { result } = run(code);
42
- expect(result).toEqual("Jun 23 2014 2:21am AEST");
41
+ expect(result).toMatch(/^Jun 23 2014 2:21am (AEST|GMT\+10)$/);
43
42
  });
44
43
  });
45
44
 
46
45
  describe("func that wraps formatDate", () => {
47
- it("wraps formatData", () => {
46
+ it("wraps formatDate", () => {
48
47
  const code = `
49
48
  Date = (date) => formatDate(date, "MMM D YYYY h:mma z", customer.tz);
50
49
  Date("2017-08-27T09:00:57.730+10:00");
@@ -53,4 +52,24 @@ describe("builtin functions", () => {
53
52
  expect(result).toEqual("Aug 26 2017 4:00pm PDT");
54
53
  });
55
54
  });
55
+
56
+ describe("additional format tokens", () => {
57
+ it("24-hour format", () => {
58
+ const code = `formatDate("2017-08-14T15:30:45+00:00", "HH:mm:ss", "UTC")`;
59
+ const { result } = run(code);
60
+ expect(result).toEqual("15:30:45");
61
+ });
62
+
63
+ it("full month name and padded day", () => {
64
+ const code = `formatDate("2017-01-05T10:00:00+00:00", "MMMM DD, YYYY")`;
65
+ const { result } = run(code);
66
+ expect(result).toEqual("January 05, 2017");
67
+ });
68
+
69
+ it("2-digit year", () => {
70
+ const code = `formatDate("2017-08-14T11:08:19+00:00", "MM/DD/YY")`;
71
+ const { result } = run(code);
72
+ expect(result).toEqual("08/14/17");
73
+ });
74
+ });
56
75
  });
@@ -1,9 +1,8 @@
1
- const runner = require("../");
1
+ import { describe, it, expect } from "vitest";
2
+ import runner from "../index.js";
2
3
 
3
4
  const run = runner();
4
5
 
5
- const DEBUG = true;
6
-
7
6
  describe("builtin functions", () => {
8
7
  it("concat", () => {
9
8
  const code = `concat(list, "i")`;
@@ -1,6 +1,5 @@
1
- const runner = require("../");
2
-
3
- const DEBUG = true;
1
+ import { describe, it, expect } from "vitest";
2
+ import runner from "../index.js";
4
3
 
5
4
  const run = runner();
6
5
 
@@ -1,6 +1,5 @@
1
- const runner = require("../");
2
-
3
- const DEBUG = true;
1
+ import { describe, it, expect } from "vitest";
2
+ import runner from "../index.js";
4
3
 
5
4
  const run = runner();
6
5
 
@@ -29,11 +28,11 @@ describe("function", () => {
29
28
 
30
29
  it("has access to env", () => {
31
30
  const code = `
32
- Date = (date) => formatDate(date, "MMM D YYYY h:mma z", customer.tz);
33
- Date("2017-08-27T09:00:57.730+10:00");
31
+ greet = (name) => "hello " + name;
32
+ greet(customer.name);
34
33
  `;
35
- const { result } = run(code, { customer: { tz: "America/Los_Angeles" } });
36
- expect(result).toEqual("Aug 26 2017 4:00pm PDT");
34
+ const { result } = run(code, { customer: { name: "Jason" } });
35
+ expect(result).toEqual("hello Jason");
37
36
  });
38
37
 
39
38
  it("can call a builtin with a function as arg", () => {
@@ -44,7 +43,7 @@ describe("function", () => {
44
43
  const { env } = run(code, vars);
45
44
  expect(env).toEqual({
46
45
  prices: [11, 22, 33],
47
- nprices: [11, 22, 33]
46
+ nprices: [11, 22, 33],
48
47
  });
49
48
  });
50
49
 
@@ -1,9 +1,8 @@
1
- const runner = require("../");
1
+ import { describe, it, expect } from "vitest";
2
+ import runner from "../index.js";
2
3
 
3
4
  const run = runner();
4
5
 
5
- const DEBUG = true;
6
-
7
6
  describe("logic", () => {
8
7
  describe("operators", () => {
9
8
  it("gt true", () => {
@@ -0,0 +1,158 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import runner from "../index.js";
3
+
4
+ const run = runner();
5
+
6
+ describe("template literals", () => {
7
+ it("evaluates a simple template literal", () => {
8
+ const code = "result = `hello world`";
9
+ const { env } = run(code, {});
10
+ expect(env.result).toEqual("hello world");
11
+ });
12
+
13
+ it("evaluates template literal with expression", () => {
14
+ const code = "name = \"Jason\"; result = `hello ${name}`";
15
+ const { env } = run(code, {});
16
+ expect(env.result).toEqual("hello Jason");
17
+ });
18
+
19
+ it("evaluates template literal with multiple expressions", () => {
20
+ const code = "a = 1; b = 2; result = `${a} + ${b} = ${a + b}`";
21
+ const { env } = run(code, {});
22
+ expect(env.result).toEqual("1 + 2 = 3");
23
+ });
24
+ });
25
+
26
+ describe("conditional (ternary) expression", () => {
27
+ it("evaluates truthy ternary", () => {
28
+ const code = `result = true ? "yes" : "no"`;
29
+ const { env } = run(code, {});
30
+ expect(env.result).toEqual("yes");
31
+ });
32
+
33
+ it("evaluates falsy ternary", () => {
34
+ const code = `result = false ? "yes" : "no"`;
35
+ const { env } = run(code, {});
36
+ expect(env.result).toEqual("no");
37
+ });
38
+
39
+ it("evaluates ternary with complex test", () => {
40
+ const code = `x = 5; result = x > 3 ? "big" : "small"`;
41
+ const { env } = run(code, {});
42
+ expect(env.result).toEqual("big");
43
+ });
44
+ });
45
+
46
+ describe("spread element", () => {
47
+ it("spreads in array literal", () => {
48
+ const code = `
49
+ a = [1, 2];
50
+ b = [...a, 3, 4];
51
+ `;
52
+ const { env } = run(code, {});
53
+ expect(env.b).toEqual([1, 2, 3, 4]);
54
+ });
55
+
56
+ it("spreads in function call", () => {
57
+ const code = `
58
+ args = [1, 2, 3];
59
+ result = concat(args, ...args);
60
+ `;
61
+ const { env } = run(code, {});
62
+ expect(env.result).toEqual([1, 2, 3, 1, 2, 3]);
63
+ });
64
+
65
+ it("spreads in object literal", () => {
66
+ const code = `
67
+ base = { a: 1, b: 2 };
68
+ result = { ...base, c: 3 };
69
+ `;
70
+ const { env } = run(code, {});
71
+ expect(env.result).toEqual({ a: 1, b: 2, c: 3 });
72
+ });
73
+ });
74
+
75
+ describe("unary expressions", () => {
76
+ it("negation", () => {
77
+ const code = `result = !true`;
78
+ const { env } = run(code, {});
79
+ expect(env.result).toEqual(false);
80
+ });
81
+
82
+ it("unary minus", () => {
83
+ const code = `result = -5`;
84
+ const { env } = run(code, {});
85
+ expect(env.result).toEqual(-5);
86
+ });
87
+
88
+ it("typeof", () => {
89
+ const code = `result = typeof "hello"`;
90
+ const { env } = run(code, {});
91
+ expect(env.result).toEqual("string");
92
+ });
93
+ });
94
+
95
+ describe("const/let declarations with expressions", () => {
96
+ it("const with function call", () => {
97
+ const code = `
98
+ const items = concat([1, 2], 3);
99
+ `;
100
+ const { env } = run(code, {});
101
+ expect(env.items).toEqual([1, 2, 3]);
102
+ });
103
+
104
+ it("let with binary expression", () => {
105
+ const code = `
106
+ let x = 5 + 3;
107
+ `;
108
+ const { env } = run(code, {});
109
+ expect(env.x).toEqual(8);
110
+ });
111
+ });
112
+
113
+ describe("if/else", () => {
114
+ it("evaluates else branch", () => {
115
+ const code = `
116
+ if (false) {
117
+ result = "yes";
118
+ } else {
119
+ result = "no";
120
+ }
121
+ `;
122
+ const { env } = run(code, {});
123
+ expect(env.result).toEqual("no");
124
+ });
125
+
126
+ it("evaluates else-if chain", () => {
127
+ const code = `
128
+ x = 2;
129
+ if (x == 1) {
130
+ result = "one";
131
+ } else if (x == 2) {
132
+ result = "two";
133
+ } else {
134
+ result = "other";
135
+ }
136
+ `;
137
+ const { env } = run(code, {});
138
+ expect(env.result).toEqual("two");
139
+ });
140
+ });
141
+
142
+ describe("custom injected functions", () => {
143
+ it("uses injected functions", () => {
144
+ const run = runner({
145
+ functions: {
146
+ add: (a: number, b: number) => a + b,
147
+ greet: (name: string) => `hello ${name}`,
148
+ },
149
+ });
150
+ const code = `
151
+ const sum = add(1, 2);
152
+ const msg = greet("world");
153
+ `;
154
+ const { env } = run(code, {});
155
+ expect(env.sum).toEqual(3);
156
+ expect(env.msg).toEqual("hello world");
157
+ });
158
+ });