Skip to content

Commit

Permalink
Every browser in our test matrix now supports binary keys
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Apr 9, 2021
1 parent 720aced commit 2c20127
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 35 deletions.
23 changes: 6 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ Being `abstract-leveldown` compliant means you can use many of the [Level module
**If you are upgrading:** please see [UPGRADING.md](UPGRADING.md).

```js
var levelup = require('levelup')
var leveljs = require('level-js')
var db = levelup(leveljs('bigdata'))
const levelup = require('levelup')
const leveljs = require('level-js')
const db = levelup(leveljs('bigdata'))

db.put('hello', Buffer.from('world'), function (err) {
if (err) throw err
Expand All @@ -61,7 +61,7 @@ db.put('hello', Buffer.from('world'), function (err) {
})
```

In ES6 browsers:
With `async/await`:

```js
const levelup = require('levelup')
Expand All @@ -74,13 +74,13 @@ const value = await db.get('hello')

## Browser Support

[![Sauce Test Status](https://saucelabs.com/browser-matrix/level-js.svg)](https://saucelabs.com/u/level-js)
[![Sauce Test Status](https://app.saucelabs.com/browser-matrix/level-js.svg)](https://app.saucelabs.com/u/level-js)

## Type Support

Keys and values can be a string or [`Buffer`][buffer]. Any other type will be irreversibly stringified. The only exceptions are `null` and `undefined`. Keys and values of that type are rejected.

In order to sort string and Buffer keys the same way, for compatibility with `leveldown` and the larger ecosystem, `level-js` internally converts keys and values to binary before passing them to IndexedDB. If binary keys are not supported by the environment (like IE11) `level-js` falls back to `String(key)`.
In order to sort string and Buffer keys the same way, for compatibility with `leveldown` and the larger ecosystem, `level-js` internally converts keys and values to binary before passing them to IndexedDB.

If you desire non-destructive encoding (e.g. to store and retrieve numbers as-is), wrap `level-js` with [`encoding-down`][encoding-down]. Alternatively install [`level`][level] which conveniently bundles [`levelup`][levelup], `level-js` and `encoding-down`. Such an approach is also recommended if you want to achieve universal (isomorphic) behavior. For example, you could have [`leveldown`][leveldown] in a backend and `level-js` in the frontend. The `level` package does exactly that.

Expand Down Expand Up @@ -118,17 +118,6 @@ The optional `options` argument may contain:

See [`IDBFactory#open`](https://developer.mozilla.org/en-US/docs/Web/API/IDBFactory/open) for more details.

## Running Tests

```sh
git clone [email protected]:Level/level-js.git
cd level-js
npm install
npm test
```

It will print out a URL to open in a browser of choice.

## Big Thanks

Cross-browser Testing Platform and Open Source ♥ Provided by [Sauce Labs](https://saucelabs.com).
Expand Down
6 changes: 3 additions & 3 deletions test/custom-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ module.exports = function (leveljs, test, testCommon) {
var db = testCommon.factory()

if (!db.supports.bufferKeys) {
t.pass('environment does not support buffer keys')
t.fail('environment does not support buffer keys')
return t.end()
}

Expand Down Expand Up @@ -165,7 +165,7 @@ module.exports = function (leveljs, test, testCommon) {
var db = testCommon.factory()

if (!db.supports.bufferKeys) {
t.pass('environment does not support buffer keys')
t.fail('environment does not support buffer keys')
return t.end()
}

Expand Down Expand Up @@ -200,7 +200,7 @@ module.exports = function (leveljs, test, testCommon) {
var db = testCommon.factory()

if (!db.supports.bufferKeys) {
t.pass('environment does not support buffer keys')
t.fail('environment does not support buffer keys')
return t.end()
}

Expand Down
21 changes: 6 additions & 15 deletions test/upgrade-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ module.exports = function (leveljs, test, testCommon) {
{ key: -1, value: 'a' },
{ key: '0', value: ab('b') },
{ key: '1', value: 1 },
{ key: maybeBinary('2'), value: new Uint8Array(ab('2')) }
{ key: ab('2'), value: new Uint8Array(ab('2')) }
]

var output = [
{ key: maybeBinary('-1'), value: new Uint8Array(ab('a')) },
{ key: maybeBinary('0'), value: new Uint8Array(ab('b')) },
{ key: maybeBinary('1'), value: new Uint8Array(ab('1')) },
{ key: maybeBinary('2'), value: new Uint8Array(ab('2')) }
{ key: ab('-1'), value: new Uint8Array(ab('a')) },
{ key: ab('0'), value: new Uint8Array(ab('b')) },
{ key: ab('1'), value: new Uint8Array(ab('1')) },
{ key: ab('2'), value: new Uint8Array(ab('2')) }
]

db.open(function (err) {
Expand All @@ -34,12 +34,7 @@ module.exports = function (leveljs, test, testCommon) {
t.ifError(err, 'no concat error')

entries.forEach(function (entry) {
if (db.supports.bufferKeys) {
t.ok(entry.key instanceof ArrayBuffer)
} else {
t.is(typeof entry.key, 'string')
}

t.ok(entry.key instanceof ArrayBuffer)
t.ok(entry.value instanceof Uint8Array)
})

Expand All @@ -50,10 +45,6 @@ module.exports = function (leveljs, test, testCommon) {
})
})

function maybeBinary (key) {
return db.supports.bufferKeys ? ab(key) : String(key)
}

function concatRaw (callback) {
var it = db.iterator()
it._deserializeKey = it._deserializeValue = identity
Expand Down

0 comments on commit 2c20127

Please sign in to comment.