diff --git a/src/proxy.js b/src/proxy.js new file mode 100644 index 0000000..3addef4 --- /dev/null +++ b/src/proxy.js @@ -0,0 +1,24 @@ +const {bootstrap} = require('global-agent'); +const core = require("@actions/core"); +const MAJOR_NODEJS_VERSION = parseInt(process.version.slice(1).split('.')[0], 10); + +/** + * Will enable global-agent proxy if the necessary + * [global-agent environment variables](https://github.com/gajus/global-agent?tab=readme-ov-file#environment-variables) + * has been set. + * Important note: `global-agent` only works with Node.js v10 and above. + * Node.js versions below v10 are not supported + * + * The bootstrap routine guards against multiple initializations of global-agent + */ +function enableProxy() { + if (MAJOR_NODEJS_VERSION >= 10) { + bootstrap(); + } else { + core.debug('proxy configuration does not work with Node.js below v10') + } +} + +module.exports = { + enableProxy +} \ No newline at end of file diff --git a/src/proxy.test.js b/src/proxy.test.js new file mode 100644 index 0000000..6472d69 --- /dev/null +++ b/src/proxy.test.js @@ -0,0 +1,72 @@ +const {enableProxy } = require('./proxy') +const got = require('got'); + +describe('global-agent proxy support works as expected', () => { + + it('should fail with a request specific RequestError if got tries to request any-host without proxy', async () => { + const expectedRequestErrorMessage = `getaddrinfo ENOTFOUND any-host`; + try { + await got.get('http://any-host') + throw new Error('Test fail if the got request succeeds against expectation'); + } catch(e) { + expect(e).toBeDefined(); + expect(e.message).toEqual(expectedRequestErrorMessage); + expect(e.code).toEqual('ENOTFOUND'); + } + }); + + it('should fail with a proxy specific RequestError if got tries to request through the defined proxy', async () => { + const PROXY_HOST = "my-test-proxy-server"; + process.env.GLOBAL_AGENT_HTTP_PROXY = `http://${PROXY_HOST}:8000`; + enableProxy(); + const expectedRequestErrorMessage = `getaddrinfo ENOTFOUND ${PROXY_HOST}`; + try { + await got.get('http://any-host') + throw new Error('Test fail if the got request succeeds against expectation'); + } catch(e) { + expect(e).toBeDefined(); + expect(e.message).toEqual(expectedRequestErrorMessage); + expect(e.code).toEqual('ENOTFOUND'); + } + }); + + it('should fail with a request specific RequestError if HTTP_PROXY URL is undefined', async () => { + // set proxy url to undefined at runtime + global.GLOBAL_AGENT.HTTP_PROXY = undefined; + const expectedRequestErrorMessage = `getaddrinfo ENOTFOUND any-host`; + try { + await got.get('http://any-host') + throw new Error('Test fail if the got request succeeds against expectation'); + } catch(e) { + expect(e).toBeDefined(); + expect(e.message).toEqual(expectedRequestErrorMessage); + expect(e.code).toEqual('ENOTFOUND'); + } + }); + + it('should fail with a request specific RequestError if HTTP_PROXY URL is empty', async () => { + // set proxy url to empty string at runtime + global.GLOBAL_AGENT.HTTP_PROXY = ''; + const expectedRequestErrorMessage = `getaddrinfo ENOTFOUND any-host`; + try { + await got.get('http://any-host') + throw new Error('Test fail if the got request succeeds against expectation'); + } catch(e) { + expect(e).toBeDefined(); + expect(e.message).toEqual(expectedRequestErrorMessage); + expect(e.code).toEqual('ENOTFOUND'); + } + }); + + it('should fail with UNEXPECTED_STATE_ERROR if HTTP_PROXY URL protocol does not start with "http:"', async () => { + // set proxy url to string with blanks at runtime + global.GLOBAL_AGENT.HTTP_PROXY = 'file://'; + try { + await got.get('http://any-host') + throw new Error('Test fail if the got request succeeds against expectation'); + } catch(e) { + expect(e).toBeDefined(); + expect(e.code).toEqual('UNEXPECTED_STATE_ERROR'); + } + }); +}) \ No newline at end of file