Skip to content

Commit d29444c

Browse files
authored
major: update to fastify v4 (#25)
1 parent 51bc2e8 commit d29444c

7 files changed

Lines changed: 291 additions & 304 deletions

File tree

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"extends": "standard"}

.github/workflows/ci.yml

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,14 @@ name: ci
22

33
on:
44
push:
5+
paths-ignore:
6+
- 'docs/**'
7+
- '*.md'
58
pull_request:
6-
7-
env:
8-
CI: true
9+
paths-ignore:
10+
- 'docs/**'
11+
- '*.md'
912

1013
jobs:
1114
test:
12-
runs-on: ${{ matrix.os }}
13-
14-
strategy:
15-
matrix:
16-
node-version: [10, 12, 14, 16]
17-
os: [ubuntu-latest, windows-latest]
18-
19-
steps:
20-
- uses: actions/checkout@v2
21-
22-
- name: Use Node.js
23-
uses: actions/setup-node@v2
24-
with:
25-
node-version: ${{ matrix.node-version }}
26-
27-
- name: Install
28-
run: |
29-
npm install --ignore-scripts
30-
31-
- name: Run tests
32-
run: |
33-
npm test
15+
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ Adds the raw body to the Fastify request object.
88

99
## Install
1010

11-
### Fastify v3
12-
1311
```
1412
npm i fastify-raw-body
1513
```
@@ -20,6 +18,7 @@ npm i fastify-raw-body
2018
| ------------- |:---------------:|
2119
| `^2.0.0` | `^2.0.0` |
2220
| `^3.0.0` | `^3.0.0` |
21+
| `^4.0.0` | `^4.0.0` |
2322

2423

2524
## Usage
@@ -28,10 +27,10 @@ This plugin will add the `request.rawBody`.
2827
It will get the data using the [`preParsing`](https://www.fastify.io/docs/latest/Reference/Hooks/#preparsing) hook.
2928

3029
```js
31-
const Fastify = require('fastify')
32-
const app = Fastify()
30+
import Fastify from 'fastify'
3331

34-
app.register(require('fastify-raw-body'), {
32+
const app = Fastify()
33+
await app.register(import('fastify-raw-body'), {
3534
field: 'rawBody', // change the default request.rawBody property name
3635
global: false, // add the rawBody to every request. **Default true**
3736
encoding: 'utf8', // set it to false to set rawBody as a Buffer **Default utf8**
@@ -51,10 +50,15 @@ app.post('/', {
5150
})
5251
```
5352

54-
Notice: Setting `global: false` and then the route configuration `{ config: { rawBody: true } }` will
55-
save memory of your server since the `rawBody` is a copy of the `body` and it will double the memory usage.
53+
> **Note**
54+
> You need to `await` the plugin registration to make sure the plugin is ready to use.
55+
> All the routes defined **before** the plugin registration will be ignored.
56+
> This change has been introduced in Fastify v4.
5657
57-
So use it only for the routes that you need to.
58+
> **Warning**
59+
> Setting `global: false` and then the route configuration `{ config: { rawBody: true } }` will
60+
> save memory of your server since the `rawBody` is a copy of the `body` and it will double the memory usage.
61+
> So use it only for the routes that you need to.
5862
5963
### Raw body as Buffer
6064

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"scripts": {
88
"lint": "standard",
99
"lint:fix": "standard --fix",
10-
"test": "tap test/**.test.js && tsd"
10+
"test": "tap test/**.test.* && tsd"
1111
},
1212
"repository": {
1313
"type": "git",
@@ -29,16 +29,16 @@
2929
},
3030
"homepage": "https://github.com/Eomm/fastify-raw-body#readme",
3131
"devDependencies": {
32-
"@types/node": "^14.14.17",
33-
"fastify": "^3.0.0-rc.4",
32+
"@types/node": "^17.0.36",
33+
"fastify": "^4.0.0-rc.4",
3434
"standard": "^17.0.0",
35-
"tap": "^15.0.1",
36-
"tsd": "^0.14.0"
35+
"tap": "^16.2.0",
36+
"tsd": "^0.20.0"
3737
},
3838
"dependencies": {
39-
"fastify-plugin": "^2.0.0",
40-
"raw-body": "^2.4.1",
41-
"secure-json-parse": "^2.1.0"
39+
"fastify-plugin": "^3.0.1",
40+
"raw-body": "^2.5.1",
41+
"secure-json-parse": "^2.4.0"
4242
},
4343
"tsd": {
4444
"directory": "test/types"

plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,6 @@ function rawBody (fastify, opts, next) {
9797
}
9898

9999
module.exports = fp(rawBody, {
100-
fastify: '^3.0.0',
100+
fastify: '^4',
101101
name: 'fastify-raw-body'
102102
})

test/example.test.mjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as t from 'tap'
2+
import Fastify from 'fastify'
3+
4+
await t.test('register in plugins', async t => {
5+
const app = Fastify()
6+
7+
app.register((i, o, next) => {
8+
i.register(import('../plugin.js'))
9+
next()
10+
})
11+
app.register((i, o, next) => {
12+
i.register(import('../plugin.js'))
13+
next()
14+
})
15+
16+
await app.ready()
17+
})

0 commit comments

Comments
 (0)