results_stream.js 948 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. const Readable = require('stream').Readable;
  3. // copy-paste from https://github.com/mysqljs/mysql/blob/master/lib/protocol/sequences/Query.js
  4. module.exports = function(command, connectionStream) {
  5. command.stream = function(options) {
  6. let stream;
  7. options = options || {};
  8. options.objectMode = true;
  9. (stream = new Readable(options)),
  10. (stream._read = function() {
  11. connectionStream.resume();
  12. });
  13. this.on('result', (row, i) => {
  14. if (!stream.push(row)) {
  15. connectionStream.pause();
  16. }
  17. stream.emit('result', row, i); // replicate old emitter
  18. });
  19. this.on('error', err => {
  20. stream.emit('error', err); // Pass on any errors
  21. });
  22. this.on('end', () => {
  23. stream.push(null); // pushing null, indicating EOF
  24. });
  25. this.on('fields', (fields, i) => {
  26. stream.emit('fields', fields, i); // replicate old emitter
  27. });
  28. return stream;
  29. };
  30. };