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
26 changes: 1 addition & 25 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,24 +288,6 @@ export abstract class APIClient {
return this.requestAPIList(Page, { method: 'get', path, ...opts });
}

private calculateContentLength(body: unknown): string | null {
if (typeof body === 'string') {
if (typeof Buffer !== 'undefined') {
return Buffer.byteLength(body, 'utf8').toString();
}

if (typeof TextEncoder !== 'undefined') {
const encoder = new TextEncoder();
const encoded = encoder.encode(body);
return encoded.length.toString();
}
} else if (ArrayBuffer.isView(body)) {
return body.byteLength.toString();
}

return null;
}

async buildRequest<Req>(
inputOptions: FinalRequestOptions<Req>,
{ retryCount = 0 }: { retryCount?: number } = {},
Expand All @@ -319,8 +301,6 @@ export abstract class APIClient {
: isMultipartBody(options.body) ? options.body.body
: options.body ? JSON.stringify(options.body, null, 2)
: null;
const contentLength = this.calculateContentLength(body);

const url = this.buildURL(path!, query, defaultBaseURL);
if ('timeout' in options) validatePositiveInteger('timeout', options.timeout);
options.timeout = options.timeout ?? this.timeout;
Expand All @@ -342,7 +322,7 @@ export abstract class APIClient {
headers[this.idempotencyHeader] = inputOptions.idempotencyKey;
}

const reqHeaders = this.buildHeaders({ options, headers, contentLength, retryCount });
const reqHeaders = this.buildHeaders({ options, headers, retryCount });

const req: RequestInit = {
method,
Expand All @@ -365,13 +345,9 @@ export abstract class APIClient {
}: {
options: FinalRequestOptions;
headers: Record<string, string | null | undefined>;
contentLength: string | null | undefined;
retryCount: number;
}): Record<string, string> {
const reqHeaders: Record<string, string> = {};
if (contentLength) {
reqHeaders['content-length'] = contentLength;
}

const defaultHeaders = this.defaultHeaders(options);
applyHeadersMut(reqHeaders, defaultHeaders);
Expand Down
9 changes: 2 additions & 7 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,9 @@ describe('request building', () => {
const client = new Browserbase({ apiKey: 'My API Key' });

describe('Content-Length', () => {
test('handles multi-byte characters', async () => {
const { req } = await client.buildRequest({ path: '/foo', method: 'post', body: { value: '—' } });
expect((req.headers as Record<string, string>)['content-length']).toEqual('20');
});

test('handles standard characters', async () => {
test('lets fetch compute the request content length', async () => {
const { req } = await client.buildRequest({ path: '/foo', method: 'post', body: { value: 'hello' } });
expect((req.headers as Record<string, string>)['content-length']).toEqual('22');
expect(req.headers as Record<string, string>).not.toHaveProperty('content-length');
});
});

Expand Down