Skip to content

Commit

Permalink
Use object with null prototype for various app properties
Browse files Browse the repository at this point in the history
`app.cache`, `app.engines`, and `app.settings` are now created with
`Object.create(null)` instead of `{}`.

This also adds a test that `app.locals` is created the same way.
  • Loading branch information
EvanHahn committed Mar 17, 2022
1 parent 6faf26d commit a2348c8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ var trustProxyDefaultSymbol = '@@symbol:trust_proxy_default';
app.init = function init() {
var router = null;

this.cache = {};
this.engines = {};
this.settings = {};
this.cache = Object.create(null);
this.engines = Object.create(null);
this.settings = Object.create(null);

this.defaultConfiguration();

Expand Down
18 changes: 12 additions & 6 deletions test/app.locals.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@

var assert = require('assert');
var express = require('../')

describe('app', function(){
describe('.locals(obj)', function(){
it('should merge locals', function(){
var app = express();
Object.keys(app.locals).should.eql(['settings']);
assert.deepStrictEqual(Object.keys(app.locals), ['settings']);
app.locals.user = 'tobi';
app.locals.age = 2;
Object.keys(app.locals).should.eql(['settings', 'user', 'age']);
app.locals.user.should.equal('tobi');
app.locals.age.should.equal(2);
assert.deepStrictEqual(Object.keys(app.locals), ['settings', 'user', 'age']);
assert.strictEqual(app.locals.user, 'tobi');
assert.strictEqual(app.locals.age, 2);
})

it('is an object with no prototype', function(){
var app = express();
assert.strictEqual(Object.getPrototypeOf(app.locals), null);
})
})

Expand All @@ -19,8 +25,8 @@ describe('app', function(){
var app = express();
app.set('title', 'House of Manny');
var obj = app.locals.settings;
obj.should.have.property('env', 'test');
obj.should.have.property('title', 'House of Manny');
assert.strictEqual(obj.env, 'test');
assert.strictEqual(obj.title, 'House of Manny');
})
})
})

0 comments on commit a2348c8

Please sign in to comment.