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

feat: use "IsomorphicRequest" class to handle request body as buffer #267

Merged
merged 33 commits into from
Jul 4, 2022
Merged

Conversation

95th
Copy link
Contributor

@95th 95th commented Jul 3, 2022

Expose new Request API which allow access to raw buffer

Roadmap

  • Add unit tests for bufferUtils
  • Add a unit test for IsomorphicRequest constructor on accepting another isomorphic request instance as the argument
  • Update the documentation on how to read request body now

@95th 95th marked this pull request as ready for review July 3, 2022 11:20
src/BufferedRequest.ts Outdated Show resolved Hide resolved
Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

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

What a fantastic kick off of this idea! Thank you, @95th!

I've left a few comments and thoughts on the changes. I would like to know what you think about those. Let's discuss it, make whichever adjustments are necessary, and have a unified request handling!

src/BufferedRequest.ts Outdated Show resolved Hide resolved
src/interceptors/ClientRequest/NodeClientRequest.test.ts Outdated Show resolved Hide resolved
src/interceptors/XMLHttpRequest/XMLHttpRequestOverride.ts Outdated Show resolved Hide resolved
src/utils/bufferCodec.ts Outdated Show resolved Hide resolved
src/BufferedRequest.test.ts Outdated Show resolved Hide resolved
src/BufferedRequest.ts Outdated Show resolved Hide resolved
src/BufferedRequest.ts Outdated Show resolved Hide resolved
src/glossary.ts Outdated Show resolved Hide resolved
@kettanaito kettanaito changed the title Expose new Request API which allow access to raw buffer feat: use "IsomorphicRequest" class to handle request body as buffer Jul 3, 2022
@kettanaito
Copy link
Member

The tests should be passing now! I've also added a few minor changes and left a roadmap in the pull request's description of what's remaining. You don't have to tackle everything that remains, just let me know what interests you and I can take care of the rest (like updating the docs).

@95th 95th requested a review from kettanaito July 3, 2022 16:21
clearTimeout(timeoutTimer)
resolve(JSON.parse(event.detail))
resolve(event.detail)
Copy link
Member

Choose a reason for hiding this comment

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

We were previously parsing this, as event.detail is a string. Do we not need to parse it into object anymore? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is automatically serialized and deserialized

@@ -2,7 +2,7 @@ import { FetchInterceptor } from '@mswjs/interceptors/lib/interceptors/fetch'

const interceptor = new FetchInterceptor()
interceptor.on('request', (request) => {
window.requestBody = request.body
window.requestBody = request.text()
Copy link
Member

Choose a reason for hiding this comment

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

We should await this, as request.text() returns a Promise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We already return promise for it in the test:

function getRequestBody(page: Page): Promise<string> {
  return page.evaluate(() => window.requestBody)
}

and documentation for evaluate says:

If the function passed to the page.evaluate(pageFunction[, arg]) returns a [Promise], then page.evaluate(pageFunction[, arg]) would wait for the promise to resolve and return its value.

@kettanaito
Copy link
Member

I'm also thinking of how the IIR will be propagated to the request listener. It'd be great to just reuse the IsomorphicRequest instance provided by this library in MSW, but what this library exposes is an interactive instance, meaning it has the respondWith() method. At the same time, the req` in MSW handlers must not have such method, and should be a barebones iso request instance.

I'm thinking of something like IsomorphicRequest.clone(): IsomorphicRequest that'd create a clean instance of request upon use. Since IIR extends IR, it will also have the .clone() method but will return a plain request without the respondWith() method.

What do you think about this approach?

@95th
Copy link
Contributor Author

95th commented Jul 4, 2022

I think we should have a MockedRequest extends IsomorphicRequest which include cookies etc and it won't expose the respondWith details in the type system.

export class MockedRequest extends IsomorphicRequest {
  public cookies: Record<string, string> = {}
  public redirect = 'manual'
  public referrer = ''
  public keepalive = false
  public cache = 'default'
  public mode = 'cors'
  public referrerPolicy = 'no-referrer'
  public integrity = ''
  public destination = 'document'
  public bodyUsed = false
  public passthrough = passthrough

  constructor(request: IsomorphicRequest) {
    super(request)
  }
}

src/IsomorphicRequest.ts Outdated Show resolved Hide resolved
Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

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

This is by far the most enjoyable pull request in my open source journey. Thank you for collaborating with me on this!

@kettanaito kettanaito merged commit e305d38 into mswjs:main Jul 4, 2022
@kettanaito
Copy link
Member

Released: v0.17.0 🎉

This has been released in v0.17.0!

Make sure to always update to the latest version (npm i @mswjs/interceptors@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants