11import { URL } from 'url' ;
22import { IBoundLog } from '@secret-agent/interfaces/ILog' ;
3- import { ICoreExtender , IOnCommandMeta } from '@secret-agent/interfaces/IPluginCoreExtender' ;
43import { IPuppetPage } from '@secret-agent/interfaces/IPuppetPage' ;
54import { IPuppetWorker } from '@secret-agent/interfaces/IPuppetWorker' ;
65import IHttpResourceLoadDetails from '@secret-agent/interfaces/IHttpResourceLoadDetails' ;
@@ -10,15 +9,16 @@ import ITlsSettings from '@secret-agent/interfaces/ITlsSettings';
109import { IInteractionGroups , IInteractionStep } from '@secret-agent/interfaces/IInteractions' ;
1110import IInteractionsHelper from '@secret-agent/interfaces/IInteractionsHelper' ;
1211import IPoint from '@secret-agent/interfaces/IPoint' ;
13- import {
12+ import ICorePlugin , {
13+ IHumanEmulator ,
1414 IBrowserEmulator ,
1515 IBrowserEmulatorConfig ,
1616 ISelectBrowserMeta ,
17- } from '@secret-agent/interfaces/IPluginBrowserEmulator' ;
18- import { IHumanEmulator } from '@secret-agent/interfaces/IPluginHumanEmulator' ;
19- import IPlugins from '@secret-agent/interfaces/IPlugins ' ;
20- import IPlugin , { IPluginClass } from '@secret-agent/interfaces/IPlugin ' ;
21- import IPluginCreateOptions from '@secret-agent/interfaces/IPluginCreateOptions ' ;
17+ ICorePluginClass ,
18+ IOnClientCommandMeta ,
19+ } from '@secret-agent/interfaces/ICorePlugin ' ;
20+ import ICorePlugins from '@secret-agent/interfaces/ICorePlugins ' ;
21+ import ICorePluginCreateOptions from '@secret-agent/interfaces/ICorePluginCreateOptions ' ;
2222import IBrowserEngine from '@secret-agent/interfaces/IBrowserEngine' ;
2323import { PluginTypes } from '@secret-agent/interfaces/IPluginTypes' ;
2424import Core from '../index' ;
@@ -34,16 +34,16 @@ interface IOptionsCreate {
3434 dependencyMap ?: { [ clientPluginId : string ] : string [ ] } ;
3535}
3636
37- export default class Plugins implements IPlugins {
38- public static pluginClassesById : { [ id : string ] : IPluginClass } = { } ;
37+ export default class CorePlugins implements ICorePlugins {
38+ public static corePluginClassesById : { [ id : string ] : ICorePluginClass } = { } ;
3939
4040 public readonly browserEngine : IBrowserEngine ;
4141 public readonly browserEmulator : IBrowserEmulator ;
4242 public readonly humanEmulator : IHumanEmulator ;
4343
44- private readonly instances : ICoreExtender [ ] = [ ] ;
45- private readonly instanceById : { [ id : string ] : ICoreExtender } = { } ;
46- private readonly createOptions : IPluginCreateOptions ;
44+ private readonly instances : ICorePlugin [ ] = [ ] ;
45+ private readonly instanceById : { [ id : string ] : ICorePlugin } = { } ;
46+ private readonly createOptions : ICorePluginCreateOptions ;
4747 private readonly logger : IBoundLog ;
4848
4949 constructor ( options : IOptionsCreate , logger : IBoundLog ) {
@@ -66,34 +66,34 @@ export default class Plugins implements IPlugins {
6666
6767 const { browserEngine, userAgentOption } =
6868 options . selectBrowserMeta || BrowserEmulator . selectBrowserMeta ( userAgentSelector ) ;
69- this . createOptions = { browserEngine, logger, plugins : this } ;
69+ this . createOptions = { browserEngine, userAgentOption , logger, corePlugins : this } ;
7070 this . browserEngine = browserEngine ;
7171 this . logger = logger ;
7272
73- this . browserEmulator = new BrowserEmulator ( this . createOptions , userAgentOption ) ;
73+ this . browserEmulator = new BrowserEmulator ( this . createOptions ) ;
7474 this . addPluginInstance ( this . browserEmulator ) ;
7575
7676 this . humanEmulator = new HumanEmulator ( this . createOptions ) ;
7777 this . addPluginInstance ( this . humanEmulator ) ;
7878
79- Core . pluginMap . coreExtenders . forEach ( x => this . use ( x ) ) ;
79+ Core . pluginMap . corePlugins . forEach ( x => this . use ( x ) ) ;
8080
8181 if ( dependencyMap && Core . allowDynamicPluginDependencies ) {
8282 Object . entries ( dependencyMap ) . forEach ( ( [ clientPluginId , dependentPluginIds ] ) => {
83- dependentPluginIds . forEach ( pluginId => {
84- if ( this . instanceById [ pluginId ] ) return ;
85- this . logger . info ( `Dynamically using ${ pluginId } required by ${ clientPluginId } ` ) ;
86- const Plugin = this . require ( pluginId ) ;
87- if ( ! Plugin ) {
88- this . logger . warn ( `Could not find ${ pluginId } required by ${ clientPluginId } ` ) ;
83+ dependentPluginIds . forEach ( corePluginId => {
84+ if ( this . instanceById [ corePluginId ] ) return ;
85+ this . logger . info ( `Dynamically using ${ corePluginId } required by ${ clientPluginId } ` ) ;
86+ const PluginObject = this . require ( corePluginId ) ;
87+ if ( ! PluginObject ) {
88+ this . logger . warn ( `Could not find ${ corePluginId } required by ${ clientPluginId } ` ) ;
8989 return ;
9090 }
91- const CoreExtender = ( Plugin as any ) . CoreExtender || Plugin ;
92- if ( CoreExtender . pluginType !== PluginTypes . CoreExtender ) {
93- this . logger . warn ( `Could not use ${ pluginId } because it's not a CoreExtender ` ) ;
91+ const CorePlugin = ( PluginObject as any ) . CorePlugin || PluginObject ;
92+ if ( CorePlugin . type !== PluginTypes . CorePlugin ) {
93+ this . logger . warn ( `Could not use ${ corePluginId } because it's not a CorePlugin ` ) ;
9494 return ;
9595 }
96- this . use ( CoreExtender ) ;
96+ this . use ( CorePlugin ) ;
9797 } ) ;
9898 } ) ;
9999 }
@@ -176,40 +176,40 @@ export default class Plugins implements IPlugins {
176176 // PLUGIN COMMANDS
177177
178178 public async onPluginCommand (
179- sendToPluginId : string ,
180- commandMeta : IOnCommandMeta ,
179+ toPluginId : string ,
180+ commandMeta : IOnClientCommandMeta ,
181181 args : any [ ] ,
182182 ) : Promise < any > {
183- const plugin = this . instanceById [ sendToPluginId ] ;
184- if ( plugin && plugin . onCommand ) {
185- return await plugin . onCommand ( commandMeta , ...args ) ;
183+ const plugin = this . instanceById [ toPluginId ] ;
184+ if ( plugin && plugin . onClientCommand ) {
185+ return await plugin . onClientCommand ( commandMeta , ...args ) ;
186186 }
187- this . logger . warn ( `Plugin (${ sendToPluginId } ) could not be found for command` ) ;
187+ this . logger . warn ( `Plugin (${ toPluginId } ) could not be found for command` ) ;
188188 }
189189
190190 // ADDING PLUGINS TO THE STACK
191191
192- public use ( Plugin : IPluginClass ) {
193- if ( this . instanceById [ Plugin . id ] ) return ;
194- this . addPluginInstance ( new Plugin ( this . createOptions ) ) ;
192+ public use ( CorePlugin : ICorePluginClass ) {
193+ if ( this . instanceById [ CorePlugin . id ] ) return ;
194+ this . addPluginInstance ( new CorePlugin ( this . createOptions ) ) ;
195195 }
196196
197- private addPluginInstance ( plugin : IPlugin ) {
198- this . instances . push ( plugin ) ;
199- this . instanceById [ plugin . id ] = plugin ;
197+ private addPluginInstance ( corePlugin : ICorePlugin ) {
198+ this . instances . push ( corePlugin ) ;
199+ this . instanceById [ corePlugin . id ] = corePlugin ;
200200 }
201201
202- private require ( pluginId : string ) : IPluginClass {
203- if ( ! Plugins . pluginClassesById [ pluginId ] ) {
202+ private require ( corePluginId : string ) : ICorePluginClass {
203+ if ( ! CorePlugins . corePluginClassesById [ corePluginId ] ) {
204204 try {
205205 // eslint-disable-next-line global-require,import/no-dynamic-require
206- const Plugin = require ( pluginId ) ;
207- if ( ! Plugin ) return ;
208- Plugins . pluginClassesById [ pluginId ] = Plugin . default || Plugin ;
206+ const CorePlugin = require ( corePluginId ) ;
207+ if ( ! CorePlugin ) return ;
208+ CorePlugins . corePluginClassesById [ corePluginId ] = CorePlugin . default || CorePlugin ;
209209 } catch ( error ) {
210210 return ;
211211 }
212212 }
213- return Plugins . pluginClassesById [ pluginId ] ;
213+ return CorePlugins . corePluginClassesById [ corePluginId ] ;
214214 }
215215}
0 commit comments