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

- Calculates the response time #725

Merged
merged 1 commit into from
Sep 15, 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
10 changes: 10 additions & 0 deletions packages/http/src/interfaces/http-response.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,14 @@ export interface HttpResponseInterface {
* The request that was sent for getting this response.
*/
request: HttpRequestInterface;

/**
* The time it took to get the response (in ms).
*/
responseTime?: number;

/**
* The time it took to get the first byte of the response (in ms).
*/
timeToFirstByte?: number;
}
7 changes: 7 additions & 0 deletions packages/http/src/wrappers/http.wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export class HttpWrapper implements HttpWrapperInterface {
executeRequest(request: HttpRequestInterface): Promise<HttpResponseInterface> {
return new Promise((resolve, reject) => {
// Define the options required by the http and https modules.
let now: number = -1;

const url = new URL(request.url);
const options: RequestOptions = {
host: url.hostname,
Expand All @@ -48,20 +50,25 @@ export class HttpWrapper implements HttpWrapperInterface {
let body = '';

res.on('data', chunk => {
response.timeToFirstByte = performance.now() - now;
body = body + "" + chunk;
});

res.on('error', error => {
response.responseTime = performance.now() - now;
return reject(error);
})

res.on('end', async () => {
response.body = body;
response.responseTime = performance.now() - now;

return resolve(response);
});
}

now = performance.now();

// Make the http or https call depending on the url.
if (url.protocol === "http://" || url.protocol === "http:" || url.protocol === "http") {
const req = httpRequest(options, callback);
Expand Down