stream-base64.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const Stream = require('readable-stream');
  2. // =============================================================================
  3. // StreamBase64 - A utility to convert to/from base64 stream
  4. // Note: does not buffer data, must be piped
  5. class StreamBase64 extends Stream.Duplex {
  6. constructor() {
  7. super();
  8. // consuming pipe streams go here
  9. this.pipes = [];
  10. }
  11. // writable
  12. // event drain - if write returns false (which it won't), indicates when safe to write again.
  13. // finish - end() has been called
  14. // pipe(src) - pipe() has been called on readable
  15. // unpipe(src) - unpipe() has been called on readable
  16. // error - duh
  17. write(/* data, encoding */) {
  18. return true;
  19. }
  20. cork() {}
  21. uncork() {}
  22. end(/* chunk, encoding, callback */) {}
  23. // readable
  24. // event readable - some data is now available
  25. // event data - switch to flowing mode - feeds chunks to handler
  26. // event end - no more data
  27. // event close - optional, indicates upstream close
  28. // event error - duh
  29. read(/* size */) {}
  30. setEncoding(encoding) {
  31. // causes stream.read or stream.on('data) to return strings of encoding instead of Buffer objects
  32. this.encoding = encoding;
  33. }
  34. pause() {}
  35. resume() {}
  36. isPaused() {}
  37. pipe(destination) {
  38. // add destination to pipe list & write current buffer
  39. this.pipes.push(destination);
  40. }
  41. unpipe(destination) {
  42. // remove destination from pipe list
  43. this.pipes = this.pipes.filter(pipe => pipe !== destination);
  44. }
  45. unshift(/* chunk */) {
  46. // some numpty has read some data that's not for them and they want to put it back!
  47. // Might implement this some day
  48. throw new Error('Not Implemented');
  49. }
  50. wrap(/* stream */) {
  51. // not implemented
  52. throw new Error('Not Implemented');
  53. }
  54. }
  55. module.exports = StreamBase64;