From ff1ba32787c97ae01640d4904e91839fa887a32b Mon Sep 17 00:00:00 2001 From: Azamat Zulpykhar Date: Fri, 15 Nov 2024 02:40:05 +0100 Subject: [PATCH 1/3] refactor: replace statusTextMap with STATUS_CODES constant for improved readability --- server/response.js | 55 +++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/server/response.js b/server/response.js index 0adc121..2fd8a57 100644 --- a/server/response.js +++ b/server/response.js @@ -2,30 +2,31 @@ const fs = require('fs') const { lookupMimeType } = require('../lib/utils') const path = require('path') +const STATUS_CODES = { + 200: 'OK', + 201: 'Created', + 202: 'Accepted', + 204: 'No Content', + 301: 'Moved Permanently', + 302: 'Found', + 303: 'See Other', + 304: 'Not Modified', + 400: 'Bad Request', + 401: 'Unauthorized', + 403: 'Forbidden', + 404: 'Not Found', + 405: 'Method Not Allowed', + 406: 'Not Acceptable', + 409: 'Conflict', + 417: 'Expectation Failed', + 500: 'Internal Server Error', + 501: 'Not Implemented', + 503: 'Service Unavailable' +} + class Response { statusCode = 200 enableCors = false // default to false - statusTextMap = { - 200: 'OK', - 201: 'Created', - 202: 'Accepted', - 204: 'No Content', - 301: 'Moved Permanently', - 302: 'Found', - 303: 'See Other', - 304: 'Not Modified', - 400: 'Bad Request', - 401: 'Unauthorized', - 403: 'Forbidden', - 404: 'Not Found', - 405: 'Method Not Allowed', - 406: 'Not Acceptable', - 409: 'Conflict', - 417: 'Expectation Failed', - 500: 'Internal Server Error', - 501: 'Not Implemented', - 503: 'Service Unavailable' - } constructor (socket, enableCors) { this.socket = socket @@ -34,7 +35,7 @@ class Response { } status (code) { - if (!this.statusTextMap[code]) { + if (!STATUS_CODES[code]) { throw new Error(`Invalid status code: ${code}`) } this.statusCode = code @@ -80,14 +81,14 @@ class Response { this.setHeader('Content-Length', Buffer.byteLength(data)) - const headers = `HTTP/1.1 ${this.statusCode} ${this.statusTextMap[this.statusCode]}\r\n${this.formatHeaders()}\r\n\r\n` + const headers = `HTTP/1.1 ${this.statusCode} ${STATUS_CODES[this.statusCode]}\r\n${this.formatHeaders()}\r\n\r\n` this.socket.write(headers + data) this.socket.end() } sendStatus (statusCode) { this.status(statusCode) - const headers = `HTTP/1.1 ${this.statusCode} ${this.statusTextMap[this.statusCode]}\r\n${this.formatHeaders()}\r\n\r\n` + const headers = `HTTP/1.1 ${this.statusCode} ${STATUS_CODES[this.statusCode]}\r\n${this.formatHeaders()}\r\n\r\n` this.socket.write(headers) this.socket.end() } @@ -99,7 +100,7 @@ class Response { const body = JSON.stringify(data) this.setHeader('Content-Type', 'application/json') this.setHeader('Content-Length', Buffer.byteLength(body)) - const headers = `HTTP/1.1 ${this.statusCode} ${this.statusTextMap[this.statusCode]}\r\n${this.formatHeaders()}\r\n\r\n` + const headers = `HTTP/1.1 ${this.statusCode} ${STATUS_CODES[this.statusCode]}\r\n${this.formatHeaders()}\r\n\r\n` this.socket.write(headers + body) this.socket.end() } @@ -119,7 +120,7 @@ class Response { this.setHeader('Content-Length', stats.size) - const headers = `HTTP/1.1 ${this.statusCode} ${this.statusTextMap[this.statusCode]}\r\n${this.formatHeaders()}\r\n\r\n` + const headers = `HTTP/1.1 ${this.statusCode} ${STATUS_CODES[this.statusCode]}\r\n${this.formatHeaders()}\r\n\r\n` this.socket.write(headers) const stream = fs.createReadStream(file) @@ -151,7 +152,7 @@ class Response { this.setHeader('Content-Length', stats.size) - const headers = `HTTP/1.1 ${this.statusCode} ${this.statusTextMap[this.statusCode]}\r\n${this.formatHeaders()}\r\n\r\n` + const headers = `HTTP/1.1 ${this.statusCode} ${STATUS_CODES[this.statusCode]}\r\n${this.formatHeaders()}\r\n\r\n` this.socket.write(headers) const stream = fs.createReadStream(file) From 9811a18986c955d5581f3797b5304367adc658f8 Mon Sep 17 00:00:00 2001 From: Azamat Zulpykhar Date: Fri, 15 Nov 2024 02:40:19 +0100 Subject: [PATCH 2/3] refactor: freeze STATUS_CODES object for immutability --- server/response.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/response.js b/server/response.js index 2fd8a57..fd0d6d8 100644 --- a/server/response.js +++ b/server/response.js @@ -2,7 +2,7 @@ const fs = require('fs') const { lookupMimeType } = require('../lib/utils') const path = require('path') -const STATUS_CODES = { +const STATUS_CODES = Object.freeze({ 200: 'OK', 201: 'Created', 202: 'Accepted', @@ -22,7 +22,7 @@ const STATUS_CODES = { 500: 'Internal Server Error', 501: 'Not Implemented', 503: 'Service Unavailable' -} +}) class Response { statusCode = 200 From b20438d0563f64eea36dbec3fdcd02444b4bbf0f Mon Sep 17 00:00:00 2001 From: Azamat Zulpykhar Date: Fri, 15 Nov 2024 02:40:37 +0100 Subject: [PATCH 3/3] feat: add STATUS_CODES to Response class for better access --- server/response.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/response.js b/server/response.js index fd0d6d8..f4596d9 100644 --- a/server/response.js +++ b/server/response.js @@ -169,4 +169,6 @@ class Response { } } +Response.STATUS_CODES = STATUS_CODES + module.exports = Response