pixl-config 1.0.13 → 1.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -28
- package/config.js +6 -12
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
1
|
# Overview
|
|
2
2
|
|
|
3
|
-
This module provides a simple interface to your application's JSON configuration file on disk. It will load and parse the file, and can automatically reload it when it changes (and emit an event when this happens). Command-line arguments are automatically merged in with your config (uses [pixl-args](https://www.npmjs.com/package/pixl-args)), and treated as overrides.
|
|
3
|
+
This module provides a simple interface to your application's JSON configuration file on disk. It will load and parse the file, and can automatically reload it when it changes (and emit an event when this happens). Command-line arguments are automatically merged in with your config (uses [pixl-args](https://www.npmjs.com/package/pixl-args)), and treated as overrides.
|
|
4
4
|
|
|
5
5
|
# Usage
|
|
6
6
|
|
|
7
7
|
Use [npm](https://www.npmjs.com/) to install the module:
|
|
8
8
|
|
|
9
|
-
```
|
|
9
|
+
```sh
|
|
10
10
|
npm install pixl-config
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
Then use `require()` to load it in your code:
|
|
14
14
|
|
|
15
|
-
```
|
|
16
|
-
|
|
15
|
+
```js
|
|
16
|
+
const Config = require('pixl-config');
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
To use the module, instantiate an object:
|
|
20
20
|
|
|
21
|
-
```
|
|
22
|
-
|
|
21
|
+
```js
|
|
22
|
+
let config = new Config('conf/config.json');
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
If there is an error loading or parsing the file, an exception will be thrown, so you might want to wrap it in a try/catch.
|
|
26
26
|
|
|
27
27
|
Upon success, you can access your configuration keys/values using the `get()` method:
|
|
28
28
|
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
```js
|
|
30
|
+
let verbose = config.get('verbose');
|
|
31
|
+
let debug = config.get('debug');
|
|
32
32
|
```
|
|
33
33
|
|
|
34
34
|
If you just want a hash of the entire config file, call `get()` without passing a key:
|
|
35
35
|
|
|
36
|
-
```
|
|
37
|
-
|
|
36
|
+
```js
|
|
37
|
+
let opts = config.get();
|
|
38
38
|
if (opts.verbose) console.log("Verbose option is set.");
|
|
39
39
|
if (opts.debug) console.log("Debug option is set.");
|
|
40
40
|
```
|
|
41
41
|
|
|
42
42
|
You can also set values by calling `set()` and passing both a key and a value. Example:
|
|
43
43
|
|
|
44
|
-
```
|
|
44
|
+
```js
|
|
45
45
|
config.set('verbose', true);
|
|
46
46
|
```
|
|
47
47
|
|
|
@@ -57,14 +57,14 @@ Please see the [pixl-args](https://www.npmjs.com/package/pixl-args) module for m
|
|
|
57
57
|
|
|
58
58
|
If you want the library to monitor your configuration file for live changes and automatically reload it, pass `true` as a second argument to the constructor:
|
|
59
59
|
|
|
60
|
-
```
|
|
61
|
-
|
|
60
|
+
```js
|
|
61
|
+
let config = new Config('conf/config.json', true);
|
|
62
62
|
```
|
|
63
63
|
|
|
64
64
|
This uses polling, and by default it checks the file for changes every 10 seconds. The library then reloads the configuration and fires off an event you can listen for:
|
|
65
65
|
|
|
66
|
-
```
|
|
67
|
-
|
|
66
|
+
```js
|
|
67
|
+
let config = new Config('conf/config.json', true);
|
|
68
68
|
|
|
69
69
|
config.on('reload', function() {
|
|
70
70
|
console.log("My app's config was reloaded!");
|
|
@@ -77,24 +77,13 @@ If there is an error reloading the configuration, an `error` event is emitted (n
|
|
|
77
77
|
To set the polling frequency, pass in the desired number of milliseconds instead of `true` as the second argument. For example, to check every five seconds pass in `5000`:
|
|
78
78
|
|
|
79
79
|
```js
|
|
80
|
-
|
|
80
|
+
let config = new Config('conf/config.json', 5000);
|
|
81
81
|
```
|
|
82
82
|
|
|
83
83
|
To stop monitoring (for example during your app's shutdown sequence) call the `stop()` method.
|
|
84
84
|
|
|
85
85
|
If you would prefer to control exactly when to check for changes, do not enable live monitoring, and instead simply call the `check()` method at any frequency you want.
|
|
86
86
|
|
|
87
|
-
## Server Environment
|
|
88
|
-
|
|
89
|
-
The library also provides a utility method for determining the server's hostname and IP address (IPv4). These things may require asynchronous operations, so you must specify a callback function. Upon completion, the hostname will be accessible via `config.hostname` and the IP address in `config.ip` (IPv4 dot-delimited string). Example:
|
|
90
|
-
|
|
91
|
-
```javascript
|
|
92
|
-
config.getEnv( function() {
|
|
93
|
-
console.log("The server hostname is: " + config.hostname);
|
|
94
|
-
console.log("The server IP is: " + config.ip);
|
|
95
|
-
} );
|
|
96
|
-
```
|
|
97
|
-
|
|
98
87
|
# License
|
|
99
88
|
|
|
100
89
|
**The MIT License**
|
package/config.js
CHANGED
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
|
|
6
6
|
var fs = require("fs");
|
|
7
7
|
var cp = require("child_process");
|
|
8
|
-
var dns = require("dns");
|
|
9
8
|
var os = require('os');
|
|
10
9
|
|
|
11
10
|
var Class = require("pixl-class");
|
|
@@ -245,7 +244,6 @@ var Config = module.exports = Class.create({
|
|
|
245
244
|
|
|
246
245
|
getIPAddress: function(callback) {
|
|
247
246
|
// determine server ip address
|
|
248
|
-
var self = this;
|
|
249
247
|
|
|
250
248
|
// allow the config to override this
|
|
251
249
|
this.ip = this.get('ip');
|
|
@@ -256,7 +254,7 @@ var Config = module.exports = Class.create({
|
|
|
256
254
|
}
|
|
257
255
|
|
|
258
256
|
// try OS networkInterfaces()
|
|
259
|
-
// find the first external IPv4 address that doesn't match 169.254.*
|
|
257
|
+
// find the first external IPv4 address that doesn't match 127.* or 169.254.*
|
|
260
258
|
var ifaces = os.networkInterfaces();
|
|
261
259
|
var addrs = [];
|
|
262
260
|
for (var key in ifaces) {
|
|
@@ -268,8 +266,8 @@ var Config = module.exports = Class.create({
|
|
|
268
266
|
var iaddrs = Tools.findObjects( addrs, { family: 'IPv4', internal: false } );
|
|
269
267
|
for (var idx = 0, len = iaddrs.length; idx < len; idx++) {
|
|
270
268
|
var addr = iaddrs[idx];
|
|
271
|
-
if (addr && addr.address && addr.address.match(/^\d+\.\d+\.\d+\.\d+$/) && !addr.address.match(/^169\.254\./)) {
|
|
272
|
-
// found an interface that is not 169.254.* so go with that one
|
|
269
|
+
if (addr && addr.address && addr.address.match(/^\d+\.\d+\.\d+\.\d+$/) && !addr.address.match(/^127\./) && !addr.address.match(/^169\.254\./)) {
|
|
270
|
+
// found an interface that is not 127.* or 169.254.* so go with that one
|
|
273
271
|
this.ip = addr.address;
|
|
274
272
|
callback();
|
|
275
273
|
return;
|
|
@@ -277,19 +275,15 @@ var Config = module.exports = Class.create({
|
|
|
277
275
|
}
|
|
278
276
|
|
|
279
277
|
var addr = iaddrs[0];
|
|
280
|
-
if (addr && addr.address && addr.address.match(/^\d+\.\d+\.\d+\.\d+$/)) {
|
|
278
|
+
if (addr && addr.address && addr.address.match(/^\d+\.\d+\.\d+\.\d+$/) && !addr.address.match(/^127\./)) {
|
|
281
279
|
// this will allow 169.254. to be chosen only after all other non-internal IPv4s are considered
|
|
282
280
|
this.ip = addr.address;
|
|
283
281
|
callback();
|
|
284
282
|
return;
|
|
285
283
|
}
|
|
286
284
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
// if (err) callback(err);
|
|
290
|
-
self.ip = addresses ? addresses[0] : '127.0.0.1';
|
|
291
|
-
callback();
|
|
292
|
-
} );
|
|
285
|
+
this.ip = '127.0.0.1';
|
|
286
|
+
callback();
|
|
293
287
|
},
|
|
294
288
|
|
|
295
289
|
setPath: function(path, value) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pixl-config",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.15",
|
|
4
4
|
"description": "A simple JSON configuration loader.",
|
|
5
5
|
"author": "Joseph Huckaby <jhuckaby@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/jhuckaby/pixl-config",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"pixl-class": "^1.0.3",
|
|
22
22
|
"pixl-args": "^1.0.0",
|
|
23
|
-
"pixl-tools": "^
|
|
23
|
+
"pixl-tools": "^2.0.2"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {}
|
|
26
26
|
}
|