Skip to content

Commit

Permalink
major: update to fastify v4 (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eomm authored May 31, 2022
1 parent 51bc2e8 commit d29444c
Show file tree
Hide file tree
Showing 7 changed files with 291 additions and 304 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"extends": "standard"}
32 changes: 7 additions & 25 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,14 @@ name: ci

on:
push:
paths-ignore:
- 'docs/**'
- '*.md'
pull_request:

env:
CI: true
paths-ignore:
- 'docs/**'
- '*.md'

jobs:
test:
runs-on: ${{ matrix.os }}

strategy:
matrix:
node-version: [10, 12, 14, 16]
os: [ubuntu-latest, windows-latest]

steps:
- uses: actions/checkout@v2

- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}

- name: Install
run: |
npm install --ignore-scripts
- name: Run tests
run: |
npm test
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ Adds the raw body to the Fastify request object.

## Install

### Fastify v3

```
npm i fastify-raw-body
```
Expand All @@ -20,6 +18,7 @@ npm i fastify-raw-body
| ------------- |:---------------:|
| `^2.0.0` | `^2.0.0` |
| `^3.0.0` | `^3.0.0` |
| `^4.0.0` | `^4.0.0` |


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

```js
const Fastify = require('fastify')
const app = Fastify()
import Fastify from 'fastify'

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

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

Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"lint": "standard",
"lint:fix": "standard --fix",
"test": "tap test/**.test.js && tsd"
"test": "tap test/**.test.* && tsd"
},
"repository": {
"type": "git",
Expand All @@ -29,16 +29,16 @@
},
"homepage": "https://github.com/Eomm/fastify-raw-body#readme",
"devDependencies": {
"@types/node": "^14.14.17",
"fastify": "^3.0.0-rc.4",
"@types/node": "^17.0.36",
"fastify": "^4.0.0-rc.4",
"standard": "^17.0.0",
"tap": "^15.0.1",
"tsd": "^0.14.0"
"tap": "^16.2.0",
"tsd": "^0.20.0"
},
"dependencies": {
"fastify-plugin": "^2.0.0",
"raw-body": "^2.4.1",
"secure-json-parse": "^2.1.0"
"fastify-plugin": "^3.0.1",
"raw-body": "^2.5.1",
"secure-json-parse": "^2.4.0"
},
"tsd": {
"directory": "test/types"
Expand Down
2 changes: 1 addition & 1 deletion plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@ function rawBody (fastify, opts, next) {
}

module.exports = fp(rawBody, {
fastify: '^3.0.0',
fastify: '^4',
name: 'fastify-raw-body'
})
17 changes: 17 additions & 0 deletions test/example.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as t from 'tap'
import Fastify from 'fastify'

await t.test('register in plugins', async t => {
const app = Fastify()

app.register((i, o, next) => {
i.register(import('../plugin.js'))
next()
})
app.register((i, o, next) => {
i.register(import('../plugin.js'))
next()
})

await app.ready()
})
Loading

0 comments on commit d29444c

Please sign in to comment.