@dotpress/adapter-fastify 2.0.0-alpha.2 → 2.0.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +7 -47
  2. package/package.json +4 -4
package/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  > 🚧 Dotpress V2 is in alpha (`2.0.0-alpha`): the API may still change before 2.0.0.
6
6
 
7
7
  ```sh
8
- pnpm add @dotpress/core @dotpress/adapter-fastify fastify zod
8
+ npm install @dotpress/core @dotpress/adapter-fastify fastify zod
9
9
  ```
10
10
 
11
11
  ```ts
@@ -27,59 +27,19 @@ const app = await createApp({
27
27
  await app.listen(3000)
28
28
  ```
29
29
 
30
- ## Options
31
-
32
- | Option | Description |
33
- | --- | --- |
34
- | `app` | Existing Fastify instance. Defaults to `Fastify()`. |
35
- | `beforeRoutes(app)` | Registers hooks, plugins and native routes. Hooks registered here apply to the Dotpress routes. |
36
- | `afterRoutes(app)` | Registers hooks, plugins and native routes in an encapsulated context: they do not apply to the Dotpress routes, except plugins wrapped with `fastify-plugin`, which apply to the parent context. |
37
- | `bodyLimit` | Maximum request body size in bytes. Defaults to 1 MiB. |
38
-
39
- ## Behavior
40
-
41
- - **Routes.** Each Dotpress route is a native Fastify route: the route path (`request.routeOptions.url`, `/users/:id`) shows in Fastify logs, metrics and traces, and a native route with the same method and path fails at startup. Routing follows the Dotpress rules whatever the router options: trailing slashes are ignored, `HEAD` requests use the `GET` routes, and unsupported methods get `405` with an `Allow` header. Between native and Dotpress routes, the most specific path wins (Fastify routing), whatever the registration order.
42
- - **Bodies.** Dotpress reads the bodies of its routes, with the same rules on every adapter: the Fastify body parsers only apply to native routes, and `@fastify/multipart` is not needed for uploads. `preParsing` hooks apply. Like Fastify, the adapter does not read the bodies of `GET` and `HEAD` requests, and rejects invalid `Content-Type` headers (`415`).
43
- - **Errors.** Errors with a 4xx `statusCode` raised by Fastify or its plugins (`@fastify/rate-limit`, `@fastify/sensible`…) use the Dotpress error format and keep their status and message. Other errors are unexpected errors (`500`). The adapter takes over the not-found and error handlers of the instance: they apply to native routes too.
44
- - **`ctx.req.ip`** is `request.ip`: proxy headers are only used with the `trustProxy` option of Fastify.
45
- - **Streamed responses** (`stream()`, `sse()`) are written directly to the Node.js response, so that their headers are sent immediately: `onSend` hooks (`@fastify/compress`…) do not apply to them. The headers set by earlier hooks are kept.
46
- - `ctx.raw` is the Fastify request.
47
- - The instance is ready once `createApp()` resolves: register plugins and routes in `beforeRoutes` or `afterRoutes`.
48
- - `listen(port, host?)` listens on every interface without `host`, like Node.js. `close()` closes the Fastify instance (`onClose` hooks of the plugins), waits for pending requests, and closes the responses being streamed. `adapter.server()` returns the Node.js HTTP server while listening.
49
-
50
- To test without opening a port, use `inject()` of Fastify:
51
-
52
- ```ts
53
- const adapter = fastifyAdapter()
54
- await createApp({ adapter })
55
- const res = await adapter.native().inject('/hello')
56
- ```
57
-
58
- ## On AWS Lambda
59
-
60
- ```sh
61
- pnpm add @dotpress/core @dotpress/adapter-fastify fastify @fastify/aws-lambda zod
62
- ```
30
+ On AWS Lambda, with `@fastify/aws-lambda`:
63
31
 
