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

Archive differ when using and not using the web walker in deno #466

Closed
vi117 opened this issue Dec 4, 2023 · 1 comment
Closed

Archive differ when using and not using the web walker in deno #466

vi117 opened this issue Dec 4, 2023 · 1 comment

Comments

@vi117
Copy link

vi117 commented Dec 4, 2023

Reproduce

Reproduce the problem with the following code.

Deno version is

deno 1.38.4 (release, x86_64-pc-windows-msvc)
v8 12.0.267.1
typescript 5.2.2

The content of test.txt is "asdfz".

import * as zip from "https://deno.land/x/zipjs@v2.7.31/index.js";

async function testZipInDeno(useWebWorkers: boolean) {
    const blobWriter = new zip.BlobWriter("application/zip");
    const zipFile = new zip.ZipWriter(blobWriter, {
        bufferedWrite: true,
        dataDescriptor: false,
    });
    const filepath = "test.txt";

    const [file, stat] = await Promise.all([
        await Deno.open(filepath, { read: true }),
        await Deno.stat(filepath)]);

        const reader = {
        readable: file.readable
            // .pipeThrough(new TransformStream({
                // transform(chunk, controller) {
                    // console.log("read", chunk.length, "bytes", chunk.byteLength, "buffer", chunk.buffer);
                    // controller.enqueue(chunk);
                // }
            // }))
            ,
        size: stat.size,
    };

    await zipFile.add(
        filepath,
        reader,
        {
            useWebWorkers: useWebWorkers,
            useCompressionStream: true,
        }
    );

    await zipFile.close();

    const blob = await blobWriter.getData();
    console.log(blob.size);
}

await testZipInDeno(true);
await testZipInDeno(false);

The result is

image

It's the same problem as issue #453.

If you uncomment it and run it, it will output something like this:

read 5 bytes 5 buffer ArrayBuffer {
  [Uint8Contents]: <61 73 64 66 7a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 65436 more bytes>,    
  byteLength: 65536
}
297
read 5 bytes 5 buffer ArrayBuffer {
  [Uint8Contents]: <61 73 64 66 7a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 65436 more bytes>,    
  byteLength: 65536
}
217

Cause

function sendMessage(message, { worker, writer, onTaskFinished, transferStreams }) {
try {
let { value, readable, writable } = message;
const transferables = [];
if (value) {
message.value = value.buffer;
transferables.push(message.value);
}
if (transferStreams && transferStreamsSupported) {
if (readable) {
transferables.push(readable);
}
if (writable) {
transferables.push(writable);
}
} else {
message.readable = message.writable = null;
}
if (transferables.length) {
try {
worker.postMessage(message, transferables);
return true;
} catch (_error) {
transferStreamsSupported = false;
message.readable = message.writable = null;
worker.postMessage(message);
}
} else {
worker.postMessage(message);
}
} catch (error) {
if (writer) {
writer.releaseLock();
}
onTaskFinished();
throw error;
}
}

The problem is caused at line 242 message.value = value.buffer;
The size of 'Uint8Array' and 'ArrayBuffer' of 'Uint8Array.buffer' may differ when reading files from Deno.

The simple solution I think is the following code.

if (value.byteLength < value.buffer.byteLength) {
    message.value = value.buffer.slice(0, chunk.byteLength);
}
else {
    message.value = value.buffer;
}

Thanks for the work you do.

@gildas-lormeau
Copy link
Owner

gildas-lormeau commented Dec 4, 2023

Thank you very much for the detailed bug report and the fix proposal. I've just published the version 2.7.32 which includes the fix.

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

No branches or pull requests

2 participants