💁🏻 Refactoring a 10+ years old code base: nock
📅 Sunday, November 21, 2021
🕐 12:00am Pacific Time
🎙️ no guests
📍 https://www.twitch.tv/gregorcodes
🏷️ opensource
Subscribe to this issues to get a notification before the show begins and a summary after the show concludes.
Refactoring a 10+ years old code base: nock
nock is, to the best of my knowledge, the most widely used http mocking library for Node.js. It's first commit by Pedro is from September 2011. Around 6 years later, Pedro invited me as a maintainer, about a year after Pedro himself stopped maintaining nock.
I offered to take one the maintenance as we depended on nock for other projects I was maintaining, mostly @hoodiehq and @octokit. I, and increasingly we as a new maintainer team, were able to onboard more maintainers (RichardLitt, Paul, and Matt), and we were able to secure a sponsorship of $5,000 by Coinbase. Together, we were able to bring nock to a more maintainable state (most notably: 100% test coverage) and started addressing open issues and pull requests again.
But the nock code base is still a huge monolith of intertwined logic which is hard to understand and impossible to maintain with a few hours of time that people graciously donated from time to time.
In order to make nock's code base easier to understand, to maintain, and to make it possible to distribute the maintainership across more people, we decided the code base needs to be split up into separate modules
@nock/intercept - overwrite Node's native APIs to hook into the request lifecycle.
@nock/mock - provides API to define HTTP mocks
@nock/recorder - APIs to record requests/responses and replay them from files
In this series of helpdesk shows, I'll live code on the nock code base attempting to split it up into the above modules.
Outline
In my first extended session working on nock (https://github.com/gr2m/helpdesk/issues/53), I dove into the current code base and tried to understand it. In the 2nd part I focused on the intercept part. After the show I managed to make it actually work, with a hard-coded mock (notes).
In this show I will try to figure out how to return a custom http.IncomingMessage instance for an intercepted request.
Once I figure that out, I want to make it optionally possible to let the request pass through without intercepting, making it possible to record both the request and the response.
With these features I should be able to replace the existing interception logic with the new internal intercept module. The goal is to make all existing tests pass using the new module.
Finally, I'll move the internal module into its own repository and publish it as its own npm module.
💁🏻 Refactoring a 10+ years old code base:
nock
📅 Sunday, November 21, 2021 🕐 12:00am Pacific Time 🎙️ no guests 📍 https://www.twitch.tv/gregorcodes 🏷️ opensourceSubscribe to this issues to get a notification before the show begins and a summary after the show concludes.
Refactoring a 10+ years old code base:
nock
nock
is, to the best of my knowledge, the most widely used http mocking library for Node.js. It's first commit by Pedro is from September 2011. Around 6 years later, Pedro invited me as a maintainer, about a year after Pedro himself stopped maintainingnock
.I offered to take one the maintenance as we depended on
nock
for other projects I was maintaining, mostly @hoodiehq and @octokit. I, and increasingly we as a new maintainer team, were able to onboard more maintainers (RichardLitt, Paul, and Matt), and we were able to secure a sponsorship of $5,000 by Coinbase. Together, we were able to bringnock
to a more maintainable state (most notably: 100% test coverage) and started addressing open issues and pull requests again.But the
nock
code base is still a huge monolith of intertwined logic which is hard to understand and impossible to maintain with a few hours of time that people graciously donated from time to time.In order to make
nock
's code base easier to understand, to maintain, and to make it possible to distribute the maintainership across more people, we decided the code base needs to be split up into separate modules@nock/intercept
- overwrite Node's native APIs to hook into the request lifecycle.@nock/mock
- provides API to define HTTP mocks@nock/recorder
- APIs to record requests/responses and replay them from filesIn this series of helpdesk shows, I'll live code on the
nock
code base attempting to split it up into the above modules.Outline
In my first extended session working on
nock
(https://github.com/gr2m/helpdesk/issues/53), I dove into the current code base and tried to understand it. In the 2nd part I focused on the intercept part. After the show I managed to make it actually work, with a hard-coded mock (notes).http.IncomingMessage
instance for an intercepted request.intercept
module. The goal is to make all existing tests pass using the new module.I expect the work about will take about 3 shows.
TODOs
Before the show
undefined
When show begins
After the show
Recordings
Shownotes
https://github.com/nock/nock/discussions/2247#discussioncomment-1679029