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

PUT padding small binary files #725

Closed
jessica-pixvana opened this issue Feb 23, 2017 · 2 comments
Closed

PUT padding small binary files #725

jessica-pixvana opened this issue Feb 23, 2017 · 2 comments

Comments

@jessica-pixvana
Copy link

jessica-pixvana commented Feb 23, 2017

I'm attempting to use axios to do a PUT to a url. I read the file off disk using fs.readFileSync into a buffer. I have confirmed that the file read in is the size I expect (908 bytes). However the uploaded file is 8192 bytes, and a hexdump of the uploaded file shows it is padded with 0s. I have confirmed that at the time xhr receives the data it has already been padded. Oddly this does not appear to happen with larger files over 8192.

export function upload(url, filepath) {
	const config = {
		headers: {
			'Content-Type': mime.lookup(filepath)
		}
	};
	const fileBuffer = fs.readFileSync(filepath);
	return axios.put(url, fileBuffer, config);
}
@jessica-pixvana jessica-pixvana changed the title PUT padding small files PUT padding small binaries files Feb 23, 2017
@jessica-pixvana jessica-pixvana changed the title PUT padding small binaries files PUT padding small binary files Feb 23, 2017
@jerfowler
Copy link
Contributor

It's not just PUT, I can confirm POST does it as well. Looks like an issue when using axios from newer versions of Node and passing a buffer to axios that is less than the size of the Buffer.poolSize, which is typically 8192 bytes. If the buffer will fit in the pool, node doesn't allocate its own memory. What axios's node adapter appears to be doing is copying the entire 8192 bytes and sending the entire internal Buffer pool rather than just the file data. That is why the Content Size show 8192 and the data payload will contain random data.

https://nodejs.org/api/buffer.html#buffer_class_method_buffer_allocunsafe_size

Use of this pre-allocated internal memory pool is a key difference between calling Buffer.alloc(size, fill) vs. Buffer.allocUnsafe(size).fill(fill). Specifically, Buffer.alloc(size, fill) will never use the internal Buffer pool, while Buffer.allocUnsafe(size).fill(fill) will use the internal Buffer pool if size is less than or equal to half Buffer.poolSize. The difference is subtle but can be important when an application requires the additional performance that Buffer.allocUnsafe() provides.

@rubennorte
Copy link
Member

Closing as we'll track this in #774 that has more information. Thanks for reporting!

@axios axios locked and limited conversation to collaborators May 22, 2020
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

3 participants