-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #913 from pmcelhaney/per-endpoint-proxy
per endpoint proxy
- Loading branch information
Showing
19 changed files
with
1,627 additions
and
5,543 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"counterfact": minor | ||
--- | ||
|
||
turn the proxy on or off for individual paths |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
[tools] | ||
node = "20" | ||
node = "22" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import repl from "node:repl"; | ||
|
||
import type { Config } from "../server/config.js"; | ||
import type { ContextRegistry } from "../server/context-registry.js"; | ||
|
||
function printToStdout(line: string) { | ||
process.stdout.write(`${line}\n`); | ||
} | ||
|
||
export function startRepl( | ||
contextRegistry: ContextRegistry, | ||
config: Config, | ||
print = printToStdout, | ||
) { | ||
// eslint-disable-next-line max-statements | ||
function printProxyStatus() { | ||
if (config.proxyUrl === "") { | ||
print("The proxy URL is not set."); | ||
print('To set it, type ".proxy url <url>'); | ||
return; | ||
} | ||
|
||
print("Proxy Configuration:"); | ||
print(""); | ||
print(`The proxy URL is ${config.proxyUrl}`); | ||
print(""); | ||
print("Paths prefixed with [+] will be proxied."); | ||
print("Paths prefixed with [-] will not be proxied."); | ||
print(""); | ||
|
||
// eslint-disable-next-line array-func/prefer-array-from | ||
const entries = [...config.proxyPaths.entries()].sort(([path1], [path2]) => | ||
path1 < path2 ? -1 : 1, | ||
); | ||
|
||
for (const [path, state] of entries) { | ||
print(`${state ? "[+]" : "[-]"} ${path}/`); | ||
} | ||
} | ||
|
||
function setProxyUrl(url: string | undefined) { | ||
if (url === undefined) { | ||
print("usage: .proxy url <url>"); | ||
return; | ||
} | ||
|
||
config.proxyUrl = url; | ||
print(`proxy URL is set to ${url}`); | ||
} | ||
|
||
function turnProxyOnOrOff(text: string) { | ||
const [command, endpoint] = text.split(" "); | ||
|
||
const printEndpoint = | ||
endpoint === undefined || endpoint === "" ? "/" : endpoint; | ||
|
||
config.proxyPaths.set( | ||
(endpoint ?? "").replace(/\/$/u, ""), | ||
command === "on", | ||
); | ||
|
||
if (command === "on") { | ||
print( | ||
`Requests to ${printEndpoint} will be proxied to ${ | ||
config.proxyUrl || "<proxy URL>" | ||
}${printEndpoint}`, | ||
); | ||
} | ||
|
||
if (command === "off") { | ||
print(`Requests to ${printEndpoint} will be handled by local code`); | ||
} | ||
} | ||
|
||
const replServer = repl.start({ prompt: "🤖> " }); | ||
|
||
replServer.defineCommand("counterfact", { | ||
action() { | ||
print( | ||
"This is a read-eval-print loop (REPL), the same as the one you get when you run node with no arguments.", | ||
); | ||
print( | ||
"Except that it's connected to the running server, which you can access with the following globals:", | ||
); | ||
print(""); | ||
print( | ||
"- loadContext('/some/path'): to access the context object for a given path", | ||
); | ||
print("- context: the root context ( same as loadContext('/') )"); | ||
print(""); | ||
print( | ||
"For more information, see https://counterfact.dev/docs/usage.html", | ||
); | ||
print(""); | ||
|
||
this.clearBufferedCommand(); | ||
this.displayPrompt(); | ||
}, | ||
|
||
help: "Get help with Counterfact", | ||
}); | ||
|
||
replServer.defineCommand("proxy", { | ||
action(text) { | ||
if (text === "help" || text === "") { | ||
print(".proxy [on|off] - turn the proxy on/off at the root level"); | ||
print(".proxy [on|off] <path-prefix> - turn the proxy on for a path"); | ||
print(".proxy status - show the proxy status"); | ||
print(".proxy help - show this message"); | ||
} else if (text.startsWith("url")) { | ||
setProxyUrl(text.split(" ")[1]); | ||
} else if (text === "status") { | ||
printProxyStatus(); | ||
} else { | ||
turnProxyOnOrOff(text); | ||
} | ||
|
||
this.clearBufferedCommand(); | ||
this.displayPrompt(); | ||
}, | ||
|
||
help: 'proxy configuration (".proxy help" for details)', | ||
}); | ||
|
||
replServer.context.loadContext = (path: string) => contextRegistry.find(path); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call | ||
replServer.context.context = replServer.context.loadContext("/"); | ||
|
||
return replServer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
interface ProxyConfig { | ||
proxyPaths: Map<string, boolean>; | ||
} | ||
|
||
export function isProxyEnabledForPath( | ||
path: string, | ||
config: ProxyConfig, | ||
): boolean { | ||
if (config.proxyPaths.has(path)) { | ||
return config.proxyPaths.get(path) ?? false; | ||
} | ||
|
||
if (path === "") { | ||
return false; | ||
} | ||
|
||
const parentPath = path.slice(0, Math.max(0, path.lastIndexOf("/"))); | ||
|
||
return isProxyEnabledForPath(parentPath, config); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.