mirror of
https://github.com/sbrl/powahroot.git
synced 2024-11-23 22:53:00 +00:00
Document a bunch of stuff.
This commit is contained in:
parent
5d6392fe3d
commit
1e56527f44
7 changed files with 5861 additions and 67 deletions
|
@ -6,14 +6,13 @@ import { pathspec_to_regex } from '../Shared/Pathspec.mjs';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Client-side request router.
|
* Client-side request router.
|
||||||
|
* You should use this in a browser. If you're not in a browser, you probably want ServerRouter instead.
|
||||||
* @extends EventEmitter
|
* @extends EventEmitter
|
||||||
|
* @param {object} options The options object to use when creating the router.
|
||||||
|
* @property {Boolean} verbose Whether to be verbose and log debugging information to the console.
|
||||||
|
* @property {Boolean} listen_pushstate Whether to listen to the browser's `pushstate` event and automatically navigate on recieving it.
|
||||||
*/
|
*/
|
||||||
class ClientRouter extends EventEmitter {
|
class ClientRouter extends EventEmitter {
|
||||||
/**
|
|
||||||
* Creates a new client-side request router.
|
|
||||||
* You should use this in a browser. If you're not in a browser, you probably want ServerRouter instead.
|
|
||||||
* @param {object} options The options object to use when creating the router.
|
|
||||||
*/
|
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
|
|
33
Server/RequestEnvironment.mjs
Normal file
33
Server/RequestEnvironment.mjs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
import cookie from 'cookie';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Holds request environment and state variables.
|
||||||
|
*/
|
||||||
|
class RequestEnvironment {
|
||||||
|
constructor(request) {
|
||||||
|
/**
|
||||||
|
* Whether the user is logged in or not.
|
||||||
|
* @type {Boolean}
|
||||||
|
*/
|
||||||
|
this.logged_in = false;
|
||||||
|
/**
|
||||||
|
* The user's name. Guaranteed to be specified - if only as "anonymous".
|
||||||
|
* @type {String}
|
||||||
|
*/
|
||||||
|
this.username = "anonymous";
|
||||||
|
/**
|
||||||
|
* The parsed cookie object
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
this.cookie = cookie.parse(request.headers["cookie"] || "");
|
||||||
|
/**
|
||||||
|
* The parsed post data as an object, if applicable.
|
||||||
|
* @type {Object|null}
|
||||||
|
*/
|
||||||
|
this.post_data = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default RequestEnvironment;
|
|
@ -7,6 +7,7 @@ import { pathspec_to_regex } from '../Shared/Pathspec.mjs';
|
||||||
* A standalone HTTP router that's based on the principle of middleware.
|
* A standalone HTTP router that's based on the principle of middleware.
|
||||||
* Based on rill (see the npm package bearing the name), but stripped down and
|
* Based on rill (see the npm package bearing the name), but stripped down and
|
||||||
* simplified.
|
* simplified.
|
||||||
|
* @param {Boolean} verbose Whether to be verbose and log a bunch of things to the console. Useful for debugging.
|
||||||
*/
|
*/
|
||||||
class Router
|
class Router
|
||||||
{
|
{
|
||||||
|
@ -28,19 +29,48 @@ class Router
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Shortcut function for attaching an action to any request method. Full function: on */
|
/**
|
||||||
|
* Shortcut function for attaching an action to any request method.
|
||||||
|
* Full function: on
|
||||||
|
* @param {string|RegExp} pathspec The pathspec that the route should match against.
|
||||||
|
* @param {Function} action The function to execute when this route is matches.gets passed 2 parameters: context (or type RequestContext) and next (a function). context contains the request / response objects, and next() should be called if the action is middleware.
|
||||||
|
*/
|
||||||
any(pathspec, action) { this.on("*", pathspec, action); }
|
any(pathspec, action) { this.on("*", pathspec, action); }
|
||||||
/** Shortcut function for attaching an action to head requests. Full function: on */
|
/**
|
||||||
|
* Shortcut function for attaching an action to head requests. Full function: on
|
||||||
|
* @param {string|RegExp} pathspec The pathspec that the route should match against.
|
||||||
|
* @param {Fuction} action The function to execute.
|
||||||
|
*/
|
||||||
head(pathspec, action) { this.on(["head"], pathspec, action); }
|
head(pathspec, action) { this.on(["head"], pathspec, action); }
|
||||||
/** Shortcut function for attaching an action to get requests. Full function: on */
|
/**
|
||||||
|
* Shortcut function for attaching an action to get requests. Full function: on
|
||||||
|
* @param {string|RegExp} pathspec The pathspec that the route should match against.
|
||||||
|
* @param {Fuction} action The function to execute.
|
||||||
|
*/
|
||||||
get(pathspec, action) { this.on(["get"], pathspec, action); }
|
get(pathspec, action) { this.on(["get"], pathspec, action); }
|
||||||
/** Shortcut function for attaching an action to post requests. Full function: on */
|
/**
|
||||||
|
* Shortcut function for attaching an action to post requests. Full function: on
|
||||||
|
* @param {string|RegExp} pathspec The pathspec that the route should match against.
|
||||||
|
* @param {Fuction} action The function to execute.
|
||||||
|
*/
|
||||||
post(pathspec, action) { this.on(["post"], pathspec, action); }
|
post(pathspec, action) { this.on(["post"], pathspec, action); }
|
||||||
/** Shortcut function for attaching an action to put requests. Full function: on */
|
/**
|
||||||
|
* Shortcut function for attaching an action to put requests. Full function: on
|
||||||
|
* @param {string|RegExp} pathspec The pathspec that the route should match against.
|
||||||
|
* @param {Fuction} action The function to execute.
|
||||||
|
*/
|
||||||
put(pathspec, action) { this.on(["put"], pathspec, action); }
|
put(pathspec, action) { this.on(["put"], pathspec, action); }
|
||||||
/** Shortcut function for attaching an action to delete requests. Full function: on */
|
/**
|
||||||
|
* Shortcut function for attaching an action to delete requests. Full function: on
|
||||||
|
* @param {string|RegExp} pathspec The pathspec that the route should match against.
|
||||||
|
* @param {Fuction} action The function to execute.
|
||||||
|
*/
|
||||||
delete(pathspec, action) { this.on(["delete"], pathspec, action); }
|
delete(pathspec, action) { this.on(["delete"], pathspec, action); }
|
||||||
/** Shortcut function for attaching an action to options requests. Full function: on */
|
/**
|
||||||
|
* Shortcut function for attaching an action to options requests. Full function: on
|
||||||
|
* @param {string|RegExp} pathspec The pathspec that the route should match against.
|
||||||
|
* @param {Fuction} action The function to execute.
|
||||||
|
*/
|
||||||
options(pathspec, action) { this.on(["options"], pathspec, action); }
|
options(pathspec, action) { this.on(["options"], pathspec, action); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -73,6 +103,11 @@ class Router
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a route that matches against every request.
|
||||||
|
* Usually useful for middleware (e.g. error handlers, request loggers, authentication handlers, etc.).
|
||||||
|
* @param {Function} action The function to execute.
|
||||||
|
*/
|
||||||
on_all(action) {
|
on_all(action) {
|
||||||
this.actions.push(action);
|
this.actions.push(action);
|
||||||
}
|
}
|
||||||
|
@ -113,6 +148,8 @@ class Router
|
||||||
* item of middleware.
|
* item of middleware.
|
||||||
* It achieves this via a combination of a generator, anonymous function
|
* It achieves this via a combination of a generator, anonymous function
|
||||||
* scope abuse, being recursive, and magic.
|
* scope abuse, being recursive, and magic.
|
||||||
|
* You shouldn't need to call this directly.
|
||||||
|
* @private
|
||||||
* @param {Generator} iterator The generator that emits the middleware.
|
* @param {Generator} iterator The generator that emits the middleware.
|
||||||
* @param {Object} context The context of the request.
|
* @param {Object} context The context of the request.
|
||||||
* @return {Function} A magic next function.
|
* @return {Function} A magic next function.
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
// core
|
|
||||||
import url from 'url';
|
import url from 'url';
|
||||||
// npm
|
|
||||||
import cookie from 'cookie';
|
|
||||||
// files
|
|
||||||
import Sender from './Sender.mjs';
|
import Sender from './Sender.mjs';
|
||||||
|
import RequestEnvironment from './RequestEnvironment.mjs';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contains context information about a single request / response pair.
|
* Contains context information about a single request / response pair.
|
||||||
|
@ -45,28 +43,7 @@ class RouterContext {
|
||||||
* go in here.
|
* go in here.
|
||||||
* @type {Object}
|
* @type {Object}
|
||||||
*/
|
*/
|
||||||
this.env = {
|
this.env = new RequestEnvironment(this.request);
|
||||||
/**
|
|
||||||
* Whether the user is logged in or not.
|
|
||||||
* @type {Boolean}
|
|
||||||
*/
|
|
||||||
logged_in: false,
|
|
||||||
/**
|
|
||||||
* The user's name. Guaranteed to be specified - if only as "anonymous".
|
|
||||||
* @type {String}
|
|
||||||
*/
|
|
||||||
username: "anonymous",
|
|
||||||
/**
|
|
||||||
* The parsed cookie object
|
|
||||||
* @type {Object}
|
|
||||||
*/
|
|
||||||
cookie: cookie.parse(this.request.headers["cookie"] || ""),
|
|
||||||
/**
|
|
||||||
* The parsed post data as an object, if applicable.
|
|
||||||
* @type {Object|null}
|
|
||||||
*/
|
|
||||||
post_data: null
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,10 @@
|
||||||
|
|
||||||
import { NightInkFile } from 'nightink';
|
import { NightInkFile } from 'nightink';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper methods for quickly sending responses to clients.
|
||||||
|
* @param {http.ServerResponse} response The response object to use when sending requests.
|
||||||
|
*/
|
||||||
class Sender {
|
class Sender {
|
||||||
constructor(response) {
|
constructor(response) {
|
||||||
this.response = response;
|
this.response = response;
|
||||||
|
|
5741
package-lock.json
generated
5741
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -21,10 +21,13 @@
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/sbrl/powerroot#readme",
|
"homepage": "https://github.com/sbrl/powerroot#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"cookie": "^0.3.1",
|
||||||
"nightink": "^0.1.2"
|
"nightink": "^0.1.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/cookie": "^0.3.2",
|
||||||
"@types/event-emitter-es6": "^1.1.0",
|
"@types/event-emitter-es6": "^1.1.0",
|
||||||
|
"documentation": "^10.1.0",
|
||||||
"event-emitter-es6": "^1.1.5"
|
"event-emitter-es6": "^1.1.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue