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

Read blob in nodejs #437

Closed
jimmywarting opened this issue Feb 26, 2021 · 2 comments · Fixed by #458
Closed

Read blob in nodejs #437

jimmywarting opened this issue Feb 26, 2021 · 2 comments · Fixed by #458

Comments

@jimmywarting
Copy link
Contributor

jimmywarting commented Feb 26, 2021

Nodejs have introduced buffer.Blob and we are about to get them from fs soon also

but they have not introduced the FileReader and likely never will. cuz there are some new file reading methods on the blob now with .stream(), .arrayBuffer(), and .text() that can return promises
so i was thinking maybe you could replace this hole function and just use blob.arrayBuffer() instead and maybe polyfill blob.prototype.arrayBuffer() if it dose not exist

file-type/browser.js

Lines 18 to 45 in 2cc0869

/**
Convert Web API File to Node Buffer.
@param {Blob} blob - Web API Blob.
@returns {Promise<Buffer>}
*/
function convertBlobToBuffer(blob) {
return new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.addEventListener('loadend', event => {
let data = event.target.result;
if (data instanceof ArrayBuffer) {
data = toBuffer(new Uint8Array(event.target.result));
}
resolve(data);
});
fileReader.addEventListener('error', event => {
reject(new Error(event.message));
});
fileReader.addEventListener('abort', event => {
reject(new Error(event.type));
});
fileReader.readAsArrayBuffer(blob);
});
}

I have also another concern, is it really necessary to read the hole blob into memory?
isn't the first and last 1000 bytes or something only needed?

it would seem more efficient to do something like:

chunk = new Blob([
  blob.slice(0, 4500),
  blob.slice(-4500)
]).arrayBuffer()

I would aslo suggest that you use Uint8Array, ArrayBuffer and DataView instead of using nodes Buffer as that would lower the browser bundle and not drag in any of nodes core modules.

I would also suggest that you make your fromStream method acceptable of a AsyncIterator that yields Uint8Arrays instead of relying on either node streams or whatwg streams or nodes EventEmitter since it could lower the bundle even a lot further
since node stream are already async iterable. and whatwg streams are too but no browser have implemented it.

@jimmywarting
Copy link
Contributor Author

Hmm, i quite like what you are doing but i would like a un-opinionated layer of the magic number - kind of how mime-db is to mime. mime-db is just pure JSON and dose not contain any logic to remain as un-opinionated as possible. and mime adds a api layer on top of it.

Not saying you have to go and make some new npm module called magic-db or something - just a json file within your project would also be enough so one could do require('file-type/magic.json') or something - that would be nice 🙂

@Borewit
Copy link
Collaborator

Borewit commented Feb 27, 2021

I would also suggest that you use Uint8Array, ArrayBuffer and DataView instead of using nodes Buffer as that would lower the browser bundle and not drag in any of nodes core modules.

As you also correctly indicated, at the very beginning in the dependency tree: Borewit/token-types#245, I propose to investigate the suggestion starting from there.

I would also suggest that you make your fromStream method acceptable of a AsyncIterator that yields Uint8Arrays instead of relying on either node streams or whatwg streams or nodes EventEmitter since it could lower the bundle even a lot further
since node stream are already async iterable. and whatwg streams are too but no browser have implemented it.

Sounds interesting as well.

just a json file within your project would also be enough so one could do require('file-type/magic.json')

That would suite those file type detections based on simple magic number.

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

Successfully merging a pull request may close this issue.

2 participants