Skip to content

streamPdfFile validates parameters lazily, not at call time #210

Description

@l2ysho

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions