Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only getting a small segment of the requested body (or 'why is my body 'passthrough?') #31

Closed
jfederer opened this issue Mar 23, 2022 · 3 comments

Comments

@jfederer
Copy link

With the following code:

const DigestFetch = require('digest-fetch')
const fs = require('fs')

const user = 'xxx'
const pass = 'xxx'
const uri = 'xxx'

const client = new DigestFetch(user, pass)

options = {}

async function doFetch() {
	client.fetch(uri, options)
		.then(async resp => {
			console.log('resp.body :>> ', resp.body);
			const filename = "picture.jpg"
			const fileContents = Buffer.from(resp.body._readableState.buffer.head.data, 'base64')
			console.log('fileContents :>> ', fileContents); // outputs a buffer that's about 1k in size
			fs.writeFile(filename, fileContents, (err) => {
				if (err) return console.error(err)
				console.log('file saved to ', filename)
				return resp.body
			})

		})
		.catch(err => console.log('err :>> ', err));
}

doFetch()

resp.body is:

PassThrough {
_readableState: ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: [Object], tail: [Object], length: 1 },
length: 920,
pipes: [],
flowing: null,
ended: false,
endEmitted: false,
reading: true,
constructed: true,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
errorEmitted: false,
emitClose: true,
autoDestroy: true,
destroyed: false,
errored: null,
closed: false,
closeEmitted: false,
defaultEncoding: 'utf8',
awaitDrainWriters: null,
multiAwaitDrain: false,
readingMore: false,
dataEmitted: false,
decoder: null,
encoding: null,
[Symbol(kPaused)]: null
},
_events: [Object: null prototype] {
prefinish: [Function: prefinish],
unpipe: [Function: onunpipe],
error: [ [Function: onerror], [Function (anonymous)] ],
close: [Function: bound onceWrapper] { listener: [Function: onclose] },
finish: [Function: bound onceWrapper] { listener: [Function: onfinish] }
},
_eventsCount: 5,
_maxListeners: undefined,
_writableState: WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
afterWriteTickInfo: null,
buffered: [],
bufferedIndex: 0,
allBuffers: true,
allNoop: true,
pendingcb: 0,
constructed: true,
prefinished: false,
errorEmitted: false,
emitClose: true,
autoDestroy: true,
errored: null,
closed: false,
closeEmitted: false,
[Symbol(kOnFinished)]: []
},
allowHalfOpen: true,
[Symbol(kCapture)]: false,
[Symbol(kCallback)]: null
}

I was expecting resp.body to return the image (as a buffer, likely)

When I dig into the resp.body Passthrough object, I get a buffer in the _readableState.buffer.head.data area... but it seems to only be 1k. ... which makes sense, as this code gives me a 1k image that only has the first few pixels.

How to do ensure the connection provides the entire image?

@devfans
Copy link
Owner

devfans commented Mar 24, 2022

digest-fetch only wraps around fetch/node-fetch, it does not touch the response body, please check the fetch/node-fetch documentation to get the full response data.

@devfans
Copy link
Owner

devfans commented Mar 24, 2022

node-fetch/node-fetch#147 might be useful for you

@jfederer
Copy link
Author

Thank you. I'll investigate through there.

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

No branches or pull requests

2 participants