createProxyController.js.flow 882 Bytes
Newer Older
jakkree kongpha's avatar
jakkree kongpha committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
// @flow

import Logger from '../Logger';

type ProxyControllerType = {|
  HTTP_PROXY: string | null,
  HTTPS_PROXY: string | null,
  NO_PROXY: string | null,
|};

const log = Logger.child({
  namespace: 'createProxyController',
});

const KNOWN_PROPERTY_NAMES = [
  'HTTP_PROXY',
  'HTTPS_PROXY',
  'NO_PROXY',
];

export default (): ProxyControllerType => {
  // eslint-disable-next-line fp/no-proxy
  return new Proxy({
    HTTP_PROXY: null,
    HTTPS_PROXY: null,
    NO_PROXY: null,
  }, {
    set: (subject, name, value) => {
      if (!KNOWN_PROPERTY_NAMES.includes(name)) {
        throw new Error('Cannot set an unmapped property "' + name + '".');
      }

      subject[name] = value;

      log.info({
        change: {
          name,
          value,
        },
        newConfiguration: subject,
      }, 'configuration changed');

      return true;
    },
  });
};