Socket
Socket
Sign inDemoInstall

clarity

Package Overview
Dependencies
2
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.4 to 0.1.5

test/defaults.js

4

index.js
var data = {},
reObfuscatedUser = /([\w\-]+)\:(\*+)/,
reObfuscatedVariable = /\*{2}([\w\-]+)\*{2}/,
reObfuscatedVariable = /\*{2}([\w\-]+)(\|.*)?\*{2}/,
hasOwn = Object.prototype.hasOwnProperty;

@@ -47,3 +47,3 @@

parts[0] = input.slice(0, matchVariable.index);
parts[1] = data[matchVariable[1]];
parts[1] = data[matchVariable[1]] || matchVariable[2].slice(1);
parts[2] = input.slice(matchVariable.index + matchVariable[0].length);

@@ -50,0 +50,0 @@

@@ -10,3 +10,3 @@ {

],
"version": "0.1.4",
"version": "0.1.5",
"engines": {

@@ -13,0 +13,0 @@ "node": ">= 0.6.x < 0.9.0"

@@ -7,17 +7,52 @@ # clarity

Let's look at a really simple example first. In this example, we have a url to a [CouchDB](http://couchdb.apache.org/) instance stored in our application code:
## Replacing Simple Values
The most common use case when using clarity is to use it to replace configuration values with configuration values that are stored in environment variables. For instance, consider the following simple example `config.json` file:
```
{
"dbserver": "localhost",
"dbport": "7632",
"dbuser": "admin",
"dbpass": "teapot"
}
```
If we were then to import our configuration into our node program, we would be able to access the configuration values:
```js
var targetUrl = 'http://test:test@damonoehlman.iriscouch.com/clarity-tests',
db = require('nano')(targetUrl);
var config = require('./config.json');
```
In the case above, both our username and password have been exposed, which is probably less than ideal. Using clarity though, we can obfuscate the connection string in the code and recreate the actual connection string by combining the information with ENVIRONMENT VARIABLES stored on the machine:
Using this technique for configuration, however, means that we don't have configurations for each of our environments (dev, test, staging, production) and we have also exposed some sensitive data in our configuration file. While we can use a package such as [node-config](https://github.com/lorenwest/node-config) to assist with managing configurations for different environments, we still have potentially sensitive information stored in our configuration files.
This can be avoided by using clarity to put value placeholders in our configuration instead:
```
{
"dbserver": "**DB_SERVER**",
"dbport": "**DB_PORT**",
"dbuser": "**DB_USER**",
"dbpass": "**DB_PASS**"
}
```
Now if we were to load the configuration via clarity we could use machine environment variables to replace the values:
```js
var clarity = require('clarity').use(process.env),
targetUrl = http://test:*****@damonoehlman.iriscouch.com/clarity-tests',
db = require('nano')(clarity.decode(targetUrl));
config = clarity.decode(require('./config.json'));
```
In the case above, clarity will be looking for a key of `test_pass` within the configured stores.
All values that have appropriate environment variables (e.g. `process.env.DB_SERVER`) would be replaced with the relevant value instead. The only downside of this technique is that in development you need a whole swag of environment variables configured which can be quite annoying.
To make life easier in development, you can use the default value functionality of clarity. This is implemented by separating the environment key with the default value using a pipe (|) character:
```
{
"dbserver": "**DB_SERVER|localhost**",
"dbport": "**DB_PORT|7632**",
"dbuser": "**DB_USER|admin**",
"dbpass": "**DB_PASS|teapot**"
}
```

@@ -5,3 +5,4 @@ var assert = require('assert'),

'test': 'test',
'this-is-a-test-key': 'test'
'this-is-a-test-key': 'test',
'this_is_another_test_key': 'test'
};

@@ -20,5 +21,15 @@

it('should be able to replace a simple key (with dashes)', function() {
assert.deepEqual(clarity.decode({ name: '**this-is-a-test-key**' }), { name: 'test' });
assert.deepEqual(
clarity.decode({ name: '**this-is-a-test-key**' }),
{ name: 'test' }
);
});
it('should be able to replace a simple key (with underscores)', function() {
assert.deepEqual(
clarity.decode({ name: '**this_is_another_test_key**'}),
{ name: 'test' }
);
});
it('should be able to replace occurrences of keys within other strings', function() {

@@ -25,0 +36,0 @@ assert.deepEqual(clarity.decode({ name: 'T**test**' }), { name: 'Ttest' });

@@ -20,2 +20,8 @@ var assert = require('assert'),

it('should be able to replace an environment var with underscores', function() {
var data = clarity.decode({ sshAgentPID: '**SSH_AGENT_PID**' });
assert.notEqual(data.sshAgentPID, '**SSH_AGENT_PID**');
});
it('should be able to replace an environment var deep within an object', function() {

@@ -22,0 +28,0 @@ var data = clarity.decode({

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc