Skip to content

Commit

Permalink
Improved types.
Browse files Browse the repository at this point in the history
  • Loading branch information
Valen-H committed May 30, 2019
1 parent 70011ab commit 0bd7f87
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 70 deletions.
13 changes: 12 additions & 1 deletion dist/lib/Classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
const http = require("http");
const fs = require("fs-extra");
const path = require("path");
const events_1 = require("events");
var Classes;
(function (Classes) {
let Errors;
Expand All @@ -20,6 +21,8 @@ var Classes;
*/
class Middleware {
constructor(name, befores = [], afters = [], body, _fromFile = false) {
this.befores = [];
this.afters = [];
this._fromFile = false;
this._idx = -1;
this._before = 0;
Expand All @@ -45,7 +48,7 @@ var Classes;
* @class Server
* @extends {require("events").EventEmitter}
*/
class Server extends require("events").EventEmitter {
class Server extends events_1.EventEmitter {
constructor(opts = Server.defaultOpts) {
super();
this.mwrs = [];
Expand Down Expand Up @@ -177,6 +180,14 @@ var Classes;
this.emit("_debug", ...msg);
return this;
} //_debug
//@Override
on(event, listener) {
return super.on(event, listener);
} //on
//@Override
once(event, listener) {
return super.once(event, listener);
} //once
} //Server
Server.defaultOpts = {
serveDir: path.resolve("__Server"),
Expand Down
104 changes: 66 additions & 38 deletions lib/Classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,60 @@
import * as http from "http";
import * as fs from "fs-extra";
import * as path from "path";
import { EventEmitter } from "events";


export module Classes {

export declare namespace Options {

export interface ServerOptions {
serveDir?: string; //local root
index?: RegExp; //index filename
root?: string; //url / mapped
mwbuilt?: string; //middleware / builtin
prbuilt?: string; //private / builtin
pubuilt?: string; //public / builtin
mwdir?: string; //local
private?: string; //local
public?: string; //local
noindex?: string; //.noindex name
nodir?: RegExp; //__files prefix
builtmpl?: RegExp; //$$code$$ prefix
dir?: string; //'dir.htm' sub-private / local
port?: number;
contentMappings?: {
readonly serveDir?: string; //local root
readonly index?: RegExp; //index filename
readonly root?: string; //url / mapped
readonly mwbuilt?: string; //middleware / builtin
readonly prbuilt?: string; //private / builtin
readonly pubuilt?: string; //public / builtin
readonly mwdir?: string; //local
readonly private?: string; //local
readonly public?: string; //local
readonly noindex?: string; //.noindex name
readonly nodir?: RegExp; //__files prefix
readonly builtmpl?: RegExp; //$$code$$ prefix
readonly dir?: string; //'dir.htm' sub-private / local
readonly port?: number;
readonly contentMappings?: {
[ext: string]: string;
}; // .htm -> text/html ...

http?: {
readonly http?: {
//require('http')
[idx: string]: any;
};

builtins?: boolean; //pass mwbuilt to mwdir if empty etc...
readonly builtins?: boolean; //pass mwbuilt to mwdir if empty etc...
allowmw?: boolean; //read mwrs from folder
} //ServerOptions

} //Options

export namespace Errors {

export const EBADPATH = new URIError("Bad Path.");
export const EBADROOT = new URIError("Bad Root.");
export const EBADPATH: URIError = new URIError("Bad Path.");
export const EBADROOT: URIError = new URIError("Bad Root.");

} //Errors

export type evt = {
stop: () => boolean;
pass: (data?: any) => Promise<boolean>;
carriage: {
readonly stop: () => boolean;
readonly pass: (data?: any) => Promise<boolean>;
readonly carriage: {
[idx: string]: any;
};
server: Server;
readonly server: Server;
stp: boolean;
fncntr: number;
reqcntr: number;
readonly reqcntr: number;
};


Expand All @@ -69,16 +70,16 @@ export module Classes {
*/
export class Middleware {
name: string;
befores: string[];
afters: string[];
befores: string[] = [ ];
afters: string[] = [ ];

_fromFile: boolean = false;
_fromFile?: boolean = false;
_idx: number = -1;
_before: number = 0;
_after: number = 0;


constructor(name: string, befores: string[] = [ ], afters: string[] = [ ], body: (req: any, res: any, event: evt) => Promise<boolean>, _fromFile: boolean = false) {
public constructor(name: string, befores: string[] = [ ], afters: string[] = [ ], body: (req: any, res: any, event: evt) => Promise<boolean>, _fromFile: boolean = false) {
this.name = name.toString();
this.befores = Array.from(befores);
this.afters = Array.from(afters);
Expand All @@ -103,19 +104,19 @@ export module Classes {
* @class Server
* @extends {require("events").EventEmitter}
*/
export class Server extends require("events").EventEmitter {
export class Server extends EventEmitter {

opts: Options.ServerOptions;
httpsrv: http.Server;
readonly opts: Options.ServerOptions;
readonly httpsrv: http.Server;
mwrs: Middleware[] = [ ];
logs: string = "";
_debuglog: string = "";
_reqcntr: number = 0;
data: {
readonly data: {
[idx: string]: any
} = { };

static defaultOpts: Options.ServerOptions = {
public static defaultOpts: Options.ServerOptions = {
serveDir: path.resolve("__Server"),
index: /^index\.html?x?$/i,
root: '/', //url mapped to serveDir
Expand Down Expand Up @@ -155,7 +156,7 @@ export module Classes {
};


constructor(opts: Options.ServerOptions = Server.defaultOpts) {
public constructor(opts: Options.ServerOptions = Server.defaultOpts) {
super();

this.opts = Object.assign({ }, Server.defaultOpts);
Expand Down Expand Up @@ -199,7 +200,7 @@ export module Classes {
* @param bb - middleware-to-bind / port-to-listen
* @param rec - allow order recalculation
*/
async bind(bb: number | Middleware = this.opts.port, rec: boolean = true): Promise<http.Server> {
public async bind(bb: number | Middleware = this.opts.port, rec: boolean = true): Promise<http.Server> {
if (bb instanceof Middleware) {
this.mwrs.push(bb);
if (rec) this._recalc();
Expand All @@ -217,7 +218,7 @@ export module Classes {
* @param from=path.join(this.opts.serveDir,this.opts.mwdir) - directory
*/
_loadMW(from: string = path.join(this.opts.serveDir, this.opts.mwdir)): void {
return fs.readdir(from, (err: Error, files: string[]) => {
return fs.readdir(from, (err: Error, files: string[]): void => {
if (!err) {
for (let file of files) {
let name: string = path.join(this.opts.serveDir, this.opts.mwdir, file);
Expand All @@ -235,7 +236,7 @@ export module Classes {
/**
* Order middlewares
*/
_recalc(): void {
private _recalc(): void {
let cntr: number = 0,
repeat: number = 0;

Expand Down Expand Up @@ -280,7 +281,7 @@ export module Classes {
* Log stuff
* @param msg - joined with whitespace
*/
log(...msg: any[]): this {
public log(...msg: any[]): this {
this.logs += msg.join(' ') + '\n';
this.emit("log", ...msg);
return this;
Expand All @@ -296,6 +297,33 @@ export module Classes {
this.emit("_debug", ...msg);
return this;
} //_debug

//mwloaded, request, _debug, log

//@Override
public on(event: "mwloaded", listener: (...args: any[]) => void): this;
//@Override
public on(event: "request", listener: (...args: any[]) => void): this;
//@Override
public on(event: "_debug", listener: (...args: any[]) => void): this;
//@Override
public on(event: "log", listener: (...args: any[]) => void): this;
//@Override
public on(event: string | symbol, listener: (...args: any[]) => void): this {
return super.on(event, listener);
} //on
//@Override
public once(event: "mwloaded", listener: (...args: any[]) => void): this;
//@Override
public once(event: "request", listener: (...args: any[]) => void): this;
//@Override
public once(event: "_debug", listener: (...args: any[]) => void): this;
//@Override
public once(event: "log", listener: (...args: any[]) => void): this;
//@Override
public once(event: string | symbol, listener: (...args: any[]) => void): this {
return super.once(event, listener);
} //once

} //Server

Expand Down
66 changes: 37 additions & 29 deletions lib/typings/lib/Classes.d.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
/// <reference types="node" />
import * as http from "http";
import { EventEmitter } from "events";
export declare module Classes {
namespace Options {
interface ServerOptions {
serveDir?: string;
index?: RegExp;
root?: string;
mwbuilt?: string;
prbuilt?: string;
pubuilt?: string;
mwdir?: string;
private?: string;
public?: string;
noindex?: string;
nodir?: RegExp;
builtmpl?: RegExp;
dir?: string;
port?: number;
contentMappings?: {
readonly serveDir?: string;
readonly index?: RegExp;
readonly root?: string;
readonly mwbuilt?: string;
readonly prbuilt?: string;
readonly pubuilt?: string;
readonly mwdir?: string;
readonly private?: string;
readonly public?: string;
readonly noindex?: string;
readonly nodir?: RegExp;
readonly builtmpl?: RegExp;
readonly dir?: string;
readonly port?: number;
readonly contentMappings?: {
[ext: string]: string;
};
http?: {
readonly http?: {
[idx: string]: any;
};
builtins?: boolean;
readonly builtins?: boolean;
allowmw?: boolean;
}
}
Expand All @@ -32,15 +33,15 @@ export declare module Classes {
const EBADROOT: URIError;
}
type evt = {
stop: () => boolean;
pass: (data?: any) => Promise<boolean>;
carriage: {
readonly stop: () => boolean;
readonly pass: (data?: any) => Promise<boolean>;
readonly carriage: {
[idx: string]: any;
};
server: Server;
readonly server: Server;
stp: boolean;
fncntr: number;
reqcntr: number;
readonly reqcntr: number;
};
/**
* Middleware class.
Expand All @@ -54,14 +55,13 @@ export declare module Classes {
name: string;
befores: string[];
afters: string[];
_fromFile: boolean;
_fromFile?: boolean;
_idx: number;
_before: number;
_after: number;
constructor(name: string, befores: string[], afters: string[], body: (req: any, res: any, event: evt) => Promise<boolean>, _fromFile?: boolean);
body(req: any, res: any, event: evt): Promise<boolean>;
}
const Server_base: any;
/**
* Starting Class.
*
Expand All @@ -71,14 +71,14 @@ export declare module Classes {
* @class Server
* @extends {require("events").EventEmitter}
*/
class Server extends Server_base {
opts: Options.ServerOptions;
httpsrv: http.Server;
class Server extends EventEmitter {
readonly opts: Options.ServerOptions;
readonly httpsrv: http.Server;
mwrs: Middleware[];
logs: string;
_debuglog: string;
_reqcntr: number;
data: {
readonly data: {
[idx: string]: any;
};
static defaultOpts: Options.ServerOptions;
Expand All @@ -97,7 +97,7 @@ export declare module Classes {
/**
* Order middlewares
*/
_recalc(): void;
private _recalc;
/**
* Log stuff
* @param msg - joined with whitespace
Expand All @@ -109,6 +109,14 @@ export declare module Classes {
* @param msg - joined with whitespace
*/
_debug(...msg: any[]): this;
on(event: "mwloaded", listener: (...args: any[]) => void): this;
on(event: "request", listener: (...args: any[]) => void): this;
on(event: "_debug", listener: (...args: any[]) => void): this;
on(event: "log", listener: (...args: any[]) => void): this;
once(event: "mwloaded", listener: (...args: any[]) => void): this;
once(event: "request", listener: (...args: any[]) => void): this;
once(event: "_debug", listener: (...args: any[]) => void): this;
once(event: "log", listener: (...args: any[]) => void): this;
}
}
export default Classes;
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vale-server-ii",
"version": "1.1.0",
"version": "1.1.1",
"description": "A Server framework",
"main": "dist/lib/server.js",
"author": "V. H.",
Expand All @@ -19,7 +19,8 @@
},
"scripts": {
"test": "node test/index.js || npm test",
"build": "tsc -w"
"build": "tsc -w",
"debug": "node --inspect-brk=9229 test/index.js || npm debug"
},
"keywords": [
"server",
Expand Down

0 comments on commit 0bd7f87

Please sign in to comment.