Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Commit

Permalink
Pull LevelDOWN loader out to non-Browserified module
Browse files Browse the repository at this point in the history
  • Loading branch information
kemitchell committed May 9, 2017
1 parent ed5a542 commit 6bcd959
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 44 deletions.
40 changes: 40 additions & 0 deletions lib/leveldown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var LevelUPError = require('level-errors').LevelUPError
, format = require('util').format
, leveldown

function getLevelDOWN () {
if (leveldown)
return leveldown

var requiredVersion = require('../package.json').devDependencies.leveldown
, leveldownVersion

try {
leveldownVersion = require('leveldown/package.json').version
} catch (e) {
throw requireError(e)
}

if (!require('semver').satisfies(leveldownVersion, requiredVersion)) {
throw new LevelUPError(
'Installed version of LevelDOWN ('
+ leveldownVersion
+ ') does not match required version ('
+ requiredVersion
+ ')'
)
}

try {
return leveldown = require('leveldown')
} catch (e) {
throw requireError(e)
}
}

function requireError (e) {
var template = 'Failed to require LevelDOWN (%s). Try `npm install leveldown` if it\'s missing'
return new LevelUPError(format(template, e.message))
}

module.exports = getLevelDOWN
2 changes: 1 addition & 1 deletion lib/levelup.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var EventEmitter = require('events').EventEmitter

, getOptions = util.getOptions
, defaultOptions = util.defaultOptions
, getLevelDOWN = util.getLevelDOWN
, getLevelDOWN = require('./leveldown')
, dispatchError = util.dispatchError
, isDefined = util.isDefined

Expand Down
40 changes: 0 additions & 40 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
*/

var extend = require('xtend')
, LevelUPError = require('level-errors').LevelUPError
, format = require('util').format
, defaultOptions = {
createIfMissing : true
, errorIfExists : false
Expand All @@ -15,8 +13,6 @@ var extend = require('xtend')
, compression : true
}

, leveldown

function getOptions (options) {
if (typeof options == 'string')
options = { valueEncoding: options }
Expand All @@ -25,41 +21,6 @@ function getOptions (options) {
return options
}

function getLevelDOWN () {
if (leveldown)
return leveldown

var requiredVersion = require('../package.json').devDependencies.leveldown
, leveldownVersion

try {
leveldownVersion = require('leveldown/package.json').version
} catch (e) {
throw requireError(e)
}

if (!require('semver').satisfies(leveldownVersion, requiredVersion)) {
throw new LevelUPError(
'Installed version of LevelDOWN ('
+ leveldownVersion
+ ') does not match required version ('
+ requiredVersion
+ ')'
)
}

try {
return leveldown = require('leveldown')
} catch (e) {
throw requireError(e)
}
}

function requireError (e) {
var template = 'Failed to require LevelDOWN (%s). Try `npm install leveldown` if it\'s missing'
return new LevelUPError(format(template, e.message))
}

function dispatchError (db, error, callback) {
typeof callback == 'function' ? callback(error) : db.emit('error', error)
}
Expand All @@ -71,7 +32,6 @@ function isDefined (v) {
module.exports = {
defaultOptions : defaultOptions
, getOptions : getOptions
, getLevelDOWN : getLevelDOWN
, dispatchError : dispatchError
, isDefined : isDefined
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"tape": "~4.2.1"
},
"browser": {
"./lib/leveldown.js": false,
"leveldown": false,
"leveldown/package": false,
"semver": false
Expand Down
6 changes: 3 additions & 3 deletions test/optional-leveldown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ function clearCache () {
delete require.cache[require.resolve('..')]
delete require.cache[require.resolve('leveldown')]
delete require.cache[require.resolve('leveldown/package')]
delete require.cache[require.resolve('../lib/util')]
delete require.cache[require.resolve('../lib/leveldown')]
}

buster.testCase('Optional LevelDOWN', {
'setUp': clearCache
, 'tearDown': clearCache

, 'test getLevelDOWN()': function () {
var util = require('../lib/util')
assert.same(util.getLevelDOWN(), require('leveldown'), 'correct leveldown provided')
var getLevelDOWN = require('../lib/leveldown')
assert.same(getLevelDOWN(), require('leveldown'), 'correct leveldown provided')
}

, 'test wrong version': function () {
Expand Down

0 comments on commit 6bcd959

Please sign in to comment.