64
32
  ```ts
65
- import { createApp } from '@dotpress/core'
66
- import { fastifyAdapter } from '@dotpress/adapter-fastify'
67
33
  import { createLambdaHandler } from '@dotpress/adapter-fastify/lambda'
68
34
 
69
- export const handler = createLambdaHandler(() =>
70
- createApp({ adapter: fastifyAdapter(), plugins: [/* ... */] })
71
- )
35
+ export const handler = createLambdaHandler(() => createApp({ adapter: fastifyAdapter() }))
72
36
  ```
73
37
 
74
- `createLambdaHandler()` (based on [`@fastify/aws-lambda`](https://github.com/fastify/aws-lambda-fastify) 6.1 or later, which sends the events to `inject()`) handles the events of API Gateway REST APIs (payload v1), HTTP APIs and Function URLs (payload v2), and Application Load Balancers, with or without multi-value headers. Enable the multi-value headers of the target group to receive repeated query keys and send several cookies through a load balancer.
38
+ ## Documentation
75
39
 
76
- - **Lazy creation.** The factory is called at the first invocation (cold start), and the app is reused by the next invocations of the execution environment. Invocations received meanwhile wait for the same app. The handler export stays synchronous: no top-level `await`.
77
- - **Recoverable failures.** When the factory fails, the error is logged (`logger` option, console by default), the invocation gets the standard `500` error, and the next invocation calls the factory again.
78
- - **Buffered responses.** Responses are sent once complete: `stream()` works for finite streams, but Server-Sent Events (`sse()`) are not supported, and a stream that fails gets an empty `500` response rather than a truncated one. Lambda response streaming is not supported yet.
79
- - **Binary responses** are sent in base64: compressed responses, and every content type except text, JSON and XML by default, or as decided by the `isContentTypeBinary(contentType)` option.
80
- - **Query strings** are read as sent when the event has them (payload v2, load balancers): plus signs and commas keep their meaning, like on the other adapters.
81
- - **`ctx.req.ip`** is the source IP of API Gateway, or the last `X-Forwarded-For` entry behind a load balancer (appended by the load balancer); `127.0.0.1` when the event has no address. With the `trustProxy` option, Fastify reads `X-Forwarded-For` instead.
82
- - The Lambda event and context are not given to the routes: `request.awsLambda` of `@fastify/aws-lambda` needs a decoration before the instance is ready.
40
+ - [The Fastify adapter](https://dotpress.dev/adapters/fastify): options, native routes and hooks, bodies, errors, streams
41
+ - [The AWS Lambda adapter](https://dotpress.dev/adapters/aws-lambda)
42
+ - [Choosing an adapter](https://dotpress.dev/adapters/)
83
43
 
84
44
  ## License
85
45
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotpress/adapter-fastify",
3
- "version": "2.0.0-alpha.2",
3
+ "version": "2.0.0-alpha.4",
4
4
  "description": "Fastify 5 adapter for Dotpress, on Node.js and AWS Lambda",
5
5
  "author": "Renaud Guyon",
6
6
  "license": "MIT",
@@ -34,7 +34,7 @@
34
34
  "node": ">=22.12"
35
35
  },
36
36
  "peerDependencies": {
37
- "@dotpress/core": "^2.0.0-alpha.2",
37
+ "@dotpress/core": "^2.0.0-alpha.4",
38
38
  "@fastify/aws-lambda": "^6.1.0",
39
39
  "fastify": "^5.0.0"
40
40
  },
@@ -44,8 +44,8 @@
44
44
  }
45
45
  },
46
46
  "devDependencies": {
47
- "@dotpress/core": "2.0.0-alpha.2",
48
- "@dotpress/testing": "2.0.0-alpha.2",
47
+ "@dotpress/core": "2.0.0-alpha.4",
48
+ "@dotpress/testing": "2.0.0-alpha.4",
49
49
  "@fastify/aws-lambda": "^6.4.2",
50
50
  "fastify": "^5.12.8",
51
51
  "zod": "^4.6.7"