-
Notifications
You must be signed in to change notification settings - Fork 226
Add devproxy installation script and update .gitignore #1647
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
Changes from all commits
aa135cd
838e9d5
16b3191
950dc32
db48868
70a3671
5c4b73c
dd05878
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -13,15 +13,15 @@ import { | |||||
| import { errorMessage } from "./error.js"; | ||||||
| import { logVerbose } from "./util.js"; | ||||||
| import { CancellationOptions } from "./cancellation.js"; | ||||||
| import { resolveHttpProxyAgent } from "./proxy.js"; | ||||||
| import { resolveHttpsProxyAgent } from "./proxy.js"; | ||||||
| import { host } from "./host.js"; | ||||||
| import { renderWithPrecision } from "./precision.js"; | ||||||
| import crossFetch from "cross-fetch"; | ||||||
| import debug from "debug"; | ||||||
| import { prettyStrings } from "./pretty.js"; | ||||||
| import type { FetchOptions, RetryOptions } from "./types.js"; | ||||||
| import { genaiscriptDebug } from "./debug.js"; | ||||||
|
|
||||||
| const dbg = debug("genaiscript:fetch"); | ||||||
| const dbg = genaiscriptDebug("fetch"); | ||||||
|
|
||||||
| /** | ||||||
| * Parses the retry-after header value. | ||||||
|
|
@@ -51,7 +51,7 @@ export function parseRetryAfter(retryAfterHeader: string): number | null { | |||||
| const delaySeconds = Math.max(0, Math.ceil(delayMs / 1000)); | ||||||
| return delaySeconds; | ||||||
| } | ||||||
| } catch(e) { | ||||||
| } catch (e) { | ||||||
| dbg(`failed to parse retry-after header as date: %s`, errorMessage(e)); | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -84,31 +84,38 @@ export type FetchType = ( | |||||
| export async function createFetch( | ||||||
| options?: TraceOptions & CancellationOptions & RetryOptions, | ||||||
| ): Promise<FetchType> { | ||||||
| options = options || {}; | ||||||
| const { | ||||||
| retries = FETCH_RETRY_DEFAULT, | ||||||
| retryOn = FETCH_RETRY_ON_DEFAULT, | ||||||
| trace, | ||||||
| retryDelay = FETCH_RETRY_DEFAULT_DEFAULT, | ||||||
| maxDelay = FETCH_RETRY_MAX_DELAY_DEFAULT, | ||||||
| cancellationToken, | ||||||
| } = options || {}; | ||||||
| } = options; | ||||||
|
|
||||||
| dbg(`create fetch`); | ||||||
| // We create a proxy based on Node.js environment variables. | ||||||
| const agent = await resolveHttpProxyAgent(); | ||||||
| const agent = await resolveHttpsProxyAgent(); | ||||||
|
|
||||||
| // We enrich crossFetch with the proxy. | ||||||
| const crossFetchWithProxy: typeof fetch = agent | ||||||
| ? (url, options) => crossFetch(url, { ...(options || {}), dispatcher: agent } as any) | ||||||
| ? (url, options) => crossFetch(url, { ...options, agent } as RequestInit) | ||||||
|
Contributor
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. The fetch agent is being set using the
Suggested change
|
||||||
| : crossFetch; | ||||||
|
|
||||||
| const loggingFetch: typeof fetch = (url, options) => { | ||||||
| dbg(`fetch: %s %s`, options?.method || "GET", url); | ||||||
| return crossFetchWithProxy(url, options); | ||||||
| }; | ||||||
|
|
||||||
| // Return the default fetch if no retry status codes are specified | ||||||
| if (!retryOn?.length) { | ||||||
| dbg("no retry logic applied, using crossFetchWithProxy directly"); | ||||||
| return crossFetchWithProxy; | ||||||
| return loggingFetch; | ||||||
| } | ||||||
|
|
||||||
| // Create a fetch function with retry logic | ||||||
| const fetchRetry = wrapFetch(crossFetchWithProxy, { | ||||||
| const fetchRetry = wrapFetch(loggingFetch, { | ||||||
| retryOn, | ||||||
| retries, | ||||||
| retryDelay: (attempt, error, response) => { | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,7 +2,18 @@ | |||||
| // Licensed under the MIT License. | ||||||
|
|
||||||
| import { genaiscriptDebug } from "./debug.js"; | ||||||
| const dbg = genaiscriptDebug("proxy"); | ||||||
| const dbg = genaiscriptDebug("fetch:proxy"); | ||||||
|
|
||||||
| function resolveProxyUrl() { | ||||||
| const proxy = | ||||||
| process.env.GENAISCRIPT_HTTPS_PROXY || | ||||||
| process.env.GENAISCRIPT_HTTP_PROXY || | ||||||
| process.env.HTTPS_PROXY || | ||||||
| process.env.HTTP_PROXY || | ||||||
| process.env.https_proxy || | ||||||
| process.env.http_proxy; | ||||||
| return proxy; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Resolves an HTTP proxy agent based on environment variables. | ||||||
|
|
@@ -23,19 +34,28 @@ const dbg = genaiscriptDebug("proxy"); | |||||
| * @returns An instance of `HttpsProxyAgent` if a proxy is configured, | ||||||
| * or null if no proxy is detected. | ||||||
| */ | ||||||
| export async function resolveHttpProxyAgent() { | ||||||
| export async function resolveUndiciProxyAgent() { | ||||||
| // We create a proxy based on Node.js environment variables. | ||||||
| const proxy = | ||||||
| process.env.GENAISCRIPT_HTTPS_PROXY || | ||||||
| process.env.GENAISCRIPT_HTTP_PROXY || | ||||||
| process.env.HTTPS_PROXY || | ||||||
| process.env.HTTP_PROXY || | ||||||
| process.env.https_proxy || | ||||||
| process.env.http_proxy; | ||||||
| if (proxy) dbg(`proxy: %s`, proxy); | ||||||
| const proxy = resolveProxyUrl(); | ||||||
| if (!proxy) return null; | ||||||
|
|
||||||
| dbg(`proxy (undici): %s`, proxy); | ||||||
| const { ProxyAgent } = await import("undici"); | ||||||
| const agent = new ProxyAgent(proxy); | ||||||
| agent.on(`connect`, (info) => dbg(`connect: %s`, info.href)); | ||||||
| agent.on(`connectionError`, (err) => dbg(`connection error: %s`, err.toString())); | ||||||
| agent.on(`disconnect`, () => dbg(`disconnect`)); | ||||||
|
Contributor
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. The ProxyAgent from "undici" does not emit 'connect', 'connectionError', or 'disconnect' events. Attempting to attach event listeners to these events will have no effect and may cause confusion or runtime errors. Please refer to the "undici" documentation for supported events and usage.
Contributor
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. The event listeners
Suggested change
|
||||||
| return agent; | ||||||
| } | ||||||
|
|
||||||
| export async function resolveHttpsProxyAgent() { | ||||||
| const proxyUrl = resolveProxyUrl(); | ||||||
| if (!proxyUrl) return null; | ||||||
|
|
||||||
| dbg(`proxy (hpa): %s`, proxyUrl); | ||||||
| const { HttpsProxyAgent } = await import("https-proxy-agent"); | ||||||
| const agent = new HttpsProxyAgent(proxyUrl); | ||||||
| agent.on(`connect`, (info) => dbg(`connect: %s`, info.href)); | ||||||
| agent.on(`error`, (err) => dbg(`error: %s`, err.toString())); | ||||||
|
Contributor
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. The event listeners
Suggested change
|
||||||
| return agent; | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.29.0/rc.schema.json", | ||
| "plugins": [ | ||
| { | ||
| "name": "DevToolsPlugin", | ||
| "enabled": true, | ||
| "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll", | ||
| "configSection": "devTools" | ||
| } | ||
| ], | ||
| "devTools": { | ||
| "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.29.0/devtoolsplugin.schema.json", | ||
| "preferredBrowser": "EdgeDev" | ||
| }, | ||
| "urlsToWatch": ["*"], | ||
| "logLevel": "debug", | ||
| "newVersionNotification": "stable", | ||
| "showSkipMessages": true, | ||
| "asSystemProxy": false | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
The function
errorMessage(e)is used, but there is no import or definition forerrorMessagein this file. This will cause a runtime error iferrorMessageis not defined elsewhere in the scope.