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

nodejs Buffer as Uint8Array cause bad format #313

Closed
i2534 opened this issue Mar 15, 2022 · 1 comment
Closed

nodejs Buffer as Uint8Array cause bad format #313

i2534 opened this issue Mar 15, 2022 · 1 comment

Comments

@i2534
Copy link

i2534 commented Mar 15, 2022

Buffer is extends Uint8Array, so I pass it to Uint8ArrayReader
When Buffer(child) is slice from another Buffer(parent) and offset is not 0,
at zip-reader#seekSignature, line 513, bytes type is Buffer, so after slice's buffer is parent's data....
Buffer's slice only mark offset and length, original is keep,
in it's document: To copy the slice, use`Uint8Array.prototype.slice()

my test code(not complete):
`class Reader implements Readable {

        private readonly buffer: Buffer;

        constructor(buffer: Buffer) {
            this.buffer = buffer;
        }

        public get size(): number {
            return this.buffer.length;
        }

        async init(): Promise<void> {
        }
        async read(index: number, length: number): Promise<Uint8Array> {
            //return Uint8Array.prototype.slice.call(this.buffer, index, index + length);
            return this.buffer.slice(index, index + length);
        }
        close(): void {
        }
    }

    it('Readable', async () => {
        const reader = new Reader(buff);
        const zip = new Zip(reader);
        zip.names().then(n => console.log(n))
    });`
@gildas-lormeau
Copy link
Owner

This is due to incompatibilities in Node.js, see https://nodejs.org/api/buffer.html#buffer_buffers_and_typedarrays.

Buffer.prototype.slice() creates a view over the existing Buffer without copying.

To circumvent this issue due to Node.js, you have to transform the Buffer object into a Uint8Array object. This kind of issue is why zip.js is not officially supported in Node.js.

For the record, this is a duplicate of #305.

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