import { WebServerSslConfigOptions } from '../types'; import { ServerOptions } from 'https'; import * as fs from 'fs'; import { cat } from 'shelljs'; export const sslConfigToHttpsServerOptions = (config: WebServerSslConfigOptions): ServerOptions => { const { certFilePath, keyFilePath, dhparamFilePath, passphrase } = config; if (!fs.existsSync(certFilePath)) { console.log(certFilePath); throw new Error(`SSL Certificate Path not found: ${certFilePath}`); } if (!fs.existsSync(keyFilePath)) { throw new Error(`SSL Key File Path not found: ${keyFilePath}`); } if (dhparamFilePath && !fs.existsSync(dhparamFilePath)) { throw new Error(`dhparam File Path not found: ${dhparamFilePath}`); } let options: ServerOptions; try { options = { cert: fs.readFileSync(certFilePath), key: fs.readFileSync(keyFilePath), dhparam: dhparamFilePath ? fs.readFileSync(dhparamFilePath) : undefined, passphrase, }; return options; } catch (e) { throw new Error('Cannot load SSL Certificate'); } }; // Straight from express.. /** * Stringify JSON, like JSON.stringify, but v8 optimized, with the * ability to escape characters that can trigger HTML sniffing. * * @param {*} value * @param {function} replaces * @param {number} spaces * @param {boolean} escape * @returns {string} * @private */ const isCyclic = (obj) => { var seenObjects = []; function detect (obj, parent?) { if (obj && typeof obj === 'object') { if (seenObjects.indexOf(obj) !== -1) { return true; } seenObjects.push(obj); for (var key in obj) { try { const thisParent = (parent || '') + '/' + key; //if (obj.hasOwnProperty(key) && detect(obj[key])) { if (Object.prototype.hasOwnProperty.call(obj, key) && detect(obj[key], thisParent)) { console.log(obj, 'cycle at ' + thisParent); return true; } } catch(e) { debugger } } } return false; } return detect(obj); } export const JSONStringify = ( value: any, replacer?: (key: string, value: any) => any, spaces?: string | number, escape?: boolean ) => { // v8 checks arguments.length for optimizing simple call // https://bugs.chromium.org/p/v8/issues/detail?id=4730 try { var json = replacer || spaces ? JSON.stringify(value, replacer, spaces) : JSON.stringify(value); if (escape) { json = json.replace(/[<>&]/g, function(c) { switch (c.charCodeAt(0)) { case 0x3c: return '\\u003c'; case 0x3e: return '\\u003e'; case 0x26: return '\\u0026'; default: return c; } }); } return json; } catch(e) { debugger try { isCyclic(value); } catch(e2) { debugger } debugger } };