Skip to content

Commit

Permalink
allow subclasses of router (#1412)
Browse files Browse the repository at this point in the history
  • Loading branch information
bengl committed Jun 7, 2021
1 parent 1e9906d commit 0f3983b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
8 changes: 2 additions & 6 deletions packages/datadog-plugin-router/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,8 @@ function wrapLayerHandle (layer, handle) {
// that contains the real handle function
wrapCallHandle._datadog_orig = handle

// TODO(bengl) copying props like this when wrapping should be done in a centralized place.
const props = Object.getOwnPropertyDescriptors(handle)
for (const key in props) {
if (wrapCallHandle.hasOwnProperty(key)) delete props[key]
}
Object.defineProperties(wrapCallHandle, props)
// TODO(bengl) assigning prototypes like this when wrapping should be done in a centralized place.
Object.setPrototypeOf(wrapCallHandle, handle)

return wrapCallHandle
}
Expand Down
16 changes: 10 additions & 6 deletions packages/datadog-plugin-router/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,24 @@ describe('Plugin', () => {
Router = require(`../../../versions/router@${version}`).get()
})

it('should expose router handle properties', () => {
it('should copy custom prototypes on routers', () => {
const router = Router()
const childRouter = Router()
class ChildRouter extends Router {
get foo () {
return 'bar'
}
}
const childRouter = new ChildRouter()
childRouter.hello = 'goodbye'

childRouter.use('/child/:id', (req, res) => {
res.writeHead(200)
res.end()
})

router.use('/parent', childRouter)
expect(router.stack[0].handle.stack.length).to.equal(1)
// Next two lines are to test the setter.
router.stack[0].handle.stack = 5
expect(router.stack[0].handle.stack).to.equal(5)
expect(router.stack[0].handle.hello).to.equal('goodbye')
expect(router.stack[0].handle.foo).to.equal('bar')
})

it('should add the route to the request span', done => {
Expand Down

0 comments on commit 0f3983b

Please sign in to comment.