test.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. const test = require('tape')
  2. , through2 = require('./')
  3. , crypto = require('crypto')
  4. , bl = require('bl')
  5. , spigot = require('stream-spigot')
  6. test('plain through', function (t) {
  7. var th2 = through2(function (chunk, enc, callback) {
  8. if (!this._i)
  9. this._i = 97 // 'a'
  10. else
  11. this._i++
  12. var b = new Buffer(chunk.length)
  13. for (var i = 0; i < chunk.length; i++)
  14. b[i] = this._i
  15. this.push(b)
  16. callback()
  17. })
  18. th2.pipe(bl(function (err, b) {
  19. var s = b.toString('ascii')
  20. t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string')
  21. t.end()
  22. }))
  23. th2.write(crypto.randomBytes(10))
  24. th2.write(crypto.randomBytes(5))
  25. th2.write(crypto.randomBytes(10))
  26. th2.end()
  27. })
  28. test('pipeable through', function (t) {
  29. var th2 = through2(function (chunk, enc, callback) {
  30. if (!this._i)
  31. this._i = 97 // 'a'
  32. else
  33. this._i++
  34. var b = new Buffer(chunk.length)
  35. for (var i = 0; i < chunk.length; i++)
  36. b[i] = this._i
  37. this.push(b)
  38. callback()
  39. })
  40. th2.pipe(bl(function (err, b) {
  41. var s = b.toString('ascii')
  42. // bl() acts like a proper streams2 stream and passes as much as it's
  43. // asked for, so we really only get one write with such a small amount
  44. // of data
  45. t.equal(s, 'aaaaaaaaaaaaaaaaaaaaaaaaa', 'got transformed string')
  46. t.end()
  47. }))
  48. var bufs = bl()
  49. bufs.append(crypto.randomBytes(10))
  50. bufs.append(crypto.randomBytes(5))
  51. bufs.append(crypto.randomBytes(10))
  52. bufs.pipe(th2)
  53. })
  54. test('object through', function (t) {
  55. t.plan(3)
  56. var th2 = through2({ objectMode: true}, function (chunk, enc, callback) {
  57. this.push({ out: chunk.in + 1 })
  58. callback()
  59. })
  60. var e = 0
  61. th2.on('data', function (o) {
  62. t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
  63. e++
  64. })
  65. th2.write({ in: 101 })
  66. th2.write({ in: 202 })
  67. th2.write({ in: -100 })
  68. th2.end()
  69. })
  70. test('flushing through', function (t) {
  71. var th2 = through2(function (chunk, enc, callback) {
  72. if (!this._i)
  73. this._i = 97 // 'a'
  74. else
  75. this._i++
  76. var b = new Buffer(chunk.length)
  77. for (var i = 0; i < chunk.length; i++)
  78. b[i] = this._i
  79. this.push(b)
  80. callback()
  81. }, function (callback) {
  82. this.push(new Buffer([ 101, 110, 100 ]))
  83. callback()
  84. })
  85. th2.pipe(bl(function (err, b) {
  86. var s = b.toString('ascii')
  87. t.equal(s, 'aaaaaaaaaabbbbbccccccccccend', 'got transformed string')
  88. t.end()
  89. }))
  90. th2.write(crypto.randomBytes(10))
  91. th2.write(crypto.randomBytes(5))
  92. th2.write(crypto.randomBytes(10))
  93. th2.end()
  94. })
  95. test('plain through ctor', function (t) {
  96. var Th2 = through2.ctor(function (chunk, enc, callback) {
  97. if (!this._i)
  98. this._i = 97 // 'a'
  99. else
  100. this._i++
  101. var b = new Buffer(chunk.length)
  102. for (var i = 0; i < chunk.length; i++)
  103. b[i] = this._i
  104. this.push(b)
  105. callback()
  106. })
  107. var th2 = new Th2()
  108. th2.pipe(bl(function (err, b) {
  109. var s = b.toString('ascii')
  110. t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string')
  111. t.end()
  112. }))
  113. th2.write(crypto.randomBytes(10))
  114. th2.write(crypto.randomBytes(5))
  115. th2.write(crypto.randomBytes(10))
  116. th2.end()
  117. })
  118. test('reuse through ctor', function (t) {
  119. t.plan(4)
  120. var Th2 = through2.ctor(function (chunk, enc, callback) {
  121. if (!this._i) {
  122. t.ok(1, 'did not contain previous instance data (this._i)')
  123. this._i = 97 // 'a'
  124. } else
  125. this._i++
  126. var b = new Buffer(chunk.length)
  127. for (var i = 0; i < chunk.length; i++)
  128. b[i] = this._i
  129. this.push(b)
  130. callback()
  131. })
  132. var th2 = Th2()
  133. th2.pipe(bl(function (err, b) {
  134. var s = b.toString('ascii')
  135. t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string')
  136. var newInstance = Th2()
  137. newInstance.pipe(bl(function (err, b) {
  138. var s = b.toString('ascii')
  139. t.equal('aaaaaaabbbbccccccc', s, 'got transformed string')
  140. }))
  141. newInstance.write(crypto.randomBytes(7))
  142. newInstance.write(crypto.randomBytes(4))
  143. newInstance.write(crypto.randomBytes(7))
  144. newInstance.end()
  145. }))
  146. th2.write(crypto.randomBytes(10))
  147. th2.write(crypto.randomBytes(5))
  148. th2.write(crypto.randomBytes(10))
  149. th2.end()
  150. })
  151. test('object through ctor', function (t) {
  152. t.plan(3)
  153. var Th2 = through2.ctor({ objectMode: true}, function (chunk, enc, callback) {
  154. this.push({ out: chunk.in + 1 })
  155. callback()
  156. })
  157. var th2 = new Th2()
  158. var e = 0
  159. th2.on('data', function (o) {
  160. t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
  161. e++
  162. })
  163. th2.write({ in: 101 })
  164. th2.write({ in: 202 })
  165. th2.write({ in: -100 })
  166. th2.end()
  167. })
  168. test('pipeable object through ctor', function (t) {
  169. t.plan(4)
  170. var Th2 = through2.ctor({ objectMode: true}, function (record, enc, callback) {
  171. if (record.temp != null && record.unit == 'F') {
  172. record.temp = ( ( record.temp - 32 ) * 5 ) / 9
  173. record.unit = 'C'
  174. }
  175. this.push(record)
  176. callback()
  177. })
  178. var th2 = Th2()
  179. var expect = [-19, -40, 100, 22]
  180. th2.on('data', function (o) {
  181. t.deepEqual(o, { temp: expect.shift(), unit: 'C' }, 'got transformed object')
  182. })
  183. spigot({objectMode: true}, [
  184. {temp: -2.2, unit: 'F'},
  185. {temp: -40, unit: 'F'},
  186. {temp: 212, unit: 'F'},
  187. {temp: 22, unit: 'C'}
  188. ]).pipe(th2)
  189. })
  190. test('object through ctor override', function (t) {
  191. t.plan(3)
  192. var Th2 = through2.ctor(function (chunk, enc, callback) {
  193. this.push({ out: chunk.in + 1 })
  194. callback()
  195. })
  196. var th2 = Th2({objectMode: true})
  197. var e = 0
  198. th2.on('data', function (o) {
  199. t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
  200. e++
  201. })
  202. th2.write({ in: 101 })
  203. th2.write({ in: 202 })
  204. th2.write({ in: -100 })
  205. th2.end()
  206. })
  207. test('object settings available in transform', function (t) {
  208. t.plan(6)
  209. var Th2 = through2.ctor({objectMode: true, peek: true}, function (chunk, enc, callback) {
  210. t.ok(this.options.peek, "reading options from inside _transform")
  211. this.push({ out: chunk.in + 1 })
  212. callback()
  213. })
  214. var th2 = Th2()
  215. var e = 0
  216. th2.on('data', function (o) {
  217. t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
  218. e++
  219. })
  220. th2.write({ in: 101 })
  221. th2.write({ in: 202 })
  222. th2.write({ in: -100 })
  223. th2.end()
  224. })
  225. test('object settings available in transform override', function (t) {
  226. t.plan(6)
  227. var Th2 = through2.ctor(function (chunk, enc, callback) {
  228. t.ok(this.options.peek, "reading options from inside _transform")
  229. this.push({ out: chunk.in + 1 })
  230. callback()
  231. })
  232. var th2 = Th2({objectMode: true, peek: true})
  233. var e = 0
  234. th2.on('data', function (o) {
  235. t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
  236. e++
  237. })
  238. th2.write({ in: 101 })
  239. th2.write({ in: 202 })
  240. th2.write({ in: -100 })
  241. th2.end()
  242. })
  243. test('object override extends options', function (t) {
  244. t.plan(6)
  245. var Th2 = through2.ctor({objectMode: true}, function (chunk, enc, callback) {
  246. t.ok(this.options.peek, "reading options from inside _transform")
  247. this.push({ out: chunk.in + 1 })
  248. callback()
  249. })
  250. var th2 = Th2({peek: true})
  251. var e = 0
  252. th2.on('data', function (o) {
  253. t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
  254. e++
  255. })
  256. th2.write({ in: 101 })
  257. th2.write({ in: 202 })
  258. th2.write({ in: -100 })
  259. th2.end()
  260. })