Skip to content

Commit 8e6bf33

Browse files
authored
fix: use timestamp instead of Date object (#511)
1 parent 9a86eaf commit 8e6bf33

File tree

3 files changed

+22
-25
lines changed

3 files changed

+22
-25
lines changed

Diff for: lib/core/context.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,31 @@ export interface RequestLog {
4242
/**
4343
* Request begin.
4444
*/
45-
requestStart: Date;
45+
requestStart: number;
4646
/**
4747
* request.on("socket")
4848
*/
49-
onSocket: Date;
49+
onSocket: number;
5050
/**
5151
* Exact time that dns look up done.
5252
*/
53-
onLookUp: Date;
53+
onLookUp: number;
5454
/**
5555
* Exact time that client finished sending HTTP request to the server.
5656
*/
57-
requestFinish: Date;
57+
requestFinish: number;
5858
/**
5959
* socket.on("connect")
6060
*/
61-
socketConnect: Date;
61+
socketConnect: number;
6262
/**
6363
* request.on("response")
6464
*/
65-
onResponse: Date;
65+
onResponse: number;
6666
/**
6767
* response.on("close")
6868
*/
69-
responseClose: Date;
69+
responseClose: number;
7070
/**
7171
* milliseconds Fiddler spent in DNS looking up the server's IP address.
7272
*/

Diff for: lib/core/runtime/capture/index.ts

+12-15
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export const hack = <T extends typeof http.request>(
9898
};
9999

100100
const { timestamps } = requestLog;
101-
timestamps.requestStart = new Date();
101+
timestamps.requestStart = new Date().getTime();
102102

103103
const clearDomain = (): void => {
104104
const parser = (request.socket as any)?.parser as any;
@@ -117,7 +117,7 @@ export const hack = <T extends typeof http.request>(
117117
};
118118

119119
request.once("socket", (socket: Socket): void => {
120-
timestamps.onSocket = new Date();
120+
timestamps.onSocket = new Date().getTime();
121121

122122
if (!isIP(hostname)) {
123123
socket.once("lookup", (
@@ -126,9 +126,8 @@ export const hack = <T extends typeof http.request>(
126126
family: string | number,
127127
host: string
128128
): void => {
129-
timestamps.onLookUp = new Date();
130-
timestamps.dnsTime = timestamps.onLookUp.getTime()
131-
- timestamps.onSocket.getTime();
129+
timestamps.onLookUp = new Date().getTime();
130+
timestamps.dnsTime = timestamps.onLookUp - timestamps.onSocket;
132131

133132
logger.debug(`${logPre} Dns lookup ${host} -> ${
134133
address || "null"}. Cost ${timestamps.dnsTime}ms`);
@@ -146,12 +145,12 @@ export const hack = <T extends typeof http.request>(
146145
}
147146

148147
socket.once("connect", (): void => {
149-
timestamps.socketConnect = new Date();
148+
timestamps.socketConnect = new Date().getTime();
150149

151150
logger.debug(`${logPre} Socket connected. Remote: ${
152151
socket.remoteAddress
153152
}:${socket.remotePort}. Cost ${
154-
timestamps.socketConnect.getTime() - timestamps.onSocket.getTime()
153+
timestamps.socketConnect - timestamps.onSocket
155154
} ms`);
156155
});
157156

@@ -177,7 +176,7 @@ export const hack = <T extends typeof http.request>(
177176
request.once("close", clearDomain);
178177

179178
request.once("finish", () => {
180-
timestamps.requestFinish = new Date();
179+
timestamps.requestFinish = new Date().getTime();
181180

182181
context.captureSN += 1;
183182

@@ -196,14 +195,14 @@ export const hack = <T extends typeof http.request>(
196195
logger.debug(`${logPre} Request send finish. Body size ${
197196
length
198197
}. Cost: ${
199-
timestamps.requestFinish.getTime() - timestamps.onSocket.getTime()
198+
timestamps.requestFinish - timestamps.onSocket
200199
} ms`);
201200

202201
clearDomain();
203202
});
204203

205204
request.once("response", (response: http.IncomingMessage): void => {
206-
timestamps.onResponse = new Date();
205+
timestamps.onResponse = new Date().getTime();
207206

208207
const { socket } = response;
209208
requestLog.serverIp = socket.remoteAddress;
@@ -220,15 +219,14 @@ export const hack = <T extends typeof http.request>(
220219
}:${socket.remotePort}. Response status code: ${
221220
response.statusCode
222221
}. Cost: ${
223-
timestamps.onResponse.getTime()
224-
- timestamps.onSocket.getTime()
222+
timestamps.onResponse - timestamps.onSocket
225223
} ms`);
226224

227225
// responseInfo can't retrieve data until response "end" event
228226
const responseInfo = captureIncoming(response);
229227

230228
response.once("end", () => {
231-
timestamps.responseClose = new Date();
229+
timestamps.responseClose = new Date().getTime();
232230

233231
requestLog.statusCode = response.statusCode;
234232
requestLog.responseLength = responseInfo.bodyLength;
@@ -262,8 +260,7 @@ export const hack = <T extends typeof http.request>(
262260
logger.debug(`${logPre} Response on end. Body size:${
263261
requestLog.responseLength
264262
}. Cost: ${
265-
timestamps.responseClose.getTime()
266-
- timestamps.onSocket.getTime()
263+
timestamps.responseClose - timestamps.onSocket
267264
} ms`);
268265

269266
finishRequest();

Diff for: lib/core/runtime/create-server.hack.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const hack = <T extends typeof http.createServer>(
4444
}
4545

4646
const requestListenerWrap: http.RequestListener = (req, res) => {
47-
const start = new Date();
47+
const start = new Date().getTime();
4848
const timestamps: RequestLog["timestamps"] = {
4949
dnsTime: 0,
5050
requestStart: start,
@@ -80,7 +80,7 @@ export const hack = <T extends typeof http.createServer>(
8080
res.writeHead = ((fn): typeof res.writeHead => (
8181
...args: unknown[]
8282
): ReturnType<typeof res.writeHead> => {
83-
timestamps.onResponse = new Date();
83+
timestamps.onResponse = new Date().getTime();
8484

8585
const context = currentContext();
8686

@@ -154,7 +154,7 @@ export const hack = <T extends typeof http.createServer>(
154154
});
155155

156156
res.once("close", () => {
157-
timestamps.responseClose = new Date();
157+
timestamps.responseClose = new Date().getTime();
158158

159159
const context = currentContext();
160160

0 commit comments

Comments
 (0)