Skip to content

Commit

Permalink
Fix adding listeners to reused sockets by removing them on request fi…
Browse files Browse the repository at this point in the history
…nish (#13)

* Fix adding listeners to reused sockets by removing them on request finish

* Use removeListener instead of off to support Node 8.x
  • Loading branch information
exogen authored Oct 3, 2019
1 parent 76fac36 commit 5b86108
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
demo/.next
21 changes: 9 additions & 12 deletions demo/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,15 @@ fragment TypeRef on __Type {
function useHarUrl(har) {
const [downloadUrl, setDownloadUrl] = useState(null);

useEffect(
() => {
if (typeof URL !== "undefined" && URL.createObjectURL) {
const blob = new Blob([JSON.stringify(har)], {
type: "data:application/json;charset=utf-8"
});
const objectUrl = URL.createObjectURL(blob);
setDownloadUrl(objectUrl);
}
},
[har]
);
useEffect(() => {
if (typeof URL !== "undefined" && URL.createObjectURL) {
const blob = new Blob([JSON.stringify(har)], {
type: "data:application/json;charset=utf-8"
});
const objectUrl = URL.createObjectURL(blob);
setDownloadUrl(objectUrl);
}
}, [har]);

return downloadUrl;
}
Expand Down
25 changes: 19 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,24 +140,37 @@ function handleRequest(request, options) {
return _end.call(this, ...args);
};

let removeSocketListeners;

request.on("socket", socket => {
entry._timestamps.socket = process.hrtime();

socket.on("lookup", () => {
const onLookup = () => {
entry._timestamps.lookup = process.hrtime();
});
};

socket.on("connect", () => {
const onConnect = () => {
entry._timestamps.connect = process.hrtime();
});
};

socket.on("secureConnect", () => {
const onSecureConnect = () => {
entry._timestamps.secureConnect = process.hrtime();
});
};

socket.once("lookup", onLookup);
socket.once("connect", onConnect);
socket.once("secureConnect", onSecureConnect);

removeSocketListeners = () => {
socket.removeListener("lookup", onLookup);
socket.removeListener("connect", onConnect);
socket.removeListener("secureConnect", onSecureConnect);
};
});

request.on("finish", () => {
entry._timestamps.sent = process.hrtime();
removeSocketListeners();
});

request.on("response", response => {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"main": "index.js",
"scripts": {
"coveralls": "coveralls < ./coverage/lcov.info",
"format": "prettier --write \"**/*.{js,md}\"",
"lint": "eslint index.js test",
"start": "cd demo && yarn start",
"test": "yarn run lint && yarn test:coverage",
Expand All @@ -39,6 +40,7 @@
"isomorphic-fetch": "^2.2.1",
"isomorphic-unfetch": "^3.0.0",
"jest": "^24.8.0",
"node-fetch": "^2.6.0"
"node-fetch": "^2.6.0",
"prettier": "^1.18.2"
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3018,6 +3018,11 @@ prelude-ls@~1.1.2:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
integrity sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=

prettier@^1.18.2:
version "1.18.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea"
integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw==

pretty-format@^24.8.0:
version "24.8.0"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-24.8.0.tgz#8dae7044f58db7cb8be245383b565a963e3c27f2"
Expand Down

0 comments on commit 5b86108

Please sign in to comment.