Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -48,7 +48,7 @@ function findFirstBrac (req, target) {
*
* @param {string} request - The HTTP request string to parse.
* @returns {Promise<Object>} 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
Expand All @@ -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()
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<string>} A promise that resolves to the extracted body string.
*
*
* @example
* const httpRequest = `POST /api/data HTTP/1.1
Host: example.com
Expand Down Expand Up @@ -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);
Expand All @@ -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";
Expand Down Expand Up @@ -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 = {};
Expand Down
26 changes: 11 additions & 15 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -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');
*/
Expand All @@ -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<string>} A promise that resolves to the cleaned-up body.
*
*
* @example
* const body = await HTTPbody(req, pos);
*/
Expand Down Expand Up @@ -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);
*/
Expand All @@ -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);
*/
Expand Down Expand Up @@ -102,14 +101,13 @@ function JSONbodyParser (body) {
return httpJSON
}


/**
* Stores key-value pairs in the provided JSON object.
* @param {Array<string>} req - The remaining request characters.
* @param {Object} httpJSON - The JSON object to store the parsed data.
* @returns {Array<string>} The remaining unprocessed request characters.
*
* @example
*
* @example
* storePair(req, httpJSON);
*/
function storePair (req, httpJSON) {
Expand Down Expand Up @@ -145,13 +143,12 @@ function storePair (req, httpJSON) {
return req
}


/**
* Parses primitive values from the request array.
* @param {Array<string>} 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.)
Expand Down Expand Up @@ -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) {
Expand All @@ -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');
*/
Expand Down
64 changes: 32 additions & 32 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -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) => {
Expand Down Expand Up @@ -150,7 +149,7 @@ function extractParams (routePath, actualPath) {
*/
class Server {
socket
/**
/**
* Creates a new Server instance
* @constructor
*/
Expand All @@ -161,15 +160,15 @@ class Server {
* @private
*/
this.socket = getSocket(handler, this)
/**
/**
* Array of routes registered with the server
* @type {Array}
* @private
*/
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
Expand All @@ -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<Function>}
* @private
* @private
*/
this.enableCors = false // default to false
/**
Expand Down Expand Up @@ -226,71 +225,72 @@ 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 })
}

/**
* POST
*
* @param {string} path
* @param {Function} callback
*
* @param {string} path
* @param {Function} callback
*/
post (path, callback) {
this.setRoute('POST', { callback, path })
}

/**
* PUT
*
* @param {string} path
* @param {Function} callback
*
* @param {string} path
* @param {Function} callback
*/
put (path, callback) {
this.setRoute('PUT', { callback, path })
}

/**
* DELETE
*
* @param {string} path
* @param {Function} callback
*
* @param {string} path
* @param {Function} callback
*/
delete (path, callback) {
this.setRoute('DELETE', { callback, path })
}

/**
* PATCH
*
* @param {string} path
* @param {Function} callback
*
* @param {string} path
* @param {Function} callback
*/
patch (path, callback) {
this.setRoute('PATCH', { callback, path })
}

/**
* HEAD
*
* @param {string} path
* @param {Function} callback
*
* @param {string} path
* @param {Function} callback
*/
head (path, callback) {
this.setRoute('HEAD', { callback, path })
}

/**
* OPTIONS
*
* @param {string} path
* @param {Function} callback
*
* @param {string} path
* @param {Function} callback
*/
options (path, callback) {
this.setRoute('OPTIONS', { callback, path })
Expand Down
Loading
Loading