This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from uniquelyparticular/feature/mustache-token-…
…replacement Feature/mustache token replacement
- Loading branch information
Showing
5 changed files
with
123 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const { filterByPrefix, mustachReplace } = require('./src/utils') | ||
|
||
process.on('unhandledRejection', (reason, p) => { | ||
console.error( | ||
'Promise unhandledRejection: ', | ||
p, | ||
', reason:', | ||
JSON.stringify(reason) | ||
) | ||
}) | ||
|
||
process.env.PROXY_REPLACE_FOO = 'very-secret-23234-324234-234' | ||
|
||
class Direct { | ||
static async run() { | ||
const secureHeader = 'asdas {{settings.foo}} sadsad' | ||
console.log('secureHeader', secureHeader) | ||
const replacements = filterByPrefix(process.env, 'PROXY_REPLACE_') | ||
console.log('replacements', replacements) | ||
const header = mustachReplace(secureHeader, replacements, 'setting') | ||
console.log('header', header) | ||
} | ||
} | ||
|
||
module.exports = Direct | ||
|
||
if (require.main === module) { | ||
Direct.run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module.exports = { | ||
filterByPrefix(input, ...matchPrefixes) { | ||
return Object.keys(input) | ||
.filter(key => { | ||
// return only objects within input who's keys start with any envPrefix | ||
let matched = false | ||
matchPrefixes.forEach(prefix => { | ||
matched = matched || key.startsWith(prefix) | ||
}) | ||
return matched | ||
}) | ||
.reduce((obj, rawKey) => { | ||
// remove any envPrefix that object startswith | ||
let key = rawKey | ||
matchPrefixes.forEach(prefix => { | ||
key = key.startsWith(prefix) ? key.replace(prefix, '') : key | ||
}) | ||
obj[key] = input[rawKey] | ||
return obj | ||
}, {}) | ||
}, | ||
|
||
mustachReplace(input, replacements, ...mustachPrefixes) { | ||
// will search for input containing {{mustachePrefix.XYZ}} | ||
return input.replace(/{{((.*?)\.(.*?))}}/g, (match, ...groups) => { | ||
if (mustachPrefixes.includes(groups[1])) { | ||
return groups[2] !== 'undefined' | ||
? replacements[groups[2].toUpperCase()] || match | ||
: match | ||
} else { | ||
return match | ||
} | ||
}) | ||
} | ||
} |