Skip to content

Use bufferWrap to support ArrayBuffer, Uint8Array and Buffer #3

Open
@CMCDragonkai

Description

@CMCDragonkai

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

  1. - Investigate ArrayBuffer compatibility in leveldb
  2. - Investigate to what extent can we enable compatibility with ArrayBuffer in DB without losing Buffer support
  3. - Replace all uses of string | Buffer with string | ArrayBuffer for keys, but retain Buffer on returned output.
  4. - Change this.crypto.key to be assumed to be ArrayBuffer
  5. - Change toArrayBuffer to an explicit slice copy of all possible instances of ArrayBuffer type
  6. - The fromArrayBuffer is still useful since we return Buffer.
  7. - Change serialize and deserialize to use ArrayBuffer and TextEncoder and TextDecoder which can do the utf-8 encoding/decoding.
  8. - Check if PK is using any of the utility functions here, some uses of deserialize and serialize might need to change.
  9. - Ensure that most signatures are still compatible
  10. - Update all tests to include Uint8Array and ArrayBuffer usage, and ensure that slice-copying is working.
  11. - Check that there's no performance regression on the benchmarks

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions