npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@spirex/config-provider-yaml

v1.0.0

Published

YAML file provider for @spirex/config — loads YAML files into layered configuration with file watching support

Downloads

64

Readme

@spirex/config-provider-yaml

YAML file provider for @spirex/config — read config from .yaml/.yml files with auto-flattening, key filtering, and live-reload on change.

License: MIT TypeScript

@spirex/config-provider-yaml is a configuration provider for @spirex/config that reads key-value pairs from YAML files. Nested objects are automatically flattened with : separators — so { db: { host: localhost } } becomes the familiar db:host key. Supports whitelist/blacklist filtering, optional files, and fs.watch–based hot reload.

Why use @spirex/config-provider-yaml?

  • YAML-native parsing — uses the yaml package for full YAML 1.2 support, including anchors, aliases, block scalars, and multi-document awareness.
  • Zero-config flattening — drop any YAML mapping in and nested keys become :-separated paths.
  • Flat-key priority — explicit "db:host": x overrides { db: { host: y } }; the most explicit form wins.
  • Key filtering — include or exclude sections with prefix-based whitelists and blacklists.
  • Hot reload — watch files for changes and pick up new values without restarting.
  • Optional filesnoThrow: true silently skips missing config files; perfect for local overrides.
  • Pure parserparseYaml() is exported separately for use outside the provider (no file I/O).
  • Human-readable config — YAML's indentation-based syntax is clean, comment-friendly, and widely used.
  • First-class TypeScript support — full type declarations included.

Installation

# npm
npm install @spirex/config-provider-yaml

# yarn
yarn add @spirex/config-provider-yaml

# pnpm
pnpm add @spirex/config-provider-yaml

The yaml package is included as a dependency and installed automatically.

Quick start

import { configBuilder } from "@spirex/config";
import { YamlConfigProvider } from "@spirex/config-provider-yaml";

const config = configBuilder()
  .add(new YamlConfigProvider("./config.defaults.yaml"))
  .add(new YamlConfigProvider("./config.local.yaml", { noThrow: true }))
  .build();

const host = config.getString("database:host");
const port = config.getInteger("database:port");

How it works

YAML flattening

Nested mappings are recursively flattened using : as the section separator. This works exactly like the JSON provider:

database:
  host: localhost
  port: 5432

app:
  name: MyApp

| Config key | Value | |---|---| | database:host | "localhost" | | database:port | "5432" | | app:name | "MyApp" |

All values are stored as strings — numbers, booleans, and dates are converted via String() during load. The core @spirex/config typed getters (getInteger, getBoolean, etc.) handle parsing back to the desired type. null / ~ values and empty arrays are skipped.

Flat keys take priority

A file can mix hierarchical and flat representations. When the same key appears in both forms, the flat (explicit) form wins:

db:
  host: nested
  port: 5432
db:host: explicit

| Key | Value | Reason | |---|---|---| | db:host | "explicit" | Flat key overrides nested | | db:port | "5432" | Only in nested form |

YAML types

YAML's richer type system is handled as follows:

| YAML type | Example | Stored as | |---|---|---| | String | hello | "hello" | | Integer | 42 | "42" | | Float | 3.14 | "3.14" | | Boolean | true / false | "true" / "false" | | Date | 2024-01-01 | stringified date | | null / ~ | ~ | skipped | | Array | [1, 2, 3] | "1,2,3" | | Empty array | [] | skipped | | Block scalar | \| or > | multi-line string | | Quoted string | 'hello' / "world" | unquoted string |

Note: The yaml package follows the YAML 1.2 spec. Values like yes, no, on, and off are not booleans in YAML 1.2 — they are kept as literal strings. Duplicate keys throw a YAMLParseError.

Top-level scalars and sequences

If the YAML file is a top-level scalar ("hello", 42) or sequence ([1, 2, 3]), no keys are produced and the cache stays empty. Only YAML mappings are flattened.

API reference

new YamlConfigProvider(filePath, options?)

Creates a new provider bound to a YAML file.

| Parameter | Type | Default | Description | |---|---|---|---| | filePath | string | (required) | Path to the YAML file. Relative paths are resolved against process.cwd(). Both .yaml and .yml extensions work. | | options.whitelist | string[] | — | Only include config keys starting with one of these prefixes. | | options.blacklist | string[] | — | Exclude config keys starting with one of these prefixes. | | options.watch | boolean | false | Watch the file for changes via fs.watch and auto-reload. | | options.noThrow | boolean | false | When true, a missing file is silently ignored instead of throwing. |

Methods

| Method | Returns | Description | |---|---|---| | get(key) | string \| undefined | Returns the value for key, or undefined if not found or before load(). | | load() | void | Reads and parses the YAML file. Throws if the file is missing (unless noThrow: true) or contains invalid YAML. | | unwatch() | void | Stops the file watcher if one is active. Safe to call multiple times or when no watcher exists. |

parseYaml(text)

A standalone pure function exported from @spirex/config-provider-yaml/yaml-reader. Parses a YAML string into a Map<string, string> — no file I/O, no side effects.

import { parseYaml } from "@spirex/config-provider-yaml/yaml-reader";

const map = parseYaml(`
  database:
    host: localhost
    port: 5432
  app:name: MyApp
`);
// Map { "database:host" → "localhost", "database:port" → "5432", "app:name" → "MyApp" }

| Parameter | Type | Description | |---|---|---| | text | string | Raw YAML content as a string. | | Returns | Map<string, string> | Parsed key-value pairs. null values, empty arrays, and top-level scalars/sequences are omitted. Throws on invalid YAML. |

Key filtering

Prefix-based whitelists and blacklists control which keys are available. When both are set, the blacklist takes priority.

// Only load database and app sections
new YamlConfigProvider("./config.yaml", { whitelist: ["database", "app"] })

// Exclude sensitive database keys
new YamlConfigProvider("./config.yaml", { blacklist: ["database:password"] })

// Combined: include database & app, but exclude database:password
new YamlConfigProvider("./config.yaml", {
  whitelist: ["database", "app"],
  blacklist: ["database:password"],
})

Layered configuration

Stack multiple providers for default + override patterns. Providers added later win:

import { configBuilder } from "@spirex/config";
import { YamlConfigProvider } from "@spirex/config-provider-yaml";
import { EnvConfigProvider } from "@spirex/config-provider-env";

const config = configBuilder()
  .add(new YamlConfigProvider("./config.defaults.yaml"))
  .add(new YamlConfigProvider("./config.local.yaml", { noThrow: true }))
  .add(new EnvConfigProvider("APP_", true))
  .build();

// Priority: env > config.local.yaml > config.defaults.yaml
const host = config.getString("database:host");

Watch mode

When watch: true, the provider monitors the YAML file for changes using fs.watch and automatically reloads with a 100ms debounce. Call unwatch() to stop watching.

const provider = new YamlConfigProvider("./config.yaml", { watch: true });
provider.load();

// File is now watched — changes are picked up automatically

// Later, when you want to stop:
provider.unwatch();

License

@spirex/config-provider-yaml is released under the MIT License.