@tezka/script-loader
v0.1.0
Published
Dynamic loader of JavaScript files
Downloads
5
Readme
sdk-load-script
This library exports an easy function to load a JavaScript file and make it available to the calling application to run.
How to use this library:
Step 1. Import the library and configure the ScriptLoader with the list of locations
from which one is allowed to load scripts, and optionally list of locations from which
loading scripts is prohibited. All rules must succeed in order to load a script, so you
can use an allow rule like /path/to/a and a deny rule like /path/to/a/b to allow
loading all scripts from /path/to/a except those in the /path/to/a/b subdirectory.
You can also add deny rules for system locations from which you know the program should
not be loading scripts, for safety, regardless of whatever other rules are later added
to allow other locations.
import { ScriptLoader, allowFilePath, denyFilePath } from '@tezka/script-loader';
const loader = new ScriptLoader({
fileAllowRules: [
allowFileFromPath('/path/to/plugins'),
denyFileFromPath('/etc')
]
});Step 2. Use the pre-configured ScriptLoader to load a script:
const pluginBasedir = '/path/to/plugins';
async function doSomething(pluginName) {
const plugin = await loader.loadFromFilePath(`${basedir}/${$pluginName}.js`);
// do something with `plugin`
}Or, if you know the script location is safe because you generate it programmatically
and don't accept any use input, you can call unsafeLoadScript with a file URL or
absolute file path:
import { unsafeLoadScript } from '@tezka/script-loader';
const pluginBasedir = '/path/to/plugins';
async function doSomething(pluginName) {
const plugin = await unsafeLoadScript(`${basedir}/${$pluginName}.js`);
// do something with `plugin`
}