From 6d0bc3269939f81b501a6b30fdc6f77e37910b25 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Thu, 26 Dec 2024 12:08:33 +0000 Subject: [PATCH] style: format code with StandardJS This commit fixes the style issues introduced in e48ecc7 according to the output from StandardJS. Details: None --- lib/parser.js | 30 +++++++++++----------- lib/utils.js | 26 ++++++++----------- server/index.js | 64 +++++++++++++++++++++++----------------------- server/response.js | 20 +++++++-------- 4 files changed, 68 insertions(+), 72 deletions(-) diff --git a/lib/parser.js b/lib/parser.js index 93feb2c..82b6a64 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -18,15 +18,15 @@ const net = require('net') * * @param {string} req - The string to search within. * @param {string} target - The character to find in the string. - * @returns {number} The index of the first occurrence of the target character, + * @returns {number} The index of the first occurrence of the target character, * or -1 if the target character is not found. - * - * @example - * + * + * @example + * * const myString = "Hello, world!"; * const targetChar = "o"; * const index = findFirstBrac(myString, targetChar); - * + * * if (index !== -1) { * console.log(`The first occurrence of '${targetChar}' is at index ${index}.`); * } else { @@ -48,7 +48,7 @@ function findFirstBrac (req, target) { * * @param {string} request - The HTTP request string to parse. * @returns {Promise} A promise that resolves to an object containing the HTTP method, path, version, and body (if applicable). - * + * * @example * // Example HTTP request string const httpRequest = `POST /api/data HTTP/1.1 @@ -67,7 +67,7 @@ httpParser(httpRequest).then(parsedRequest => { }).catch(error => { console.error('Error parsing HTTP request:', error); }); -* +* */ async function httpParser (request) { const req = new Object() @@ -98,13 +98,13 @@ httpParser(request).then((data) => { }) -/** +/** * Stores a key-value pair from a request string into a JSON object. * * @param {Array} req - The request string split into an array of characters. * @param {Object} httpJSON - The JSON object to store the key-value pair. * @returns {Array} The modified request array after extracting the key-value pair. - * + * * @example * // Example request string const requestString = "key1:value1,key2:value2"; @@ -151,7 +151,7 @@ function storePair (req, httpJSON) { * * @param {string} body - The JSON body string to parse. * @returns {Object} The parsed JSON object. - * + * * @example * const jsonString = "{\"key1\":\"value1\",\"key2\":\"value2\"}"; const parsedObject = JSONbodyParser(jsonString); @@ -190,7 +190,7 @@ function JSONbodyParser (body) { * @param {Array} req - The request string split into an array of lines. * @param {number} pos - The position to start extracting the body. * @returns {Promise} A promise that resolves to the extracted body string. - * + * * @example * const httpRequest = `POST /api/data HTTP/1.1 Host: example.com @@ -233,7 +233,7 @@ function HTTPbody (req, pos) { * Parses a query string from a URL and extracts its components. * * @param {string} request - The URL containing the query string. - * + * * @example const url = 'https://example.com?name=JohnDoe&age=25&city=NewYork'; const parsedQuery = queryParser(url); @@ -253,7 +253,7 @@ function queryParser (request) { * * @param {string} query - The query string to parse. * @returns {Object} The parsed query string as a JSON object. - * + * * @example * // Example usage const queryString = "key1=value1&key2=value2"; @@ -286,8 +286,8 @@ function storeQuery (query) { * * @param {Array} req - The query string split into an array of characters. * @returns {Object} The JSON object containing the key-value pair. - * - * @example + * + * @example * const queryString = "key1=value1&key2=value2"; const queryArray = queryString.split(''); let queryJSON = {}; diff --git a/lib/utils.js b/lib/utils.js index 87fd2e0..e938063 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,10 +1,9 @@ - /** * Finds the index of the first occurrence of the target character in the given string. * @param {string} req - The string to search through. * @param {string} target - The character to find in the string. * @returns {number} The index of the target character or -1 if not found. - * + * * @example * const index = findFirstBrac('Hello, World!', 'o'); */ @@ -22,7 +21,7 @@ function findFirstBrac (req, target) { * @param {string} req - The HTTP request as a string. * @param {number} pos - The position in the string to start parsing. * @returns {Promise} A promise that resolves to the cleaned-up body. - * + * * @example * const body = await HTTPbody(req, pos); */ @@ -53,7 +52,7 @@ function HTTPbody (req, pos) { * Cleans up the body content by trimming spaces and standardizing spacing around colons and commas. * @param {string} body - The body content to clean up. * @returns {string} body - The cleaned-up body. - * + * * @example * const cleanedBody = cleanUpBody(body); */ @@ -74,7 +73,7 @@ function cleanUpBody (body) { * Parses a JSON-like HTTP body into an object. * @param {string} body - The HTTP body content as a string. * @returns {Object} The parsed JSON object. - * + * * @example * const parsedBody = JSONbodyParser(body); */ @@ -102,14 +101,13 @@ function JSONbodyParser (body) { return httpJSON } - /** * Stores key-value pairs in the provided JSON object. * @param {Array} req - The remaining request characters. * @param {Object} httpJSON - The JSON object to store the parsed data. * @returns {Array} The remaining unprocessed request characters. - * - * @example + * + * @example * storePair(req, httpJSON); */ function storePair (req, httpJSON) { @@ -145,13 +143,12 @@ function storePair (req, httpJSON) { return req } - /** * Parses primitive values from the request array. * @param {Array} req - The remaining request characters. * @returns {string|number} The parsed value, either as a string or number. - * - * @example + * + * @example * const parsedValue = parseValue(req); */ // Helper function to parse primitive values (strings, numbers, etc.) @@ -195,8 +192,8 @@ function parseValue (req) { * Parses a query string from a request URL into a JSON object. * @param {string} request - The request URL as a string. * @returns {Object} The parsed query parameters as a JSON object. - * - * @example + * + * @example * const queryParams = queryParser(request); */ function queryParser (request) { @@ -221,12 +218,11 @@ function queryParser (request) { const mimeDb = require('./mimeDb') // Adjust the path as needed - /** * Looks up the MIME type based on the file extension. * @param {string} extension - The file extension to look up. * @returns {string} The MIME type or 'application/octet-stream' if not found. - * + * * @example * const mimeType = lookupMimeType('application/json'); */ diff --git a/server/index.js b/server/index.js index bb1aa46..5e99200 100644 --- a/server/index.js +++ b/server/index.js @@ -5,11 +5,11 @@ const Response = require('./response.js') // Import the response object const { warn } = require('console') /** - * + * * @param {Function} callback - The callback function to handle incoming connections. * @param {Object} context - The context object containing the server configuration. * @returns A server socket object. - * + * * @example * ```javascript * const server = getSocket(handler, { @@ -28,12 +28,11 @@ function getSocket (callback, context) { return net.createServer(Socket => callback(Socket, context)) } - /** - * + * * @param {Socket} socket - The socket object for the response. * @param {Object} context - The context object containing the server configuration. - * @returns A server socket object. + * @returns A server socket object. */ function handler (socket, context) { socket.on('data', (data) => { @@ -150,7 +149,7 @@ function extractParams (routePath, actualPath) { */ class Server { socket - /** + /** * Creates a new Server instance * @constructor */ @@ -161,7 +160,7 @@ class Server { * @private */ this.socket = getSocket(handler, this) - /** + /** * Array of routes registered with the server * @type {Array} * @private @@ -169,7 +168,7 @@ class Server { this.routes = [] } - /** + /** * Starts the server listening on specified port * @param {number} PORT - The port number to listen on * @param {Function} callback - Callback function to execute when server starts listening @@ -186,16 +185,16 @@ class Server { } class Hasty extends Server { - /** + /** * Creates a new Hasty server instance * @constructor */ constructor () { super() - /** + /** * Collection of middleware functions * @type {Array} - * @private + * @private */ this.enableCors = false // default to false /** @@ -226,11 +225,12 @@ class Hasty extends Server { cors (enable) { this.enableCors = enable } + /** * GET - * - * @param {string} path - * @param {Function} callback + * + * @param {string} path + * @param {Function} callback */ get (path, callback) { this.setRoute('GET', { callback, path }) @@ -238,9 +238,9 @@ class Hasty extends Server { /** * POST - * - * @param {string} path - * @param {Function} callback + * + * @param {string} path + * @param {Function} callback */ post (path, callback) { this.setRoute('POST', { callback, path }) @@ -248,9 +248,9 @@ class Hasty extends Server { /** * PUT - * - * @param {string} path - * @param {Function} callback + * + * @param {string} path + * @param {Function} callback */ put (path, callback) { this.setRoute('PUT', { callback, path }) @@ -258,9 +258,9 @@ class Hasty extends Server { /** * DELETE - * - * @param {string} path - * @param {Function} callback + * + * @param {string} path + * @param {Function} callback */ delete (path, callback) { this.setRoute('DELETE', { callback, path }) @@ -268,9 +268,9 @@ class Hasty extends Server { /** * PATCH - * - * @param {string} path - * @param {Function} callback + * + * @param {string} path + * @param {Function} callback */ patch (path, callback) { this.setRoute('PATCH', { callback, path }) @@ -278,9 +278,9 @@ class Hasty extends Server { /** * HEAD - * - * @param {string} path - * @param {Function} callback + * + * @param {string} path + * @param {Function} callback */ head (path, callback) { this.setRoute('HEAD', { callback, path }) @@ -288,9 +288,9 @@ class Hasty extends Server { /** * OPTIONS - * - * @param {string} path - * @param {Function} callback + * + * @param {string} path + * @param {Function} callback */ options (path, callback) { this.setRoute('OPTIONS', { callback, path }) diff --git a/server/response.js b/server/response.js index a5c4864..4d7891f 100644 --- a/server/response.js +++ b/server/response.js @@ -4,13 +4,13 @@ const path = require('path') /** * Response class for handling HTTP responses. - * + * * @class Response - * + * * @param {Socket} socket - The socket object for the response. * @param {boolean} enableCors - Enable Cross-Origin Resource Sharing (CORS). * @param {string} statusTextMap - Map of status codes to status texts. - * @example + * @example * ```javascript const Response = require('./response.js'); @@ -64,7 +64,7 @@ class Response { } /** - * + * * @param {number} code - The HTTP status code. * @returns - The Response instance. */ @@ -77,7 +77,7 @@ class Response { } /** - * + * * @param {string} key - The header key. * @param {string} value - The header value. * @returns - The Response instance. @@ -109,7 +109,7 @@ class Response { } /** - * + * * @param {string} data - The data to send. * @returns - If the data is an object or array, send as JSON */ @@ -138,7 +138,7 @@ class Response { } /** - * + * * @param {number} statusCode - The HTTP status code. * Updates the status code and sends the status code as a response. */ @@ -150,7 +150,7 @@ class Response { } /** - * + * * @param {*} data - The data to send. */ json (data) { @@ -166,7 +166,7 @@ class Response { } /** - * + * * @param {*} file - The file to send. */ sendFile (file) { @@ -201,7 +201,7 @@ class Response { } /** - * + * * @param {*} file - The file to send. * @param {*} filename - The filename to send. */