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

Buffer with callback doesn't work #132

Open
phawxby opened this issue Nov 26, 2018 · 3 comments
Open

Buffer with callback doesn't work #132

phawxby opened this issue Nov 26, 2018 · 3 comments

Comments

@phawxby
Copy link

phawxby commented Nov 26, 2018

The codepath for input strings allows for the callback to be invoked.
https://github.com/image-size/image-size/blob/master/lib/index.js#L118

But the codepath for buffers does not, it's always a return breaking the promise.
https://github.com/image-size/image-size/blob/master/lib/index.js#L96

@netroy
Copy link
Member

netroy commented Nov 28, 2018

@phawxby operating on a buffer, in this case, isn't an async operation.
Can you please share more info on how this is being used in an async way?

@phawxby
Copy link
Author

phawxby commented Nov 28, 2018

const got = require('got');
const { promisify } = require('util');
const sizeOf = promisify(require('image-size'));

const resp = await got('https://www.foo.com/foo.png', { encoding: null });
const dimensions = await sizeOf(resp.body); // resp.body is a buffer when the encoding is null

I've fixed it for now by not promisifying sizeOf but it is breaking the terms of the callback/promise if there is a codepath which can result in it not being invoked.

@BrendanFDMoore
Copy link

BrendanFDMoore commented Apr 4, 2019

Personally I'd prefer a consistent await-able function, but I can see the value in supporting non-async Buffers as an option.

Could the promisify be handled internally? ie, always return a Promise, but if a Buffer is detected, do the processing in the initial Promise setup before returning, then just resolve with the value immediately.

edit: meant to comment on the proposal issue, not this one. Oops. Sorry.

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

3 participants