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

koa2-router

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa2-router - npm Package Compare versions

Comparing version 1.0.7 to 1.0.8

67

lib/index.js

@@ -24,2 +24,3 @@ /*!

var setPrototypeOf = require('setprototypeof')
var delegates = require('delegates')

@@ -48,2 +49,33 @@ /**

/**
* Add baseUrl/params to the
* prototype of request and context in app
*
* We only need to patch once for a better
* performance
*
* @param {*} app
* @private
*/
function patchPrototype(app) {
const context = app.context
const request = app.request
if (!request.hasOwnProperty('baseUrl')) {
Object.defineProperties(request, {
baseUrl: {
get: function() { return this.req.baseUrl },
set: function(baseUrl) { this.req.baseUrl = baseUrl }
},
params: {
get: function() { return this.req.params },
set: function(params) { this.req.params = params }
}
})
delegates(context, 'request')
.access('baseUrl')
.access('params')
}
}
/**
* Initialize a new `Router` with the given `options`.

@@ -64,30 +96,9 @@ *

function router(ctx, next) {
// set ctx.originalUrl and ctx.req.originalUrl
if (typeof ctx.originalUrl !== 'string') {
Object.defineProperties(ctx, {
originalUrl: {
get: function() { return ctx.req.originalUrl },
set: function(originalUrl) { return ctx.req.originalUrl = originalUrl },
enumerable: true,
configurable: true
},
baseUrl: {
get: function() { return ctx.req.baseUrl },
set: function(baseUrl) { return ctx.req.baseUrl = baseUrl },
enumerable: true,
configurable: true
},
params: {
get: function() { return ctx.params.params },
set: function(params) { return ctx.req.params = params },
enumerable: true,
configurable: true
}
})
// initialize ctx.req with `originalUrl`, `baseUrl`, `params`
ctx.req.originalUrl = ctx.req.originalUrl || ctx.req.url
ctx.req.baseUrl = ctx.req.baseUrl || ''
ctx.req.params = ctx.req.params || {}
}
// patch context/request prototype
patchPrototype(ctx.app)
// initialize ctx.req with `originalUrl`, `baseUrl`, `params`
const req = ctx.req
req.originalUrl = req.originalUrl || req.url
req.baseUrl = req.baseUrl || ''
req.params = req.params || {}
return router.handle(ctx, next)

@@ -94,0 +105,0 @@ }

{
"name": "koa2-router",
"version": "1.0.7",
"version": "1.0.8",
"description": "A express-liked router component for koa2",

@@ -21,2 +21,3 @@ "main": "index.js",

"debug": "^4.1.1",
"delegates": "^1.0.0",
"depd": "^2.0.0",

@@ -23,0 +24,0 @@ "methods": "^1.1.2",

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