• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pmcelhaney / counterfact / 9200039388

23 May 2024 12:07AM UTC coverage: 87.981%. First build
9200039388

Pull #913

github

web-flow
Merge branch 'main' into per-endpoint-proxy
Pull Request #913: per endpoint proxy

991 of 1098 branches covered (90.26%)

Branch coverage included in aggregate %.

139 of 173 new or added lines in 7 files covered. (80.35%)

3218 of 3686 relevant lines covered (87.3%)

44.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.48
/src/repl/repl.ts
1
import repl from "node:repl";
2✔
2

2✔
3
import type { Config } from "../server/config.js";
2✔
4
import type { ContextRegistry } from "../server/context-registry.js";
2✔
5

2✔
NEW
6
function printToStdout(line: string) {
×
NEW
7
  process.stdout.write(`${line}\n`);
×
NEW
8
}
×
9

2✔
10
export function startRepl(
2✔
11
  contextRegistry: ContextRegistry,
28✔
12
  config: Config,
28✔
13
  print = printToStdout,
28✔
14
) {
28✔
15
  // eslint-disable-next-line max-statements
28✔
16
  function printProxyStatus() {
28✔
17
    if (config.proxyUrl === "") {
4✔
18
      print("The proxy URL is not set.");
2✔
19
      print('To set it, type ".proxy url <url>');
2✔
20
      return;
2✔
21
    }
2✔
22

2✔
23
    print("Proxy Configuration:");
2✔
24
    print("");
2✔
25
    print(`The proxy URL is ${config.proxyUrl}`);
2✔
26
    print("");
2✔
27
    print("Paths prefixed with [+] will be proxied.");
2✔
28
    print("Paths prefixed with [-] will not be proxied.");
2✔
29
    print("");
2✔
30

2✔
31
    // eslint-disable-next-line array-func/prefer-array-from
2✔
32
    const entries = [...config.proxyPaths.entries()].sort(([path1], [path2]) =>
2✔
33
      path1 < path2 ? -1 : 1,
2!
34
    );
2✔
35

2✔
36
    for (const [path, state] of entries) {
4✔
37
      print(`${state ? "[+]" : "[-]"} ${path}/`);
4✔
38
    }
4✔
39
  }
2✔
40

28✔
41
  function setProxyUrl(url: string | undefined) {
28✔
42
    if (url === undefined) {
4✔
43
      print("usage: .proxy url <url>");
2✔
44
      return;
2✔
45
    }
2✔
46

2✔
47
    config.proxyUrl = url;
2✔
48
    print(`proxy URL is set to ${url}`);
2✔
49
  }
2✔
50

28✔
51
  function turnProxyOnOrOff(text: string) {
28✔
52
    const [command, endpoint] = text.split(" ");
16✔
53

16✔
54
    const printEndpoint =
16✔
55
      endpoint === undefined || endpoint === "" ? "/" : endpoint;
16✔
56

16✔
57
    config.proxyPaths.set(
16✔
58
      (endpoint ?? "").replace(/\/$/u, ""),
16✔
59
      command === "on",
16✔
60
    );
16✔
61

16✔
62
    if (command === "on") {
16✔
63
      print(
8✔
64
        `Requests to ${printEndpoint} will be proxied to ${
8✔
65
          config.proxyUrl || "<proxy URL>"
8!
66
        }${printEndpoint}`,
8✔
67
      );
8✔
68
    }
8✔
69

16✔
70
    if (command === "off") {
16✔
71
      print(`Requests to ${printEndpoint} will be handled by local code`);
8✔
72
    }
8✔
73
  }
16✔
74

28✔
75
  const replServer = repl.start({ prompt: "🤖> " });
28✔
76

28✔
77
  replServer.defineCommand("counterfact", {
28✔
78
    action() {
28✔
NEW
79
      print(
×
NEW
80
        "This is a read-eval-print loop (REPL), the same as the one you get when you run node with no arguments.",
×
NEW
81
      );
×
NEW
82
      print(
×
NEW
83
        "Except that it's connected to the running server, which you can access with the following globals:",
×
NEW
84
      );
×
NEW
85
      print("");
×
NEW
86
      print(
×
NEW
87
        "- loadContext('/some/path'): to access the context object for a given path",
×
NEW
88
      );
×
NEW
89
      print("- context: the root context ( same as loadContext('/') )");
×
NEW
90
      print("");
×
NEW
91
      print(
×
NEW
92
        "For more information, see https://counterfact.dev/docs/usage.html",
×
NEW
93
      );
×
NEW
94
      print("");
×
NEW
95

×
NEW
96
      this.clearBufferedCommand();
×
NEW
97
      this.displayPrompt();
×
NEW
98
    },
×
99

28✔
100
    help: "Get help with Counterfact",
28✔
101
  });
28✔
102

28✔
103
  replServer.defineCommand("proxy", {
28✔
104
    action(text) {
28✔
105
      if (text === "help" || text === "") {
28✔
106
        print(".proxy [on|off] - turn the proxy on/off at the root level");
4✔
107
        print(".proxy [on|off] <path-prefix> - turn the proxy on for a path");
4✔
108
        print(".proxy status - show the proxy status");
4✔
109
        print(".proxy help - show this message");
4✔
110
      } else if (text.startsWith("url")) {
28✔
111
        setProxyUrl(text.split(" ")[1]);
4✔
112
      } else if (text === "status") {
24✔
113
        printProxyStatus();
4✔
114
      } else {
20✔
115
        turnProxyOnOrOff(text);
16✔
116
      }
16✔
117

28✔
118
      this.clearBufferedCommand();
28✔
119
      this.displayPrompt();
28✔
120
    },
28✔
121

28✔
122
    help: 'proxy configuration (".proxy help" for details)',
28✔
123
  });
28✔
124

28✔
125
  replServer.context.loadContext = (path: string) => contextRegistry.find(path);
28✔
126
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
28✔
127
  replServer.context.context = replServer.context.loadContext("/");
28✔
128

28✔
129
  return replServer;
28✔
130
}
28✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc