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
Should we remove sync/async APIs, & Offer only a single API that returns a Promise? #142
Comments
While it'd be nice to remove the callbacks for a Promise API, I think taking out the synchronous API would be a bad move. If they already have the image in a Buffer, there's no reason to be asynchronous. What you could do to avoid the linked problem is to make the interface a bit more explicit const imageSize = require('image-size');
// Always returns a Promise that'll get the size of the image from that file (or reject).
imageSize.fromFile('./myImage.png');
const myImageInABuffer = require('fs').readFileSync('./myImage.png');
// Always returns the size of the image from that buffer (or throws).
imageSize.fromBuffer(myImageInABuffer); Alternatively, you could limit the scope of the project to just the synchronous method and make getting the Buffer the responsibility of the user, as they currently have to do with images taken over HTTP. const imageSize = require('image-size');
getImageFromDisk().then(buffer => imageSize(buffer));
getImageFromInternet().then(buffer => imageSize(buffer));
getImageFromCanvas().then(buffer => imageSize(buffer)); |
Moving to v1.0 is clearly a big step :-) As it is a major version, breaking some legacy API compatibility is technically allowed by semver but does it worth giving pain in the a... ? I would not remove the sync version for now Wide usage of it is also for writing CLI tools for which it is often handy (even if not optimized) to be able to call the APIs synchronously (some grunt plugins are sync code dependent). At least I would either:
Regarding the async API It is very easy & light to support both the callback & promise APIs. The only issue is if we want to support it with the current API signatures. The polymorphic approach cannot choose to treat When I first started to look at This could give:
with
Advantages
|
I like the idea of a Onion Architecture The idea should be that it should have as little knowledge about a system environment as possible. Deno has a security layer where 3th party modules are sandboxed and have no way to access to the File IO, or the network Then the next layer should be platform specifics, Reading files from the fileSystem, urls, sockets, or whatever. This could be extracted out as a separate file, But overall i think it's better if just the users/developers deals with this instead // Where a web developer could simply do:
const {width, height} = await blob.slice(0, 1024).arrayBuffer().then(sizeOf)
// cross platform way of doing stuff
const {width, height} = sizeOf(arrayBuffer) You can also have a utility file that isn't imported directly. Developers would have to import it manually import { sizeFromFs } from 'image-size/from-node-path.js' |
This might help with avoiding issues like #132
The text was updated successfully, but these errors were encountered: