fastify-raw-body
Advanced tools
Comparing version 2.0.0 to 3.0.0
{ | ||
"name": "fastify-raw-body", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "Request raw body", | ||
@@ -25,12 +25,12 @@ "main": "plugin.js", | ||
"engines": { | ||
"node": ">= 6" | ||
"node": ">= 10" | ||
}, | ||
"homepage": "https://github.com/Eomm/fastify-raw-body#readme", | ||
"devDependencies": { | ||
"fastify": "^2.15.1", | ||
"fastify": "^3.0.0-rc.4", | ||
"standard": "^14.3.4", | ||
"tap": "^12.6.1" | ||
"tap": "^14.10.7" | ||
}, | ||
"dependencies": { | ||
"fastify-plugin": "^1.0.0", | ||
"fastify-plugin": "^2.0.0", | ||
"raw-body": "^2.4.1", | ||
@@ -37,0 +37,0 @@ "secure-json-parse": "^2.1.0" |
@@ -15,6 +15,7 @@ 'use strict' | ||
const { field, encoding, global } = Object.assign({ | ||
const { field, encoding, global, runFirst } = Object.assign({ | ||
field: 'rawBody', | ||
encoding: 'utf8', | ||
global: true | ||
global: true, | ||
runFirst: false | ||
}, opts) | ||
@@ -35,5 +36,13 @@ | ||
} else if (Array.isArray(routeOptions.preParsing)) { | ||
routeOptions.preParsing.push(preparsingRawBody) | ||
if (runFirst) { | ||
routeOptions.preParsing.unshift(preparsingRawBody) | ||
} else { | ||
routeOptions.preParsing.push(preparsingRawBody) | ||
} | ||
} else { | ||
routeOptions.preParsing = [routeOptions.preParsing, preparsingRawBody] | ||
if (runFirst) { | ||
routeOptions.preParsing = [preparsingRawBody, routeOptions.preParsing] | ||
} else { | ||
routeOptions.preParsing = [routeOptions.preParsing, preparsingRawBody] | ||
} | ||
} | ||
@@ -46,4 +55,4 @@ } | ||
function preparsingRawBody (request, reply, done) { | ||
getRawBody(request.raw, { | ||
function preparsingRawBody (request, reply, payload, done) { | ||
getRawBody(runFirst ? request.raw : payload, { | ||
length: null, // avoid content lenght check: fastify will do it | ||
@@ -65,3 +74,3 @@ limit: fastify.initialConfig.bodyLimit, // limit to avoid memory leak or DoS | ||
done() | ||
done(null, payload) | ||
} | ||
@@ -90,4 +99,4 @@ | ||
module.exports = fp(rawBody, { | ||
fastify: '^2.0.0', | ||
fastify: '^3.0.0', | ||
name: 'fastify-raw-body' | ||
}) |
@@ -6,6 +6,8 @@ # fastify-raw-body | ||
Adds the raw body to the Fastify **v2** request object. | ||
Adds the raw body to the Fastify request object. | ||
## Install | ||
### Fastify v3 | ||
``` | ||
@@ -15,2 +17,10 @@ npm i fastify-raw-body | ||
### Fastify v2 | ||
The version `2.x` of this module support Fastify v2 and Node.js>=6 | ||
``` | ||
npm i fastify-raw-body@2.0.0 | ||
``` | ||
## Usage | ||
@@ -28,3 +38,4 @@ | ||
global: false, // add the rawBody to every request. **Default true** | ||
encoding: 'utf8' // set it to false to set rawBody as a Buffer **Default utf8** | ||
encoding: 'utf8', // set it to false to set rawBody as a Buffer **Default utf8** | ||
runFirst: true // get the body before any preParsing hook change/uncompress it. **Default false** | ||
}) | ||
@@ -31,0 +42,0 @@ |
@@ -5,2 +5,3 @@ 'use strict' | ||
const Fastify = require('fastify') | ||
const { Transform, Readable } = require('stream') | ||
const rawBody = require('../plugin') | ||
@@ -22,3 +23,3 @@ | ||
app.addHook('preParsing', function (request, reply, done) { | ||
app.addHook('preParsing', (request, reply, payload, done) => { | ||
t.notOk(request.rawBody) | ||
@@ -175,5 +176,9 @@ done(null, payload) | ||
let order = 0 | ||
app.addHook('preParsing', function (req, reply, done) { | ||
app.addHook('preParsing', function (req, reply, payload, done) { | ||
t.equals(order++, 0) | ||
done() | ||
const change = new Readable() | ||
change.receivedEncodedLength = parseInt(req.headers['content-length'], 10) | ||
change.push('{"hello":"another world"}') | ||
change.push(null) | ||
done(null, change) | ||
}) | ||
@@ -184,3 +189,3 @@ | ||
app.post('/', (req, reply) => { | ||
t.deepEquals(req.body, { hello: 'world' }) | ||
t.deepEquals(req.body, { hello: 'another world' }) | ||
t.equals(JSON.stringify(req.body), req.rawBody) | ||
@@ -191,9 +196,13 @@ reply.send(req.rawBody) | ||
app.post('/preparsing', { | ||
preParsing: function (req, reply, done) { | ||
preParsing: function (req, reply, payload, done) { | ||
t.equals(order++, 1) | ||
t.notOk(req.rawBody) | ||
done() | ||
const change = new Readable() | ||
change.receivedEncodedLength = parseInt(req.headers['content-length'], 10) | ||
change.push('{"hello":"last world"}') | ||
change.push(null) | ||
done(null, change) | ||
} | ||
}, (req, reply) => { | ||
t.deepEquals(req.body, { hello: 'world' }) | ||
t.deepEquals(req.body, { hello: 'last world' }) | ||
t.equals(JSON.stringify(req.body), req.rawBody) | ||
@@ -210,3 +219,3 @@ reply.send(req.rawBody) | ||
t.equal(res.statusCode, 200) | ||
t.equals(res.payload, JSON.stringify({ hello: 'world' })) | ||
t.equals(res.payload, JSON.stringify({ hello: 'another world' })) | ||
@@ -221,3 +230,3 @@ order = 0 // reset the global order | ||
t.equal(res.statusCode, 200) | ||
t.equals(res.payload, JSON.stringify({ hello: 'world' })) | ||
t.equals(res.payload, JSON.stringify({ hello: 'last world' })) | ||
}) | ||
@@ -227,3 +236,3 @@ }) | ||
t.test('raw body run before content type parser', t => { | ||
t.test('raw body is the first body stream value', t => { | ||
t.plan(8) | ||
@@ -234,12 +243,13 @@ const app = Fastify() | ||
app.addContentTypeParser('application/json', { parseAs: 'string' }, function (req, body, done) { | ||
const json = JSON.parse(body.toUpperCase()) | ||
done(null, json) | ||
app.addHook('preParsing', function (req, reply, payload, done) { | ||
const transformation = new Transform({ | ||
writableObjectMode: true, | ||
transform (chunk, encoding, done) { | ||
this.push(chunk.toString(encoding).toUpperCase()) | ||
done() | ||
} | ||
}) | ||
done(null, payload.pipe(transformation)) | ||
}) | ||
app.addHook('preParsing', function (req, reply, done) { | ||
// cannot change payload | ||
done(null) | ||
}) | ||
app.register(rawBody, { runFirst: true }) | ||
@@ -253,4 +263,11 @@ | ||
app.post('/preparsing', { | ||
preParsing: function (req, reply, done) { | ||
done() | ||
preParsing: function (req, reply, payload, done) { | ||
const transformation = new Transform({ | ||
writableObjectMode: true, | ||
transform (chunk, encoding, done) { | ||
this.push(chunk.toString(encoding).toUpperCase()) | ||
done() | ||
} | ||
}) | ||
done(null, payload.pipe(transformation)) | ||
} | ||
@@ -283,4 +300,4 @@ }, (req, reply) => { | ||
t.test('raw body run before content type parser even with buffer', t => { | ||
t.plan(8) | ||
t.test('raw body route array', t => { | ||
t.plan(6) | ||
const app = Fastify() | ||
@@ -290,25 +307,16 @@ | ||
app.addContentTypeParser('application/json', { parseAs: 'buffer' }, function (req, body, done) { | ||
const json = JSON.parse(body.toString('utf8').toUpperCase()) | ||
done(null, json) | ||
}) | ||
app.register(rawBody) | ||
app.addHook('preParsing', function (req, reply, done) { | ||
// cannot change payload | ||
done(null) | ||
}) | ||
app.register(rawBody, { runFirst: true }) | ||
app.post('/', (req, reply) => { | ||
t.deepEquals(req.body, { HELLO: 'WORLD' }) | ||
reply.send(req.rawBody) | ||
}) | ||
app.post('/preparsing', { | ||
preParsing: function (req, reply, done) { | ||
done() | ||
} | ||
preParsing: [function (req, reply, payload, done) { | ||
t.notOk(req.rawBody) | ||
const change = new Readable() | ||
change.receivedEncodedLength = parseInt(req.headers['content-length'], 10) | ||
change.push('{"hello":"last world"}') | ||
change.push(null) | ||
done(null, change) | ||
}] | ||
}, (req, reply) => { | ||
t.deepEquals(req.body, { HELLO: 'WORLD' }) | ||
t.deepEquals(req.body, { hello: 'last world' }) | ||
t.equals(JSON.stringify(req.body), req.rawBody) | ||
reply.send(req.rawBody) | ||
@@ -319,3 +327,3 @@ }) | ||
method: 'POST', | ||
url: '/', | ||
url: '/preparsing', | ||
payload | ||
@@ -325,18 +333,8 @@ }, (err, res) => { | ||
t.equal(res.statusCode, 200) | ||
t.equals(res.payload, JSON.stringify(payload)) | ||
app.inject({ | ||
method: 'POST', | ||
url: '/preparsing', | ||
payload | ||
}, (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.equals(res.payload, JSON.stringify(payload)) | ||
}) | ||
t.equals(res.payload, JSON.stringify({ hello: 'last world' })) | ||
}) | ||
}) | ||
t.test('raw body route array - preparsing cannot change payload', t => { | ||
t.plan(6) | ||
t.test('preparsing run first', t => { | ||
t.plan(5) | ||
const app = Fastify() | ||
@@ -346,12 +344,18 @@ | ||
app.register(rawBody) | ||
app.register(rawBody, { runFirst: true }) | ||
app.post('/preparsing', { | ||
preParsing: [function (req, reply, done) { | ||
t.notOk(req.rawBody) | ||
done() | ||
preParsing: [function (req, reply, payload, done) { | ||
const transformation = new Transform({ | ||
writableObjectMode: true, | ||
transform (chunk, encoding, done) { | ||
this.push(chunk.toString(encoding).toUpperCase()) | ||
done() | ||
} | ||
}) | ||
done(null, payload.pipe(transformation)) | ||
}] | ||
}, (req, reply) => { | ||
t.deepEquals(req.body, { hello: 'world' }) | ||
t.equals(JSON.stringify(req.body), req.rawBody) | ||
t.deepEquals(req.body, { HELLO: 'WORLD' }) | ||
t.equals(req.rawBody, JSON.stringify(payload)) | ||
reply.send(req.rawBody) | ||
@@ -367,3 +371,3 @@ }) | ||
t.equal(res.statusCode, 200) | ||
t.equals(res.payload, JSON.stringify({ hello: 'world' })) | ||
t.equals(res.payload, JSON.stringify(payload)) | ||
}) | ||
@@ -370,0 +374,0 @@ }) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17649
475
68
+ Addedfastify-plugin@2.3.4(transitive)
+ Addedsemver@7.6.3(transitive)
- Removedfastify-plugin@1.6.1(transitive)
- Removedsemver@6.3.1(transitive)
Updatedfastify-plugin@^2.0.0