Skip to content

Commit 95e11b5

Browse files
committed
refactor: clean up
1 parent 4eef96c commit 95e11b5

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

packages/utilities/src/memory_calculator.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const ALLOWED_RUN_OPTION_KEYS = new Set<keyof ActorRunOptions>([
3838

3939
/**
4040
* Create a mathjs instance with all functions, then disable potentially dangerous ones.
41-
* Was taken from official mathjs security recommendations: https://mathjs.org/docs/expressions/security.html
41+
* MathJS security recommendations: https://mathjs.org/docs/expressions/security.html
4242
*/
4343
const math = create(all);
4444
const limitedEvaluate = math.evaluate;
@@ -81,7 +81,7 @@ const customGetFunc = (obj: any, path: string, defaultVal?: number) => {
8181
* @returns The closest power of 2 within min/max range.
8282
*/
8383
const roundToClosestPowerOf2 = (num: number): number | undefined => {
84-
// Handle 0 or negative values. The smallest power of 2 is 2^7 = 128.
84+
// Handle 0 or negative values.
8585
if (num <= 0) {
8686
throw new Error(`Calculated memory value must be a positive number, greater than 0, got: ${num}`);
8787
}
@@ -159,9 +159,7 @@ const preprocessDefaultMemoryExpression = (defaultMemoryMbytes: string): string
159159
*
160160
* @param defaultMemoryMbytes The string expression to evaluate (e.g., "get(input, 'size', 10) * 1024").
161161
* @param context The `MemoryEvaluationContext` (containing `input` and `runOptions`) available to the expression.
162-
* @returns The calculated memory value rounded to the closest power of 2,
163-
* or `undefined` if the expression fails, is non-numeric,
164-
* or results in a non-positive value.
162+
* @returns The calculated memory value rounded to the closest power of 2 clamped within allowed limits.
165163
*/
166164
export const calculateDefaultMemoryFromExpression = (
167165
defaultMemoryMbytes: string,
@@ -172,8 +170,8 @@ export const calculateDefaultMemoryFromExpression = (
172170
throw new Error(`The defaultMemoryMbytes expression is too long. Max length is ${DEFAULT_MEMORY_MBYTES_MAX_CHARS} characters.`);
173171
}
174172

175-
// Replaces all occurrences of {{variable}} with runOptions.variable
176-
// e.g., "{{memoryMbytes}} + 1024" becomes "runOptions.memoryMbytes + 1024"
173+
// Replaces all occurrences of {{variable}} with variable
174+
// e.g., "{{runOptions.memoryMbytes}} + 1024" becomes "runOptions.memoryMbytes + 1024"
177175
const preProcessedExpression = preprocessDefaultMemoryExpression(defaultMemoryMbytes);
178176

179177
const preparedContext = {

test/memory_calculator.test.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('calculateDefaultMemoryFromExpression', () => {
99
describe('Basic Evaluation', () => {
1010
it('correctly calculates and rounds memory from one-line expression', () => {
1111
const context = { input: { size: 10 }, runOptions: {} };
12-
// 10 * 1024 = 10240. log2(10240) ~ 13.32. round(13) -> 2^13 = 8192
12+
// 10 * 1024 = 10240. log2(10240) ~ 13.32. round(13.32) -> 2^13 = 8192
1313
const result = calculateDefaultMemoryFromExpression('input.size * 1024', context);
1414
expect(result).toBe(8192);
1515
});
@@ -35,7 +35,6 @@ describe('calculateDefaultMemoryFromExpression', () => {
3535
});
3636

3737
it('correctly handles a single number expression', () => {
38-
// 2048 is 2^11
3938
const result = calculateDefaultMemoryFromExpression('2048', emptyContext);
4039
expect(result).toBe(2048);
4140
});
@@ -66,23 +65,20 @@ describe('calculateDefaultMemoryFromExpression', () => {
6665
it('correctly replaces {{runOptions.variable}} with valid runOptions.variable', () => {
6766
const context = { input: {}, runOptions: { memoryMbytes: 16 } };
6867
const expr = '{{runOptions.memoryMbytes}} * 1024';
69-
// 16 * 1024 = 16384, which is 2^14
7068
const result = calculateDefaultMemoryFromExpression(expr, context);
7169
expect(result).toBe(16384);
7270
});
7371

7472
it('correctly replaces {{input.variable}} with valid input.variable', () => {
7573
const context = { input: { value: 16 }, runOptions: { } };
7674
const expr = '{{input.value}} * 1024';
77-
// 16 * 1024 = 16384, which is 2^14
7875
const result = calculateDefaultMemoryFromExpression(expr, context);
7976
expect(result).toBe(16384);
8077
});
8178

82-
it('correctly throw error if runOptions variable is invalid', () => {
79+
it('should throw error if runOptions variable is invalid', () => {
8380
const context = { input: { value: 16 }, runOptions: { } };
8481
const expr = '{{runOptions.customVariable}} * 1024';
85-
// 16 * 1024 = 16384, which is 2^14
8682
expect(() => calculateDefaultMemoryFromExpression(expr, context))
8783
.toThrow(`Invalid variable '{{runOptions.customVariable}}' in expression. Only the following runOptions are allowed:`);
8884
});
@@ -132,27 +128,23 @@ describe('calculateDefaultMemoryFromExpression', () => {
132128
.toThrow();
133129
});
134130

135-
it('should return undefined for a 0 result', () => {
131+
it('should throw error if result is 0', () => {
136132
expect(() => calculateDefaultMemoryFromExpression('10 - 10', emptyContext)).toThrow(`Calculated memory value must be a positive number, greater than 0, got: 0`);
137133
});
138134

139-
it('should return undefined for a negative result', () => {
135+
it('should throw error if result is negative', () => {
140136
expect(() => calculateDefaultMemoryFromExpression('5 - 10', emptyContext)).toThrow(`Calculated memory value must be a positive number, greater than 0, got: -5`);
141137
});
142138

143-
it('should return undefined for a NaN result', () => {
139+
it('should throw error if result is NaN', () => {
144140
expect(() => calculateDefaultMemoryFromExpression('0 / 0', emptyContext)).toThrow('Failed to round number to a power of 2.');
145141
});
146142

147-
it('should return undefined for a non-numeric (string) result', () => {
143+
it('should throw error if result is a non-numeric (string)', () => {
148144
expect(() => calculateDefaultMemoryFromExpression("'hello'", emptyContext)).toThrow('Failed to round number to a power of 2.');
149145
});
150146

151-
it('should return undefined for a non-numeric (object) result', () => {
152-
expect(() => calculateDefaultMemoryFromExpression('{ a: 1, b: 2 }', emptyContext)).toThrow('Failed to round number to a power of 2.');
153-
});
154-
155-
it('should return error when disabled functionality of MathJS is used', () => {
147+
it('should throw error when disabled functionality of MathJS is used', () => {
156148
expect(() => calculateDefaultMemoryFromExpression('evaluate(512)', emptyContext)).toThrow('Function evaluate is disabled');
157149
});
158150
});

0 commit comments

Comments
 (0)