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

@uppy/aws-s3: resolve all headers on response #5195

Merged
merged 4 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions packages/@uppy/aws-s3-multipart/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ describe('AwsS3Multipart', () => {
expect(uploadSuccessHandler.mock.calls[0][1]).toStrictEqual({
body: {
ETag: 'test',
etag: 'test',
location: 'http://example.com',
},
status: 200,
Expand Down Expand Up @@ -257,16 +258,16 @@ describe('AwsS3Multipart', () => {
.mock.calls[0][1]

expect(completeCall.parts).toEqual([
{ ETag: 'test', PartNumber: 1 },
{ ETag: 'test', PartNumber: 2 },
{ ETag: 'test', PartNumber: 3 },
{ ETag: 'test', PartNumber: 4 },
{ ETag: 'test', PartNumber: 5 },
{ ETag: 'test', PartNumber: 6 },
{ ETag: 'test', PartNumber: 7 },
{ ETag: 'test', PartNumber: 8 },
{ ETag: 'test', PartNumber: 9 },
{ ETag: 'test', PartNumber: 10 },
{ ETag: 'test', etag: 'test', PartNumber: 1 },
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
{ ETag: 'test', etag: 'test', PartNumber: 2 },
{ ETag: 'test', etag: 'test', PartNumber: 3 },
{ ETag: 'test', etag: 'test', PartNumber: 4 },
{ ETag: 'test', etag: 'test', PartNumber: 5 },
{ ETag: 'test', etag: 'test', PartNumber: 6 },
{ ETag: 'test', etag: 'test', PartNumber: 7 },
{ ETag: 'test', etag: 'test', PartNumber: 8 },
{ ETag: 'test', etag: 'test', PartNumber: 9 },
{ ETag: 'test', etag: 'test', PartNumber: 10 },
])
})

Expand Down
23 changes: 17 additions & 6 deletions packages/@uppy/aws-s3-multipart/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ export default class AwsS3Multipart<
;(error as any).source = { status: 403 }
reject(error)
})
xhr.addEventListener('load', (ev) => {
xhr.addEventListener('load', () => {
cleanup()

if (
Expand All @@ -748,9 +748,20 @@ export default class AwsS3Multipart<
// todo make a proper onProgress API (breaking change)
onProgress?.({ loaded: size, lengthComputable: true })

// NOTE This must be allowed by CORS.
const etag = xhr.getResponseHeader('ETag')
const location = xhr.getResponseHeader('Location')
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/getAllResponseHeaders#examples
const arr = xhr
.getAllResponseHeaders()
.trim()
.split(/[\r\n]+/)
// @ts-expect-error null is allowed to avoid inherited properties
const headersMap: Record<string, string> = { __proto__: null }
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
for (const line of arr) {
const parts = line.split(': ')
const header = parts.shift()!
const value = parts.join(': ')
headersMap[header] = value
}
const { etag, location } = headersMap

if (method.toUpperCase() === 'POST' && location === null) {
// Not being able to read the Location header is not a fatal error.
Expand All @@ -770,8 +781,8 @@ export default class AwsS3Multipart<

onComplete?.(etag)
resolve({
ETag: etag,
...(location ? { location } : undefined),
...headersMap,
ETag: etag, // keep capitalised ETag for backwards compatiblity
})
})

Expand Down
Loading