Skip to content

Commit

Permalink
chore: update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Sep 17, 2024
1 parent 45613b7 commit 2b0d96b
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 18 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

<!-- /automd -->

Universal Server API. Based on web platform standards and natively works with [Deno](https://deno.com/), [Bun](https://bun.sh/) and [Node.js](https://nodejs.org/en).
Universal Server API based on web platform standards. Works with [Deno](https://deno.com/), [Bun](https://bun.sh/) and [Node.js](https://nodejs.org/en).

- ✅ Seamless runtime integration with identical usage ([handler](https://srvx.unjs.io/guide/handler) and
- ✅ Zero overhead [Deno](https://deno.com/) and [Bun](https://bun.sh/) support
-[Node.js compatibility](https://srvx.unjs.io/guide/node) with ~native perf and [fast response]https://srvx.unjs.io/guide/node#fast-response) support

## Quick start

Expand Down Expand Up @@ -35,7 +39,7 @@ console.log(`🚀 Server ready at ${server.url}`);
<summary>local development</summary>

- Clone this repository
- Install latest LTS version of [Node.js](https://nodejs.org/en/)
- Install the latest LTS version of [Node.js](https://nodejs.org/en/)
- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`
- Install dependencies using `pnpm install`
- Run interactive tests using `pnpm dev`
Expand Down
2 changes: 1 addition & 1 deletion docs/.config/docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

name: srvx
shortDescription: Universal Server API
description: Based on web platform standards and natively works with Deno, Bun and Node.js
description: Based on web platform standards and works seamlessly with Deno, Bun and Node.js.
github: unjs/srvx
themeColor: red
landing:
Expand Down
2 changes: 1 addition & 1 deletion docs/1.guide/0.why.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ As you probably noticed, there is a difference between [Node.js][Node.js] and [D

There are subtle differences between [Deno][Deno] and [Bun][Bun] too in the way to provide options, server lifecycle, access to request info such as client IP which is not part of [Request][Request] standard are some examples.

Main usecase of this library is for tools and frameworks that want to be runtime agnostic. By using srvx as standard server layer, instead of depending on of the individual runtime APIs, we push javascript ecosystem to be more consistent and moving towards web standards!
Main use-case of this library is for tools and frameworks that want to be runtime agnostic. By using srvx as standard server layer, instead of depending on of the individual runtime APIs, we push javascript ecosystem to be more consistent and moving towards web standards!

[Deno]: https://deno.com/
[Bun]: https://bun.sh/
Expand Down
4 changes: 2 additions & 2 deletions docs/1.guide/1.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ icon: ph:book-open-duotone

# Getting started

srvx provides a unified standard API to create HTTP servers based on the standard web platform primitives, [fetch][fetch], [Request][Request] and [Response][Response] and works seamlesly with [Deno](Deno), [Bun](Bun) and [Node.js](Node.js).
srvx provides a unified standard API to create HTTP servers based on the standard web platform primitives ([fetch][fetch], [Request][Request] and [Response][Response]) and works seamlessly with [Deno](Deno), [Bun](Bun) and [Node.js](Node.js).

:read-more{to="/guide/why" title="Why srvx"}

For [Deno](Deno) and [Bun](Bun) srvx mainly translates options and subtle API differences and for [Node.js][Node.js], srvx creates a lightweight Proxy to wrap [node:IncomingMessage](IncomingMessage) as a standard [Request](Request) object and convert final state of [node:ServerResponse](ServerResponse) to a standard [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) object.
For [Deno](Deno) and [Bun](Bun) srvx unifies interface with zero overhead and for [Node.js][Node.js], creates a lightweight compatibility layer to wrap [node:IncomingMessage](IncomingMessage) as a standard [Request](Request) object and convert final state of [node:ServerResponse](ServerResponse) to a standard [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) object.

:read-more{to="/guide/node" title="Node.js support"}

Expand Down
10 changes: 4 additions & 6 deletions docs/1.guide/2.handler.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ icon: i-fluent:target-24-regular

# Fetch handler

Request handler is defined via `fetch` key since it is almost similar to [fetch][fetch] API.

The input is a [Request][Request] object and handler should return a [Response][Response] or a promise if the server handler is async.
Request handler is defined via `fetch` key since it is similar to [fetch][fetch] API. The input is a [Request][Request] object and handler should return a [Response][Response] or a promise if the server handler is async.

**Example:**

Expand All @@ -29,14 +27,14 @@ serve({

## Additional properties

> srvx **never** patches or overrides globals such as [Request][Request] and [Response][Response]. Only few lazy getters will optionally be added to the `request` object instance to allow server adoption of [Request][Request].
> srvx **never** patches, overrides or extends globals such as [Request][Request] and [Response][Response]. Only few lazy getters prefixed with `x` will optionally be added to the `request` object instance to allow server adoption of [Request][Request].
> [!TIP]
> You can use `xRequest` type for typescript usage.
### `request.xRemoteAddress?`

For [Node.js][Node.js], and [Deno][Deno]/[Bun][Bun] (only if [`xRemoteAddress`](/guide/options#xremoteaddress) server option is enabled), `request.xRemoteAddress` allows to access connected client's ipv4/ipv6 address or hostname.
For [Node.js][Node.js], ([Deno][Deno] and [Bun][Bun] only if [`xRemoteAddress`](/guide/options#xremoteaddress) server option is enabled), `request.xRemoteAddress` allows to access connected client's ipv4/ipv6 address or hostname.

```js
import { serve } from "srvx";
Expand Down Expand Up @@ -72,7 +70,7 @@ serve({
```
> [!TIP]
> srvx implementation of [Request][Request] proxy directly uses underlying [node:IncomingMessage][IncomingMessage] as source of trust. Any changes to [Request][Request] will be reflected to the underlying [node:IncomingMessage][IncomingMessage] and vise-versa.
> srvx implementation of [Request][Request] proxy directly uses the underlying [node:IncomingMessage][IncomingMessage] as source of trust. Any changes to [Request][Request] will be reflected to the underlying [node:IncomingMessage][IncomingMessage] and vise-versa.
:read-more{to="/guide/node" title="Node.js support"}
Expand Down
2 changes: 1 addition & 1 deletion docs/1.guide/4.options.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ import { serve } from "srvx";
serve({
node: {
maxHeadersize: 16384 * 2, // Double default
ipv6Only: true, // Disable doual-stack support
ipv6Only: true, // Disable dual-stack support
},
fetch: () => new Response("👋 Hello there!"),
});
Expand Down
2 changes: 1 addition & 1 deletion docs/1.guide/5.bundler.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ If you are directly using `srvx` in your project without bundling or having `srv

## Bundler usage

If unserve is being bundled (by for example [rollup](https://rollupjs.org/)), just during bundling the bundler also has to run the ESM resolution algorithm with a specific ESM condition. This will result `srvx` in the bundle to be only working with one specific runtime.
If srvx is being bundled (by for example [rollup](https://rollupjs.org/)), just during bundling the bundler also has to run the ESM resolution algorithm with a specific ESM condition. This will result `srvx` in the bundle to be only working with one specific runtime.

In order to avoid this, simplest way is to put `srvx` into the `externals` config.
6 changes: 3 additions & 3 deletions docs/1.guide/6.node.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function nodeHandler(nodeReq: IncomingMessage, nodeRes: ServerResponse) {

## Fast response

When initializing a new [Response][Response] in Node.js, a lot of extra internals have to be initialized including a [ReadableStream][ReadableStream] object for `response.body` and [Headers][Headers] for `response.headers` which adds significiant overhead since Node.js response handling does not need them.
When initializing a new [Response][Response] in Node.js, a lot of extra internals have to be initialized including a [ReadableStream][ReadableStream] object for `response.body` and [Headers][Headers] for `response.headers` which adds significant overhead since Node.js response handling does not need them.

Until there will be native [Response][Response] handling support in Node.js http module, srvx provides a faster class `NodeFastResponse`. You can use this instead to replace `Response` and improve performance.

Expand All @@ -64,13 +64,13 @@ await server.ready();
console.log(`Server running at ${server.url}`);
```

You can locally run benchmarks by cloning srvx repository from github and running `bench:node [--all]` script. Speedup in v22.8.0 was roughly **%94**!
You can locally run benchmarks by cloning [srvx repository](https://github.com/unjs/srvx) and running `npm run bench:node [--all]` script. Speedup in v22.8.0 was roughly **%94**!

## Reverse compatibility

srvx converts a [fetch][fetch]-like [Request][Request] => [Response][Response] handler to [node:IncomingMessage](IncomingMessage) => [node:ServerResponse](ServerResponse) handler that is compatible **with** Node.js runtime.

If you want to instead convert a Node.js server handler (like [Express][Express]) with `(req, IncomingMessage, res: ServerResponse)` signuture to [fetch][fetch]-like handler ([Request][Request] => [Response][Response]) that can work **without** Node.js runtime you can instead use [unjs/unenv](https://unenv.unjs.io) Node.js compatibility layer to emulate Node.js API.
If you want to instead convert a Node.js server handler (like [Express][Express]) with `(req, IncomingMessage, res: ServerResponse)` signature to [fetch][fetch]-like handler ([Request][Request] => [Response][Response]) that can work **without** Node.js runtime you can instead use [unjs/unenv](https://unenv.unjs.io) Node.js compatibility layer to emulate Node.js API.

```js
// unenv does not have any Node.js dependency
Expand Down
2 changes: 1 addition & 1 deletion src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class NodeServer extends Server {
request.xNode = { req: nodeReq, res: nodeRes };
const res = options.fetch(request);
return res instanceof Promise
? res.then((reolvedRes) => sendNodeResponse(nodeRes, reolvedRes))
? res.then((resolvedRes) => sendNodeResponse(nodeRes, resolvedRes))
: sendNodeResponse(nodeRes, res);
},
));
Expand Down

0 comments on commit 2b0d96b

Please sign in to comment.