-
Notifications
You must be signed in to change notification settings - Fork 625
Description
If I have a stream that is paused, pass it to gm, then resume it, stream gives an error:
var gm = require("gm");
var fs = require("fs");
var pause = require("connect").utils.pause;
var source = fs.createReadStream("img.png");
var pauser = pause(source);
setTimeout(function () {
gm(source, "img.png")
.resize(200, 200)
.stream("png", function (err, stdout, stderr) {
if (err) {
console.log("err", err);
process.exit(1);
} else {
stdout.pipe(process.stdout);
stderr.pipe(process.stderr);
}
});
pauser.resume();
}, 100);$ node test.js
err [Error: gm().stream() or gm().write() with a non-readable stream. Pass "{bufferStream: true}" to identify() or getter (size, format, etc...)]The problem is that the stream does indeed have readable: false since it's already emitted all its events, which are caught by connect and stored in the _events property to be re-emitted later, when I call pauser.resume().
So I guess it's partially connect's fault, since usually you can assume that non-readable streams will not emit any more events. But, a solution is kind of necessary, since there's otherwise no way to defer streaming image processing until after an asynchronous event (e.g. user authorization) has occurred. Note that the native stream's pause method does not work on HTTP streams; data events get dropped, necessitating something like connect's custom pause function.
Not sure what a good solution is here. For now I've hacked around it by inserting source.readable = true before calling gm.
/cc @visionmedia for any connect-related advice, @isaacs since this is an interesting case of "unfuck HTTP"