WiseLibs / better-sqlite3

The fastest and simplest library for SQLite3 in Node.js.
MIT License
5.46k stars 396 forks source link

Support a no-Buffer mode #1047

Closed fabiospampinato closed 11 months ago

fabiospampinato commented 1 year ago

Node's Buffer can be somewhat painful to work with sometimes, because they can be passed to functions accepting Uint8Arrays, but while they provide an extension of the same interface they actually work differently in some cases, for example when calling .toString() on them.

As such I try not to deal with Buffers if I can help it, but I see better-sqlite3 defaults to returning me Buffers.

Would it be possible to add some kind of flag that opts into using standard Uint8Arrays instead?

JoshuaWise commented 1 year ago

Interesting... I've used Uint8Arrays for front-end or isomorphic code, but never for node-specific projects. Practically every single core API in Node.js uses Buffers. How do you accomplish anything without using them?

fabiospampinato commented 1 year ago

What do you mean? I can pass a Uint8Array to anything that accepts a Uint8Array, which includes everything I want to pass Uint8Arrays to, like fs.writeFile and whatever else I use support Uint8Arrays. I can't think of a single thing I use that accepts Buffers but not Uint8Arrays.

JoshuaWise commented 1 year ago

Sure, but every core API such as fs.readFile provides a Buffer, with no way of requesting a Uint8Array.

fabiospampinato commented 1 year ago

APIs like fs.readFile don't necessarily return a buffer if you don't need one, like maybe want you want is a string, in which case the problem doesn't exist. If you need a buffer but you don't want a Buffer buffer then you can just convert a Buffer into a Uint8Array.

I'm not exactly sure what your point is, but avoiding Buffer buffers has more pros than cons for me, hence why I'd like to be able to get Uint8Arrays from better-sqlite3.

Prinzhorn commented 1 year ago

Related https://github.com/nodejs/node/issues/41588

mceachen commented 11 months ago

The confusion and support for this switch will be non-trivial. I think we should only consider switching to Uint8Arrays if Node itself makes the decision to make this large switch.