pedronauck / micro-router

:station: A tiny and functional router for Zeit's Micro
https://www.npmjs.com/microrouter
MIT License
623 stars 34 forks source link

Replying with stream fails when using micro-router #32

Closed yamalight closed 6 years ago

yamalight commented 6 years ago

For some reason when replying with stream while using micro-router, request fails to go through. E.g. the following code will result in Cannot GET /:

const {Readable} = require('stream');
const micro = require('micro');
const _ = require('highland');
const { router, get } = require('microrouter')

const streamTest = (req, res) => {
  const stream = _();
  micro.send(res, 200, new Readable().wrap(stream));
  stream.write("a");
  stream.write("b");
  stream.end();
};

const server = micro(router(get('/', streamTest)));

server.listen(3000);

Changing it to use only micro itself makes it work:

const {Readable} = require('stream');
const micro = require('micro');
const _ = require('highland');

const streamTest = (req, res) => {
  const stream = _();
  micro.send(res, 200, new Readable().wrap(stream));
  stream.write("a");
  stream.write("b");
  stream.end();
};
const server = micro(streamTest);

server.listen(3000);

Any idea why that might happen? Am I doing something wrong? Is this a bug? (Haven't noticed any streams in tests)

johanbrook commented 6 years ago

@yamalight Tried reproduce in tests in https://github.com/pedronauck/micro-router/pull/33, but to no avail. Really weird. I can repro your bug in an app using this package.

johanbrook commented 6 years ago

Removing this line makes the file read stream work in my app.

johanbrook commented 6 years ago

I think all of this is due to https://github.com/pedronauck/micro-router/issues/31, since apps using this package is directed to the dist/index file, while the tests are using the raw source code.

yamalight commented 6 years ago

@johanbrook Ooooh, that's a sneaky bug. Is there any way to manually update dist/?

johanbrook commented 6 years ago

@yamalight Run npm run build after cloning down this repo :)

yamalight commented 6 years ago

@johanbrook ah, makes sense. gonna give it a shot, thanks!