not-node 6.2.17 → 6.2.18
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/package.json +1 -1
- package/src/common.js +59 -0
package/package.json
CHANGED
package/src/common.js
CHANGED
|
@@ -292,3 +292,62 @@ module.exports.getIP = (req) => {
|
|
|
292
292
|
return undefined;
|
|
293
293
|
}
|
|
294
294
|
};
|
|
295
|
+
|
|
296
|
+
const createIsStringPrefixedTester = (prefix) => {
|
|
297
|
+
if (!prefix || prefix.length === 0) throw Error("No prefix provided");
|
|
298
|
+
return (str) => {
|
|
299
|
+
return str && str.length > prefix.length && str.indexOf(prefix) === 0;
|
|
300
|
+
};
|
|
301
|
+
};
|
|
302
|
+
module.exports.createIsStringPrefixedTester = createIsStringPrefixedTester;
|
|
303
|
+
|
|
304
|
+
const stringIsPrefixed = (val, options) => {
|
|
305
|
+
if (typeof val === "string" && options) {
|
|
306
|
+
if (options.tester && typeof options.tester === "function") {
|
|
307
|
+
const res = options.tester(val);
|
|
308
|
+
if (Object.hasOwn(process.env, val)) {
|
|
309
|
+
return res;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
if (
|
|
313
|
+
options.prefix &&
|
|
314
|
+
typeof options.prefix === "string" &&
|
|
315
|
+
options.prefix.length > 0
|
|
316
|
+
) {
|
|
317
|
+
const res = createIsStringPrefixedTester(options.prefix)(val);
|
|
318
|
+
if (Object.hasOwn(process.env, val)) {
|
|
319
|
+
return res;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return 0;
|
|
324
|
+
};
|
|
325
|
+
module.exports.stringIsPrefixed = stringIsPrefixed;
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* It will get value of name from object property and then test,
|
|
329
|
+
* if value corresponds to a property process.ENV object. If it does -
|
|
330
|
+
* value from process.ENV.* will be returned, if not - just value of source[name]
|
|
331
|
+
* @param {object} source source object
|
|
332
|
+
* @param {string} name option name
|
|
333
|
+
* @param {object} [options] options
|
|
334
|
+
* @param {function} [options.tester] (str)=>integer, checks value of source[name], if returns not 0 than value is name of property in process.ENV to be returned, if number more than 0 returned and drop is set to true, returned number of chars will be droped
|
|
335
|
+
* @param {string} [options.prefix = 'ENV$'] prefix that indicates that source[name] value corresponds to process.ENV property
|
|
336
|
+
* @param {boolean} [options.drop = true] should we drop prefix from value before reach for real value in process.ENV, ENV$VALUE - process.ENV.VALUE
|
|
337
|
+
* @returns {*} any
|
|
338
|
+
*/
|
|
339
|
+
const getValueFromEnv = (
|
|
340
|
+
source,
|
|
341
|
+
name,
|
|
342
|
+
options = { prefix: "ENV$", drop: true }
|
|
343
|
+
) => {
|
|
344
|
+
if (source && Object.hasOwn(source, name)) {
|
|
345
|
+
const val = source[name];
|
|
346
|
+
const len = stringIsPrefixed(val, options);
|
|
347
|
+
if (len) {
|
|
348
|
+
return process.env[options.drop ? val.slice(0, len) : val];
|
|
349
|
+
}
|
|
350
|
+
return val;
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
module.exports.getValueFromEnv = getValueFromEnv;
|