Skip to content

Commit

Permalink
Stable
Browse files Browse the repository at this point in the history
  • Loading branch information
haroldiedema committed Feb 22, 2021
1 parent 8718cd0 commit 253ca13
Show file tree
Hide file tree
Showing 42 changed files with 745 additions and 1,576 deletions.
2 changes: 1 addition & 1 deletion dist/Event/ErrorEvent.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ResponseAwareEvent } from './ResponseAwareEvent';
import { ServerError } from '../Exception/ServerError';
import { Request } from '../Request';
import { Request } from '../Request/Request';
export declare class ErrorEvent extends ResponseAwareEvent {
readonly error: ServerError | Error;
readonly request: Request;
Expand Down
2 changes: 1 addition & 1 deletion dist/Event/RenderTemplateEvent.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Request } from '../Request';
import { Request } from '../Request/Request';
import { Session } from '../Session/Session';
import { ResponseAwareEvent } from './ResponseAwareEvent';
export declare class RenderTemplateEvent extends ResponseAwareEvent {
Expand Down
2 changes: 1 addition & 1 deletion dist/Event/RequestEvent.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ResponseAwareEvent } from './ResponseAwareEvent';
import { Request } from '../Request';
import { Request } from '../Request/Request';
import { IRoute } from '../Router/Router';
export declare class RequestEvent extends ResponseAwareEvent {
readonly request: Request;
Expand Down
2 changes: 1 addition & 1 deletion dist/Event/ResponseEvent.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Request } from '../Request';
import { Request } from '../Request/Request';
import { Response } from '../Response/Response';
import { IRoute } from '../Router/Router';
export declare class ResponseEvent {
Expand Down
2 changes: 1 addition & 1 deletion dist/Event/StaticRequestEvent.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ResponseAwareEvent } from '../Event/ResponseAwareEvent';
import { Request } from '../Request';
import { Request } from '../Request/Request';
export declare class StaticRequestEvent extends ResponseAwareEvent {
readonly request: Request;
readonly fileName: string;
Expand Down
2 changes: 1 addition & 1 deletion dist/Event/StaticResponseEvent.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Request } from '../Request';
import { Request } from '../Request/Request';
import { Response } from '../Response/Response';
export declare class StaticResponseEvent {
readonly request: Request;
Expand Down
8 changes: 8 additions & 0 deletions dist/Harmony.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export declare class Harmony {
private readonly options;
private readonly router;
private readonly server;
private readonly requestDecoder;
private readonly sessionManager;
private readonly staticAssetHandler;
private readonly templateManager;
Expand Down Expand Up @@ -88,6 +89,13 @@ export interface IConstructorOptions {
* Defaults to 8000.
*/
port?: number;
/**
* The maximum size of a request body in bytes.
*
* Make sure to keep this number relatively low to prevent flood attacks.
* Defaults to 1MB.
*/
maxUploadSize?: number;
static?: {
/**
* One or more directories to serve static assets from.
Expand Down
3 changes: 2 additions & 1 deletion dist/Request.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export declare class Request {
private readonly _cookies;
private readonly _query;
private readonly _path;
constructor(r: IncomingMessage);
private readonly _body;
constructor(r: IncomingMessage, body: Buffer);
/**
* Returns a value from one of the available bags in the following order:
* - cookies
Expand Down
7 changes: 7 additions & 0 deletions dist/Request/IUploadedFile.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// <reference types="node" />
export interface IUploadedFile {
name: string;
fileName: string;
mimeType: string;
data: Buffer;
}
22 changes: 22 additions & 0 deletions dist/Request/MultipartParser.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Multipart Parser (Finite State Machine)
* usage:
* const multipart = require('./multipart.js');
* const body = multipart.DemoData(); // raw body
* const body = new Buffer(event['body-json'].toString(),'base64'); // AWS case
* const boundary = multipart.getBoundary(event.params.header['content-type']);
* const parts = multipart.Parse(body,boundary);
* each part is:
* { filename: 'A.txt', type: 'text/plain', data: <Buffer 41 41 41 41 42 42 42 42> }
* or { name: 'key', data: <Buffer 41 41 41 41 42 42 42 42> }
*/
/// <reference types="node" />
declare type Input = {
filename?: string;
name?: string;
type: string;
data: Buffer;
};
export declare function parse(multipartBodyBuffer: Buffer, boundary: string): Input[];
export declare function getBoundary(header: string): string;
export {};
114 changes: 114 additions & 0 deletions dist/Request/Request.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/// <reference types="node" />
import { Bag } from '../Bag';
import { IRoute } from '../Router/Router';
import { IncomingMessage } from 'http';
import { IUploadedFile } from './IUploadedFile';
import { RequestBody } from './RequestBody';
export declare class Request {
private r;
private readonly _clientIp;
private readonly _method;
private readonly _parameters;
private readonly _headers;
private readonly _cookies;
private readonly _query;
private readonly _path;
private readonly _post;
private readonly _files;
private readonly _body;
constructor(r: IncomingMessage, body: RequestBody);
/**
* Returns a value from one of the available bags in the following order:
* - post fields
* - files
* - cookies
* - headers
* - parameters (derived from path)
* - query parameters
*
* @param {string} name
* @return {*}
*/
get(name: string): any;
/**
* Returns the IP address of the client.
*
* @return {string}
*/
get clientIp(): string;
/**
* Returns the request path.
*
* @return {string}
*/
get path(): string;
/**
* Returns the HTTP method in upper-case string representation.
*
* @return {string}
*/
get method(): string;
/**
* Returns the cookie bag.
*
* @return {Bag}
*/
get cookies(): Bag<string>;
/**
* Returns the headers bag.
*
* @return {Bag}
*/
get headers(): Bag<string>;
/**
* Returns the query parameters bag.
*
* @return {Bag}
*/
get query(): Bag<string | number>;
/**
* Returns the route parameters bag.
*
* @returns {Bag<string>}
*/
get parameters(): Bag<string>;
/**
* Returns the POST-fields bag.
*
* @returns {Bag<string | string[]>}
*/
get post(): Bag<string | string[]>;
/**
* Returns the files bag.
*
* @returns {Bag<IUploadedFile | IUploadedFile[]>}
*/
get files(): Bag<IUploadedFile | IUploadedFile[]>;
/**
* Returns the request body as parsed JSON content.
*
* @returns {any}
*/
get json(): any;
/**
* Returns true if the given route matches this request.
*
* @param {IRoute} route
* @return {boolean}
*/
isMatchingRoute(route: IRoute): boolean;
/**
* @param {string} pattern
* @return {{regExp: RegExp, namedParams: string[]}}
* @private
*/
private _parsePattern;
/**
* Parses a cookie header into a key-value object.
*
* @param {string} cookieString
* @returns {{[p: string]: string}}
* @private
*/
private _parseCookies;
}
16 changes: 16 additions & 0 deletions dist/Request/RequestBody.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference types="node" />
import { Bag } from '../Bag';
import { IUploadedFile } from './IUploadedFile';
export declare class RequestBody {
readonly raw: Buffer;
fields: Bag<string | string[]>;
files: Bag<IUploadedFile | IUploadedFile[]>;
constructor(raw: Buffer, parts: RequestBodyPart[]);
private addPartToBag;
}
export declare type RequestBodyPart = {
name: string;
data: Buffer;
type?: string;
filename?: string;
};
44 changes: 44 additions & 0 deletions dist/Request/RequestBodyDecoder.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/// <reference types="node" />
import { IncomingMessage, ServerResponse } from 'http';
import { RequestBody } from './RequestBody';
export declare class RequestBodyDecoder {
private readonly maxUploadSize;
constructor(maxUploadSize: number);
/**
* Attempts to deserialize the request body into data suitable for a Bag.
*
* If this process fails, the body is most likely uploaded content like
* a binary file.
*/
decode(req: IncomingMessage, res: ServerResponse): Promise<RequestBody>;
/**
* Parses the given body as a multipart/form-data payload.
*
* @param {ServerResponse} res
* @param {string} type
* @param {Buffer} body
* @private
*/
private parseMultipartFormData;
/**
* Parses the given body as a x-www-form-urlencoded payload.
*
* @param {ServerResponse} res
* @param {Buffer} body
* @returns {RequestBody}
* @private
*/
private parseFormUrlEncoded;
/**
* Wait for the request to complete, then return the request body.
*
* Sends an HTTP 413 status back to the client if the request body exceeds
* the maximum upload size.
*
* @param {IncomingMessage} req
* @param {ServerResponse} res
* @returns {Promise<Buffer>}
* @private
*/
private getRequestBody;
}
2 changes: 1 addition & 1 deletion dist/Response/HtmlResponse.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpStatus, Response } from './Response';
export declare class HtmlResponse extends Response {
constructor(html: string, statusCode?: HttpStatus, pretty?: boolean);
constructor(html: string, statusCode?: HttpStatus);
}
4 changes: 4 additions & 0 deletions dist/Response/RedirectResponse.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { HttpStatus, Response } from './Response';
export declare class RedirectResponse extends Response {
constructor(url: string, statusCode?: HttpStatus);
}
2 changes: 1 addition & 1 deletion dist/Router/Router.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Request } from '../Request';
import { Request } from '../Request/Request';
export declare class Router {
private routes;
/**
Expand Down
2 changes: 1 addition & 1 deletion dist/Session/SessionManager.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RequestEvent } from '../Event/RequestEvent';
import { ResponseEvent } from '../Event/ResponseEvent';
import { Request } from '../Request';
import { Request } from '../Request/Request';
import { Session } from './Session';
import { ISessionStorage } from './ISessionStorage';
/**
Expand Down
2 changes: 1 addition & 1 deletion dist/Templating/TemplateManager.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Request } from '../Request';
import { Request } from '../Request/Request';
import { Response } from '../Response/Response';
import { RenderTemplateEventListener } from '../Harmony';
import { Session } from '../Session/Session';
Expand Down
7 changes: 6 additions & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
export * from './Bag';
export * from './Cookie/Cookie';
export * from './Cookie/CookieBag';
export * from './Exception/AccessDeniedError';
export * from './Exception/InternalServerError';
export * from './Exception/NotFoundError';
export * from './Exception/ServerError';
export * from './Event/ErrorEvent';
export * from './Event/RenderTemplateEvent';
export * from './Event/RequestEvent';
export * from './Event/ResponseEvent';
export * from './Event/StaticRequestEvent';
export * from './Event/StaticResponseEvent';
export * from './Request';
export * from './Request/Request';
export * from './Response/HtmlResponse';
export * from './Response/JsonResponse';
export * from './Response/RedirectResponse';
export * from './Response/Response';
export * from './Router/Route';
export * from './Router/Router';
Expand Down
Loading

0 comments on commit 253ca13

Please sign in to comment.