Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const deconstructUrl = input => {
const unescaped = decodeURI(input)
const deconstructed = url.parse(unescaped, true)
// Deal with the (silly) way arrays are handled
// https://github.com/Patreon/url-factory/blob/master/src/index.js#L4
map(deconstructed.query, (item, key) => {
// Handle the string-encoded empty array
if (item === '[]') {
deconstructed.query[key] = []
// Handle the fact that [ 'a', 'b' ] is encoded as "a,b"
} else if (includes(item, ',')) {
deconstructed.query[key] = item.split(',')
// Handle the fact that [ 'a' ] is encoded as "a"
} else if (key === 'include' && item.length) {
deconstructed.query[key] = [item]
}
})
export default async function (instance, request, response, requestedAt) {
const serverModifiers = instance._modifiers
// run server (before) modifiers
const serverBefore = serverModifiers.before
if (serverBefore) {
await serverBefore.execute()
}
// parse route
const router = instance._router
const url = request.url
const parsed = parseUrl(url, false)
const pathname = parsed.pathname.replace(/\/{2,}/g, '/')
const route = router.find(pathname, request.method)
let final
let context = createContext(instance, request, response, requestedAt, pathname, parsed.query)
if (route) {
const routeData = route.data
const routeModifiers = routeData.modifiers
// run route (before) modifiers
const routeBefore = routeModifiers.before
if (routeBefore) {
final = await routeBefore.execute(context(), true)
}
if (final === undefined) {
// method resolver
const resolver = routeData.resolver
const ctx = context(true)
import JuttleViewer from './components/juttle-viewer';
import 'juttle-client-library/dist/juttle-client-library.css';
import '../sass/main.scss';
// construct client plus views and inputs
let outriggerHost = window.location.host;
let client = new Juttle(outriggerHost);
let view = new client.View(document.getElementById('juttle-view-layout'));
let inputs = new client.Input(document.getElementById('juttle-input-groups'));
let errors = new client.Errors(document.getElementById('error-view'));
let renderError = errors.render.bind(errors);
let juttleSourceEl = document.getElementById('juttle-source');
let currentBundle;
let parsed = url.parse(window.location.href, true);
let initBundle = (bundle) => {
ReactDOM.render(, juttleSourceEl);
currentBundle = bundle;
client.describe(bundle)
.then((desc) => {
// if we have no inputs go ahead and run
if (desc.inputs.length === 0) {
return view.run(bundle)
.then(function(jobEvents) {
jobEvents.on('error', function(err) {
errors.render(err);
});
jobEvents.on('warning', function(warning) {
return function(req, res, next) {
var config = req.superstatic;
var trailingSlashBehavior = config.trailingSlash;
var parsedUrl = url.parse(req.url);
var pathname = pathutils.normalizeMultiSlashes(parsedUrl.pathname);
var search = parsedUrl.search || '';
var cleanUrlRules = !!_.get(req, 'superstatic.cleanUrls');
// Exact file always wins.
return res.superstatic.provider(req, pathname).then(function(result) {
if (result) {
// If we are using cleanURLs, we'll trim off any `.html` (or `/index.html`), if it exists.
if (cleanUrlRules) {
if (_.endsWith(pathname, '.html')) {
var redirPath = pathutils.removeTrailingString(pathname, '.html');
if (_.endsWith(redirPath, '/index')) {
redirPath = pathutils.removeTrailingString(redirPath, '/index');
}
// But if we need to keep the trailing slashes, we will do so.
function redirectAsCleanUrl (req, res) {
var pathname = url.parse(req.url).pathname;
var query = qs.stringify(req.query);
var redirectUrl = (isDirectoryIndexFile(pathname, req.config.index))
? path.dirname(pathname)
: path.join('/', path.dirname(pathname), path.basename(pathname.split('?')[0], '.html'));
redirectUrl += (query) ? '?' + query : '';
res.writeHead(301, { Location: redirectUrl });
res.end();
}
parseQuery()
{
const parsed = url.parse(window.location.search, true);
for (const key in parsed.query)
{
if (parsed.query[key] === 'true')
{
this[key] = true;
}
else if (parsed.query[key] === 'false')
{
this[key] = false;
}
else
{
this[key] = JSON.parse(parsed.query[key]);
}
}
return function (req, res, next) {
var pathname = url.parse(req.url).pathname;
query()(req, res, function (err) {
if (err) {
return next(err);
}
isDirectory(function(isDir) {
isDirectoryIndex(function(isDirIndex) {
if (isDir && !isDirIndex) {
return next();
}
fileExists(function(fExist) {
var hasTrailSlash = hasTrailingSlash();
return function (req, res, next) {
if (!req.config) return next();
var pathname = url.parse(req.url).pathname;
var headersConfig = globject(slash(req.config.headers));
var headers = headersConfig(slash(pathname)) || {};
_.each(headers, function (val, name) {
res.setHeader(name, val);
});
return next();
};
};
handle(name: string, requestOptions: WardenRequestOptions, cb: RequestCallback) {
if (!this.streams[name]) throw new Error(`Route configuration not provided for ${name}`);
const request = Url.parse(requestOptions.url, true);
const headers = requestOptions.headers || {};
const cookies = Cookie.parse(headers.cookie || headers.Cookie || '');
const key = this.streams[name][0].keyMaker(
request.pathname,
cookies,
headers,
request.query,
requestOptions.method
);
return this.streams[name][0].stream.start(key, ++this.requestId, requestOptions, cb);
}
function validateThumbnail(thumbnail) {
if (!thumbnail) {
throw new Error('ERROR_LIST_THUMBNAIL_EMPTY');
}
const parsedThumbnailURI = url.parse(thumbnail);
if (parsedThumbnailURI && !parsedThumbnailURI.host) {
throw new Error('ERROR_LIST_THUMBNAIL_NO_URL');
}
return thumbnail;
}