Skip to content

Commit

Permalink
Datasource parameter extension (#297)
Browse files Browse the repository at this point in the history
* feat: obtain parameter from config, session, request

* chore: package lock

* refactor: remove underscorejs from ds module

* feat: inject parameters into chained ds endpoints

* refactor: remove underscore from view/public.js

* refactor: remove underscore usage

* refactor: favour const over let

* style: fix JSON indents

* refactor: remove underscore usage

* refactor: remove underscorejs usage

* refactor: remove underscorejs usage

* refactor: remove underscore usage

* refactor: remove underscorejs usage

* refactor: remove underscorejs usage

* fix: return first component in array

* fix: disable coverage check

* refactor: remove underscore usage

* style: standardjs

* chore: update package, remove underscore dependency 🎉

* feat: allow modification of ds endpoint from a special event

* feat: remove underscore from test files #198

* Remove underscore string (#305)

* feat: remove underscore.string #198

* feat: simplify token path generation

* fix: readability

* fix: cache flush

* fix: bearer token
  • Loading branch information
jimlambie authored and David Longworth committed Dec 6, 2017
1 parent 4b8494d commit 4836813
Show file tree
Hide file tree
Showing 46 changed files with 1,318 additions and 758 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<img src="https://dadi.tech/assets/products/dadi-web-full.png" alt="DADI Web" height="65"/>

[![npm (scoped)](https://img.shields.io/npm/v/@dadi/web.svg?maxAge=10800&style=flat-square)](https://www.npmjs.com/package/@dadi/web)
[![coverage](https://img.shields.io/badge/coverage-80%25-yellow.svg?style=flat?style=flat-square)](https://github.com/dadi/web)
[![coverage](https://img.shields.io/badge/coverage-81%25-yellow.svg?style=flat?style=flat-square)](https://github.com/dadi/web)
[![Build Status](https://travis-ci.org/dadi/web.svg?branch=master)](https://travis-ci.org/dadi/web)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com/)
[![Greenkeeper badge](https://badges.greenkeeper.io/dadi/web.svg)](https://greenkeeper.io/)
Expand Down
56 changes: 19 additions & 37 deletions dadi/lib/api/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var _ = require('underscore')
var debug = require('debug')('web:api')
var fs = require('fs')
var http = require('http')
Expand Down Expand Up @@ -135,29 +134,10 @@ Api.prototype.use = function (path, host, handler) {
*/
Api.prototype.unuse = function (path) {
debug('unuse %s', path)
var indx = 0

if (typeof path === 'function') {
if (path.length === 4) {
indx = this.errors.indexOf(path)
return !!~indx && this.errors.splice(indx, 1)
}

var functionStr = path.toString()
_.each(this.all, func => {
if (func.toString() === functionStr) {
return this.all.splice(indx, 1)
} else {
indx++
}
})

// indx = this.all.indexOf(path)
// return !!~indx && this.all.splice(indx, 1)
}

var existing = _.findWhere(this.paths, { path: path })
this.paths = _.without(this.paths, existing)
this.paths = this.paths.filter(item => {
return item.path !== path
})
}

/**
Expand Down Expand Up @@ -213,7 +193,7 @@ Api.prototype.listener = function (req, res) {

// add the original params back, in case a middleware
// has modified the current req.params
_.extend(req.params, originalReqParams)
Object.assign(req.params, originalReqParams)

try {
// if end of the stack, no middleware could handle the current
Expand All @@ -237,9 +217,9 @@ Api.prototype.listener = function (req, res) {
hrend[1] / 1000000
)

if (!_.isEmpty(matches)) {
if (matches.length > 0) {
// add the matches after the cache middleware and before the final 404 handler
_.each(matches, match => {
matches.forEach(match => {
this.stack.splice(-1, 0, match)
})
}
Expand Down Expand Up @@ -297,11 +277,11 @@ Api.prototype.getMatchingRoutes = function (req) {
// get the host key that matches the request's host header
var virtualHosts = config.get('virtualHosts')
var host =
_.findKey(virtualHosts, virtualHost => {
return _.contains(virtualHost.hostnames, req.headers.host)
Object.keys(virtualHosts).find(key => {
return virtualHosts[key].hostnames.includes(req.headers.host)
}) || ''

var paths = _.filter(this.paths, path => {
var paths = this.paths.filter(path => {
return path.path.indexOf(host) > -1
})

Expand Down Expand Up @@ -425,17 +405,17 @@ function findPath (req, paths, pathString) {
var virtualHosts = config.get('virtualHosts')

var host =
_.findKey(virtualHosts, virtualHost => {
return _.contains(virtualHost.hostnames, req.headers.host)
Object.keys(virtualHosts).find(key => {
return virtualHosts[key].hostnames.includes(req.headers.host)
}) || ''

var matchingPaths = _.filter(paths, path => {
var matchingPaths = paths.filter(path => {
return path.path.indexOf(host) > -1
})

// look for a page matching the pathString that has been loaded
// along with the rest of the API
return _.filter(matchingPaths, path => {
return matchingPaths.filter(path => {
return path.path.indexOf(pathString) > -1
})
}
Expand All @@ -445,19 +425,21 @@ function routePriority (path, keys) {

var staticRouteLength = 0
if (typeof tokens[0] === 'string') {
staticRouteLength = _.compact(tokens[0].split('/')).length
staticRouteLength = tokens[0].split('/').filter(item => {
return item && item !== ''
}).length
}

var requiredParamLength = _.filter(keys, function (key) {
var requiredParamLength = keys.filter(key => {
return !key.optional
}).length

var optionalParamLength = _.filter(keys, function (key) {
var optionalParamLength = keys.filter(key => {
return key.optional
}).length

// if there is a "page" parameter in the route, give it a slightly higher priority
var paginationParam = _.find(keys, key => {
var paginationParam = keys.find(key => {
return key.name && key.name === 'page'
})

Expand Down
13 changes: 6 additions & 7 deletions dadi/lib/auth/bearer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var path = require('path')
var config = require(path.join(__dirname, '/../../../config.js'))
var help = require(path.join(__dirname, '/../help'))
var Passport = require('@dadi/passport')

var BearerAuthStrategy = function (options) {
Expand Down Expand Up @@ -30,12 +29,12 @@ BearerAuthStrategy.prototype.getToken = function (authStrategy, done) {
walletOptions: {
path:
config.get('paths.tokenWallets') +
'/' +
help.generateTokenWalletFilename(
strategy.host,
strategy.port,
strategy.credentials.clientId
)
'/token.' +
strategy.host +
strategy.port +
'.' +
strategy.credentials.clientId +
'.json'
}
})
.then(function (bearerToken) {
Expand Down
61 changes: 35 additions & 26 deletions dadi/lib/cache/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @module Cache
*/
var _ = require('underscore')
var crypto = require('crypto')
var debug = require('debug')('web:cache')
var path = require('path')
Expand Down Expand Up @@ -80,27 +79,32 @@ Cache.prototype.cachingEnabled = function (req) {
* @returns {object}
*/
Cache.prototype.getEndpointMatchingRequest = function (req) {
var endpoints = this.server.components
var requestUrl = url.parse(req.url, true).pathname.replace(/\/+$/, '')
const endpoints = this.server.components || {}
const requestUrl = url.parse(req.url, true).pathname.replace(/\/+$/, '')

// get the host key that matches the request's host header
var virtualHosts = config.get('virtualHosts')
const virtualHosts = config.get('virtualHosts')

var host =
_.findKey(virtualHosts, virtualHost => {
return _.contains(virtualHost.hostnames, req.headers.host)
const host =
Object.keys(virtualHosts).find(key => {
return virtualHosts.hostnames.includes(req.headers.host)
}) || ''

// check if there is a match in the loaded routes for the current request URL
return _.find(endpoints, endpoint => {
var paths = _.pluck(endpoint.page.routes, 'path')
return (
_.contains(paths, requestUrl) &&
(endpoint.options && endpoint.options.host
? endpoint.options.host === host
: true)
)
const matchKey = Object.keys(endpoints).find(key => {
const paths = endpoints[key].page.routes.map(route => route.path)

if (!paths.includes(requestUrl)) {
return false
}

if (endpoints[key].options && endpoints[key].options.host) {
return endpoints[key].options.host === host
}

return true
})

return endpoints[matchKey]
}

/**
Expand All @@ -109,15 +113,19 @@ Cache.prototype.getEndpointMatchingRequest = function (req) {
* @returns {object}
*/
Cache.prototype.getEndpointMatchingLoadedPaths = function (req) {
var endpoints = this.server.components
const endpoints = this.server.components || {}

// check if there is a match in the loaded routes for the current pages `route:
// e.g. { paths: ['xx','yy'] }` property
return _.find(endpoints, endpoint => {
return !_.isEmpty(
_.intersection(_.pluck(endpoint.page.routes, 'path'), req.paths)
)
const matchKey = Object.keys(endpoints).find(key => {
const paths = endpoints[key].page.routes
.map(route => route.path)
.filter(path => req.paths.includes(path))

return paths.length > 0
})

return endpoints[matchKey]
}

/**
Expand Down Expand Up @@ -176,11 +184,11 @@ Cache.prototype.init = function () {
var requestUrl = url.parse(req.url, true).path

// get the host key that matches the request's host header
var virtualHosts = config.get('virtualHosts')
const virtualHosts = config.get('virtualHosts')

var host =
_.findKey(virtualHosts, virtualHost => {
return _.contains(virtualHost.hostnames, req.headers.host)
const host =
Object.keys(virtualHosts).find(key => {
return virtualHosts.hostnames.includes(req.headers.host)
}) || ''

var filename = crypto
Expand Down Expand Up @@ -348,7 +356,8 @@ module.exports.delete = function (pattern, callback) {
}

var i = 0
_.each(cacheKeys, function (key) {

cacheKeys.forEach(key => {
self.client().del(key, function (err, result) {
if (err) console.log(err)
i++
Expand Down
39 changes: 22 additions & 17 deletions dadi/lib/controller/forceDomain.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
var _ = require('underscore')
var url = require('url')
const url = require('url')

var forceDomain = function (options) {
const forceDomain = function (options) {
return function forceDomain (req, res, next) {
var protocol = req.headers['x-forwarded-proto'] || req.protocol || 'http'
var newRoute = domainRedirect(protocol, req.headers.host, req.url, options)
var statusCode
const protocol = req.headers['x-forwarded-proto'] || req.protocol || 'http'
const newRoute = domainRedirect(
protocol,
req.headers.host,
req.url,
options
)
let statusCode

if (!newRoute) {
return next()
Expand All @@ -28,21 +32,21 @@ var forceDomain = function (options) {
* @param {string} url - the URL of the current request
* @param {Object} options - the options passed in from the configuration block rewrites.forceDomain
*/
var domainRedirect = function (protocol, hostHeader, url, options) {
var rewrittenRoute
var route
const domainRedirect = function (protocol, hostHeader, url, options) {
let rewrittenRoute
let route

options = _.extend(options, {
options = Object.assign({}, options, {
protocol: 'http',
type: 'permanent'
})

var hostHeaderParts = (hostHeader || '').split(':')
var hostname = hostHeaderParts[0] || ''
var port = hostHeaderParts[1] - 0 || 80
const hostHeaderParts = (hostHeader || '').split(':')
const hostname = hostHeaderParts[0] || ''
const port = hostHeaderParts[1] - 0 || 80

if (options.hostname.split(':').length > 1) {
var hostnameParts = options.hostname.split(':')
const hostnameParts = options.hostname.split(':')
options.hostname = hostnameParts[0]
options.port = hostnameParts[1]
}
Expand Down Expand Up @@ -70,16 +74,17 @@ var domainRedirect = function (protocol, hostHeader, url, options) {
/**
*
*/
var domainRewrite = function (route, options) {
options = _.extend(
const domainRewrite = function (route, options) {
options = Object.assign(
{},
{
protocol: undefined,
hostname: undefined
},
options
)

var parsedRoute = url.parse(route)
let parsedRoute = url.parse(route)
parsedRoute.host = undefined

if (options.protocol) {
Expand Down
Loading

0 comments on commit 4836813

Please sign in to comment.