From c806e274e7381ab84e849cb662703b71b6bb3430 Mon Sep 17 00:00:00 2001 From: Paradoxe Date: Fri, 17 May 2019 22:29:18 +0200 Subject: [PATCH] feat(adoscope): watchers can be enabled/disabled via config file BREAKING CHANGE: Config file must be updated since keys has been updated or run instructions file --- config/adoscope.js | 38 +++++++++++- src/Adoscope.ts | 151 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 162 insertions(+), 27 deletions(-) diff --git a/config/adoscope.js b/config/adoscope.js index 6f86ad9..941b611 100644 --- a/config/adoscope.js +++ b/config/adoscope.js @@ -27,14 +27,46 @@ module.exports = { path: 'adoscope', mime_types: [ - // + 'text/plain' ], ignore_paths: [ - // + ], middleware: [ // - ] + ], + + watchers: { + query: { + enabled: true + }, + model: { + enabled: true, + options: { + ignore: [ + + ] // List of ignored models. + } + }, + request: { + enabled: true, + options: { + mime_types: [ + + ], // List of accepted mime types. + + ignore: [ + + ] // List of ignored paths. + } + }, + schedule: { + enabled: true + }, + view: { + enabled: true + } + } } diff --git a/src/Adoscope.ts b/src/Adoscope.ts index ce8718b..1b71d2a 100644 --- a/src/Adoscope.ts +++ b/src/Adoscope.ts @@ -10,8 +10,10 @@ import { ServerResponse } from 'http' import * as _ from 'lodash' import * as pluralize from 'pluralize' -import { ValueOf, Config, Route, Http, Logger, AdoscopeConfig, Helpers } from '../src/Contracts' +import { ValueOf, Config, Route, Http, Lucid, AdoscopeConfig, Helpers } from '../src/Contracts' +import AlreadyRegisteredWatcherException from './Exceptions/AlreadyRegisteredWatcherException' +import NotFoundWatcherException from './Exceptions/NotFoundWatcherException' import ScheduleWatcher from './Watchers/ScheduleWatcher' import RequestWatcher from './Watchers/RequestWatcher' import QueryWatcher from './Watchers/QueryWatcher' @@ -20,6 +22,8 @@ import ViewWatcher from './Watchers/ViewWatcher' import EntryType from './EntryType' +type Watcher = RequestWatcher | QueryWatcher | ModelWatcher | ScheduleWatcher + const pathToRegexp = require('path-to-regexp') /** @@ -32,20 +36,19 @@ const pathToRegexp = require('path-to-regexp') export default class Adoscope { private _onFinished: any - private _scheduleWatcher: ScheduleWatcher - private _requestWatcher: RequestWatcher - private _queryWatcher: QueryWatcher - private _modelWatcher: ModelWatcher public data: {[x: string]: any} = {} /** - * Creates an instance of Adoscope and registers the global variable Adoscope. - * + *Creates an instance of Adoscope and registers the global variable Adoscope. + * @param {*} _app * @param {AdoscopeConfig} _config * @param {Route.Manager} _route * @param {Helpers} _helpers + * @param {boolean} [_booted=false] + * @param {Map} [_watchers=new Map()] + * @param {RequestWatcher} [_requestWatcher=null] * @param {boolean} [_recordingRequest=false] * * @memberof Adoscope @@ -55,18 +58,15 @@ export default class Adoscope { private _config: AdoscopeConfig, private _route: Route.Manager, private _helpers: Helpers, - private _recordingRequest: boolean = false + private _booted: boolean = false, + private _watchers: Map = new Map(), + private _requestWatcher: RequestWatcher = null, + private _recordingRequest: boolean = false, ) { this._onFinished = this._app.use('on-finished') - this._scheduleWatcher = new ScheduleWatcher(this._helpers) - this._requestWatcher = new RequestWatcher(this._config, this._route) - this._queryWatcher = new QueryWatcher(this, this._app.use('Database')) - this._modelWatcher = new ModelWatcher(this._config) if (this.enabled()) { - this._scheduleWatcher.record() - this._queryWatcher.record() - this._modelWatcher.record() + this._boot() } _.each(_.values(EntryType), (entry: ValueOf) => { @@ -78,16 +78,86 @@ export default class Adoscope { } /** - * Checks if Adoscope is enabled or not. + * Boot application and register watchers. * - * @method enabled + * @private + * + * @method _boot + * + * @memberof Adoscope + */ + private _boot (): void { + if (this._booted) { + return + } + + this._booted = true + + if (this._shouldWatch('request')) { + this._requestWatcher = new RequestWatcher(this._config, this._route) + + this._addWatcher(this._requestWatcher) + } + + if (this._shouldWatch('query')) { + const queryWatcher = new QueryWatcher(this, this._app.use('Database')) + + this._addWatcher(queryWatcher) + + queryWatcher.record() + } + + if (this._shouldWatch('model')) { + const modelWatcher = new ModelWatcher(this._config) + + this._addWatcher(modelWatcher) + + modelWatcher.record() + } + + if (this._shouldWatch('schedule')) { + const scheduleWatcher = new ScheduleWatcher(this._helpers) + + this._addWatcher(scheduleWatcher) + + scheduleWatcher.record() + } + } + + /** + * Checks whether a watcher should be registered. + * + * @private + * + * @method _shouldWatch + * + * @param {string} watcher * * @returns {boolean} * * @memberof Adoscope */ - public enabled (): boolean { - return this._config.enabled + private _shouldWatch(watcher: string): boolean { + return this._config.watchers[watcher].enabled + } + + /** + * Checks whether a watcher is registered and add it. + * + * @private + * + * @method _addWatcher + * + * @param {Watcher} watcher + * + * @memberof Adoscope + */ + private _addWatcher(watcher: Watcher): void { + if (this._hasWatcher(watcher.type)) { + throw new AlreadyRegisteredWatcherException(watcher.constructor.name) + } + + this._watchers.set(watcher.type, watcher) } /** @@ -107,9 +177,7 @@ export default class Adoscope { private _approveRequest(url: string): boolean { let approved: boolean = true - _.each(_.concat(this._config.ignore_paths, [ - `${this._config.path}/(.*)?` - ]), (path: string) => { + _.each([...this._config.watchers.request.options.ignore, `${this._config.path}/(.*)?`], (path: string) => { path = path.replace(/\/\*/g, '/(.*)').replace(/^\/|\/$/g, '') if (pathToRegexp(path).exec(url.replace(/^\/|\/$/g, ''))) { @@ -122,11 +190,29 @@ export default class Adoscope { return approved } + private _hasWatcher (watcher: string): boolean { + return this._watchers.has(watcher) + } + + public get watchers (): Map { + return this._watchers + } + + public getWatcher (watcher: string): any { + if (!this._hasWatcher(watcher)) { + throw new NotFoundWatcherException(watcher) + } + + return this._watchers.get(watcher) + } + /** - * Getter for @_recordingRequest property + * Getter for _recordingRequest property * * @type {boolean} * + * @member recordingRequest + * * @memberof Adoscope */ get recordingRequest (): boolean { @@ -134,7 +220,11 @@ export default class Adoscope { } /** - * Setter for @_recordingRequest property + * Setter for _recordingRequest property + * + * @member recordingRequest + * + * @param {boolean} value * * @memberof Adoscope */ @@ -142,6 +232,19 @@ export default class Adoscope { this._recordingRequest = value } + /** + * Checks if Adoscope is enabled or not. + * + * @method enabled + * + * @returns {boolean} + * + * @memberof Adoscope + */ + public enabled (): boolean { + return this._config.enabled + } + /** * Sets client-side script variables. *