Skip to content

Commit

Permalink
remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
75lb committed May 23, 2020
1 parent 190a0b7 commit ebe902a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 43 deletions.
7 changes: 1 addition & 6 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,5 @@ if (!(from && to && url)) {
console.log('USAGE:')
console.log('$ lws-rewrite <from> <to> <url>')
} else {
const urlUtil = require('url')
if (urlUtil.parse(to).host) {
console.log(util.getRemoteTargetUrl(from, to, url))
} else {
console.log(util.getRemoteTargetUrl(from, to, url))
}
console.log(util.getTargetUrl(from, to, url))
}
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function proxyRequest (route, mw, lws) {
ctx.respond = false

/* get remote URL */
const remoteUrl = util.getRemoteTargetUrl(route.from, route.to, ctx.url)
const remoteUrl = util.getTargetUrl(route.from, route.to, ctx.url)

/* info about this rewrite */
const rewrite = {
Expand Down Expand Up @@ -159,7 +159,7 @@ function proxyRequest (route, mw, lws) {

function rewrite (from, to, mw) {
return async function (ctx, next) {
const targetUrl = util.getLocalTargetUrl(from, to, ctx.url)
const targetUrl = util.getTargetUrl(from, to, ctx.url)
if (ctx.url !== targetUrl) {
const initialUrl = ctx.url
ctx.url = targetUrl
Expand Down
31 changes: 2 additions & 29 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function parseRewriteRules (rules) {
})
}

function getRemoteTargetUrl (from, to, url) {
function getTargetUrl (from, to, url) {
const { pathToRegexp } = require('path-to-regexp')
const fromParams = []
const re = pathToRegexp(from, fromParams)
Expand Down Expand Up @@ -63,34 +63,7 @@ function removeCookieAttribute (cookie = '', attr) {
.join('; ')
}

function getLocalTargetUrl (from, to, url) {
const { pathToRegexp } = require('path-to-regexp')
const keys = []
const re = pathToRegexp(from, keys)
const keysObject = toObject(keys)
const matches = re.exec(url)

if (matches) {
return to.replace(/\$(\d+)|(?::(\w+))/g, (_, n, name) => {
if (name) return matches[keysObject[name].index + 1] || ''
return matches[n] || ''
})
} else {
return url
}
}

function toObject (params) {
const object = {}
params.forEach((param, i) => {
param.index = i
object[param.name] = param
})
return object
}

exports.parseRewriteRules = parseRewriteRules
exports.getRemoteTargetUrl = getRemoteTargetUrl
exports.getTargetUrl = getTargetUrl
exports.removeHopSpecificHeaders = removeHopSpecificHeaders
exports.removeCookieAttribute = removeCookieAttribute
exports.getLocalTargetUrl = getLocalTargetUrl
12 changes: 6 additions & 6 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,32 @@ tom.test('parseRewriteRules', async function () {
})

tom.test('getToUrl: no params', async function () {
const result = util.getRemoteTargetUrl('/one', '/two', '/one')
const result = util.getTargetUrl('/one', '/two', '/one')
a.strictEqual(result, '/two')
})

tom.test('getToUrl: replace named parameter', async function () {
const result = util.getRemoteTargetUrl('/one/:id', '/:id/two', '/one/2')
const result = util.getTargetUrl('/one/:id', '/:id/two', '/one/2')
a.strictEqual(result, '/2/two')
})

tom.test("getToUrl: don't replace named parameter", async function () {
const result = util.getRemoteTargetUrl('/one/:id', '/:id/two', '/one/2/one')
const result = util.getTargetUrl('/one/:id', '/:id/two', '/one/2/one')
a.strictEqual(result, '/one/2/one')
})

tom.test('getToUrl: replace named parameter twice', async function () {
const result = util.getRemoteTargetUrl('/one/:id', '/:id/two/:id', '/one/2')
const result = util.getTargetUrl('/one/:id', '/:id/two/:id', '/one/2')
a.strictEqual(result, '/2/two/2')
})

tom.test('getToUrl: replaced wildcard', async function () {
const result = util.getRemoteTargetUrl('/(.*)', 'http://example.com/$1', '/api/2/data')
const result = util.getTargetUrl('/(.*)', 'http://example.com/$1', '/api/2/data')
a.strictEqual(result, 'http://example.com/api/2/data')
})

tom.test('getToUrl: replaced named param plus wildcard', async function () {
const result = util.getRemoteTargetUrl('/:name/(.*)', 'http://example.com/$2/:name', '/api/2/data')
const result = util.getTargetUrl('/:name/(.*)', 'http://example.com/$2/:name', '/api/2/data')
a.strictEqual(result, 'http://example.com/2/data/api')
})

Expand Down

0 comments on commit ebe902a

Please sign in to comment.