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

[Bug]: Uncaught error on unexpected data after end of stream #18876

Closed
CyberAndrii opened this issue Oct 10, 2024 · 0 comments · Fixed by #18878
Closed

[Bug]: Uncaught error on unexpected data after end of stream #18876

CyberAndrii opened this issue Oct 10, 2024 · 0 comments · Fixed by #18878
Assignees

Comments

@CyberAndrii
Copy link
Contributor

CyberAndrii commented Oct 10, 2024

Attach (recommended) or Link to PDF file

PDF_FILE-1.pdf

Web browser and its version

Chrome 129.0.6668.10 / Firefox 131.0

Operating system and its version

macOS Sonoma 14.6.1 / Ubuntu 22.04.5

PDF.js version

4.4.168

Is the bug present in the latest PDF.js version?

Yes

Is a browser extension

No

Steps to reproduce the problem

  1. Go to https://jsfiddle.net/tr2h1j8f/
  2. Open the DevTools console
  3. Click the Run button

What is the expected behavior?

No errors should appear in the console.

What went wrong?

Chrome

The Uncaught (in promise) TypeError: Junk found after end of compressed data. error appears in the console.

image

Firefox

The TypeError: Unexpected input after the end of stream error appears in the console.

image

Link to a viewer

No response

Additional context

Related code:

async asyncGetBytes() {
this.str.reset();
const bytes = this.str.getBytes();
try {
const { readable, writable } = new DecompressionStream("deflate");
const writer = writable.getWriter();
writer.write(bytes);
writer.close();
const chunks = [];
let totalLength = 0;
for await (const chunk of readable) {
chunks.push(chunk);
totalLength += chunk.byteLength;
}
const data = new Uint8Array(totalLength);
let offset = 0;
for (const chunk of chunks) {
data.set(chunk, offset);
offset += chunk.byteLength;
}
return data;
} catch {
// DecompressionStream failed (for example because there are some extra
// bytes after the end of the compressed data), so we fallback to our
// decoder.
// We already get the bytes from the underlying stream, so we just reuse
// them to avoid get them again.
this.str = new Stream(
bytes,
2 /* = header size (see ctor) */,
bytes.length,
this.str.dict
);
this.reset();
return null;
}
}

This error occurs because promises returned by writer.write(bytes); and writer.close(); are not being awaited. There’s a reason for this though which is explained in whatwg/compression#55 (comment).

A potential solution could be to discard the errors

writer.write(bytes).catch(() => {});
writer.close().catch(() => {});

They will still be caught by the catch block when awaited here

for await (const chunk of readable) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants