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

node-object-sync

v1.0.2

Published

A small library for syncing on-disk config files with the in-memory object state.

Downloads

15

Readme

node-object-sync

This module is a small drop-in utility for automatically and transparently synchronizing the contents of on-disk configuration files with the in-memory state.

Documentation

Basic setup

The main functionality of this utility is simple - It lets you wrap an existing object. You can interact with that object through the "wrapper" completely transparently. Each time a property is added, reassigned or deleted, the state of your object will be written to the disk.


const myConfig = {
    logLevel: "info",
    useTimestamps: true,
    maxLogFileSize: "20MB",
    ...
}

// Wrap "myConfig" and use the new wrapper as a proxy and specify 
// the path to the location where the config is being saved to.
const myWrappedConfig = ObjectSync.wrap(myConfig, "/path/to/my-config.json")

// Modify "logLevel"
myWrappedConfig.logLevel = "debug"
// After the property is reassigned, the changes are immediately
// saved to the disk.

API

There is no API!

You can interact with your object directly like you have done to this time and the SyncedObject instance will act as a middleman, monitoring your changes and writing them to the configuration file for you.

Configuration options

  • param #0: target

    Type: <Object>

    Defines the default content of the configuration file that is written to it if the file doesn't already exist.

  • param #1: fileLocation

    Type: <string>

    Defines the location where wrapped object is stored.
    If the file already exists in the specified location then all of it's data will be loaded.
    If not, a new file will be created and the content of defaultContent will be written to it.

  • recursive

    Type: <boolean>
    Default: false

    Analogue to a recursive fs.mkDir.
    Defines whether or not to create the file's parent directory recursively.

  • save

    Type: <"sync" | "async" | number>
    Default: "sync"

    Defines the behavior of synchronizing the configuration file's content with the in-memory state.

    • <"sync"> - Overwrites the configuration file immediately after any object modifications. This behavior is using fs.writeFileSync under the hood and is blocking!
      Note: Any write, permission or filesystem errors will be thrown on assign and delete actions!

    • <"async"> - Overwrites the configuration file immediately after any object modifications, but with use of the asynchronous fs API.
      Note: Any errors thrown during the file save are silenced to prevent application crashes.

    • <number> - Overwrites the configuration file after a specified timeout, asynchronously. This saving method is a "lazy" method and is best suited for when the target object is expected to receive lots of writes.
      If set to 1s, and two writes were received 300ms apart, the file would be overwritten exactly 1s after the most recent write, so in this case after 1.3s. If any writes are made within 1s of the last one, the timeout is simply moved a second later, until the frequency is lower.

  • format

    Type: <{ parse: Function, stringify: Function }>
    Default: JSON.parse & JSON.stringify

    Defines the parse and stringify methods used to read/write from/to the file. By default the JSON format is used, but these can be used to set any arbitrary configuration format, such as Yaml, XML, PKL, INI or any other.