Skip to content

Commit 5e9415b

Browse files
author
craig
committed
@alt-javascript/config initial commit.
1 parent e9a0c9e commit 5e9415b

25 files changed

+10070
-58
lines changed

.eslintrc.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"commonjs": true,
5+
"es2021": true,
6+
"mocha" : true
7+
},
8+
"plugins": [
9+
"cucumber"
10+
],
11+
"extends": [
12+
"airbnb-base"
13+
],
14+
"parserOptions": {
15+
"ecmaVersion": 12
16+
17+
},
18+
"parser": "babel-eslint",
19+
"rules": {
20+
"no-plusplus": ["error", { "allowForLoopAfterthoughts": true }],
21+
"cucumber/async-then": ["error", { "All": false, "words": [ ] }],
22+
"cucumber/expression-type": 2,
23+
"cucumber/no-restricted-tags": [2, "wip", "broken", "foo"],
24+
"cucumber/no-arrow-functions": 2,
25+
"import/no-extraneous-dependencies": ["error", { "devDependencies": ["test/**/*.js", "features/**/*.js"] }]
26+
}
27+
}

.gitignore

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
logs
33
*.log
44
npm-debug.log*
5-
yarn-debug.log*
6-
yarn-error.log*
7-
lerna-debug.log*
85

96
# Diagnostic reports (https://nodejs.org/api/report.html)
107
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
@@ -15,22 +12,13 @@ pids
1512
*.seed
1613
*.pid.lock
1714

18-
# Directory for instrumented libs generated by jscoverage/JSCover
19-
lib-cov
20-
2115
# Coverage directory used by tools like istanbul
2216
coverage
2317
*.lcov
2418

2519
# nyc test coverage
2620
.nyc_output
2721

28-
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29-
.grunt
30-
31-
# Bower dependency directory (https://bower.io/)
32-
bower_components
33-
3422
# node-waf configuration
3523
.lock-wscript
3624

@@ -39,13 +27,6 @@ build/Release
3927

4028
# Dependency directories
4129
node_modules/
42-
jspm_packages/
43-
44-
# TypeScript v1 declaration files
45-
typings/
46-
47-
# TypeScript cache
48-
*.tsbuildinfo
4930

5031
# Optional npm cache directory
5132
.npm
@@ -64,41 +45,4 @@ typings/
6445

6546
# Output of 'npm pack'
6647
*.tgz
67-
68-
# Yarn Integrity file
69-
.yarn-integrity
70-
71-
# dotenv environment variables file
72-
.env
73-
.env.test
74-
75-
# parcel-bundler cache (https://parceljs.org/)
76-
.cache
77-
78-
# Next.js build output
79-
.next
80-
81-
# Nuxt.js build / generate output
82-
.nuxt
83-
dist
84-
85-
# Gatsby files
86-
.cache/
87-
# Comment in the public line in if your project uses Gatsby and *not* Next.js
88-
# https://nextjs.org/blog/next-9-1#public-directory-support
89-
# public
90-
91-
# vuepress build output
92-
.vuepress/dist
93-
94-
# Serverless directories
95-
.serverless/
96-
97-
# FuseBox cache
98-
.fusebox/
99-
100-
# DynamoDB Local files
101-
.dynamodb/
102-
103-
# TernJS port file
104-
.tern-port
48+
/.idea/

.npmignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/.nyc_output
2+
/config
3+
/coverage
4+
/features
5+
/test
6+
/node_modules
7+
.*

ConfigFactory.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const npmconfig = require('config');
2+
const ValueResolvingConfig = require('./ValueResolvingConfig');
3+
const DelegatingResolver = require('./DelegatingResolver');
4+
const PlaceHolderResolver = require('./PlaceHolderResolver');
5+
const PlaceHolderSelector = require('./PlaceHolderSelector');
6+
const JasyptDecryptor = require('./JasyptDecryptor');
7+
const PrefixSelector = require('./PrefixSelector');
8+
9+
module.exports = class ConfigFactory {
10+
static getConfig(config, resolver) {
11+
const placeHolderResolver = new PlaceHolderResolver(new PlaceHolderSelector());
12+
const jasyptDecryptor = new JasyptDecryptor(new PrefixSelector());
13+
const delegatingResolver = new DelegatingResolver([placeHolderResolver, jasyptDecryptor]);
14+
const valueResolvingConfig = new ValueResolvingConfig(config || npmconfig,
15+
resolver || delegatingResolver);
16+
17+
placeHolderResolver.reference = valueResolvingConfig;
18+
return valueResolvingConfig;
19+
}
20+
};

