Skip to content

Commit

Permalink
feat: log global.GLOBAL_AGENT configuration changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Jun 21, 2019
1 parent 02086d9 commit 8d5eeca
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
42 changes: 42 additions & 0 deletions src/factories/createGlobalAgentGlobal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// @flow

import Logger from '../Logger';

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

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

export default () => {
// eslint-disable-next-line fp/no-proxy
return new Proxy({
bootstrapped: false,
HTTP_PROXY: '',
HTTPS_PROXY: '',
NO_PROXY: ''
}, {
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;
}
});
};
3 changes: 3 additions & 0 deletions src/factories/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @flow

export {default as createGlobalAgentGlobal} from './createGlobalAgentGlobal';
5 changes: 4 additions & 1 deletion src/routines/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import {
import {
UnexpectedStateError
} from '../errors';
import {
createGlobalAgentGlobal
} from '../factories';
import {
bindHttpMethod,
isUrlMatchingNoProxy,
Expand Down Expand Up @@ -43,7 +46,7 @@ const createConfiguration = (configurationInput: ConfigurationInputType): Config
export default (configurationInput: ConfigurationInputType) => {
const configuration = createConfiguration(configurationInput);

global.GLOBAL_AGENT = global.GLOBAL_AGENT || {};
global.GLOBAL_AGENT = global.GLOBAL_AGENT || createGlobalAgentGlobal();

if (global.GLOBAL_AGENT.bootstrapped) {
log.warn('found global.globalAgent; second attempt to bootstrap global-agent was ignored');
Expand Down
51 changes: 51 additions & 0 deletions test/global-agent/factories/createGlobalAgentGlobal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// @flow

import test from 'ava';
import createGlobalAgentGlobal from '../../../src/factories/createGlobalAgentGlobal';

test('defaults bootstrapped to false', (t) => {
const globalAgentGlobal = createGlobalAgentGlobal();

t.is(globalAgentGlobal.bootstrapped, false);
});

test('sets bootstrapped', (t) => {
const globalAgentGlobal = createGlobalAgentGlobal();

globalAgentGlobal.bootstrapped = true;

t.is(globalAgentGlobal.bootstrapped, true);
});

test('sets HTTP_PROXY', (t) => {
const globalAgentGlobal = createGlobalAgentGlobal();

globalAgentGlobal.HTTP_PROXY = 'http://127.0.0.1';

t.is(globalAgentGlobal.HTTP_PROXY, 'http://127.0.0.1');
});

test('sets HTTPS_PROXY', (t) => {
const globalAgentGlobal = createGlobalAgentGlobal();

globalAgentGlobal.HTTPS_PROXY = 'http://127.0.0.1';

t.is(globalAgentGlobal.HTTPS_PROXY, 'http://127.0.0.1');
});

test('sets NO_PROXY', (t) => {
const globalAgentGlobal = createGlobalAgentGlobal();

globalAgentGlobal.NO_PROXY = '*';

t.is(globalAgentGlobal.NO_PROXY, '*');
});

test('throws an error if unknown property is set', (t) => {
const globalAgentGlobal = createGlobalAgentGlobal();

t.throws(() => {
// $FlowFixMe
globalAgentGlobal.FOO = 'BAR';
}, 'Cannot set an unmapped property "FOO".');
});

0 comments on commit 8d5eeca

Please sign in to comment.