99import * as http from "http" ;
1010import * as https from "https" ;
1111import * as domain from "domain" ;
12- import currentContext , { RequestLog } from "../context" ;
12+ import currentContext , { Context , RequestLog } from "../context" ;
1313import { address } from "ip" ;
1414import { AddressInfo , isIP } from "net" ;
1515import { captureOutgoing } from "./capture/outgoing" ;
@@ -56,7 +56,7 @@ export const hack = <T extends typeof http.createServer>(
5656
5757 // Creating a domain and wrapping the execution.
5858 const d = domain . create ( ) ;
59-
59+ const context = new Context ( ) ;
6060 d . add ( req ) ;
6161 d . add ( res ) ;
6262
@@ -86,8 +86,6 @@ export const hack = <T extends typeof http.createServer>(
8686 ) : ReturnType < typeof res . writeHead > => {
8787 timestamps . onResponse = new Date ( ) . getTime ( ) ;
8888
89- const context = currentContext ( ) ;
90-
9189 eventBus . emit ( EVENT_LIST . RESPONSE_START , {
9290 req, res, context
9391 } ) ;
@@ -98,8 +96,6 @@ export const hack = <T extends typeof http.createServer>(
9896 } ) ( res . writeHead ) ;
9997
10098 res . once ( "finish" , ( ) => {
101- const context = currentContext ( ) ;
102-
10399 context . currentRequest = {
104100 SN : context . SN ,
105101
@@ -159,9 +155,6 @@ export const hack = <T extends typeof http.createServer>(
159155
160156 res . once ( "close" , ( ) => {
161157 timestamps . responseClose = new Date ( ) . getTime ( ) ;
162-
163- const context = currentContext ( ) ;
164-
165158 clearDomain ( ) ;
166159
167160 eventBus . emit ( EVENT_LIST . RESPONSE_CLOSE , {
@@ -170,7 +163,8 @@ export const hack = <T extends typeof http.createServer>(
170163 } ) ;
171164
172165 d . run ( ( ) => {
173- const context = currentContext ( ) ;
166+ process . domain . currentContext = context ;
167+
174168 eventBus . emit ( EVENT_LIST . REQUEST_START , {
175169 req, context
176170 } ) ;
0 commit comments