Helper

Internal helpers called by Bajo and other classes. It should remains hidden and not to be imported by any program. It listed here for documentation purpose only.

Methods

(async, static) bootOrder()

Determine the boot order of plugins based on their bajo.bootorder property in their package.json file. If not provided, the plugin will be booted in the order they are listed in the package.json file or .plugins file.

(async, static) buildConfig()

Building Bajo configuration object. Called after all config handlers are collected, this function will read config files written in any of config handlers' supported formats:

  • if runtime configuration object is provided (the one passed to the app constructor), it will be used as the base. Otherwise, files are read from the {dataDir}/config
  • then it will be merged with the configuration coming from the command line arguments and environment variables
  • doing some normalization and validation works, including setting the language, environment, log level, and cache purge settings
  • and setting up the app's log instance with the proper log level

(async, static) buildPlugins()

Load, build and sanitize all plugins defined in package.json or .plugins file, and add them to the app's plugin list. It will also make sure the main plugin is there too, if not, it will be created with the default template and settings.

Finally, it will loop through all loaded plugins and its config and language files.

This function is called by Bajo during the initialization process.

(async, static) checkDependencies()

Ensure all plugins' dependencies are met. If there is a missing dependency, an error will be thrown.

Semver is also supported, so if a plugin requires a specific version of another plugin, it will be checked as well.

(async, static) checkNameAliases()

Ensure the uniqueness of all plugins' name and alias. If there is a clash, an error will be thrown.

(async, static) collectConfigHandlers()

Collect all plugins' config handlers and push them to the app's configHandlers array.

(async, static) collectHooks()

Collect all plugins' hooks and push them to the app's hooks array.

(static) collectPlugins()

Collect all plugins defined in package.json or .plugins file, and add them to the app's plugin list. It will also make sure the main plugin is there too, if not, it will be created with the default template and settings.

(async, static) ensureDirs()

Ensure the existence of necessary directories for Bajo. This function is called by Bajo during the initialization process:

  • it sets up the base directory,
  • it checks the validity of the data directory,
  • it ensures the temporary directory exists,

(async, static) exitHandler()

Attach plugins exit handlers to make sure when the app shuts down, all plugins shut down gracefully first

(static) findDeep(item, paths) → {string}

Find item deep in paths.

Parameters:
NameTypeDescription
itemstring

Item to find.

pathsArray

Array of path to look for.

Returns:
Type: 
string

(static) freeze(obj, shallowopt)

Freeze object.

Parameters:
NameTypeAttributesDefaultDescription
objObject

Object to freeze.

shallowboolean<optional>
false

If false (default), deep freeze object.

(async, static) importModule(file, optionsopt) → {any}

Import file/module from any loaded plugins.

E.g. your plugin structure looks like this: |- src | |- lib | | |- my-module.js |- index.js |- package.json

And this is how to import my-module.js: javascript const { importModule } = this.app.bajo const myModule = await importModule('myPlugin:/src/lib/my-module.js')

Parameters:
NameTypeAttributesDefaultDescription
fileTNsPathPairs

File to import.

optionsObject<optional>
{}

Options.

Properties
NameTypeAttributesDefaultDescription
asDefaultImportboolean<optional>
true

If true (default), return default imported module.

asHandlerboolean<optional>

If true, return as a handler.

noCacheboolean<optional>

If true, always import as a fresh copy.

Returns:
Type: 
any

(static) outmatchNs(source, pattern) → {boolean}

Outmacth with support for scoped source and pattern. Scoped source/pattern is a source/pattern that has a plugin's namespace prefix, separated by : symbol.

This function is usefull for matching a source/pattern that is scoped to a specific plugin's namespace. For example, if you have a source myPlugin:foo/bar and a pattern myPlugin:foo/*, this function will return true because the source matches the pattern within the same namespace.

Parameters:
NameTypeDescription
sourcestring

Scoped source to match against the pattern. It should be in the format of ns:path

patternstring

Scoped pattern to match against the source. It should be in the format of ns:path

Returns:

Returns true if the source matches the pattern, otherwise false.

Type: 
boolean

(static) parseObject(obj, optionsopt) → {object}

Parse object and normalize their values.

Support automatic value translation for keys with t: prefix. The keys are then converted to the one without the t: prefix, and the original key is removed from the object.

See aneka.parseObject() for more details.

Parameters:
NameTypeAttributesDefaultDescription
obj*

The object to parse and normalize.

optionsobject<optional>
{}

Options for parsing and translation.

Returns:

The parsed and normalized object.

Type: 
object

(async, static) runAsApplet()

If app is in applet mode, this little helper should take care plugin's applet boot process

(async, static) runPlugins()

Run all loaded plugins' init() and start() methods in the order determined previously during boot process.

(inner) getLockFile(file, ensureFileopt) → {string}

Normalize file path to lock file. If the provided file is an absolute path, it will be returned as is. Otherwise, it will be converted to a lock file path in the temporary directory of the application.

Parameters:
NameTypeAttributesDefaultDescription
filestring

File path

ensureFileboolean<optional>
true

Whether to ensure the lock file exists. Defaults to true.

Returns:

Normalized lock file path.

Type: 
string

(async, inner) lockFile(file, optionsopt) → {Promise.<function()>}

Acquire a lock on a specified file to prevent concurrent access.

Parameters:
NameTypeAttributesDefaultDescription
filestring

The path to the file to lock. If no path found, it will be created in {tmpDir}. Can be in many formats, see getPluginFile for details.

optionsObject<optional>
{}

The options for locking the file.

Returns:
  • A promise that resolves to the lock release function.
Type: 
Promise.<function()>

(async, inner) setInterval(handler, timeout, optionsopt)

Shortcut to aneka.setInterval() with Bajo's lockFile support.

Parameters:
NameTypeAttributesDefaultDescription
handlerfunction

The function to be executed at each interval

timeoutnumber | string

In milliseconds or a string that can be parsed by aneka.parseDuration().

optionsObject | string<optional>
{}

If a string, it will be treated as lockFile path. Otherwise, it will be passed to aneka.setInterval() as is.

(async, inner) unlockFile(file, optionsopt) → {Promise.<(boolean|null)>}

Unlock a specified file that was previously locked, allowing concurrent access again.

If file does not exist, it will return true immediately.

Parameters:
NameTypeAttributesDefaultDescription
filestring

The path to the file to unlock. If no path found, it will be searched in {tmpDir}. Can be in many formats, see getPluginFile for details.

optionsObject<optional>
{}

The options for unlocking the file.

Returns:
  • A promise that resolves to true if the file was successfully unlocked, null if silent and an error occurred.
Type: 
Promise.<(boolean|null)>