From b608bc823e7b9698862ecce3b130f6f4b925387f Mon Sep 17 00:00:00 2001 From: roll Date: Tue, 14 May 2024 15:45:12 +0100 Subject: [PATCH 01/16] Added an implementation plan --- desktop/python.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/desktop/python.ts b/desktop/python.ts index eab6abbf9..3f06016d6 100644 --- a/desktop/python.ts +++ b/desktop/python.ts @@ -18,6 +18,9 @@ export async function ensurePython() { log.info('[ensurePython]', { message }) } +// TODO: +// 1. Detect proxy using a library (https://www.npmjs.com/package/get-proxy-settings) +// 2. Pass them as environment variables to PIP (https://stackoverflow.com/a/41957788) export async function ensureLibraries() { log.info('[ensureLibraries]') From 0c002bcff7a08e20e79c1af9a137072118e999e0 Mon Sep 17 00:00:00 2001 From: roll Date: Wed, 22 May 2024 08:46:04 +0100 Subject: [PATCH 02/16] Use better name for starting desktop dev --- Makefile | 10 +++++----- package.json | 2 +- portal/content/docs/contributing/development.md | 8 +++++--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 090a577d5..b0e05b822 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: all build client dist docs format install lint preview release server start test type version +.PHONY: all build client desktop dist docs format install lint release server start test type version VERSION := $(shell node -p -e "require('./package.json').version") @@ -15,6 +15,10 @@ build: client: npm run start +## Runs the Electron application with live reload (requires a running server). +desktop: + npm run desktop + ## Runs electron-builder to package and build a ready for distribution app. dist: npm run dist @@ -38,10 +42,6 @@ lint: hatch run lint npm run lint -## Runs the Electron application with live reload (requires a running server). -preview: - npm run preview - release: git checkout main && git pull origin && git fetch -p @git log --pretty=format:"%C(yellow)%h%Creset %s%Cgreen%d" --reverse -20 diff --git a/package.json b/package.json index e86ead116..6c474c55f 100644 --- a/package.json +++ b/package.json @@ -26,10 +26,10 @@ "build": "electron-vite build", "dist": "electron-builder", "coverage": "sensible-browser coverage/index.html", + "desktop": "electron-vite dev", "format": "prettier --write \"client/**/*.ts*\" && eslint --fix \"client/**/*.ts*\"", "lint": "prettier --check \"client/**/*.ts*\" && eslint \"client/**/*.ts*\"", "prepare": "husky install", - "preview": "electron-vite dev", "start": "vite --open --port 8080", "spec": "vitest run", "test": "npm run lint && npm run type && npm run spec", diff --git a/portal/content/docs/contributing/development.md b/portal/content/docs/contributing/development.md index 43fe4fe3a..eeba458e4 100644 --- a/portal/content/docs/contributing/development.md +++ b/portal/content/docs/contributing/development.md @@ -86,12 +86,14 @@ npm run start ### Desktop -Previewing descript application: +> Note that you need to start the server as well + +Running the desktop application: ```bash -make preview +make desktop # OR -npm run preview +npm run desktop ``` ## Documentation From e3b90cd3eeaeb983f2c36bc54376600f01bd247f Mon Sep 17 00:00:00 2001 From: roll Date: Wed, 22 May 2024 08:57:48 +0100 Subject: [PATCH 03/16] Added get-proxy-settings lib --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 6c474c55f..6cac8c658 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "dependencies": { "@electron-toolkit/utils": "2.0.1", "electron-log": "4.4.8", + "get-proxy-settings": "0.1.13", "portfinder": "1.0.32", "toml": "3.0.0" }, From ec5a4ad8f51d140356f6ca74ffd1d4da35412d68 Mon Sep 17 00:00:00 2001 From: roll Date: Wed, 22 May 2024 09:06:19 +0100 Subject: [PATCH 04/16] Support env in system calls --- desktop/system.ts | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/desktop/system.ts b/desktop/system.ts index af45f19e4..b4dae02d2 100644 --- a/desktop/system.ts +++ b/desktop/system.ts @@ -17,17 +17,29 @@ export async function findPort() { return port } -export async function execFile(path: string, args: string[], cwd?: string) { - log.info('[execFile]', { path, args, cwd }) - - const { stdout } = await execFilePromise(path, args, { cwd }) +export async function execFile( + path: string, + args: string[], + opts?: { cwd?: string; env?: Record } +) { + log.info('[execFile]', { path, args, opts }) + + const cwd = opts?.cwd + const env = { ...process.env, ...opts?.env } + const { stdout } = await execFilePromise(path, args, { cwd, env }) return stdout } -export async function spawnFile(path: string, args: string[], cwd?: string) { - log.info('[spawnFile]', { path, args, cwd }) +export async function spawnFile( + path: string, + args: string[], + opts?: { cwd?: string; env?: Record } +) { + log.info('[spawnFile]', { path, args, opts }) - const proc = cp.spawn(path, args, { cwd }) + const cwd = opts?.cwd + const env = { ...process.env, ...opts?.env } + const proc = cp.spawn(path, args, { cwd, env }) proc.stdout.on('data', (data) => log.info(data.toString().trim())) proc.stderr.on('data', (data) => log.error(data.toString().trim())) proc.on('close', (code) => { From f63c9751898e61d447127582a8880f76241126d3 Mon Sep 17 00:00:00 2001 From: roll Date: Wed, 22 May 2024 09:29:52 +0100 Subject: [PATCH 05/16] Implemented detectProxyConfiguration --- desktop/python.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/desktop/python.ts b/desktop/python.ts index 3f06016d6..e22ff34fb 100644 --- a/desktop/python.ts +++ b/desktop/python.ts @@ -5,6 +5,7 @@ import { join } from 'path' import log from 'electron-log' import toml from 'toml' import * as system from './system' +import { getProxySettings } from 'get-proxy-settings' export async function ensurePython() { log.info('[ensurePython]', { path: settings.APP_PYTHON }) @@ -68,3 +69,16 @@ export async function readInstalledLibraries() { log.info('[readInstalledLibraries]', { data }) return data } + +export async function detectProxyConfiguration() { + log.info('[detectProxyConfiguration]') + + const proxy = await getProxySettings() + const proxyUrlFull = proxy?.http ? proxy.http.toString() : undefined + const proxyUrl = proxy?.http + ? `http://***@${proxy.http.host}:${proxy.http.port}` + : undefined + + log.info('[detectProxyConfiguration]', { proxyUrl }) + return proxyUrlFull +} From 0aaecc1223079c77dc7b276fcfaf3c6b25a0f888 Mon Sep 17 00:00:00 2001 From: roll Date: Wed, 22 May 2024 09:38:33 +0100 Subject: [PATCH 06/16] Call pip with http_proxy env --- desktop/python.ts | 30 ++++++++++++++---------------- desktop/system.ts | 4 ++-- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/desktop/python.ts b/desktop/python.ts index e22ff34fb..c7fde2f12 100644 --- a/desktop/python.ts +++ b/desktop/python.ts @@ -25,19 +25,17 @@ export async function ensurePython() { export async function ensureLibraries() { log.info('[ensureLibraries]') + const httpProxyUrl = await detectHttpProxyUrl() const required = await readRequiredLibraries() const installed = await readInstalledLibraries() const missing = required.filter((spec) => !installed.includes(spec)) if (!missing.length) return - await system.execFile(settings.PYTHON_TARGET, [ - '-m', - 'pip', - 'install', - '--upgrade', - '--disable-pip-version-check', - ...missing, - ]) + await system.execFile( + settings.PYTHON_TARGET, + ['-m', 'pip', 'install', '--upgrade', '--disable-pip-version-check', ...missing], + { env: { http_proxy: httpProxyUrl } } + ) log.info('[ensureLibraries]', { missing }) } @@ -70,15 +68,15 @@ export async function readInstalledLibraries() { return data } -export async function detectProxyConfiguration() { - log.info('[detectProxyConfiguration]') +export async function detectHttpProxyUrl() { + log.info('[detectHttpProxyUrl]') const proxy = await getProxySettings() - const proxyUrlFull = proxy?.http ? proxy.http.toString() : undefined - const proxyUrl = proxy?.http - ? `http://***@${proxy.http.host}:${proxy.http.port}` - : undefined + const url = proxy?.http ? proxy.http.toString() : undefined + const message = proxy?.http + ? `proxy detected: http://***@${proxy.http.host}:${proxy.http.port}` + : `no proxy detected` - log.info('[detectProxyConfiguration]', { proxyUrl }) - return proxyUrlFull + log.info('[detectHttpProxyUrl]', { message }) + return url } diff --git a/desktop/system.ts b/desktop/system.ts index b4dae02d2..67ce829af 100644 --- a/desktop/system.ts +++ b/desktop/system.ts @@ -20,7 +20,7 @@ export async function findPort() { export async function execFile( path: string, args: string[], - opts?: { cwd?: string; env?: Record } + opts?: { cwd?: string; env?: Record } ) { log.info('[execFile]', { path, args, opts }) @@ -33,7 +33,7 @@ export async function execFile( export async function spawnFile( path: string, args: string[], - opts?: { cwd?: string; env?: Record } + opts?: { cwd?: string; env?: Record } ) { log.info('[spawnFile]', { path, args, opts }) From 97a9b78e8dff99dc8fe7eb6d17c69d84bef9d208 Mon Sep 17 00:00:00 2001 From: roll Date: Wed, 22 May 2024 09:41:41 +0100 Subject: [PATCH 07/16] Removed implementation plan --- desktop/python.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/desktop/python.ts b/desktop/python.ts index c7fde2f12..255d44d6e 100644 --- a/desktop/python.ts +++ b/desktop/python.ts @@ -19,9 +19,6 @@ export async function ensurePython() { log.info('[ensurePython]', { message }) } -// TODO: -// 1. Detect proxy using a library (https://www.npmjs.com/package/get-proxy-settings) -// 2. Pass them as environment variables to PIP (https://stackoverflow.com/a/41957788) export async function ensureLibraries() { log.info('[ensureLibraries]') From 292e12e280af877a9ba523f21a30b524beae5ca8 Mon Sep 17 00:00:00 2001 From: roll Date: Wed, 22 May 2024 09:51:44 +0100 Subject: [PATCH 08/16] Added a comment --- desktop/python.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/desktop/python.ts b/desktop/python.ts index 255d44d6e..e9fcb0dc3 100644 --- a/desktop/python.ts +++ b/desktop/python.ts @@ -31,6 +31,7 @@ export async function ensureLibraries() { await system.execFile( settings.PYTHON_TARGET, ['-m', 'pip', 'install', '--upgrade', '--disable-pip-version-check', ...missing], + // https://stackoverflow.com/a/41957788 { env: { http_proxy: httpProxyUrl } } ) From 3b2246242cf2b87b1bdd49fd33794de427b29bbd Mon Sep 17 00:00:00 2001 From: roll Date: Mon, 3 Jun 2024 14:43:24 +0100 Subject: [PATCH 09/16] Pass http proxy to the server as well --- desktop/index.ts | 5 +++-- desktop/python.ts | 27 +++++++++------------------ desktop/server.ts | 13 +++++++++++-- desktop/system.ts | 14 ++++++++++++++ tsconfig.json | 1 + 5 files changed, 38 insertions(+), 22 deletions(-) diff --git a/desktop/index.ts b/desktop/index.ts index 3e6b5b8e8..74cdc2521 100644 --- a/desktop/index.ts +++ b/desktop/index.ts @@ -18,6 +18,7 @@ app.whenReady().then(async () => { log.info('# Start application') electronApp.setAppUserModelId(settings.APP_USER_MODEL_ID) const serverPort = await system.findPort() + const httpProxyUrl = await system.detectHttpProxyUrl() log.info('## Start client') createBridge({ serverPort }) @@ -27,8 +28,8 @@ app.whenReady().then(async () => { log.info('## Start server') await resources.ensureRunner() await python.ensurePython() - await python.ensureLibraries() - await server.runServer({ serverPort }) + await python.ensureLibraries({ httpProxyUrl }) + await server.runServer({ httpProxyUrl, serverPort }) } }) diff --git a/desktop/python.ts b/desktop/python.ts index e9fcb0dc3..6f278826b 100644 --- a/desktop/python.ts +++ b/desktop/python.ts @@ -5,7 +5,6 @@ import { join } from 'path' import log from 'electron-log' import toml from 'toml' import * as system from './system' -import { getProxySettings } from 'get-proxy-settings' export async function ensurePython() { log.info('[ensurePython]', { path: settings.APP_PYTHON }) @@ -19,10 +18,9 @@ export async function ensurePython() { log.info('[ensurePython]', { message }) } -export async function ensureLibraries() { +export async function ensureLibraries(props: { httpProxyUrl?: string }) { log.info('[ensureLibraries]') - const httpProxyUrl = await detectHttpProxyUrl() const required = await readRequiredLibraries() const installed = await readInstalledLibraries() const missing = required.filter((spec) => !installed.includes(spec)) @@ -31,8 +29,14 @@ export async function ensureLibraries() { await system.execFile( settings.PYTHON_TARGET, ['-m', 'pip', 'install', '--upgrade', '--disable-pip-version-check', ...missing], - // https://stackoverflow.com/a/41957788 - { env: { http_proxy: httpProxyUrl } } + { + // pip requires explicit proxy settings + // https://stackoverflow.com/a/41957788 + env: { + HTTP_PROXY: props.httpProxyUrl, // UNIX + http_proxy: props.httpProxyUrl, // Windows + }, + } ) log.info('[ensureLibraries]', { missing }) @@ -65,16 +69,3 @@ export async function readInstalledLibraries() { log.info('[readInstalledLibraries]', { data }) return data } - -export async function detectHttpProxyUrl() { - log.info('[detectHttpProxyUrl]') - - const proxy = await getProxySettings() - const url = proxy?.http ? proxy.http.toString() : undefined - const message = proxy?.http - ? `proxy detected: http://***@${proxy.http.host}:${proxy.http.port}` - : `no proxy detected` - - log.info('[detectHttpProxyUrl]', { message }) - return url -} diff --git a/desktop/server.ts b/desktop/server.ts index 2bc04831c..8310e3be8 100644 --- a/desktop/server.ts +++ b/desktop/server.ts @@ -2,14 +2,23 @@ import { spawnFile } from './system' import * as settings from './settings' import log from 'electron-log' -export async function runServer({ serverPort }: { serverPort: number }) { +export async function runServer(props: { httpProxyUrl?: string; serverPort: number }) { + const { serverPort } = props log.info('[runServer]', { serverPort }) // Start server const proc = spawnFile( settings.PYTHON_TARGET, ['-m', 'server', settings.APP_TMP, '--port', serverPort.toString()], - process.resourcesPath + { + cwd: process.resourcesPath, + // frictionless-py uses `requests` for HTTP requests + // https://stackoverflow.com/questions/8287628/proxies-with-python-requests-module + env: { + HTTP_PROXY: props.httpProxyUrl, // UNIX + http_proxy: props.httpProxyUrl, // Windows + }, + } ) return proc diff --git a/desktop/system.ts b/desktop/system.ts index 67ce829af..e24d71586 100644 --- a/desktop/system.ts +++ b/desktop/system.ts @@ -4,6 +4,7 @@ import log from 'electron-log' import portfinder from 'portfinder' import { is } from '@electron-toolkit/utils' import * as settings from './settings' +import { getProxySettings } from 'get-proxy-settings' const execFilePromise = util.promisify(cp.execFile) export async function findPort() { @@ -17,6 +18,19 @@ export async function findPort() { return port } +export async function detectHttpProxyUrl() { + log.info('[detectHttpProxyUrl]') + + const proxy = await getProxySettings() + const url = proxy?.http ? proxy.http.toString() : undefined + const message = proxy?.http + ? `proxy detected: http://***@${proxy.http.host}:${proxy.http.port}` + : `no proxy detected` + + log.info('[detectHttpProxyUrl]', { message }) + return url +} + export async function execFile( path: string, args: string[], diff --git a/tsconfig.json b/tsconfig.json index 89a736351..28a3622b5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,6 +11,7 @@ "noUnusedParameters": true, "noImplicitReturns": true, "noFallthroughCasesInSwitch": false, + "allowImportingTsExtensions": true, "experimentalDecorators": true, "inlineSourceMap": true, "inlineSources": true, From 0231858dc224c6de9b1e25be37d20b5bbb9f0036 Mon Sep 17 00:00:00 2001 From: roll Date: Mon, 3 Jun 2024 14:57:49 +0100 Subject: [PATCH 10/16] Added TODO --- desktop/system.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/desktop/system.ts b/desktop/system.ts index e24d71586..9592d9ed8 100644 --- a/desktop/system.ts +++ b/desktop/system.ts @@ -18,6 +18,9 @@ export async function findPort() { return port } +// TODO: consider using `ses.resolveProxy(url)` instead +// https://www.electronjs.org/docs/latest/api/session/#sesresolveproxyurl +// https://github.com/felicienfrancois/node-electron-proxy-agent/blob/master/index.js export async function detectHttpProxyUrl() { log.info('[detectHttpProxyUrl]') From 6f92412be1969c73e695740e161f6e628615c53b Mon Sep 17 00:00:00 2001 From: roll Date: Tue, 4 Jun 2024 11:07:02 +0100 Subject: [PATCH 11/16] Fixed proxy detection --- desktop/server.ts | 2 +- desktop/system.ts | 24 ++++++++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/desktop/server.ts b/desktop/server.ts index 8310e3be8..8120b148b 100644 --- a/desktop/server.ts +++ b/desktop/server.ts @@ -11,7 +11,7 @@ export async function runServer(props: { httpProxyUrl?: string; serverPort: numb settings.PYTHON_TARGET, ['-m', 'server', settings.APP_TMP, '--port', serverPort.toString()], { - cwd: process.resourcesPath, + cwd: settings.DIST, // frictionless-py uses `requests` for HTTP requests // https://stackoverflow.com/questions/8287628/proxies-with-python-requests-module env: { diff --git a/desktop/system.ts b/desktop/system.ts index 9592d9ed8..58adc24d3 100644 --- a/desktop/system.ts +++ b/desktop/system.ts @@ -18,17 +18,25 @@ export async function findPort() { return port } -// TODO: consider using `ses.resolveProxy(url)` instead -// https://www.electronjs.org/docs/latest/api/session/#sesresolveproxyurl -// https://github.com/felicienfrancois/node-electron-proxy-agent/blob/master/index.js +// We cannot use `ses.resolveProxy(url)` because it does not return credentials export async function detectHttpProxyUrl() { log.info('[detectHttpProxyUrl]') + let message = 'no proxy detected' + let url: string | undefined - const proxy = await getProxySettings() - const url = proxy?.http ? proxy.http.toString() : undefined - const message = proxy?.http - ? `proxy detected: http://***@${proxy.http.host}:${proxy.http.port}` - : `no proxy detected` + // NOTE: + // the proxy detection library is broken; it fails if only one kind of proxy is set + // we can safely mix HTTP/HTTPS because we are only interested in the HTTP one + process.env.HTTP_PROXY = process.env.HTTP_PROXY || process.env.HTTPS_PROXY + process.env.HTTPS_PROXY = process.env.HTTPS_PROXY || process.env.HTTP_PROXY + + try { + const proxy = await getProxySettings() + if (proxy?.http) { + url = proxy.http.toString() + message = `proxy detected: http://***@${proxy.http.host}:${proxy.http.port}` + } + } catch {} log.info('[detectHttpProxyUrl]', { message }) return url From 9728f4c5961cfb20cc9b00b40952e385308cef06 Mon Sep 17 00:00:00 2001 From: roll Date: Tue, 4 Jun 2024 11:15:50 +0100 Subject: [PATCH 12/16] Removed env from logging --- desktop/system.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/desktop/system.ts b/desktop/system.ts index 58adc24d3..9e46979f9 100644 --- a/desktop/system.ts +++ b/desktop/system.ts @@ -47,11 +47,12 @@ export async function execFile( args: string[], opts?: { cwd?: string; env?: Record } ) { - log.info('[execFile]', { path, args, opts }) - const cwd = opts?.cwd - const env = { ...process.env, ...opts?.env } - const { stdout } = await execFilePromise(path, args, { cwd, env }) + const thisEnv = opts?.env || {} + const fullEnv = { ...process.env, ...opts?.env } + log.info('[execFile]', { path, args, cwd, env: Object.keys(thisEnv) }) + + const { stdout } = await execFilePromise(path, args, { cwd, env: fullEnv }) return stdout } @@ -60,11 +61,12 @@ export async function spawnFile( args: string[], opts?: { cwd?: string; env?: Record } ) { - log.info('[spawnFile]', { path, args, opts }) - const cwd = opts?.cwd - const env = { ...process.env, ...opts?.env } - const proc = cp.spawn(path, args, { cwd, env }) + const thisEnv = opts?.env || {} + const fullEnv = { ...process.env, ...opts?.env } + log.info('[spawnFile]', { path, args, cwd, env: Object.keys(thisEnv) }) + + const proc = cp.spawn(path, args, { cwd, env: fullEnv }) proc.stdout.on('data', (data) => log.info(data.toString().trim())) proc.stderr.on('data', (data) => log.error(data.toString().trim())) proc.on('close', (code) => { From 20f4f3ec643d2e85c3e40910e99d7c19df07c802 Mon Sep 17 00:00:00 2001 From: roll Date: Tue, 4 Jun 2024 11:50:55 +0100 Subject: [PATCH 13/16] Support https proxy as well --- desktop/index.ts | 6 +++--- desktop/python.ts | 25 ++++++++++++++++--------- desktop/server.ts | 14 +++++++------- desktop/system.ts | 22 ++++++++++------------ desktop/types/index.ts | 1 + desktop/types/proxy.ts | 1 + 6 files changed, 38 insertions(+), 31 deletions(-) create mode 100644 desktop/types/index.ts create mode 100644 desktop/types/proxy.ts diff --git a/desktop/index.ts b/desktop/index.ts index 74cdc2521..50562f2c1 100644 --- a/desktop/index.ts +++ b/desktop/index.ts @@ -18,7 +18,7 @@ app.whenReady().then(async () => { log.info('# Start application') electronApp.setAppUserModelId(settings.APP_USER_MODEL_ID) const serverPort = await system.findPort() - const httpProxyUrl = await system.detectHttpProxyUrl() + const proxyUrls = await system.detectProxyUrls() log.info('## Start client') createBridge({ serverPort }) @@ -28,8 +28,8 @@ app.whenReady().then(async () => { log.info('## Start server') await resources.ensureRunner() await python.ensurePython() - await python.ensureLibraries({ httpProxyUrl }) - await server.runServer({ httpProxyUrl, serverPort }) + await python.ensureLibraries({ proxyUrls }) + await server.runServer({ proxyUrls, serverPort }) } }) diff --git a/desktop/python.ts b/desktop/python.ts index 6f278826b..46fe319c3 100644 --- a/desktop/python.ts +++ b/desktop/python.ts @@ -1,6 +1,7 @@ import fs from 'fs' import fsp from 'fs/promises' import * as settings from './settings' +import * as types from './types' import { join } from 'path' import log from 'electron-log' import toml from 'toml' @@ -18,7 +19,7 @@ export async function ensurePython() { log.info('[ensurePython]', { message }) } -export async function ensureLibraries(props: { httpProxyUrl?: string }) { +export async function ensureLibraries(props: { proxyUrls: types.IProxyUrls }) { log.info('[ensureLibraries]') const required = await readRequiredLibraries() @@ -29,14 +30,7 @@ export async function ensureLibraries(props: { httpProxyUrl?: string }) { await system.execFile( settings.PYTHON_TARGET, ['-m', 'pip', 'install', '--upgrade', '--disable-pip-version-check', ...missing], - { - // pip requires explicit proxy settings - // https://stackoverflow.com/a/41957788 - env: { - HTTP_PROXY: props.httpProxyUrl, // UNIX - http_proxy: props.httpProxyUrl, // Windows - }, - } + { env: createEnvFromProxyUrls(props.proxyUrls) } ) log.info('[ensureLibraries]', { missing }) @@ -69,3 +63,16 @@ export async function readInstalledLibraries() { log.info('[readInstalledLibraries]', { data }) return data } + +export function createEnvFromProxyUrls(proxyUrls: types.IProxyUrls) { + // https://stackoverflow.com/a/41957788 + // https://stackoverflow.com/questions/8287628/proxies-with-python-requests-module + return { + // UNIX + HTTP_PROXY: proxyUrls.http, + HTTPS_PROXY: proxyUrls.https, + // Windows + http_proxy: proxyUrls.http, + https_proxy: proxyUrls.https, + } +} diff --git a/desktop/server.ts b/desktop/server.ts index 8120b148b..6d3be604c 100644 --- a/desktop/server.ts +++ b/desktop/server.ts @@ -1,8 +1,13 @@ import { spawnFile } from './system' +import { createEnvFromProxyUrls } from './python' import * as settings from './settings' +import * as types from './types' import log from 'electron-log' -export async function runServer(props: { httpProxyUrl?: string; serverPort: number }) { +export async function runServer(props: { + proxyUrls: types.IProxyUrls + serverPort: number +}) { const { serverPort } = props log.info('[runServer]', { serverPort }) @@ -12,12 +17,7 @@ export async function runServer(props: { httpProxyUrl?: string; serverPort: numb ['-m', 'server', settings.APP_TMP, '--port', serverPort.toString()], { cwd: settings.DIST, - // frictionless-py uses `requests` for HTTP requests - // https://stackoverflow.com/questions/8287628/proxies-with-python-requests-module - env: { - HTTP_PROXY: props.httpProxyUrl, // UNIX - http_proxy: props.httpProxyUrl, // Windows - }, + env: createEnvFromProxyUrls(props.proxyUrls), } ) diff --git a/desktop/system.ts b/desktop/system.ts index 9e46979f9..fbd4a5f69 100644 --- a/desktop/system.ts +++ b/desktop/system.ts @@ -4,6 +4,7 @@ import log from 'electron-log' import portfinder from 'portfinder' import { is } from '@electron-toolkit/utils' import * as settings from './settings' +import * as types from './types' import { getProxySettings } from 'get-proxy-settings' const execFilePromise = util.promisify(cp.execFile) @@ -19,27 +20,24 @@ export async function findPort() { } // We cannot use `ses.resolveProxy(url)` because it does not return credentials -export async function detectHttpProxyUrl() { - log.info('[detectHttpProxyUrl]') - let message = 'no proxy detected' - let url: string | undefined +export async function detectProxyUrls() { + log.info('[detectProxyUrls]') + const proxyUrls: types.IProxyUrls = {} - // NOTE: + // TODO: // the proxy detection library is broken; it fails if only one kind of proxy is set - // we can safely mix HTTP/HTTPS because we are only interested in the HTTP one + // we might need to fix it in the library process.env.HTTP_PROXY = process.env.HTTP_PROXY || process.env.HTTPS_PROXY process.env.HTTPS_PROXY = process.env.HTTPS_PROXY || process.env.HTTP_PROXY try { const proxy = await getProxySettings() - if (proxy?.http) { - url = proxy.http.toString() - message = `proxy detected: http://***@${proxy.http.host}:${proxy.http.port}` - } + if (proxy?.http) proxyUrls.http = proxy.http.toString() + if (proxy?.https) proxyUrls.https = proxy.https.toString() } catch {} - log.info('[detectHttpProxyUrl]', { message }) - return url + log.info('[detectHttpProxyUrl]', { proxies: Object.keys(proxyUrls) }) + return proxyUrls } export async function execFile( diff --git a/desktop/types/index.ts b/desktop/types/index.ts new file mode 100644 index 000000000..e739ac856 --- /dev/null +++ b/desktop/types/index.ts @@ -0,0 +1 @@ +export * from './proxy' diff --git a/desktop/types/proxy.ts b/desktop/types/proxy.ts new file mode 100644 index 000000000..95cbb1500 --- /dev/null +++ b/desktop/types/proxy.ts @@ -0,0 +1 @@ +export type IProxyUrls = { http?: string; https?: string } From 53677b7001ed97ca63a4545e5d909847e1841ea2 Mon Sep 17 00:00:00 2001 From: roll Date: Tue, 4 Jun 2024 11:54:46 +0100 Subject: [PATCH 14/16] Removed underlaying library hacking --- desktop/system.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/desktop/system.ts b/desktop/system.ts index fbd4a5f69..5127c2a2e 100644 --- a/desktop/system.ts +++ b/desktop/system.ts @@ -19,17 +19,13 @@ export async function findPort() { return port } +// TODO: it only works if both HTTP ans HTTPS proxies are set +// If only one of them is set, the `get-proxy-settings` library fails // We cannot use `ses.resolveProxy(url)` because it does not return credentials export async function detectProxyUrls() { log.info('[detectProxyUrls]') const proxyUrls: types.IProxyUrls = {} - // TODO: - // the proxy detection library is broken; it fails if only one kind of proxy is set - // we might need to fix it in the library - process.env.HTTP_PROXY = process.env.HTTP_PROXY || process.env.HTTPS_PROXY - process.env.HTTPS_PROXY = process.env.HTTPS_PROXY || process.env.HTTP_PROXY - try { const proxy = await getProxySettings() if (proxy?.http) proxyUrls.http = proxy.http.toString() From 88c31d11e18ca4dc4a65f5896a3598e424b88214 Mon Sep 17 00:00:00 2001 From: roll Date: Tue, 4 Jun 2024 13:30:32 +0100 Subject: [PATCH 15/16] Updated comment --- desktop/system.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/desktop/system.ts b/desktop/system.ts index 5127c2a2e..97eeca85d 100644 --- a/desktop/system.ts +++ b/desktop/system.ts @@ -19,7 +19,7 @@ export async function findPort() { return port } -// TODO: it only works if both HTTP ans HTTPS proxies are set +// It only works if both HTTP ans HTTPS proxies are set // If only one of them is set, the `get-proxy-settings` library fails // We cannot use `ses.resolveProxy(url)` because it does not return credentials export async function detectProxyUrls() { From 4ac59f7c7587f5e3b1ef8d96807056abc690b2a5 Mon Sep 17 00:00:00 2001 From: gtzatchkova Date: Fri, 21 Jun 2024 17:25:16 +0200 Subject: [PATCH 16/16] Trigger Build