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.

test.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. var tape = require('tape')
  2. , crypto = require('crypto')
  3. , fs = require('fs')
  4. , hash = require('hash_file')
  5. , BufferList = require('../')
  6. , Buffer = require('safe-buffer').Buffer
  7. , encodings =
  8. ('hex utf8 utf-8 ascii binary base64'
  9. + (process.browser ? '' : ' ucs2 ucs-2 utf16le utf-16le')).split(' ')
  10. tape('single bytes from single buffer', function (t) {
  11. var bl = new BufferList()
  12. bl.append(Buffer.from('abcd'))
  13. t.equal(bl.length, 4)
  14. t.equal(bl.get(0), 97)
  15. t.equal(bl.get(1), 98)
  16. t.equal(bl.get(2), 99)
  17. t.equal(bl.get(3), 100)
  18. t.end()
  19. })
  20. tape('single bytes from multiple buffers', function (t) {
  21. var bl = new BufferList()
  22. bl.append(Buffer.from('abcd'))
  23. bl.append(Buffer.from('efg'))
  24. bl.append(Buffer.from('hi'))
  25. bl.append(Buffer.from('j'))
  26. t.equal(bl.length, 10)
  27. t.equal(bl.get(0), 97)
  28. t.equal(bl.get(1), 98)
  29. t.equal(bl.get(2), 99)
  30. t.equal(bl.get(3), 100)
  31. t.equal(bl.get(4), 101)
  32. t.equal(bl.get(5), 102)
  33. t.equal(bl.get(6), 103)
  34. t.equal(bl.get(7), 104)
  35. t.equal(bl.get(8), 105)
  36. t.equal(bl.get(9), 106)
  37. t.end()
  38. })
  39. tape('multi bytes from single buffer', function (t) {
  40. var bl = new BufferList()
  41. bl.append(Buffer.from('abcd'))
  42. t.equal(bl.length, 4)
  43. t.equal(bl.slice(0, 4).toString('ascii'), 'abcd')
  44. t.equal(bl.slice(0, 3).toString('ascii'), 'abc')
  45. t.equal(bl.slice(1, 4).toString('ascii'), 'bcd')
  46. t.equal(bl.slice(-4, -1).toString('ascii'), 'abc')
  47. t.end()
  48. })
  49. tape('multi bytes from single buffer (negative indexes)', function (t) {
  50. var bl = new BufferList()
  51. bl.append(Buffer.from('buffer'))
  52. t.equal(bl.length, 6)
  53. t.equal(bl.slice(-6, -1).toString('ascii'), 'buffe')
  54. t.equal(bl.slice(-6, -2).toString('ascii'), 'buff')
  55. t.equal(bl.slice(-5, -2).toString('ascii'), 'uff')
  56. t.end()
  57. })
  58. tape('multiple bytes from multiple buffers', function (t) {
  59. var bl = new BufferList()
  60. bl.append(Buffer.from('abcd'))
  61. bl.append(Buffer.from('efg'))
  62. bl.append(Buffer.from('hi'))
  63. bl.append(Buffer.from('j'))
  64. t.equal(bl.length, 10)
  65. t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')
  66. t.equal(bl.slice(3, 10).toString('ascii'), 'defghij')
  67. t.equal(bl.slice(3, 6).toString('ascii'), 'def')
  68. t.equal(bl.slice(3, 8).toString('ascii'), 'defgh')
  69. t.equal(bl.slice(5, 10).toString('ascii'), 'fghij')
  70. t.equal(bl.slice(-7, -4).toString('ascii'), 'def')
  71. t.end()
  72. })
  73. tape('multiple bytes from multiple buffer lists', function (t) {
  74. var bl = new BufferList()
  75. bl.append(new BufferList([ Buffer.from('abcd'), Buffer.from('efg') ]))
  76. bl.append(new BufferList([ Buffer.from('hi'), Buffer.from('j') ]))
  77. t.equal(bl.length, 10)
  78. t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')
  79. t.equal(bl.slice(3, 10).toString('ascii'), 'defghij')
  80. t.equal(bl.slice(3, 6).toString('ascii'), 'def')
  81. t.equal(bl.slice(3, 8).toString('ascii'), 'defgh')
  82. t.equal(bl.slice(5, 10).toString('ascii'), 'fghij')
  83. t.end()
  84. })
  85. // same data as previous test, just using nested constructors
  86. tape('multiple bytes from crazy nested buffer lists', function (t) {
  87. var bl = new BufferList()
  88. bl.append(new BufferList([
  89. new BufferList([
  90. new BufferList(Buffer.from('abc'))
  91. , Buffer.from('d')
  92. , new BufferList(Buffer.from('efg'))
  93. ])
  94. , new BufferList([ Buffer.from('hi') ])
  95. , new BufferList(Buffer.from('j'))
  96. ]))
  97. t.equal(bl.length, 10)
  98. t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')
  99. t.equal(bl.slice(3, 10).toString('ascii'), 'defghij')
  100. t.equal(bl.slice(3, 6).toString('ascii'), 'def')
  101. t.equal(bl.slice(3, 8).toString('ascii'), 'defgh')
  102. t.equal(bl.slice(5, 10).toString('ascii'), 'fghij')
  103. t.end()
  104. })
  105. tape('append accepts arrays of Buffers', function (t) {
  106. var bl = new BufferList()
  107. bl.append(Buffer.from('abc'))
  108. bl.append([ Buffer.from('def') ])
  109. bl.append([ Buffer.from('ghi'), Buffer.from('jkl') ])
  110. bl.append([ Buffer.from('mnop'), Buffer.from('qrstu'), Buffer.from('vwxyz') ])
  111. t.equal(bl.length, 26)
  112. t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz')
  113. t.end()
  114. })
  115. tape('append accepts arrays of BufferLists', function (t) {
  116. var bl = new BufferList()
  117. bl.append(Buffer.from('abc'))
  118. bl.append([ new BufferList('def') ])
  119. bl.append(new BufferList([ Buffer.from('ghi'), new BufferList('jkl') ]))
  120. bl.append([ Buffer.from('mnop'), new BufferList([ Buffer.from('qrstu'), Buffer.from('vwxyz') ]) ])
  121. t.equal(bl.length, 26)
  122. t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz')
  123. t.end()
  124. })
  125. tape('append chainable', function (t) {
  126. var bl = new BufferList()
  127. t.ok(bl.append(Buffer.from('abcd')) === bl)
  128. t.ok(bl.append([ Buffer.from('abcd') ]) === bl)
  129. t.ok(bl.append(new BufferList(Buffer.from('abcd'))) === bl)
  130. t.ok(bl.append([ new BufferList(Buffer.from('abcd')) ]) === bl)
  131. t.end()
  132. })
  133. tape('append chainable (test results)', function (t) {
  134. var bl = new BufferList('abc')
  135. .append([ new BufferList('def') ])
  136. .append(new BufferList([ Buffer.from('ghi'), new BufferList('jkl') ]))
  137. .append([ Buffer.from('mnop'), new BufferList([ Buffer.from('qrstu'), Buffer.from('vwxyz') ]) ])
  138. t.equal(bl.length, 26)
  139. t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz')
  140. t.end()
  141. })
  142. tape('consuming from multiple buffers', function (t) {
  143. var bl = new BufferList()
  144. bl.append(Buffer.from('abcd'))
  145. bl.append(Buffer.from('efg'))
  146. bl.append(Buffer.from('hi'))
  147. bl.append(Buffer.from('j'))
  148. t.equal(bl.length, 10)
  149. t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')
  150. bl.consume(3)
  151. t.equal(bl.length, 7)
  152. t.equal(bl.slice(0, 7).toString('ascii'), 'defghij')
  153. bl.consume(2)
  154. t.equal(bl.length, 5)
  155. t.equal(bl.slice(0, 5).toString('ascii'), 'fghij')
  156. bl.consume(1)
  157. t.equal(bl.length, 4)
  158. t.equal(bl.slice(0, 4).toString('ascii'), 'ghij')
  159. bl.consume(1)
  160. t.equal(bl.length, 3)
  161. t.equal(bl.slice(0, 3).toString('ascii'), 'hij')
  162. bl.consume(2)
  163. t.equal(bl.length, 1)
  164. t.equal(bl.slice(0, 1).toString('ascii'), 'j')
  165. t.end()
  166. })
  167. tape('complete consumption', function (t) {
  168. var bl = new BufferList()
  169. bl.append(Buffer.from('a'))
  170. bl.append(Buffer.from('b'))
  171. bl.consume(2)
  172. t.equal(bl.length, 0)
  173. t.equal(bl._bufs.length, 0)
  174. t.end()
  175. })
  176. tape('test readUInt8 / readInt8', function (t) {
  177. var buf1 = Buffer.alloc(1)
  178. , buf2 = Buffer.alloc(3)
  179. , buf3 = Buffer.alloc(3)
  180. , bl = new BufferList()
  181. buf2[1] = 0x3
  182. buf2[2] = 0x4
  183. buf3[0] = 0x23
  184. buf3[1] = 0x42
  185. bl.append(buf1)
  186. bl.append(buf2)
  187. bl.append(buf3)
  188. t.equal(bl.readUInt8(2), 0x3)
  189. t.equal(bl.readInt8(2), 0x3)
  190. t.equal(bl.readUInt8(3), 0x4)
  191. t.equal(bl.readInt8(3), 0x4)
  192. t.equal(bl.readUInt8(4), 0x23)
  193. t.equal(bl.readInt8(4), 0x23)
  194. t.equal(bl.readUInt8(5), 0x42)
  195. t.equal(bl.readInt8(5), 0x42)
  196. t.end()
  197. })
  198. tape('test readUInt16LE / readUInt16BE / readInt16LE / readInt16BE', function (t) {
  199. var buf1 = Buffer.alloc(1)
  200. , buf2 = Buffer.alloc(3)
  201. , buf3 = Buffer.alloc(3)
  202. , bl = new BufferList()
  203. buf2[1] = 0x3
  204. buf2[2] = 0x4
  205. buf3[0] = 0x23
  206. buf3[1] = 0x42
  207. bl.append(buf1)
  208. bl.append(buf2)
  209. bl.append(buf3)
  210. t.equal(bl.readUInt16BE(2), 0x0304)
  211. t.equal(bl.readUInt16LE(2), 0x0403)
  212. t.equal(bl.readInt16BE(2), 0x0304)
  213. t.equal(bl.readInt16LE(2), 0x0403)
  214. t.equal(bl.readUInt16BE(3), 0x0423)
  215. t.equal(bl.readUInt16LE(3), 0x2304)
  216. t.equal(bl.readInt16BE(3), 0x0423)
  217. t.equal(bl.readInt16LE(3), 0x2304)
  218. t.equal(bl.readUInt16BE(4), 0x2342)
  219. t.equal(bl.readUInt16LE(4), 0x4223)
  220. t.equal(bl.readInt16BE(4), 0x2342)
  221. t.equal(bl.readInt16LE(4), 0x4223)
  222. t.end()
  223. })
  224. tape('test readUInt32LE / readUInt32BE / readInt32LE / readInt32BE', function (t) {
  225. var buf1 = Buffer.alloc(1)
  226. , buf2 = Buffer.alloc(3)
  227. , buf3 = Buffer.alloc(3)
  228. , bl = new BufferList()
  229. buf2[1] = 0x3
  230. buf2[2] = 0x4
  231. buf3[0] = 0x23
  232. buf3[1] = 0x42
  233. bl.append(buf1)
  234. bl.append(buf2)
  235. bl.append(buf3)
  236. t.equal(bl.readUInt32BE(2), 0x03042342)
  237. t.equal(bl.readUInt32LE(2), 0x42230403)
  238. t.equal(bl.readInt32BE(2), 0x03042342)
  239. t.equal(bl.readInt32LE(2), 0x42230403)
  240. t.end()
  241. })
  242. tape('test readFloatLE / readFloatBE', function (t) {
  243. var buf1 = Buffer.alloc(1)
  244. , buf2 = Buffer.alloc(3)
  245. , buf3 = Buffer.alloc(3)
  246. , bl = new BufferList()
  247. buf2[1] = 0x00
  248. buf2[2] = 0x00
  249. buf3[0] = 0x80
  250. buf3[1] = 0x3f
  251. bl.append(buf1)
  252. bl.append(buf2)
  253. bl.append(buf3)
  254. t.equal(bl.readFloatLE(2), 0x01)
  255. t.end()
  256. })
  257. tape('test readDoubleLE / readDoubleBE', function (t) {
  258. var buf1 = Buffer.alloc(1)
  259. , buf2 = Buffer.alloc(3)
  260. , buf3 = Buffer.alloc(10)
  261. , bl = new BufferList()
  262. buf2[1] = 0x55
  263. buf2[2] = 0x55
  264. buf3[0] = 0x55
  265. buf3[1] = 0x55
  266. buf3[2] = 0x55
  267. buf3[3] = 0x55
  268. buf3[4] = 0xd5
  269. buf3[5] = 0x3f
  270. bl.append(buf1)
  271. bl.append(buf2)
  272. bl.append(buf3)
  273. t.equal(bl.readDoubleLE(2), 0.3333333333333333)
  274. t.end()
  275. })
  276. tape('test toString', function (t) {
  277. var bl = new BufferList()
  278. bl.append(Buffer.from('abcd'))
  279. bl.append(Buffer.from('efg'))
  280. bl.append(Buffer.from('hi'))
  281. bl.append(Buffer.from('j'))
  282. t.equal(bl.toString('ascii', 0, 10), 'abcdefghij')
  283. t.equal(bl.toString('ascii', 3, 10), 'defghij')
  284. t.equal(bl.toString('ascii', 3, 6), 'def')
  285. t.equal(bl.toString('ascii', 3, 8), 'defgh')
  286. t.equal(bl.toString('ascii', 5, 10), 'fghij')
  287. t.end()
  288. })
  289. tape('test toString encoding', function (t) {
  290. var bl = new BufferList()
  291. , b = Buffer.from('abcdefghij\xff\x00')
  292. bl.append(Buffer.from('abcd'))
  293. bl.append(Buffer.from('efg'))
  294. bl.append(Buffer.from('hi'))
  295. bl.append(Buffer.from('j'))
  296. bl.append(Buffer.from('\xff\x00'))
  297. encodings.forEach(function (enc) {
  298. t.equal(bl.toString(enc), b.toString(enc), enc)
  299. })
  300. t.end()
  301. })
  302. !process.browser && tape('test stream', function (t) {
  303. var random = crypto.randomBytes(65534)
  304. , rndhash = hash(random, 'md5')
  305. , md5sum = crypto.createHash('md5')
  306. , bl = new BufferList(function (err, buf) {
  307. t.ok(Buffer.isBuffer(buf))
  308. t.ok(err === null)
  309. t.equal(rndhash, hash(bl.slice(), 'md5'))
  310. t.equal(rndhash, hash(buf, 'md5'))
  311. bl.pipe(fs.createWriteStream('/tmp/bl_test_rnd_out.dat'))
  312. .on('close', function () {
  313. var s = fs.createReadStream('/tmp/bl_test_rnd_out.dat')
  314. s.on('data', md5sum.update.bind(md5sum))
  315. s.on('end', function() {
  316. t.equal(rndhash, md5sum.digest('hex'), 'woohoo! correct hash!')
  317. t.end()
  318. })
  319. })
  320. })
  321. fs.writeFileSync('/tmp/bl_test_rnd.dat', random)
  322. fs.createReadStream('/tmp/bl_test_rnd.dat').pipe(bl)
  323. })
  324. tape('instantiation with Buffer', function (t) {
  325. var buf = crypto.randomBytes(1024)
  326. , buf2 = crypto.randomBytes(1024)
  327. , b = BufferList(buf)
  328. t.equal(buf.toString('hex'), b.slice().toString('hex'), 'same buffer')
  329. b = BufferList([ buf, buf2 ])
  330. t.equal(b.slice().toString('hex'), Buffer.concat([ buf, buf2 ]).toString('hex'), 'same buffer')
  331. t.end()
  332. })
  333. tape('test String appendage', function (t) {
  334. var bl = new BufferList()
  335. , b = Buffer.from('abcdefghij\xff\x00')
  336. bl.append('abcd')
  337. bl.append('efg')
  338. bl.append('hi')
  339. bl.append('j')
  340. bl.append('\xff\x00')
  341. encodings.forEach(function (enc) {
  342. t.equal(bl.toString(enc), b.toString(enc))
  343. })
  344. t.end()
  345. })
  346. tape('test Number appendage', function (t) {
  347. var bl = new BufferList()
  348. , b = Buffer.from('1234567890')
  349. bl.append(1234)
  350. bl.append(567)
  351. bl.append(89)
  352. bl.append(0)
  353. encodings.forEach(function (enc) {
  354. t.equal(bl.toString(enc), b.toString(enc))
  355. })
  356. t.end()
  357. })
  358. tape('write nothing, should get empty buffer', function (t) {
  359. t.plan(3)
  360. BufferList(function (err, data) {
  361. t.notOk(err, 'no error')
  362. t.ok(Buffer.isBuffer(data), 'got a buffer')
  363. t.equal(0, data.length, 'got a zero-length buffer')
  364. t.end()
  365. }).end()
  366. })
  367. tape('unicode string', function (t) {
  368. t.plan(2)
  369. var inp1 = '\u2600'
  370. , inp2 = '\u2603'
  371. , exp = inp1 + ' and ' + inp2
  372. , bl = BufferList()
  373. bl.write(inp1)
  374. bl.write(' and ')
  375. bl.write(inp2)
  376. t.equal(exp, bl.toString())
  377. t.equal(Buffer.from(exp).toString('hex'), bl.toString('hex'))
  378. })
  379. tape('should emit finish', function (t) {
  380. var source = BufferList()
  381. , dest = BufferList()
  382. source.write('hello')
  383. source.pipe(dest)
  384. dest.on('finish', function () {
  385. t.equal(dest.toString('utf8'), 'hello')
  386. t.end()
  387. })
  388. })
  389. tape('basic copy', function (t) {
  390. var buf = crypto.randomBytes(1024)
  391. , buf2 = Buffer.alloc(1024)
  392. , b = BufferList(buf)
  393. b.copy(buf2)
  394. t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer')
  395. t.end()
  396. })
  397. tape('copy after many appends', function (t) {
  398. var buf = crypto.randomBytes(512)
  399. , buf2 = Buffer.alloc(1024)
  400. , b = BufferList(buf)
  401. b.append(buf)
  402. b.copy(buf2)
  403. t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer')
  404. t.end()
  405. })
  406. tape('copy at a precise position', function (t) {
  407. var buf = crypto.randomBytes(1004)
  408. , buf2 = Buffer.alloc(1024)
  409. , b = BufferList(buf)
  410. b.copy(buf2, 20)
  411. t.equal(b.slice().toString('hex'), buf2.slice(20).toString('hex'), 'same buffer')
  412. t.end()
  413. })
  414. tape('copy starting from a precise location', function (t) {
  415. var buf = crypto.randomBytes(10)
  416. , buf2 = Buffer.alloc(5)
  417. , b = BufferList(buf)
  418. b.copy(buf2, 0, 5)
  419. t.equal(b.slice(5).toString('hex'), buf2.toString('hex'), 'same buffer')
  420. t.end()
  421. })
  422. tape('copy in an interval', function (t) {
  423. var rnd = crypto.randomBytes(10)
  424. , b = BufferList(rnd) // put the random bytes there
  425. , actual = Buffer.alloc(3)
  426. , expected = Buffer.alloc(3)
  427. rnd.copy(expected, 0, 5, 8)
  428. b.copy(actual, 0, 5, 8)
  429. t.equal(actual.toString('hex'), expected.toString('hex'), 'same buffer')
  430. t.end()
  431. })
  432. tape('copy an interval between two buffers', function (t) {
  433. var buf = crypto.randomBytes(10)
  434. , buf2 = Buffer.alloc(10)
  435. , b = BufferList(buf)
  436. b.append(buf)
  437. b.copy(buf2, 0, 5, 15)
  438. t.equal(b.slice(5, 15).toString('hex'), buf2.toString('hex'), 'same buffer')
  439. t.end()
  440. })
  441. tape('shallow slice across buffer boundaries', function (t) {
  442. var bl = new BufferList(['First', 'Second', 'Third'])
  443. t.equal(bl.shallowSlice(3, 13).toString(), 'stSecondTh')
  444. t.end()
  445. })
  446. tape('shallow slice within single buffer', function (t) {
  447. t.plan(2)
  448. var bl = new BufferList(['First', 'Second', 'Third'])
  449. t.equal(bl.shallowSlice(5, 10).toString(), 'Secon')
  450. t.equal(bl.shallowSlice(7, 10).toString(), 'con')
  451. t.end()
  452. })
  453. tape('shallow slice single buffer', function (t) {
  454. t.plan(3)
  455. var bl = new BufferList(['First', 'Second', 'Third'])
  456. t.equal(bl.shallowSlice(0, 5).toString(), 'First')
  457. t.equal(bl.shallowSlice(5, 11).toString(), 'Second')
  458. t.equal(bl.shallowSlice(11, 16).toString(), 'Third')
  459. })
  460. tape('shallow slice with negative or omitted indices', function (t) {
  461. t.plan(4)
  462. var bl = new BufferList(['First', 'Second', 'Third'])
  463. t.equal(bl.shallowSlice().toString(), 'FirstSecondThird')
  464. t.equal(bl.shallowSlice(5).toString(), 'SecondThird')
  465. t.equal(bl.shallowSlice(5, -3).toString(), 'SecondTh')
  466. t.equal(bl.shallowSlice(-8).toString(), 'ondThird')
  467. })
  468. tape('shallow slice does not make a copy', function (t) {
  469. t.plan(1)
  470. var buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
  471. var bl = (new BufferList(buffers)).shallowSlice(5, -3)
  472. buffers[1].fill('h')
  473. buffers[2].fill('h')
  474. t.equal(bl.toString(), 'hhhhhhhh')
  475. })
  476. tape('duplicate', function (t) {
  477. t.plan(2)
  478. var bl = new BufferList('abcdefghij\xff\x00')
  479. , dup = bl.duplicate()
  480. t.equal(bl.prototype, dup.prototype)
  481. t.equal(bl.toString('hex'), dup.toString('hex'))
  482. })
  483. tape('destroy no pipe', function (t) {
  484. t.plan(2)
  485. var bl = new BufferList('alsdkfja;lsdkfja;lsdk')
  486. bl.destroy()
  487. t.equal(bl._bufs.length, 0)
  488. t.equal(bl.length, 0)
  489. })
  490. !process.browser && tape('destroy with pipe before read end', function (t) {
  491. t.plan(2)
  492. var bl = new BufferList()
  493. fs.createReadStream(__dirname + '/test.js')
  494. .pipe(bl)
  495. bl.destroy()
  496. t.equal(bl._bufs.length, 0)
  497. t.equal(bl.length, 0)
  498. })
  499. !process.browser && tape('destroy with pipe before read end with race', function (t) {
  500. t.plan(2)
  501. var bl = new BufferList()
  502. fs.createReadStream(__dirname + '/test.js')
  503. .pipe(bl)
  504. setTimeout(function () {
  505. bl.destroy()
  506. setTimeout(function () {
  507. t.equal(bl._bufs.length, 0)
  508. t.equal(bl.length, 0)
  509. }, 500)
  510. }, 500)
  511. })
  512. !process.browser && tape('destroy with pipe after read end', function (t) {
  513. t.plan(2)
  514. var bl = new BufferList()
  515. fs.createReadStream(__dirname + '/test.js')
  516. .on('end', onEnd)
  517. .pipe(bl)
  518. function onEnd () {
  519. bl.destroy()
  520. t.equal(bl._bufs.length, 0)
  521. t.equal(bl.length, 0)
  522. }
  523. })
  524. !process.browser && tape('destroy with pipe while writing to a destination', function (t) {
  525. t.plan(4)
  526. var bl = new BufferList()
  527. , ds = new BufferList()
  528. fs.createReadStream(__dirname + '/test.js')
  529. .on('end', onEnd)
  530. .pipe(bl)
  531. function onEnd () {
  532. bl.pipe(ds)
  533. setTimeout(function () {
  534. bl.destroy()
  535. t.equals(bl._bufs.length, 0)
  536. t.equals(bl.length, 0)
  537. ds.destroy()
  538. t.equals(bl._bufs.length, 0)
  539. t.equals(bl.length, 0)
  540. }, 100)
  541. }
  542. })
  543. !process.browser && tape('handle error', function (t) {
  544. t.plan(2)
  545. fs.createReadStream('/does/not/exist').pipe(BufferList(function (err, data) {
  546. t.ok(err instanceof Error, 'has error')
  547. t.notOk(data, 'no data')
  548. }))
  549. })