Files
onenote-tools/src/onenote/parser/limits.ts

78 lines
2.4 KiB
TypeScript

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
import { DEFAULT_BROWSER_IMAGE_SAFETY_LIMITS } from '../image-safety.js';
export interface OneNoteParserLimits {
maxFileBytes: number;
maxTransactionFragments: number;
maxTransactionEntries: number;
maxListFragments: number;
maxListDepth: number;
maxFileNodes: number;
maxObjects: number;
maxPropertiesPerSet: number;
maxPropertyDepth: number;
maxStreamIds: number;
maxVectorBytes: number;
maxObjectReferences: number;
maxFileDataObjects: number;
maxFileDataBytes: number;
maxTotalFileDataBytes: number;
maxAutoRenderImageBytes: number;
maxAutoRenderImageDimension: number;
maxAutoRenderImagePixels: number;
maxGraphDepth: number;
maxContentBlocks: number;
maxTextRuns: number;
maxTableCells: number;
maxInkPoints: number;
maxPages: number;
maxDiagnostics: number;
maxExtractedTextChars: number;
}
export const DEFAULT_ONENOTE_PARSER_LIMITS: Readonly<OneNoteParserLimits> = {
maxFileBytes: 512 * 1024 * 1024,
maxTransactionFragments: 16_384,
maxTransactionEntries: 1_000_000,
maxListFragments: 16_384,
maxListDepth: 64,
maxFileNodes: 1_000_000,
maxObjects: 500_000,
maxPropertiesPerSet: 16_384,
maxPropertyDepth: 64,
maxStreamIds: 1_000_000,
maxVectorBytes: 64 * 1024 * 1024,
maxObjectReferences: 1_000_000,
maxFileDataObjects: 10_000,
maxFileDataBytes: 256 * 1024 * 1024,
maxTotalFileDataBytes: 512 * 1024 * 1024,
maxAutoRenderImageBytes: DEFAULT_BROWSER_IMAGE_SAFETY_LIMITS.maxBytes,
maxAutoRenderImageDimension: DEFAULT_BROWSER_IMAGE_SAFETY_LIMITS.maxDimension,
maxAutoRenderImagePixels: DEFAULT_BROWSER_IMAGE_SAFETY_LIMITS.maxPixels,
maxGraphDepth: 256,
maxContentBlocks: 100_000,
maxTextRuns: 100_000,
maxTableCells: 100_000,
maxInkPoints: 100_000,
maxPages: 10_000,
maxDiagnostics: 10_000,
maxExtractedTextChars: 2_000_000,
};
export function resolveParserLimits(
limits: Partial<OneNoteParserLimits> = {}
): OneNoteParserLimits {
const resolved = { ...DEFAULT_ONENOTE_PARSER_LIMITS, ...limits };
for (const [name, value] of Object.entries(resolved)) {
if (!Number.isSafeInteger(value) || value <= 0) {
throw new TypeError(`${name} must be a positive safe integer`);
}
}
return resolved;
}