@seangenabe/fastify-bankai-hook
Version:
Fastify plugin for bankai v9
40 lines (39 loc) • 1.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const bankai = require("bankai/http");
const fp = require("fastify-plugin");
function _fastifyBankaiHook(fastify, o, next) {
const { entry, opts = {} } = o;
if (entry == null) {
throw new Error('entry is not defined');
}
if (opts.quiet === undefined) {
opts.quiet = true;
}
const handler = bankai(entry, opts);
const { compiler } = handler;
fastify.get('/*', (request, reply) => {
handler(request.raw, reply.res, () => {
reply
.code(404)
.type('text/plain')
.send('not found');
});
});
fastify.addHook('onClose', (f, done) => {
compiler.close();
setImmediate(done);
});
compiler.on('error', (nodeName, edgeName, err) => {
fastify.log.error(err.message);
});
compiler.once('change', () => next( /* err = undefined */));
}
function FastifyBankaiHook() {
const plugin = fp(_fastifyBankaiHook, {
fastify: '>= 0.40.0'
});
_fastifyBankaiHook[Symbol.for('skip-override')] = false;
return plugin;
}
exports.default = FastifyBankaiHook;