Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

light-config

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

light-config

Share specific subsets of your server-side config with the browser

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

Light Config

Share specific subsets of your server-side config with the browser

Installation

npm i light-config --save

The Problem

Say one has the following JSON config for a Node.js app:

{
	"version": "1.0.0",
	"environment": "production1",
	"services": {
		"payments": {
			"url": "https://api/payments"
		},

		"users": {
			"url": "https://api/users"
		}
	},

	"clusters": {
		[...]
	},

	"messageQueue": {
		[...]
	}
}

If one wishes to retrieve the services property in the browser to make a client-side XMLHttpRequest, it becomes necessary to pass the configuration to the client. In the above example, the other properties are only relevant to the server, and passing a large config in its entirety will result in an increased response size.

The Solution

Light Config allows one to create a subset of a config. For example:

// ECMAScript 5: var lightConfig = require('light-config');
import lightConfig from 'light-config';
import serverConfig from './serverConfig.json';

const clientConfig = lightConfig(serverConfig, ['environment', 'services.users']);

/* Outputs:
 * {
 *		environment: 'production1',
 		services: {
 			users: {
 				url: 'https://api/users'
 			}
 		}
 * }

console.log(clientConfig);

As can be seen, the format of the server config is maintained (i.e. clientConfig.services.users.url). This is particularly useful when developing isomorphic apps.

clientConfig can be passed to the client in various ways. In Express, one could pass it as a cookie header:

response.set('Set-Cookie', JSON.stringify(clientConfig));

Alternatively, one could pass it to a template and attach it to the window:

response.render('view.handlebars', { clientConfig: JSON.stringify(clientConfig) });

// view.handlebars
<script>window.NAMESPACE.config = {{{ clientConfig }}}</script>

API

lightConfig(serverConfig, propertyTrees)

Returns a subset of the specified server config object according to the propertyTrees array, which can contain root properties (e.g. ['environment']) or deep properties (e.g. ['services.users']); the latter honours the existing contract.

Keywords

FAQs

Package last updated on 03 Nov 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc