版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

915 строки
22 KiB

  1. // @ts-check
  2. 'use strict'
  3. const tape = require('tape')
  4. const crypto = require('crypto')
  5. const fs = require('fs')
  6. const path = require('path')
  7. const os = require('os')
  8. const BufferListStream = require('../')
  9. const { Buffer } = require('buffer')
  10. /**
  11. * This typedef allows us to add _bufs to the API without declaring it publicly on types.
  12. * @typedef { BufferListStream & { _bufs?: Buffer[] }} BufferListStreamWithPrivate
  13. */
  14. /**
  15. * Just for typechecking in js
  16. * @type { NodeJS.Process & { browser?: boolean }}
  17. */
  18. const process = globalThis.process
  19. /** @type {BufferEncoding[]} */
  20. const encodings = ['ascii', 'utf8', 'utf-8', 'hex', 'binary', 'base64']
  21. if (process.browser) {
  22. encodings.push(
  23. 'ucs2',
  24. 'ucs-2',
  25. 'utf16le',
  26. /**
  27. * This alias is not in typescript typings for BufferEncoding. Still have to fix
  28. * @see https://nodejs.org/api/buffer.html#buffers-and-character-encodings
  29. */
  30. // @ts-ignore
  31. 'utf-16le'
  32. )
  33. }
  34. require('./indexOf')
  35. require('./isBufferList')
  36. require('./convert')
  37. tape('single bytes from single buffer', function (t) {
  38. const bl = new BufferListStream()
  39. bl.append(Buffer.from('abcd'))
  40. t.equal(bl.length, 4)
  41. t.equal(bl.get(-1), undefined)
  42. t.equal(bl.get(0), 97)
  43. t.equal(bl.get(1), 98)
  44. t.equal(bl.get(2), 99)
  45. t.equal(bl.get(3), 100)
  46. t.equal(bl.get(4), undefined)
  47. t.end()
  48. })
  49. tape('single bytes from multiple buffers', function (t) {
  50. const bl = new BufferListStream()
  51. bl.append(Buffer.from('abcd'))
  52. bl.append(Buffer.from('efg'))
  53. bl.append(Buffer.from('hi'))
  54. bl.append(Buffer.from('j'))
  55. t.equal(bl.length, 10)
  56. t.equal(bl.get(0), 97)
  57. t.equal(bl.get(1), 98)
  58. t.equal(bl.get(2), 99)
  59. t.equal(bl.get(3), 100)
  60. t.equal(bl.get(4), 101)
  61. t.equal(bl.get(5), 102)
  62. t.equal(bl.get(6), 103)
  63. t.equal(bl.get(7), 104)
  64. t.equal(bl.get(8), 105)
  65. t.equal(bl.get(9), 106)
  66. t.end()
  67. })
  68. tape('multi bytes from single buffer', function (t) {
  69. const bl = new BufferListStream()
  70. bl.append(Buffer.from('abcd'))
  71. t.equal(bl.length, 4)
  72. t.equal(bl.slice(0, 4).toString('ascii'), 'abcd')
  73. t.equal(bl.slice(0, 3).toString('ascii'), 'abc')
  74. t.equal(bl.slice(1, 4).toString('ascii'), 'bcd')
  75. t.equal(bl.slice(-4, -1).toString('ascii'), 'abc')
  76. t.end()
  77. })
  78. tape('multi bytes from single buffer (negative indexes)', function (t) {
  79. const bl = new BufferListStream()
  80. bl.append(Buffer.from('buffer'))
  81. t.equal(bl.length, 6)
  82. t.equal(bl.slice(-6, -1).toString('ascii'), 'buffe')
  83. t.equal(bl.slice(-6, -2).toString('ascii'), 'buff')
  84. t.equal(bl.slice(-5, -2).toString('ascii'), 'uff')
  85. t.end()
  86. })
  87. tape('multiple bytes from multiple buffers', function (t) {
  88. const bl = new BufferListStream()
  89. bl.append(Buffer.from('abcd'))
  90. bl.append(Buffer.from('efg'))
  91. bl.append(Buffer.from('hi'))
  92. bl.append(Buffer.from('j'))
  93. t.equal(bl.length, 10)
  94. t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')
  95. t.equal(bl.slice(3, 10).toString('ascii'), 'defghij')
  96. t.equal(bl.slice(3, 6).toString('ascii'), 'def')
  97. t.equal(bl.slice(3, 8).toString('ascii'), 'defgh')
  98. t.equal(bl.slice(5, 10).toString('ascii'), 'fghij')
  99. t.equal(bl.slice(-7, -4).toString('ascii'), 'def')
  100. t.end()
  101. })
  102. tape('multiple bytes from multiple buffer lists', function (t) {
  103. const bl = new BufferListStream()
  104. bl.append(new BufferListStream([Buffer.from('abcd'), Buffer.from('efg')]))
  105. bl.append(new BufferListStream([Buffer.from('hi'), Buffer.from('j')]))
  106. t.equal(bl.length, 10)
  107. t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')
  108. t.equal(bl.slice(3, 10).toString('ascii'), 'defghij')
  109. t.equal(bl.slice(3, 6).toString('ascii'), 'def')
  110. t.equal(bl.slice(3, 8).toString('ascii'), 'defgh')
  111. t.equal(bl.slice(5, 10).toString('ascii'), 'fghij')
  112. t.end()
  113. })
  114. // same data as previous test, just using nested constructors
  115. tape('multiple bytes from crazy nested buffer lists', function (t) {
  116. const bl = new BufferListStream()
  117. bl.append(
  118. new BufferListStream([
  119. new BufferListStream([
  120. new BufferListStream(Buffer.from('abc')),
  121. Buffer.from('d'),
  122. new BufferListStream(Buffer.from('efg'))
  123. ]),
  124. new BufferListStream([Buffer.from('hi')]),
  125. new BufferListStream(Buffer.from('j'))
  126. ])
  127. )
  128. t.equal(bl.length, 10)
  129. t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')
  130. t.equal(bl.slice(3, 10).toString('ascii'), 'defghij')
  131. t.equal(bl.slice(3, 6).toString('ascii'), 'def')
  132. t.equal(bl.slice(3, 8).toString('ascii'), 'defgh')
  133. t.equal(bl.slice(5, 10).toString('ascii'), 'fghij')
  134. t.end()
  135. })
  136. tape('append accepts arrays of Buffers', function (t) {
  137. const bl = new BufferListStream()
  138. bl.append(Buffer.from('abc'))
  139. bl.append([Buffer.from('def')])
  140. bl.append([Buffer.from('ghi'), Buffer.from('jkl')])
  141. bl.append([Buffer.from('mnop'), Buffer.from('qrstu'), Buffer.from('vwxyz')])
  142. t.equal(bl.length, 26)
  143. t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz')
  144. t.end()
  145. })
  146. tape('append accepts arrays of Uint8Arrays', function (t) {
  147. const bl = new BufferListStream()
  148. bl.append(new Uint8Array([97, 98, 99]))
  149. bl.append([Uint8Array.from([100, 101, 102])])
  150. bl.append([new Uint8Array([103, 104, 105]), new Uint8Array([106, 107, 108])])
  151. bl.append([new Uint8Array([109, 110, 111, 112]), new Uint8Array([113, 114, 115, 116, 117]), new Uint8Array([118, 119, 120, 121, 122])])
  152. t.equal(bl.length, 26)
  153. t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz')
  154. t.end()
  155. })
  156. tape('append accepts arrays of BufferLists', function (t) {
  157. const bl = new BufferListStream()
  158. bl.append(Buffer.from('abc'))
  159. bl.append([new BufferListStream('def')])
  160. bl.append(
  161. new BufferListStream([Buffer.from('ghi'), new BufferListStream('jkl')])
  162. )
  163. bl.append([
  164. Buffer.from('mnop'),
  165. new BufferListStream([Buffer.from('qrstu'), Buffer.from('vwxyz')])
  166. ])
  167. t.equal(bl.length, 26)
  168. t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz')
  169. t.end()
  170. })
  171. tape('append chainable', function (t) {
  172. const bl = new BufferListStream()
  173. t.ok(bl.append(Buffer.from('abcd')) === bl)
  174. t.ok(bl.append([Buffer.from('abcd')]) === bl)
  175. t.ok(bl.append(new BufferListStream(Buffer.from('abcd'))) === bl)
  176. t.ok(bl.append([new BufferListStream(Buffer.from('abcd'))]) === bl)
  177. t.end()
  178. })
  179. tape('append chainable (test results)', function (t) {
  180. const bl = new BufferListStream('abc')
  181. .append([new BufferListStream('def')])
  182. .append(
  183. new BufferListStream([Buffer.from('ghi'), new BufferListStream('jkl')])
  184. )
  185. .append([
  186. Buffer.from('mnop'),
  187. new BufferListStream([Buffer.from('qrstu'), Buffer.from('vwxyz')])
  188. ])
  189. t.equal(bl.length, 26)
  190. t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz')
  191. t.end()
  192. })
  193. tape('consuming from multiple buffers', function (t) {
  194. const bl = new BufferListStream()
  195. bl.append(Buffer.from('abcd'))
  196. bl.append(Buffer.from('efg'))
  197. bl.append(Buffer.from('hi'))
  198. bl.append(Buffer.from('j'))
  199. t.equal(bl.length, 10)
  200. t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')
  201. bl.consume(3)
  202. t.equal(bl.length, 7)
  203. t.equal(bl.slice(0, 7).toString('ascii'), 'defghij')
  204. bl.consume(2)
  205. t.equal(bl.length, 5)
  206. t.equal(bl.slice(0, 5).toString('ascii'), 'fghij')
  207. bl.consume(1)
  208. t.equal(bl.length, 4)
  209. t.equal(bl.slice(0, 4).toString('ascii'), 'ghij')
  210. bl.consume(1)
  211. t.equal(bl.length, 3)
  212. t.equal(bl.slice(0, 3).toString('ascii'), 'hij')
  213. bl.consume(2)
  214. t.equal(bl.length, 1)
  215. t.equal(bl.slice(0, 1).toString('ascii'), 'j')
  216. t.end()
  217. })
  218. tape('complete consumption', function (t) {
  219. /** @type {BufferListStreamWithPrivate} */
  220. const bl = new BufferListStream()
  221. bl.append(Buffer.from('a'))
  222. bl.append(Buffer.from('b'))
  223. bl.consume(2)
  224. t.equal(bl.length, 0)
  225. t.equal(bl._bufs.length, 0)
  226. t.end()
  227. })
  228. tape('test readUInt8 / readInt8', function (t) {
  229. const buf1 = Buffer.alloc(1)
  230. const buf2 = Buffer.alloc(3)
  231. const buf3 = Buffer.alloc(3)
  232. const bl = new BufferListStream()
  233. buf1[0] = 0x1
  234. buf2[1] = 0x3
  235. buf2[2] = 0x4
  236. buf3[0] = 0x23
  237. buf3[1] = 0x42
  238. bl.append(buf1)
  239. bl.append(buf2)
  240. bl.append(buf3)
  241. t.equal(bl.readUInt8(), 0x1)
  242. t.equal(bl.readUInt8(2), 0x3)
  243. t.equal(bl.readInt8(2), 0x3)
  244. t.equal(bl.readUInt8(3), 0x4)
  245. t.equal(bl.readInt8(3), 0x4)
  246. t.equal(bl.readUInt8(4), 0x23)
  247. t.equal(bl.readInt8(4), 0x23)
  248. t.equal(bl.readUInt8(5), 0x42)
  249. t.equal(bl.readInt8(5), 0x42)
  250. t.end()
  251. })
  252. tape('test readUInt16LE / readUInt16BE / readInt16LE / readInt16BE', function (t) {
  253. const buf1 = Buffer.alloc(1)
  254. const buf2 = Buffer.alloc(3)
  255. const buf3 = Buffer.alloc(3)
  256. const bl = new BufferListStream()
  257. buf1[0] = 0x1
  258. buf2[1] = 0x3
  259. buf2[2] = 0x4
  260. buf3[0] = 0x23
  261. buf3[1] = 0x42
  262. bl.append(buf1)
  263. bl.append(buf2)
  264. bl.append(buf3)
  265. t.equal(bl.readUInt16BE(), 0x0100)
  266. t.equal(bl.readUInt16LE(), 0x0001)
  267. t.equal(bl.readUInt16BE(2), 0x0304)
  268. t.equal(bl.readUInt16LE(2), 0x0403)
  269. t.equal(bl.readInt16BE(2), 0x0304)
  270. t.equal(bl.readInt16LE(2), 0x0403)
  271. t.equal(bl.readUInt16BE(3), 0x0423)
  272. t.equal(bl.readUInt16LE(3), 0x2304)
  273. t.equal(bl.readInt16BE(3), 0x0423)
  274. t.equal(bl.readInt16LE(3), 0x2304)
  275. t.equal(bl.readUInt16BE(4), 0x2342)
  276. t.equal(bl.readUInt16LE(4), 0x4223)
  277. t.equal(bl.readInt16BE(4), 0x2342)
  278. t.equal(bl.readInt16LE(4), 0x4223)
  279. t.end()
  280. })
  281. tape('test readUInt32LE / readUInt32BE / readInt32LE / readInt32BE', function (t) {
  282. const buf1 = Buffer.alloc(1)
  283. const buf2 = Buffer.alloc(3)
  284. const buf3 = Buffer.alloc(3)
  285. const bl = new BufferListStream()
  286. buf1[0] = 0x1
  287. buf2[1] = 0x3
  288. buf2[2] = 0x4
  289. buf3[0] = 0x23
  290. buf3[1] = 0x42
  291. bl.append(buf1)
  292. bl.append(buf2)
  293. bl.append(buf3)
  294. t.equal(bl.readUInt32BE(), 0x01000304)
  295. t.equal(bl.readUInt32LE(), 0x04030001)
  296. t.equal(bl.readUInt32BE(2), 0x03042342)
  297. t.equal(bl.readUInt32LE(2), 0x42230403)
  298. t.equal(bl.readInt32BE(2), 0x03042342)
  299. t.equal(bl.readInt32LE(2), 0x42230403)
  300. t.end()
  301. })
  302. tape('test readUIntLE / readUIntBE / readIntLE / readIntBE', function (t) {
  303. const buf1 = Buffer.alloc(1)
  304. const buf2 = Buffer.alloc(3)
  305. const buf3 = Buffer.alloc(3)
  306. const bl = new BufferListStream()
  307. buf2[0] = 0x2
  308. buf2[1] = 0x3
  309. buf2[2] = 0x4
  310. buf3[0] = 0x23
  311. buf3[1] = 0x42
  312. buf3[2] = 0x61
  313. bl.append(buf1)
  314. bl.append(buf2)
  315. bl.append(buf3)
  316. t.equal(bl.readUIntBE(1, 1), 0x02)
  317. t.equal(bl.readUIntBE(1, 2), 0x0203)
  318. t.equal(bl.readUIntBE(1, 3), 0x020304)
  319. t.equal(bl.readUIntBE(1, 4), 0x02030423)
  320. t.equal(bl.readUIntBE(1, 5), 0x0203042342)
  321. t.equal(bl.readUIntBE(1, 6), 0x020304234261)
  322. t.equal(bl.readUIntLE(1, 1), 0x02)
  323. t.equal(bl.readUIntLE(1, 2), 0x0302)
  324. t.equal(bl.readUIntLE(1, 3), 0x040302)
  325. t.equal(bl.readUIntLE(1, 4), 0x23040302)
  326. t.equal(bl.readUIntLE(1, 5), 0x4223040302)
  327. t.equal(bl.readUIntLE(1, 6), 0x614223040302)
  328. t.equal(bl.readIntBE(1, 1), 0x02)
  329. t.equal(bl.readIntBE(1, 2), 0x0203)
  330. t.equal(bl.readIntBE(1, 3), 0x020304)
  331. t.equal(bl.readIntBE(1, 4), 0x02030423)
  332. t.equal(bl.readIntBE(1, 5), 0x0203042342)
  333. t.equal(bl.readIntBE(1, 6), 0x020304234261)
  334. t.equal(bl.readIntLE(1, 1), 0x02)
  335. t.equal(bl.readIntLE(1, 2), 0x0302)
  336. t.equal(bl.readIntLE(1, 3), 0x040302)
  337. t.equal(bl.readIntLE(1, 4), 0x23040302)
  338. t.equal(bl.readIntLE(1, 5), 0x4223040302)
  339. t.equal(bl.readIntLE(1, 6), 0x614223040302)
  340. t.end()
  341. })
  342. tape('test readFloatLE / readFloatBE', function (t) {
  343. const buf1 = Buffer.alloc(1)
  344. const buf2 = Buffer.alloc(3)
  345. const buf3 = Buffer.alloc(3)
  346. const bl = new BufferListStream()
  347. buf1[0] = 0x01
  348. buf2[1] = 0x00
  349. buf2[2] = 0x00
  350. buf3[0] = 0x80
  351. buf3[1] = 0x3f
  352. bl.append(buf1)
  353. bl.append(buf2)
  354. bl.append(buf3)
  355. const canonical = Buffer.concat([buf1, buf2, buf3])
  356. t.equal(bl.readFloatLE(), canonical.readFloatLE())
  357. t.equal(bl.readFloatBE(), canonical.readFloatBE())
  358. t.equal(bl.readFloatLE(2), canonical.readFloatLE(2))
  359. t.equal(bl.readFloatBE(2), canonical.readFloatBE(2))
  360. t.end()
  361. })
  362. tape('test readDoubleLE / readDoubleBE', function (t) {
  363. const buf1 = Buffer.alloc(1)
  364. const buf2 = Buffer.alloc(3)
  365. const buf3 = Buffer.alloc(10)
  366. const bl = new BufferListStream()
  367. buf1[0] = 0x01
  368. buf2[1] = 0x55
  369. buf2[2] = 0x55
  370. buf3[0] = 0x55
  371. buf3[1] = 0x55
  372. buf3[2] = 0x55
  373. buf3[3] = 0x55
  374. buf3[4] = 0xd5
  375. buf3[5] = 0x3f
  376. bl.append(buf1)
  377. bl.append(buf2)
  378. bl.append(buf3)
  379. const canonical = Buffer.concat([buf1, buf2, buf3])
  380. t.equal(bl.readDoubleBE(), canonical.readDoubleBE())
  381. t.equal(bl.readDoubleLE(), canonical.readDoubleLE())
  382. t.equal(bl.readDoubleBE(2), canonical.readDoubleBE(2))
  383. t.equal(bl.readDoubleLE(2), canonical.readDoubleLE(2))
  384. t.end()
  385. })
  386. tape('test toString', function (t) {
  387. const bl = new BufferListStream()
  388. bl.append(Buffer.from('abcd'))
  389. bl.append(Buffer.from('efg'))
  390. bl.append(Buffer.from('hi'))
  391. bl.append(Buffer.from('j'))
  392. t.equal(bl.toString('ascii', 0, 10), 'abcdefghij')
  393. t.equal(bl.toString('ascii', 3, 10), 'defghij')
  394. t.equal(bl.toString('ascii', 3, 6), 'def')
  395. t.equal(bl.toString('ascii', 3, 8), 'defgh')
  396. t.equal(bl.toString('ascii', 5, 10), 'fghij')
  397. t.end()
  398. })
  399. tape('test toString encoding', function (t) {
  400. const bl = new BufferListStream()
  401. const b = Buffer.from('abcdefghij\xff\x00')
  402. bl.append(Buffer.from('abcd'))
  403. bl.append(Buffer.from('efg'))
  404. bl.append(Buffer.from('hi'))
  405. bl.append(Buffer.from('j'))
  406. bl.append(Buffer.from('\xff\x00'))
  407. encodings.forEach(function (enc) {
  408. t.equal(bl.toString(enc), b.toString(enc), enc)
  409. })
  410. t.end()
  411. })
  412. tape('uninitialized memory', function (t) {
  413. const secret = crypto.randomBytes(256)
  414. for (let i = 0; i < 1e6; i++) {
  415. const clone = Buffer.from(secret)
  416. const bl = new BufferListStream()
  417. bl.append(Buffer.from('a'))
  418. bl.consume(-1024)
  419. const buf = bl.slice(1)
  420. if (buf.indexOf(clone) !== -1) {
  421. t.fail(`Match (at ${i})`)
  422. break
  423. }
  424. }
  425. t.end()
  426. })
  427. !process.browser && tape('test stream', function (t) {
  428. const random = crypto.randomBytes(65534)
  429. const bl = new BufferListStream((err, buf) => {
  430. t.ok(Buffer.isBuffer(buf))
  431. t.ok(err === null)
  432. t.ok(random.equals(bl.slice()))
  433. t.ok(random.equals(buf.slice()))
  434. bl.pipe(fs.createWriteStream(path.join(os.tmpdir(), 'bl_test_rnd_out.dat')))
  435. .on('close', function () {
  436. const rndhash = crypto.createHash('md5').update(random).digest('hex')
  437. const md5sum = crypto.createHash('md5')
  438. const s = fs.createReadStream(path.join(os.tmpdir(), 'bl_test_rnd_out.dat'))
  439. s.on('data', md5sum.update.bind(md5sum))
  440. s.on('end', function () {
  441. t.equal(rndhash, md5sum.digest('hex'), 'woohoo! correct hash!')
  442. t.end()
  443. })
  444. })
  445. })
  446. fs.writeFileSync(path.join(os.tmpdir(), 'bl_test_rnd.dat'), random)
  447. fs.createReadStream(path.join(os.tmpdir(), 'bl_test_rnd.dat')).pipe(bl)
  448. })
  449. tape('instantiation with Buffer', function (t) {
  450. const buf = crypto.randomBytes(1024)
  451. const buf2 = crypto.randomBytes(1024)
  452. let b = BufferListStream(buf)
  453. t.equal(buf.toString('hex'), b.slice().toString('hex'), 'same buffer')
  454. b = BufferListStream([buf, buf2])
  455. t.equal(b.slice().toString('hex'), Buffer.concat([buf, buf2]).toString('hex'), 'same buffer')
  456. t.end()
  457. })
  458. tape('test String appendage', function (t) {
  459. const bl = new BufferListStream()
  460. const b = Buffer.from('abcdefghij\xff\x00')
  461. bl.append('abcd')
  462. bl.append('efg')
  463. bl.append('hi')
  464. bl.append('j')
  465. bl.append('\xff\x00')
  466. encodings.forEach(function (enc) {
  467. t.equal(bl.toString(enc), b.toString(enc))
  468. })
  469. t.end()
  470. })
  471. tape('test Number appendage', function (t) {
  472. const bl = new BufferListStream()
  473. const b = Buffer.from('1234567890')
  474. bl.append(1234)
  475. bl.append(567)
  476. bl.append(89)
  477. bl.append(0)
  478. encodings.forEach(function (enc) {
  479. t.equal(bl.toString(enc), b.toString(enc))
  480. })
  481. t.end()
  482. })
  483. tape('write nothing, should get empty buffer', function (t) {
  484. t.plan(3)
  485. BufferListStream(function (err, data) {
  486. t.notOk(err, 'no error')
  487. t.ok(Buffer.isBuffer(data), 'got a buffer')
  488. t.equal(0, data.length, 'got a zero-length buffer')
  489. t.end()
  490. }).end()
  491. })
  492. tape('unicode string', function (t) {
  493. t.plan(2)
  494. const inp1 = '\u2600'
  495. const inp2 = '\u2603'
  496. const exp = inp1 + ' and ' + inp2
  497. const bl = BufferListStream()
  498. bl.write(inp1)
  499. bl.write(' and ')
  500. bl.write(inp2)
  501. t.equal(exp, bl.toString())
  502. t.equal(Buffer.from(exp).toString('hex'), bl.toString('hex'))
  503. })
  504. tape('should emit finish', function (t) {
  505. const source = BufferListStream()
  506. const dest = BufferListStream()
  507. source.write('hello')
  508. source.pipe(dest)
  509. dest.on('finish', function () {
  510. t.equal(dest.toString('utf8'), 'hello')
  511. t.end()
  512. })
  513. })
  514. tape('basic copy', function (t) {
  515. const buf = crypto.randomBytes(1024)
  516. const buf2 = Buffer.alloc(1024)
  517. const b = BufferListStream(buf)
  518. b.copy(buf2)
  519. t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer')
  520. t.end()
  521. })
  522. tape('copy after many appends', function (t) {
  523. const buf = crypto.randomBytes(512)
  524. const buf2 = Buffer.alloc(1024)
  525. const b = BufferListStream(buf)
  526. b.append(buf)
  527. b.copy(buf2)
  528. t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer')
  529. t.end()
  530. })
  531. tape('copy at a precise position', function (t) {
  532. const buf = crypto.randomBytes(1004)
  533. const buf2 = Buffer.alloc(1024)
  534. const b = BufferListStream(buf)
  535. b.copy(buf2, 20)
  536. t.equal(b.slice().toString('hex'), buf2.slice(20).toString('hex'), 'same buffer')
  537. t.end()
  538. })
  539. tape('copy starting from a precise location', function (t) {
  540. const buf = crypto.randomBytes(10)
  541. const buf2 = Buffer.alloc(5)
  542. const b = BufferListStream(buf)
  543. b.copy(buf2, 0, 5)
  544. t.equal(b.slice(5).toString('hex'), buf2.toString('hex'), 'same buffer')
  545. t.end()
  546. })
  547. tape('copy in an interval', function (t) {
  548. const rnd = crypto.randomBytes(10)
  549. const b = BufferListStream(rnd) // put the random bytes there
  550. const actual = Buffer.alloc(3)
  551. const expected = Buffer.alloc(3)
  552. rnd.copy(expected, 0, 5, 8)
  553. b.copy(actual, 0, 5, 8)
  554. t.equal(actual.toString('hex'), expected.toString('hex'), 'same buffer')
  555. t.end()
  556. })
  557. tape('copy an interval between two buffers', function (t) {
  558. const buf = crypto.randomBytes(10)
  559. const buf2 = Buffer.alloc(10)
  560. const b = BufferListStream(buf)
  561. b.append(buf)
  562. b.copy(buf2, 0, 5, 15)
  563. t.equal(b.slice(5, 15).toString('hex'), buf2.toString('hex'), 'same buffer')
  564. t.end()
  565. })
  566. tape('shallow slice across buffer boundaries', function (t) {
  567. const bl = new BufferListStream(['First', 'Second', 'Third'])
  568. t.equal(bl.shallowSlice(3, 13).toString(), 'stSecondTh')
  569. t.end()
  570. })
  571. tape('shallow slice within single buffer', function (t) {
  572. t.plan(2)
  573. const bl = new BufferListStream(['First', 'Second', 'Third'])
  574. t.equal(bl.shallowSlice(5, 10).toString(), 'Secon')
  575. t.equal(bl.shallowSlice(7, 10).toString(), 'con')
  576. t.end()
  577. })
  578. tape('shallow slice single buffer', function (t) {
  579. t.plan(3)
  580. const bl = new BufferListStream(['First', 'Second', 'Third'])
  581. t.equal(bl.shallowSlice(0, 5).toString(), 'First')
  582. t.equal(bl.shallowSlice(5, 11).toString(), 'Second')
  583. t.equal(bl.shallowSlice(11, 16).toString(), 'Third')
  584. })
  585. tape('shallow slice with negative or omitted indices', function (t) {
  586. t.plan(4)
  587. const bl = new BufferListStream(['First', 'Second', 'Third'])
  588. t.equal(bl.shallowSlice().toString(), 'FirstSecondThird')
  589. t.equal(bl.shallowSlice(5).toString(), 'SecondThird')
  590. t.equal(bl.shallowSlice(5, -3).toString(), 'SecondTh')
  591. t.equal(bl.shallowSlice(-8).toString(), 'ondThird')
  592. })
  593. tape('shallow slice does not make a copy', function (t) {
  594. t.plan(1)
  595. const buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
  596. const bl = new BufferListStream(buffers).shallowSlice(5, -3)
  597. buffers[1].fill('h')
  598. buffers[2].fill('h')
  599. t.equal(bl.toString(), 'hhhhhhhh')
  600. })
  601. tape('shallow slice with 0 length', function (t) {
  602. t.plan(1)
  603. const buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
  604. const bl = (new BufferListStream(buffers)).shallowSlice(0, 0)
  605. t.equal(bl.length, 0)
  606. })
  607. tape('shallow slice with 0 length from middle', function (t) {
  608. t.plan(1)
  609. const buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
  610. const bl = (new BufferListStream(buffers)).shallowSlice(10, 10)
  611. t.equal(bl.length, 0)
  612. })
  613. tape('duplicate', function (t) {
  614. t.plan(2)
  615. const bl = new BufferListStream('abcdefghij\xff\x00')
  616. const dup = bl.duplicate()
  617. t.equal(bl.prototype, dup.prototype)
  618. t.equal(bl.toString('hex'), dup.toString('hex'))
  619. })
  620. tape('destroy no pipe', function (t) {
  621. t.plan(2)
  622. /** @type {BufferListStreamWithPrivate} */
  623. const bl = new BufferListStream('alsdkfja;lsdkfja;lsdk')
  624. bl.destroy()
  625. t.equal(bl._bufs.length, 0)
  626. t.equal(bl.length, 0)
  627. })
  628. tape('destroy with error', function (t) {
  629. t.plan(3)
  630. /** @type {BufferListStreamWithPrivate} */
  631. const bl = new BufferListStream('alsdkfja;lsdkfja;lsdk')
  632. const err = new Error('kaboom')
  633. bl.destroy(err)
  634. bl.on('error', function (_err) {
  635. t.equal(_err, err)
  636. })
  637. t.equal(bl._bufs.length, 0)
  638. t.equal(bl.length, 0)
  639. })
  640. !process.browser && tape('destroy with pipe before read end', function (t) {
  641. t.plan(2)
  642. /** @type {BufferListStreamWithPrivate} */
  643. const bl = new BufferListStream()
  644. fs.createReadStream(path.join(__dirname, '/test.js'))
  645. .pipe(bl)
  646. bl.destroy()
  647. t.equal(bl._bufs.length, 0)
  648. t.equal(bl.length, 0)
  649. })
  650. !process.browser && tape('destroy with pipe before read end with race', function (t) {
  651. t.plan(2)
  652. /** @type {BufferListStreamWithPrivate} */
  653. const bl = new BufferListStream()
  654. fs.createReadStream(path.join(__dirname, '/test.js'))
  655. .pipe(bl)
  656. setTimeout(function () {
  657. bl.destroy()
  658. setTimeout(function () {
  659. t.equal(bl._bufs.length, 0)
  660. t.equal(bl.length, 0)
  661. }, 500)
  662. }, 500)
  663. })
  664. !process.browser && tape('destroy with pipe after read end', function (t) {
  665. t.plan(2)
  666. /** @type {BufferListStreamWithPrivate} */
  667. const bl = new BufferListStream()
  668. fs.createReadStream(path.join(__dirname, '/test.js'))
  669. .on('end', onEnd)
  670. .pipe(bl)
  671. function onEnd () {
  672. bl.destroy()
  673. t.equal(bl._bufs.length, 0)
  674. t.equal(bl.length, 0)
  675. }
  676. })
  677. !process.browser && tape('destroy with pipe while writing to a destination', function (t) {
  678. t.plan(4)
  679. /** @type {BufferListStreamWithPrivate} */
  680. const bl = new BufferListStream()
  681. const ds = new BufferListStream()
  682. fs.createReadStream(path.join(__dirname, '/test.js'))
  683. .on('end', onEnd)
  684. .pipe(bl)
  685. function onEnd () {
  686. bl.pipe(ds)
  687. setTimeout(function () {
  688. bl.destroy()
  689. t.equals(bl._bufs.length, 0)
  690. t.equals(bl.length, 0)
  691. ds.destroy()
  692. t.equals(bl._bufs.length, 0)
  693. t.equals(bl.length, 0)
  694. }, 100)
  695. }
  696. })
  697. !process.browser && tape('handle error', function (t) {
  698. t.plan(2)
  699. fs.createReadStream('/does/not/exist').pipe(BufferListStream(function (err, data) {
  700. t.ok(err instanceof Error, 'has error')
  701. t.notOk(data, 'no data')
  702. }))
  703. })