DelegatingConfig.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = class DelegatingConfig {
2+
constructor(config, path) {
3+
this.config = config;
4+
this.path = path;
5+
}
6+
7+
has(path) {
8+
return this.config.has(path);
9+
}
10+
};

DelegatingResolver.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const Resolver = require('./Resolver');
2+
3+
module.exports = class DelegatingResolver extends Resolver {
4+
constructor(resolvers) {
5+
super();
6+
this.resolvers = resolvers;
7+
}
8+
9+
resolve(config) {
10+
let resolvedConfig = config;
11+
for (let i = 0; i < this.resolvers.length; i++) {
12+
resolvedConfig = this.resolvers[i].resolve(resolvedConfig);
13+
}
14+
return resolvedConfig;
15+
}
16+
};

EphemeralConfig.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const _ = require('lodash');
2+
3+
module.exports = class EphemeralConfig {
4+
constructor(object, path) {
5+
const self = this;
6+
this.object = object;
7+
this.path = path;
8+
if (this.object) {
9+
_.assignIn(self, this.object);
10+
}
11+
}
12+
13+
get(path, defaultValue) {
14+
const pathSteps = path?.split('.') || [];
15+
let root = this.object;
16+
for (let i = 0; i < pathSteps.length && root !== null && root !== undefined; i++) {
17+
root = root?.[pathSteps[i]];
18+
}
19+
if (root) {
20+
return root;
21+
}
22+
if (defaultValue) {
23+
return defaultValue;
24+
}
25+
throw new Error(`Config path ${path} returned no value.`);
26+
}
27+
28+
has(path) {
29+
const pathSteps = path?.split('.') || [];
30+
let root = this.object;
31+
for (let i = 0; i < pathSteps.length && root !== null && root !== undefined; i++) {
32+
root = root?.[pathSteps[i]];
33+
}
34+
return root !== null && root !== undefined;
35+
}
36+
};

History.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1.0.0 / 2021-07-12
2+
==================
3+
4+
* Initial npm publish - @craigparra

JasyptDecryptor.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const Jasypt = require('jasypt');
2+
3+
const Resolver = require('./Resolver');
4+
const SelectiveResolver = require('./SelectiveResolver');
5+
const PrefixSelector = require('./PrefixSelector');
6+
7+
module.exports = class JasyptDecryptor extends SelectiveResolver {
8+
constructor(selector, password) {
9+
super(selector || (new PrefixSelector('enc.')));
10+
this.jasypt = new Jasypt();
11+
this.jasypt.setPassword(password || process.env.NODE_CONFIG_PASSPHRASE || 'changeit');
12+
}
13+
14+
resolve(config) {
15+
const self = this;
16+
const resolvedConfig = Resolver.prototype.mapValuesDeep(config, (v) => {
17+
if (self.selector.matches(v)) {
18+
try {
19+
const selectedValue = self.selector.resolveValue(v);
20+
const decryptedValue = self.jasypt.decrypt(selectedValue);
21+
return decryptedValue;
22+
} catch (e) {
23+
return v;
24+
}
25+
}
26+
return v;
27+
});
28+
return resolvedConfig;
29+
}
30+
};

PlaceHolderResolver.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const Resolver = require('./Resolver');
2+
const SelectiveResolver = require('./SelectiveResolver');
3+
const PlaceHolderSelector = require('./PlaceHolderSelector');
4+
5+
module.exports = class PlaceHolderResolver extends SelectiveResolver {
6+
constructor(selector, reference) {
7+
super(selector || (new PlaceHolderSelector()));
8+
this.reference = reference;
9+
}
10+
11+
resolve(config) {
12+
const self = this;
13+
const resolvedConfig = Resolver.prototype.mapValuesDeep(config, (v) => {
14+
if (self.selector.matches(v)) {
15+
try {
16+
let resolvedValue = '';
17+
let remainder = v;
18+
let placeholder;
19+
20+
while (resolvedValue === '' || (remainder.includes('${')
21+
&& remainder.includes('}')
22+
&& remainder.indexOf('${') < remainder.indexOf('}'))
23+
) {
24+
resolvedValue = `${resolvedValue}${remainder.substring(0, remainder.indexOf('${'))}`;
25+
placeholder = remainder.substring(remainder.indexOf('${') + 2, remainder.indexOf('}'));
26+
resolvedValue = `${resolvedValue}${self.reference.get(placeholder)}`;
27+
remainder = remainder.substring(remainder.indexOf('}') + 1);
28+
}
29+
resolvedValue = `${resolvedValue}${remainder}`;
30+
return resolvedValue;
31+
} catch (e) {
32+
return v;
33+
}
34+
}
35+
return v;
36+
});
37+
return resolvedConfig;
38+
}
39+
};

0 commit comments

Comments
 (0)