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(fromTraffic): clone response for subsequent requests #67

Merged
merged 2 commits into from
Oct 29, 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
2 changes: 1 addition & 1 deletion src/traffic/from-traffic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function fromTraffic(
await delay(entry.time)
}

return response
return isUniqueHandler ? response.clone() : response
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the repercussions of always cloning the response?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have detailed the repercussions of cloning (and not cloning) the response in this comment in the original issue. In brief summary, "cloning" a response does not duplicate the response data in memory, it points to the same internal buffer.

The response data is held in memory until there are no more unread responses. This should be fine since we are already keeping response data in memory for all generated handlers when we call toResponse.

},
{
once: !isUniqueHandler,
Expand Down
344 changes: 344 additions & 0 deletions test/traffic/fixtures/archives/response-cloning.har
Original file line number Diff line number Diff line change
@@ -0,0 +1,344 @@
{
"log": {
"version": "1.2",
"creator": {
"name": "WebInspector",
"version": "537.36"
},
"pages": [],
"entries": [
{
"_initiator": {
"type": "script",
"stack": {
"callFrames": [
{
"functionName": "eval",
"scriptId": "13",
"url": "",
"lineNumber": 3,
"columnNumber": 39
},
{
"functionName": "eval",
"scriptId": "13",
"url": "",
"lineNumber": 1,
"columnNumber": 56
},
{
"functionName": "evaluate",
"scriptId": "6",
"url": "",
"lineNumber": 340,
"columnNumber": 15
},
{
"functionName": "",
"scriptId": "12",
"url": "",
"lineNumber": 0,
"columnNumber": 43
}
]
}
},
"_priority": "High",
"_resourceType": "fetch",
"cache": {},
"connection": "29",
"request": {
"method": "GET",
"url": "http://127.0.0.1:51345/resource",
"httpVersion": "HTTP/1.1",
"headers": [
{
"name": "Host",
"value": "127.0.0.1:51345"
},
{
"name": "Connection",
"value": "keep-alive"
},
{
"name": "sec-ch-ua",
"value": "\"Chromium\";v=\"93\", \" Not;A Brand\";v=\"99\""
},
{
"name": "sec-ch-ua-mobile",
"value": "?0"
},
{
"name": "User-Agent",
"value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4576.0 Safari/537.36"
},
{
"name": "sec-ch-ua-platform",
"value": "\"macOS\""
},
{
"name": "Accept",
"value": "*/*"
},
{
"name": "Origin",
"value": "null"
},
{
"name": "Sec-Fetch-Site",
"value": "cross-site"
},
{
"name": "Sec-Fetch-Mode",
"value": "cors"
},
{
"name": "Sec-Fetch-Dest",
"value": "empty"
},
{
"name": "Accept-Encoding",
"value": "gzip, deflate, br"
},
{
"name": "Accept-Language",
"value": "en-US,en;q=0.9"
}
],
"queryString": [],
"cookies": [],
"headersSize": 477,
"bodySize": 0
},
"response": {
"status": 200,
"statusText": "OK",
"httpVersion": "HTTP/1.1",
"headers": [
{
"name": "X-Powered-By",
"value": "Express"
},
{
"name": "Access-Control-Allow-Origin",
"value": "*"
},
{
"name": "Content-Type",
"value": "text/html; charset=utf-8"
},
{
"name": "Content-Length",
"value": "14"
},
{
"name": "ETag",
"value": "W/\"e-lDXCsB3mEANLxMr+ZDknI3O1vrg\""
},
{
"name": "Date",
"value": "Sun, 15 Aug 2021 13:41:24 GMT"
},
{
"name": "Connection",
"value": "keep-alive"
}
],
"cookies": [],
"content": {
"size": 14,
"mimeType": "text/html",
"compression": 0,
"text": "first"
},
"redirectURL": "",
"headersSize": 236,
"bodySize": 14,
"_transferSize": 250,
"_error": null
},
"serverIPAddress": "127.0.0.1",
"startedDateTime": "2021-08-15T13:41:24.681Z",
"time": 6.774000000096828,
"timings": {
"blocked": 0.7860000000414147,
"dns": 0.013000000000000012,
"ssl": -1,
"connect": 0.38500000000000006,
"send": 0.07999999999999996,
"wait": 5.130000000105326,
"receive": 0.37999999995008693,
"_blocked_queueing": 0.6140000000414148
}
},
{
"_initiator": {
"type": "script",
"stack": {
"callFrames": [
{
"functionName": "eval",
"scriptId": "13",
"url": "",
"lineNumber": 3,
"columnNumber": 39
},
{
"functionName": "eval",
"scriptId": "13",
"url": "",
"lineNumber": 1,
"columnNumber": 56
},
{
"functionName": "evaluate",
"scriptId": "6",
"url": "",
"lineNumber": 340,
"columnNumber": 15
},
{
"functionName": "",
"scriptId": "12",
"url": "",
"lineNumber": 0,
"columnNumber": 43
}
]
}
},
"_priority": "High",
"_resourceType": "fetch",
"cache": {},
"connection": "34",
"request": {
"method": "GET",
"url": "http://127.0.0.1:51345/resource",
"httpVersion": "HTTP/1.1",
"headers": [
{
"name": "Host",
"value": "127.0.0.1:51345"
},
{
"name": "Connection",
"value": "keep-alive"
},
{
"name": "sec-ch-ua",
"value": "\"Chromium\";v=\"93\", \" Not;A Brand\";v=\"99\""
},
{
"name": "sec-ch-ua-mobile",
"value": "?0"
},
{
"name": "User-Agent",
"value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4576.0 Safari/537.36"
},
{
"name": "sec-ch-ua-platform",
"value": "\"macOS\""
},
{
"name": "Accept",
"value": "*/*"
},
{
"name": "Origin",
"value": "null"
},
{
"name": "Sec-Fetch-Site",
"value": "cross-site"
},
{
"name": "Sec-Fetch-Mode",
"value": "cors"
},
{
"name": "Sec-Fetch-Dest",
"value": "empty"
},
{
"name": "Accept-Encoding",
"value": "gzip, deflate, br"
},
{
"name": "Accept-Language",
"value": "en-US,en;q=0.9"
},
{
"name": "If-None-Match",
"value": "W/\"e-lDXCsB3mEANLxMr+ZDknI3O1vrg\""
}
],
"queryString": [],
"cookies": [],
"headersSize": 527,
"bodySize": 0
},
"response": {
"status": 200,
"statusText": "OK",
"httpVersion": "HTTP/1.1",
"headers": [
{
"name": "X-Powered-By",
"value": "Express"
},
{
"name": "Access-Control-Allow-Origin",
"value": "*"
},
{
"name": "Content-Type",
"value": "text/html; charset=utf-8"
},
{
"name": "Content-Length",
"value": "14"
},
{
"name": "ETag",
"value": "W/\"e-3dlKK58QOHvviPbNHbANXgv8hSk\""
},
{
"name": "Date",
"value": "Sun, 15 Aug 2021 13:41:24 GMT"
},
{
"name": "Connection",
"value": "keep-alive"
}
],
"cookies": [],
"content": {
"size": 14,
"mimeType": "text/html",
"compression": 0,
"text": "latest"
},
"redirectURL": "",
"headersSize": 236,
"bodySize": 14,
"_transferSize": 250,
"_error": null
},
"serverIPAddress": "127.0.0.1",
"startedDateTime": "2021-08-15T13:41:24.681Z",
"time": 7.915999999921842,
"timings": {
"blocked": 6.177999999940497,
"dns": 0.004999999999999893,
"ssl": -1,
"connect": 0.12699999999999978,
"send": 0.3100000000000005,
"wait": 1.0960000000315482,
"receive": 0.1999999999497959,
"_blocked_queueing": 0.6989999999404972
}
}
]
}
}
21 changes: 21 additions & 0 deletions test/traffic/fixtures/requests/response-cloning.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createTrafficScenario } from '../../utils/create-traffic-scenario'

createTrafficScenario(
(app) => {
let isFirstRequest = true

app.get('/resource', (req, res) => {
if (isFirstRequest) {
isFirstRequest = false
return res.send('first')
}

res.send('latest')
})
},
(server) => [
// Intentionally request two same endpoints.
[server.http.url('/resource')],
[server.http.url('/resource')],
],
)
Loading
Loading