scriptable 0.1.1 → 0.2.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/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as acorn from "acorn";
2
- import evaluate, { resetStepCounter } from "./evaluate.js";
2
+ import evaluate from "./evaluate.js";
3
3
  import builtinFuncs from "./builtin-functions.js";
4
- import Environment from "./environment.js";
4
+ import Environment, { Runtime } from "./environment.js";
5
5
  function parse(code) {
6
6
  return acorn.parse(code, {
7
7
  ecmaVersion: "latest",
@@ -10,17 +10,22 @@ function parse(code) {
10
10
  });
11
11
  }
12
12
  const DEFAULT_MAX_STEPS = 100000;
13
- export default function runner({ functions = builtinFuncs, maxSteps = DEFAULT_MAX_STEPS } = {}) {
13
+ const DEFAULT_MAX_CODE_SIZE = 64 * 1024;
14
+ const DEFAULT_MAX_ALLOC_SIZE = 1000000;
15
+ const DEFAULT_MAX_DURATION_MS = 0;
16
+ export default function runner({ functions = builtinFuncs, maxSteps = DEFAULT_MAX_STEPS, maxCodeSize = DEFAULT_MAX_CODE_SIZE, maxAllocSize = DEFAULT_MAX_ALLOC_SIZE, maxDurationMs = DEFAULT_MAX_DURATION_MS, } = {}) {
14
17
  return (code, env = {}, debug) => {
18
+ if (maxCodeSize > 0 && code.length > maxCodeSize) {
19
+ throw new Error(`Source exceeds maxCodeSize (${code.length} > ${maxCodeSize})`);
20
+ }
15
21
  const ast = parse(code);
16
22
  debug && console.log(JSON.stringify(ast.body, null, 2));
17
- resetStepCounter(maxSteps);
18
- const globalEnv = new Environment();
23
+ const globalEnv = new Environment(undefined, new Runtime(maxSteps, maxAllocSize, maxDurationMs));
19
24
  Object.keys(env).forEach((key) => {
20
- globalEnv.def(key, env[key]);
25
+ globalEnv.def([key], env[key]);
21
26
  });
22
27
  const result = evaluate(ast, globalEnv, functions);
23
28
  return { result, env: globalEnv.vars };
24
29
  };
25
30
  }
26
- export { Environment };
31
+ export { Environment, Runtime };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "scriptable",
3
- "version": "0.1.1",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -16,15 +16,19 @@
16
16
  "type": "git",
17
17
  "url": "https://github.com/lecstor/scriptable.git"
18
18
  },
19
+ "files": [
20
+ "dist"
21
+ ],
19
22
  "dependencies": {
20
23
  "acorn": "^8.14.1"
21
24
  },
22
25
  "devDependencies": {
23
26
  "@changesets/cli": "^2.30.0",
24
27
  "typescript": "^5.7.0",
25
- "vitest": "^3.1.0"
28
+ "vitest": "^4.1.4"
26
29
  },
27
30
  "scripts": {
31
+ "prebuild": "rm -rf dist",
28
32
  "build": "tsc",
29
33
  "test": "vitest run",
30
34
  "test:watch": "vitest",
@@ -1,8 +0,0 @@
1
- # Changesets
2
-
3
- Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
4
- with multi-package repos, or single-package repos to help you version and publish your code. You can
5
- find the full documentation for it [in our repository](https://github.com/changesets/changesets).
6
-
7
- We have a quick list of common questions to get you started engaging with this project in
8
- [our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md).
@@ -1,11 +0,0 @@
1
- {
2
- "$schema": "https://unpkg.com/@changesets/config@3.1.3/schema.json",
3
- "changelog": "@changesets/cli/changelog",
4
- "commit": false,
5
- "fixed": [],
6
- "linked": [],
7
- "access": "public",
8
- "baseBranch": "main",
9
- "updateInternalDependencies": "patch",
10
- "ignore": []
11
- }
@@ -1,62 +0,0 @@
1
- name: release
2
-
3
- # Publishes to npm via OIDC trusted publishing — no NPM_TOKEN secret.
4
- #
5
- # Triggered by pushing an annotated tag like `v0.1.1`. The local `pnpm release`
6
- # script handles versioning, committing, tagging, and pushing — the tag push
7
- # is what starts this workflow.
8
- #
9
- # If this workflow file is renamed, update the matching Trusted Publisher
10
- # config at https://www.npmjs.com/package/scriptable/access — the
11
- # "Workflow filename" field takes the basename only (e.g. `release.yml`).
12
-
13
- on:
14
- push:
15
- tags:
16
- - "v*"
17
-
18
- jobs:
19
- publish:
20
- runs-on: ubuntu-latest
21
- permissions:
22
- # id-token: write is required for OIDC trusted publishing to npm.
23
- # This lets `changeset publish` exchange a short-lived GitHub Actions
24
- # token for an npm publish token — no long-lived NPM_TOKEN secret
25
- # ever exists in this repo.
26
- id-token: write
27
- contents: read
28
-
29
- steps:
30
- - uses: actions/checkout@v6
31
-
32
- - uses: pnpm/action-setup@v5
33
- with:
34
- version: 10
35
-
36
- # Node 24, NOT 22. npm trusted publishing requires npm >= 11.5.1, and
37
- # Node 22 LTS is permanently stuck on npm 10.9.x.
38
- - uses: actions/setup-node@v6
39
- with:
40
- node-version: 24
41
- registry-url: https://registry.npmjs.org
42
- cache: pnpm
43
-
44
- - name: Install dependencies
45
- run: pnpm install --frozen-lockfile --ignore-scripts
46
-
47
- - name: Test
48
- run: pnpm test
49
-
50
- - name: Build
51
- run: pnpm build
52
-
53
- # --no-git-tag: the tag already exists (we pushed it from the
54
- # maintainer's laptop — that tag push is what triggered this
55
- # workflow), so skip changesets' tag creation step.
56
- # NPM_CONFIG_PROVENANCE=true: stamps the package with an npm
57
- # provenance attestation so the npmjs.com page shows "Built
58
- # and signed on GitHub Actions".
59
- - name: Publish to npm
60
- run: pnpm exec changeset publish --no-git-tag
61
- env:
62
- NPM_CONFIG_PROVENANCE: "true"
package/CHANGELOG.md DELETED
@@ -1,14 +0,0 @@
1
- # scriptable
2
-
3
- ## 0.1.1
4
-
5
- ### Patch Changes
6
-
7
- - e243ccb: Releases are now published from GitHub Actions via npm OIDC trusted publishing. Each published artifact carries an npm provenance attestation, and no `NPM_TOKEN` secret is stored anywhere.
8
-
9
- ## 0.1.0
10
-
11
- ### Minor Changes
12
-
13
- - Rewritten in TypeScript, using the `acorn` parser. All legacy runtime dependencies have been dropped.
14
- - Hardened the sandbox: fixed a set of sandbox-escape and prototype-pollution vectors, and added DoS protections (execution limits) so runaway scripts can no longer lock up the host process.
@@ -1,2 +0,0 @@
1
- declare const logicOps: Record<string, (l: unknown, r: unknown) => unknown>;
2
- export default logicOps;
package/dist/logic-ops.js DELETED
@@ -1,5 +0,0 @@
1
- const logicOps = {
2
- "||": (l, r) => l || r,
3
- "&&": (l, r) => l && r,
4
- };
5
- export default logicOps;
@@ -1,127 +0,0 @@
1
- #!/usr/bin/env node
2
- /**
3
- * Release a new version end-to-end.
4
- *
5
- * Guards:
6
- * 1. Refuses to run on a dirty tree.
7
- * 2. Refuses to run off main.
8
- * 3. Refuses to run if local main is behind origin/main (fetches first).
9
- * 4. Refuses to run with no pending changesets.
10
- *
11
- * Steps:
12
- * 5. Runs tests + build as a safety gate.
13
- * 6. Consumes changesets to bump package.json + CHANGELOG.md.
14
- * 7. Commits the result as "Version X.Y.Z".
15
- * 8. Shows the commit and prompts for confirmation.
16
- * 9. On confirm: pushes main, creates the vX.Y.Z annotated tag,
17
- * pushes the tag. The tag push triggers .github/workflows/release.yml,
18
- * which publishes to npm via OIDC (with provenance).
19
- * 10. On abort: prints instructions to undo the version commit and exits 1.
20
- *
21
- * There is deliberately only one user-facing pnpm script (`pnpm release`) —
22
- * the interactive prompt at step 8 is the abort window. Everything before
23
- * the prompt is local-only and reversible with `git reset --hard HEAD^`.
24
- */
25
- import { execSync } from "node:child_process";
26
- import { readFileSync, readdirSync } from "node:fs";
27
- import { createInterface } from "node:readline/promises";
28
- import { stdin, stdout } from "node:process";
29
-
30
- function run(cmd) {
31
- execSync(cmd, { stdio: "inherit" });
32
- }
33
-
34
- function capture(cmd) {
35
- return execSync(cmd, { encoding: "utf-8" }).trim();
36
- }
37
-
38
- function fail(msg) {
39
- console.error(`release: ${msg}`);
40
- process.exit(1);
41
- }
42
-
43
- async function prompt(question) {
44
- const rl = createInterface({ input: stdin, output: stdout });
45
- try {
46
- return (await rl.question(question)).trim().toLowerCase();
47
- } finally {
48
- rl.close();
49
- }
50
- }
51
-
52
- // 1. Clean tree check.
53
- if (capture("git status --porcelain")) {
54
- fail("working tree is not clean — commit or stash first.");
55
- }
56
-
57
- // 2. On main?
58
- const branch = capture("git rev-parse --abbrev-ref HEAD");
59
- if (branch !== "main") {
60
- fail(`not on main branch (currently on ${branch}).`);
61
- }
62
-
63
- // 3. Up-to-date check.
64
- run("git fetch origin main");
65
- const localMain = capture("git rev-parse main");
66
- const remoteMain = capture("git rev-parse origin/main");
67
- if (localMain !== remoteMain) {
68
- const behind = capture(`git rev-list --count ${localMain}..${remoteMain}`);
69
- if (behind !== "0") {
70
- fail(`local main is ${behind} commit(s) behind origin/main — pull first.`);
71
- }
72
- }
73
-
74
- // 4. Pending changeset check.
75
- const pending = readdirSync(".changeset").filter(
76
- (f) => f.endsWith(".md") && f.toLowerCase() !== "readme.md",
77
- );
78
- if (pending.length === 0) {
79
- fail("no pending changesets — run `pnpm changeset` first.");
80
- }
81
- console.log(
82
- `Found ${pending.length} pending changeset(s): ${pending.join(", ")}\n`,
83
- );
84
-
85
- // 5. Safety gate.
86
- run("pnpm test");
87
- run("pnpm build");
88
-
89
- // 6. Consume changesets → bump package.json + CHANGELOG.md.
90
- run("pnpm exec changeset version");
91
-
92
- // 7. Commit. Stage only the files changeset version touches.
93
- const { version } = JSON.parse(readFileSync("./package.json", "utf-8"));
94
- run("git add package.json CHANGELOG.md .changeset");
95
- run(`git commit -m "Version ${version}"`);
96
-
97
- // 8. Show the commit and prompt.
98
- console.log("");
99
- run("git --no-pager show HEAD --stat");
100
- console.log("");
101
- console.log("-".repeat(60));
102
- console.log(` About to release scriptable v${version}`);
103
- console.log("-".repeat(60));
104
- console.log(` Next: push main, create tag v${version}, push tag`);
105
- console.log(" -> tag push triggers .github/workflows/release.yml");
106
- console.log(" -> CI publishes to npm via OIDC (with provenance)");
107
- console.log("");
108
- console.log(" This is your last chance to abort. Once the tag is pushed,");
109
- console.log(" publishing happens automatically.");
110
- console.log("");
111
- const answer = await prompt("Proceed? [y/N] ");
112
-
113
- if (answer !== "y" && answer !== "yes") {
114
- console.log("\nAborted. To undo the version commit locally:");
115
- console.log(" git reset --hard HEAD^");
116
- console.log(" # your .changeset files will come back from the reset");
117
- process.exit(1);
118
- }
119
-
120
- // 9. Push main, tag, push tag.
121
- run("git push origin main");
122
- run("pnpm exec changeset tag");
123
- run("git push --follow-tags");
124
-
125
- console.log(`\nv${version} tag pushed.`);
126
- console.log(` https://github.com/lecstor/scriptable/actions`);
127
- console.log(` The release workflow will publish to npm shortly.`);
@@ -1,43 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import runner from "../index.js";
3
-
4
- const run = runner();
5
-
6
- /*
7
- * https://blog.liftsecurity.io/2015/04/27/when-this-is-really-that/
8
- */
9
-
10
- describe("try to get require", () => {
11
- it("this.constructor.constructor", () => {
12
- const code = `bar = this.constructor.constructor`;
13
- const env = {};
14
- expect(() => run(code, env)).toThrow("Cannot evaluate ThisExpression");
15
- });
16
-
17
- it("object.constructor", () => {
18
- const code = `
19
- bar = { any: "thing" };
20
- func = bar.constructor;
21
- func2 = func("return process");
22
- process = func2();
23
- `;
24
- const env = {};
25
- expect(() => run(code, env)).toThrow(
26
- "Access to property 'constructor' is not allowed"
27
- );
28
- });
29
-
30
- it("arguments.callee.caller", () => {
31
- const code = `
32
- function getFunc () {
33
- return arguments.callee.caller.arguments.callee.caller.arguments.callee.caller.arguments.callee.arguments.callee.caller;
34
- }
35
- func = getFunc();
36
- func.call(func, "var fs = process.mainModule.require('fs');");
37
- `;
38
- const env = {};
39
- expect(() => run(code, env)).toThrow(
40
- new ReferenceError(`"arguments" is not defined (3:15)`)
41
- );
42
- });
43
- });
@@ -1,75 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import runner from "../index.js";
3
-
4
- const run = runner();
5
-
6
- describe("builtin functions", () => {
7
- describe("date", () => {
8
- it("formats an iso8601 timestamp (+00:00 -> +00:00)", () => {
9
- const code = `formatDate("2017-08-14T11:08:19+00:00", "MMM D YYYY h:mma z")`;
10
- const { result } = run(code);
11
- expect(result).toEqual("Aug 14 2017 11:08am UTC");
12
- });
13
-
14
- it("formats an iso8601 timestamp (+10:00 -> +00:00)", () => {
15
- const code = `formatDate("2017-08-14T11:08:19+10:00", "MMM D YYYY h:mma z")`;
16
- const { result } = run(code);
17
- expect(result).toEqual("Aug 14 2017 1:08am UTC");
18
- });
19
-
20
- it("formats an iso8601 timestamp (+10:00 -> Australia/Brisbane)", () => {
21
- const code = `formatDate("2017-08-14T11:08:19+10:00", "MMM D YYYY h:mma z", "Australia/Brisbane")`;
22
- const { result } = run(code);
23
- expect(result).toMatch(/^Aug 14 2017 11:08am (AEST|GMT\+10)$/);
24
- });
25
-
26
- it("formats an iso8601 timestamp (+00:00 -> Australia/Brisbane)", () => {
27
- const code = `formatDate("2017-08-14T11:08:19+00:00", "MMM D YYYY h:mma z", "Australia/Brisbane")`;
28
- const { result } = run(code);
29
- expect(result).toMatch(/^Aug 14 2017 9:08pm (AEST|GMT\+10)$/);
30
- });
31
-
32
- it("formats a Unix timestamp (+00:00 -> +00:00)", () => {
33
- const code = `formatDate("1403454068850", "MMM D YYYY h:mma z")`;
34
- const { result } = run(code);
35
- expect(result).toEqual("Jun 22 2014 4:21pm UTC");
36
- });
37
-
38
- it("formats a Unix timestamp (+00:00 -> Australia/Brisbane)", () => {
39
- const code = `formatDate("1403454068850", "MMM D YYYY h:mma z", "Australia/Brisbane")`;
40
- const { result } = run(code);
41
- expect(result).toMatch(/^Jun 23 2014 2:21am (AEST|GMT\+10)$/);
42
- });
43
- });
44
-
45
- describe("func that wraps formatDate", () => {
46
- it("wraps formatDate", () => {
47
- const code = `
48
- Date = (date) => formatDate(date, "MMM D YYYY h:mma z", customer.tz);
49
- Date("2017-08-27T09:00:57.730+10:00");
50
- `;
51
- const { result } = run(code, { customer: { tz: "America/Los_Angeles" } });
52
- expect(result).toEqual("Aug 26 2017 4:00pm PDT");
53
- });
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
- });
75
- });
@@ -1,56 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import runner from "../index.js";
3
-
4
- const run = runner();
5
-
6
- describe("builtin functions", () => {
7
- it("concat", () => {
8
- const code = `concat(list, "i")`;
9
- const env = { list: ["me", "myself"] };
10
- const { result } = run(code, env);
11
- expect(result).toEqual(["me", "myself", "i"]);
12
- });
13
-
14
- it("push", () => {
15
- const code = `push(list, "i")`;
16
- const env = { list: ["me", "myself"] };
17
- const { result } = run(code, env);
18
- expect(result).toBeUndefined();
19
- expect(env.list).toEqual(["me", "myself", "i"]);
20
- });
21
-
22
- it("shift", () => {
23
- const code = `shift(list)`;
24
- const env = { list: ["me", "myself"] };
25
- const { result } = run(code, env);
26
- expect(result).toEqual("me");
27
- expect(env.list).toEqual(["myself"]);
28
- });
29
-
30
- it("numberFormatter - no args", () => {
31
- const code = `formatD = numberFormatter(); formatD(12340);`;
32
- const env = {};
33
- const { result } = run(code, env);
34
- expect(result).toEqual("12,340");
35
- });
36
-
37
- it("numberFormatter - with args", () => {
38
- const code = `
39
- formatD = numberFormatter("en-us", {
40
- minimumFractionDigits: 2,
41
- maximumFractionDigits: 2
42
- });
43
- formatD(12340);
44
- `;
45
- const env = {};
46
- const { result } = run(code, env);
47
- expect(result).toEqual("12,340.00");
48
- });
49
-
50
- it("formatDollars", () => {
51
- const code = `formatDollars(12340);`;
52
- const env = {};
53
- const { result } = run(code, env);
54
- expect(result).toEqual("12,340.00");
55
- });
56
- });
@@ -1,13 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import runner from "../index.js";
3
-
4
- const run = runner();
5
-
6
- describe("expressions", () => {
7
- it("6 + 3", () => {
8
- const code = `6 + 3`;
9
- const env = {};
10
- const { result } = run(code, env);
11
- expect(result).toEqual(9);
12
- });
13
- });
@@ -1,81 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import runner from "../index.js";
3
-
4
- const run = runner();
5
-
6
- describe("function", () => {
7
- it("defines a function", () => {
8
- const code = `
9
- function foo(bar){ return bar + 3; }
10
- baz = foo(7);
11
- `;
12
- const vars = {};
13
- const { env } = run(code, vars);
14
- expect(env.foo).toBeDefined();
15
- expect(env.baz).toEqual(10);
16
- });
17
-
18
- it("defines an arrow function", () => {
19
- const code = `
20
- foo = (bar) => { return bar + 3; }
21
- baz = foo(7);
22
- `;
23
- const vars = {};
24
- const { env } = run(code, vars);
25
- expect(env.foo).toBeDefined();
26
- expect(env.baz).toEqual(10);
27
- });
28
-
29
- it("has access to env", () => {
30
- const code = `
31
- greet = (name) => "hello " + name;
32
- greet(customer.name);
33
- `;
34
- const { result } = run(code, { customer: { name: "Jason" } });
35
- expect(result).toEqual("hello Jason");
36
- });
37
-
38
- it("can call a builtin with a function as arg", () => {
39
- const code = `
40
- nprices = map(prices, (price) => { price });
41
- `;
42
- const vars = { prices: [11, 22, 33] };
43
- const { env } = run(code, vars);
44
- expect(env).toEqual({
45
- prices: [11, 22, 33],
46
- nprices: [11, 22, 33],
47
- });
48
- });
49
-
50
- it("can call a builtin with a function as arg", () => {
51
- const code = `
52
- addTax = (price) => { return price + price / 10 };
53
- incTax = map(prices, (price) => { return addTax(price) });
54
- incTax2 = map(prices, (price) => addTax(price));
55
- incTax3 = map(prices, addTax);
56
- `;
57
- const vars = { prices: [10, 20, 30] };
58
- const { env } = run(code, vars);
59
- expect(env.incTax).toEqual([11, 22, 33]);
60
- expect(env.incTax2).toEqual([11, 22, 33]);
61
- expect(env.incTax3).toEqual([11, 22, 33]);
62
- });
63
-
64
- it("can use an array as a function arg", () => {
65
- const code = `
66
- getLength = (list) => list.length;
67
- getLength([1, 2, 3]);
68
- `;
69
- const { result } = run(code, {});
70
- expect(result).toEqual(3);
71
- });
72
-
73
- it("can use an object as a function arg", () => {
74
- const code = `
75
- getLength = (obj) => keys(obj).length;
76
- getLength({ one: 1, two: 2, three: 3 });
77
- `;
78
- const { result } = run(code, {});
79
- expect(result).toEqual(3);
80
- });
81
- });
@@ -1,54 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import runner from "../index.js";
3
-
4
- const run = runner();
5
-
6
- describe("logic", () => {
7
- describe("operators", () => {
8
- it("gt true", () => {
9
- const code = `6 > 3`;
10
- const env = {};
11
- const { result } = run(code, env);
12
- expect(result).toEqual(true);
13
- });
14
-
15
- it("gt false", () => {
16
- const code = `3 > 6`;
17
- const env = {};
18
- const { result } = run(code, env);
19
- expect(result).toEqual(false);
20
- });
21
- });
22
-
23
- describe("if", () => {
24
- it("if gt true", () => {
25
- const code = `if (6 > 3) { true }`;
26
- const env = {};
27
- const { result } = run(code, env);
28
- expect(result).toEqual(true);
29
- });
30
- });
31
-
32
- describe("||", () => {
33
- it("handles ||", () => {
34
- const code = `thing = who || "world"; thing`;
35
- const env = { who: "Me" };
36
- const { result } = run(code, env);
37
- expect(result).toEqual("Me");
38
- });
39
- });
40
- describe("&&", () => {
41
- it("handles && - true", () => {
42
- const code = `if (1 && 2) { thing = "true" }`;
43
- const vars = {};
44
- const { env } = run(code, vars);
45
- expect(env.thing).toEqual("true");
46
- });
47
- it("handles && - false", () => {
48
- const code = `if (0 && 2) { thing = "true" }`;
49
- const env = {};
50
- run(code, env);
51
- expect(env.thing).toBeUndefined();
52
- });
53
- });
54
- });