autolevel.js

Automatically use the right abstract-leveldown module for your configuration
git clone git://git.finwo.net/lib/autolevel.js
Log | Files | Refs | README | LICENSE

commit f0505c3d63d112fd7b9302b06126295fe41ead79
parent 855eba96f4f6e78b00e5b885d751a14df87e5d14
Author: finwo <finwo@pm.me>
Date:   Fri,  2 Nov 2018 11:03:15 +0100

Better plain path detection

Diffstat:
Msrc/plain.js | 19+++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/src/plain.js b/src/plain.js @@ -1,14 +1,29 @@ +const path = require('path'); + module.exports = function (hasModule, supported) { + const basedir = global.approot || (''+require('app-root-path')); + + // Turn the parsed location into an absolute path + function toAbsolutePath( parsedLocation ) { + if ( 'string' === typeof parsedLocation ) { + parsedLocation = require('url-parse')(parsedLocation); + } + let targetLocation = parsedLocation.hostname + parsedLocation.pathname; + if ( targetLocation.substr(0,1) !== '/' ) { + targetLocation = basedir + '/' + targetLocation; + } + return targetLocation.replace(/\//g,path.sep); + } if (hasModule('level')) { // Directly use level supported.dir = function (parsedLocation, options, callback) { - return require('level')(parsedLocation.href || parsedLocation, options, callback); + return require('level')(toAbsolutePath(parsedLocation), options, callback); }; } else if (hasModule('levelup') && hasModule('leveldown')) { // Combine levelup + leveldown supported.dir = function (parsedLocation, options, callback) { - return require('levelup')(require('leveldown')(parsedLocation.href || parsedLocation), options, callback); + return require('levelup')(require('leveldown')(toAbsolutePath(parsedLocation), options, callback); }; } else { // Can't load this