NodeSecure runtime configuration.
- Node.js v18 or higher
This package is available in the Node Package Repository and can be easily installed with npm or yarn.
$ npm i @nodesecure/rc
# or
$ yarn add @nodesecure/rcread:
import*asRCfrom"@nodesecure/rc";constconfigurationPayload=(awaitRC.read(void0,{createIfDoesNotExist: true})).unwrap();console.log(configurationPayload);write:
importassertfrom"node:assert/strict";import*asRCfrom"@nodesecure/rc";constwriteOpts: RC.writeOptions={payload: {version: "2.0.0"},partialUpdate: true,};constresult=(awaitRC.write(void0,writeOpts)).unwrap();assert.strictEqual(result,void0);memoize/memoized:
import*asRCfrom"@nodesecure/rc";importassertfrom"node:assert";constconfigurationPayload=(awaitRC.read(void0,{createMode: "ci"})).unwrap()RC.memoize(configurationPayload,{overwrite: true});constmemoizedPayload=RC.memoized();assert.deepEqual(configurationPayload,memoizedPayload);👀 .read and .write return Rust like Result object.
Note
If undefined, the location will be assigned to process.cwd().
interfacecreateReadOptions{/** * If enabled, the file will be created if it does not exist on disk. * * @default false */createIfDoesNotExist?: boolean;/** * Generate a more or less complete configuration. * * @default `minimal` */createMode?: RCGenerationMode|RCGenerationMode[];/** * Automatically cache the configuration when enabled. * * @default false */memoize?: boolean;}exporttypereadOptions=RequireAtLeastOne<createReadOptions,"createIfDoesNotExist"|"createMode">;The createIfDoesNotExist argument can be ignored if createMode is provided.
import*asRCfrom"@nodesecure/rc";constconfigurationPayload=(awaitRC.read(void0,{createMode: "ci"})).unwrap();console.log(configurationPayload);By default the write API will overwrite the current payload with the provided one. When the partialUpdate option is enabled it will merge the new properties with the existing one.
/** * Overwrite the complete payload. partialUpdate property is mandatory. */exportinterfacewriteCompletePayload{payload: RC;partialUpdate?: false;}/** * Partially update the payload. This implies not to rewrite the content of the file when enabled. **/exportinterfacewritePartialPayload{payload: Partial<RC>;partialUpdate: true;}exporttypewriteOptions=writeCompletePayload|writePartialPayload;By default, the memory API overwrites the previous stored payload. When the OVERWRITE option is false, it merges new properties with existing properties.
exportinterfacememoizeOptions{overwrite?: boolean;}The overwrite option is used to specify whether data should be overwritten or merged.
This method returns null, when the default value is null, otherwise, it returns the current value of memoizedValue.
exportinterfacememoizedOptions{defaultValue: Partial<RC>;}If the defaultValue property is at null, then this value will be returned when memoized is called.
Same as memoized but return an Option monad.
import*asRCfrom"@nodesecure/rc";constmemoized=RC.maybeMemoized().unwrapOr({});// Some default RC hereClear/reset memoized RC
Dedicated directory for NodeSecure to store the configuration in the os HOME directory.
import*asRCfrom"@nodesecure/rc";consthomedir=RC.homedir();importassertfrom"node:assert/strict";import*asRCfrom"@nodesecure/rc";assert.strictEqual(RC.CONSTANTS.CONFIGURATION_NAME,".nodesecurerc");We provide by default a configuration generation that we consider minimal. On the contrary, a complete value will indicate the generation with all possible default keys.
exporttypeRCGenerationMode="minimal"|"ci"|"report"|"scanner"|"complete";However, depending on the NodeSecure tool you are working on, it can be interesting to generate a configuration with some property sets specific to your needs.
Note that you can combine several modes:
import*asRCfrom"@nodesecure/rc";awaitRC.read(void0,{createMode: ["ci","report"]});The runtime configuration is validated using a JSON Schema: ./src/schema/nodesecurerc.json.
It can be retrieved via API if needed:
import*asRCfrom"@nodesecure/rc";console.log(RC.JSONSchema);The JSON schema is a composition of multiple definitions for each tool:
Thanks goes to these wonderful people (emoji key):
Gentilhomme 💻🐛👀📖🛡️ | Antoine Coulon 💻🐛👀 | PierreD 💻 | Kouadio Fabrice Nguessan 💻🚧 |
MIT