global-box
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

global-box

Version David (path) TypeScript

This package will never have breaking change and keeps everything simple and backward-compatible.

A simple key-value store singleton.

Sometimes you have to do horrible things, like use the global object to share a singleton or some values.

Instead of attaching values to the global object, which can lead to security concerns, this global-box package provides a key-value store which is guaranteed to be a singleton, so you can use it instead of the global object. Only code that are part of the same application bundle can access this store, making it more secured.

Example

package A

import { globalBox } from 'global-box';
const box = globalBox();
box.set('share.config.something', 123);

package B

import { globalBox } from 'global-box';
const box = globalBox();
box.get('share.config.something'); // 123;

Installation

For this to work correctly, there must be only a single copy of global-box in node_modules at all times. (Similar to how there must be only a single copy of react.)

Note: This package will never have breaking change and keeps everything simple and backward-compatible.

  • For application developers, just do a regular installation. If you want to use global-box directly, or have some libraries that depends on it.
npm install global-box
  • For library authors, you must always list this as peerDependencies in package.json. Listing it as dependencies may cause the library consumers to have duplicates in the final application.

In library

{
  "peerDependencies": {
    "global-box": "*"
  },
  "devDependencies": {
    "global-box": "x.x"
  }
}

vs. in application

{
  "dependencies": {
    "global-box": "x.x"
  }
}

Available functions

import { globalBox } from 'global-box';
const box = globalBox();
box.has(globalId);
box.get<string>(globalId); // must specify value type via generic
box.getOrCreate(globalId, factory);
box.remove(globalId);
box.set(globalId);

Credits

Inspired by global-cache

License

Apache-2.0

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.2.0
    16,459
    • latest

Version History

Package Sidebar

Install

npm i global-box

Weekly Downloads

16,459

Version

1.2.0

License

Apache-2.0

Unpacked Size

24.9 kB

Total Files

20

Last publish

Collaborators

  • kristw