2016-09-21 00:03:26 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const nunjucks = require('nunjucks');
|
|
|
|
const traverse = require('traverse');
|
|
|
|
const uuid = require('node-uuid');
|
|
|
|
const db = require('./database');
|
|
|
|
const {TYPE_WORKSPACE} = require('./database/index');
|
|
|
|
const {getBasicAuthHeader, hasAuthHeader, setDefaultProtocol} = require('./util');
|
2016-04-15 02:13:49 +00:00
|
|
|
|
2016-09-13 00:05:04 +00:00
|
|
|
const nunjucksEnvironment = nunjucks.configure({
|
2016-07-20 21:15:11 +00:00
|
|
|
autoescape: false
|
|
|
|
});
|
2016-04-10 02:58:48 +00:00
|
|
|
|
2016-09-13 00:05:04 +00:00
|
|
|
class NoArgsExtension {
|
|
|
|
parse (parser, nodes, lexer) {
|
|
|
|
const args = parser.parseSignature(null, true);
|
|
|
|
parser.skip(lexer.TOKEN_BLOCK_END);
|
|
|
|
return new nodes.CallExtension(this, 'run', args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ArgsExtension {
|
|
|
|
parse (parser, nodes, lexer) {
|
|
|
|
const tok = parser.nextToken();
|
|
|
|
const args = parser.parseSignature(null, true);
|
|
|
|
parser.advanceAfterBlockEnd(tok.value);
|
|
|
|
return new nodes.CallExtension(this, 'run', args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class TimestampExtension extends NoArgsExtension {
|
|
|
|
constructor () {
|
|
|
|
super();
|
|
|
|
this.tags = ['timestamp'];
|
|
|
|
}
|
|
|
|
|
|
|
|
run (context) {
|
|
|
|
return Date.now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class UuidExtension extends NoArgsExtension {
|
|
|
|
constructor () {
|
|
|
|
super();
|
|
|
|
this.tags = ['uuid'];
|
|
|
|
}
|
|
|
|
|
|
|
|
run (context) {
|
|
|
|
return uuid.v4();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nunjucksEnvironment.addExtension('uuid', new UuidExtension());
|
|
|
|
nunjucksEnvironment.addExtension('timestamp', new TimestampExtension());
|
|
|
|
|
2016-09-21 00:03:26 +00:00
|
|
|
module.exports.render = (template, context = {}) => {
|
2016-09-03 04:32:45 +00:00
|
|
|
try {
|
2016-09-13 00:05:04 +00:00
|
|
|
return nunjucksEnvironment.renderString(template, context);
|
2016-09-03 04:32:45 +00:00
|
|
|
} catch (e) {
|
|
|
|
throw new Error(e.message.replace(/\(unknown path\)\s*/, ''));
|
|
|
|
}
|
2016-09-21 00:03:26 +00:00
|
|
|
};
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2016-09-21 00:03:26 +00:00
|
|
|
module.exports.buildRenderContext = (ancestors, rootEnvironment, subEnvironment) => {
|
2016-09-03 04:32:45 +00:00
|
|
|
const renderContext = {};
|
|
|
|
|
|
|
|
if (rootEnvironment) {
|
|
|
|
Object.assign(renderContext, rootEnvironment.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (subEnvironment) {
|
|
|
|
Object.assign(renderContext, subEnvironment.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(ancestors)) {
|
|
|
|
ancestors = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let doc of ancestors) {
|
|
|
|
if (!doc.environment) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.assign(renderContext, doc.environment);
|
|
|
|
}
|
|
|
|
|
2016-09-12 21:16:55 +00:00
|
|
|
// Now we're going to render the renderContext with itself.
|
|
|
|
// This is to support templating inside environments
|
|
|
|
const stringifiedEnvironment = JSON.stringify(renderContext);
|
|
|
|
return JSON.parse(
|
2016-09-21 00:03:26 +00:00
|
|
|
module.exports.render(stringifiedEnvironment, renderContext)
|
2016-09-12 21:16:55 +00:00
|
|
|
);
|
2016-09-21 00:03:26 +00:00
|
|
|
};
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2016-09-21 00:03:26 +00:00
|
|
|
module.exports.recursiveRender = (obj, context) => {
|
2016-09-03 04:32:45 +00:00
|
|
|
// Make a copy so no one gets mad :)
|
2016-09-06 16:14:48 +00:00
|
|
|
const newObj = traverse.clone(obj);
|
2016-09-03 04:32:45 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
traverse(newObj).forEach(function (x) {
|
|
|
|
if (typeof x === 'string') {
|
2016-09-21 00:03:26 +00:00
|
|
|
const str = module.exports.render(x, context);
|
2016-09-03 04:32:45 +00:00
|
|
|
this.update(str);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
// Failed to render Request
|
|
|
|
throw new Error(`Render Failed: "${e.message}"`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return newObj;
|
2016-09-21 00:03:26 +00:00
|
|
|
};
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2016-09-21 00:03:26 +00:00
|
|
|
module.exports.getRenderedRequest = request => {
|
2016-08-04 04:12:45 +00:00
|
|
|
return db.requestGetAncestors(request).then(ancestors => {
|
2016-08-15 17:04:36 +00:00
|
|
|
const workspace = ancestors.find(doc => doc.type === TYPE_WORKSPACE);
|
|
|
|
|
|
|
|
return Promise.all([
|
|
|
|
db.environmentGetOrCreateForWorkspace(workspace),
|
2016-08-15 22:31:30 +00:00
|
|
|
db.environmentGetById(workspace.metaActiveEnvironmentId),
|
2016-08-15 17:04:36 +00:00
|
|
|
db.cookieJarGetOrCreateForWorkspace(workspace)
|
|
|
|
]).then(([rootEnvironment, subEnvironment, cookieJar]) => {
|
|
|
|
|
2016-09-03 04:32:45 +00:00
|
|
|
// Generate the context we need to render
|
2016-09-21 00:03:26 +00:00
|
|
|
const renderContext = module.exports.buildRenderContext(
|
2016-09-03 04:32:45 +00:00
|
|
|
ancestors,
|
|
|
|
rootEnvironment,
|
|
|
|
subEnvironment
|
|
|
|
);
|
2016-08-15 17:04:36 +00:00
|
|
|
|
2016-09-03 04:32:45 +00:00
|
|
|
// Render all request properties
|
2016-09-21 00:03:26 +00:00
|
|
|
const renderedRequest = module.exports.recursiveRender(
|
2016-09-03 04:32:45 +00:00
|
|
|
request,
|
|
|
|
renderContext
|
|
|
|
);
|
2016-08-15 17:04:36 +00:00
|
|
|
|
|
|
|
// Default the proto if it doesn't exist
|
2016-09-03 04:32:45 +00:00
|
|
|
renderedRequest.url = setDefaultProtocol(renderedRequest.url);
|
2016-08-15 17:04:36 +00:00
|
|
|
|
|
|
|
// Add the yummy cookies
|
|
|
|
renderedRequest.cookieJar = cookieJar;
|
|
|
|
|
2016-09-03 04:32:45 +00:00
|
|
|
// Add authentication
|
|
|
|
const missingAuthHeader = !hasAuthHeader(renderedRequest.headers);
|
|
|
|
if (missingAuthHeader && renderedRequest.authentication.username) {
|
|
|
|
const {username, password} = renderedRequest.authentication;
|
|
|
|
const header = getBasicAuthHeader(username, password);
|
|
|
|
|
|
|
|
renderedRequest.headers.push(header);
|
2016-08-15 22:31:30 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
return new Promise(resolve => resolve(renderedRequest));
|
|
|
|
});
|
2016-07-20 23:16:28 +00:00
|
|
|
});
|
2016-09-21 00:03:26 +00:00
|
|
|
};
|