From aafe79c44587b17686279be15a934879a91c28e9 Mon Sep 17 00:00:00 2001 From: Abhinav Kumar Singh Date: Sat, 15 Aug 2026 14:34:07 +0530 Subject: [PATCH] fix: preserve ArrayBufferView bounds --- src/core.ts | 3 ++- tests/index.test.ts | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/core.ts b/src/core.ts index 10b9d3f..fe07747 100644 --- a/src/core.ts +++ b/src/core.ts @@ -273,7 +273,8 @@ export abstract class APIClient { opts && isBlobLike(opts?.body) ? new DataView(await opts.body.arrayBuffer()) : opts?.body instanceof DataView ? opts.body : opts?.body instanceof ArrayBuffer ? new DataView(opts.body) - : opts && ArrayBuffer.isView(opts?.body) ? new DataView(opts.body.buffer) + : opts && ArrayBuffer.isView(opts?.body) ? + new DataView(opts.body.buffer, opts.body.byteOffset, opts.body.byteLength) : opts?.body; return { method, path, ...opts, body }; }), diff --git a/tests/index.test.ts b/tests/index.test.ts index 7639b23..c8282bf 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -236,6 +236,30 @@ describe('instantiate client', () => { describe('request building', () => { const client = new Browserbase({ apiKey: 'My API Key' }); + describe('binary bodies', () => { + test.each([ + ['Uint8Array', new Uint8Array([10, 20, 30, 40]), [10, 20, 30, 40]], + ['Uint8Array subarray', new Uint8Array([10, 20, 30, 40]).subarray(1, 3), [20, 30]], + ['Buffer subarray', Buffer.from([10, 20, 30, 40]).subarray(1, 3), [20, 30]], + ])('preserves the bounds of a %s', async (_name, input, expected) => { + let capturedBody: unknown; + const binaryClient = new Browserbase({ + apiKey: 'My API Key', + maxRetries: 0, + fetch: async (_url, init) => { + capturedBody = init?.body; + return new Response(JSON.stringify({}), { headers: { 'Content-Type': 'application/json' } }); + }, + }); + + await binaryClient.post('/foo', { body: input, __binaryRequest: true }); + + expect(capturedBody).toBeInstanceOf(DataView); + const view = capturedBody as DataView; + expect(Array.from(new Uint8Array(view.buffer, view.byteOffset, view.byteLength))).toEqual(expected); + }); + }); + describe('Content-Length', () => { test('handles multi-byte characters', async () => { const { req } = await client.buildRequest({ path: '/foo', method: 'post', body: { value: '—' } });