Skip to content

Commit

Permalink
fix: support SharedArrayBuffer in HttpResponse.arrayBuffer (#2389)
Browse files Browse the repository at this point in the history
Co-authored-by: Artem Zakharchenko <[email protected]>
  • Loading branch information
danilofuchs and kettanaito authored Dec 15, 2024
1 parent a6c419c commit 41f00e1
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 11 deletions.
91 changes: 81 additions & 10 deletions src/core/HttpResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,89 @@ describe('HttpResponse.html()', () => {
})
})

it('creates an array buffer response', async () => {
const buffer = new TextEncoder().encode('hello world')
const response = HttpResponse.arrayBuffer(buffer)
describe('HttpResponse.arrayBuffer()', () => {
it('creates an array buffer response', async () => {
const buffer = new TextEncoder().encode('hello world')
const response = HttpResponse.arrayBuffer(buffer)

expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)

const responseData = await response.arrayBuffer()
expect(responseData).toEqual(buffer.buffer)
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-length': '11',
const responseData = await response.arrayBuffer()
expect(responseData).toEqual(buffer.buffer)
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-length': '11',
'content-type': 'application/octet-stream',
})
})

it('allows overriding the "Content-Type" response header', async () => {
const buffer = new TextEncoder().encode('hello world')
const response = HttpResponse.arrayBuffer(buffer, {
headers: {
'Content-Type': 'text/plain; charset=utf-8',
},
})

expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)

const responseData = await response.arrayBuffer()
expect(responseData).toEqual(buffer.buffer)
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-length': '11',
'content-type': 'text/plain; charset=utf-8',
})
})

it('creates an array buffer response from a shared array buffer', async () => {
const arrayBuffer = new TextEncoder().encode('hello world')

// Copy the data from the array buffer to a shared array buffer
const sharedBuffer = new SharedArrayBuffer(11)
const sharedView = new Uint8Array(sharedBuffer)
sharedView.set(arrayBuffer)

const response = HttpResponse.arrayBuffer(sharedBuffer)

expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)

const responseData = await response.arrayBuffer()
expect(responseData).toEqual(arrayBuffer.buffer)
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-length': '11',
'content-type': 'application/octet-stream',
})
})

it('allows overriding the "Content-Type" response header for shared array buffers', async () => {
const arrayBuffer = new TextEncoder().encode('hello world')

// Copy the data from the array buffer to a shared array buffer
const sharedBuffer = new SharedArrayBuffer(11)
const sharedView = new Uint8Array(sharedBuffer)
sharedView.set(arrayBuffer)

const response = HttpResponse.arrayBuffer(sharedBuffer, {
headers: {
'Content-Type': 'text/plain; charset=utf-8',
},
})

expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)

const responseData = await response.arrayBuffer()
expect(responseData).toEqual(arrayBuffer.buffer)
expect(Object.fromEntries(response.headers.entries())).toEqual({
'content-length': '11',
'content-type': 'text/plain; charset=utf-8',
})
})
})

Expand Down
9 changes: 8 additions & 1 deletion src/core/HttpResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,16 @@ export class HttpResponse extends Response {
*
* HttpResponse.arrayBuffer(buffer)
*/
static arrayBuffer(body?: ArrayBuffer, init?: HttpResponseInit): Response {
static arrayBuffer(
body?: ArrayBuffer | SharedArrayBuffer,
init?: HttpResponseInit,
): Response {
const responseInit = normalizeResponseInit(init)

if (!responseInit.headers.has('Content-Type')) {
responseInit.headers.set('Content-Type', 'application/octet-stream')
}

if (body && !responseInit.headers.has('Content-Length')) {
responseInit.headers.set('Content-Length', body.byteLength.toString())
}
Expand Down

0 comments on commit 41f00e1

Please sign in to comment.