Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added maximumWalkingDepth option for supporting large documents #360

Merged
merged 2 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ const report = await createReport({
* Defaults to false.
*/
fixSmartQuotes: false;

/**
* Maximum loop iterations allowed when walking through the template.
* You can increase this to generate reports with large amount of FOR loop elements.
* Tip: You can disable infinite loop protection by using the `Infinity` constant.
* This may be useful if you implement a process timeout instead.
* (Default: 1,000,000)
*/
maximumWalkingDepth: 1_000_000;
});
```

Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ async function createReport(
options.processLineBreaksAsNewText == null
? false
: options.processLineBreaksAsNewText,
maximumWalkingDepth: options.maximumWalkingDepth,
};
const xmlOptions = { literalXmlDelimiter };

Expand Down
5 changes: 3 additions & 2 deletions src/processTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ export async function walkTemplate(
const errors: Error[] = [];

let loopCount = 0;
const maximumWalkingDepth = ctx.options?.maximumWalkingDepth || 1_000_000;
while (true) {
const curLoop = getCurLoop(ctx);
let nextSibling: Node | null = null;
Expand Down Expand Up @@ -249,14 +250,14 @@ export async function walkTemplate(
`=== parent is null, breaking after ${loopCount} loops...`
);
break;
} else if (loopCount > 1000000) {
} else if (loopCount > maximumWalkingDepth) {
// adding a emergency exit to avoid infit loops
logger.debug(
`=== parent is still not null after ${loopCount} loops, something must be wrong ...`,
debugPrintNode(parent)
);
throw new InternalError(
'something went wrong with the document. Please review and try again'
'infinite loop or massive dataset detected. Please review and try again'
);
}
nodeIn = parent;
Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ export type UserOptions = {
* (Default: false)
*/
processLineBreaksAsNewText?: boolean;

/**
* Maximum loop iterations allowed when walking through the template.
* You can increase this to generate reports with large amount of FOR loop elements.
* Tip: You can disable infinite loop protection by using the `Infinity` constant.
* This may be useful if you implement a process timeout instead.
* (Default: 1,000,000)
*/
maximumWalkingDepth?: number;
};

export type CreateReportOptions = {
Expand All @@ -132,6 +141,7 @@ export type CreateReportOptions = {
errorHandler: ErrorHandler | null;
fixSmartQuotes: boolean;
processLineBreaksAsNewText: boolean;
maximumWalkingDepth?: number;
};

export type SandBox = {
Expand Down
Loading