Skip to content

Commit bdf0dfd

Browse files
pi0pigent
andauthored
feat: srvx fetch (#169)
Co-authored-by: pi0::agent <[email protected]>
1 parent 85cce89 commit bdf0dfd

File tree

9 files changed

+324
-5
lines changed

9 files changed

+324
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ $ bunx --bun srvx
6060
| `jsx` | [examples/jsx](https://github.com/h3js/srvx/tree/main/examples/jsx/) | `npx giget gh:h3js/srvx/examples/jsx srvx-jsx` |
6161
| `node-handler` | [examples/node-handler](https://github.com/h3js/srvx/tree/main/examples/node-handler/) | `npx giget gh:h3js/srvx/examples/node-handler srvx-node-handler` |
6262
| `service-worker` | [examples/service-worker](https://github.com/h3js/srvx/tree/main/examples/service-worker/) | `npx giget gh:h3js/srvx/examples/service-worker srvx-service-worker` |
63+
| `streaming` | [examples/streaming](https://github.com/h3js/srvx/tree/main/examples/streaming/) | `npx giget gh:h3js/srvx/examples/streaming srvx-streaming` |
6364
| `tracing` | [examples/tracing](https://github.com/h3js/srvx/tree/main/examples/tracing/) | `npx giget gh:h3js/srvx/examples/tracing srvx-tracing` |
6465
| `websocket` | [examples/websocket](https://github.com/h3js/srvx/tree/main/examples/websocket/) | `npx giget gh:h3js/srvx/examples/websocket srvx-websocket` |
6566

docs/1.guide/1.index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ bun run server.mjs
9898
| `jsx` | [examples/jsx](https://github.com/h3js/srvx/tree/main/examples/jsx/) | `npx giget gh:h3js/srvx/examples/jsx srvx-jsx` |
9999
| `node-handler` | [examples/node-handler](https://github.com/h3js/srvx/tree/main/examples/node-handler/) | `npx giget gh:h3js/srvx/examples/node-handler srvx-node-handler` |
100100
| `service-worker` | [examples/service-worker](https://github.com/h3js/srvx/tree/main/examples/service-worker/) | `npx giget gh:h3js/srvx/examples/service-worker srvx-service-worker` |
101+
| `streaming` | [examples/streaming](https://github.com/h3js/srvx/tree/main/examples/streaming/) | `npx giget gh:h3js/srvx/examples/streaming srvx-streaming` |
101102
| `tracing` | [examples/tracing](https://github.com/h3js/srvx/tree/main/examples/tracing/) | `npx giget gh:h3js/srvx/examples/tracing srvx-tracing` |
102103
| `websocket` | [examples/websocket](https://github.com/h3js/srvx/tree/main/examples/websocket/) | `npx giget gh:h3js/srvx/examples/websocket srvx-websocket` |
103104

docs/1.guide/8.cli.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ $ srvx --host=localhost # Bind to localhost only
5252
$ srvx --import=jiti/register # Enable [jiti](https://github.com/unjs/jiti) loader
5353
$ srvx --tls --cert=cert.pem --key=key.pem # Enable TLS (HTTPS/HTTP2)
5454

55+
FETCH MODE
56+
57+
# srvx fetch [options] [url]
58+
$ srvx fetch # Fetch from default entry
59+
$ srvx fetch /api/users # Fetch a specific URL/path
60+
$ srvx fetch --entry ./server.ts /api/users # Fetch using a specific entry
61+
$ srvx fetch -X POST /api/users # POST request
62+
$ srvx fetch -H "Content-Type: application/json" /api # With headers
63+
$ srvx fetch -d '{"name":"foo"}' /api # With request body
64+
$ echo '{"name":"foo"}' | srvx fetch -d @- /api # Body from stdin
65+
$ srvx fetch -v /api/users # Verbose output (show headers)
5566

5667
ARGUMENTS
5768

@@ -69,7 +80,14 @@ OPTIONS
6980
--cert <file> TLS certificate file
7081
--key <file> TLS private key file
7182
-h, --help Show this help message
72-
-v, --version Show server and runtime versions
83+
--version Show server and runtime versions
84+
85+
FETCH OPTIONS
86+
87+
-X, --request <method> HTTP method (default: GET, or POST if body is provided)
88+
-H, --header <header> Add header (format: "Name: Value", can be used multiple times)
89+
-d, --data <data> Request body (use @- for stdin, @file for file)
90+
-v, --verbose Show request and response headers
7391

7492
ENVIRONMENT
7593

examples/streaming/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "srvx-examples-hello-world",
3+
"version": "0.0.0",
4+
"type": "module",
5+
"scripts": {
6+
"start": "srvx --prod",
7+
"dev": "srvx"
8+
},
9+
"devDependencies": {
10+
"srvx": "latest"
11+
}
12+
}

examples/streaming/server.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Fixture: Streaming lorem ipsum text word by word
2+
const loremIpsum =
3+
"Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua";
4+
5+
export default {
6+
fetch: (): Response => {
7+
const words = loremIpsum.split(" ");
8+
let index = 0;
9+
10+
const stream = new ReadableStream({
11+
async pull(controller) {
12+
if (index < words.length) {
13+
const word = words[index] + (index < words.length - 1 ? " " : "");
14+
controller.enqueue(new TextEncoder().encode(word));
15+
index++;
16+
await new Promise((resolve) => setTimeout(resolve, 100));
17+
} else {
18+
controller.close();
19+
}
20+
},
21+
});
22+
23+
return new Response(stream, {
24+
headers: {
25+
"Content-Type": "text/plain",
26+
"Cache-Control": "no-cache",
27+
"X-Content-Type-Options": "nosniff",
28+
},
29+
});
30+
},
31+
};

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)