Description
Specification
The ArrayBuffer
type is more generalised in the JS ecosystem. The DB right now focuses on taking Node Buffers.
Node buffers can be converted to ArrayBuffer
easily, and ArrayBuffer
can be wrapped in Node buffers.
According to this comment: Level/level-js#34 (comment) it is possible to use ArrayBuffers directly in leveldb, it just not documented.
It says that the asBuffer
has to be turned false.
I'm not sure how this would work with keyEncoding
and valueEncoding
that we have set to 'binary'
.
Our raw
is also specifying that we would get Buffer
, would that mean we instead say that we return ArrayBuffer
instead, and with keys it would be string | ArrayBuffer
?
Furthermore this would impact js-encryptedfs, so it's worthwhile to explore the implications of this.
The primary reason to do this would be cross-platform compatibility for mobile platforms that may not have the Node buffer.
The alternative would be use https://github.com/feross/buffer everywhere as a dependency so that way everything just uses the buffer
library. This will mean that any usage of import { Buffer } from 'buffer';
will be resolved by feross/buffer first though, so one should beware of that.
Note that all Node Buffer
is Uint8Array
which is ArrayBuffer
.
If something supports ArrayBuffer
, they would support Uint8Array
and Buffer
at the same time.
One benefit would be integration of js-id MatrixAI/js-id#1 can be simplified since Id
as Uint8Array
can be stored directly in the DB without first wrapping them as Buffer
.
Additional context
Tasks
- - Investigate
ArrayBuffer
compatibility in leveldb - - Investigate to what extent can we enable compatibility with
ArrayBuffer
in DB without losingBuffer
support - - Replace all uses of
string | Buffer
withstring | ArrayBuffer
for keys, but retainBuffer
on returned output.- fix
domainPath
with UsebufferWrap
to supportArrayBuffer
,Uint8Array
andBuffer
#3 (comment) - this incorporates the Robustness principle: https://en.wikipedia.org/wiki/Robustness_principle
- fix
- - Change
this.crypto.key
to be assumed to beArrayBuffer
- - Change
toArrayBuffer
to an explicit slice copy of all possible instances ofArrayBuffer
type - - The
fromArrayBuffer
is still useful since we returnBuffer
. - - Change
serialize
anddeserialize
to useArrayBuffer
andTextEncoder
andTextDecoder
which can do theutf-8
encoding/decoding. - - Check if PK is using any of the utility functions here, some uses of
deserialize
andserialize
might need to change. - - Ensure that most signatures are still compatible
- - Update all tests to include
Uint8Array
andArrayBuffer
usage, and ensure that slice-copying is working. - - Check that there's no performance regression on the benchmarks