2.7.121 • Published 3 months ago

@taktikorg/nihil-itaque-nesciunt v2.7.121

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

@taktikorg/nihil-itaque-nesciunt

js-standard-style Node CI NPM downloads

A crazy fast HTTP router, internally uses an highly performant Radix Tree (aka compact Prefix Tree), supports route params, wildcards, and it's framework independent.

If you want to see a benchmark comparison with the most commonly used routers, see here. Do you need a real-world example that uses this router? Check out Fastify or Restify.

Install

npm i @taktikorg/nihil-itaque-nesciunt --save

Usage

const http = require('http')
const router = require('@taktikorg/nihil-itaque-nesciunt')()

router.on('GET', '/', (req, res, params) => {
  res.end('{"message":"hello world"}')
})

const server = http.createServer((req, res) => {
  router.lookup(req, res)
})

server.listen(3000, err => {
  if (err) throw err
  console.log('Server listening on: http://localhost:3000')
})

API

FindMyWay(options)

Instance a new router. You can pass a default route with the option defaultRoute.

const router = require('@taktikorg/nihil-itaque-nesciunt')({
  defaultRoute: (req, res) => {
    res.statusCode = 404
    res.end()
  }
})

In case of a badly formatted url (eg: /hello/%world), by default @taktikorg/nihil-itaque-nesciunt will invoke the defaultRoute, unless you specify the onBadUrl option:

const router = require('@taktikorg/nihil-itaque-nesciunt')({
  onBadUrl: (path, req, res) => {
    res.statusCode = 400
    res.end(`Bad path: ${path}`)
  }
})

Trailing slashes can be ignored by supplying the ignoreTrailingSlash option:

const router = require('@taktikorg/nihil-itaque-nesciunt')({
  ignoreTrailingSlash: true
})
function handler (req, res, params) {
  res.end('foo')
}
// maps "/foo/" and "/foo" to `handler`
router.on('GET', '/foo/', handler)

Duplicate slashes can be ignored by supplying the ignoreDuplicateSlashes option:

const router = require('@taktikorg/nihil-itaque-nesciunt')({
  ignoreDuplicateSlashes: true
})
function handler (req, res, params) {
  res.end('foo')
}
// maps "/foo", "//foo", "///foo", etc to `handler`
router.on('GET', '////foo', handler)

Note that when ignoreTrailingSlash and ignoreDuplicateSlashes are both set to true, duplicate slashes will first be removed and then trailing slashes will, meaning //a//b//c// will be converted to /a/b/c.

You can set a custom length for parameters in parametric (standard, regex and multi) routes by using maxParamLength option, the default value is 100 characters. If the maximum length limit is reached, the default route will be invoked.

const router = require('@taktikorg/nihil-itaque-nesciunt')({
  maxParamLength: 500
})

If you are using a regex based route, @taktikorg/nihil-itaque-nesciunt will throw an error if detects potentially catastrophic exponential-time regular expressions (internally uses safe-regex2). If you want to disable this behavior, pass the option allowUnsafeRegex.

const router = require('@taktikorg/nihil-itaque-nesciunt')({
  allowUnsafeRegex: true
})

According to RFC3986, @taktikorg/nihil-itaque-nesciunt is case sensitive by default. You can disable this by setting the caseSensitive option to false: in that case, all paths will be matched as lowercase, but the route parameters or wildcards will maintain their original letter casing. You can turn off case sensitivity with:

const router = require('@taktikorg/nihil-itaque-nesciunt')({
  caseSensitive: false
})

The default query string parser that @taktikorg/nihil-itaque-nesciunt uses is fast-querystring module. You can change this default setting by passing the option querystringParser and use a custom one, such as qs.

const qs = require('qs')
const router = require('@taktikorg/nihil-itaque-nesciunt')({
  querystringParser: str => qs.parse(str)
})

router.on('GET', '/', (req, res, params, store, searchParams) => {
  assert.equal(searchParams, { foo: 'bar', baz: 'faz' })
})

router.lookup({ method: 'GET', url: '/?foo=bar&baz=faz' }, null)

According to RFC3986, @taktikorg/nihil-itaque-nesciunt separates path and query string with ? character. But earlier versions also used ; as delimiter character. To support this behaviour, add the useSemicolonDelimiter option to true:

const router = require('@taktikorg/nihil-itaque-nesciunt')({
  useSemicolonDelimiter: true
})

You can assign a buildPrettyMeta function to sanitize a route's store object to use with the prettyPrint functions. This function should accept a single object and return an object.

const privateKey = new Symbol('private key')
const store = { token: '12345', [privateKey]: 'private value' }

