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

fix(File): respect typed array byteOffset and byteLength #1601

Merged
merged 1 commit into from Aug 14, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/fetch/file.js
Expand Up @@ -278,7 +278,9 @@ function processBlobParts (parts, options) {
if (!element.buffer) { // ArrayBuffer
bytes.push(new Uint8Array(element))
} else {
bytes.push(element.buffer)
bytes.push(
new Uint8Array(element.buffer, element.byteOffset, element.byteLength)
)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this is still wrong. We need to make a copy, not just a new reference. Could you fix that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KhafraDev is this something you could look into? The copy part.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do. On vacation for the next 6 days but I'll see if I can get to it

} else if (isBlobLike(element)) {
// 3. If element is a Blob, append the bytes it represents
Expand Down
19 changes: 19 additions & 0 deletions test/fetch/file.js
Expand Up @@ -120,6 +120,16 @@ test('File.prototype.text', async (t) => {
t.equal(await file.text(), 'hello world')
t.end()
})

t.test('With TypedArray range', async (t) => {
const uint8_1 = new Uint8Array(Buffer.from('hello world'))
const uint8_2 = new Uint8Array(uint8_1.buffer, 1, 4)

const file = new File([uint8_2], 'hello_world.txt')

t.equal(await file.text(), 'ello')
t.end()
})
/* eslint-enable camelcase */

t.test('With ArrayBuffer', async (t) => {
Expand All @@ -140,6 +150,15 @@ test('File.prototype.text', async (t) => {
t.end()
})

t.test('With Buffer', async (t) => {
const buffer = Buffer.from('hello world')

const file = new File([buffer], 'hello_world.txt')

t.equal(await file.text(), 'hello world')
t.end()
})

t.test('Mixed', async (t) => {
const blob = new Blob(['Hello, '])
const uint8 = new Uint8Array(Buffer.from('world! This'))
Expand Down