iiegor / ptyw.js

Bindings to forkpty(3) for node.js.
MIT License
20 stars 13 forks source link

Windows: Cannot read property 'on' of undefined #5

Closed NuSkooler closed 8 years ago

NuSkooler commented 8 years ago

Code that is functioning in Linux crashes on Windows with the stack trace below.

< G:\dev\enigma-bbs\node_modules\ptyw.js\lib\pty.js:295
<     this._internalee.on('close', func);
<                     ^
< TypeError: Cannot read property 'on' of undefined
<     at Terminal.addListener.Terminal.on (G:\dev\enigma-bbs\node_modules\ptyw.js\lib\pty.js:295:21)
<     at Door.run (G:\dev\enigma-bbs\core\door.js:64:7)
<     at AbracadabraModule.options.runDoor (G:\dev\enigma-bbs\mods\abracadabra.js:160:16)
<     at AbracadabraModule.finishedLoading (G:\dev\enigma-bbs\mods\abracadabra.js:181:7)
<     at complete (G:\dev\enigma-bbs\mods\abracadabra.js:126:11)
<     at G:\dev\enigma-bbs\node_modules\async\lib\async.js:721:13
<     at _toString (G:\dev\enigma-bbs\node_modules\async\lib\async.js:52:16)
<     at G:\dev\enigma-bbs\node_modules\async\lib\async.js:269:32
<     at G:\dev\enigma-bbs\node_modules\async\lib\async.js:44:16
<     at G:\dev\enigma-bbs\node_modules\async\lib\async.js:718:17

The relevant code causing this:

var door = pty.spawn(self.exeInfo.cmd, self.exeInfo.args, {
        cols : self.client.term.termWidth,
        rows : self.client.term.termHeight,
    });

    self.client.term.output.pipe(door);

    door.on('data', function doorData(data) {
        self.client.term.write(decode(data, self.client.term.outputEncoding));
    });

    door.on('close', function closed() {
        self.client.term.output.unpipe(door);
        self.client.term.output.resume();
    });

    door.on('exit', function exited(code) {
        self.client.log.info( { code : code }, 'Door exited');

        door.removeAllListeners();

        self.emit('finished');
    });
iiegor commented 8 years ago

Sorry for the late response @NuSkooler . I think this can be solved by adding the missing this._internalee = new EventEmitter; into the Terminal contructor on pty_win.js.

I can't reproduce this issue so if you can check if that solves the problem would be great :+1:

NuSkooler commented 8 years ago

@iiegor This does appear to fix the crash. You'll need to bring in EventEmitter as well of course:

var EventEmitter = require('events').EventEmitter;

I'm not sure if it's related at all, but on Windows, I do not get the close event. I do however, get the exit event. Is this expected? If so I can work around that I'm sure.

My updated usage of the code is here: https://github.com/NuSkooler/enigma-bbs/blob/master/core/door.js (when the code path is using "stdio")

iiegor commented 8 years ago

@NuSkooler Fixed. Regarding the close/exit event issue, try with the latest release.