find-my-way
Advanced tools
Comparing version
@@ -5,15 +5,13 @@ 'use strict' | ||
function HostStorage () { | ||
var hosts = {} | ||
var regexHosts = [] | ||
const hosts = {} | ||
const regexHosts = [] | ||
return { | ||
get: (host) => { | ||
var exact = hosts[host] | ||
const exact = hosts[host] | ||
if (exact) { | ||
return exact | ||
} | ||
var item | ||
for (var i = 0; i < regexHosts.length; i++) { | ||
item = regexHosts[i] | ||
if (item.host.test(host)) { | ||
return item.value | ||
for (const regex of regexHosts) { | ||
if (regex.host.test(host)) { | ||
return regex.value | ||
} | ||
@@ -28,10 +26,2 @@ } | ||
} | ||
}, | ||
del: (host) => { | ||
delete hosts[host] | ||
regexHosts = regexHosts.filter((obj) => String(obj.host) !== String(host)) | ||
}, | ||
empty: () => { | ||
hosts = {} | ||
regexHosts = [] | ||
} | ||
@@ -38,0 +28,0 @@ } |
'use strict' | ||
const SemVerStore = require('semver-store') | ||
const assert = require('assert') | ||
function SemVerStore () { | ||
if (!(this instanceof SemVerStore)) { | ||
return new SemVerStore() | ||
} | ||
this.store = {} | ||
this.maxMajor = 0 | ||
this.maxMinors = {} | ||
this.maxPatches = {} | ||
} | ||
SemVerStore.prototype.set = function (version, store) { | ||
if (typeof version !== 'string') { | ||
throw new TypeError('Version should be a string') | ||
} | ||
let [major, minor, patch] = version.split('.') | ||
major = Number(major) || 0 | ||
minor = Number(minor) || 0 | ||
patch = Number(patch) || 0 | ||
if (major >= this.maxMajor) { | ||
this.maxMajor = major | ||
this.store.x = store | ||
this.store['*'] = store | ||
this.store['x.x'] = store | ||
this.store['x.x.x'] = store | ||
} | ||
if (minor >= (this.maxMinors[major] || 0)) { | ||
this.maxMinors[major] = minor | ||
this.store[`${major}.x`] = store | ||
this.store[`${major}.x.x`] = store | ||
} | ||
if (patch >= (this.store[`${major}.${minor}`] || 0)) { | ||
this.maxPatches[`${major}.${minor}`] = patch | ||
this.store[`${major}.${minor}.x`] = store | ||
} | ||
this.store[`${major}.${minor}.${patch}`] = store | ||
return this | ||
} | ||
SemVerStore.prototype.get = function (version) { | ||
return this.store[version] | ||
} | ||
module.exports = { | ||
@@ -7,0 +55,0 @@ name: 'version', |
{ | ||
"name": "find-my-way", | ||
"version": "5.4.0", | ||
"version": "5.4.1", | ||
"description": "Crazy fast http radix based router", | ||
@@ -48,4 +48,3 @@ "main": "index.js", | ||
"fast-deep-equal": "^3.1.3", | ||
"safe-regex2": "^2.0.0", | ||
"semver-store": "^0.3.0" | ||
"safe-regex2": "^2.0.0" | ||
}, | ||
@@ -52,0 +51,0 @@ "tsd": { |
@@ -164,5 +164,3 @@ # find-my-way | ||
get: (type) => { return handlers[type] || null }, | ||
set: (type, store) => { handlers[type] = store }, | ||
del: (type) => { delete handlers[type] }, | ||
empty: () => { handlers = {} } | ||
set: (type, store) => { handlers[type] = store } | ||
} | ||
@@ -209,5 +207,3 @@ }, | ||
get: (version) => { return versions[version] || null }, | ||
set: (version, store) => { versions[version] = store }, | ||
del: (version) => { delete versions[version] }, | ||
empty: () => { versions = {} } | ||
set: (version, store) => { versions[version] = store } | ||
} | ||
@@ -499,3 +495,3 @@ }, | ||
To include a display of the `store` data passed to individual routes, the | ||
To include a display of the `store` data passed to individual routes, the | ||
option `includeMeta` may be passed. If set to `true` all items will be | ||
@@ -502,0 +498,0 @@ displayed, this can also be set to an array specifying which keys (if |
@@ -28,27 +28,1 @@ const acceptHostStrategy = require('../lib/strategies/accept-host') | ||
}) | ||
t.test('exact host matches can be removed', async (t) => { | ||
const storage = acceptHostStrategy.storage() | ||
storage.set('fastify.io', true) | ||
t.equal(storage.get('fastify.io'), true) | ||
storage.del('fastify.io') | ||
t.equal(storage.get('fastify.io'), undefined) | ||
}) | ||
t.test('regexp host matches can be removed', async (t) => { | ||
const storage = acceptHostStrategy.storage() | ||
t.equal(storage.get('fastify.io'), undefined) | ||
storage.set(/.+fastify\.io/, true) | ||
t.equal(storage.get('foo.fastify.io'), true) | ||
storage.del(/.+fastify\.io/) | ||
t.equal(storage.get('foo.fastify.io'), undefined) | ||
}) | ||
t.test('storage can be emptied', async (t) => { | ||
const storage = acceptHostStrategy.storage() | ||
storage.set(/.+fastify\.io/, 'wildcard') | ||
storage.set('auth.fastify.io', 'exact') | ||
storage.empty() | ||
t.equal(storage.get('fastify.io'), undefined) | ||
t.equal(storage.get('foo.fastify.io'), undefined) | ||
}) |
@@ -37,2 +37,19 @@ 'use strict' | ||
test('route with an extension regex 2', t => { | ||
t.plan(2) | ||
const findMyWay = FindMyWay({ | ||
defaultRoute: (req) => { | ||
t.fail(`route not matched: ${req.url}`) | ||
} | ||
}) | ||
findMyWay.on('GET', '/test/S/:file(^\\S+).png', () => { | ||
t.ok('regex match') | ||
}) | ||
findMyWay.on('GET', '/test/D/:file(^\\D+).png', () => { | ||
t.ok('regex match') | ||
}) | ||
findMyWay.lookup({ method: 'GET', url: '/test/S/foo.png', headers: {} }, null) | ||
findMyWay.lookup({ method: 'GET', url: '/test/D/foo.png', headers: {} }, null) | ||
}) | ||
test('nested route with matching regex', t => { | ||
@@ -39,0 +56,0 @@ t.plan(1) |
Sorry, the diff of this file is not supported yet
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
270250
0.09%3
-25%6931
0.3%574
-0.69%- Removed
- Removed