animastor-worker 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/worker-env.cjs ADDED
@@ -0,0 +1,50 @@
1
+ // ======================================================
2
+ // Animastor Private Worker — minimal .env loader (no deps)
3
+ // ======================================================
4
+ // Makes the worker runtime bundle self-contained: `cp .env.example .env`,
5
+ // edit, `node worker.cjs`. Loads `./.env` next to the worker entry file;
6
+ // REAL environment variables always win (the file never overrides them).
7
+ // Never logs values — the Worker Key stays out of logs.
8
+
9
+ const fs = require("fs");
10
+ const path = require("path");
11
+
12
+ /** Parse one KEY=VALUE line; returns [key, value] or null. */
13
+ function parseEnvLine(line) {
14
+ const m = String(line).match(/^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*?)\s*$/);
15
+ if (!m) return null;
16
+ let value = m[2];
17
+ if (value.length >= 2 && (
18
+ (value.startsWith('"') && value.endsWith('"')) ||
19
+ (value.startsWith("'") && value.endsWith("'"))
20
+ )) {
21
+ value = value.slice(1, -1);
22
+ }
23
+ return [m[1], value];
24
+ }
25
+
26
+ /**
27
+ * Load `./.env` from `dir` (default: this directory) into process.env.
28
+ * Existing process.env entries are NEVER overridden. Comments/blank lines
29
+ * are ignored. Returns true when a .env file was read.
30
+ */
31
+ function loadDotEnv(dir) {
32
+ const file = path.join(dir || __dirname, ".env");
33
+ let raw;
34
+ try {
35
+ raw = fs.readFileSync(file, "utf8");
36
+ } catch (_) {
37
+ return false; // no .env — environment variables only
38
+ }
39
+ for (const line of raw.split(/\r?\n/)) {
40
+ if (/^\s*(#|$)/.test(line)) continue;
41
+ const parsed = parseEnvLine(line);
42
+ if (!parsed) continue;
43
+ const [key, value] = parsed;
44
+ if (Object.prototype.hasOwnProperty.call(process.env, key)) continue;
45
+ process.env[key] = value;
46
+ }
47
+ return true;
48
+ }
49
+
50
+ module.exports = { loadDotEnv, parseEnvLine };