Skip to content
This repository has been archived by the owner on Aug 12, 2020. It is now read-only.

Setting offset in catReadableStream results in incorrect stream length #229

Closed
jhiesey opened this issue Oct 11, 2018 · 2 comments
Closed

Comments

@jhiesey
Copy link

jhiesey commented Oct 11, 2018

@achingbrain

The following example doesn't give the expected result:

const Ipfs = require('ipfs')
const videoCid = 'QmcPZGXSyaQSnDVEtJrgtp2EyxhENiBdpGa8pfs5rMBCWr' // sintel.mp4
const fs = require('fs')

const offset = 48

const ipfs = new Ipfs()
ipfs.on('ready', () => {
	let numDone = 0
	const s1Data = []
	const s2Data = []
	const s1 = ipfs.files.catReadableStream(videoCid, {
		offset: 0
	})

	s1.on('data', (buf) => {
		s1Data.push(buf)
	})

	s1.on('end', () => {
		if (++numDone === 2) {
			checkResult()
		}
	})

	const s2 = ipfs.files.catReadableStream(videoCid, {
		offset: offset
	})

	s2.on('data', (buf) => {
		s2Data.push(buf)
	})

	s2.on('end', () => {
		if (++numDone === 2) {
			checkResult()
		}
	})

	function checkResult () {
		const b1 = Buffer.concat(s1Data).slice(offset)
		const b2 = Buffer.concat(s2Data)
		const eq = b1.equals(b2)

		console.log('length 1', b1.length)
		console.log('length 2', b2.length)
		console.log('equal?', eq)
		process.exit(eq ? 0 : 1)
	}
})
@achingbrain
Copy link
Collaborator

Great catch, thanks for pointing this out! The fix has been released in v0.32.7.

@jhiesey
Copy link
Author

jhiesey commented Oct 12, 2018

Thanks for the quick fix!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants