Skip to content
Open
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
3 changes: 2 additions & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}),
Expand Down
24 changes: 24 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '—' } });
Expand Down