Skip to content

Commit

Permalink
lib: Use symbols for private Request and Response properties
Browse files Browse the repository at this point in the history
  • Loading branch information
nwoltman committed Jan 6, 2019
1 parent fceb5b9 commit 754675d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
12 changes: 7 additions & 5 deletions lib/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

const querystring = require('querystring')

const kQuery = Symbol('query')

function buildRequest(trustProxy, queryParser = querystring.parse) {
class Request {
constructor(stream, headers, params) {
this.stream = stream
this.headers = headers
this.params = params
this.body = undefined
this._query = null
this[kQuery] = null
}

get host() {
Expand Down Expand Up @@ -58,15 +60,15 @@ function buildRequest(trustProxy, queryParser = querystring.parse) {
}

get query() {
if (this._query === null) {
this._query = queryParser(this.querystring)
if (this[kQuery] === null) {
this[kQuery] = queryParser(this.querystring)
}

return this._query
return this[kQuery]
}

set query(value) {
this._query = value
this[kQuery] = value
}

get querystring() {
Expand Down
19 changes: 11 additions & 8 deletions lib/Response.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
'use strict'

const compileJSONStringify = require('compile-json-stringify')
const destroyStream = require('destroy')
const eos = require('end-of-stream')
const statusCodes = require('http').STATUS_CODES
const compileJSONStringify = require('compile-json-stringify')
const runHooks = require('./HookRunners').onSendHookRunner
const statusCodes = require('http').STATUS_CODES

const {serialize} = require('./Serializer')

const kRanCustomError = Symbol('ranCustomError')
const kRanOnSendHooks = Symbol('ranOnSendHooks')

const serializeError = compileJSONStringify({
type: 'object',
properties: {
Expand All @@ -26,8 +29,8 @@ function buildResponse() {
this.sent = false
this.state = {}
this._headers = {}
this._ranCustomError = false
this._ranOnSendHooks = false
this[kRanCustomError] = false
this[kRanOnSendHooks] = false
}

get headersSent() {
Expand Down Expand Up @@ -131,8 +134,8 @@ function buildResponse() {
this.statusCode = statusCode

var customErrorHandler = this.route.errorHandler
if (customErrorHandler !== null && this._ranCustomError === false) {
this._ranCustomError = true // Prevent the custom error handler from running again
if (customErrorHandler !== null && this[kRanCustomError] === false) {
this[kRanCustomError] = true // Prevent the custom error handler from running again

// Remove the current Content-Type so .send() doesn't assume the old type
this.remove('content-type')
Expand Down Expand Up @@ -215,10 +218,10 @@ function buildResponse() {
}

function runOnSendHooks(res, payload) {
if (res.route.onSendHooks === null || res._ranOnSendHooks) {
if (res.route.onSendHooks === null || res[kRanOnSendHooks]) {
sendFinalPayload(res, payload)
} else {
res._ranOnSendHooks = true
res[kRanOnSendHooks] = true
runHooks(
res.route.onSendHooks,
res,
Expand Down
14 changes: 2 additions & 12 deletions test/decorator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@ test('.decorateRequest() should not allow decorating Medley values', (t) => {
new Error("A decorator called 'body' has already been added to Request")
)

t.throws(
() => app.decorateRequest('_query', null),
new Error("A decorator called '_query' has already been added to Request")
)

t.throws(
() => app.decorateRequest('query', null),
new Error("A decorator called 'query' has already been added to Request")
Expand Down Expand Up @@ -136,13 +131,8 @@ test('.decorateResponse() should not allow decorating Medley values', (t) => {
)

t.throws(
() => app.decorateResponse('_ranCustomError', null),
new Error("A decorator called '_ranCustomError' has already been added to Response")
)

t.throws(
() => app.decorateResponse('_ranOnSendHooks', null),
new Error("A decorator called '_ranOnSendHooks' has already been added to Response")
() => app.decorateResponse('_headers', null),
new Error("A decorator called '_headers' has already been added to Response")
)

t.end()
Expand Down

0 comments on commit 754675d

Please sign in to comment.