Skip to content

Commit 20796e2

Browse files
committed
refactor(cli): improve fetch mode
1 parent 3b224b8 commit 20796e2

File tree

2 files changed

+43
-49
lines changed

2 files changed

+43
-49
lines changed

src/cli/fetch.ts

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { createReadStream } from "node:fs";
44

55
import { loadServerEntry, type LoadOptions } from "../loader.ts";
66
import type { CLIOptions } from "./types.ts";
7+
import type { ServerHandler } from "../types.ts";
8+
import { resolve } from "node:path";
79

810
export async function cliFetch(
911
cliOpts: CLIOptions &
@@ -18,49 +20,44 @@ export async function cliFetch(
1820
const stdout = cliOpts.stdout || process.stdout;
1921
const stderr = cliOpts.stderr || process.stderr;
2022

21-
const loaded = await loadServerEntry({
22-
dir: cliOpts.dir,
23-
entry: cliOpts.entry,
24-
...cliOpts?.loader,
25-
});
23+
let fetchHandler: ServerHandler = globalThis.fetch;
2624

27-
if (cliOpts.verbose && loaded.url) {
28-
stderr.write(`* Entry: ${fileURLToPath(loaded.url)}\n`);
29-
if (loaded.nodeCompat) {
30-
stderr.write(`* Using node compat mode\n`);
31-
}
32-
}
25+
const inputURL = cliOpts.url || "/";
3326

34-
if (loaded.notFound) {
35-
if (URL.canParse?.(cliOpts.url || "")) {
36-
stderr.write(
37-
`* WARNING: server entry file not found. Falling back to network fetch for URL: ${cliOpts.url}\n`,
38-
);
39-
loaded.fetch = globalThis.fetch.bind(globalThis);
40-
} else {
41-
throw new Error("Server entry file not found.", {
27+
if (inputURL[0] === "/") {
28+
const loaded = await loadServerEntry({
29+
dir: cliOpts.dir,
30+
entry: cliOpts.entry,
31+
...cliOpts?.loader,
32+
});
33+
if (cliOpts.verbose && loaded.url) {
34+
stderr.write(`* Entry: ${fileURLToPath(loaded.url)}\n`);
35+
if (loaded.nodeCompat) {
36+
stderr.write(`* Using node compat mode\n`);
37+
}
38+
}
39+
if (loaded.notFound) {
40+
throw new Error(`Server entry file not found in ${resolve(cliOpts.dir || ".")}`, {
4241
cause: {
4342
dir: cliOpts.dir || process.cwd(),
4443
entry: cliOpts.entry || undefined,
4544
},
4645
});
46+
} else if (!loaded.fetch) {
47+
throw new Error("No fetch handler exported", {
48+
cause: {
49+
dir: cliOpts.dir || process.cwd(),
50+
entry: cliOpts.entry || undefined,
51+
loaded,
52+
},
53+
});
4754
}
48-
} else if (!loaded.fetch) {
49-
throw new Error("No fetch handler exported", {
50-
cause: {
51-
dir: cliOpts.dir || process.cwd(),
52-
entry: cliOpts.entry || undefined,
53-
loaded,
54-
},
55-
});
55+
fetchHandler = loaded.fetch;
56+
} else {
57+
stderr.write(`* Fetching remote URL: ${inputURL}\n`);
58+
fetchHandler = globalThis.fetch;
5659
}
5760

58-
// Build request URL
59-
const url = new URL(
60-
cliOpts.url || "/",
61-
`http${cliOpts.tls ? "s" : ""}://${cliOpts.host || "localhost"}`,
62-
).toString();
63-
6461
// Build Headers
6562
const headers = new Headers();
6663
if (cliOpts.header) {
@@ -74,10 +71,13 @@ export async function cliFetch(
7471
}
7572
}
7673
if (!headers.has("User-Agent")) {
77-
headers.set("User-Agent", "curl/7.81.0");
74+
headers.set("User-Agent", "srvx (curl)");
7875
}
7976
if (!headers.has("Accept")) {
80-
headers.set("Accept", "text/markdown, text/plain, text/html, text/*;q=0.9, */*;q=0.8");
77+
headers.set(
78+
"Accept",
79+
"text/markdown, application/json;q=0.9, text/plain;q=0.8, text/html;q=0.7, text/*;q=0.6, */*;q=0.5",
80+
);
8181
}
8282

8383
// Build body
@@ -101,9 +101,12 @@ export async function cliFetch(
101101
}
102102
}
103103

104-
const method = cliOpts.method || (body === undefined ? "GET" : "POST");
105-
106104
// Build request
105+
const method = cliOpts.method || (body === undefined ? "GET" : "POST");
106+
const url = new URL(
107+
cliOpts.url || "/",
108+
`http${cliOpts.tls ? "s" : ""}://${cliOpts.host || cliOpts.hostname || "localhost"}`,
109+
);
107110
const req = new Request(url, {
108111
method,
109112
headers,
@@ -121,7 +124,7 @@ export async function cliFetch(
121124
stderr.write(">\n");
122125
}
123126

124-
const res = await loaded.fetch(req);
127+
const res = await fetchHandler(req);
125128

126129
// Verbose: print response info
127130
if (cliOpts.verbose) {

src/cli/main.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,10 @@ export async function main(mainOpts: MainOptions): Promise<void> {
3535
// Fetch mode
3636
if (cliOpts.mode === "fetch") {
3737
try {
38-
const res = await cliFetch({
39-
url: cliOpts.url,
40-
entry: cliOpts.entry,
41-
dir: cliOpts.dir,
42-
method: cliOpts.method,
43-
header: cliOpts.header,
44-
data: cliOpts.data,
45-
verbose: cliOpts.verbose,
46-
host: cliOpts.host,
47-
});
38+
const res = await cliFetch(cliOpts);
4839
process.exit(res.ok ? 0 : 22);
4940
} catch (error) {
50-
console.error("\n" + c.red((error as Error)?.stack || String(error)) + "\n");
41+
console.error(error);
5142
process.exit(1);
5243
}
5344
}

0 commit comments

Comments
 (0)