aws / aws-sdk-js-v3

Modularized AWS SDK for JavaScript.
Apache License 2.0
3.03k stars 569 forks source link

PutObjectCommand output error when file is added to the body #6385

Open hamzzy opened 1 month ago

hamzzy commented 1 month ago

Checkboxes for prior research

Describe the bug

If I use an empty body, the code executes without issues. However, when I attach a file buffer to the body, I encounter the following error:

const command = new PutObjectCommand({
    Body: "",
    Bucket: this.bucketName,
    Key: key,
});

SDK version number

@aws-sdk/client-s3 ^3.629.0

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

20.0.0

Reproduction Steps

const command = new PutObjectCommand({
    Body: "Hello world",
    Bucket: this.bucketName,
    Key: key,
});

Observed Behavior

node:internal/deps/undici/undici:5901
        throw new RangeError('init["status"] must be in the range of 200 to 599, inclusive.');
        ^

RangeError: init["status"] must be in the range of 200 to 599, inclusive.
    at initializeResponse (node:internal/deps/undici/undici:5901:15)
    at new Response (node:internal/deps/undici/undici:5713:9)
    at Object.construct (/Users/user/susi-andromeda/node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/utils/recordRawHeaders.ts:177:32)
    at MockHttpSocket.MockHttpSocket.onResponseStart (/Users/user/susi-andromeda/node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/MockHttpSocket.ts:547:22)
    at Object.read (/Users/user/susi-andromeda/node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/MockHttpSocket.ts:80:31)
    at MockHttpSocket.push (/Users/user/susi-andromeda/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts:56:18)
    at TLSSocket.<anonymous> (/Users/user/susi-andromeda/node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/MockHttpSocket.ts:247:14)
    at TLSSocket.emit (node:events:517:28)
    at TLSSocket.emit (node:domain:489:12)
    at addChunk (node:internal/streams/readable:368:12)

Expected Behavior

it should successfully upload the file to Cloudflare r2

Possible Solution

No response

Additional Information/Context

No response

hamzzy commented 1 month ago

@aBurmeseDev

aBurmeseDev commented 1 month ago

Hi @hamzzy - thanks for your patience.

It appears that the error you're seeing comes from @mswjs/interceptors library, which is used for mocking HTTP requests during testing. This library seems to be intercepting the request made by the PutObjectCommand and attempting to mock the response, but it's encountering an issue with the response status code.

The error message suggests that the mocked response is attempting to set an invalid status code. This could be due to a configuration issue or a bug in the library itself, rather than SDK.

To narrow down the root cause, you can try disabling the library/interceptor for the request that's causing the issue. You can do this by modifying your test setup or configuration to exclude the PutObjectCommand from being intercepted.

Check if you have the latest version of @mswjs/interceptors: Sometimes, issues like this are resolved in newer versions of the library. Reach out to https://github.com/mswjs/interceptors/issues.

It's worth noting that this error is likely related to the mocking library and not the AWS SDK itself, as the SDK should handle the request and response correctly when interacting with the actual AWS S3 service.