Comparing version 0.1.2 to 1.0.0
@@ -1,2 +0,3 @@ | ||
export * from "./server"; | ||
export * from "./router"; | ||
export { Server } from "./server"; | ||
export { Router } from "./plugin"; | ||
export { Logger } from "./util"; |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./server")); | ||
__export(require("./router")); | ||
var server_1 = require("./server"); | ||
exports.Server = server_1.Server; | ||
var plugin_1 = require("./plugin"); | ||
exports.Router = plugin_1.Router; | ||
var util_1 = require("./util"); | ||
exports.Logger = util_1.Logger; | ||
//# sourceMappingURL=index.js.map |
/// <reference types="node" /> | ||
import * as http from "http"; | ||
import * as https from "https"; | ||
import { Router } from "./router"; | ||
import * as middleware from "./middleware"; | ||
import { Logger } from "./util"; | ||
export declare class Server { | ||
private options; | ||
static Middleware: typeof middleware; | ||
private servers; | ||
private router; | ||
private events; | ||
private logger; | ||
constructor(options: Server.Options); | ||
start(): void; | ||
start(): Promise<void>; | ||
stop(): void; | ||
private http; | ||
private https; | ||
plugin(plugin: Server.Plugin): void; | ||
plugin(plugin: Server.Plugin): Promise<void>; | ||
on(event: Server.Event.Type, handler: Server.Event.Handler): void; | ||
emit(event: Server.Event.Type, payload: any): void; | ||
} | ||
export declare namespace Server { | ||
interface Plugin { | ||
name: string; | ||
routes: Router.Route[]; | ||
register: (server: Server, logger: Logger) => Promise<void>; | ||
} | ||
namespace Event { | ||
type Type = "start" | "stop" | "request"; | ||
type Handler = (...args: any[]) => any; | ||
} | ||
interface Options { | ||
routes: Router.Route[]; | ||
http?: number; | ||
@@ -26,0 +29,0 @@ https?: number; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -6,4 +41,2 @@ var http = require("http"); | ||
var util_1 = require("./util"); | ||
var router_1 = require("./router"); | ||
var middleware = require("./middleware"); | ||
var Server = /** @class */ (function () { | ||
@@ -13,4 +46,5 @@ function Server(options) { | ||
this.servers = {}; | ||
util_1.Logger.info("..."); | ||
this.router = new router_1.Router(options.routes); | ||
this.events = {}; | ||
this.logger = new util_1.Logger("decade"); | ||
this.logger.info("initializing"); | ||
if (options.http) { | ||
@@ -24,34 +58,91 @@ this.http(); | ||
Server.prototype.start = function () { | ||
var _this = this; | ||
if (this.servers.http) { | ||
this.servers.http.listen(this.options.http, function () { return util_1.Logger.good("[http]: http://localhost:" + _this.options.http); }); | ||
} | ||
if (this.servers.https) { | ||
this.servers.https.listen(this.options.https, function () { return util_1.Logger.good("[https]: https://localhost:" + _this.options.https); }); | ||
} | ||
return __awaiter(this, void 0, void 0, function () { | ||
var promises; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
promises = [new util_1.Deferred(), new util_1.Deferred()]; | ||
if (this.servers.http) { | ||
this.servers.http.listen(this.options.http, function () { | ||
_this.emit("start", "http"); | ||
promises[0].resolve(); | ||
_this.logger.good("[http]: http://localhost:" + _this.options.http); | ||
}); | ||
} | ||
else { | ||
promises[0].resolve(); | ||
} | ||
if (this.servers.https) { | ||
this.servers.https.listen(this.options.https, function () { | ||
_this.emit("start", "https"); | ||
promises[1].resolve(); | ||
_this.logger.good("[https]: https://localhost:" + _this.options.https); | ||
}); | ||
} | ||
else { | ||
promises[1].resolve(); | ||
} | ||
return [4 /*yield*/, Promise.all(promises)]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
Server.prototype.stop = function () { | ||
var _this = this; | ||
if (this.servers.http) { | ||
this.servers.http.close(function () { return util_1.Logger.good("http : stopped"); }); | ||
this.servers.http.close(function () { | ||
_this.emit("stop", "http"); | ||
_this.logger.good("[http]: stopped"); | ||
}); | ||
} | ||
if (this.servers.https) { | ||
this.servers.https.close(function () { return util_1.Logger.good("https: stopped"); }); | ||
this.servers.https.close(function () { | ||
_this.emit("stop", "https"); | ||
_this.logger.good("[https]: stopped"); | ||
}); | ||
} | ||
}; | ||
Server.prototype.http = function () { | ||
var _this = this; | ||
this.servers.http = http.createServer(); | ||
this.servers.http.on("request", this.router.handle.bind(this.router)); | ||
this.servers.http.on("request", function (request, response) { return _this.emit("request", { request: request, response: response }); }); | ||
}; | ||
Server.prototype.https = function () { | ||
var _this = this; | ||
this.servers.https = https.createServer(this.options.options); | ||
this.servers.https.on("request", this.router.handle.bind(this.router)); | ||
this.servers.https.on("request", function (request, response) { return _this.emit("request", { request: request, response: response }); }); | ||
}; | ||
Server.prototype.plugin = function (plugin) { | ||
util_1.Logger.good("plugin: " + plugin.name); | ||
for (var _i = 0, _a = plugin.routes; _i < _a.length; _i++) { | ||
var route = _a[_i]; | ||
this.router.register(route); | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
this.logger.good("[plugin]: <" + plugin.constructor.name.toLowerCase() + ">"); | ||
return [4 /*yield*/, plugin.register(this, new util_1.Logger(plugin.constructor.name.toLowerCase()))]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
Server.prototype.on = function (event, handler) { | ||
if (!this.events.hasOwnProperty(event)) { | ||
this.events[event] = []; | ||
} | ||
this.events[event].push(handler); | ||
}; | ||
Server.Middleware = middleware; | ||
Server.prototype.emit = function (event, payload) { | ||
if (!this.events.hasOwnProperty(event)) { | ||
return; | ||
} | ||
for (var _i = 0, _a = this.events[event]; _i < _a.length; _i++) { | ||
var handler = _a[_i]; | ||
handler(payload); | ||
} | ||
}; | ||
return Server; | ||
@@ -58,0 +149,0 @@ }()); |
@@ -8,6 +8,9 @@ export declare class Deferred<T> { | ||
export declare class Logger { | ||
private service; | ||
constructor(service: string); | ||
static active: () => string; | ||
static info(...args: any[]): void; | ||
static good(...args: any[]): void; | ||
static error(...args: any[]): void; | ||
info(...args: any[]): void; | ||
detail(...args: any[]): void; | ||
good(...args: any[]): void; | ||
error(...args: any[]): void; | ||
/** Color Utilities */ | ||
@@ -14,0 +17,0 @@ private static colorApply; |
@@ -15,5 +15,6 @@ "use strict"; | ||
var Logger = /** @class */ (function () { | ||
function Logger() { | ||
function Logger(service) { | ||
this.service = service; | ||
} | ||
Logger.info = function () { | ||
Logger.prototype.info = function () { | ||
var args = []; | ||
@@ -24,6 +25,6 @@ for (var _i = 0; _i < arguments.length; _i++) { | ||
if (Logger.active()) { | ||
console.log.apply(console, [Logger.colorFgWhite("[decade]")].concat(args)); | ||
console.log.apply(console, [Logger.colorFgWhite("[" + this.service + "]")].concat(args)); | ||
} | ||
}; | ||
Logger.good = function () { | ||
Logger.prototype.detail = function () { | ||
var args = []; | ||
@@ -34,6 +35,6 @@ for (var _i = 0; _i < arguments.length; _i++) { | ||
if (Logger.active()) { | ||
console.log.apply(console, [Logger.colorFgGreen("[decade]")].concat(args)); | ||
console.log.apply(console, [Logger.colorFgYellow("[" + this.service + "]")].concat(args)); | ||
} | ||
}; | ||
Logger.error = function () { | ||
Logger.prototype.good = function () { | ||
var args = []; | ||
@@ -44,5 +45,14 @@ for (var _i = 0; _i < arguments.length; _i++) { | ||
if (Logger.active()) { | ||
console.error.apply(console, [Logger.colorFgRed("[decade]")].concat(args)); | ||
console.log.apply(console, [Logger.colorFgGreen("[" + this.service + "]")].concat(args)); | ||
} | ||
}; | ||
Logger.prototype.error = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
if (Logger.active()) { | ||
console.error.apply(console, [Logger.colorFgRed("[" + this.service + "]")].concat(args)); | ||
} | ||
}; | ||
Logger.active = function () { return (process.env.DEBUG); }; | ||
@@ -49,0 +59,0 @@ /** Color Utilities */ |
{ | ||
"name": "decade", | ||
"version": "0.1.2", | ||
"version": "1.0.0", | ||
"description": "Dead simple HTTP/s Server", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -21,3 +21,2 @@ # Decade ☄️ | ||
import { Server } from "decade"; | ||
import * as routes from "./routes"; | ||
@@ -31,6 +30,46 @@ const server = new Server({ | ||
}, | ||
routes: Object.values(routes), | ||
}); | ||
``` | ||
### Plugin | ||
Decade provides a simple **Plugin** system, a plugin should be an _something_ with a `register` method which will receive the **Server** instance, from there you can listen for and emit events. | ||
```ts | ||
import { Server } from "decade"; | ||
export class MyPlugin implements Server.Plugin { | ||
... | ||
async register(server: Server): Promise<void> { | ||
this.server = server; | ||
server.on("request", this.doSomething.bind(this)); | ||
} | ||
async someOtherMethod() { | ||
this.server.emit("customevent", "foo"); | ||
} | ||
} | ||
``` | ||
To use a plugin, use the `Server.plugin` method. | ||
```ts | ||
import { Server } from "decade"; | ||
import { FooPlugin } from "decade-foo-plugin"; | ||
const server = new Server({...}); | ||
server.plugin(new FooPlugin("bar")); | ||
``` | ||
### Router | ||
There's a built in **Router** plugin available for import which can be initialized with an array of `Router.Route` objects. | ||
```ts | ||
import { Server, Router } from "decade"; | ||
import * as routes from "./routes"; | ||
const server = new Server({...}) | ||
const router = new Router(Object.values(routes)); | ||
server.plugin(router); | ||
``` | ||
For simple `JSON` responses you can return a `Router.Route.Payload` object from a handler which will be parsed and sent to the client. | ||
@@ -37,0 +76,0 @@ ```ts |
@@ -1,2 +0,3 @@ | ||
export * from "./server"; | ||
export * from "./router"; | ||
export { Server } from "./server"; | ||
export { Router } from "./plugin"; | ||
export { Logger } from "./util" |
import * as http from "http"; | ||
import * as https from "https"; | ||
import { Logger } from "./util"; | ||
import { Router } from "./router"; | ||
import * as middleware from "./middleware"; | ||
import { Logger, Deferred } from "./util"; | ||
export class Server { | ||
public static Middleware = middleware; | ||
private servers: Server.Servers = {}; | ||
private router: Router; | ||
private events: { [key: string]: Server.Event.Handler[] } = {}; | ||
private logger: Logger = new Logger("decade"); | ||
@@ -18,6 +14,4 @@ constructor( | ||
) { | ||
Logger.info(`...`); | ||
this.logger.info(`initializing`); | ||
this.router = new Router(options.routes); | ||
if (options.http) { | ||
@@ -31,9 +25,25 @@ this.http(); | ||
public start(): void { | ||
public async start(): Promise<void> { | ||
const promises = [ new Deferred(), new Deferred() ]; | ||
if (this.servers.http) { | ||
this.servers.http.listen(this.options.http, () => Logger.good(`[http]: http://localhost:${this.options.http}`)); | ||
this.servers.http.listen(this.options.http, () => { | ||
this.emit("start", "http"); | ||
promises[0].resolve(); | ||
this.logger.good(`[http]: http://localhost:${this.options.http}`); | ||
}); | ||
} else { | ||
promises[0].resolve(); | ||
} | ||
if (this.servers.https) { | ||
this.servers.https.listen(this.options.https, () => Logger.good(`[https]: https://localhost:${this.options.https}`)); | ||
this.servers.https.listen(this.options.https, () => { | ||
this.emit("start", "https"); | ||
promises[1].resolve(); | ||
this.logger.good(`[https]: https://localhost:${this.options.https}`); | ||
}); | ||
} else { | ||
promises[1].resolve(); | ||
} | ||
await Promise.all(promises); | ||
} | ||
@@ -43,6 +53,12 @@ | ||
if (this.servers.http) { | ||
this.servers.http.close(() => Logger.good(`http : stopped`)); | ||
this.servers.http.close(() => { | ||
this.emit("stop", "http"); | ||
this.logger.good(`[http]: stopped`); | ||
}); | ||
} | ||
if (this.servers.https) { | ||
this.servers.https.close(() => Logger.good(`https: stopped`)); | ||
this.servers.https.close(() => { | ||
this.emit("stop", "https"); | ||
this.logger.good(`[https]: stopped`); | ||
}); | ||
} | ||
@@ -53,3 +69,3 @@ } | ||
this.servers.http = http.createServer(); | ||
this.servers.http.on("request", this.router.handle.bind(this.router)); | ||
this.servers.http.on("request", (request, response) => this.emit("request", { request, response })); | ||
} | ||
@@ -59,11 +75,27 @@ | ||
this.servers.https = https.createServer(this.options.options); | ||
this.servers.https.on("request", this.router.handle.bind(this.router)); | ||
this.servers.https.on("request", (request, response) => this.emit("request", { request, response })); | ||
} | ||
public plugin (plugin: Server.Plugin): void { | ||
Logger.good(`plugin: ${plugin.name}`); | ||
for (const route of plugin.routes) { | ||
this.router.register(route); | ||
public async plugin(plugin: Server.Plugin): Promise<void> { | ||
this.logger.good(`[plugin]: <${plugin.constructor.name.toLowerCase()}>`); | ||
await plugin.register(this, new Logger(plugin.constructor.name.toLowerCase())); | ||
} | ||
public on(event: Server.Event.Type, handler: Server.Event.Handler): void { | ||
if(!this.events.hasOwnProperty(event)) { | ||
this.events[event] = []; | ||
} | ||
this.events[event].push(handler); | ||
} | ||
public emit(event: Server.Event.Type, payload: any): void { | ||
if(!this.events.hasOwnProperty(event)) { | ||
return; | ||
} | ||
for(const handler of this.events[event]) { | ||
handler(payload); | ||
} | ||
} | ||
} | ||
@@ -73,7 +105,9 @@ | ||
export interface Plugin { | ||
name: string; | ||
routes: Router.Route[]; | ||
register: (server: Server, logger: Logger) => Promise<void>; | ||
} | ||
export namespace Event { | ||
export type Type = "start" | "stop" | "request"; | ||
export type Handler = (...args: any[]) => any; | ||
} | ||
export interface Options { | ||
routes: Router.Route[]; | ||
http?: number; | ||
@@ -80,0 +114,0 @@ https?: number; |
@@ -14,22 +14,32 @@ export class Deferred<T> { | ||
export class Logger { | ||
constructor( | ||
private service: string, | ||
) {} | ||
static active = () => (process.env.DEBUG); | ||
public static info (...args: any[]): void { | ||
public info (...args: any[]): void { | ||
if(Logger.active()) { | ||
console.log(Logger.colorFgWhite("[decade]"), ...args); | ||
console.log(Logger.colorFgWhite(`[${this.service}]`), ...args); | ||
} | ||
} | ||
public static good (...args: any[]): void { | ||
public detail (...args: any[]): void { | ||
if(Logger.active()) { | ||
console.log(Logger.colorFgGreen("[decade]"), ...args); | ||
console.log(Logger.colorFgYellow(`[${this.service}]`), ...args); | ||
} | ||
} | ||
public static error (...args: any[]): void { | ||
public good (...args: any[]): void { | ||
if(Logger.active()) { | ||
console.error(Logger.colorFgRed("[decade]"), ...args); | ||
console.log(Logger.colorFgGreen(`[${this.service}]`), ...args); | ||
} | ||
} | ||
public error (...args: any[]): void { | ||
if(Logger.active()) { | ||
console.error(Logger.colorFgRed(`[${this.service}]`), ...args); | ||
} | ||
} | ||
/** Color Utilities */ | ||
@@ -36,0 +46,0 @@ private static colorApply = (color: string, message: any) => `${color}${message}${Logger.colorReset()}`; |
@@ -11,3 +11,2 @@ import * as mocha from "mocha"; | ||
http: 3000, | ||
routes: [], | ||
}); | ||
@@ -14,0 +13,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
105819
60
1481
1
228