const router = require('@taktikorg/nihil-itaque-nesciunt')({
  buildPrettyMeta: route => {
    const cleanMeta = Object.assign({}, route.store)

    // remove private properties
    Object.keys(cleanMeta).forEach(k => {
      if (typeof k === 'symbol') delete cleanMeta[k]
    })

    return cleanMeta // this will show up in the pretty print output!
  }
})

store[privateKey] = 'private value'
router.on('GET', '/hello_world', (req, res) => {}, store)

router.prettyPrint()

//└── / (-)
//    └── hello_world (GET)
//        • (token) "12345"

on(method, path, opts, handler, store)

Register a new route.

router.on('GET', '/example', (req, res, params, store, searchParams) => {
  // your code
})

Last argument, store is used to pass an object that you can access later inside the handler function. If needed, store can be updated.

router.on('GET', '/example', (req, res, params, store) => {
  assert.equal(store, { message: 'hello world' })
}, { message: 'hello world' })
Versioned routes

If needed, you can provide a version route constraint, which will allow you to declare multiple versions of the same route that are used selectively when requests ask for different version using the Accept-Version header. This is useful if you want to support several different behaviours for a given route and different clients select among them.

If you never configure a versioned route, the 'Accept-Version' header will be ignored. Remember to set a Vary header in your responses with the value you are using for defining the versioning (e.g.: 'Accept-Version'), to prevent cache poisoning attacks. You can also configure this as part your Proxy/CDN.

default

The default versioning strategy follows the semver specification. When using lookup, @taktikorg/nihil-itaque-nesciunt will automatically detect the Accept-Version header and route the request accordingly. Internally @taktikorg/nihil-itaque-nesciunt uses the semver-store to get the correct version of the route; advanced ranges and pre-releases currently are not supported.

Be aware that using this feature will cause a degradation of the overall performances of the router.

router.on('GET', '/example', { constraints: { version: '1.2.0' }}, (req, res, params) => {
  res.end('Hello from 1.2.0!')
})

router.on('GET', '/example', { constraints: { version: '2.4.0' }}, (req, res, params) => {
  res.end('Hello from 2.4.0!')
})

// The 'Accept-Version' header could be '1.2.0' as well as '*', '2.x' or '2.4.x'

If you declare multiple versions with the same major or minor @taktikorg/nihil-itaque-nesciunt will always choose the highest compatible with the Accept-Version header value.

custom

It's also possible to define a custom versioning strategy during the @taktikorg/nihil-itaque-nesciunt initialization. In this case the logic of matching the request to the specific handler depends on the versioning strategy you use.

on(methods[], path, opts, handler, store)

Register a new route for each method specified in the methods array. It comes handy when you need to declare multiple routes with the same handler but different methods.

router.on(['GET', 'POST'], '/example', (req, res, params) => {
  // your code
})

Supported path formats

To register a parametric path, use the colon before the parameter name. For wildcard use the star. Remember that static routes are always inserted before parametric and wildcard.

// parametric
router.on('GET', '/example/:userId', (req, res, params) => {}))
router.on('GET', '/example/:userId/:secretToken', (req, res, params) => {}))

// wildcard
router.on('GET', '/example/*', (req, res, params) => {}))

Regular expression routes are supported as well, but pay attention, RegExp are very expensive in term of performance! If you want to declare a regular expression route, you must put the regular expression inside round parenthesis after the parameter name.

// parametric with regexp
router.on('GET', '/example/:file(^\\d+).png', () => {}))

It's possible to define more than one parameter within the same couple of slash ("/"). Such as:

router.on('GET', '/example/near/:lat-:lng/radius/:r', (req, res, params) => {}))

Remember in this case to use the dash ("-") as parameters separator.

Finally it's possible to have multiple parameters with RegExp.

router.on('GET', '/example/at/:hour(^\\d{2})h:minute(^\\d{2})m', (req, res, params) => {}))

In this case as parameter separator it's possible to use whatever character is not matched by the regular expression.

The last parameter can be made optional if you add a question mark ("?") at the end of the parameters name.

router.on('GET', '/example/posts/:id?', (req, res, params) => {}))

In this case you can request /example/posts as well as /example/posts/1. The optional param will be undefined if not specified.

Having a route with multiple parameters may affect negatively the performance, so prefer single parameter approach whenever possible, especially on routes which are on the hot path of your application.

Note that you must encode the parameters containing reserved characters.

Match order

The routing algorithm matches one node at a time (where the node is a string between two slashes), this means that it cannot know if a route is static or dynamic until it finishes to match the URL.

The nodes are matched in the following order:

  1. static
  2. parametric node with static ending
  3. parametric(regex)/multi-parametric
  4. parametric
  5. wildcard

