@blamejs/core 0.5.10 → 0.5.11
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/CHANGELOG.md +1 -0
- package/index.js +2 -0
- package/lib/config.js +127 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -8,6 +8,7 @@ upgrading across more than a few patches at a time.
|
|
|
8
8
|
|
|
9
9
|
## v0.5.x
|
|
10
10
|
|
|
11
|
+
- **0.5.10** (2026-04-30) — b.middleware.sse: Server-Sent Events
|
|
11
12
|
- **0.5.9** (2026-04-30) — b.csv: RFC 4180 parser + serializer
|
|
12
13
|
- **0.5.8** (2026-04-30) — b.uuid: RFC 4122 v4 + RFC 9562 v7
|
|
13
14
|
- **0.5.7** (2026-04-30) — defensive validation + queue closure capture + audit context
|
package/index.js
CHANGED
|
@@ -106,6 +106,7 @@ var forms = require("./lib/forms");
|
|
|
106
106
|
var app = require("./lib/app");
|
|
107
107
|
var jobs = require("./lib/jobs");
|
|
108
108
|
var breakGlass = require("./lib/break-glass");
|
|
109
|
+
var config = require("./lib/config");
|
|
109
110
|
var csv = require("./lib/csv");
|
|
110
111
|
var uuid = require("./lib/uuid");
|
|
111
112
|
var mail = require("./lib/mail");
|
|
@@ -210,6 +211,7 @@ module.exports = {
|
|
|
210
211
|
createApp: app.createApp,
|
|
211
212
|
jobs: jobs,
|
|
212
213
|
breakGlass: breakGlass,
|
|
214
|
+
config: config,
|
|
213
215
|
csv: csv,
|
|
214
216
|
uuid: uuid,
|
|
215
217
|
mail: mail,
|
package/lib/config.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* config — schema-validated environment configuration.
|
|
4
|
+
*
|
|
5
|
+
* Operators read process.env throughout their app code. A typo in the
|
|
6
|
+
* key name OR a value in the wrong shape (port="abc", flag="yas")
|
|
7
|
+
* surfaces three days later as a mysterious 500. This primitive validates
|
|
8
|
+
* env at boot via b.safeSchema so the app refuses to start with broken
|
|
9
|
+
* config.
|
|
10
|
+
*
|
|
11
|
+
* var config = b.config.create({
|
|
12
|
+
* schema: b.safeSchema.object({
|
|
13
|
+
* NODE_ENV: b.safeSchema.enum_(["development", "test", "production"]),
|
|
14
|
+
* PORT: b.config.coerce.number().default(3000),
|
|
15
|
+
* LOG_LEVEL: b.safeSchema.enum_(["debug", "info", "warn", "error"]).default("info"),
|
|
16
|
+
* SESSION_SECRET: b.safeSchema.string().min(32),
|
|
17
|
+
* DATABASE_URL: b.safeSchema.string().url(),
|
|
18
|
+
* REDIS_URL: b.safeSchema.string().url().optional(),
|
|
19
|
+
* FEATURE_X: b.config.coerce.boolean().default(false),
|
|
20
|
+
* }),
|
|
21
|
+
* // env: process.env (default) — operators in tests pass a fake object
|
|
22
|
+
* // redactKeys: ["SESSION_SECRET", "DATABASE_URL"] — never log these
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* config.value → the validated, typed value object
|
|
26
|
+
* config.value.PORT → 3000 (Number, not "3000")
|
|
27
|
+
* config.boot() → throws ConfigError on validation failure;
|
|
28
|
+
* returns the validated value otherwise
|
|
29
|
+
*
|
|
30
|
+
* The factory immediately runs validation — the throw at create() time
|
|
31
|
+
* is intentional. Operators want config errors at app boot, not at
|
|
32
|
+
* the first request that touches the broken value.
|
|
33
|
+
*/
|
|
34
|
+
var safeSchema = require("./safe-schema");
|
|
35
|
+
var validateOpts = require("./validate-opts");
|
|
36
|
+
var { defineClass } = require("./framework-error");
|
|
37
|
+
|
|
38
|
+
var REDACT_MASK = "[REDACTED]";
|
|
39
|
+
|
|
40
|
+
var ConfigError = defineClass("ConfigError", { alwaysPermanent: true });
|
|
41
|
+
|
|
42
|
+
// Coercion shapes most operators want for env values: number, integer,
|
|
43
|
+
// boolean — env is always a string at the source so coerce. Operators
|
|
44
|
+
// who want stricter parsing chain .refine() or use raw schemas.
|
|
45
|
+
var coerce = {
|
|
46
|
+
number: function () {
|
|
47
|
+
return safeSchema.preprocess(function (v) {
|
|
48
|
+
if (v === undefined || v === null || v === "") return v;
|
|
49
|
+
var n = Number(v);
|
|
50
|
+
return isNaN(n) ? v : n;
|
|
51
|
+
}, safeSchema.number());
|
|
52
|
+
},
|
|
53
|
+
boolean: function () {
|
|
54
|
+
return safeSchema.preprocess(function (v) {
|
|
55
|
+
if (typeof v === "boolean") return v;
|
|
56
|
+
if (v === "1" || v === "true" || v === "yes") return true;
|
|
57
|
+
if (v === "0" || v === "false" || v === "no") return false;
|
|
58
|
+
return v; // pass through — schema rejects non-boolean
|
|
59
|
+
}, safeSchema.boolean());
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
function create(opts) {
|
|
64
|
+
if (!opts || typeof opts !== "object") {
|
|
65
|
+
throw new ConfigError("config/bad-opts",
|
|
66
|
+
"create: opts is required (must include opts.schema)");
|
|
67
|
+
}
|
|
68
|
+
validateOpts(opts, ["schema", "env", "redactKeys"], "config.create");
|
|
69
|
+
if (!opts.schema || typeof opts.schema.parse !== "function") {
|
|
70
|
+
throw new ConfigError("config/bad-schema",
|
|
71
|
+
"create: opts.schema must be a b.safeSchema instance (built via b.safeSchema.object({...}))");
|
|
72
|
+
}
|
|
73
|
+
var env = opts.env || process.env;
|
|
74
|
+
if (env !== process.env && (typeof env !== "object" || env === null)) {
|
|
75
|
+
throw new ConfigError("config/bad-env",
|
|
76
|
+
"create: opts.env must be an object (default process.env)");
|
|
77
|
+
}
|
|
78
|
+
var redactKeys = Array.isArray(opts.redactKeys) ? opts.redactKeys.slice() : [];
|
|
79
|
+
for (var i = 0; i < redactKeys.length; i++) {
|
|
80
|
+
if (typeof redactKeys[i] !== "string" || redactKeys[i].length === 0) {
|
|
81
|
+
throw new ConfigError("config/bad-redact-keys",
|
|
82
|
+
"create: redactKeys[" + i + "] must be a non-empty string");
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// Filter env to a plain object — process.env's prototype chain
|
|
86
|
+
// includes inherited Object.prototype keys we don't want to validate.
|
|
87
|
+
var input = {};
|
|
88
|
+
for (var k in env) {
|
|
89
|
+
if (Object.prototype.hasOwnProperty.call(env, k)) input[k] = env[k];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
var result = opts.schema.safeParse(input);
|
|
93
|
+
if (!result.ok) {
|
|
94
|
+
var msg = "config validation failed:\n";
|
|
95
|
+
for (var ei = 0; ei < result.errors.length; ei++) {
|
|
96
|
+
var err = result.errors[ei];
|
|
97
|
+
msg += " - " + err.path.join(".") + ": " + err.message + "\n";
|
|
98
|
+
}
|
|
99
|
+
throw new ConfigError("config/validation-failed", msg);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
var value = result.value;
|
|
103
|
+
|
|
104
|
+
function redactedView() {
|
|
105
|
+
// For logging the validated config without leaking secrets — useful
|
|
106
|
+
// at boot ("loaded config: { NODE_ENV: 'production', PORT: 3000, ... }").
|
|
107
|
+
var out = {};
|
|
108
|
+
for (var k in value) {
|
|
109
|
+
if (!Object.prototype.hasOwnProperty.call(value, k)) continue;
|
|
110
|
+
out[k] = redactKeys.indexOf(k) !== -1 ? REDACT_MASK : value[k];
|
|
111
|
+
}
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
value: value,
|
|
117
|
+
get: function (key) { return value[key]; },
|
|
118
|
+
has: function (key) { return Object.prototype.hasOwnProperty.call(value, key); },
|
|
119
|
+
redacted: redactedView,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
module.exports = {
|
|
124
|
+
create: create,
|
|
125
|
+
ConfigError: ConfigError,
|
|
126
|
+
coerce: coerce,
|
|
127
|
+
};
|