-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🌎 Allow https proxy from env var #933
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'myst-cli-utils': patch | ||
'myst-templates': patch | ||
'myst-execute': patch | ||
'myst-to-jats': patch | ||
'myst-cli': patch | ||
--- | ||
|
||
Move fetch function to session |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'myst-cli': patch | ||
--- | ||
|
||
Allow https proxy set as environment var |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import type { RequestInfo, RequestInit, Response } from 'node-fetch'; | ||
|
||
export type Logger = Pick<typeof console, 'debug' | 'info' | 'warn' | 'error'>; | ||
|
||
export type LoggerDE = Pick<Logger, 'debug' | 'error'>; | ||
|
||
export interface ISession { | ||
log: Logger; | ||
fetch(url: URL | RequestInfo, init?: RequestInit): Promise<Response>; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import fs from 'fs-extra'; | ||
import path from 'node:path'; | ||
import { writeFileToFolder } from 'myst-cli-utils'; | ||
import fetch from 'node-fetch'; | ||
import type { ISession } from '../../session/types.js'; | ||
import { getSiteManifest } from '../site/manifest.js'; | ||
import { getMystTemplate } from '../site/template.js'; | ||
|
@@ -111,7 +110,7 @@ export async function buildHtml(session: ISession, opts: any) { | |
// Fetch all HTML pages and assets by the template | ||
await Promise.all( | ||
routes.map(async (page) => { | ||
const resp = await fetch(page.url); | ||
const resp = await session.fetch(page.url); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not really sure about the implications of using an HTTPS proxy for Do we need logic in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes sounds like we do? i.e. skip proxy requests for any localhost / 127.0.0.1 requests based on the url, and proxy all others? |
||
const content = await resp.text(); | ||
writeFileToFolder(path.join(htmlDir, page.path), content); | ||
}), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import type { MystPlugin, RuleId } from 'myst-common'; | |
import latestVersion from 'latest-version'; | ||
import boxen from 'boxen'; | ||
import chalk from 'chalk'; | ||
import { HttpsProxyAgent } from 'https-proxy-agent'; | ||
import { | ||
findCurrentProjectAndLoad, | ||
findCurrentSiteAndLoad, | ||
|
@@ -22,6 +23,7 @@ import type { ISession } from './types.js'; | |
import { KernelManager, ServerConnection, SessionManager } from '@jupyterlab/services'; | ||
import type { JupyterServerSettings } from 'myst-execute'; | ||
import { findExistingJupyterServer, launchJupyterServer } from 'myst-execute'; | ||
import type { RequestInfo, RequestInit } from 'node-fetch'; | ||
import { default as nodeFetch, Headers, Request, Response } from 'node-fetch'; | ||
|
||
// fetch polyfill for node<18 | ||
|
@@ -36,6 +38,7 @@ const CONFIG_FILES = ['myst.yml']; | |
const API_URL = 'https://api.mystmd.org'; | ||
const NPM_COMMAND = 'npm i -g mystmd@latest'; | ||
const PIP_COMMAND = 'pip install -U mystmd'; | ||
const LOCALHOSTS = ['localhost', '127.0.0.1', '::1']; | ||
|
||
export function logUpdateAvailable({ | ||
current, | ||
|
@@ -72,6 +75,7 @@ export class Session implements ISession { | |
store: Store<RootState>; | ||
$logger: Logger; | ||
|
||
proxyAgent?: HttpsProxyAgent<string>; | ||
_shownUpgrade = false; | ||
_latestVersion?: string; | ||
_jupyterSessionManagerPromise?: Promise<SessionManager | undefined>; | ||
|
@@ -84,6 +88,8 @@ export class Session implements ISession { | |
this.API_URL = API_URL; | ||
this.configFiles = CONFIG_FILES; | ||
this.$logger = opts.logger ?? chalkLogger(LogLevel.info, process.cwd()); | ||
const proxyUrl = process.env.HTTPS_PROXY; | ||
if (proxyUrl) this.proxyAgent = new HttpsProxyAgent(proxyUrl); | ||
this.store = createStore(rootReducer); | ||
this.reload(); | ||
// Allow the latest version to be loaded | ||
|
@@ -116,6 +122,18 @@ export class Session implements ISession { | |
return this; | ||
} | ||
|
||
async fetch(url: URL | RequestInfo, init?: RequestInit): Promise<Response> { | ||
const urlOnly = new URL((url as Request).url ?? (url as URL | string)); | ||
this.log.debug(`Fetching: ${urlOnly}`); | ||
if (this.proxyAgent && !LOCALHOSTS.includes(urlOnly.hostname)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kept this localhost check super simple... We could try to catch more edge cases with something like this: https://github.com/Kikobeats/localhost-url-regex/blob/master/src/index.js and/or we could add a I feel like all of that is probably overkill though. |
||
if (!init) init = {}; | ||
init = { agent: this.proxyAgent, ...init }; | ||
this.log.debug(`Using HTTPS proxy: ${this.proxyAgent.proxy}`); | ||
} | ||
const resp = await nodeFetch(url, init); | ||
return resp; | ||
} | ||
|
||
plugins: MystPlugin | undefined; | ||
|
||
_pluginPromise: Promise<MystPlugin> | undefined; | ||
|
@@ -196,7 +214,7 @@ export class Session implements ISession { | |
}; | ||
} else { | ||
// Load existing running server | ||
const existing = await findExistingJupyterServer(); | ||
const existing = await findExistingJupyterServer(this); | ||
if (existing) { | ||
this.log.debug(`Found existing server on: ${existing.appUrl}`); | ||
partialServerSettings = existing; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This super minimal session implementation isn't used anywhere (besides a single test file - easily replaced). I just removed it so we did not need to implement
fetch
in this package.