Comparing version 1.4.0 to 1.4.1
{ | ||
"name": "darcon", | ||
"version": "1.4.0", | ||
"version": "1.4.1", | ||
"description": "Simple Messaging/Service Bus for node entities over NatsIO", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -28,3 +28,2 @@ const _ = require('isa.js') | ||
if (fastifyConfig.defaultPlugins) | ||
@@ -31,0 +30,0 @@ await fastifyConfig.defaultPlugins( this.fastify ) |
@@ -11,2 +11,4 @@ let fs = require('fs') | ||
let { functions } = require( './Extender' ) | ||
let Assigner = require('assign.js') | ||
@@ -25,116 +27,124 @@ let assigner = new Assigner() | ||
mortar.init = async function (options = {}) { | ||
let self = this | ||
self.options = options | ||
Object.assign( mortar, { | ||
async events () { | ||
return [] | ||
}, | ||
async services () { | ||
return functions( this ) | ||
}, | ||
async init (options = {}) { | ||
let self = this | ||
self.options = options | ||
if (!self.configs) | ||
self.configs = {} | ||
self.watchMonitors = [] | ||
if (!self.configs) | ||
self.configs = {} | ||
self.watchMonitors = [] | ||
let extension = '.js' | ||
self.matcher = self.options.matcher || ( self.options.pattern ? function (filePath) { return self.options.pattern.test(filePath) } : function (filePath) { return filePath.endsWith( extension ) } ) | ||
let extension = '.js' | ||
self.matcher = self.options.matcher || ( self.options.pattern ? function (filePath) { return self.options.pattern.test(filePath) } : function (filePath) { return filePath.endsWith( extension ) } ) | ||
if ( !fs.existsSync( self.options.folder ) ) | ||
mkdirp.sync( self.options.folder ) | ||
if ( !fs.existsSync( self.options.folder ) ) | ||
mkdirp.sync( self.options.folder ) | ||
self.files = [] | ||
self.files = [] | ||
await self.firstRead() | ||
} | ||
mortar.firstRead = async function () { | ||
let self = this | ||
await self.firstRead() | ||
}, | ||
async firstRead () { | ||
let self = this | ||
if ( self.options.waitFor && self.options.waitFor.entity && !self.inflicterContext._barrel.firestarter( self.options.waitFor.entity ) ) { | ||
return setTimeout( () => { | ||
self.firstRead().then( () => {} ).catch( (reason) => { self.options.logger.darconlog(reason) } ) | ||
}, self.options.waitFor.timeout || 1000 ) | ||
} | ||
if ( self.options.waitFor && self.options.waitFor.entity && !self.inflicterContext._barrel.firestarter( self.options.waitFor.entity ) ) { | ||
return setTimeout( () => { | ||
self.firstRead().then( () => {} ).catch( (reason) => { self.options.logger.darconlog(reason) } ) | ||
}, self.options.waitFor.timeout || 1000 ) | ||
} | ||
let isComponent = function (filePath) { | ||
return self.matcher(filePath) | ||
} | ||
let isComponent = function (filePath) { | ||
return self.matcher(filePath) | ||
} | ||
await self.readFiles( self.options.folder ) | ||
await self.readFiles( self.options.folder ) | ||
await self.igniteFiles( ) | ||
await self.igniteFiles( ) | ||
if ( self.options.liveReload ) { | ||
self.watchMonitors.push( | ||
fs.watch( self.options.folder, { | ||
persistent: true, | ||
recursive: false, | ||
encoding: 'utf8' | ||
}, (eventType, filename) => { | ||
let filePath = path.resolve( self.options.folder, filename ) | ||
fs.exists( filePath, (exists) => { | ||
if ( !exists || !isComponent( filePath ) ) return | ||
self.scheduleFile( null, filePath ) | ||
if ( self.options.liveReload ) { | ||
self.watchMonitors.push( | ||
fs.watch( self.options.folder, { | ||
persistent: true, | ||
recursive: false, | ||
encoding: 'utf8' | ||
}, (eventType, filename) => { | ||
let filePath = path.resolve( self.options.folder, filename ) | ||
fs.exists( filePath, (exists) => { | ||
if ( !exists || !isComponent( filePath ) ) return | ||
self.scheduleFile( null, filePath ) | ||
} ) | ||
} ) | ||
} ) | ||
) | ||
) | ||
self.setInterval( async function () { | ||
self.options.logger.darconlog( null, 'Mortar is checking for entity changes', null, 'trace' ) | ||
return self.igniteFiles( ) | ||
}, self.options.liveReloadTimeout || 5000 ) | ||
} | ||
} | ||
mortar.addConfig = function ( name, config ) { | ||
this.configs[name] = config | ||
self.setInterval( async function () { | ||
self.options.logger.darconlog( null, 'Mortar is checking for entity changes', null, 'trace' ) | ||
return self.igniteFiles( ) | ||
}, self.options.liveReloadTimeout || 5000 ) | ||
} | ||
}, | ||
addConfig ( name, config ) { | ||
this.configs[name] = config | ||
return this | ||
} | ||
mortar.scheduleFile = function ( folder, fileName ) { | ||
let fPath = folder ? folder + path.sep + fileName : fileName | ||
if ( this.files.indexOf( fPath ) === -1 ) | ||
this.files.push( fPath ) | ||
return this | ||
} | ||
mortar.igniteFiles = async function ( ) { | ||
let self = this | ||
return this | ||
}, | ||
scheduleFile ( folder, fileName ) { | ||
let fPath = folder ? folder + path.sep + fileName : fileName | ||
if ( this.files.indexOf( fPath ) === -1 ) | ||
this.files.push( fPath ) | ||
return this | ||
}, | ||
async igniteFiles ( ) { | ||
let self = this | ||
let newFiles = self.files.slice() | ||
self.files.length = 0 | ||
// liveReloadTimeout: -1 | ||
newFiles.forEach( async function (newFile) { | ||
if ( fs.existsSync( newFile ) ) { | ||
self.options.logger.darconlog( null, '(Re)New entity file', newFile, 'info' ) | ||
let compName = newFile.substring( 0, newFile.length - 3 ) | ||
delete require.cache[require.resolve( compName )] | ||
let component = require( compName ) | ||
let newFiles = self.files.slice() | ||
self.files.length = 0 | ||
// liveReloadTimeout: -1 | ||
newFiles.forEach( async function (newFile) { | ||
if ( fs.existsSync( newFile ) ) { | ||
self.options.logger.darconlog( null, '(Re)New entity file', newFile, 'info' ) | ||
let compName = newFile.substring( 0, newFile.length - 3 ) | ||
delete require.cache[require.resolve( compName )] | ||
let component = require( compName ) | ||
if (component.passive) return | ||
if (component.passive) return | ||
if ( self.options.entityPatcher ) | ||
self.options.entityPatcher( component ) | ||
if ( !component.name ) | ||
throw new Error( 'Entity has no name', newFile ) | ||
if ( RESERVATION.find( (name) => { return name.toLowerCase() === component.name.toLowerCase() } ) ) | ||
throw new Error( 'Entity has forbidden name', newFile ) | ||
if ( component.adequate && !component.adequate() ) | ||
throw new Error( 'Entity failed to be adequate', newFile ) | ||
try { | ||
await self.Darcon.publish( component, assigner.assign( {}, globalConfig[component.name], self.configs[component.name] ) ) | ||
} catch (err) { | ||
self.options.logger.darconlog( err ) | ||
if ( self.options.entityPatcher ) | ||
self.options.entityPatcher( component ) | ||
if ( !component.name ) | ||
throw new Error( 'Entity has no name', newFile ) | ||
if ( RESERVATION.find( (name) => { return name.toLowerCase() === component.name.toLowerCase() } ) ) | ||
throw new Error( 'Entity has forbidden name', newFile ) | ||
if ( component.adequate && !component.adequate() ) | ||
throw new Error( 'Entity failed to be adequate', newFile ) | ||
try { | ||
await self.Darcon.publish( component, assigner.assign( {}, globalConfig[component.name], self.configs[component.name] ) ) | ||
} catch (err) { | ||
self.options.logger.darconlog( err ) | ||
} | ||
} else { | ||
self.options.logger.darconlog( null, 'Removed entity file', newFile, 'info' ) | ||
await self.Darcon.unpublish( path.basename( newFile, '.js') ) | ||
} | ||
} else { | ||
self.options.logger.darconlog( null, 'Removed entity file', newFile, 'info' ) | ||
await self.Darcon.unpublish( path.basename( newFile, '.js') ) | ||
} | ||
} ) | ||
} | ||
mortar.readFiles = async function ( folder ) { | ||
let files = await readdir( folder ) | ||
for (let i = 0; i < files.length; i += 1) | ||
if ( this.matcher(files[i]) ) | ||
this.scheduleFile( folder, files[i] ) | ||
} | ||
mortar.close = async function ( ) { | ||
this.watchMonitors.forEach( function ( watcher ) { | ||
watcher.close() | ||
} ) | ||
this.watchMonitors.length = 0 | ||
return 'Stopped' | ||
} | ||
} ) | ||
}, | ||
async readFiles ( folder ) { | ||
let files = await readdir( folder ) | ||
for (let i = 0; i < files.length; i += 1) | ||
if ( this.matcher(files[i]) ) | ||
this.scheduleFile( folder, files[i] ) | ||
}, | ||
async close ( ) { | ||
this.watchMonitors.forEach( function ( watcher ) { | ||
watcher.close() | ||
} ) | ||
this.watchMonitors.length = 0 | ||
return 'Stopped' | ||
} | ||
} ) | ||
@@ -141,0 +151,0 @@ module.exports = { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
56886
1333