kriszyp / lmdb-js

Simple, efficient, ultra-fast, scalable data store wrapper for LMDB
Other
486 stars 39 forks source link

Support Uint8Array with `encoding: "binary"` and not only Buffers #53

Closed mischnic closed 3 years ago

mischnic commented 3 years ago

It would be great if this worked:

import * as lmdb from "lmdb-store";
let store = lmdb.open("cache", {
   name: "cache",
   encoding: "binary",
});

await store.put("x", new Uint8Array([1,2,3]));
console.log(store.get("x"));

(at least for Uint8Array, since Buffer is compatible with that. Not so sure about other typed arrays)

This is currently prevented by at least this check https://github.com/DoctorEvidence/lmdb-store/blob/33c3101b825750b45d301f4b3db58900943b9a0b/index.js#L508-L509 but it seems to work fine when commenting that line out.

So I'd suggest instead checking value instanceof Uint8Array (or even replacing the value.readUInt16BE since Buffers are subclasses: (Buffer.from("abc") instanceof Uint8Array) === true

kriszyp commented 3 years ago

In case it helps, I published a v1.5.3-pre with the fixes for #51 through #53, but it doesn't have pre-built binaries.

kriszyp commented 3 years ago

Now available in v1.5.3.

mischnic commented 3 years ago

Thanks!