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

Fetch interceptor does not abort signal for long response #693

Open
mikicho opened this issue Dec 21, 2024 · 1 comment
Open

Fetch interceptor does not abort signal for long response #693

mikicho opened this issue Dec 21, 2024 · 1 comment

Comments

@mikicho
Copy link
Contributor

mikicho commented Dec 21, 2024

const http = require('http');
const stream = require('stream');
const { FetchInterceptor } = require('@mswjs/interceptors/fetch');

const interceptor = new FetchInterceptor();
interceptor.apply();

interceptor.on('request', ({ controller}) => {
  const readable = new stream.Readable({
    read() {
      setTimeout(() => this.push('hello'), 100)
    }
  })
  controller.respondWith(new Response(readable))
})

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' })
  setInterval(() => res.write('hello\n'), 100)
})
server.listen(3000);

(async () => {
  const start = process.hrtime.bigint()
  const response = await fetch('http://localhost:3000', {
    signal: AbortSignal.timeout(300),
  })
  console.log((process.hrtime.bigint() - start) / BigInt(1e6));
  for await (const data of response.body) {
    console.log((process.hrtime.bigint() - start) / BigInt(1e6));
    console.log(Buffer.from(data).toString())
  }
  console.log((process.hrtime.bigint() - start) / BigInt(1e6));
})()

This process continues to run indefinitely.
It should abort the request for a long response as well.

P.S. this is weird and somewhat unexpected behavior, I must say.

@mikicho
Copy link
Contributor Author

mikicho commented Dec 22, 2024

I think we should extract the signal abort from the handleRequest method and send it to the outer interceptor because each client behaves differently.
From a quick check:

  1. Fetch abort for the entire request & response. (as shown above)
  2. ClientRequest does not use this abort at all and relies on Node.js internal code for the abort.
  3. IIUC, XMLHttpRequest does not have a signal property.

WDYT?

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

No branches or pull requests

1 participant