Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize constraints' strategies #246

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 3 additions & 7 deletions README.md
Expand Up @@ -161,9 +161,7 @@ const customResponseTypeStrategy = {
let handlers = {}
return {
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 }
}
},
// function to get the value of the constraint from each incoming request
Expand Down Expand Up @@ -205,9 +203,7 @@ const customVersioning = {
let versions = {}
return {
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 }
}
},
deriveConstraint: (req, ctx) => {
Expand Down Expand Up @@ -494,7 +490,7 @@ console.log(findMyWay.prettyPrint({ commonPrefix: false }))
// └── update (PUT)
```

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
displayed, this can also be set to an array specifying which keys (if
present) should be displayed. This information can be further sanitized
Expand Down
22 changes: 6 additions & 16 deletions lib/strategies/accept-host.js
Expand Up @@ -2,19 +2,17 @@
const assert = require('assert')

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
}
}
},
Expand All @@ -24,14 +22,6 @@ function HostStorage () {
} else {
hosts[host] = value
}
},
del: (host) => {
delete hosts[host]
regexHosts = regexHosts.filter((obj) => String(obj.host) !== String(host))
},
empty: () => {
hosts = {}
regexHosts = []
}
}
}
Expand Down
50 changes: 49 additions & 1 deletion lib/strategies/accept-version.js
@@ -1,8 +1,56 @@
'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 = {
name: 'version',
mustMatchWhenDerived: true,
Expand Down
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -45,8 +45,7 @@
"dependencies": {
"fast-decode-uri-component": "^1.0.1",
"fast-deep-equal": "^3.1.3",
"safe-regex2": "^2.0.0",
"semver-store": "^0.3.0"
"safe-regex2": "^2.0.0"
},
"tsd": {
"directory": "test/types"
Expand Down
26 changes: 0 additions & 26 deletions test/host-storage.test.js
Expand Up @@ -25,29 +25,3 @@ t.test('exact host matches take precendence over regexp matches', async (t) => {
t.equal(storage.get('bar.fastify.io'), 'wildcard')
t.equal(storage.get('auth.fastify.io'), 'exact')
})

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)
})