2019-04-26 22:35:10 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import url from 'url';
|
2024-06-05 11:44:59 +00:00
|
|
|
import querystring from 'querystring';
|
2019-04-27 14:27:09 +00:00
|
|
|
|
2019-04-26 22:35:10 +00:00
|
|
|
import Sender from './Sender.mjs';
|
2019-04-27 14:27:09 +00:00
|
|
|
import RequestEnvironment from './RequestEnvironment.mjs';
|
2019-04-26 22:35:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Contains context information about a single request / response pair.
|
|
|
|
*/
|
|
|
|
class RouterContext {
|
2024-06-05 11:44:59 +00:00
|
|
|
/**
|
|
|
|
* Returns the parsed querystring from the request url, or an empty object if no query string was found.
|
|
|
|
*
|
|
|
|
* NOTE FROM THE NODE.JS DOCS: The object returned by the querystring.parse() method [used in this getter] does not prototypically inherit from the JavaScript Object. This means that typical Object methods such as obj.toString(), obj.hasOwnProperty(), and others are not defined and will not work.
|
|
|
|
*
|
|
|
|
* @return {Object} The parsed query string as an object, or an empty object.
|
|
|
|
*/
|
|
|
|
get querystring() {
|
2024-09-12 16:02:50 +00:00
|
|
|
if(this.url.search == null) return {};
|
2024-06-10 12:49:57 +00:00
|
|
|
return querystring.parse(this.url.search.substring(1));
|
2024-06-05 11:44:59 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 22:35:10 +00:00
|
|
|
constructor(in_request, in_response) {
|
|
|
|
/**
|
|
|
|
* The Node.JS request object
|
|
|
|
* @type {http.ClientRequest}
|
|
|
|
*/
|
|
|
|
this.request = in_request;
|
|
|
|
/**
|
|
|
|
* The Node.JS response object
|
|
|
|
* @type {http.ServerResponse}
|
|
|
|
*/
|
|
|
|
this.response = in_response;
|
|
|
|
/**
|
|
|
|
* The parsed request URL
|
2024-06-10 12:49:57 +00:00
|
|
|
*
|
|
|
|
* The built-in Node.js `url` module (and NOT the browser-like `URL` class) is used for parsing URLs.
|
|
|
|
*
|
|
|
|
* See the `querystring` getter for a portable way to grab the parsed query string.
|
|
|
|
*
|
|
|
|
* @type {Url}
|
2019-04-26 22:35:10 +00:00
|
|
|
*/
|
|
|
|
this.url = url.parse(this.request.url, true);
|
|
|
|
/**
|
|
|
|
* The url parameters parsed out by the router
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
this.params = {};
|
|
|
|
/**
|
|
|
|
* An object containing some utitlity methods for quickly sending responses
|
|
|
|
* @type {Sender}
|
|
|
|
*/
|
|
|
|
this.send = new Sender(this.response);
|
|
|
|
|
|
|
|
// FUTURE: Refactor the default population of this object elsewhere
|
|
|
|
/**
|
|
|
|
* The environment object.
|
|
|
|
* State variables that need to be attached to a specific request can
|
|
|
|
* go in here.
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
2019-04-27 14:27:09 +00:00
|
|
|
this.env = new RequestEnvironment(this.request);
|
2019-04-26 22:35:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default RouterContext;
|