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

Check request size for POST and POSTForm requests #24

Merged
merged 3 commits into from
Jul 4, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ type Method = "POST" | "GET" | "PUT" | "DELETE";
/* eslint-disable @typescript-eslint/no-explicit-any */

export class CortexApiClient {
private readonly maxRequestSize = 32 * 1000 * 1000; // API is configured with max request body size of 32mb

constructor(
private org: string,
private apiUrl: string,
Expand All @@ -27,6 +29,11 @@ export class CortexApiClient {
}

async POSTForm(path: string, form: FormData) {
const requestSize = CortexApiClient.getFormDataSize(form);
if (requestSize > this.maxRequestSize) {
throw new Error("Request body too large");
}

return fetch(`${this.apiUrl}/org/${this.org}${path}`, {
method: "POST",
headers: {
Expand All @@ -37,13 +44,29 @@ export class CortexApiClient {
}

private async makeRequest(method: Method, path: string, body?: any) {
const requestBody = body ? JSON.stringify(body) : undefined;
// Note that we use character size instead of byte size. This is still a useful heuristic as we don't want to incur the overhead
// of using TextEncoder to calculate the precise byte count
if (requestBody && requestBody.length > this.maxRequestSize) {
throw new Error("Request body too large");
}

return fetch(`${this.apiUrl}/org/${this.org}${path}`, {
method,
headers: {
Authorization: `Bearer ${this.accessToken}`,
"Content-Type": "application/json",
},
body: body ? JSON.stringify(body) : undefined,
body: requestBody,
});
}

private static getFormDataSize(formData: FormData) {
return [...formData].reduce(
(size, [_, value]) =>
// Use heuristic of string length instead of byte size, to avoid incurring the cost of using TextEncoder
size + (typeof value === "string" ? value.length : value.size),
0,
);
}
}
6 changes: 6 additions & 0 deletions content.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ test(
expect(content.version).toBe(0);
expect(content.commands.length).toBe(1);

// check that prompt that is too large will fail gracefully without hitting the service
const hugePrompt = "p".repeat(32 * 1000 * 1000);
await expect(() =>
Copy link
Member

Choose a reason for hiding this comment

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

Do you think this should be a separate test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I considered that, but prompt is a natural fit for testing this because it can be large but also easy to manipulate in test code.

cortex.generateContent({ title, prompt: hugePrompt }),
).rejects.toThrow("Request body too large");

// get content
const getContent = await testClient.getContent(content.id);
expect(getContent.content.length).toBe(content.content.length);
Expand Down