From ebe902a0089a599530ed336356de775765d9904f Mon Sep 17 00:00:00 2001 From: Lloyd Brookes Date: Sat, 23 May 2020 12:10:17 +0100 Subject: [PATCH] remove unused code --- bin/cli.js | 7 +------ index.js | 4 ++-- lib/util.js | 31 ++----------------------------- test/util.js | 12 ++++++------ 4 files changed, 11 insertions(+), 43 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index 0d09258..3e26056 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -6,10 +6,5 @@ if (!(from && to && url)) { console.log('USAGE:') console.log('$ lws-rewrite ') } 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)) } diff --git a/index.js b/index.js index 253e60b..5f535fa 100644 --- a/index.js +++ b/index.js @@ -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 = { @@ -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 diff --git a/lib/util.js b/lib/util.js index d536cf8..8015b12 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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) @@ -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 diff --git a/test/util.js b/test/util.js index f648ee4..8d7c473 100644 --- a/test/util.js +++ b/test/util.js @@ -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') })