@blamejs/core 0.12.64 → 0.12.66

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 CHANGED
@@ -8,6 +8,10 @@ upgrading across more than a few patches at a time.
8
8
 
9
9
  ## v0.12.x
10
10
 
11
+ - v0.12.66 (2026-05-26) — **`b.uriTemplate` — RFC 6570 URI Template expansion.** Expand RFC 6570 URI Templates — the {var} syntax that OpenAPI links, HAL _links, and hypermedia API clients use to turn a template plus a set of variables into a concrete URI. The full Level 4 grammar is supported: every operator ({+var} reserved, {#var} fragment, {.var} label, {/var} path, {;var} path-style parameters, {?var} query, {&var} query continuation), the {var:3} prefix modifier, and the {var*} explode modifier for lists and associative arrays. b.uriTemplate.expand(template, vars) returns the expanded string; b.uriTemplate.compile(template) parses once for templates applied to many variable sets. A malformed template (unclosed expression, reserved operator, non-numeric prefix, unmatched brace) throws UriTemplateError. Verified against the official uritemplate-test conformance suite (all 135 spec, extended, and negative cases). **Added:** *`b.uriTemplate.expand` / `b.uriTemplate.compile`* — RFC 6570 URI Template expansion, full Level 4. `expand(template, vars)` substitutes variables into a template and returns the URI; `compile(template)` returns a reusable `{ expand }` for repeated use. Variable values may be strings, numbers, booleans, arrays (lists), or plain objects (associative arrays); undefined, null, and empty list/map variables are omitted. All eight operators, the `:N` prefix modifier, and the `*` explode modifier follow §3.2, including reserved-set encoding for `{+var}` / `{#var}`. Composes naturally with `b.hal`, `b.linkHeader`, and `b.openapi` link objects. A malformed template throws `UriTemplateError`.
12
+
13
+ - v0.12.65 (2026-05-26) — **`b.base32` — RFC 4648 Base32 encode / decode.** Encode and decode RFC 4648 Base32 — the case-insensitive alphabet behind TOTP / 2FA secrets, DNSSEC NSEC3 hashes, and human-transcribable identifiers. Both RFC 4648 variants are supported: the standard alphabet (the default) and the extended-hex alphabet. b.base32.encode pads to an 8-character boundary by default (pass padding: false for the bare form TOTP key URIs use); b.base32.decode is strict by default but accepts the real-world shapes humans produce — lower-case, embedded spaces and dashes, missing padding — under loose: true. Verified against the RFC 4648 §10 test vectors for both alphabets. The TOTP primitive now composes this codec instead of carrying its own Base32 implementation. **Added:** *`b.base32.encode` / `b.base32.decode`* — RFC 4648 Base32 codec. `encode(buf, opts)` takes a Buffer or Uint8Array and returns a Base32 string, padded to an 8-character boundary unless `padding: false`; `decode(str, opts)` returns a Buffer. The `variant` option selects the standard (`"rfc4648"`, default) or extended-hex (`"rfc4648-hex"`) alphabet. Decoding is strict by default — any character outside the alphabet throws `Base32Error` — and `loose: true` up-cases the input and ignores embedded spaces, dashes, and missing padding, which is how copied TOTP keys and hand-typed codes arrive. **Changed:** *TOTP composes `b.base32`* — `b.auth.totp` now encodes and decodes its secrets through `b.base32` rather than a private Base32 implementation. Behavior is unchanged — secrets are still emitted unpadded and parsed leniently (case-insensitive, ignoring spaces and dashes).
14
+
11
15
  - v0.12.64 (2026-05-25) — **`b.jsonSchema` — JSON Schema 2020-12 validation.** Validate JSON against a JSON Schema 2020-12 document — the dialect OpenAPI 3.1 adopted and the most widely implemented schema language. b.jsonSchema.compile(schema) returns a reusable validator; b.jsonSchema.validate(schema, instance) compiles and runs in one call, returning { valid, errors } where each error names the failing instance location, keyword, and schema path. The full 2020-12 vocabulary is supported: every applicator (allOf / anyOf / oneOf / not / if-then-else, properties / patternProperties / additionalProperties / prefixItems / items / contains), the annotation-aware unevaluatedProperties / unevaluatedItems, every assertion keyword, and reference resolution ($ref / $anchor / $dynamicRef / $dynamicAnchor / $defs / $id base URIs). format is an annotation by default (opt in to assertion with assertFormat). External references resolve through an operator-supplied schema map — never a network fetch. Verified against the official JSON-Schema-Test-Suite (1292 of 1295 draft2020-12 cases; the remainder need the bundled dialect metaschema or $vocabulary selection, both opt-in). This is the standards-track counterpart to the fluent b.safeSchema builder and the portable b.jtd. **Added:** *`b.jsonSchema` — JSON Schema 2020-12* — `compile(schema, opts)` returns `{ validate, isValid }`; `validate(schema, instance, opts)` and `isValid(schema, instance, opts)` compile and run in one call. `validate` returns `{ valid, errors }`, each error a `{ instancePath, keyword, schemaPath, message }`. The full 2020-12 vocabulary is implemented — applicators, annotation-aware `unevaluatedProperties` / `unevaluatedItems`, every assertion keyword, and `$ref` / `$anchor` / `$dynamicRef` / `$dynamicAnchor` / `$defs` / `$id` resolution. `format` is an annotation by default (`assertFormat: true` to assert). External references resolve through `opts.schemas` (a URI→schema map), never a network fetch. Reach for it when the schema is an existing JSON Schema document (an API contract, OpenAPI component, or config schema); `b.safeSchema` remains the fluent in-process builder and `b.jtd` the portable codegen-friendly option. Validating a schema document against the dialect metaschema requires supplying that metaschema via `opts.schemas`, and `$vocabulary`-based keyword selection is not honored (every standard keyword always asserts).
12
16
 
13
17
  - v0.12.63 (2026-05-25) — **`b.cloudEvents` gains the JSON event format, batch, and the HTTP binding.** b.cloudEvents grows beyond wrap / parse into a full CloudEvents 1.0.2 surface. b.cloudEvents.validate / isValid check an envelope against the spec without throwing (the non-throwing companion to parse). toJSON / fromJSON serialize and parse the JSON event format, and toJSONBatch / fromJSONBatch handle the JSON batch format; untrusted bodies parse through the framework's bounded, prototype-pollution-safe JSON reader. The new http.* binding speaks both content modes the spec defines — binary mode spreads context attributes across percent-encoded ce-* headers with the data in the body, structured mode carries the whole event as application/cloudevents+json — plus the batch mode, and http.decode auto-detects the incoming mode from Content-Type exactly as a conformant receiver does. Verified against the spec's normative example events. **Added:** *`b.cloudEvents` JSON event format, batch, and HTTP binding* — `validate` / `isValid` report spec violations without throwing (the non-throwing companion to `parse`). `toJSON` / `fromJSON` and `toJSONBatch` / `fromJSONBatch` serialize and parse the JSON event and batch formats over the existing envelope shape. `http.encodeBinary` / `http.encodeStructured` / `http.encodeBatch` render the three HTTP content modes — binary spreads attributes across percent-encoded `ce-*` headers, structured and batched carry the event(s) as `application/cloudevents+json` / `application/cloudevents-batch+json` — and `http.decode` parses a request back into an envelope (or array) by auto-detecting the mode from `Content-Type`. `b.jtd` or `b.safeSchema` still validate the event's `data` payload. **Fixed:** *`b.csp.build` accepts `fenced-frame-src` and `webrtc`* — The CSP3 `fenced-frame-src` directive — which the default security-headers policy emits to block `<fencedframe>` embeds — was missing from the builder's recognized-directive set, so the default policy could not round-trip through `b.csp.build` (it threw `csp/unknown-directive`). Both `fenced-frame-src` and the CSP3 `webrtc` directive are now recognized.
package/README.md CHANGED
@@ -99,8 +99,10 @@ The framework bundles the surface a typical Node app reaches for. Every primitiv
99
99
  - **Signed webhooks + API encryption** — SLH-DSA-SHAKE-256f default; ML-DSA-65 opt-in; ECIES API encryption (`b.webhook`, `b.crypto`)
100
100
  - **HPKE / HTTP signatures** — RFC 9180 HPKE with ML-KEM-1024 + HKDF-SHA3-512 + ChaCha20-Poly1305 (`b.crypto.hpke`); RFC 9421 HTTP Message Signatures with derived components and ed25519 / ML-DSA-65 (`b.crypto.httpSig`); RFC 9530 Content-Digest / Repr-Digest body-integrity fields (SHA-256 / SHA-512, legacy algorithms refused — `b.contentDigest`) to sign the digest rather than the whole body
101
101
  - **Link header** — RFC 8288 Web Linking codec (`b.linkHeader.parse` / `serialize`): parse and build `Link: <uri>; rel="next"` relations, the standard REST pagination mechanism; quote-aware (a comma inside a quoted parameter never splits the list)
102
+ - **URI Templates** — RFC 6570 expansion (`b.uriTemplate.expand` / `compile`): full Level 4 — every operator, the `:N` prefix and `*` explode modifiers — turning `{/path}{?q*}` plus variables into a concrete URI; validated against the official uritemplate-test suite. The `{var}` syntax behind OpenAPI links and HAL `_links`
102
103
  - **JSON Type Definition** — RFC 8927 validation (`b.jtd.validate` / `isValid`): portable, cross-implementation schema validation (all eight forms — type / enum / elements / properties / values / discriminator / ref / empty), returning instancePath / schemaPath errors; validated against the official 316-case suite. Interop companion to the fluent `b.safeSchema` builder
103
104
  - **JSON Schema 2020-12** — the OpenAPI 3.1 dialect (`b.jsonSchema.compile` / `validate` / `isValid`): full vocabulary including every applicator, annotation-aware `unevaluatedProperties` / `unevaluatedItems`, and `$ref` / `$dynamicRef` / `$anchor` / `$id` resolution (external refs via an operator-supplied schema map, never a network fetch); `format` is an annotation unless `assertFormat` is set; returns located `{ valid, errors }`. Validated against the official JSON-Schema-Test-Suite. Standards-track counterpart to `b.safeSchema` and `b.jtd`
105
+ - **Base32** — RFC 4648 codec (`b.base32.encode` / `decode`): standard + extended-hex alphabets, padded or bare, strict or lenient decode (case-insensitive, ignoring spaces / dashes for copied TOTP keys); validated against the RFC 4648 §10 vectors. The codec behind `b.auth.totp` secrets
104
106
  - **JSONPath** — full RFC 9535 query evaluator (`b.jsonPath.query` / `paths`): name / wildcard / index / slice / descendant selectors, `?filter` expressions, and the five standard functions, with compile-time well-typedness checks (validated against the official 703-case compliance suite); complements the JSONPath guards
105
107
  - **JSON Pointer / Patch** — RFC 6901 `b.jsonPointer.get` (reference a value by `/foo/0/bar`) + RFC 6902 `b.jsonPatch.apply` (atomic add / remove / replace / move / copy / test for HTTP PATCH; the input document is never mutated, structural `test` comparison) + RFC 7396 `b.jsonMergePatch.merge` (the `merge-patch+json` partial-document format; both PATCH formats are prototype-pollution-safe)
106
108
  - **Canonical JSON** — RFC 8785 JSON Canonicalization Scheme (`b.canonicalJson.stringifyJcs`): the deterministic, sorted-key byte form to hash or sign (custom credentials, receipts, deterministic request signing); UTF-16 key ordering + ECMAScript number formatting, with a lenient `stringify` variant for Buffers / Dates / BigInts
package/index.js CHANGED
@@ -404,6 +404,8 @@ var jsonMergePatch = require("./lib/json-merge-patch");
404
404
  var jsonPath = require("./lib/json-path");
405
405
  var jtd = require("./lib/jtd");
406
406
  var jsonSchema = require("./lib/json-schema");
407
+ var base32 = require("./lib/base32");
408
+ var uriTemplate = require("./lib/uri-template");
407
409
  var standardWebhooks = require("./lib/standard-webhooks");
408
410
  var lro = require("./lib/lro");
409
411
  var jsonApi = require("./lib/jsonapi");
@@ -429,6 +431,8 @@ module.exports = {
429
431
  jsonPath: jsonPath,
430
432
  jtd: jtd,
431
433
  jsonSchema: jsonSchema,
434
+ base32: base32,
435
+ uriTemplate: uriTemplate,
432
436
  standardWebhooks: standardWebhooks,
433
437
  lro: lro,
434
438
  jsonApi: jsonApi,
package/lib/base32.js ADDED
@@ -0,0 +1,154 @@
1
+ "use strict";
2
+ /**
3
+ * @module b.base32
4
+ * @nav Data
5
+ * @title Base32
6
+ *
7
+ * @intro
8
+ * Encode and decode <a href="https://www.rfc-editor.org/rfc/rfc4648">RFC
9
+ * 4648</a> Base32 — the case-insensitive, digit-light alphabet used for
10
+ * TOTP / 2FA secrets, DNSSEC NSEC3 hashes, and human-transcribable
11
+ * identifiers. Both RFC 4648 variants are supported: the standard
12
+ * alphabet (<code>variant: "rfc4648"</code>, default) and the
13
+ * extended-hex alphabet (<code>variant: "rfc4648-hex"</code>, which sorts
14
+ * in the same order as the underlying bytes).
15
+ *
16
+ * <code>encode</code> pads to an 8-character boundary with
17
+ * <code>=</code> by default (pass <code>padding: false</code> for the
18
+ * bare form TOTP key URIs use). <code>decode</code> is strict by default
19
+ * — it rejects any character outside the alphabet — but
20
+ * <code>loose: true</code> accepts the real-world shapes humans produce:
21
+ * lower-case input, embedded spaces and dashes, and missing padding.
22
+ *
23
+ * @card
24
+ * RFC 4648 Base32 encode / decode (standard + extended-hex alphabets,
25
+ * padded or bare, strict or lenient) — the codec behind TOTP secrets and
26
+ * transcribable identifiers.
27
+ */
28
+
29
+ var { defineClass } = require("./framework-error");
30
+
31
+ var Base32Error = defineClass("Base32Error", { alwaysPermanent: true });
32
+
33
+ var ALPHABETS = {
34
+ "rfc4648": "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
35
+ "rfc4648-hex": "0123456789ABCDEFGHIJKLMNOPQRSTUV",
36
+ };
37
+ // Reverse lookups per variant: char-code → 5-bit value.
38
+ var LOOKUPS = {};
39
+ Object.keys(ALPHABETS).forEach(function (v) {
40
+ var map = {};
41
+ for (var i = 0; i < ALPHABETS[v].length; i++) map[ALPHABETS[v].charAt(i)] = i;
42
+ LOOKUPS[v] = map;
43
+ });
44
+
45
+ var GROUP = 8; // allow:raw-byte-literal — Base32 emits 8 chars per 5 input bytes (RFC 4648 §6)
46
+ var BITS = 5; // 5 bits per Base32 symbol
47
+
48
+ function _alphabet(variant) {
49
+ var a = ALPHABETS[variant || "rfc4648"];
50
+ if (!a) throw new Base32Error("base32/bad-variant", "base32: variant must be 'rfc4648' or 'rfc4648-hex'");
51
+ return a;
52
+ }
53
+
54
+ /**
55
+ * @primitive b.base32.encode
56
+ * @signature b.base32.encode(input, opts?)
57
+ * @since 0.12.65
58
+ * @status stable
59
+ * @related b.base32.decode
60
+ *
61
+ * Encode a Buffer (or Uint8Array) to an RFC 4648 Base32 string. Output is
62
+ * padded to an 8-character boundary with <code>=</code> unless
63
+ * <code>padding: false</code>. The empty input encodes to the empty string.
64
+ *
65
+ * @opts
66
+ * variant: "rfc4648" | "rfc4648-hex", // default: "rfc4648"
67
+ * padding: boolean, // default: true
68
+ *
69
+ * @example
70
+ * b.base32.encode(Buffer.from("foobar"));
71
+ * // → "MFRGGZDFMZTWQ===="
72
+ */
73
+ function encode(input, opts) {
74
+ opts = opts || {};
75
+ var buf;
76
+ if (Buffer.isBuffer(input)) buf = input;
77
+ else if (input instanceof Uint8Array) buf = Buffer.from(input);
78
+ else throw new Base32Error("base32/bad-input", "base32.encode: input must be a Buffer or Uint8Array");
79
+ var alphabet = _alphabet(opts.variant);
80
+ var pad = opts.padding !== false;
81
+
82
+ var out = "";
83
+ var value = 0, bits = 0;
84
+ for (var i = 0; i < buf.length; i++) {
85
+ value = (value << 8) | buf[i]; // allow:raw-byte-literal — shift in one input byte
86
+ bits += 8; // allow:raw-byte-literal — eight bits per input byte
87
+ while (bits >= BITS) {
88
+ out += alphabet.charAt((value >>> (bits - BITS)) & 31); // allow:raw-byte-literal — low 5 bits mask (2^5 - 1)
89
+ bits -= BITS;
90
+ }
91
+ }
92
+ if (bits > 0) out += alphabet.charAt((value << (BITS - bits)) & 31); // allow:raw-byte-literal — final partial group, low 5 bits
93
+ if (pad) while (out.length % GROUP !== 0) out += "=";
94
+ return out;
95
+ }
96
+
97
+ /**
98
+ * @primitive b.base32.decode
99
+ * @signature b.base32.decode(str, opts?)
100
+ * @since 0.12.65
101
+ * @status stable
102
+ * @related b.base32.encode
103
+ *
104
+ * Decode an RFC 4648 Base32 string to a Buffer. Strict by default: any
105
+ * character outside the variant's alphabet (other than trailing
106
+ * <code>=</code> padding) throws <code>Base32Error</code>. With
107
+ * <code>loose: true</code> the decoder up-cases the input and ignores
108
+ * embedded spaces and dashes (and missing padding) — the shapes TOTP keys
109
+ * and hand-typed codes take.
110
+ *
111
+ * @opts
112
+ * variant: "rfc4648" | "rfc4648-hex", // default: "rfc4648"
113
+ * loose: boolean, // default: false
114
+ *
115
+ * @example
116
+ * b.base32.decode("MFRGGZDFMZTWQ====").toString();
117
+ * // → "foobar"
118
+ */
119
+ function decode(str, opts) {
120
+ opts = opts || {};
121
+ if (typeof str !== "string") throw new Base32Error("base32/bad-input", "base32.decode: input must be a string");
122
+ _alphabet(opts.variant);
123
+ var lookup = LOOKUPS[opts.variant || "rfc4648"];
124
+ var loose = opts.loose === true;
125
+
126
+ var bytes = [];
127
+ var value = 0, bits = 0;
128
+ var inPad = false; // once "=" padding starts, only more "=" may follow
129
+ for (var i = 0; i < str.length; i++) {
130
+ var ch = str.charAt(i);
131
+ if (ch === "=") { inPad = true; continue; } // trailing padding
132
+ if (loose && (ch === " " || ch === "-")) continue; // ignore separators
133
+ // A data character after padding is malformed in either mode — the "="
134
+ // run must be trailing (rejects "M=Y======" / "MZXW=6YTB").
135
+ if (inPad) throw new Base32Error("base32/bad-char", "base32.decode: data character '" + ch + "' after padding at index " + i);
136
+ if (loose) ch = ch.toUpperCase();
137
+ var idx = lookup[ch];
138
+ if (idx === undefined) throw new Base32Error("base32/bad-char", "base32.decode: invalid Base32 character '" + str.charAt(i) + "' at index " + i);
139
+ value = (value << BITS) | idx;
140
+ bits += BITS;
141
+ if (bits >= 8) { // allow:raw-byte-literal — emit a full output byte
142
+ bytes.push((value >>> (bits - 8)) & 0xff); // allow:raw-byte-literal — eight-bit output byte mask
143
+ bits -= 8; // allow:raw-byte-literal — consumed eight bits
144
+ }
145
+ }
146
+ return Buffer.from(bytes);
147
+ }
148
+
149
+ module.exports = {
150
+ encode: encode,
151
+ decode: decode,
152
+ ALPHABETS: ALPHABETS,
153
+ Base32Error: Base32Error,
154
+ };
package/lib/totp.js CHANGED
@@ -61,6 +61,7 @@
61
61
  */
62
62
  var nodeCrypto = require("node:crypto");
63
63
  var C = require("./constants");
64
+ var base32 = require("./base32");
64
65
  var { generateBytes, generateToken, timingSafeEqual } = require("./crypto");
65
66
  var { AuthError } = require("./framework-error");
66
67
 
@@ -84,45 +85,23 @@ var DEFAULT_SECRET_BYTES = C.BYTES.bytes(128);
84
85
  var MIN_SECRET_BYTES = 20;
85
86
  // HOTP counter is an 8-byte big-endian field per RFC 4226 §5.1.
86
87
  var HOTP_COUNTER_BYTES = C.BYTES.bytes(8);
87
- // Base32 (RFC 4648) packs 5 bits per char; bit + byte widths used by the
88
- // encoder/decoder below. Routed through C.BYTES so every byte literal in
89
- // the file lives behind the same helper.
90
- var BITS_PER_BYTE = C.BYTES.bytes(8);
91
88
 
92
89
  // ---- Base32 (RFC 4648, no padding — TOTP convention) ----
93
-
94
- var BASE32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
90
+ // Composes b.base32: secrets are emitted unpadded and parsed leniently
91
+ // (case-insensitive, ignoring the spaces / dashes humans add when copying
92
+ // a key). An invalid character is surfaced as the TOTP-specific error code.
95
93
 
96
94
  function _base32Encode(buf) {
97
- var bits = "";
98
- for (var i = 0; i < buf.length; i++) {
99
- bits += buf[i].toString(2).padStart(BITS_PER_BYTE, "0");
100
- }
101
- var out = "";
102
- for (var j = 0; j < bits.length; j += 5) {
103
- var chunk = bits.substring(j, j + 5).padEnd(5, "0");
104
- out += BASE32[parseInt(chunk, 2)];
105
- }
106
- return out;
95
+ return base32.encode(buf, { padding: false });
107
96
  }
108
97
 
109
98
  function _base32Decode(str) {
110
- var bits = "";
111
- for (var i = 0; i < str.length; i++) {
112
- var c = str[i].toUpperCase();
113
- if (c === "=" || c === " " || c === "-") continue; // spaces + dashes + padding
114
- var idx = BASE32.indexOf(c);
115
- if (idx === -1) {
116
- throw new AuthError("auth-totp/bad-secret",
117
- "secret contains invalid base32 character: '" + str[i] + "'");
118
- }
119
- bits += idx.toString(2).padStart(5, "0");
120
- }
121
- var bytes = [];
122
- for (var j = 0; j + BITS_PER_BYTE <= bits.length; j += BITS_PER_BYTE) {
123
- bytes.push(parseInt(bits.substring(j, j + BITS_PER_BYTE), 2));
99
+ try {
100
+ return base32.decode(str, { loose: true });
101
+ } catch (e) {
102
+ throw new AuthError("auth-totp/bad-secret",
103
+ "secret contains invalid base32 character" + (e && e.message ? ": " + e.message : ""));
124
104
  }
125
- return Buffer.from(bytes);
126
105
  }
127
106
 
128
107
  // ---- Core HOTP (RFC 4226 §5.3) ----
@@ -0,0 +1,286 @@
1
+ "use strict";
2
+ /**
3
+ * @module b.uriTemplate
4
+ * @nav HTTP
5
+ * @title URI Templates
6
+ *
7
+ * @intro
8
+ * Expand <a href="https://www.rfc-editor.org/rfc/rfc6570">RFC 6570</a> URI
9
+ * Templates — the <code>{var}</code> syntax that OpenAPI links, HAL
10
+ * <code>_links</code>, and hypermedia API clients use to turn a template
11
+ * plus a set of variables into a concrete URI. The full Level 4 grammar
12
+ * is supported: every operator (<code>{+var}</code> reserved,
13
+ * <code>{#var}</code> fragment, <code>{.var}</code> label,
14
+ * <code>{/var}</code> path, <code>{;var}</code> path-style parameters,
15
+ * <code>{?var}</code> query, <code>{&amp;var}</code> query continuation),
16
+ * the <code>{var:3}</code> prefix modifier, and the <code>{var*}</code>
17
+ * explode modifier for lists and associative arrays.
18
+ *
19
+ * <code>expand(template, vars)</code> returns the expanded string;
20
+ * <code>compile(template)</code> parses once and returns a reusable
21
+ * <code>{ expand }</code> for templates applied to many variable sets. A
22
+ * malformed template (an unclosed expression, an unknown operator, or a
23
+ * non-numeric prefix) throws <code>UriTemplateError</code>.
24
+ *
25
+ * @card
26
+ * RFC 6570 URI Template expansion (full Level 4 — every operator, the
27
+ * <code>:N</code> prefix and <code>*</code> explode modifiers) — the
28
+ * <code>{var}</code> syntax behind OpenAPI links and HAL hypermedia.
29
+ */
30
+
31
+ var { defineClass } = require("./framework-error");
32
+
33
+ var UriTemplateError = defineClass("UriTemplateError", { alwaysPermanent: true });
34
+
35
+ var MAX_PREFIX = 10000; // allow:raw-byte-literal — RFC 6570 caps prefix length at 9999
36
+
37
+ // Operator table (RFC 6570 §2.2 / §3.2.1). first = prefix when any value is
38
+ // present; sep = separator between values; named = emit "name=value";
39
+ // ifemp = string used when a named value is empty; reserved = allow the
40
+ // reserved set through unencoded.
41
+ var OPERATORS = {
42
+ "": { first: "", sep: ",", named: false, ifemp: "", reserved: false },
43
+ "+": { first: "", sep: ",", named: false, ifemp: "", reserved: true },
44
+ "#": { first: "#", sep: ",", named: false, ifemp: "", reserved: true },
45
+ ".": { first: ".", sep: ".", named: false, ifemp: "", reserved: false },
46
+ "/": { first: "/", sep: "/", named: false, ifemp: "", reserved: false },
47
+ ";": { first: ";", sep: ";", named: true, ifemp: "", reserved: false },
48
+ "?": { first: "?", sep: "&", named: true, ifemp: "=", reserved: false },
49
+ "&": { first: "&", sep: "&", named: true, ifemp: "=", reserved: false },
50
+ };
51
+
52
+ var UNRESERVED = /[A-Za-z0-9\-._~]/;
53
+ // Reserved = gen-delims + sub-delims (RFC 3986 §2.2).
54
+ var RESERVED = /[:/?#[\]@!$&'()*+,;=]/;
55
+
56
+ function _pctEncode(str, allowReserved) {
57
+ var out = "";
58
+ for (var i = 0; i < str.length; i++) {
59
+ var ch = str.charAt(i);
60
+ // Preserve existing percent-encoded triplets when the reserved set is
61
+ // allowed (operators "+" and "#").
62
+ if (allowReserved && ch === "%" && /^[0-9A-Fa-f]{2}$/.test(str.substr(i + 1, 2))) {
63
+ out += str.substr(i, 3); i += 2; continue;
64
+ }
65
+ if (UNRESERVED.test(ch) || (allowReserved && RESERVED.test(ch))) { out += ch; continue; }
66
+ // Percent-encode the character's raw UTF-8 bytes (handles surrogate
67
+ // pairs). encodeURIComponent is not used — it leaves !*'() unencoded,
68
+ // which RFC 6570 unreserved-only expansion must escape.
69
+ var cp = str.codePointAt(i);
70
+ var bytes = Buffer.from(String.fromCodePoint(cp), "utf8");
71
+ for (var b = 0; b < bytes.length; b++) out += "%" + bytes[b].toString(16).toUpperCase().padStart(2, "0"); // allow:raw-byte-literal — hex radix
72
+ if (cp > 0xFFFF) i++; // consumed a surrogate pair // allow:raw-byte-literal — BMP boundary for surrogate-pair detection
73
+ }
74
+ return out;
75
+ }
76
+
77
+ function _allDigits(s) {
78
+ if (s.length === 0) return false;
79
+ for (var i = 0; i < s.length; i++) { var c = s.charCodeAt(i); if (c < 48 || c > 57) return false; } // allow:raw-byte-literal — ASCII '0'..'9' code-point bounds
80
+ return true;
81
+ }
82
+
83
+ // A composite member/value is "defined" unless it is undefined or null
84
+ // (an empty string, 0, or false is defined and expands).
85
+ function _memberDefined(v) { return v !== undefined && v !== null; }
86
+
87
+ function _isDefined(v) {
88
+ if (v === undefined || v === null) return false;
89
+ if (Array.isArray(v)) return v.length > 0;
90
+ if (typeof v === "object") return Object.keys(v).length > 0;
91
+ return true;
92
+ }
93
+ function _toStr(v) {
94
+ if (typeof v === "string") return v;
95
+ if (typeof v === "number" || typeof v === "boolean") return String(v);
96
+ return String(v);
97
+ }
98
+
99
+ // A literal run may not contain a stray "}" (an unmatched expression close)
100
+ // — RFC 6570 literals exclude "{" and "}".
101
+ function _checkLiteral(lit) {
102
+ if (lit.indexOf("}") !== -1) throw new UriTemplateError("uri-template/unmatched-brace", "uriTemplate: unmatched '}' in template literal");
103
+ }
104
+
105
+ // Parse a template into an array of literal strings + expression objects.
106
+ function _parse(template) {
107
+ if (typeof template !== "string") throw new UriTemplateError("uri-template/bad-template", "uriTemplate: template must be a string");
108
+ var parts = [];
109
+ var i = 0;
110
+ while (i < template.length) {
111
+ var open = template.indexOf("{", i);
112
+ if (open === -1) { _checkLiteral(template.slice(i)); parts.push({ literal: template.slice(i) }); break; }
113
+ if (open > i) { _checkLiteral(template.slice(i, open)); parts.push({ literal: template.slice(i, open) }); }
114
+ var close = template.indexOf("}", open);
115
+ if (close === -1) throw new UriTemplateError("uri-template/unclosed", "uriTemplate: unclosed expression at index " + open);
116
+ parts.push(_parseExpr(template.slice(open + 1, close)));
117
+ i = close + 1;
118
+ }
119
+ return parts;
120
+ }
121
+
122
+ function _parseExpr(body) {
123
+ if (body.length === 0) throw new UriTemplateError("uri-template/empty-expression", "uriTemplate: empty expression {}");
124
+ var op = "";
125
+ var c0 = body.charAt(0);
126
+ if ("+#./;?&".indexOf(c0) !== -1) { op = c0; body = body.slice(1); }
127
+ else if ("=,!@|".indexOf(c0) !== -1) {
128
+ // Operators reserved by RFC 6570 §2.2 for future extensions → error.
129
+ throw new UriTemplateError("uri-template/reserved-operator", "uriTemplate: operator '" + c0 + "' is reserved");
130
+ }
131
+ var specs = body.split(",").map(function (raw) {
132
+ if (raw.length === 0) throw new UriTemplateError("uri-template/bad-varspec", "uriTemplate: empty variable name");
133
+ var explode = false, prefix = null;
134
+ var name = raw;
135
+ if (raw.charAt(raw.length - 1) === "*") { explode = true; name = raw.slice(0, -1); }
136
+ else {
137
+ var colon = raw.indexOf(":");
138
+ if (colon !== -1) {
139
+ name = raw.slice(0, colon);
140
+ var n = raw.slice(colon + 1);
141
+ if (!_allDigits(n)) throw new UriTemplateError("uri-template/bad-prefix", "uriTemplate: prefix length must be a non-negative integer");
142
+ prefix = parseInt(n, 10);
143
+ if (prefix >= MAX_PREFIX) throw new UriTemplateError("uri-template/bad-prefix", "uriTemplate: prefix length exceeds 9999");
144
+ }
145
+ }
146
+ if (!/^(?:[A-Za-z0-9_]|%[0-9A-Fa-f]{2})(?:\.?(?:[A-Za-z0-9_]|%[0-9A-Fa-f]{2}))*$/.test(name)) {
147
+ throw new UriTemplateError("uri-template/bad-varname", "uriTemplate: invalid variable name '" + name + "'");
148
+ }
149
+ return { name: name, explode: explode, prefix: prefix };
150
+ });
151
+ return { op: op, specs: specs };
152
+ }
153
+
154
+ function _expandExpr(expr, vars) {
155
+ var o = OPERATORS[expr.op];
156
+ var pieces = [];
157
+ expr.specs.forEach(function (spec) {
158
+ var value = vars[spec.name];
159
+ if (!_isDefined(value)) return;
160
+
161
+ if (typeof value !== "object") {
162
+ // Simple string/number/boolean value.
163
+ var s = _toStr(value);
164
+ if (spec.prefix !== null) s = _sliceChars(s, spec.prefix);
165
+ pieces.push(_named(o, spec.name, _pctEncode(s, o.reserved), s.length === 0));
166
+ } else if (Array.isArray(value)) {
167
+ if (spec.prefix !== null) throw new UriTemplateError("uri-template/prefix-on-list", "uriTemplate: prefix modifier cannot apply to a list");
168
+ // Undefined / null members are ignored (RFC 6570 §3.2.1); a list with
169
+ // no defined members is treated as undefined and omitted entirely.
170
+ var members = value.filter(_memberDefined);
171
+ if (members.length === 0) return;
172
+ if (!spec.explode) {
173
+ var joined = members.map(function (m) { return _pctEncode(_toStr(m), o.reserved); }).join(",");
174
+ pieces.push(_named(o, spec.name, joined, false));
175
+ } else {
176
+ members.forEach(function (m) {
177
+ pieces.push(_named(o, spec.name, _pctEncode(_toStr(m), o.reserved), _toStr(m).length === 0));
178
+ });
179
+ }
180
+ } else {
181
+ // Associative array (object). Pairs whose value is undefined / null
182
+ // are omitted (RFC 6570 §3.2.1).
183
+ if (spec.prefix !== null) throw new UriTemplateError("uri-template/prefix-on-map", "uriTemplate: prefix modifier cannot apply to a map");
184
+ var keys = Object.keys(value).filter(function (k) { return _memberDefined(value[k]); });
185
+ if (keys.length === 0) return;
186
+ if (!spec.explode) {
187
+ var pairs = [];
188
+ keys.forEach(function (k) { pairs.push(_pctEncode(k, o.reserved)); pairs.push(_pctEncode(_toStr(value[k]), o.reserved)); });
189
+ pieces.push(_named(o, spec.name, pairs.join(","), false));
190
+ } else {
191
+ keys.forEach(function (k) {
192
+ // Exploded map: the key becomes the name.
193
+ pieces.push(_namedPair(o, _pctEncode(k, o.reserved), _pctEncode(_toStr(value[k]), o.reserved)));
194
+ });
195
+ }
196
+ }
197
+ });
198
+ if (pieces.length === 0) return "";
199
+ return o.first + pieces.join(o.sep);
200
+ }
201
+
202
+ // Build one "name=value" (or bare value) piece for a non-exploded or
203
+ // string varspec.
204
+ function _named(o, name, encodedValue, isEmpty) {
205
+ if (!o.named) return encodedValue;
206
+ if (isEmpty) return name + o.ifemp;
207
+ return name + "=" + encodedValue;
208
+ }
209
+ // Exploded map pair: key is already the name.
210
+ function _namedPair(o, encodedKey, encodedValue) {
211
+ if (!o.named) return encodedKey + "=" + encodedValue;
212
+ if (encodedValue.length === 0) return encodedKey + o.ifemp;
213
+ return encodedKey + "=" + encodedValue;
214
+ }
215
+
216
+ // Truncate to N Unicode code points (RFC 6570 prefix length is in chars).
217
+ function _sliceChars(s, n) {
218
+ var out = "", count = 0;
219
+ for (var i = 0; i < s.length && count < n; i++) {
220
+ var cp = s.codePointAt(i);
221
+ out += String.fromCodePoint(cp);
222
+ if (cp > 0xFFFF) i++; // allow:raw-byte-literal — BMP boundary for surrogate pairs
223
+ count++;
224
+ }
225
+ return out;
226
+ }
227
+
228
+ /**
229
+ * @primitive b.uriTemplate.compile
230
+ * @signature b.uriTemplate.compile(template)
231
+ * @since 0.12.66
232
+ * @status stable
233
+ * @related b.uriTemplate.expand, b.hal, b.linkHeader
234
+ *
235
+ * Parse an RFC 6570 URI Template once and return a reusable
236
+ * <code>{ expand(vars) }</code>, so a template applied to many variable
237
+ * sets is parsed a single time. Throws <code>UriTemplateError</code> if the
238
+ * template is malformed.
239
+ *
240
+ * @example
241
+ * var t = b.uriTemplate.compile("/users/{id}{?fields*}");
242
+ * t.expand({ id: 7, fields: ["name", "email"] });
243
+ * // → "/users/7?fields=name&fields=email"
244
+ */
245
+ function compile(template) {
246
+ var parts = _parse(template);
247
+ return {
248
+ expand: function (vars) {
249
+ vars = vars || {};
250
+ var out = "";
251
+ for (var i = 0; i < parts.length; i++) {
252
+ out += Object.prototype.hasOwnProperty.call(parts[i], "literal") ? parts[i].literal : _expandExpr(parts[i], vars);
253
+ }
254
+ return out;
255
+ },
256
+ };
257
+ }
258
+
259
+ /**
260
+ * @primitive b.uriTemplate.expand
261
+ * @signature b.uriTemplate.expand(template, vars)
262
+ * @since 0.12.66
263
+ * @status stable
264
+ * @related b.uriTemplate.compile
265
+ *
266
+ * Expand an RFC 6570 URI Template against a set of variables and return the
267
+ * resulting URI string. Variable values may be strings, numbers, booleans,
268
+ * arrays (lists), or plain objects (associative arrays); an undefined,
269
+ * null, or empty list/map variable is omitted. Reserved-set encoding,
270
+ * <code>:N</code> prefixes, and <code>*</code> explosion follow RFC 6570
271
+ * §3.2. Throws <code>UriTemplateError</code> on a malformed template.
272
+ *
273
+ * @example
274
+ * b.uriTemplate.expand("{/path}/here{?q,limit}",
275
+ * { path: "search", q: "json schema", limit: 10 });
276
+ * // → "/search/here?q=json%20schema&limit=10"
277
+ */
278
+ function expand(template, vars) {
279
+ return compile(template).expand(vars);
280
+ }
281
+
282
+ module.exports = {
283
+ expand: expand,
284
+ compile: compile,
285
+ UriTemplateError: UriTemplateError,
286
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blamejs/core",
3
- "version": "0.12.64",
3
+ "version": "0.12.66",
4
4
  "description": "The Node framework that owns its stack.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "blamejs contributors",
package/sbom.cdx.json CHANGED
@@ -2,10 +2,10 @@
2
2
  "$schema": "http://cyclonedx.org/schema/bom-1.5.schema.json",
3
3
  "bomFormat": "CycloneDX",
4
4
  "specVersion": "1.5",
5
- "serialNumber": "urn:uuid:35a8fec8-fa70-4913-a315-c839ce432741",
5
+ "serialNumber": "urn:uuid:4e9da547-078d-46ac-833b-3eae7e281a06",
6
6
  "version": 1,
7
7
  "metadata": {
8
- "timestamp": "2026-05-26T07:36:53.769Z",
8
+ "timestamp": "2026-05-26T10:01:10.136Z",
9
9
  "lifecycles": [
10
10
  {
11
11
  "phase": "build"
@@ -19,14 +19,14 @@
19
19
  }
20
20
  ],
21
21
  "component": {
22
- "bom-ref": "@blamejs/core@0.12.64",
22
+ "bom-ref": "@blamejs/core@0.12.66",
23
23
  "type": "application",
24
24
  "name": "blamejs",
25
- "version": "0.12.64",
25
+ "version": "0.12.66",
26
26
  "scope": "required",
27
27
  "author": "blamejs contributors",
28
28
  "description": "The Node framework that owns its stack.",
29
- "purl": "pkg:npm/%40blamejs/core@0.12.64",
29
+ "purl": "pkg:npm/%40blamejs/core@0.12.66",
30
30
  "properties": [],
31
31
  "externalReferences": [
32
32
  {
@@ -54,7 +54,7 @@
54
54
  "components": [],
55
55
  "dependencies": [
56
56
  {
57
- "ref": "@blamejs/core@0.12.64",
57
+ "ref": "@blamejs/core@0.12.66",
58
58
  "dependsOn": []
59
59
  }
60
60
  ]