mirror of
https://github.com/idanoo/GoScrobble
synced 2025-07-01 21:52:19 +00:00
0.2.0 - Mid migration
This commit is contained in:
parent
139e6a915e
commit
7e38fdbd7d
42393 changed files with 5358157 additions and 62 deletions
129
web/node_modules/http-proxy-middleware/lib/config-factory.js
generated
vendored
Normal file
129
web/node_modules/http-proxy-middleware/lib/config-factory.js
generated
vendored
Normal file
|
@ -0,0 +1,129 @@
|
|||
var _ = require('lodash')
|
||||
var url = require('url')
|
||||
var ERRORS = require('./errors')
|
||||
var logger = require('./logger').getInstance()
|
||||
|
||||
module.exports = {
|
||||
createConfig: createConfig
|
||||
}
|
||||
|
||||
function createConfig(context, opts) {
|
||||
// structure of config object to be returned
|
||||
var config = {
|
||||
context: undefined,
|
||||
options: {}
|
||||
}
|
||||
|
||||
// app.use('/api', proxy({target:'http://localhost:9000'}));
|
||||
if (isContextless(context, opts)) {
|
||||
config.context = '/'
|
||||
config.options = _.assign(config.options, context)
|
||||
|
||||
// app.use('/api', proxy('http://localhost:9000'));
|
||||
// app.use(proxy('http://localhost:9000/api'));
|
||||
} else if (isStringShortHand(context)) {
|
||||
var oUrl = url.parse(context)
|
||||
var target = [oUrl.protocol, '//', oUrl.host].join('')
|
||||
|
||||
config.context = oUrl.pathname || '/'
|
||||
config.options = _.assign(config.options, { target: target }, opts)
|
||||
|
||||
if (oUrl.protocol === 'ws:' || oUrl.protocol === 'wss:') {
|
||||
config.options.ws = true
|
||||
}
|
||||
// app.use('/api', proxy({target:'http://localhost:9000'}));
|
||||
} else {
|
||||
config.context = context
|
||||
config.options = _.assign(config.options, opts)
|
||||
}
|
||||
|
||||
configureLogger(config.options)
|
||||
|
||||
if (!config.options.target) {
|
||||
throw new Error(ERRORS.ERR_CONFIG_FACTORY_TARGET_MISSING)
|
||||
}
|
||||
|
||||
// Legacy option.proxyHost
|
||||
config.options = mapLegacyProxyHostOption(config.options)
|
||||
|
||||
// Legacy option.proxyTable > option.router
|
||||
config.options = mapLegacyProxyTableOption(config.options)
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a String only target/config is provided.
|
||||
* This can be just the host or with the optional path.
|
||||
*
|
||||
* @example
|
||||
* app.use('/api', proxy('http://localhost:9000'));
|
||||
app.use(proxy('http://localhost:9000/api'));
|
||||
*
|
||||
* @param {String} context [description]
|
||||
* @return {Boolean} [description]
|
||||
*/
|
||||
function isStringShortHand(context) {
|
||||
if (_.isString(context)) {
|
||||
return !!url.parse(context).host
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a Object only config is provided, without a context.
|
||||
* In this case the all paths will be proxied.
|
||||
*
|
||||
* @example
|
||||
* app.use('/api', proxy({target:'http://localhost:9000'}));
|
||||
*
|
||||
* @param {Object} context [description]
|
||||
* @param {*} opts [description]
|
||||
* @return {Boolean} [description]
|
||||
*/
|
||||
function isContextless(context, opts) {
|
||||
return _.isPlainObject(context) && _.isEmpty(opts)
|
||||
}
|
||||
|
||||
function mapLegacyProxyHostOption(options) {
|
||||
// set options.headers.host when option.proxyHost is provided
|
||||
if (options.proxyHost) {
|
||||
logger.warn('*************************************')
|
||||
logger.warn('[HPM] Deprecated "option.proxyHost"')
|
||||
logger.warn(
|
||||
' Use "option.changeOrigin" or "option.headers.host" instead'
|
||||
)
|
||||
logger.warn(' "option.proxyHost" will be removed in future release.')
|
||||
logger.warn('*************************************')
|
||||
|
||||
options.headers = options.headers || {}
|
||||
options.headers.host = options.proxyHost
|
||||
}
|
||||
|
||||
return options
|
||||
}
|
||||
|
||||
// Warn deprecated proxyTable api usage
|
||||
function mapLegacyProxyTableOption(options) {
|
||||
if (options.proxyTable) {
|
||||
logger.warn('*************************************')
|
||||
logger.warn('[HPM] Deprecated "option.proxyTable"')
|
||||
logger.warn(' Use "option.router" instead')
|
||||
logger.warn(' "option.proxyTable" will be removed in future release.')
|
||||
logger.warn('*************************************')
|
||||
|
||||
options.router = _.clone(options.proxyTable)
|
||||
_.omit(options, 'proxyTable')
|
||||
}
|
||||
|
||||
return options
|
||||
}
|
||||
|
||||
function configureLogger(options) {
|
||||
if (options.logLevel) {
|
||||
logger.setLevel(options.logLevel)
|
||||
}
|
||||
|
||||
if (options.logProvider) {
|
||||
logger.setProvider(options.logProvider)
|
||||
}
|
||||
}
|
94
web/node_modules/http-proxy-middleware/lib/context-matcher.js
generated
vendored
Normal file
94
web/node_modules/http-proxy-middleware/lib/context-matcher.js
generated
vendored
Normal file
|
@ -0,0 +1,94 @@
|
|||
var _ = require('lodash')
|
||||
var url = require('url')
|
||||
var isGlob = require('is-glob')
|
||||
var micromatch = require('micromatch')
|
||||
var ERRORS = require('./errors')
|
||||
|
||||
module.exports = {
|
||||
match: matchContext
|
||||
}
|
||||
|
||||
function matchContext(context, uri, req) {
|
||||
// single path
|
||||
if (isStringPath(context)) {
|
||||
return matchSingleStringPath(context, uri)
|
||||
}
|
||||
|
||||
// single glob path
|
||||
if (isGlobPath(context)) {
|
||||
return matchSingleGlobPath(context, uri)
|
||||
}
|
||||
|
||||
// multi path
|
||||
if (Array.isArray(context)) {
|
||||
if (context.every(isStringPath)) {
|
||||
return matchMultiPath(context, uri)
|
||||
}
|
||||
if (context.every(isGlobPath)) {
|
||||
return matchMultiGlobPath(context, uri)
|
||||
}
|
||||
|
||||
throw new Error(ERRORS.ERR_CONTEXT_MATCHER_INVALID_ARRAY)
|
||||
}
|
||||
|
||||
// custom matching
|
||||
if (_.isFunction(context)) {
|
||||
var pathname = getUrlPathName(uri)
|
||||
return context(pathname, req)
|
||||
}
|
||||
|
||||
throw new Error(ERRORS.ERR_CONTEXT_MATCHER_GENERIC)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String} context '/api'
|
||||
* @param {String} uri 'http://example.org/api/b/c/d.html'
|
||||
* @return {Boolean}
|
||||
*/
|
||||
function matchSingleStringPath(context, uri) {
|
||||
var pathname = getUrlPathName(uri)
|
||||
return pathname.indexOf(context) === 0
|
||||
}
|
||||
|
||||
function matchSingleGlobPath(pattern, uri) {
|
||||
var pathname = getUrlPathName(uri)
|
||||
var matches = micromatch(pathname, pattern)
|
||||
return matches && matches.length > 0
|
||||
}
|
||||
|
||||
function matchMultiGlobPath(patternList, uri) {
|
||||
return matchSingleGlobPath(patternList, uri)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String} contextList ['/api', '/ajax']
|
||||
* @param {String} uri 'http://example.org/api/b/c/d.html'
|
||||
* @return {Boolean}
|
||||
*/
|
||||
function matchMultiPath(contextList, uri) {
|
||||
for (var i = 0; i < contextList.length; i++) {
|
||||
var context = contextList[i]
|
||||
if (matchSingleStringPath(context, uri)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses URI and returns RFC 3986 path
|
||||
*
|
||||
* @param {String} uri from req.url
|
||||
* @return {String} RFC 3986 path
|
||||
*/
|
||||
function getUrlPathName(uri) {
|
||||
return uri && url.parse(uri).pathname
|
||||
}
|
||||
|
||||
function isStringPath(context) {
|
||||
return _.isString(context) && !isGlob(context)
|
||||
}
|
||||
|
||||
function isGlobPath(context) {
|
||||
return isGlob(context)
|
||||
}
|
12
web/node_modules/http-proxy-middleware/lib/errors.js
generated
vendored
Normal file
12
web/node_modules/http-proxy-middleware/lib/errors.js
generated
vendored
Normal file
|
@ -0,0 +1,12 @@
|
|||
/* eslint-disable max-len */
|
||||
|
||||
module.exports = {
|
||||
ERR_CONFIG_FACTORY_TARGET_MISSING:
|
||||
'[HPM] Missing "target" option. Example: {target: "http://www.example.org"}',
|
||||
ERR_CONTEXT_MATCHER_GENERIC:
|
||||
'[HPM] Invalid context. Expecting something like: "/api" or ["/api", "/ajax"]',
|
||||
ERR_CONTEXT_MATCHER_INVALID_ARRAY:
|
||||
'[HPM] Invalid context. Expecting something like: ["/api", "/ajax"] or ["/api/**", "!**.html"]',
|
||||
ERR_PATH_REWRITER_CONFIG:
|
||||
'[HPM] Invalid pathRewrite config. Expecting object with pathRewrite config or a rewrite function'
|
||||
}
|
82
web/node_modules/http-proxy-middleware/lib/handlers.js
generated
vendored
Normal file
82
web/node_modules/http-proxy-middleware/lib/handlers.js
generated
vendored
Normal file
|
@ -0,0 +1,82 @@
|
|||
var _ = require('lodash')
|
||||
var logger = require('./logger').getInstance()
|
||||
|
||||
module.exports = {
|
||||
init: init,
|
||||
getHandlers: getProxyEventHandlers
|
||||
}
|
||||
|
||||
function init(proxy, opts) {
|
||||
var handlers = getProxyEventHandlers(opts)
|
||||
|
||||
_.forIn(handlers, function(handler, eventName) {
|
||||
proxy.on(eventName, handlers[eventName])
|
||||
})
|
||||
|
||||
logger.debug('[HPM] Subscribed to http-proxy events: ', _.keys(handlers))
|
||||
}
|
||||
|
||||
function getProxyEventHandlers(opts) {
|
||||
// https://github.com/nodejitsu/node-http-proxy#listening-for-proxy-events
|
||||
var proxyEvents = [
|
||||
'error',
|
||||
'proxyReq',
|
||||
'proxyReqWs',
|
||||
'proxyRes',
|
||||
'open',
|
||||
'close'
|
||||
]
|
||||
var handlers = {}
|
||||
|
||||
_.forEach(proxyEvents, function(event) {
|
||||
// all handlers for the http-proxy events are prefixed with 'on'.
|
||||
// loop through options and try to find these handlers
|
||||
// and add them to the handlers object for subscription in init().
|
||||
var eventName = _.camelCase('on ' + event)
|
||||
var fnHandler = _.get(opts, eventName)
|
||||
|
||||
if (_.isFunction(fnHandler)) {
|
||||
handlers[event] = fnHandler
|
||||
}
|
||||
})
|
||||
|
||||
// add default error handler in absence of error handler
|
||||
if (!_.isFunction(handlers.error)) {
|
||||
handlers.error = defaultErrorHandler
|
||||
}
|
||||
|
||||
// add default close handler in absence of close handler
|
||||
if (!_.isFunction(handlers.close)) {
|
||||
handlers.close = logClose
|
||||
}
|
||||
|
||||
return handlers
|
||||
}
|
||||
|
||||
function defaultErrorHandler(err, req, res) {
|
||||
var host = req.headers && req.headers.host
|
||||
var code = err.code
|
||||
|
||||
if (res.writeHead && !res.headersSent) {
|
||||
if (/HPE_INVALID/.test(code)) {
|
||||
res.writeHead(502)
|
||||
} else {
|
||||
switch (code) {
|
||||
case 'ECONNRESET':
|
||||
case 'ENOTFOUND':
|
||||
case 'ECONNREFUSED':
|
||||
res.writeHead(504)
|
||||
break
|
||||
default:
|
||||
res.writeHead(500)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res.end('Error occured while trying to proxy to: ' + host + req.url)
|
||||
}
|
||||
|
||||
function logClose(req, socket, head) {
|
||||
// view disconnected websocket connections
|
||||
logger.info('[HPM] Client disconnected')
|
||||
}
|
182
web/node_modules/http-proxy-middleware/lib/index.js
generated
vendored
Normal file
182
web/node_modules/http-proxy-middleware/lib/index.js
generated
vendored
Normal file
|
@ -0,0 +1,182 @@
|
|||
var _ = require('lodash')
|
||||
var httpProxy = require('http-proxy')
|
||||
var configFactory = require('./config-factory')
|
||||
var handlers = require('./handlers')
|
||||
var contextMatcher = require('./context-matcher')
|
||||
var PathRewriter = require('./path-rewriter')
|
||||
var Router = require('./router')
|
||||
var logger = require('./logger').getInstance()
|
||||
var getArrow = require('./logger').getArrow
|
||||
|
||||
module.exports = HttpProxyMiddleware
|
||||
|
||||
function HttpProxyMiddleware(context, opts) {
|
||||
// https://github.com/chimurai/http-proxy-middleware/issues/57
|
||||
var wsUpgradeDebounced = _.debounce(handleUpgrade)
|
||||
var wsInitialized = false
|
||||
var config = configFactory.createConfig(context, opts)
|
||||
var proxyOptions = config.options
|
||||
|
||||
// create proxy
|
||||
var proxy = httpProxy.createProxyServer({})
|
||||
logger.info(
|
||||
'[HPM] Proxy created:',
|
||||
config.context,
|
||||
' -> ',
|
||||
proxyOptions.target
|
||||
)
|
||||
|
||||
var pathRewriter = PathRewriter.create(proxyOptions.pathRewrite) // returns undefined when "pathRewrite" is not provided
|
||||
|
||||
// attach handler to http-proxy events
|
||||
handlers.init(proxy, proxyOptions)
|
||||
|
||||
// log errors for debug purpose
|
||||
proxy.on('error', logError)
|
||||
|
||||
// https://github.com/chimurai/http-proxy-middleware/issues/19
|
||||
// expose function to upgrade externally
|
||||
middleware.upgrade = wsUpgradeDebounced
|
||||
|
||||
return middleware
|
||||
|
||||
function middleware(req, res, next) {
|
||||
if (shouldProxy(config.context, req)) {
|
||||
var activeProxyOptions = prepareProxyRequest(req)
|
||||
proxy.web(req, res, activeProxyOptions)
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
|
||||
if (proxyOptions.ws === true) {
|
||||
// use initial request to access the server object to subscribe to http upgrade event
|
||||
catchUpgradeRequest(req.connection.server)
|
||||
}
|
||||
}
|
||||
|
||||
function catchUpgradeRequest(server) {
|
||||
// subscribe once; don't subscribe on every request...
|
||||
// https://github.com/chimurai/http-proxy-middleware/issues/113
|
||||
if (!wsInitialized) {
|
||||
server.on('upgrade', wsUpgradeDebounced)
|
||||
wsInitialized = true
|
||||
}
|
||||
}
|
||||
|
||||
function handleUpgrade(req, socket, head) {
|
||||
// set to initialized when used externally
|
||||
wsInitialized = true
|
||||
|
||||
if (shouldProxy(config.context, req)) {
|
||||
var activeProxyOptions = prepareProxyRequest(req)
|
||||
proxy.ws(req, socket, head, activeProxyOptions)
|
||||
logger.info('[HPM] Upgrading to WebSocket')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether request should be proxied.
|
||||
*
|
||||
* @private
|
||||
* @param {String} context [description]
|
||||
* @param {Object} req [description]
|
||||
* @return {Boolean}
|
||||
*/
|
||||
function shouldProxy(context, req) {
|
||||
var path = req.originalUrl || req.url
|
||||
return contextMatcher.match(context, path, req)
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply option.router and option.pathRewrite
|
||||
* Order matters:
|
||||
* Router uses original path for routing;
|
||||
* NOT the modified path, after it has been rewritten by pathRewrite
|
||||
* @param {Object} req
|
||||
* @return {Object} proxy options
|
||||
*/
|
||||
function prepareProxyRequest(req) {
|
||||
// https://github.com/chimurai/http-proxy-middleware/issues/17
|
||||
// https://github.com/chimurai/http-proxy-middleware/issues/94
|
||||
req.url = req.originalUrl || req.url
|
||||
|
||||
// store uri before it gets rewritten for logging
|
||||
var originalPath = req.url
|
||||
var newProxyOptions = _.assign({}, proxyOptions)
|
||||
|
||||
// Apply in order:
|
||||
// 1. option.router
|
||||
// 2. option.pathRewrite
|
||||
__applyRouter(req, newProxyOptions)
|
||||
__applyPathRewrite(req, pathRewriter)
|
||||
|
||||
// debug logging for both http(s) and websockets
|
||||
if (proxyOptions.logLevel === 'debug') {
|
||||
var arrow = getArrow(
|
||||
originalPath,
|
||||
req.url,
|
||||
proxyOptions.target,
|
||||
newProxyOptions.target
|
||||
)
|
||||
logger.debug(
|
||||
'[HPM] %s %s %s %s',
|
||||
req.method,
|
||||
originalPath,
|
||||
arrow,
|
||||
newProxyOptions.target
|
||||
)
|
||||
}
|
||||
|
||||
return newProxyOptions
|
||||
}
|
||||
|
||||
// Modify option.target when router present.
|
||||
function __applyRouter(req, options) {
|
||||
var newTarget
|
||||
|
||||
if (options.router) {
|
||||
newTarget = Router.getTarget(req, options)
|
||||
|
||||
if (newTarget) {
|
||||
logger.debug(
|
||||
'[HPM] Router new target: %s -> "%s"',
|
||||
options.target,
|
||||
newTarget
|
||||
)
|
||||
options.target = newTarget
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// rewrite path
|
||||
function __applyPathRewrite(req, pathRewriter) {
|
||||
if (pathRewriter) {
|
||||
var path = pathRewriter(req.url, req)
|
||||
|
||||
if (typeof path === 'string') {
|
||||
req.url = path
|
||||
} else {
|
||||
logger.info('[HPM] pathRewrite: No rewritten path found. (%s)', req.url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function logError(err, req, res) {
|
||||
var hostname =
|
||||
(req.headers && req.headers.host) || (req.hostname || req.host) // (websocket) || (node0.10 || node 4/5)
|
||||
var target = proxyOptions.target.host || proxyOptions.target
|
||||
var errorMessage =
|
||||
'[HPM] Error occurred while trying to proxy request %s from %s to %s (%s) (%s)'
|
||||
var errReference =
|
||||
'https://nodejs.org/api/errors.html#errors_common_system_errors' // link to Node Common Systems Errors page
|
||||
|
||||
logger.error(
|
||||
errorMessage,
|
||||
req.url,
|
||||
hostname,
|
||||
target,
|
||||
err.code || err,
|
||||
errReference
|
||||
)
|
||||
}
|
||||
}
|
172
web/node_modules/http-proxy-middleware/lib/logger.js
generated
vendored
Normal file
172
web/node_modules/http-proxy-middleware/lib/logger.js
generated
vendored
Normal file
|
@ -0,0 +1,172 @@
|
|||
var util = require('util')
|
||||
var _ = require('lodash')
|
||||
|
||||
var loggerInstance
|
||||
|
||||
var defaultProvider = {
|
||||
log: console.log,
|
||||
debug: console.log, // use .log(); since console does not have .debug()
|
||||
info: console.info,
|
||||
warn: console.warn,
|
||||
error: console.error
|
||||
}
|
||||
|
||||
// log level 'weight'
|
||||
var LEVELS = {
|
||||
debug: 10,
|
||||
info: 20,
|
||||
warn: 30,
|
||||
error: 50,
|
||||
silent: 80
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
// singleton
|
||||
getInstance: function() {
|
||||
if (!loggerInstance) {
|
||||
loggerInstance = new Logger()
|
||||
}
|
||||
|
||||
return loggerInstance
|
||||
},
|
||||
getArrow: getArrow
|
||||
}
|
||||
|
||||
function Logger() {
|
||||
var logLevel
|
||||
var provider
|
||||
|
||||
var api = {
|
||||
log: log,
|
||||
debug: debug,
|
||||
info: info,
|
||||
warn: warn,
|
||||
error: error,
|
||||
setLevel: function(v) {
|
||||
if (isValidLevel(v)) {
|
||||
logLevel = v
|
||||
}
|
||||
},
|
||||
setProvider: function(fn) {
|
||||
if (fn && isValidProvider(fn)) {
|
||||
provider = fn(defaultProvider)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
init()
|
||||
|
||||
return api
|
||||
|
||||
function init() {
|
||||
api.setLevel('info')
|
||||
api.setProvider(function() {
|
||||
return defaultProvider
|
||||
})
|
||||
}
|
||||
|
||||
// log will log messages, regardless of logLevels
|
||||
function log() {
|
||||
provider.log(_interpolate.apply(null, arguments))
|
||||
}
|
||||
|
||||
function debug() {
|
||||
if (_showLevel('debug')) {
|
||||
provider.debug(_interpolate.apply(null, arguments))
|
||||
}
|
||||
}
|
||||
|
||||
function info() {
|
||||
if (_showLevel('info')) {
|
||||
provider.info(_interpolate.apply(null, arguments))
|
||||
}
|
||||
}
|
||||
|
||||
function warn() {
|
||||
if (_showLevel('warn')) {
|
||||
provider.warn(_interpolate.apply(null, arguments))
|
||||
}
|
||||
}
|
||||
|
||||
function error() {
|
||||
if (_showLevel('error')) {
|
||||
provider.error(_interpolate.apply(null, arguments))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decide to log or not to log, based on the log levels 'weight'
|
||||
* @param {String} showLevel [debug, info, warn, error, silent]
|
||||
* @return {Boolean}
|
||||
*/
|
||||
function _showLevel(showLevel) {
|
||||
var result = false
|
||||
var currentLogLevel = LEVELS[logLevel]
|
||||
|
||||
if (currentLogLevel && currentLogLevel <= LEVELS[showLevel]) {
|
||||
result = true
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// make sure logged messages and its data are return interpolated
|
||||
// make it possible for additional log data, such date/time or custom prefix.
|
||||
function _interpolate() {
|
||||
var fn = _.spread(util.format)
|
||||
var result = fn(_.slice(arguments))
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
function isValidProvider(fnProvider) {
|
||||
var result = true
|
||||
|
||||
if (fnProvider && !_.isFunction(fnProvider)) {
|
||||
throw new Error('[HPM] Log provider config error. Expecting a function.')
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
function isValidLevel(levelName) {
|
||||
var validLevels = _.keys(LEVELS)
|
||||
var isValid = _.includes(validLevels, levelName)
|
||||
|
||||
if (!isValid) {
|
||||
throw new Error('[HPM] Log level error. Invalid logLevel.')
|
||||
}
|
||||
|
||||
return isValid
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* -> normal proxy
|
||||
* => router
|
||||
* ~> pathRewrite
|
||||
* ≈> router + pathRewrite
|
||||
*
|
||||
* @param {String} originalPath
|
||||
* @param {String} newPath
|
||||
* @param {String} originalTarget
|
||||
* @param {String} newTarget
|
||||
* @return {String}
|
||||
*/
|
||||
function getArrow(originalPath, newPath, originalTarget, newTarget) {
|
||||
var arrow = ['>']
|
||||
var isNewTarget = originalTarget !== newTarget // router
|
||||
var isNewPath = originalPath !== newPath // pathRewrite
|
||||
|
||||
if (isNewPath && !isNewTarget) {
|
||||
arrow.unshift('~')
|
||||
} else if (!isNewPath && isNewTarget) {
|
||||
arrow.unshift('=')
|
||||
} else if (isNewPath && isNewTarget) {
|
||||
arrow.unshift('≈')
|
||||
} else {
|
||||
arrow.unshift('-')
|
||||
}
|
||||
|
||||
return arrow.join('')
|
||||
}
|
79
web/node_modules/http-proxy-middleware/lib/path-rewriter.js
generated
vendored
Normal file
79
web/node_modules/http-proxy-middleware/lib/path-rewriter.js
generated
vendored
Normal file
|
@ -0,0 +1,79 @@
|
|||
var _ = require('lodash')
|
||||
var logger = require('./logger').getInstance()
|
||||
var ERRORS = require('./errors')
|
||||
|
||||
module.exports = {
|
||||
create: createPathRewriter
|
||||
}
|
||||
|
||||
/**
|
||||
* Create rewrite function, to cache parsed rewrite rules.
|
||||
*
|
||||
* @param {Object} rewriteConfig
|
||||
* @return {Function} Function to rewrite paths; This function should accept `path` (request.url) as parameter
|
||||
*/
|
||||
function createPathRewriter(rewriteConfig) {
|
||||
var rulesCache
|
||||
|
||||
if (!isValidRewriteConfig(rewriteConfig)) {
|
||||
return
|
||||
}
|
||||
|
||||
if (_.isFunction(rewriteConfig)) {
|
||||
var customRewriteFn = rewriteConfig
|
||||
return customRewriteFn
|
||||
} else {
|
||||
rulesCache = parsePathRewriteRules(rewriteConfig)
|
||||
return rewritePath
|
||||
}
|
||||
|
||||
function rewritePath(path) {
|
||||
var result = path
|
||||
|
||||
_.forEach(rulesCache, function(rule) {
|
||||
if (rule.regex.test(path)) {
|
||||
result = result.replace(rule.regex, rule.value)
|
||||
logger.debug('[HPM] Rewriting path from "%s" to "%s"', path, result)
|
||||
return false
|
||||
}
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
function isValidRewriteConfig(rewriteConfig) {
|
||||
if (_.isFunction(rewriteConfig)) {
|
||||
return true
|
||||
} else if (!_.isEmpty(rewriteConfig) && _.isPlainObject(rewriteConfig)) {
|
||||
return true
|
||||
} else if (
|
||||
_.isUndefined(rewriteConfig) ||
|
||||
_.isNull(rewriteConfig) ||
|
||||
_.isEqual(rewriteConfig, {})
|
||||
) {
|
||||
return false
|
||||
} else {
|
||||
throw new Error(ERRORS.ERR_PATH_REWRITER_CONFIG)
|
||||
}
|
||||
}
|
||||
|
||||
function parsePathRewriteRules(rewriteConfig) {
|
||||
var rules = []
|
||||
|
||||
if (_.isPlainObject(rewriteConfig)) {
|
||||
_.forIn(rewriteConfig, function(value, key) {
|
||||
rules.push({
|
||||
regex: new RegExp(key),
|
||||
value: rewriteConfig[key]
|
||||
})
|
||||
logger.info(
|
||||
'[HPM] Proxy rewrite rule created: "%s" ~> "%s"',
|
||||
key,
|
||||
rewriteConfig[key]
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
return rules
|
||||
}
|
51
web/node_modules/http-proxy-middleware/lib/router.js
generated
vendored
Normal file
51
web/node_modules/http-proxy-middleware/lib/router.js
generated
vendored
Normal file
|
@ -0,0 +1,51 @@
|
|||
var _ = require('lodash')
|
||||
var logger = require('./logger.js').getInstance()
|
||||
|
||||
module.exports = {
|
||||
getTarget: getTarget
|
||||
}
|
||||
|
||||
function getTarget(req, config) {
|
||||
var newTarget
|
||||
var router = config.router
|
||||
|
||||
if (_.isPlainObject(router)) {
|
||||
newTarget = getTargetFromProxyTable(req, router)
|
||||
} else if (_.isFunction(router)) {
|
||||
newTarget = router(req)
|
||||
}
|
||||
|
||||
return newTarget
|
||||
}
|
||||
|
||||
function getTargetFromProxyTable(req, table) {
|
||||
var result
|
||||
var host = req.headers.host
|
||||
var path = req.url
|
||||
|
||||
var hostAndPath = host + path
|
||||
|
||||
_.forIn(table, function(value, key) {
|
||||
if (containsPath(key)) {
|
||||
if (hostAndPath.indexOf(key) > -1) {
|
||||
// match 'localhost:3000/api'
|
||||
result = table[key]
|
||||
logger.debug('[HPM] Router table match: "%s"', key)
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
if (key === host) {
|
||||
// match 'localhost:3000'
|
||||
result = table[key]
|
||||
logger.debug('[HPM] Router table match: "%s"', host)
|
||||
return false
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
function containsPath(v) {
|
||||
return v.indexOf('/') > -1
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue