Skip to content

Commit

Permalink
fix: per-call encoding count argument
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmeese7 committed Mar 25, 2024
1 parent 9258b32 commit f63a5a2
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,26 @@ import { Op } from './ParseOp';
import ParseRelation from './ParseRelation';

const MAX_RECURSIVE_CALLS = 999;
let recursiveCallsCount = 0;

function encode(
value: mixed,
disallowObjects: boolean,
forcePointers: boolean,
seen: Array<mixed>,
offline: boolean
offline: boolean,
counter: number = 0

Check warning on line 21 in src/encode.js

View check run for this annotation

Codecov / codecov/patch

src/encode.js#L21

Added line #L21 was not covered by tests
): any {
recursiveCallsCount++;
counter++;

if (recursiveCallsCount > MAX_RECURSIVE_CALLS) {
const errorMessage = 'Maximum recursive calls exceeded in encode function. Potential infinite recursion detected.';
console.warn(errorMessage);
console.debug('Value causing potential infinite recursion:', value);
console.debug('Disallow objects:', disallowObjects);
console.debug('Force pointers:', forcePointers);
console.debug('Seen:', seen);
console.debug('Offline:', offline);
if (counter > MAX_RECURSIVE_CALLS) {
console.error('Maximum recursive calls exceeded in encode function. Potential infinite recursion detected.');
console.error('Value causing potential infinite recursion:', value);
console.error('Disallow objects:', disallowObjects);
console.error('Force pointers:', forcePointers);
console.error('Seen:', seen);
console.error('Offline:', offline);

Check warning on line 31 in src/encode.js

View check run for this annotation

Codecov / codecov/patch

src/encode.js#L26-L31

Added lines #L26 - L31 were not covered by tests

throw new Error(errorMessage);
throw new Error('Maximum recursive calls exceeded in encode function. Potential infinite recursion detected.');

Check warning on line 33 in src/encode.js

View check run for this annotation

Codecov / codecov/patch

src/encode.js#L33

Added line #L33 was not covered by tests
}

if (value instanceof ParseObject) {
Expand Down Expand Up @@ -84,14 +83,14 @@ function encode(

if (Array.isArray(value)) {
return value.map(v => {
return encode(v, disallowObjects, forcePointers, seen, offline);
return encode(v, disallowObjects, forcePointers, seen, offline, counter);
});
}

if (value && typeof value === 'object') {
const output = {};
for (const k in value) {
output[k] = encode(value[k], disallowObjects, forcePointers, seen, offline);
output[k] = encode(value[k], disallowObjects, forcePointers, seen, offline, counter);
}
return output;
}
Expand All @@ -106,6 +105,5 @@ export default function (
seen?: Array<mixed>,
offline?: boolean
): any {
recursiveCallsCount = 0;
return encode(value, !!disallowObjects, !!forcePointers, seen || [], offline);
return encode(value, !!disallowObjects, !!forcePointers, seen || [], offline, 0);
}

0 comments on commit f63a5a2

Please sign in to comment.