daily pastebin goal
65%
SHARE
TWEET

Untitled

a guest Mar 23rd, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // resources/assets/js/helpers/node-utils.js
  2. // include dependencies
  3. // - - - - - - - - - -
  4. const path = require('path');
  5. const fs = require('fs');
  6. const tls = require('tls');
  7.  
  8. // Globalize
  9. let locale = 'es-CO';
  10. const Globalize = require('globalize/dist/globalize');
  11. require('globalize/dist/globalize/message');
  12. require('globalize/dist/globalize/plural');
  13. // load required CLDR JSON
  14. Globalize.load(
  15.     require('cldr-data/supplemental/likelySubtags.json'),
  16.     require('cldr-data/supplemental/plurals.json'),
  17.     require('cldr-data/supplemental/ordinals.json')
  18. );
  19. // load globalize messages from json file
  20. Globalize.loadMessages( require('../../../lang/es/messages.json') );
  21. Globalize.locale( locale );
  22.  
  23. // Initialize attributes
  24. // - - - - - - - - - - -
  25. const assetsBaseDir = `${__dirname}/../../../assets/`;
  26.  
  27. /**
  28.  * get a built path from the assets location
  29.  * @param {String} topath
  30.  * @param {String} currentPath, eg. __dirname from module to resolve
  31.  * @return {String} path to assets module
  32.  */
  33. const getAssetsPath = exports.getAssetsPath = (topath, currentPath) => {
  34.     currentPath || (currentPath = process.env.NODE_PATH || __dirname);
  35.     return path.relative( currentPath, path.resolve(assetsBaseDir, topath) )
  36.             .replace((new RegExp(`^[^\\.\\${path.sep}]`)), `.${path.sep}$&`);
  37. }
  38.  
  39. /**
  40.  * format message for institution
  41.  * @param {String} message
  42.  * @param {Object} data
  43.  * @param {String} _locale
  44.  * @return {String} formated message
  45.  */
  46. const formatMessage = exports.formatMessage = (message, data, _locale) => {
  47.  
  48.     const _Globalize = _locale ? Globalize(_locale) : Globalize;
  49.     const slugInstitution = process.env.SLUG_INSTITUTION;
  50.  
  51.     try {
  52.         return _Globalize.formatMessage(`${slugInstitution ? slugInstitution +'/' : ''}${message}`, data);
  53.  
  54.     }catch(e) {
  55.         return _Globalize.formatMessage(message, data);
  56.     }
  57. }
  58.  
  59. /**
  60.  * message formatter for institution
  61.  * @param {String} message
  62.  * @param {String} _locale
  63.  * @return {Function} formatter, parameters: {Object}
  64.  */
  65. const messageFormatter = exports.messageFormatter = (message, _locale)  => {
  66.  
  67.     const _Globalize = _locale ? Globalize(_locale) : Globalize;
  68.     const slugInstitution = process.env.SLUG_INSTITUTION;
  69.  
  70.     try {
  71.         return _Globalize.messageFormatter(`${slugInstitution ? slugInstitution +'/' : ''}${message}`);
  72.  
  73.     }catch(e) {
  74.         return _Globalize.messageFormatter(message);
  75.     }
  76. }
  77.  
  78. /**
  79.  * (multiple certs in one file) into an array exploded by the -END CERTIFICATE- line
  80.  * @param {String} Bundle path
  81.  * @return {Array} ca
  82.  */
  83. const getCABundle = exports.getCABundle = (Bundle) => {
  84.     let ca = [];
  85.     let cert = [];
  86.     const chain = fs.readFileSync(Bundle, 'utf8').split('\n');
  87.  
  88.     for(let line in chain) {
  89.         if(line.length > 0) {
  90.             cert.push(chain[line]);
  91.             if(chain[line].match(/-END CERTIFICATE-/)){
  92.                 ca.push(cert.join("\n"));
  93.                 cert = [];
  94.             }
  95.         }
  96.     }
  97.     return ca;
  98. }
  99.  
  100. const getCredentialsContext = exports.getCredentialsContext = (cer) => {
  101.     return tls.createSecureContext({
  102.         key:  fs.readFileSync(cer.key),
  103.         cert: fs.readFileSync(cer.cert),
  104.         ca: getCABundle(cer.ca)
  105.     });
  106. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top