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

electron-proxy-store

v1.0.1

Published

Persistent, auto-saving data store for Electron utilizing the modern JavaScript Proxy handlers

Downloads

3

Readme

electron-proxy-store

Easy, auto-saving data persistence for your Electron app or module - Save and load user preferences, app state, cache, etc.

Electron doesn't have a built-in way to persist data across app launches. This module handles that for you, so you can focus on building your app.

Electron-proxy-store uses Proxies, the most modern way to handle fundamental actions (i.e. setting and deleting variables) and attach other actions to those actions, in this case, to automatically save the data object to disk.

Unlike other data storage modules which require you to use specific functions to set, get, delete, and otherwise interact with your store, Electron-proxy-store allows you to interact with the Store like any other variable.

The data is saved in a JSON file in (by default) app.getPath('userData').

You can use this module directly in both the main and renderer process.

Changes are written to disk atomically, so if the process crashes during a write, it will not corrupt the existing config.

Node.js (Install)

Requirements:

  • Node.js
  • npm (Node.js package manager)
npm install electron-proxy-store

Usage

const Store = require('electron-proxy-store')
const store = new Store()

// Assign properties as usual
store.data.unicorn = '🦄'
console.log(store.data.unicorn)
//=> '🦄'

delete store.data.unicorn
console.log(store.data.unicorn);
//=> undefined

// You can also nest as deep as you like
store.data.unicornFamily = {
    daddy = 'D🦄',
    mommy = 'M🦄',
    children = {
        sister = 's🦄',
        brother = 'b🦄'
    }
}

API

Store([options])

Returns a new instance.

Options

defaults

Type: Object

Default data.

name

Type: string Default: config

Name of the storage file (without extension).

This is useful if you want multiple storage files for your app. Or if you're making a reusable Electron module that persists some data, in which case you should not use the name config.

path

Type: string Default: app.getPath('userData').name.json

Storage file location. Don't specify this unless absolutely necessary!

If a relative path, it's relative to the current working directory (cwd). For example, {path: 'unicorn'} would result in a storage file in ~/Library/Application Support/App Name/unicorn.

defaults

Type: object Default: { }

encryptionKey

Type: string Default: undefined

This can theoretically be used as a secure way to store sensitive data by prompting the user for the password before loading the store. I do not guarantee the security of the implementation.

Note that this should not be used for security purposes if the encryption key is inside a plain-text Electron app. In this case, its main use would be for obscurity. If a user looks through the config directory and finds the config file, since it's just a JSON file, they may be tempted to modify it. By providing an encryption key, the file will be obfuscated, which should hopefully deter any users from doing so.

Even so, it has the added bonus of ensuring the config file's integrity. If the file is changed in any way, the decryption will not work, in which case you can specify the option useAsIntegrityCheck to reload the store back to its default state.

When specified, the store will be encrypted using AES encryption algorithm.

Encryption is handled using Evanvosberg's 'crypto-js' module.

useAsIntegrityCheck

Type: boolean Default: false

If set to true, the store will initialize with the default options if the stored file fails to be decrypted for any of the 2 following reasons:

  1. The file has been tampered with or has been corrupted.

  2. encryptionKey is incorrect.

Warning Loading the store with the defaults and the same path and name will overwrite the previous data.

Therefore, this should be set to false in almost all situations when using encryption.

decryptErrorHandler

Type: function Default: function (err) { }

The callback to execute when the file is read (see saveToDiskErrorHandler below) but fails to decrypt into a JavScript object or variable.

This is useful for re-prompting the user for a password or location.

This callback is SKIPPED if useAsIntegrityCheck is set to true.

saveToDiskErrorHandler

Type: function Default: function (err) { }

The callback to execute if there is an error opening the store file except when the file doesn't exist, such as on first launch, in which case the store creates a new file.

Instance

.name

The filename without the ".json".

.path

Path to the storage file.

.defaults

The default data. store.reset() will restore to this.

.encryptionKey

The current encryption key.

.data

Where the data is stored. Write properties to this object. Writing a value to store.data will overwrite the proxy, making it stop working.

.backup(location)

Saves a copy of store.data to location.

.changePassword(newPass)

Replaces old file with a file encrypted with newPass. Leaving newPass blank will write the file unencrypted.

.changeSaveLocation(newPath, newName)

Saves the store to newPath.newName.json. If newName is blank, the store will continue using the old filename.

.clear()

Sets store.data to an empty object, {}.

.save()

Saves the store data. There shouldn't really be any reason to call this yourself.

.reset()

Resets store.data to options.defaults.

.openInEditor()

Open the storage file in the user's editor.

License

MIT © ParmesanPangolin