Skip to content

Commit

Permalink
Use setprototypeof module to replace __proto__ setting
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Feb 23, 2017
1 parent 12ff56e commit 6022567
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ unreleased
- Improves compatibility with Node.js 8 nightly
* Skip routing when `req.url` is not set
* Use `Object.create` to setup request & response prototypes
* Use `setprototypeof` module to replace `__proto__` setting
* Use `statuses` instead of `http` module for status messages
* deps: debug@2.6.1
- Allow colors in workers
Expand Down
13 changes: 7 additions & 6 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var deprecate = require('depd')('express');
var flatten = require('array-flatten');
var merge = require('utils-merge');
var resolve = require('path').resolve;
var setPrototyeOf = require('setprototypeof')
var slice = Array.prototype.slice;

/**
Expand Down Expand Up @@ -94,10 +95,10 @@ app.defaultConfiguration = function defaultConfiguration() {
}

// inherit protos
this.request.__proto__ = parent.request;
this.response.__proto__ = parent.response;
this.engines.__proto__ = parent.engines;
this.settings.__proto__ = parent.settings;
setPrototyeOf(this.request, parent.request)
setPrototyeOf(this.response, parent.response)
setPrototyeOf(this.engines, parent.engines)
setPrototyeOf(this.settings, parent.settings)
});

// setup locals
Expand Down Expand Up @@ -227,8 +228,8 @@ app.use = function use(fn) {
router.use(path, function mounted_app(req, res, next) {
var orig = req.app;
fn.handle(req, res, function (err) {
req.__proto__ = orig.request;
res.__proto__ = orig.response;
setPrototyeOf(req, orig.request)
setPrototyeOf(res, orig.response)
next(err);
});
});
Expand Down
11 changes: 9 additions & 2 deletions lib/middleware/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

'use strict';

/**
* Module dependencies.
* @private
*/

var setPrototyeOf = require('setprototypeof')

/**
* Initialization middleware, exposing the
* request and response to each other, as well
Expand All @@ -25,8 +32,8 @@ exports.init = function(app){
res.req = req;
req.next = next;

req.__proto__ = app.request;
res.__proto__ = app.response;
setPrototyeOf(req, app.request)
setPrototyeOf(res, app.response)

res.locals = res.locals || Object.create(null);

Expand Down
3 changes: 2 additions & 1 deletion lib/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var debug = require('debug')('express:router');
var deprecate = require('depd')('express');
var flatten = require('array-flatten');
var parseUrl = require('parseurl');
var setPrototypeOf = require('setprototypeof')

/**
* Module variables.
Expand All @@ -47,7 +48,7 @@ var proto = module.exports = function(options) {
}

// mixin Router class functions
router.__proto__ = proto;
setPrototypeOf(router, proto)

router.params = {};
router._params = [];
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"range-parser": "~1.2.0",
"send": "0.14.2",
"serve-static": "~1.11.2",
"setprototypeof": "1.0.3",
"statuses": "~1.3.1",
"type-is": "~1.6.14",
"utils-merge": "1.0.0",
Expand Down

0 comments on commit 6022567

Please sign in to comment.