Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ArrayBuffer in favor of Buffer #85

Merged
merged 1 commit into from
May 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ level.js uses [IDBWrapper](https://github.com/jensarps/IDBWrapper) by jensarps t

Here are the goals of this level.js:

- Store large amounts of ascii (strings, JSON) and binary (ArrayBuffers, Typed Arrays) data in modern browsers
- Store large amounts of ascii (strings, JSON) and binary (Buffer) data in modern browsers
- Be as fast as possible
- Use the leveldown test suite and sync with [multilevel](https://github.com/juliangruber/multilevel) over either ascii or binary transports (websockets and xhr both have ascii/binary modes in browsers now)

Expand Down Expand Up @@ -44,8 +44,6 @@ The test suite for this library is in the [abstract-leveldown](https://github.co

For more code examples see the [abstract-leveldown test suite](https://github.com/rvagg/node-abstract-leveldown/tree/master/abstract)

The only differences between this and leveldown is that you can store `ArrayBuffers` in this (whereas leveldown just uses node `Buffer` objects)

## run the tests

```sh
Expand Down
29 changes: 4 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var IDB = require('idb-wrapper')
var AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
var util = require('util')
var Iterator = require('./iterator')
var isBuffer = require('isbuffer')
var xtend = require('xtend')
var toBuffer = require('typedarray-to-buffer')

Expand Down Expand Up @@ -80,15 +79,12 @@ Level.prototype._put = function (key, value, options, callback) {
// - Number, except NaN. Includes Infinity and -Infinity
// - Date, except invalid (NaN)
// - String
// - ArrayBuffer or a view thereof (typed arrays)
// - ArrayBuffer or a view thereof (typed arrays). In level-js we only support
// Buffer (which is an Uint8Array).
// - Array, except cyclical and empty (e.g. Array(10)). Elements must be valid
// types themselves.
Level.prototype._serializeKey = function (key) {
// TODO: do we still need to support ArrayBuffer?
if (key instanceof ArrayBuffer) {
key = Buffer.from(key)
return Level.binaryKeys ? key : key.toString()
} else if (Buffer.isBuffer(key)) {
if (Buffer.isBuffer(key)) {
return Level.binaryKeys ? key : key.toString()
} else if (Array.isArray(key)) {
return key.map(this._serializeKey, this)
Expand All @@ -100,11 +96,7 @@ Level.prototype._serializeKey = function (key) {
}

Level.prototype._serializeValue = function (value) {
// TODO: do we still need to support ArrayBuffer?
if (value instanceof ArrayBuffer) return Buffer.from(value)
if (value == null) return ''

return value
return value == null ? '' : value
}

Level.prototype._iterator = function (options) {
Expand Down Expand Up @@ -159,16 +151,3 @@ Level.destroy = function (db, callback) {
callback(err)
}
}

var checkKeyValue = Level.prototype._checkKeyValue = function (obj, type) {
Copy link
Member Author

@vweevers vweevers May 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method was dead (it's now called _checkKey in abstract-leveldown), but without the need for ArrayBuffer, the default _checkKey works fine. Except for Level/abstract-leveldown#230, but that's hardly a priority.

if (obj === null || obj === undefined)
return new Error(type + ' cannot be `null` or `undefined`')
if (obj === null || obj === undefined)
return new Error(type + ' cannot be `null` or `undefined`')
if (isBuffer(obj) && obj.byteLength === 0)
return new Error(type + ' cannot be an empty ArrayBuffer')
if (String(obj) === '')
return new Error(type + ' cannot be an empty String')
if (obj.length === 0)
return new Error(type + ' cannot be an empty Array')
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"dependencies": {
"abstract-leveldown": "~5.0.0",
"idb-wrapper": "^1.5.0",
"isbuffer": "~0.0.0",
"ltgt": "^2.1.2",
"typedarray-to-buffer": "~3.1.5",
"xtend": "~4.0.1"
Expand Down