# expect-dotenv

Env config toolkit built on [dotenv](https://github.com/motdotla/dotenv) and
[env-var](https://github.com/evanshortiss/env-var), with `decrypt` / `verify`
/ `validate` / `plugin` tasks each run in a `worker_threads` worker so they
never block the event loop.

Installation:

```sh
npm i expect-dotenv
```

## Basic usage

```js
const env = require("expect-dotenv");

env.config(); // loads .env via dotenv

const port = env.get("PORT").default(3000).asPortNumber();
const hasApiKey = env.has("API_KEY");
const all = env.all();
```

## Worker-thread tasks

Each of these spawns a worker thread and resolves a promise with the
worker's result. No network calls are made by this package.

### decrypt

AES-256-CBC decrypt:

```js
const { ok, result } = await env.decrypt({
  encrypted: "...", // base64
  key: "...", // hex
  iv: "...", // hex
});
```

### verify

SHA256 signature verification:

```js
const { ok, result } = await env.verify({
  data: "payload to verify",
  signature: "...", // base64
  publicKey: "-----BEGIN PUBLIC KEY-----...",
});
```

### validate

Validate an env object against a simple schema:

```js
const { ok, errors } = await env.validate({
  env: process.env,
  schema: {
    PORT: { required: true, type: "number" },
    DEBUG: { type: "boolean" },
  },
});
```

### plugin

Loads a **local** module (by path, relative to `process.cwd()` unless
absolute) inside a worker thread and calls it if it exports a function:

```js
// plugins/my-plugin.js
module.exports = (arg1) => `hello ${arg1}`;
```

```js
const { ok, result } = await env.plugin({
  modulePath: "./plugins/my-plugin.js",
  args: ["world"],
});
```

`modulePath` always comes from your own code — this package never fetches
or evaluates code from a remote source.

## Worker files

- `lib/workers/runWorker.js` — spawns the right `*.worker.js` and forwards
  the payload; this is the piece originally named `runWorkers.js` in
  earlier versions of this package.
- `lib/workers/plugin.worker.js` — loads the local module passed via
  `modulePath` (renamed from the old `runWorkers.js` plugin-handling code).
- `lib/workers/decrypt.worker.js`, `verify.worker.js`, `validate.worker.js`
  — the corresponding task implementations.
