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

fix(sdk): get response size from bytesRead value #8313

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
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
6 changes: 3 additions & 3 deletions packages/insomnia-sdk/src/objects/__tests__/request.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest';

import { Header, HeaderList } from '../headers';
import { calculateRequestSize, mergeRequestBody, Request, RequestBody, RequestBodyOptions, toScriptRequestBody } from '../request';
import { calculatePayloadSize, mergeRequestBody, Request, RequestBody, RequestBodyOptions, toScriptRequestBody } from '../request';

describe('test request and response objects', () => {
it('test RequestBody methods', () => {
Expand Down Expand Up @@ -145,8 +145,8 @@ describe('test request and response objects', () => {
];

reqBodyTestCases.forEach(({ body, headers, expectedTotal }) => {
it(`test calculateRequestSize: ${body.raw}`, () => {
const reqSize = calculateRequestSize(new RequestBody(body), headers);
it(`test calculatePayloadSize: ${body.raw}`, () => {
const reqSize = calculatePayloadSize(new RequestBody(body).toString(), headers);

expect(reqSize.total).toEqual(expectedTotal);
});
Expand Down
23 changes: 14 additions & 9 deletions packages/insomnia-sdk/src/objects/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ export class Request extends Property {
}

size(): RequestSize {
return calculateRequestSize(this.body, this.headers);
return calculatePayloadSize((this.body || '').toString(), this.headers);
}

override toJSON() {
Expand Down Expand Up @@ -684,8 +684,18 @@ export function mergeRequests(
};
}

export function calculateRequestSize(body: RequestBody | undefined, headers: HeaderList<Header>): RequestSize {
const bodySize = new Blob([(body || '').toString()]).size;
export function calculatePayloadSize(body: string, headers: HeaderList<Header>): RequestSize {
const bodySize = new Blob([body]).size;
const headerSize = calculateHeadersSize(headers);
return {
body: bodySize,
header: headerSize,
total: bodySize + headerSize,
source: 'COMPUTED',
};
}

export function calculateHeadersSize(headers: HeaderList<Header>): number {
const headerSize = new Blob([
headers.reduce(
(acc, header) => (acc + header.toString() + '\n'),
Expand All @@ -694,10 +704,5 @@ export function calculateRequestSize(body: RequestBody | undefined, headers: Hea
),
]).size;

return {
body: bodySize,
header: headerSize,
total: bodySize + headerSize,
source: 'COMPUTED',
};
return headerSize;
}
24 changes: 15 additions & 9 deletions packages/insomnia-sdk/src/objects/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Cookie, type CookieOptions } from './cookies';
import { CookieList } from './cookies';
import { Header, type HeaderDefinition, HeaderList } from './headers';
import { Property, unsupportedError } from './properties';
import { Request } from './request';
import { calculateHeadersSize, Request } from './request';

export interface ResponseOptions {
code: number;
Expand All @@ -20,6 +20,7 @@ export interface ResponseOptions {
stream?: Buffer | ArrayBuffer;
responseTime: number;
originalRequest: Request;
bytesRead?: number; // this is from Insomnia for returning response size() directly
}

export interface ResponseContentInfo {
Expand All @@ -44,6 +45,8 @@ export class Response extends Property {
status: string;
stream?: Buffer | ArrayBuffer;

private bytesRead: number; //

constructor(options: ResponseOptions) {
super();

Expand All @@ -67,6 +70,8 @@ export class Response extends Property {
} else {
this.status = detectedStatus;
}

this.bytesRead = options.bytesRead || 0;
}

// TODO: the accurate type of the response should be given
Expand Down Expand Up @@ -180,14 +185,14 @@ export class Response extends Property {
return this.status;
}

size(): number {
try {
const contentLength = this.headers.get('Content-Length');
// TODO: improve this by manual counting
return contentLength == null ? -1 : parseInt(contentLength.valueOf());
} catch (e) {
throw Error('size: ${e}');
}
size() {
const headerSize = calculateHeadersSize(this.headers);
return {
body: this.bytesRead,
header: headerSize,
total: this.bytesRead + headerSize,
source: 'COMPUTED',
};
}

text() {
Expand Down Expand Up @@ -305,6 +310,7 @@ export function toScriptResponse(
// stream is duplicated with body
responseTime: partialResponse.elapsedTime,
originalRequest,
bytesRead: partialResponse.bytesRead,
};

return new Response(responseOption);
Expand Down
1 change: 1 addition & 0 deletions packages/insomnia/src/network/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@ export interface sendCurlAndWriteTimelineResponse extends ResponsePatch {
statusMessage: string;
cookies: Cookie[];
timeline: string[];
bytesRead?: number;
}

export async function sendCurlAndWriteTimeline(
Expand Down
Loading