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] Buffers modified inside multer storage class #1840

Closed
MatejSkrbis opened this issue Apr 4, 2022 · 9 comments · Fixed by #1841
Closed

[BUG] Buffers modified inside multer storage class #1840

MatejSkrbis opened this issue Apr 4, 2022 · 9 comments · Fixed by #1841

Comments

@MatejSkrbis
Copy link

Information

Previous version: 5.61.1
Current version: 6.105.4

I've had custom multer storage implementation for uploading files directly into azure storage blob. This worked fine in tsed v5, but stopped working in tsed v6. After debugging I figured out that all Buffer objects are somehow modified somewhen after initialization, breaking the code. (Blob client from azure is using some Buffers internally)

I'm not using the latest version of tsed as 6.105.4 is the latest where npm audit isn't producing errors.

Example

Here is the simplest example of custom multer storage that will fail on file upload.

import { PlatformMulterFile } from '@tsed/common';
import { StorageEngine } from 'multer';
import { Request } from 'express';

export class BlobMemoryStorage implements StorageEngine {
    testBuffer: Buffer;

    constructor() {
        this.testBuffer = Buffer.from('Hello');
        console.log(this.testBuffer); // This will log out normally
    }

    _handleFile(
        req: Request,
        file: PlatformMulterFile,
        callback: (error?: any, info?: Partial<PlatformMulterFile>) => void
    ): void {
        console.log(this.testBuffer); // This will fail when uploading file as Buffer was modified
    }

    _removeFile(
        req: Request,
        file: PlatformMulterFile,
        callback: (error: Error) => void
    ): void {
        console.log(this.testBuffer);
    }
}

This example will produce following error when trying to upload the file.

TypeError: Method get TypedArray.prototype.length called on incompatible receiver [object Object]
    at Buffer.get length [as length] (<anonymous>)
    at Buffer.inspect (buffer.js:827:39)
    at internal/per_context/primordials.js:23:32
    at formatValue (internal/util/inspect.js:783:19)
    at inspect (internal/util/inspect.js:337:10)
    at formatWithOptionsInternal (internal/util/inspect.js:2016:40)
    at formatWithOptions (internal/util/inspect.js:1898:10)
    at console.value (internal/console/constructor.js:323:14)
    at console.log (internal/console/constructor.js:358:61)
    at console.originalConsole.<computed>

Acceptance criteria

The Buffer objects should probably not get modified as this will break some code.

@Romakita
Copy link
Collaborator

Romakita commented Apr 4, 2022

Hello @MatejSkrbis
Can you give a repo example to reproduce the error.
storage engine isn’t enough for me.

@MatejSkrbis
Copy link
Author

OK, I will try to make full example and upload it here.

@Romakita
Copy link
Collaborator

Romakita commented Apr 4, 2022

If you can init with the cli a small it’s fine and a controller to upload file with the storage engine it will be really appreciate ;)

@MatejSkrbis
Copy link
Author

Here is small repo with test controller. You just need to install the modules build it and run it.
To upload test file I simply used curl:

curl -v -F [email protected] http://localhost:8080/test

TestStorage.zip

@Romakita
Copy link
Collaborator

Romakita commented Apr 4, 2022

Ok I see the issue :)

@Romakita
Copy link
Collaborator

Romakita commented Apr 4, 2022

#1841

@Romakita
Copy link
Collaborator

Romakita commented Apr 4, 2022

Release in progress @MatejSkrbis

@Romakita
Copy link
Collaborator

Romakita commented Apr 4, 2022

🎉 This issue has been resolved in version 6.110.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@Romakita
Copy link
Collaborator

🎉 This issue has been resolved in version 7.0.0-beta.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging a pull request may close this issue.

2 participants