Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

commuter

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

commuter - npm Package Compare versions

Comparing version 2.0.0 to 2.0.1

.github/workflows/ci.yaml

27

index.js

@@ -1,15 +0,15 @@

var url = require('url')
var url = require("url")
var Routes = require('routes')
var extend = require('deep-extend')
var Routes = require("routes")
var extend = require("deep-extend")
module.exports = createRouter
function createRouter (defaultRoute, _root, _verbs) {
function createRouter(defaultRoute, _root, _verbs) {
var routers = {}
var root = _root || ''
var verbs = _verbs || ['get', 'post', 'put', 'patch', 'delete']
var root = _root || ""
var verbs = _verbs || ["get", "post", "put", "patch", "delete"]
var routeFn
verbs.push('any')
verbs.push("any")

@@ -19,6 +19,7 @@ routeFn = function () {

var req = args[0]
var route = req.splats && req.splats.length ?
req.splats[req.splats.length - 1] :
url.parse(req.url).pathname
var method = req.method ? req.method.toLowerCase() : 'any'
var route =
req.splats && req.splats.length
? req.splats[req.splats.length - 1]
: url.parse(req.url).pathname
var method = req.method ? req.method.toLowerCase() : "any"
var result

@@ -42,3 +43,3 @@

} else {
if (typeof defaultRoute === 'function') {
if (typeof defaultRoute === "function") {
defaultRoute.apply(null, args)

@@ -49,3 +50,3 @@

throw new Error('No match for route: ' + route)
throw new Error("No match for route: " + route)
}

@@ -52,0 +53,0 @@ }

{
"name": "commuter",
"version": "2.0.0",
"version": "2.0.1",
"description": "A minimal, composable router that supports sub-routes.",
"main": "index.js",
"scripts": {
"test": "tape test.js"
"lint": "prettier --list-different **/*.js",
"test": "npm run lint && tape test.js"
},

@@ -25,8 +26,12 @@ "repository": {

"dependencies": {
"deep-extend": "^0.3.2",
"routes": "^2.0.0"
"deep-extend": "^0.6.0",
"routes": "^2.1.0"
},
"devDependencies": {
"tape": "^3.0.3"
"prettier": "^2.3.2",
"tape": "^5.3.1"
},
"prettier": {
"semi": false
}
}

@@ -36,4 +36,4 @@ # commuter

router.get('/post*', subrouter)
subrouter.get('/view/:title', onRoute)
router.get('/post/*', subrouter)
subrouter.get('^/?view/:title', onRoute) // optionally leading slash

@@ -51,2 +51,23 @@ // later, a GET request with url '/post/view/some-title'

Even handle the "index" route in your subrouter:
```javascript
var commuter = require('commuter')
var router = commuter()
var subrouter = commuter()
router.get('/post/*', subrouter)
subrouter.get('^$', onRoute) // optionally leading slash
// later, a GET request with url '/post/'
router(req, res)
function onRoute(req, res) {
console.log(req.url) // '/post/'
// handle route...
}
```
## API

@@ -53,0 +74,0 @@

@@ -1,9 +0,9 @@

var test = require('tape')
var commuter = require('./index')
var test = require("tape")
var commuter = require("./index")
test('routes on a request-like object', function (t) {
test("routes on a request-like object", function (t) {
t.plan(1)
var router = commuter()
var req = {url: '/path', method: 'GET'}
var req = { url: "/path", method: "GET" }

@@ -14,3 +14,3 @@ router.get(req.url, onRoute)

function onRoute () {
function onRoute() {
t.ok(true)

@@ -22,13 +22,13 @@

test('routes on a querystring path', function (t) {
test("routes on a querystring path", function (t) {
t.plan(1)
var router = commuter()
var req = {url: '/path?something=true', method: 'GET'}
var req = { url: "/path?something=true", method: "GET" }
router.get('/path', onRoute)
router.get("/path", onRoute)
router(req)
function onRoute () {
function onRoute() {
t.ok(true)

@@ -40,8 +40,8 @@

test('passes through any additional parameters provided', function (t) {
test("passes through any additional parameters provided", function (t) {
t.plan(2)
var router = commuter()
var req = {url: '/path', method: 'GET'}
var res = {ok: function () {}}
var req = { url: "/path", method: "GET" }
var res = { ok: function () {} }
var superflous = {}

@@ -53,3 +53,3 @@

function onRoute (request, response, unnecessary) {
function onRoute(request, response, unnecessary) {
t.strictEqual(res, response)

@@ -62,8 +62,8 @@ t.strictEqual(superflous, unnecessary)

test('calls error function on no match', function (t) {
test("calls error function on no match", function (t) {
t.plan(1)
var router = commuter(onError)
var url = '/path'
var req = {url: '/boop', method: 'GET'}
var url = "/path"
var req = { url: "/boop", method: "GET" }

@@ -74,3 +74,3 @@ router.get(url, noop)

function onError (r) {
function onError(r) {
t.strictEqual(r, req)

@@ -80,8 +80,8 @@ }

test('throws error if no error function provided', function (t) {
test("throws error if no error function provided", function (t) {
t.plan(1)
var router = commuter()
var url = '/path'
var req = {url: '/boop', method: 'GET'}
var url = "/path"
var req = { url: "/boop", method: "GET" }

@@ -92,3 +92,3 @@ router.get(url, noop)

router(req)
} catch(e) {
} catch (e) {
t.ok(e)

@@ -100,8 +100,8 @@

test('places parameters on the request object', function (t) {
test("places parameters on the request object", function (t) {
t.plan(1)
var router = commuter()
var url = '/admin/:panel'
var req = {url: '/admin/home', method: 'GET'}
var url = "/admin/:panel"
var req = { url: "/admin/home", method: "GET" }

@@ -112,4 +112,4 @@ router.get(url, onRoute)

function onRoute (r) {
t.equal('home', r.params.panel)
function onRoute(r) {
t.equal("home", r.params.panel)

@@ -120,3 +120,3 @@ t.end()

test('routes on expected sub-routes', function (t) {
test("routes on expected sub-routes", function (t) {
t.plan(2)

@@ -126,12 +126,12 @@

var subRouter = commuter()
var req = {url: '/admin/section/home', method: 'GET'}
var res = {ok: function () {}}
var req = { url: "/admin/section/home", method: "GET" }
var res = { ok: function () {} }
router.get('/admin*', subRouter)
subRouter.get('/section/:panel', onRoute)
router.get("/admin/*", subRouter)
subRouter.get("section/:panel", onRoute)
router(req, res)
function onRoute (request, response) {
t.equal(request.params.panel, 'home')
function onRoute(request, response) {
t.equal(request.params.panel, "home")
t.strictEqual(response, res)

@@ -143,3 +143,44 @@

test('routes on expected sub-routes with query params', function (t) {
test("can place empty route in sub-route", function (t) {
t.plan(1)
var router = commuter()
var subRouter = commuter()
var req = { url: "/admin/", method: "GET" }
var res = { ok: function () {} }
router.get("/admin/*", subRouter)
subRouter.get("^$", onRoute)
router(req, res)
function onRoute(request, response) {
t.strictEqual(response, res)
t.end()
}
})
test("can handle optional slashes", function (t) {
t.plan(4)
var router = commuter()
var subRouter = commuter()
var req1 = { url: "admin/section/home", method: "GET" }
var req2 = { url: "/admin/section/home", method: "GET" }
var res = { ok: function () {} }
router.get("^/?admin/*", subRouter)
subRouter.get("^/?section/:panel", onRoute)
router(req1, res)
router(req2, res)
function onRoute(request, response) {
t.equal(request.params.panel, "home")
t.strictEqual(response, res)
}
})
test("routes on expected sub-routes with query params", function (t) {
t.plan(2)

@@ -149,12 +190,12 @@

var subRouter = commuter()
var req = {url: '/admin/section/home?something=true', method: 'GET'}
var res = {ok: function () {}}
var req = { url: "/admin/section/home?something=true", method: "GET" }
var res = { ok: function () {} }
router.get('/admin*', subRouter)
subRouter.get('/section/:panel', onRoute)
router.get("/admin/*", subRouter)
subRouter.get("section/:panel", onRoute)
router(req, res)
function onRoute (request, response) {
t.equal(request.params.panel, 'home')
function onRoute(request, response) {
t.equal(request.params.panel, "home")
t.strictEqual(response, res)

@@ -166,13 +207,13 @@

test('strips root from url when provided', function (t) {
test("strips root from url when provided", function (t) {
t.plan(1)
var router = commuter(null, '/strip')
var req = {url: '/strip/admin/section/home', method: 'GET'}
var router = commuter(null, "/strip")
var req = { url: "/strip/admin/section/home", method: "GET" }
router.get('/admin*', onRoute)
router.get("/admin/*", onRoute)
router(req)
function onRoute (request) {
function onRoute(request) {
t.ok(request)

@@ -184,17 +225,17 @@

test('root url does not affect sub-routes', function (t) {
test("root url does not affect sub-routes", function (t) {
t.plan(2)
var router = commuter(null, '/strip')
var router = commuter(null, "/strip")
var subRouter = commuter()
var req = {url: '/strip/admin/section/home', method: 'GET'}
var res = {ok: function () {}}
var req = { url: "/strip/admin/section/home", method: "GET" }
var res = { ok: function () {} }
router.get('/admin*', subRouter)
subRouter.get('/section/:panel', onRoute)
router.get("/admin/*", subRouter)
subRouter.get("section/:panel", onRoute)
router(req, res)
function onRoute (request, response) {
t.equal(request.params.panel, 'home')
function onRoute(request, response) {
t.equal(request.params.panel, "home")
t.strictEqual(response, res)

@@ -206,7 +247,7 @@

test('arbitrary verbs can be defined', function (t) {
test("arbitrary verbs can be defined", function (t) {
t.plan(1)
var router = commuter(null, null, ['leap'])
var req = {url: '/path', method: 'leap'}
var router = commuter(null, null, ["leap"])
var req = { url: "/path", method: "leap" }

@@ -217,3 +258,3 @@ router.leap(req.url, onRoute)

function onRoute () {
function onRoute() {
t.ok(true)

@@ -228,4 +269,4 @@

var router = commuter(null, null, ['leap'])
var req = {url: '/path', method: 'wut'}
var router = commuter(null, null, ["leap"])
var req = { url: "/path", method: "wut" }

@@ -236,3 +277,3 @@ router.any(req.url, onRoute)

function onRoute () {
function onRoute() {
t.ok(true)

@@ -244,3 +285,3 @@

test('latest defined parameter takes precedence', function (t) {
test("latest defined parameter takes precedence", function (t) {
t.plan(2)

@@ -250,13 +291,13 @@

var subRouter = commuter()
var req = {url: '/admin/item/go/section/home', method: 'GET'}
var res = {ok: function () {}}
var req = { url: "/admin/item/go/section/home", method: "GET" }
var res = { ok: function () {} }
router.get('/admin/:panel/:section/*', subRouter)
subRouter.get('section/:section', onRoute)
router.get("/admin/:panel/:section/*", subRouter)
subRouter.get("section/:section", onRoute)
router(req, res)
function onRoute (request, response) {
t.equal(request.params.panel, 'item')
t.equal(request.params.section, 'home')
function onRoute(request, response) {
t.equal(request.params.panel, "item")
t.equal(request.params.section, "home")

@@ -267,4 +308,4 @@ t.end()

function noop () {
function noop() {
//
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc