Problem
streamPdfFile (backing streamPdf2image and streamPdf2string) is declared as async function*. Calling an async function* returns an AsyncGenerator immediately — the body does not execute until the first .next() / for await iteration.
This means validateParameters() runs lazily, not eagerly. A caller wrapping the invocation in try/catch expecting early rejection on bad arguments will silently miss the error:
// This does NOT throw — error surfaces only on first iteration
try {
const stream = streamPdf2image('./file.pdf', { scale: 99 });
} catch (e) {
// never reached
}
// Error surfaces here instead
for await (const page of stream) { ... }
This is inconsistent with parsePdfFile (async function), which rejects eagerly at call time.
Expected behavior
Parameter validation errors should be thrown/rejected at call time, consistent with pdf2image, pdf2string, and parsePdf.
Possible fix
Separate validation from the generator body — validate eagerly, then return the generator:
export function streamPdf2image(
input: Buffer | string | Uint8Array | URL,
options?: AfppParseOptions,
): AsyncGenerator<StreamingResult<Buffer>> {
return streamPdfFile(PROCESSING_TYPE.IMAGE, input, options);
}
Where streamPdfFile itself validates synchronously before yielding, or by wrapping:
async function* streamPdfFile(...) {
// move validateParameters call to a non-async wrapper that returns the generator
}
Problem
streamPdfFile(backingstreamPdf2imageandstreamPdf2string) is declared asasync function*. Calling anasync function*returns anAsyncGeneratorimmediately — the body does not execute until the first.next()/for awaititeration.This means
validateParameters()runs lazily, not eagerly. A caller wrapping the invocation intry/catchexpecting early rejection on bad arguments will silently miss the error:This is inconsistent with
parsePdfFile(async function), which rejects eagerly at call time.Expected behavior
Parameter validation errors should be thrown/rejected at call time, consistent with
pdf2image,pdf2string, andparsePdf.Possible fix
Separate validation from the generator body — validate eagerly, then return the generator:
Where
streamPdfFileitself validates synchronously before yielding, or by wrapping: