gr2m / helpdesk

Answering all your GitHub API/automation questions live on Twitch
https://twitch.tv/gregorcodes
Creative Commons Zero v1.0 Universal
22 stars 11 forks source link

📅 11/21 @ 12:00pm PT - Refactoring a 10+ years old code base: `nock` (Part II) #54

Closed gr2m closed 2 years ago

gr2m commented 2 years ago

💁🏻 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

  1. @nock/intercept - overwrite Node's native APIs to hook into the request lifecycle.
  2. @nock/mock - provides API to define HTTP mocks
  3. @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).

  1. In this show I will try to figure out how to return a custom http.IncomingMessage instance for an intercepted request.
  2. 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.
  3. 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.
  4. Finally, I'll move the internal module into its own repository and publish it as its own npm 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

gr2m commented 2 years ago

Going live in 30 minutes at https://twitch.tv/gregorcodes

gr2m commented 2 years ago

I'm now live on https://twitch.tv/gregorcodes

gr2m commented 2 years ago

I'm taking a break, I'll be back in 20 min

gr2m commented 2 years ago

more like 30min, but I'm back

gr2m commented 2 years ago

done, for good now :)