From 6bcd95927dce29575112dbc76038fdb2cdef4d36 Mon Sep 17 00:00:00 2001 From: "Kyle E. Mitchell" Date: Fri, 5 May 2017 09:46:24 -0700 Subject: [PATCH] Pull LevelDOWN loader out to non-Browserified module --- lib/leveldown.js | 40 +++++++++++++++++++++++++++++++++ lib/levelup.js | 2 +- lib/util.js | 40 --------------------------------- package.json | 1 + test/optional-leveldown-test.js | 6 ++--- 5 files changed, 45 insertions(+), 44 deletions(-) create mode 100644 lib/leveldown.js diff --git a/lib/leveldown.js b/lib/leveldown.js new file mode 100644 index 00000000..e32aa1ee --- /dev/null +++ b/lib/leveldown.js @@ -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 diff --git a/lib/levelup.js b/lib/levelup.js index 6664cdff..66f1c564 100644 --- a/lib/levelup.js +++ b/lib/levelup.js @@ -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 diff --git a/lib/util.js b/lib/util.js index d5282a46..900c2f0b 100644 --- a/lib/util.js +++ b/lib/util.js @@ -5,8 +5,6 @@ */ var extend = require('xtend') - , LevelUPError = require('level-errors').LevelUPError - , format = require('util').format , defaultOptions = { createIfMissing : true , errorIfExists : false @@ -15,8 +13,6 @@ var extend = require('xtend') , compression : true } - , leveldown - function getOptions (options) { if (typeof options == 'string') options = { valueEncoding: options } @@ -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) } @@ -71,7 +32,6 @@ function isDefined (v) { module.exports = { defaultOptions : defaultOptions , getOptions : getOptions - , getLevelDOWN : getLevelDOWN , dispatchError : dispatchError , isDefined : isDefined } diff --git a/package.json b/package.json index 643270f9..dbd0ecae 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,7 @@ "tape": "~4.2.1" }, "browser": { + "./lib/leveldown.js": false, "leveldown": false, "leveldown/package": false, "semver": false diff --git a/test/optional-leveldown-test.js b/test/optional-leveldown-test.js index 6336a502..026cb0a5 100644 --- a/test/optional-leveldown-test.js +++ b/test/optional-leveldown-test.js @@ -14,7 +14,7 @@ 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', { @@ -22,8 +22,8 @@ buster.testCase('Optional LevelDOWN', { , '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 () {