Skip to content

Commit 823b188

Browse files
committed
refactor: clean up
1 parent 2dcee65 commit 823b188

File tree

5 files changed

+72
-216
lines changed

5 files changed

+72
-216
lines changed

package-lock.json

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

packages/math-utils/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
},
5050
"dependencies": {
5151
"@apify/consts": "^2.47.0",
52-
"@apify/log": "^2.5.26"
52+
"@apify/log": "^2.5.26",
53+
"mathjs": "^15.1.0"
5354
}
5455
}

packages/math-utils/src/memory_calculator.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
// MathJS bundle with only numbers is ~2x smaller than the default one.
2-
import { all, create, type EvalFunction } from 'mathjs/number';
2+
import {
3+
addDependencies,
4+
andDependencies,
5+
compileDependencies,
6+
create,
7+
divideDependencies,
8+
type EvalFunction,
9+
evaluateDependencies,
10+
maxDependencies,
11+
minDependencies,
12+
multiplyDependencies,
13+
notDependencies,
14+
nullishDependencies,
15+
orDependencies,
16+
subtractDependencies,
17+
xorDependencies,
18+
} from 'mathjs';
319

420
import { ACTOR_LIMITS } from '@apify/consts';
521

@@ -37,10 +53,29 @@ const ALLOWED_RUN_OPTION_KEYS = new Set<keyof ActorRunOptions>([
3753
]);
3854

3955
/**
40-
* Create a mathjs instance with all functions, then disable potentially dangerous ones.
56+
* Create a mathjs instance with selected dependencies, then disable potentially dangerous ones.
4157
* MathJS security recommendations: https://mathjs.org/docs/expressions/security.html
4258
*/
43-
const math = create(all);
59+
const math = create({
60+
// arithmetic dependencies
61+
addDependencies,
62+
subtractDependencies,
63+
multiplyDependencies,
64+
divideDependencies,
65+
// expression dependencies
66+
compileDependencies,
67+
evaluateDependencies,
68+
// statistics dependencies
69+
maxDependencies,
70+
minDependencies,
71+
// logical dependencies
72+
andDependencies,
73+
notDependencies,
74+
orDependencies,
75+
xorDependencies,
76+
// without that dependency 'null ?? 5', won't work
77+
nullishDependencies,
78+
});
4479
const limitedEvaluate = math.evaluate;
4580
const limitedCompile = math.compile;
4681

packages/utilities/src/memory_calculator.ts

Lines changed: 0 additions & 201 deletions
This file was deleted.

test/memory_calculator.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,36 @@ describe('calculateDefaultMemoryFromExpression', () => {
5252
const result = calculateDefaultMemoryFromExpression(expr, context);
5353
expect(result).toBe(512);
5454
});
55+
56+
describe('operations supported', () => {
57+
const context = {
58+
input: { A: 200, B: 10, C: 4, nullVal: null, zeroVal: 0, startUrls: [1, 2, 3] },
59+
runOptions: { timeoutSecs: 60, memoryMbytes: 512 },
60+
};
61+
62+
const cases = [
63+
{ expression: '5 + 5', desc: '+ allowed' },
64+
{ expression: '6 - 5', desc: '- allowed' },
65+
{ expression: '5 / 5', desc: '/ allowed' },
66+
{ expression: '5 * 5', desc: '* allowed' },
67+
{ expression: 'max(1, 2, 3)', desc: 'max() allowed' },
68+
{ expression: 'min(1, 2, 3)', desc: 'min() allowed' },
69+
{ expression: '(true and false) ? 0 : 5', desc: 'and allowed' },
70+
{ expression: '(true or false) ? 5 : 0', desc: 'or allowed' },
71+
{ expression: '(true xor false) ? 5 : 0', desc: 'xor allowed' },
72+
{ expression: 'not(false) ? 5 : 0', desc: 'not allowed' },
73+
{ expression: 'input.nullVal ?? 256', desc: 'nullish coalescing allowed' },
74+
{ expression: 'a = 5', desc: 'variable assignment' },
75+
];
76+
77+
it.each(cases)(
78+
'$desc',
79+
({ expression }) => {
80+
// in case operation is not supported, mathjs will throw
81+
expect(calculateDefaultMemoryFromExpression(expression, context)).toBeDefined();
82+
},
83+
);
84+
});
5585
});
5686

5787
describe('Preprocessing with {{variable}}', () => {

0 commit comments

Comments
 (0)