36 lines
783 B
JavaScript
36 lines
783 B
JavaScript
|
'use strict'
|
||
|
|
||
|
const fp = require('fastify-plugin')
|
||
|
const levelup = require('levelup')
|
||
|
const leveldown = require('leveldown')
|
||
|
const encode = require('encoding-down')
|
||
|
|
||
|
// mostly from level-packager
|
||
|
const levelMore = (location) => {
|
||
|
;['destroy', 'repair'].forEach(function (m) {
|
||
|
if (typeof leveldown[m] === 'function') {
|
||
|
levelMore[m] = () => leveldown[m].apply(leveldown, arguments)
|
||
|
}
|
||
|
})
|
||
|
|
||
|
return levelup(encode(leveldown(location), { }), { })
|
||
|
}
|
||
|
|
||
|
levelMore.errors = levelup.errors
|
||
|
|
||
|
function levelPlugin (fastify, opt, next) {
|
||
|
fastify
|
||
|
.decorate('lauth', levelMore('auth'))
|
||
|
.addHook('onClose', close)
|
||
|
|
||
|
next()
|
||
|
}
|
||
|
|
||
|
function close (fastify, done) {
|
||
|
fastify.level.close(done)
|
||
|
}
|
||
|
|
||
|
module.exports = fp(levelPlugin, {
|
||
|
fastify: '>=1.0.0',
|
||
|
name: 'level_auth'
|
||
|
});
|