Skip to content

Commit f3a97bf

Browse files
authored
feat: add function to calculate dynamic default Actor memory (#570)
This PR introduces the logic to calculate a dynamic default Actor memory limit based on a provided expression string. **TL;DR:** - Uses a sandboxed MathJS instance to securely evaluate expressions. - Supports `{{variable}}` placeholders with strict validation: `input.*` paths are allowed, while `runOptions.*` keys are strictly whitelisted(only 7 variables allowed). - Injects the Actor input and run options into the evaluation scope. - Adds a custom `get(obj, path, default)` helper to safely access nested properties without crashing on missing paths (e.g., `get(input, 'startUrls.length', 0)`). - Rounds the evaluated result to the closest valid power-of-2 memory tier. Result is clamped to min/max range. (128MB-32GB) Note: when using this function in our API, we'll use it in `worker_thread`. I didn't add worker_thread to this package, because I suppose it can be used from client-side as well. More context: [Dynamic default Actor run memory notion spec](https://www.notion.so/apify/Dynamic-default-Actor-memory-WIP-293f39950a2280b9aa34fa9cd07f52a4?source=copy_link)
1 parent 27fa703 commit f3a97bf

File tree

10 files changed

+699
-0
lines changed

10 files changed

+699
-0
lines changed

package-lock.json

Lines changed: 122 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/actor-memory-expression/CHANGELOG.md

Whitespace-only changes.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"name": "@apify/actor-memory-expression",
3+
"version": "0.0.1",
4+
"description": "Utility to evaluate dynamic memory expressions for Apify actors.",
5+
"main": "./dist/cjs/index.cjs",
6+
"module": "./dist/esm/index.mjs",
7+
"typings": "./dist/cjs/index.d.ts",
8+
"exports": {
9+
".": {
10+
"import": {
11+
"types": "./dist/esm/index.d.mts",
12+
"default": "./dist/esm/index.mjs"
13+
},
14+
"require": {
15+
"types": "./dist/cjs/index.d.ts",
16+
"default": "./dist/cjs/index.cjs"
17+
}
18+
}
19+
},
20+
"keywords": [
21+
"apify"
22+
],
23+
"author": {
24+
"name": "Apify",
25+
"email": "[email protected]",
26+
"url": "https://apify.com"
27+
},
28+
"contributors": [
29+
"Jan Curn <[email protected]>",
30+
"Marek Trunkát <[email protected]>"
31+
],
32+
"license": "Apache-2.0",
33+
"repository": {
34+
"type": "git",
35+
"url": "git+https://github.com/apify/apify-shared-js"
36+
},
37+
"bugs": {
38+
"url": "https://github.com/apify/apify-shared-js/issues"
39+
},
40+
"homepage": "https://apify.com",
41+
"scripts": {
42+
"build": "npm run clean && npm run compile && npm run copy",
43+
"clean": "rimraf ./dist",
44+
"compile": "tsup",
45+
"copy": "ts-node -T ../../scripts/copy.ts"
46+
},
47+
"publishConfig": {
48+
"access": "public"
49+
},
50+
"dependencies": {
51+
"@apify/consts": "^2.47.0",
52+
"@apify/log": "^2.5.26",
53+
"mathjs": "^15.1.0"
54+
}
55+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './memory_calculator';

0 commit comments

Comments
 (0)