fix: "cannot access variable before initialization" errors in route2 (#8978)

Co-authored-by: Jennifer Shehane <jennifer@cypress.io>
This commit is contained in:
Kukhyeon Heo
2020-10-30 04:11:20 +09:00
committed by GitHub
parent 18f4d9a09a
commit 756b6b8b0e

View File

@@ -47,50 +47,12 @@ export const onRequestReceived: HandlerFn<NetEventFrames.HttpRequestReceived> =
const route = getRoute(frame.routeHandlerId)
const { req, requestId, routeHandlerId } = frame
const sendContinueFrame = () => {
if (continueSent) {
throw new Error('sendContinueFrame called twice in handler')
}
continueSent = true
if (request) {
request.state = 'Intercepted'
}
if (continueFrame) {
// copy changeable attributes of userReq to req in frame
// @ts-ignore
continueFrame.req = {
..._.pick(userReq, SERIALIZABLE_REQ_PROPS),
}
_.merge(request.request, continueFrame.req)
emitNetEvent('http:request:continue', continueFrame)
}
}
if (!route) {
return sendContinueFrame()
}
const request: Partial<Request> = {
id: requestId,
request: req,
state: 'Received',
}
request.log = getRequestLog(route, request as Omit<Request, 'log'>)
// TODO: this misnomer is a holdover from XHR, should be numRequests
route.log.set('numResponses', (route.log.get('numResponses') || 0) + 1)
route.requests[requestId] = request as Request
if (frame.notificationOnly) {
return
}
const continueFrame: Partial<NetEventFrames.HttpRequestContinue> = {
routeHandlerId,
requestId,
@@ -98,9 +60,6 @@ export const onRequestReceived: HandlerFn<NetEventFrames.HttpRequestReceived> =
let resolved = false
let replyCalled = false
let continueSent = false
route.hitCount++
const userReq: CyHttpMessages.IncomingHttpRequest = {
...req,
@@ -154,6 +113,48 @@ export const onRequestReceived: HandlerFn<NetEventFrames.HttpRequestReceived> =
},
}
let continueSent = false
const sendContinueFrame = () => {
if (continueSent) {
throw new Error('sendContinueFrame called twice in handler')
}
continueSent = true
if (request) {
request.state = 'Intercepted'
}
if (continueFrame) {
// copy changeable attributes of userReq to req in frame
// @ts-ignore
continueFrame.req = {
..._.pick(userReq, SERIALIZABLE_REQ_PROPS),
}
_.merge(request.request, continueFrame.req)
emitNetEvent('http:request:continue', continueFrame)
}
}
if (!route) {
return sendContinueFrame()
}
request.log = getRequestLog(route, request as Omit<Request, 'log'>)
// TODO: this misnomer is a holdover from XHR, should be numRequests
route.log.set('numResponses', (route.log.get('numResponses') || 0) + 1)
route.requests[requestId] = request as Request
if (frame.notificationOnly) {
return
}
route.hitCount++
if (!_.isFunction(route.handler)) {
return sendContinueFrame()
}