Skip to content

Commit

Permalink
feat(adoscope): watchers can be enabled/disabled via config file
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Config file must be updated since keys has been updated or run instructions file
  • Loading branch information
Melchyore committed May 17, 2019
1 parent 06ece2f commit c806e27
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 27 deletions.
38 changes: 35 additions & 3 deletions config/adoscope.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
151 changes: 127 additions & 24 deletions src/Adoscope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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')

/**
Expand All @@ -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<string, Watcher>} [_watchers=new Map()]
* @param {RequestWatcher} [_requestWatcher=null]
* @param {boolean} [_recordingRequest=false]
*
* @memberof Adoscope
Expand All @@ -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<string, Watcher> = 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<EntryType>) => {
Expand All @@ -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)
}

/**
Expand All @@ -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, ''))) {
Expand All @@ -122,26 +190,61 @@ export default class Adoscope {
return approved
}

private _hasWatcher (watcher: string): boolean {
return this._watchers.has(watcher)
}

public get watchers (): Map<string, Watcher> {
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 {
return this._recordingRequest
}

/**
* Setter for @_recordingRequest property
* Setter for _recordingRequest property
*
* @member recordingRequest
*
* @param {boolean} value
*
* @memberof Adoscope
*/
set recordingRequest (value: boolean) {
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.
*
Expand Down

0 comments on commit c806e27

Please sign in to comment.