So if you declare the following routes

  • /foo/filename.png - static route
  • /foo/:filename.png - route with param filename and static ending .png
  • /foo/:filename.:ext - route with two params filename and ext
  • /foo/:filename - route with one param filename
  • /* - wildcard route

You will have next matching rules:

  • the static node would have the highest priority. It will be matched only if incoming URL equals /foo/filename.png
  • the parametric node with a static ending would have the higher priority than other parametric nodes without it. This node would match any filenames with .png extension. If one node static ending ends with another node static ending, the node with a longer static ending would have higher priority.
    • /foo/:filename.png.png - higher priority, more specific route
    • /foo/:filename.png - lower priority
  • the multi-parametric node (or any regexp node) without static ending would have lower priority than parametric node with static ending and higher priority than generic parametric node. You can declare only one node like that for the same route (see caveats). It would match any filenames with any extensions.
  • the parametric node has lower priority than any other parametric node. It would match any filenames, even if they don't have an extension.
  • the wildcard node has the lowest priority of all nodes.

Once a url has been matched, @taktikorg/nihil-itaque-nesciunt will figure out which handler registered for that path matches the request if there are any constraints. @taktikorg/nihil-itaque-nesciunt will check the most constrained handlers first, which means the handlers with the most keys in the constraints object.

If you just want a path containing a colon without declaring a parameter, use a double colon. For example, /name::customVerb will be interpreted as /name:customVerb

Supported methods

The router is able to route all HTTP methods defined by http core module.

off(methods[], path, constraints)

Used to deregister routes.

off(methods, path)

If no constraint argument is passed, all routes with identical path and method are deregistered, regardless of whether a route has constraints or not.

router.on('GET', '/example', { constraints: { host: 'fastify.io' } })
router.on('GET', '/example', { constraints: { version: '1.x' } })
router.on('GET', '/example')

// Deregisters all 3 routes registered above
router.off('GET', '/example')
off(methods, path, constraints)

If a constraint object is specified, only those routes are deleted that have the same constraints as well as the identical path and method. If an empty object is passed, only unconstrained routes will be deleted.

router.on('GET', '/example', { constraints: { host: 'fastify.io' } })
router.on('GET', '/example', { constraints: { version: '1.x' } })
router.on('GET', '/example')

// Deregisters only the third route without constraints
router.off('GET', '/example', {})

// Deregisters only the first route
router.off('GET', '/example', { host: 'fastify.io' })
off(methods[], path)

Deregister a route for each method specified in the methods array. It comes handy when you need to deregister multiple routes with the same path but different methods. As explained above, the constraints will be ignored here.

router.on('GET', '/example', { constraints: { host: 'fastify.io' } })
router.on('POST', '/example', { constraints: { version: '1.x' } })
router.on('PUT', '/example')

// Deregisters all 3 routes registered above
router.off(['POST', 'GET', 'PUT'], '/example')
off(methods[], path, constraints)
router.on('GET', '/example', { constraints: { host: 'fastify.io' } }) // first route
router.on('POST', '/example', { constraints: { host: 'fastify.io' } }) // second route
router.on('POST', '/example', { constraints: { host: 'google.de' } }) // third route
router.on('GET', '/example') // fourth route 
router.on('POST', '/example') // fifth route 

// Deregisters only first and second route
router.off(['POST', 'GET'], '/example', { host: 'fastify.io' })

// Deregisters only fourth and fifth route
router.off(['POST', 'GET'], '/example', {})

findRoute (method, path, constraints)

Finds a route by server route's path (not like find which finds a route by the url). Returns the route object if found, otherwise returns null. findRoute does not compare routes paths directly, instead it compares only paths patters. This means that findRoute will return a route even if the path passed to it does not match the route's path exactly. For example, if a route is registered with the path /example/:param1, findRoute will return the route if the path passed to it is /example/:param2.

const handler = (req, res, params) => {
  res.end('Hello World!')
}
router.on('GET', '/:file(^\\S+).png', handler)

router.findRoute('GET', '/:file(^\\S+).png')
// => { handler: Function, store: Object, params: ['file'] }

router.findRoute('GET', '/:file(^\\D+).jpg')
// => null
const handler = (req, res, params) => {
  res.end('Hello World!')
}
router.on('GET', '/:param1', handler)

router.findRoute('GET', '/:param1')
// => { handler: Function, store: Object, params: ['param1'] }

router.findRoute('GET', '/:param2')
// => { handler: Function, store: Object, params: ['param1'] }

hasRoute (method, path, constraints)

Checks if a route exists by server route's path (see findRoute for more details). Returns true if found, otherwise returns false.

router.on('GET', '/:file(^\\S+).png', handler)

router.hasRoute('GET', '/:file(^\\S+).png')
// => true

router.hasRoute('GET', '/:file(^\\D+).jpg')
// => false

lookup(request, response, context, done)

Start a new search, request and response are the server req/res objects. If a route is found it will automatically call the handler, otherwise the default route will be called. The url is sanitized internally, all the parameters and wildcards are decoded automatically.

router.lookup(req, res)

lookup accepts an optional context which will be the value of this when executing a handler

router.on('GET', '*', function(req, res) {
  res.end(this.greeting);
})
router.lookup(req, res, { greeting: 'Hello, World!' })

lookup accepts an optional done callback for case when you use an async deriveConstraint function.

router.on('GET', '*', function(req, res) {
  res.end({ hello: 'world' });
})
router.lookup(req, res, (err) => {
  if (err !== null) {
    // handle error
  }
  console.log('Handler executed!!!'); 
})

find(method, path, constraints)

Return (if present) the route registered in method:path. The path must be sanitized, all the parameters and wildcards are decoded automatically. An object with routing constraints should usually be passed as constraints, containing keys like the host for the request, the version for the route to be matched, or other custom constraint values. If the router is using the default versioning strategy, the version value should be conform to the semver specification. If you want to use the existing constraint strategies to derive the constraint values from an incoming request, use lookup instead of find. If no value is passed for constraints, the router won't match any constrained routes. If using constrained routes, passing undefined for the constraints leads to undefined behavior and should be avoided.

router.find('GET', '/example', { host: 'fastify.io' })
// => { handler: Function, params: Object, store: Object}
// => null

router.find('GET', '/example', { host: 'fastify.io', version: '1.x' })
// => { handler: Function, params: Object, store: Object}
// => null

prettyPrint([{ commonPrefix: false, includeMeta: true || [] }])

@taktikorg/nihil-itaque-nesciunt builds a tree of routes for each HTTP method. If you call the prettyPrint without specifying an HTTP method, it will merge all the trees to one and print it. The merged tree does't represent the internal router structure. Don't use it for debugging.

findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test/hello', () => {})
findMyWay.on('GET', '/testing', () => {})
findMyWay.on('GET', '/testing/:param', () => {})
findMyWay.on('PUT', '/update', () => {})

console.log(findMyWay.prettyPrint())
// └── /
//     ├── test (GET)
//     │   ├── /hello (GET)
//     │   └── ing (GET)
//     │       └── /
//     │           └── :param (GET)
//     └── update (PUT)

If you want to print the internal tree, you can specify the method param. Printed tree will represent the internal router structure. Use it for debugging.

findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test/hello', () => {})
findMyWay.on('GET', '/testing', () => {})
findMyWay.on('GET', '/testing/:param', () => {})
findMyWay.on('PUT', '/update', () => {})

console.log(findMyWay.prettyPrint({ method: 'GET' }))
// └── /
//     └── test (GET)
//         ├── /hello (GET)
//         └── ing (GET)
//             └── /
//                 └── :param (GET)

console.log(findMyWay.prettyPrint({ method: 'PUT' }))
// └── /
//     └── update (PUT)

prettyPrint accepts an optional setting to print compressed routes. This is useful when you have a large number of routes with common prefixes. Doesn't represent the internal router structure. Don't use it for debugging.

console.log(findMyWay.prettyPrint({ commonPrefix: false }))
// ├── /test (GET)
// │   ├── /hello (GET)
// │   └── ing (GET)
// │       └── /:param (GET)
// └── /update (PUT)

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 by specifying a buildPrettyMeta function which consumes and returns an object.

findMyWay.on('GET', '/test', () => {}, { onRequest: () => {}, authIDs: [1, 2, 3] })
findMyWay.on('GET', '/test/hello', () => {}, { token: 'df123-4567' })
findMyWay.on('GET', '/testing', () => {})
findMyWay.on('GET', '/testing/:param', () => {})
findMyWay.on('PUT', '/update', () => {})

console.log(findMyWay.prettyPrint({ commonPrefix: false, includeMeta: ['onRequest'] }))
// ├── /test (GET)
// │   • (onRequest) "onRequest()"
// │   ├── /hello (GET)
// │   └── ing (GET)
// │       └── /:param (GET)
// └── /update (PUT)

console.log(findMyWay.prettyPrint({ commonPrefix: false, includeMeta: true }))
// ├── /test (GET)
// │   • (onRequest) "onRequest()"
// │   • (authIDs) [1,2,3]
// │   ├── /hello (GET)
// │   │   • (token) "df123-4567"
// │   └── ing (GET)
// │       └── /:param (GET)
// └── /update (PUT)

reset()

Empty router.

router.reset()

routes

Return the all routes registered at moment, useful for debugging.

const findMyWay = require('@taktikorg/nihil-itaque-nesciunt')()

findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test/hello', () => {})

console.log(findMyWay.routes)
// Will print
// [
//   {
//     method: 'GET',
//     path: '/test',
//     opts: {},
//     handler: [Function],
//     store: undefined
//   },
//   {
//     method: 'GET',
//     path: '/test/hello',
//     opts: {},
//     handler: [Function],
//     store: undefined
//   }
// ]

Caveats

  • It's not possible to register two routes which differs only for their parameters, because internally they would be seen as the same route. In a such case you'll get an early error during the route registration phase. An example is worth thousand words:
const findMyWay = FindMyWay({
  defaultRoute: (req, res) => {}
})

findMyWay.on('GET', '/user/:userId(^\\d+)', (req, res, params) => {})

findMyWay.on('GET', '/user/:username(^[a-z]+)', (req, res, params) => {})
// Method 'GET' already declared for route ':'

Shorthand methods

If you want an even nicer api, you can also use the shorthand methods to declare your routes.

For each HTTP supported method, there's the shorthand method. For example:

router.get(path, handler [, store])
router.delete(path, handler [, store])
router.head(path, handler [, store])
router.patch(path, handler [, store])
router.post(path, handler [, store])
router.put(path, handler [, store])
router.options(path, handler [, store])
// ...

If you need a route that supports all methods you can use the all api.

router.all(path, handler [, store])

Constraints

@taktikorg/nihil-itaque-nesciunt supports restricting handlers to only match certain requests for the same path. This can be used to support different versions of the same route that conform to a semver based versioning strategy, or restricting some routes to only be available on hosts. @taktikorg/nihil-itaque-nesciunt has the semver based versioning strategy and a regex based hostname constraint strategy built in.

To constrain a route to only match sometimes, pass constraints to the route options when registering the route:

findMyWay.on('GET', '/', { constraints: { version: '1.0.2' } }, (req, res) => {
  // will only run when the request's Accept-Version header asks for a version semver compatible with 1.0.2, like 1.x, or 1.0.x.
})

findMyWay.on('GET', '/', { constraints: { host: 'example.com' } }, (req, res) => {
  // will only run when the request's Host header is `example.com`
})

Constraints can be combined, and route handlers will only match if all of the constraints for the handler match the request. @taktikorg/nihil-itaque-nesciunt does a boolean AND with each route constraint, not an OR.

@taktikorg/nihil-itaque-nesciunt will try to match the most constrained handlers first before handler with fewer or no constraints.

Custom Constraint Strategies

Custom constraining strategies can be added and are matched against incoming requests while trying to maintain @taktikorg/nihil-itaque-nesciunt's high performance. To register a new type of constraint, you must add a new constraint strategy that knows how to match values to handlers, and that knows how to get the constraint value from a request. Register strategies when constructing a router or use the addConstraintStrategy method.

Add a custom constrain strategy when constructing a router:

const customResponseTypeStrategy = {
  // strategy name for referencing in the route handler `constraints` options
  name: 'accept',
  // storage factory for storing routes in the @taktikorg/nihil-itaque-nesciunt route tree
  storage: function () {
    let handlers = {}
    return {
      get: (type) => { return handlers[type] || null },
      set: (type, store) => { handlers[type] = store }
    }
  },
  // function to get the value of the constraint from each incoming request
  deriveConstraint: (req, ctx) => {
    return req.headers['accept']
  },
  // optional flag marking if handlers without constraints can match requests that have a value for this constraint
  mustMatchWhenDerived: true
}

const router = FindMyWay({ constraints: { accept: customResponseTypeStrategy } });

Add an async custom constrain strategy when constructing a router:

const asyncCustomResponseTypeStrategy = {
  // strategy name for referencing in the route handler `constraints` options
  name: 'accept',
  // storage factory for storing routes in the @taktikorg/nihil-itaque-nesciunt route tree
  storage: function () {
    let handlers = {}
    return {
      get: (type) => { return handlers[type] || null },
      set: (type, store) => { handlers[type] = store }
    }
  },
  // function to get the value of the constraint from each incoming request
  deriveConstraint: (req, ctx, done) => {
    done(null, req.headers['accept'])
  },
  // optional flag marking if handlers without constraints can match requests that have a value for this constraint
  mustMatchWhenDerived: true
}

const router = FindMyWay({ constraints: { accept: asyncCustomResponseTypeStrategy } });

Add a custom constraint strategy using the addConstraintStrategy method:

const customResponseTypeStrategy = {
  // strategy name for referencing in the route handler `constraints` options
  name: 'accept',
  // storage factory for storing routes in the @taktikorg/nihil-itaque-nesciunt route tree
  storage: function () {
    let handlers = {}
    return {
      get: (type) => { return handlers[type] || null },
      set: (type, store) => { handlers[type] = store }
    }
  },
  // function to get the value of the constraint from each incoming request
  deriveConstraint: (req, ctx) => {
    return req.headers['accept']
  },
  // optional flag marking if handlers without constraints can match requests that have a value for this constraint
  mustMatchWhenDerived: true
}

const router = FindMyWay();
router.addConstraintStrategy(customResponseTypeStrategy);

Once a custom constraint strategy is registered, routes can be added that are constrained using it:

findMyWay.on('GET', '/', { constraints: { accept: 'application/fancy+json' } }, (req, res) => {
  // will only run when the request's Accept header asks for 'application/fancy+json'
})

findMyWay.on('GET', '/', { constraints: { accept: 'application/fancy+xml' } }, (req, res) => {
  // will only run when the request's Accept header asks for 'application/fancy+xml'
})

Constraint strategies should be careful to make the deriveConstraint function performant as it is run for every request matched by the router. See the lib/strategies directory for examples of the built in constraint strategies.

By default, @taktikorg/nihil-itaque-nesciunt uses a built in strategies for the version constraint that uses semantic version based matching logic, which is detailed below. It is possible to define an alternative strategy:

const customVersioning = {
  // replace the built in version strategy
  name: 'version',
  // provide a storage factory to store handlers in a simple way
  storage: function () {
    let versions = {}
    return {
      get: (version) => { return versions[version] || null },
      set: (version, store) => { versions[version] = store }
    }
  },
  deriveConstraint: (req, ctx) => {
    return req.headers['accept']
  },
  mustMatchWhenDerived: true, // if the request is asking for a version, don't match un-version-constrained handlers
  validate (value) {  // optional validate function, validates the assigned value at route-configuration (the .on function) time (not the runtime-value)
    assert(typeof value === 'string', 'Version should be a string')
  }
}

const router = FindMyWay({ constraints: { version: customVersioning } });

The custom strategy object should contain next properties:

  • storage - a factory function to store lists of handlers for each possible constraint value. The storage object can use domain-specific storage mechanisms to store handlers in a way that makes sense for the constraint at hand. See lib/strategies for examples, like the version constraint strategy that matches using semantic versions, or the host strategy that allows both exact and regex host constraints.
  • deriveConstraint - the function to determine the value of this constraint given a request

The signature of the functions and objects must match the one from the example above.

Please, be aware, if you use your own constraining strategy - you use it on your own risk. This can lead both to the performance degradation and bugs which are not related to @taktikorg/nihil-itaque-nesciunt itself!

Acknowledgements

It is inspired by the echo router, some parts have been extracted from trekjs router.

Past sponsor

License

@taktikorg/nihil-itaque-nesciunt - MIT trekjs/router - MIT

Copyright © 2017-2019 Tomas Della Vedova

CSScryptoCSSStyleDeclarationlanguagehttpsflattenslotdatastructurelistenersinspectjestIteratorstdlibdeep-copyjoidomstyledataviewconvertexecutableworkerSymbol.toStringTag[[Prototype]]requireMapmimetypeswrapmonorepolinkqueryobjtestingesSetes5dirdescriptiondebugnameECMAScript 2019inferencerm -frcurlgroupBy_.extendprotobufzeropackage.jsoncliec2writableassertsisConcatSpreadablespeediamiedependencies$.extendECMAScript 3spinnerObject.keysES5RegExp.prototype.flagscssES8xtermtoArray.envrfc4122lruequalityairbnbwarningPushfilebundlingmake dirfscollection.es6syntaxboundsidetoolsasyncmatchAllvpcpromiseoffsetexpresscloudsearchi18nES2020Reflect.getPrototypeOfgetemitcharsetUint8ClampedArrayplugincolourdeepreal-timelookfile systems3multi-packageUint32ArraytoobjectFunction.prototype.namepersistenttaskcheckfunctionalfull-widthwhatwgbrowserslisticuevents@@toStringTagrgbredux-toolkitcollectiondependency managerpreserve-symlinksECMAScript 2022TypeBoxautoscalingArrayBuffer#sliceReactiveXes2017toolkitfunctionsframeworkminimalECMAScript 2023ES2023mime-dbtddlastfpsvaluesgetintrinsicwgetformattingJSON-SchemalintponyfillparsingRFC-6455es7matchclass-validatorbuffersECMAScript 2018trimStart0urldefaultavahelperes6startpackagesspinnersmergejsonawsslicewordwrapRxJStypedarraysamazones-shimshooksObject.isassertelbsescloudtrailansiasciisameValueZerobyteLengthrmECMAScript 2016censortesterStreamsmobilemodulestrimEndnamesstructuredClonedeepcopyescapeFloat32Arrayelectronredirectschemegenericsinternal slot3dES2018uninstallECMAScript 7browserReactiveExtensionsroutelinewrapwordbreakcachepipedebuggerarktypeprettyprunetextelasticacheclonerandomglobalonceshampushfastcopyoptimistloggingmakecreateStyleSheetsortedhelpersutilityObject.getPrototypeOfgetPrototypeOfxdgObject.valuessomedatadom-testing-librarybreakmapreducemovefullTypedArrayObject.assignregexhasfolderajaxqsdynamodbttymatcheses2018fast-deep-copyscheme-validationphoneroutingstreamsYAMLObject.definePropertyartwaapiconsolegetoptprivatetypedfast-copyinstallproxystyled-componentscomputed-typesguidprefixstringimportyamlassertionajvenderargspolyfillopentapexpressionarraybuffernoperatelimitString.prototype.matchAlldotenvdeleteperformanteverydefineparsergetteroption-0HyBiflatJSONdeterministicformatreact animationconcurrencyinsuperstructstoragegatewaytc39typescriptutil.inspectbundlertapenodejschinesemkdirstslibjsdomfilterargumentscommand-linehasOwnvariablesloadingcloudfrontArray.prototype.findLastIndexbannerUint16ArrayjsdiffstringifiersetspecfindLastIndexidentifierssetImmediatewafbddfindLastnodelaunchenvironmentreduceres-shim APIperformancestylinges-abstractcircularsymlinkgroupES2019typeoffunctionintrinsicentriescloudformationES2015mochaObject.fromEntriesbatchtypeerrorprocessprototypesearchreadableoutputECMAScript 2015es2016es2015weaksetstreamxdg-openeslintdataViewpyyamlstylesWebSocketssharedarraybuffererrorpnpm9chromeObservablescompilercoercibleextendWeakSetqueuegetOwnPropertyDescriptorrdscompareTypeScriptconfigurable__proto__typedarraykarmaiconvtsspringpropertiesxhridagentindicatorpositivejscss-in-jslengthistraverseURLSearchParamsformsaccessibilityexemkdirpcommanderpostcsscoretostringtagurlsassignrobustmkdirArrayidlecolumntypesnegative zerocolordayjsconcathardlinksautoprefixerextensionregular expressionpatchsqsownswfrapidlook-upcallbackparentsstringifymimewidthdatetouchbufferlogtoSortedenumerableECMAScript 5beanstalkconsumeform-validationsymlinksarraysspawnserializationcmdRegExp#flagscjkremovelimitreact-hookstesttyped arrayiterateES2017ES3negativecopyresolveglaciercore-jsroute53uuidArray.prototype.flattypeprotocol-buffersvisualArray.prototype.flatMapreactbusyshrinkwrapanimationloggerstreams2rangeerrorvalidateES2021io-tsfindupsequenceArray.prototype.includeschildomitformmruforEachregularsimpledbclientInt8ArrayUnderscorefixed-widthpopmotionESnextjsxchannelenvfastcloneRxrequestefficientMicrosoftstarterlazyequalInt16ArrayasttrimArrayBuffer.prototype.sliceFloat64Arrayes8propwebebsconcatMapgesturesutilitiessetterjasmineglobimmerecmascriptcontainsmodulekeyeast-asian-widthemojiratetypaniondeep-cloneflagdropcode pointsPromiserecursivesymbolthreeshebangviewcolorssafeawaitES2022nativeWebSocketworkflowterminaltakeredacttimeAsyncIteratorrm -rfargparsehookformcall-bindhigher-ordercharactersprotoopenssymbolslimitedcall-boundfind-upcodesargumentweakmapcharactertermBigInt64Arrayeslint-plugindescriptorsincludesyupcloudwatchkeysoptimizerpackage managerUint8Arrayregular expressionsshimfullwidthupinputbyteopenerapollogdprappfunction.lengthglobalsfetchzodhttpvaluenpmpromisesreact-hook-formrmdirObservablejQueryregexpshellschemareusefasttoStringTagthrottleinstallerruntimepathduplexwriteSymbolprivate dataiteratorsetPrototypeOfWeakMapsortapiArray.prototype.findLastimmutableparseeslintconfigES2016react-testing-librarypackagebrowserlistframerl10nBigUint64ArrayimportexporteslintpluginhashinvariantcolumnsArray.prototype.filterunicodejavascriptthroatcallboundpostcss-pluginloadbalancingfastifywhichreduxprogressString.prototype.trimestreeexecdiffqueueMicrotaskfigletnumberjsonpatharrayeventDispatcherdescriptorreact posekinesisjapanesechromiumespreeaccessorpropertyreadablestreamwalkingworkspace:*ESserializebluebirdES6walkwebsiteenvironmentsmetadatachaifast-deep-clonestatelessvalidationdefinePropertysnsiterationemrmomentkoreanextrautilreducepicomatchtrimLeftsettingsawesomesauceargvArrayBufferlibphonenumbermapendpointsyntaxerrorfromURLdeepclonetrimRighteditor256a11y
1.1.29

7 months ago

1.1.28

7 months ago

2.6.102

4 months ago

2.2.59

6 months ago

2.2.57

6 months ago

1.1.30

7 months ago

2.2.58

6 months ago

2.2.55

6 months ago

2.2.56

6 months ago

2.2.53

6 months ago

1.1.34

7 months ago

2.2.54

6 months ago

1.1.33

7 months ago

2.2.51

6 months ago

1.1.32

7 months ago

2.2.52

6 months ago

1.1.31

7 months ago

2.2.50

6 months ago

1.1.36

6 months ago

1.1.35

6 months ago

2.2.68

5 months ago

2.2.69

5 months ago

2.2.66

5 months ago

2.2.67

5 months ago

2.2.64

5 months ago

2.2.65

5 months ago

2.2.62

6 months ago

2.2.63

5 months ago

2.2.60

6 months ago

2.2.61

6 months ago

2.7.117

4 months ago

2.7.118

3 months ago

2.7.119

3 months ago

2.7.115

4 months ago

2.7.116

4 months ago

2.7.120

3 months ago

2.7.121

3 months ago

2.2.79

5 months ago

2.2.77

5 months ago

2.2.78

5 months ago

2.4.98

4 months ago

2.2.75

5 months ago

2.4.97

4 months ago

2.2.76

5 months ago

2.2.73

5 months ago

2.4.99

4 months ago

2.2.74

5 months ago

2.2.71

5 months ago

2.2.72

5 months ago

2.4.96

4 months ago

2.4.95

4 months ago

2.2.70

5 months ago

1.1.19

7 months ago

2.1.38

6 months ago

2.1.39

6 months ago

2.1.36

6 months ago

2.1.37

6 months ago

2.5.100

4 months ago

2.5.101

4 months ago

2.5.102

4 months ago

2.2.88

5 months ago

2.2.89

5 months ago

2.2.86

5 months ago

1.1.23

7 months ago

2.2.87

5 months ago

1.1.22

7 months ago

2.2.84

5 months ago

1.1.21

7 months ago

2.2.85

5 months ago

1.1.20

7 months ago

2.2.82

5 months ago

1.1.27

7 months ago

2.2.83

5 months ago

1.1.26

7 months ago

2.2.80

5 months ago

1.1.25

7 months ago

2.2.81

5 months ago

1.1.24

7 months ago

1.0.19

7 months ago

2.2.90

5 months ago

1.0.18

7 months ago

1.0.17

7 months ago

2.1.45

6 months ago

2.1.43

6 months ago

2.1.44

6 months ago

2.1.41

6 months ago

2.1.42

6 months ago

2.1.40

6 months ago

2.2.91

5 months ago

2.2.92

5 months ago

2.6.114

4 months ago

2.6.115

4 months ago

2.6.112

4 months ago

2.6.113

4 months ago

2.6.110

4 months ago

2.6.111

4 months ago

2.4.100

4 months ago

2.2.48

6 months ago

2.2.49

6 months ago

2.2.46

6 months ago

2.2.47

6 months ago

2.2.45

6 months ago

2.6.105

4 months ago

2.6.106

4 months ago

2.6.103

4 months ago

2.6.104

4 months ago

2.6.109

4 months ago

2.3.93

4 months ago

2.3.92

4 months ago

2.6.107

4 months ago

2.3.95

4 months ago

2.6.108

4 months ago

2.3.94

4 months ago

1.0.16

7 months ago

1.0.15

7 months ago

1.0.11

7 months ago

1.0.14

7 months ago

1.0.13

7 months ago

1.0.12

7 months ago

1.0.10

7 months ago

1.0.9

7 months ago

1.0.8

7 months ago

1.0.7

7 months ago

1.0.6

7 months ago

1.0.5

8 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago