Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
feather-route-matcher
Advanced tools
This tiny module exports a single function that takes an object of url patterns and returns a function that can be called to get the matching object, based on the url.
This is in support of experiments I'm doing for building lightweight clientside apps here.
You call createMatcher
and pass it an object of routes where the key is the url pattern (using same matching logic as Backbone.js) and the result is whatever you want to return as a match to that url. Usually this would be a component representing the page you want to show for that url pattern, but it could be anything.
You could, for example, create a module that exports that function like this:
routes.js
import createMatcher from 'feather-route-matcher'
import homePage from './pages/home'
import courseListingPage from './pages/course-listing'
import courseDetailPage from './pages/course-detail'
import notFoundPage from './pages/not-found'
export default createMatcher({
'/': homePage,
'/courses': courseListingPage,
'/courses/:id': courseDetailPage,
'/*': notFoundPage
})
This returns a function that can be called to retrieve the value, along with extracted parameters:
other.js
import routeMatcher from './routes'
// call it with a pathname you want to match
routeMatcher('/')
// =>
// {
// value: homePage,
// url: '/',
// params: null
// }
routeMatcher('/courses/47')
// =>
// {
// value: courseDetailPage,
// url: '/',
// params: {
// id: '47'
// }
// }
routeMatcher('/some-garbage')
// =>
// {
// value: notFoundPage,
// url: '/some/garbage',
// params: {
// // anything matched by a wildcard `*`
// // will return a param named `path`
// // with whatever existed in the location
// // where the `*` was
// path: 'some/garbage'
// }
// }
If you treat the url
in a clientside app as just another piece of application state in a frontend app you'll likely need some sort of switch
statement or set of if
/else
blocks to match the current url with the component you want to show.
That's easy with urls that are known ahead of time, such as /home
but becomes a bit more arduous when you want to see whether it matches a given pattern and want to extract values such as: /user/42
. That's where this module helps with.
The result of the matcher is a great candidate for going into a state store, like redux.
This module could be used as a really lightweight routing system for a react/redux app without the need for React Router.
pattern: '/users/:id'
url: '/something-else'
extracted params: nothing, because it won't match
pattern: '/users/:id'
url: '/users/scrooge-mc-duck'
extracted params: {id: 'scrooge-mc-duck'}
pattern: '/users/:id'
url: '/users/47'
extracted params: {id: '47'}
pattern: '/schools/:schoolId/teachers/:teacherId'
url: '/schools/richland/teachers/47'
extracted params: {schoolId: 'richland', teacherId: '47'}
pattern: *
url: '/asdfas'
extracted params: {path: '/asdfas'}
note: extracted param always called path
pattern: '/schools/*
url: '/schools/richland/teachers/47'
extracted params: {path: 'richland/teachers/47'}
This module borrows a few extremely well-tested regexes from Backbone.js to do its pattern matching. Thanks for the generous licensing!
Things to be aware of...
query-string
to parse query values.npm install feather-route-matcher --save
If you like this follow @HenrikJoreteg on twitter. The regex patterns were borrowed from Backbone.js because they're extremely well tested and I want this to Just Work™.
npm run test
4.0.0
- Breaking changes: In the matched response, the property called page
was renamed value
. Removed fallback
option because it's easy to do in app code using this. Fixed bug where passed in route object was being mutated.
3.1.0
- Non-breaking conversion of ./index.js
into an esm module. umd version remains available as ./feather-route-matcher.js
and ./feather-route-matcher.min.js
3.0.0
- Changed result to now include the pattern that was matched as well.
2.0.1
- Remove accidentally left ES6 usage.
2.0.0
- Instead of expecting the values of object passed to createMatcher
to be function of a certain structure, it now always just returns an object of a pre-determined structure, including the passed url, any extracted params, and a page
key that contains whatever the original value of that key was.
1.0.0
- initial release
FAQs
featherweight url to handler matching
The npm package feather-route-matcher receives a total of 4,802 weekly downloads. As such, feather-route-matcher popularity was classified as popular.
We found that feather-route-matcher demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.