You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.1 KiB

4 years ago
  1. var stream = require('stream')
  2. var pump = require('./index')
  3. var rs = new stream.Readable()
  4. var ws = new stream.Writable()
  5. rs._read = function (size) {
  6. this.push(Buffer(size).fill('abc'))
  7. }
  8. ws._write = function (chunk, encoding, cb) {
  9. setTimeout(function () {
  10. cb()
  11. }, 100)
  12. }
  13. var toHex = function () {
  14. var reverse = new (require('stream').Transform)()
  15. reverse._transform = function (chunk, enc, callback) {
  16. reverse.push(chunk.toString('hex'))
  17. callback()
  18. }
  19. return reverse
  20. }
  21. var wsClosed = false
  22. var rsClosed = false
  23. var callbackCalled = false
  24. var check = function () {
  25. if (wsClosed && rsClosed && callbackCalled) {
  26. console.log('test-browser.js passes')
  27. clearTimeout(timeout)
  28. }
  29. }
  30. ws.on('finish', function () {
  31. wsClosed = true
  32. check()
  33. })
  34. rs.on('end', function () {
  35. rsClosed = true
  36. check()
  37. })
  38. var res = pump(rs, toHex(), toHex(), toHex(), ws, function () {
  39. callbackCalled = true
  40. check()
  41. })
  42. if (res !== ws) {
  43. throw new Error('should return last stream')
  44. }
  45. setTimeout(function () {
  46. rs.push(null)
  47. rs.emit('close')
  48. }, 1000)
  49. var timeout = setTimeout(function () {
  50. check()
  51. throw new Error('timeout')
  52. }, 5000)