Skip to content

Commit

Permalink
improve proxy handler (#119)
Browse files Browse the repository at this point in the history
SEMVER_MAJOR
  • Loading branch information
asamuzaK committed Sep 16, 2023
1 parent 9852e18 commit fa69ab7
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 119 deletions.
28 changes: 16 additions & 12 deletions modules/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { watch } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import process from 'node:process';
import { HttpsProxyAgent } from 'https-proxy-agent';
import undici from 'undici';
import { compareSemVer, isValidSemVer } from 'semver-parser';
import {
ChildProcess, CmdArgs, Input, Output,
Expand Down Expand Up @@ -163,19 +163,9 @@ export const exportFileData = async (obj = {}) => {
* @returns {?string} - latest host version string
*/
export const fetchLatestHostVersion = async () => {
const url = `https://registry.npmjs.org/${HOST}`;
const proxy = process.env.HTTPS_PROXY || process.env.https_proxy ||
process.env.HTTP_PROXY || process.env.http_proxy;
let version;
try {
let res;
if (proxy) {
res = await fetch(url, {
agent: new HttpsProxyAgent(proxy)
});
} else {
res = await fetch(url);
}
const res = await fetch(`https://registry.npmjs.org/${HOST}`);
if (res.ok) {
const data = await res.json();
const { latest } = data['dist-tags'];
Expand Down Expand Up @@ -740,6 +730,19 @@ export const handleExit = code => {
}
};

/**
* set dispatcher
* @returns {void}
*/
export const setDispatcher = () => {
const proxy = process.env.HTTPS_PROXY || process.env.https_proxy ||
process.env.HTTP_PROXY || process.env.http_proxy;
if (proxy) {
const agent = new undici.ProxyAgent(proxy);
undici.setGlobalDispatcher(agent);
}
};

/**
* add process listeners
* @returns {void}
Expand All @@ -756,6 +759,7 @@ export const addProcessListeners = () => {
*/
export const startup = () => Promise.all([
addProcessListeners(),
setDispatcher(),
createDirectory(TMPDIR_FILES, PERM_DIR),
createDirectory(TMPDIR_FILES_PB, PERM_DIR)
]).then(exportAppStatus).catch(handleReject);
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"main": "./index.js",
"dependencies": {
"commander": "^11.0.0",
"https-proxy-agent": "^7.0.2",
"readline-sync": "^1.4.10",
"semver-parser": "^4.1.4",
"undici": "^5.24.0",
"web-ext-native-msg": "^6.1.10"
},
"devDependencies": {
Expand All @@ -32,8 +32,7 @@
"genversion": "^3.1.1",
"jszip-cli": "^1.4.24",
"mocha": "^10.2.0",
"sinon": "^16.0.0",
"undici": "^5.24.0"
"sinon": "^16.0.0"
},
"scripts": {
"bundle-js": "esbuild --platform=node --outdir=./bundle --bundle --minify ./index.js",
Expand Down
159 changes: 55 additions & 104 deletions test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import sinon from 'sinon';
import { assert } from 'chai';
import { afterEach, beforeEach, describe, it } from 'mocha';
import { compareSemVer, parseSemVer } from 'semver-parser';
import { getGlobalDispatcher, MockAgent, setGlobalDispatcher } from 'undici';
import undici, {
getGlobalDispatcher, MockAgent, setGlobalDispatcher
} from 'undici';
import {
Input, Output,
createDirectory, createFile, getFileTimestamp, isDir, isFile, removeDir
Expand All @@ -25,7 +27,7 @@ import {
handleChildProcessErr, handleChildProcessExit, handleChildProcessStderr,
handleChildProcessStdout, handleCreatedTmpFile, handleExit, handleMsg,
handleReject, hostMsg, initPrivateTmpDir, readStdin, removeTmpFileData,
startup, unwatchFile, viewLocalFile, watchTmpFile, writeStdout
setDispatcher, startup, unwatchFile, viewLocalFile, watchTmpFile, writeStdout
} from '../modules/main.js';
import {
EDITOR_CONFIG_FILE, EDITOR_CONFIG_GET, EDITOR_CONFIG_RES, EDITOR_CONFIG_TS,
Expand Down Expand Up @@ -386,33 +388,11 @@ describe('fetchLatestHostVersion', () => {
});

it('should get null', async () => {
process.env.HTTPS_PROXY = 'http://localhost:9000';
const hostName = process.env.npm_package_name;
mockAgent.get('https://registry.npmjs.org').intercept({
path: `/${hostName}`
}).reply(404);
const res = await fetchLatestHostVersion();
delete process.env.HTTPS_PROXY;
assert.isNull(res);
});

it('should get null', async () => {
const hostName = process.env.npm_package_name;
mockAgent.get('https://registry.npmjs.org').intercept({
path: `/${hostName}`
}).replyWithError(new Error('Error'));
const res = await fetchLatestHostVersion();
assert.isNull(res);
});

it('should get null', async () => {
process.env.HTTPS_PROXY = 'http://localhost:9000';
const hostName = process.env.npm_package_name;
mockAgent.get('https://registry.npmjs.org').intercept({
path: `/${hostName}`
}).replyWithError(new Error('Error'));
const res = await fetchLatestHostVersion();
delete process.env.HTTPS_PROXY;
assert.isNull(res);
});

Expand All @@ -433,86 +413,6 @@ describe('fetchLatestHostVersion', () => {
const res = await fetchLatestHostVersion();
assert.strictEqual(res, version);
});

it('should get result', async () => {
process.env.HTTPS_PROXY = 'http://localhost:9000';
const hostVersion = process.env.npm_package_version;
const hostName = process.env.npm_package_name;
const {
major, minor, patch
} = await parseSemVer(hostVersion);
const version = `${major}.${minor}.${patch + 1}`;
mockAgent.get('https://registry.npmjs.org').intercept({
path: `/${hostName}`
}).reply(200, {
'dist-tags': {
latest: version
}
});
const res = await fetchLatestHostVersion();
delete process.env.HTTPS_PROXY;
assert.strictEqual(res, version);
});

it('should get result', async () => {
process.env.https_proxy = 'http://localhost:9000';
const hostVersion = process.env.npm_package_version;
const hostName = process.env.npm_package_name;
const {
major, minor, patch
} = await parseSemVer(hostVersion);
const version = `${major}.${minor}.${patch + 1}`;
mockAgent.get('https://registry.npmjs.org').intercept({
path: `/${hostName}`
}).reply(200, {
'dist-tags': {
latest: version
}
});
const res = await fetchLatestHostVersion();
delete process.env.https_proxy;
assert.strictEqual(res, version);
});

it('should get result', async () => {
process.env.HTTP_PROXY = 'http://localhost:9000';
const hostVersion = process.env.npm_package_version;
const hostName = process.env.npm_package_name;
const {
major, minor, patch
} = await parseSemVer(hostVersion);
const version = `${major}.${minor}.${patch + 1}`;
mockAgent.get('https://registry.npmjs.org').intercept({
path: `/${hostName}`
}).reply(200, {
'dist-tags': {
latest: version
}
});
const res = await fetchLatestHostVersion();
delete process.env.HTTP_PROXY;
assert.strictEqual(res, version);
});

it('should get result', async () => {
process.env.http_proxy = 'http://localhost:9000';
const hostVersion = process.env.npm_package_version;
const hostName = process.env.npm_package_name;
const {
major, minor, patch
} = await parseSemVer(hostVersion);
const version = `${major}.${minor}.${patch + 1}`;
mockAgent.get('https://registry.npmjs.org').intercept({
path: `/${hostName}`
}).reply(200, {
'dist-tags': {
latest: version
}
});
const res = await fetchLatestHostVersion();
delete process.env.http_proxy;
assert.strictEqual(res, version);
});
});

describe('exportHostVersion', () => {
Expand Down Expand Up @@ -2694,6 +2594,57 @@ describe('addProcessListeners', () => {
});
});

describe('set dispatcher', () => {
it('should call function', () => {
const stubAgent = sinon.stub(undici, 'setGlobalDispatcher');
process.env.HTTPS_PROXY = 'http://localhost:3000';
setDispatcher();
const { calledOnce: agentCalled } = stubAgent;
stubAgent.restore();
delete process.env.HTTPS_PROXY;
assert.isTrue(agentCalled, 'called');
});

it('should call function', () => {
const stubAgent = sinon.stub(undici, 'setGlobalDispatcher');
process.env.https_proxy = 'http://localhost:3000';
setDispatcher();
const { calledOnce: agentCalled } = stubAgent;
stubAgent.restore();
delete process.env.https_proxy;
assert.isTrue(agentCalled, 'called');
});

it('should call function', () => {
const stubAgent = sinon.stub(undici, 'setGlobalDispatcher');
process.env.HTTP_PROXY = 'http://localhost:3000';
setDispatcher();
console.log(stubAgent.called);
const { calledOnce: agentCalled } = stubAgent;
stubAgent.restore();
delete process.env.HTTP_PROXY;
assert.isTrue(agentCalled, 'called');
});

it('should call function', () => {
const stubAgent = sinon.stub(undici, 'setGlobalDispatcher');
process.env.http_proxy = 'http://localhost:3000';
setDispatcher();
const { calledOnce: agentCalled } = stubAgent;
stubAgent.restore();
delete process.env.http_proxy;
assert.isTrue(agentCalled, 'called');
});

it('should not call function', () => {
const stubAgent = sinon.stub(undici, 'setGlobalDispatcher');
setDispatcher();
const { called: agentCalled } = stubAgent;
stubAgent.restore();
assert.isFalse(agentCalled, 'not called');
});
});

describe('startup', () => {
beforeEach(() => {
removeDir(TMPDIR_APP, TMPDIR);
Expand Down

0 comments on commit fa69ab7

Please sign in to comment.