@webtask/middleware-compiler
Advanced tools
Comparing version 1.5.0 to 1.5.1
{ | ||
"name": "@webtask/middleware-compiler", | ||
"version": "1.5.0", | ||
"version": "1.5.1", | ||
"description": "Webtask compiler that provides a configurable middleware pipeline", | ||
@@ -10,3 +10,3 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "lab -cvL -t 90" | ||
"test": "lab -cvL -t 90 --leaks" | ||
}, | ||
@@ -20,7 +20,8 @@ "keywords": [], | ||
"devDependencies": { | ||
"@webtask/bearer-auth-middleware": "^1.5.0", | ||
"@webtask/bearer-auth-middleware": "^1.5.1", | ||
"body-parser": "^1.17.2", | ||
"lab": "^14.2.0", | ||
"shot": "^3.4.2" | ||
} | ||
}, | ||
"gitHead": "d56b7c111f2a0036bb2bae75d65e15c02fadff81" | ||
} |
@@ -462,2 +462,222 @@ 'use strict'; | ||
it( | ||
'succeeds in running a webtask with a single middleware that reassigns the script to a custom function', | ||
(done, onCleanup) => { | ||
const script = `module.exports = cb => cb(null, 'This function will be reassigned');`; | ||
// Any time the script is reassigned, `req.webtaskContext.compiler.nodejsCompiler` must be decorated with optional compilation. | ||
// For more context, see https://github.com/auth0/webtask-userspace/blob/f337550a007bf10d3f2d71395822501008c152dc/packages/middleware-compiler/index.js#L48-L62 | ||
// and https://github.com/auth0/webtask-userspace/blob/f337550a007bf10d3f2d71395822501008c152dc/packages/middleware-compiler/lib/middleware.js#L36-L39 | ||
const middleware = ` | ||
module.exports = () => (req, res, next) => { | ||
const defaultCompiler = req.webtaskContext.compiler.nodejsCompiler; | ||
const optionallyCompile = (s, cb) => typeof s === 'function' ? cb(null, s) : defaultCompiler(s, cb); | ||
req.webtaskContext.compiler.nodejsCompiler = optionallyCompile; | ||
req.webtaskContext.compiler.script = cb => cb(null, 'OK'); | ||
return next(); | ||
} | ||
`; | ||
return Helpers.spawnServer(middleware, (error, server) => { | ||
Assert.ifError(error); | ||
onCleanup(server.stop); | ||
const middlewareSpecs = [server.baseUrl]; | ||
return createPipeline( | ||
{ script, middlewareSpecs }, | ||
(error, webtaskFn) => { | ||
Assert.ifError(error); | ||
return invokePipeline( | ||
webtaskFn, | ||
{}, | ||
res => { | ||
Assert.equal(res.statusCode, 200); | ||
Assert.equal(res.payload, '"OK"'); | ||
done(); | ||
} | ||
); | ||
} | ||
); | ||
}); | ||
} | ||
); | ||
it( | ||
'succeeds in running a webtask with compiler middleware and custom programming model', | ||
(done, onCleanup) => { | ||
const script = `module.exports = async (context) => 'OK';`; | ||
const middleware = ` | ||
module.exports = () => (req, res, next) => { | ||
const defaultCompiler = req.webtaskContext.compiler.nodejsCompiler; | ||
const optionallyCompile = (s, cb) => typeof s === 'function' ? cb(null, s) : defaultCompiler(s, cb); | ||
req.webtaskContext.compiler.nodejsCompiler = optionallyCompile; | ||
return req.webtaskContext.compiler.nodejsCompiler(req.webtaskContext.compiler.script, function(compilerError, code) { | ||
if (compilerError) return next(compilerError); | ||
const webtask = require('util').callbackify(code); | ||
req.webtaskContext.compiler.script = (context, cb) => webtask(context, cb); | ||
return next(); | ||
}); | ||
}; | ||
`; | ||
return Helpers.spawnServer(middleware, (error, server) => { | ||
Assert.ifError(error); | ||
onCleanup(server.stop); | ||
const middlewareSpecs = [server.baseUrl]; | ||
return createPipeline( | ||
{ script, middlewareSpecs }, | ||
(error, webtaskFn) => { | ||
Assert.ifError(error); | ||
return invokePipeline( | ||
webtaskFn, | ||
{}, | ||
res => { | ||
Assert.equal(res.statusCode, 200); | ||
Assert.equal(res.payload, '"OK"'); | ||
done(); | ||
} | ||
); | ||
} | ||
); | ||
}); | ||
} | ||
); | ||
it( | ||
'succeeds in running a webtask with a compiler followed by multiple middleware functions', | ||
(done, onCleanup) => { | ||
const script = `module.exports = cb => cb(null, 'OK');`; | ||
const middleware = (req, res) => { | ||
res.writeHead(200, {'Content-Type': 'application/javascript'}); | ||
const router = { | ||
'/compiler': ` | ||
module.exports = () => (req, res, next) => { | ||
const nodejsCompiler = req.webtaskContext.compiler.nodejsCompiler; | ||
const optionallyCompile = (s, cb) => typeof s === 'function' ? cb(null, s) : nodejsCompiler(s, cb); | ||
req.webtaskContext.compiler.nodejsCompiler = optionallyCompile; | ||
return req.webtaskContext.compiler.nodejsCompiler(req.webtaskContext.compiler.script, function(compilerError, webtask) { | ||
if (compilerError) return next(compilerError); | ||
req.webtaskContext.compiler.script = webtask; | ||
return next(); | ||
}); | ||
}; | ||
`, | ||
'/auth': ` | ||
module.exports = () => (req, res, next) => { | ||
const isAuthorized = true; | ||
if (!isAuthorized) return next(new Error('Not autorized!')); | ||
return next(); | ||
}; | ||
`, | ||
'/noop': `module.exports = () => (req, res, next) => next();` | ||
}; | ||
res.end(router[req.url]); | ||
}; | ||
return Helpers.spawnServer(middleware, (error, server) => { | ||
Assert.ifError(error); | ||
onCleanup(server.stop); | ||
const middlewareSpecs = [ | ||
`${server.baseUrl}/compiler`, | ||
`${server.baseUrl}/auth`, | ||
`${server.baseUrl}/noop`, | ||
]; | ||
return createPipeline( | ||
{ script, middlewareSpecs }, | ||
(error, webtaskFn) => { | ||
Assert.ifError(error); | ||
return invokePipeline( | ||
webtaskFn, | ||
{}, | ||
res => { | ||
Assert.equal(res.statusCode, 200); | ||
Assert.equal(res.payload, '"OK"'); | ||
done(); | ||
} | ||
); | ||
} | ||
); | ||
}); | ||
} | ||
); | ||
it( | ||
'succeeds in running a webtask with composed compilers', | ||
(done, onCleanup) => { | ||
const script = `module.exports = cb => cb(null, 'OK');`; | ||
const middleware = (req, res) => { | ||
res.writeHead(200, {'Content-Type': 'application/javascript'}); | ||
const router = { | ||
'/compose': ` | ||
module.exports = () => (req, res, next) => { | ||
const nodejsCompiler = req.webtaskContext.compiler.nodejsCompiler; | ||
const optionallyCompile = (s, cb) => typeof s === 'function' ? cb(null, s) : nodejsCompiler(s, cb); | ||
req.webtaskContext.compiler.nodejsCompiler = optionallyCompile; | ||
return next(); | ||
} | ||
`, | ||
'/promisify': ` | ||
module.exports = () => (req, res, next) => { | ||
return req.webtaskContext.compiler.nodejsCompiler(req.webtaskContext.compiler.script, function(compilerError, webtask) { | ||
if (compilerError) return next(compilerError); | ||
req.webtaskContext.compiler.script = require('util').promisify(webtask); | ||
return next(); | ||
}); | ||
}; | ||
`, | ||
'/callbackify': ` | ||
module.exports = () => (req, res, next) => { | ||
return req.webtaskContext.compiler.nodejsCompiler(req.webtaskContext.compiler.script, function(compilerError, code) { | ||
if (compilerError) return next(compilerError); | ||
const webtask = require('util').callbackify(code); | ||
req.webtaskContext.compiler.script = (cb) => webtask(cb); | ||
return next(); | ||
}); | ||
}; | ||
` | ||
}; | ||
res.end(router[req.url]); | ||
}; | ||
return Helpers.spawnServer(middleware, (error, server) => { | ||
Assert.ifError(error); | ||
onCleanup(server.stop); | ||
const middlewareSpecs = [ | ||
`${server.baseUrl}/compose`, | ||
`${server.baseUrl}/promisify`, | ||
`${server.baseUrl}/callbackify`, | ||
]; | ||
return createPipeline( | ||
{ script, middlewareSpecs }, | ||
(error, webtaskFn) => { | ||
Assert.ifError(error); | ||
return invokePipeline( | ||
webtaskFn, | ||
{}, | ||
res => { | ||
Assert.equal(res.statusCode, 200); | ||
done(); | ||
} | ||
); | ||
} | ||
); | ||
}); | ||
} | ||
); | ||
it( | ||
'succeeds in running a webtask with a url-based middleware that intercepts on the response', | ||
@@ -464,0 +684,0 @@ (done, onCleanup) => { |
57913
1296