scriptable 0.0.6 → 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/.changeset/README.md +8 -0
- package/.changeset/config.json +11 -0
- package/.github/workflows/release.yml +62 -0
- package/CHANGELOG.md +14 -0
- package/README.md +185 -0
- package/dist/binary-ops.d.ts +2 -0
- package/dist/binary-ops.js +16 -0
- package/dist/builtin-functions.d.ts +3 -0
- package/dist/builtin-functions.js +151 -0
- package/dist/environment.d.ts +16 -0
- package/dist/environment.js +111 -0
- package/dist/evaluate.d.ts +5 -0
- package/dist/evaluate.js +232 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +26 -0
- package/dist/logic-ops.d.ts +2 -0
- package/dist/logic-ops.js +5 -0
- package/package.json +22 -52
- package/scripts/release.js +127 -0
- package/src/__tests__/{break-out.js → break-out.test.ts} +3 -14
- package/src/__tests__/{builtin-functions-date.js → builtin-functions-date.test.ts} +30 -11
- package/src/__tests__/{builtin-functions.js → builtin-functions.test.ts} +2 -3
- package/src/__tests__/{expressions.js → expressions.test.ts} +2 -3
- package/src/__tests__/{functions.js → functions.test.ts} +7 -8
- package/src/__tests__/{logic.js → logic.test.ts} +2 -3
- package/src/__tests__/new-features.test.ts +158 -0
- package/src/__tests__/security.test.ts +399 -0
- package/src/__tests__/{variables.js → variables.test.ts} +3 -6
- package/src/binary-ops.ts +18 -0
- package/src/builtin-functions.ts +177 -0
- package/src/environment.ts +132 -0
- package/src/evaluate.ts +275 -0
- package/src/index.ts +42 -0
- package/src/logic-ops.ts +6 -0
- package/tsconfig.json +16 -0
- package/.npmignore +0 -1
- package/src/__tests__/index.js +0 -85
- package/src/binary-ops.js +0 -13
- package/src/builtin-functions.js +0 -58
- package/src/environment.js +0 -73
- package/src/evaluate.js +0 -174
- package/src/index.js +0 -26
- package/src/logic-ops.js +0 -6
- package/yarn.lock +0 -2632
|
@@ -0,0 +1,8 @@
|
|
|
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).
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# scriptable
|
|
2
|
+
|
|
3
|
+
A safe, sandboxed JavaScript interpreter that walks the AST node-by-node. No `eval()`, no `new Function()` — only explicitly handled node types are executed, with only whitelisted functions available to scripts.
|
|
4
|
+
|
|
5
|
+
Designed for executing user-provided or AI-generated JS in constrained environments like Cloudflare Workers where CSP blocks dynamic code evaluation.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add scriptable
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import runner from "scriptable";
|
|
17
|
+
|
|
18
|
+
const run = runner();
|
|
19
|
+
|
|
20
|
+
const { result, env } = run(`
|
|
21
|
+
const tax = price * 0.1;
|
|
22
|
+
const total = price + tax;
|
|
23
|
+
`, { price: 100 });
|
|
24
|
+
|
|
25
|
+
// result: 110 (last expression value)
|
|
26
|
+
// env: { price: 100, tax: 10, total: 110 }
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Injecting custom functions
|
|
30
|
+
|
|
31
|
+
Scripts can only call functions you explicitly provide. Pass them via the `functions` option:
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
const run = runner({
|
|
35
|
+
functions: {
|
|
36
|
+
add: (a, b) => a + b,
|
|
37
|
+
greet: (name) => `hello ${name}`,
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const { result } = run(`greet("world")`);
|
|
42
|
+
// result: "hello world"
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The default `functions` include array/collection operations (`map`, `filter`, `reduce`, `forEach`, `concat`, `keys`, `values`, etc.), number formatting (`numberFormatter`, `formatDollars`), date formatting (`formatDate`, `currentDate`), and array mutation (`push`, `shift`).
|
|
46
|
+
|
|
47
|
+
### API
|
|
48
|
+
|
|
49
|
+
**`runner(options?)`** returns an executor function.
|
|
50
|
+
|
|
51
|
+
- `options.functions` — `Record<string, Function>` of callable functions (defaults to built-in set)
|
|
52
|
+
|
|
53
|
+
**`run(code, env?, debug?)`** executes code and returns `{ result, env }`.
|
|
54
|
+
|
|
55
|
+
- `code` — JavaScript source string
|
|
56
|
+
- `env` — initial variables available to the script (default `{}`)
|
|
57
|
+
- `debug` — if truthy, logs the AST to console
|
|
58
|
+
- Returns `result` (value of last expression) and `env` (all variables after execution)
|
|
59
|
+
|
|
60
|
+
## Supported syntax
|
|
61
|
+
|
|
62
|
+
| Node type | Examples |
|
|
63
|
+
|---|---|
|
|
64
|
+
| Variables | `var`, `let`, `const`, assignment |
|
|
65
|
+
| Functions | `function foo() {}`, arrow functions `() => {}` |
|
|
66
|
+
| Expressions | Binary (`+`, `-`, `*`, `/`, `%`, comparisons), logical (`&&`, `\|\|`), unary (`!`, `-`, `+`, `typeof`) |
|
|
67
|
+
| Conditionals | `if`/`else if`/`else`, ternary `? :` |
|
|
68
|
+
| Literals | Strings, numbers, booleans, arrays, objects |
|
|
69
|
+
| Template literals | `` `hello ${name}` `` |
|
|
70
|
+
| Spread | `[...arr]`, `{...obj}`, `fn(...args)` |
|
|
71
|
+
| Member access | `obj.prop`, `obj["prop"]`, `arr[0]` |
|
|
72
|
+
| Return | `return` in functions |
|
|
73
|
+
|
|
74
|
+
Unsupported node types throw `"Cannot evaluate <type>"`, which is the primary security mechanism — there is no way to access `this`, `arguments`, constructors, `import`, `require`, or any runtime API unless explicitly injected via `functions`.
|
|
75
|
+
|
|
76
|
+
## Changes from v0.0.x
|
|
77
|
+
|
|
78
|
+
This is a full rewrite of the original package. The interpreter behavior and API shape are preserved.
|
|
79
|
+
|
|
80
|
+
### Parser: Babylon → Acorn
|
|
81
|
+
|
|
82
|
+
Replaced [Babylon](https://github.com/babel/babylon) with [Acorn](https://github.com/acornjs/acorn) (~12KB, zero transitive deps, ESM, actively maintained, Cloudflare Workers compatible).
|
|
83
|
+
|
|
84
|
+
### Language: JavaScript → TypeScript
|
|
85
|
+
|
|
86
|
+
All source converted to TypeScript with strict mode. Builds to ESM (`dist/`) with declaration files.
|
|
87
|
+
|
|
88
|
+
### Dependencies removed
|
|
89
|
+
|
|
90
|
+
| Old | Replacement |
|
|
91
|
+
|---|---|
|
|
92
|
+
| `babylon` | `acorn` |
|
|
93
|
+
| `lodash` | Native equivalents (`Object.keys`, `Array.map`, etc.) |
|
|
94
|
+
| `moment` + `moment-timezone` | `Intl.DateTimeFormat` (zero deps, built into all runtimes) |
|
|
95
|
+
| `jest` | `vitest` |
|
|
96
|
+
|
|
97
|
+
### Test runner: Jest → Vitest
|
|
98
|
+
|
|
99
|
+
All tests ported to Vitest. The moment-dependent date tests now use `Intl.DateTimeFormat` and accept runtime-dependent timezone abbreviations (e.g. `AEST` in browsers/Workers, `GMT+10` in some Node versions).
|
|
100
|
+
|
|
101
|
+
### New AST node types
|
|
102
|
+
|
|
103
|
+
- **TemplateLiteral** — backtick strings with `${expression}` interpolation
|
|
104
|
+
- **SpreadElement** — `...` in arrays, objects, and function arguments
|
|
105
|
+
- **ConditionalExpression** — ternary `condition ? a : b`
|
|
106
|
+
- **UnaryExpression** — `!`, `-`, `+`, `typeof`
|
|
107
|
+
- **IfStatement else** — `else` and `else if` branches (previously only `if`)
|
|
108
|
+
|
|
109
|
+
### Bug fixes
|
|
110
|
+
|
|
111
|
+
- `VariableDeclarator` now evaluates initializers with the environment, so `const x = foo(bar)` works (previously only literal initializers like `const x = "hello"` worked)
|
|
112
|
+
- `BlockStatement` uses a native `for` loop instead of `lodash/forEach` for return-statement breaking
|
|
113
|
+
|
|
114
|
+
### Date builtins
|
|
115
|
+
|
|
116
|
+
`currentDate(tz?)` and `formatDate(dateString, format, timezone?)` are reimplemented using `Intl.DateTimeFormat`. The `formatDate` function accepts moment-style format tokens:
|
|
117
|
+
|
|
118
|
+
| Token | Output | Example |
|
|
119
|
+
|---|---|---|
|
|
120
|
+
| `YYYY` | 4-digit year | `2017` |
|
|
121
|
+
| `YY` | 2-digit year | `17` |
|
|
122
|
+
| `MMMM` | Full month | `January` |
|
|
123
|
+
| `MMM` | Short month | `Jan` |
|
|
124
|
+
| `MM` | Padded month | `01` |
|
|
125
|
+
| `DD` | Padded day | `05` |
|
|
126
|
+
| `D` | Day | `5` |
|
|
127
|
+
| `HH` | 24-hour padded | `09` |
|
|
128
|
+
| `h` | 12-hour | `9` |
|
|
129
|
+
| `mm` | Minutes padded | `08` |
|
|
130
|
+
| `ss` | Seconds padded | `05` |
|
|
131
|
+
| `a` | am/pm | `am` |
|
|
132
|
+
| `A` | AM/PM | `AM` |
|
|
133
|
+
| `z` | Timezone abbr | `UTC` |
|
|
134
|
+
|
|
135
|
+
### Package setup
|
|
136
|
+
|
|
137
|
+
- ESM (`"type": "module"`) with TypeScript build
|
|
138
|
+
- pnpm for package management
|
|
139
|
+
- `exports` field with types
|
|
140
|
+
|
|
141
|
+
## Contributing
|
|
142
|
+
|
|
143
|
+
### Adding a changeset
|
|
144
|
+
|
|
145
|
+
Every user-facing change needs a changeset so it appears in the CHANGELOG:
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
pnpm changeset # interactive: pick patch/minor/major, write a summary
|
|
149
|
+
git add .changeset # commit the generated .changeset/*.md file with your PR
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Releasing
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
pnpm release
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
This runs the release script (`scripts/release.js`), which:
|
|
159
|
+
|
|
160
|
+
1. Guards: clean tree, on `main`, up to date with `origin/main`, pending changesets exist.
|
|
161
|
+
2. Runs tests and build as a safety gate.
|
|
162
|
+
3. Consumes changesets to bump `package.json` and update `CHANGELOG.md`.
|
|
163
|
+
4. Commits as `Version X.Y.Z` and shows the diff.
|
|
164
|
+
5. Prompts for confirmation (last chance to abort).
|
|
165
|
+
6. Pushes `main`, creates an annotated `vX.Y.Z` tag, and pushes the tag.
|
|
166
|
+
7. The tag push triggers `.github/workflows/release.yml`, which publishes to npm via OIDC trusted publishing with provenance attestation. No `NPM_TOKEN` is involved.
|
|
167
|
+
|
|
168
|
+
### Recovery: if the release workflow fails after the tag is pushed
|
|
169
|
+
|
|
170
|
+
The version commit is on `main` and the tag is pushed, but npm publish failed. You can't rerun `pnpm release` because the changesets have already been consumed.
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
# 1. Delete the tag locally and on the remote
|
|
174
|
+
git tag -d vX.Y.Z
|
|
175
|
+
git push origin :refs/tags/vX.Y.Z
|
|
176
|
+
|
|
177
|
+
# 2. Fix whatever broke, commit on main
|
|
178
|
+
git commit -am "fix(ci): ..."
|
|
179
|
+
git push origin main
|
|
180
|
+
|
|
181
|
+
# 3. Re-tag HEAD (annotated!) and push to re-trigger the workflow.
|
|
182
|
+
# Same version number — this is a retry, not a new version.
|
|
183
|
+
git tag -a vX.Y.Z -m "Version X.Y.Z"
|
|
184
|
+
git push origin vX.Y.Z
|
|
185
|
+
```
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const binaryOps = {
|
|
2
|
+
"+": (l, r) => l + r,
|
|
3
|
+
"-": (l, r) => l - r,
|
|
4
|
+
"*": (l, r) => l * r,
|
|
5
|
+
"/": (l, r) => l / r,
|
|
6
|
+
"%": (l, r) => l % r,
|
|
7
|
+
">": (l, r) => l > r,
|
|
8
|
+
"<": (l, r) => l < r,
|
|
9
|
+
">=": (l, r) => l >= r,
|
|
10
|
+
"<=": (l, r) => l <= r,
|
|
11
|
+
"==": (l, r) => l === r,
|
|
12
|
+
"!=": (l, r) => l !== r,
|
|
13
|
+
"===": (l, r) => l === r,
|
|
14
|
+
"!==": (l, r) => l !== r,
|
|
15
|
+
};
|
|
16
|
+
export default binaryOps;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// Date helpers (Intl-based, no moment dependency)
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
function getDateParts(date, tz) {
|
|
5
|
+
const fmt = (opts) => new Intl.DateTimeFormat("en-US", { timeZone: tz, ...opts });
|
|
6
|
+
const year = parseInt(fmt({ year: "numeric" }).format(date));
|
|
7
|
+
const month = parseInt(fmt({ month: "numeric" }).format(date));
|
|
8
|
+
const day = parseInt(fmt({ day: "numeric" }).format(date));
|
|
9
|
+
const h24Parts = fmt({
|
|
10
|
+
hour: "numeric",
|
|
11
|
+
minute: "numeric",
|
|
12
|
+
second: "numeric",
|
|
13
|
+
hourCycle: "h23",
|
|
14
|
+
}).formatToParts(date);
|
|
15
|
+
const part = (type) => h24Parts.find((p) => p.type === type)?.value || "0";
|
|
16
|
+
const hour24 = parseInt(part("hour"));
|
|
17
|
+
const minute = parseInt(part("minute"));
|
|
18
|
+
const second = parseInt(part("second"));
|
|
19
|
+
const hour12 = hour24 === 0 ? 12 : hour24 > 12 ? hour24 - 12 : hour24;
|
|
20
|
+
const ampm = hour24 >= 12 ? "pm" : "am";
|
|
21
|
+
const monthShort = fmt({ month: "short" }).format(date);
|
|
22
|
+
const monthLong = fmt({ month: "long" }).format(date);
|
|
23
|
+
const tzAbbr = fmt({ timeZoneName: "short" })
|
|
24
|
+
.formatToParts(date)
|
|
25
|
+
.find((p) => p.type === "timeZoneName")?.value || "";
|
|
26
|
+
return {
|
|
27
|
+
year, month, day,
|
|
28
|
+
hour12, hour24, minute, second,
|
|
29
|
+
ampm, monthShort, monthLong, tzAbbr,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
const FORMAT_TOKENS = /YYYY|YY|MMMM|MMM|MM|M|DD|D|HH|H|hh|h|mm|m|ss|s|a|A|z/g;
|
|
33
|
+
function applyFormat(parts, format) {
|
|
34
|
+
return format.replace(FORMAT_TOKENS, (token) => {
|
|
35
|
+
switch (token) {
|
|
36
|
+
case "YYYY": return String(parts.year);
|
|
37
|
+
case "YY": return String(parts.year).slice(-2);
|
|
38
|
+
case "MMMM": return parts.monthLong;
|
|
39
|
+
case "MMM": return parts.monthShort;
|
|
40
|
+
case "MM": return String(parts.month).padStart(2, "0");
|
|
41
|
+
case "M": return String(parts.month);
|
|
42
|
+
case "DD": return String(parts.day).padStart(2, "0");
|
|
43
|
+
case "D": return String(parts.day);
|
|
44
|
+
case "HH": return String(parts.hour24).padStart(2, "0");
|
|
45
|
+
case "H": return String(parts.hour24);
|
|
46
|
+
case "hh": return String(parts.hour12).padStart(2, "0");
|
|
47
|
+
case "h": return String(parts.hour12);
|
|
48
|
+
case "mm": return String(parts.minute).padStart(2, "0");
|
|
49
|
+
case "m": return String(parts.minute);
|
|
50
|
+
case "ss": return String(parts.second).padStart(2, "0");
|
|
51
|
+
case "s": return String(parts.second);
|
|
52
|
+
case "a": return parts.ampm;
|
|
53
|
+
case "A": return parts.ampm.toUpperCase();
|
|
54
|
+
case "z": return parts.tzAbbr;
|
|
55
|
+
default: return token;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
// ---------------------------------------------------------------------------
|
|
60
|
+
// Number helpers
|
|
61
|
+
// ---------------------------------------------------------------------------
|
|
62
|
+
function numberFormatter(...args) {
|
|
63
|
+
return new Intl.NumberFormat(...args).format;
|
|
64
|
+
}
|
|
65
|
+
const functions = {
|
|
66
|
+
push(...args) {
|
|
67
|
+
const [target, item] = args;
|
|
68
|
+
target.push(item);
|
|
69
|
+
},
|
|
70
|
+
shift(...args) {
|
|
71
|
+
const [target] = args;
|
|
72
|
+
return target.shift();
|
|
73
|
+
},
|
|
74
|
+
currentDate(tz) {
|
|
75
|
+
return new Date();
|
|
76
|
+
},
|
|
77
|
+
formatDate(dateString, format, timezone) {
|
|
78
|
+
const date = /^\d+$/.test(dateString)
|
|
79
|
+
? new Date(+dateString)
|
|
80
|
+
: new Date(dateString);
|
|
81
|
+
const tz = timezone || "UTC";
|
|
82
|
+
return applyFormat(getDateParts(date, tz), format);
|
|
83
|
+
},
|
|
84
|
+
numberFormatter,
|
|
85
|
+
formatDollars: numberFormatter("en-us", {
|
|
86
|
+
minimumFractionDigits: 2,
|
|
87
|
+
maximumFractionDigits: 2,
|
|
88
|
+
}),
|
|
89
|
+
// Array/collection operations (replaces lodash)
|
|
90
|
+
map(arr, fn) {
|
|
91
|
+
return arr.map(fn);
|
|
92
|
+
},
|
|
93
|
+
filter(arr, fn) {
|
|
94
|
+
return arr.filter(fn);
|
|
95
|
+
},
|
|
96
|
+
find(arr, fn) {
|
|
97
|
+
return arr.find(fn);
|
|
98
|
+
},
|
|
99
|
+
forEach(arr, fn) {
|
|
100
|
+
arr.forEach(fn);
|
|
101
|
+
},
|
|
102
|
+
reduce(arr, fn, initial) {
|
|
103
|
+
return initial !== undefined ? arr.reduce(fn, initial) : arr.reduce(fn);
|
|
104
|
+
},
|
|
105
|
+
concat(arr, ...items) {
|
|
106
|
+
return arr.concat(...items);
|
|
107
|
+
},
|
|
108
|
+
keys(obj) {
|
|
109
|
+
return Object.keys(obj);
|
|
110
|
+
},
|
|
111
|
+
values(obj) {
|
|
112
|
+
return Object.values(obj);
|
|
113
|
+
},
|
|
114
|
+
entries(obj) {
|
|
115
|
+
return Object.entries(obj);
|
|
116
|
+
},
|
|
117
|
+
assign(...args) {
|
|
118
|
+
return Object.assign({}, ...args);
|
|
119
|
+
},
|
|
120
|
+
join(arr, sep) {
|
|
121
|
+
return arr.join(sep);
|
|
122
|
+
},
|
|
123
|
+
includes(arr, item) {
|
|
124
|
+
return arr.includes(item);
|
|
125
|
+
},
|
|
126
|
+
slice(arr, start, end) {
|
|
127
|
+
return arr.slice(start, end);
|
|
128
|
+
},
|
|
129
|
+
sort(arr, fn) {
|
|
130
|
+
return [...arr].sort(fn);
|
|
131
|
+
},
|
|
132
|
+
reverse(arr) {
|
|
133
|
+
return [...arr].reverse();
|
|
134
|
+
},
|
|
135
|
+
flat(arr, depth) {
|
|
136
|
+
return arr.flat(depth);
|
|
137
|
+
},
|
|
138
|
+
flatMap(arr, fn) {
|
|
139
|
+
return arr.flatMap(fn);
|
|
140
|
+
},
|
|
141
|
+
some(arr, fn) {
|
|
142
|
+
return arr.some(fn);
|
|
143
|
+
},
|
|
144
|
+
every(arr, fn) {
|
|
145
|
+
return arr.every(fn);
|
|
146
|
+
},
|
|
147
|
+
indexOf(arr, item) {
|
|
148
|
+
return arr.indexOf(item);
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
export default functions;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface Loc {
|
|
2
|
+
line: number;
|
|
3
|
+
column: number;
|
|
4
|
+
}
|
|
5
|
+
export default class Environment {
|
|
6
|
+
vars: Record<string, any>;
|
|
7
|
+
parent: Environment | null;
|
|
8
|
+
constructor(parent?: Environment);
|
|
9
|
+
extend(): Environment;
|
|
10
|
+
getGlobalScope(): Environment;
|
|
11
|
+
lookup(name: string): Environment;
|
|
12
|
+
defined(name: string): boolean;
|
|
13
|
+
get(name: string, loc: Loc): any;
|
|
14
|
+
set(name: string, value: any, loc: Loc): any;
|
|
15
|
+
def(name: string, value: any): any;
|
|
16
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// Properties that could be used for prototype-chain traversal or pollution.
|
|
2
|
+
// Defense-in-depth: also checked in evaluate.ts MemberExpression.
|
|
3
|
+
const BLOCKED_PROPS = new Set([
|
|
4
|
+
"constructor",
|
|
5
|
+
"__proto__",
|
|
6
|
+
"prototype",
|
|
7
|
+
"__defineGetter__",
|
|
8
|
+
"__defineSetter__",
|
|
9
|
+
"__lookupGetter__",
|
|
10
|
+
"__lookupSetter__",
|
|
11
|
+
]);
|
|
12
|
+
function assertSafeParts(parts) {
|
|
13
|
+
for (const part of parts) {
|
|
14
|
+
if (BLOCKED_PROPS.has(part)) {
|
|
15
|
+
throw new Error(`Access to property '${part}' is not allowed`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function deepHas(obj, path) {
|
|
20
|
+
const parts = path.split(".");
|
|
21
|
+
assertSafeParts(parts);
|
|
22
|
+
let current = obj;
|
|
23
|
+
for (const part of parts) {
|
|
24
|
+
if (current == null || typeof current !== "object")
|
|
25
|
+
return false;
|
|
26
|
+
if (!Object.prototype.hasOwnProperty.call(current, part))
|
|
27
|
+
return false;
|
|
28
|
+
current = current[part];
|
|
29
|
+
}
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
function deepGet(obj, path) {
|
|
33
|
+
const parts = path.split(".");
|
|
34
|
+
assertSafeParts(parts);
|
|
35
|
+
let current = obj;
|
|
36
|
+
for (const part of parts) {
|
|
37
|
+
if (current == null)
|
|
38
|
+
return undefined;
|
|
39
|
+
current = current[part];
|
|
40
|
+
}
|
|
41
|
+
return current;
|
|
42
|
+
}
|
|
43
|
+
function deepSet(obj, path, value) {
|
|
44
|
+
const parts = path.split(".");
|
|
45
|
+
assertSafeParts(parts);
|
|
46
|
+
let current = obj;
|
|
47
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
48
|
+
if (current[parts[i]] == null || typeof current[parts[i]] !== "object") {
|
|
49
|
+
current[parts[i]] = {};
|
|
50
|
+
}
|
|
51
|
+
current = current[parts[i]];
|
|
52
|
+
}
|
|
53
|
+
current[parts[parts.length - 1]] = value;
|
|
54
|
+
}
|
|
55
|
+
export default class Environment {
|
|
56
|
+
constructor(parent) {
|
|
57
|
+
this.vars = Object.create(parent ? parent.vars : null);
|
|
58
|
+
this.parent = parent || null;
|
|
59
|
+
}
|
|
60
|
+
extend() {
|
|
61
|
+
return new Environment(this);
|
|
62
|
+
}
|
|
63
|
+
getGlobalScope() {
|
|
64
|
+
let scope = this;
|
|
65
|
+
while (scope.parent) {
|
|
66
|
+
scope = scope.parent;
|
|
67
|
+
}
|
|
68
|
+
return scope;
|
|
69
|
+
}
|
|
70
|
+
lookup(name) {
|
|
71
|
+
let scope = this;
|
|
72
|
+
while (scope.parent) {
|
|
73
|
+
if (Object.prototype.hasOwnProperty.call(scope.vars, name)) {
|
|
74
|
+
return scope;
|
|
75
|
+
}
|
|
76
|
+
scope = scope.parent;
|
|
77
|
+
}
|
|
78
|
+
return scope;
|
|
79
|
+
}
|
|
80
|
+
defined(name) {
|
|
81
|
+
if (name in this.vars) {
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
if (deepHas(this.vars, name)) {
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
get(name, loc) {
|
|
90
|
+
if (name in this.vars) {
|
|
91
|
+
return this.vars[name];
|
|
92
|
+
}
|
|
93
|
+
if (deepHas(this.vars, name)) {
|
|
94
|
+
return deepGet(this.vars, name);
|
|
95
|
+
}
|
|
96
|
+
throw new ReferenceError(`"${name}" is not defined (${loc.line}:${loc.column})`);
|
|
97
|
+
}
|
|
98
|
+
set(name, value, loc) {
|
|
99
|
+
const baseName = name.replace(/\..*/, "");
|
|
100
|
+
const scope = this.lookup(baseName);
|
|
101
|
+
if (!scope && this.parent) {
|
|
102
|
+
throw new ReferenceError(`"${name}" is not defined (${loc.line}:${loc.column})`);
|
|
103
|
+
}
|
|
104
|
+
deepSet((scope || this).vars, name, value);
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
107
|
+
def(name, value) {
|
|
108
|
+
deepSet(this.vars, name, value);
|
|
109
|
+
return value;
|
|
110
|
+
}
|
|
111
|
+
}
|