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 🙏

© 2024 – Pkg Stats / Ryan Hefner

dynamic-config-listener

v1.0.5

Published

Dynamic Config Listener

Downloads

6

Readme

Dynamic Config Listener

dynamic-config-listener is a Node.js library designed to streamline the process of monitoring changes in configuration files within the file system. Through the use of an onChange callback, users can receive instantaneous notifications whenever alterations are made to the configuration. Then, it serialized the up-to-date config to specified serialization type.

Moreover, for situations where direct callback listening might not be preferable, the library offers a readData feature that allows for seamless retrieval of the most up-to-date configuration with optimal performance. This dual functionality ensures that users have the flexibility to choose the method that best suits their specific needs, enhancing the overall adaptability and efficiency of the library.

Installation

You can install dynamic-config-listener via npm:

npm install dynamic-config-listener

Usage

import ListenerBuilder, { FilePathType, SerializerType } from 'dynamic-config-listener';

const configListener = ListenerBuilder.builder('path/to/config.json')
  .setPathType(FilePathType.RELATIVE) // Set path type (optional, default is 'RELATIVE')
  .setEncoding('utf-8') // Set encoding (optional, default is 'utf-8')
  .setSerialization(SerializerType.JSON) // Set serialization type (optional, default is 'JSON')
  .setOnError((error, previousConfig) => {
    console.error('Error:', error);
    if (previousConfig) {
      console.log('Previous config:', previousConfig);
    }
  }) // Set error handler (optional)
  .build();

// Read the most-up-to-date data and start listening for config changes. 
// Retriggering of this method won't effect the performance since it use previous listener if exist
const data = await configListener.readData();

// To stop listening of config changes
configListener.stop();

API Reference

ListenerBuilder

builder(file: string): Static method that creates a new instance of ListenerBuilder.

  • file: Path to the configuration file.

setPathType(pathType: FilePathType): ListenerBuilder: Sets the type of file path (absolute or relative).

  • pathType: One of FilePathType.ABSOLUTE or FilePathType.RELATIVE.

setEncoding(encoding: BufferEncoding): ListenerBuilder: Sets the encoding for reading the configuration file. Default is 'utf-8'.

  • encoding: The encoding type (e.g., 'utf-8', 'ascii', etc.).

setSerialization(serialization: SerializerType): ListenerBuilder: Sets the serialization type for the configuration data. Default is SerializerType.JSON.

  • serialization: One of SerializerType.JSON or SerializerType.RAW.

setOnError(onError: (e: Error, previousConfig: SerializedData | undefined) => void): ListenerBuilder: Sets the error handler for any encountered errors.

  • onError: Callback function to handle errors.

build(): DynamicConfigListener: Builds the DynamicConfigListener instance with the specified options.

DynamicConfigListener

readData(): Promise<SerializedData>: Read and serialize data. Also starts listening for configuration changes.

stop(): void: Stops listening for configuration changes.

Types

DynamicConfigOptions

Options for configuring the DynamicConfigListener.

  • file: Path to the configuration file.
  • pathType: Type of file path (absolute or relative).
  • encoding: Encoding for reading the configuration file.
  • serialization: Serialization type for the configuration data.
  • onChange: Callback function to be executed when the configuration changes.
  • onError: Callback function to handle errors.

FilePathType

Enum for specifying the type of file path.

  • ABSOLUTE: Absolute file path.
  • RELATIVE: Relative file path.

SerializedData

Data structure containing both the raw and serialized representation of the configuration.

  • raw: Raw string data from the file.
  • serialized: Serialized object (if applicable).

SerializerType

Enum for specifying the type of serialization.

  • JSON: JSON serialization.
  • RAW: Raw serialization.

Contributing

Feel free to contribute by opening issues or pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details.