| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 3f5be93 commit 7feea6a
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -171,8 +171,14 @@ PythonShell.prototype.send = function (message) { | |||
| 171 | 171 | PythonShell.prototype.receive = function (data) { | |
| 172 | 172 | var self = this; | |
| 173 | 173 | var lines = (''+data).split(/\n/g); | |
| 174 | - var lastLine = lines.pop(); | ||
| 175 | 174 | ||
| 175 | + if (lines.length === 1) { | ||
| 176 | + // an incomplete record, keep buffering | ||
| 177 | + this._remaining = (this._remaining || '') + lines[0]; | ||
| 178 | + return this; | ||
| 179 | + } | ||
| 180 | + | ||
| 181 | + var lastLine = lines.pop(); | ||
| 176 | 182 | // fix the first line with the remaining from the previous iteration of 'receive' | |
| 177 | 183 | lines[0] = (this._remaining || '') + lines[0]; | |
| 178 | 184 | // keep the remaining for the next iteration of 'receive' | |
@@ -184,14 +190,16 @@ PythonShell.prototype.receive = function (data) { | |||
| 184 | 190 | self.emit('message', JSON.parse(line)); | |
| 185 | 191 | } catch (err) { | |
| 186 | 192 | self.emit('error', extend( | |
| 187 | - new Error('invalid JSON message: ' + data), | ||
| 188 | - { inner: err, data: data} | ||
| 193 | + new Error('invalid JSON message: ' + data + ' >> ' + err), | ||
| 194 | + { inner: err, data: line} | ||
| 189 | 195 | )); | |
| 190 | 196 | } | |
| 191 | 197 | } else { | |
| 192 | 198 | self.emit('message', line); | |
| 193 | 199 | } | |
| 194 | 200 | }); | |
| 201 | + | ||
| 202 | + return this; | ||
| 195 | 203 | }; | |
| 196 | 204 | ||
| 197 | 205 | /** | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -131,6 +131,15 @@ describe('PythonShell', function () { | |||
| 131 | 131 | count.should.be.exactly(3); | |
| 132 | 132 | }).end(done); | |
| 133 | 133 | }); | |
| 134 | + it('should properly buffer partial messages', function (done) { | ||
| 135 | + var pyshell = new PythonShell('echo_json.py', { | ||
| 136 | + mode: 'json' | ||
| 137 | + }); | ||
| 138 | + pyshell.on('message', function (message) { | ||
| 139 | + message.should.be.an.Object; | ||
| 140 | + message.should.eql({ a: true }); | ||
| 141 | + }).receive('{"a"').receive(':').receive('true}\n').end(done); | ||
| 142 | + }); | ||
| 134 | 143 | it('should not be invoked when mode is "binary"', function (done) { | |
| 135 | 144 | var pyshell = new PythonShell('echo_args.py', { | |
| 136 | 145 | args: ['hello', 'world'], | |
| Back | FazBrowse Home | New Git URL |
0 commit comments