@@ -4,6 +4,8 @@ import { createReadStream } from "node:fs";
44
55import { loadServerEntry , type LoadOptions } from "../loader.ts" ;
66import type { CLIOptions } from "./types.ts" ;
7+ import type { ServerHandler } from "../types.ts" ;
8+ import { resolve } from "node:path" ;
79
810export 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 ) {
0 commit comments