Comparing version 0.9.0 to 0.10.0
@@ -15,2 +15,3 @@ /* eslint-disable no-useless-return */ | ||
this.sent = false | ||
this._serializer = null | ||
} | ||
@@ -58,3 +59,4 @@ | ||
const str = serialize(this.handle, payload) | ||
// Here we are assuming that the custom serializer is a json serializer | ||
const str = this._serializer ? this._serializer(payload) : serialize(this.handle, payload) | ||
if (!this.res.getHeader('Content-Length')) { | ||
@@ -67,2 +69,8 @@ this.res.setHeader('Content-Length', Buffer.byteLength(str)) | ||
// All the code below must have a 'content-type' setted | ||
if (this._serializer) { | ||
setImmediate(wrapReplyEnd, this, this._serializer(payload)) | ||
return | ||
} | ||
setImmediate(wrapReplyEnd, this, payload) | ||
@@ -82,2 +90,7 @@ return | ||
Reply.prototype.serializer = function (fn) { | ||
this._serializer = fn | ||
return this | ||
} | ||
function wrapPumpCallback (reply) { | ||
@@ -84,0 +97,0 @@ return function pumpCallback (err) { |
@@ -8,4 +8,5 @@ 'use strict' | ||
this.query = query | ||
this.log = req.log | ||
} | ||
module.exports = Request |
{ | ||
"name": "fastify", | ||
"version": "0.9.0", | ||
"version": "0.10.0", | ||
"description": "Fast and low overhead web framework, for Node.js", | ||
@@ -40,15 +40,15 @@ "main": "fastify.js", | ||
"dns-prefetch-control": "^0.1.0", | ||
"express": "^4.15.0", | ||
"express": "^4.15.2", | ||
"frameguard": "^3.0.0", | ||
"hapi": "^16.1.0", | ||
"helmet": "^3.4.1", | ||
"helmet": "^3.5.0", | ||
"hide-powered-by": "^1.0.0", | ||
"hsts": "^2.0.0", | ||
"ienoopen": "^1.0.0", | ||
"koa": "^2.0.1", | ||
"koa": "^2.1.0", | ||
"pre-commit": "^1.2.2", | ||
"request": "^2.80.0", | ||
"request": "^2.81.0", | ||
"snazzy": "^6.0.0", | ||
"split2": "^2.1.1", | ||
"standard": "^9.0.0", | ||
"standard": "^9.0.1", | ||
"take-five": "^1.3.3", | ||
@@ -63,4 +63,4 @@ "tap": "^10.3.0", | ||
"body": "^5.1.0", | ||
"fast-json-stringify": "^0.10.3", | ||
"fast-safe-stringify": "^1.1.6", | ||
"fast-json-stringify": "^0.10.4", | ||
"fast-safe-stringify": "^1.1.11", | ||
"fastseries": "^1.7.2", | ||
@@ -71,4 +71,4 @@ "middie": "^0.1.1", | ||
"pump": "^1.0.2", | ||
"wayfarer": "^6.4.1" | ||
"wayfarer": "^6.5.0" | ||
} | ||
} |
@@ -235,2 +235,3 @@ <p align="center"> | ||
* `req` - the incoming HTTP request from Node core | ||
* `log` - the logger instance of the incoming request | ||
@@ -244,2 +245,3 @@ <a name="reply"></a> | ||
* `.header(name, value)` - Sets the headers. | ||
* `.serializer(function)` - Sets a custom serializer for the payload. | ||
@@ -252,4 +254,13 @@ Example: | ||
.header('Content-Type', 'application/json') | ||
.send({ hello 'world' }) | ||
.send({ hello: 'world' }) | ||
}) | ||
// If you need a custom serializer | ||
fastify.get('/custom-serializer', schema, function (request, reply) { | ||
reply | ||
.code(200) | ||
.header('Content-Type', 'application/x-protobuf') | ||
.serializer(protoBuf.serialize) | ||
.send({ hello: 'world' }) | ||
}) | ||
``` | ||
@@ -409,3 +420,3 @@ | ||
<a name="hooks"></a> | ||
### fastify.addHook({ hook: callback }) | ||
### fastify.addHook('hookName', callback) | ||
Use to add one or more hooks inside the fastify lifecycle. | ||
@@ -445,2 +456,7 @@ Currently supported hooks (in order of execution): | ||
}) | ||
fastify.get('/', schema, function (req, reply) { | ||
req.log.info('Some info about the current request') | ||
reply.send({ hello: 'world' }) | ||
}) | ||
``` | ||
@@ -447,0 +463,0 @@ |
@@ -23,3 +23,3 @@ 'use strict' | ||
fastify.listen(3000, err => { | ||
fastify.listen(0, err => { | ||
t.error(err) | ||
@@ -26,0 +26,0 @@ |
@@ -7,2 +7,3 @@ 'use strict' | ||
const fastify = require('..')() | ||
const safeStringify = require('fast-safe-stringify') | ||
@@ -20,2 +21,13 @@ const schema = { | ||
const numberSchema = { | ||
out: { | ||
type: 'object', | ||
properties: { | ||
hello: { | ||
type: 'number' | ||
} | ||
} | ||
} | ||
} | ||
const querySchema = { | ||
@@ -94,2 +106,27 @@ querystring: { | ||
test('wrong object for schema - get', t => { | ||
t.plan(1) | ||
try { | ||
fastify.get('/wrong-object-for-schema', numberSchema, function (req, reply) { | ||
// will send { hello: null } | ||
reply.code(200).send({ hello: 'world' }) | ||
}) | ||
t.pass() | ||
} catch (e) { | ||
t.fail() | ||
} | ||
}) | ||
test('custom serializer - get', t => { | ||
t.plan(1) | ||
try { | ||
fastify.get('/custom-serializer', numberSchema, function (req, reply) { | ||
reply.code(200).serializer(safeStringify).send({ hello: 'world' }) | ||
}) | ||
t.pass() | ||
} catch (e) { | ||
t.fail() | ||
} | ||
}) | ||
fastify.listen(0, err => { | ||
@@ -186,2 +223,28 @@ t.error(err) | ||
}) | ||
test('shorthand - request get missing schema', t => { | ||
t.plan(4) | ||
request({ | ||
method: 'GET', | ||
uri: 'http://localhost:' + fastify.server.address().port + '/wrong-object-for-schema' | ||
}, (err, response, body) => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 200) | ||
t.strictEqual(response.headers['content-length'], '' + body.length) | ||
t.deepEqual(JSON.parse(body), { hello: null }) | ||
}) | ||
}) | ||
test('shorthand - custom serializer', t => { | ||
t.plan(4) | ||
request({ | ||
method: 'GET', | ||
uri: 'http://localhost:' + fastify.server.address().port + '/custom-serializer' | ||
}, (err, response, body) => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 200) | ||
t.strictEqual(response.headers['content-length'], '' + body.length) | ||
t.deepEqual(JSON.parse(body), { hello: 'world' }) | ||
}) | ||
}) | ||
}) |
@@ -30,4 +30,4 @@ 'use strict' | ||
test('reply.header and reply.code should return an instance of Reply', t => { | ||
t.plan(2) | ||
test('reply.header, reply.code and reply-serializer should return an instance of Reply', t => { | ||
t.plan(3) | ||
const request = {} | ||
@@ -37,2 +37,3 @@ const response = { setHeader: () => {} } | ||
t.type(reply.code(1), Reply) | ||
t.type(reply.serializer(() => {}), Reply) | ||
t.type(reply.header('hello', 'world'), Reply) | ||
@@ -55,2 +56,10 @@ }) | ||
test('reply.serializer should set a custom serializer', t => { | ||
t.plan(2) | ||
const reply = new Reply(null, null, null) | ||
t.equal(reply._serializer, null) | ||
reply.serializer('serializer') | ||
t.equal(reply._serializer, 'serializer') | ||
}) | ||
test('Reply can set code and header of a response', t => { | ||
@@ -57,0 +66,0 @@ t.plan(1) |
@@ -17,9 +17,10 @@ /* eslint-disable no-useless-return */ | ||
test('Request object', t => { | ||
t.plan(5) | ||
const req = new Request('params', 'req', 'body', 'query') | ||
t.plan(6) | ||
const req = new Request('params', { log: null }, 'body', 'query') | ||
t.type(req, Request) | ||
t.equal(req.params, 'params') | ||
t.equal(req.req, 'req') | ||
t.deepEqual(req.req, { log: null }) | ||
t.equal(req.body, 'body') | ||
t.equal(req.query, 'query') | ||
t.equal(req.log, null) | ||
}) | ||
@@ -85,3 +86,3 @@ | ||
buildSchema(handle) | ||
internals.handler(hooks, handle, null, null, res, null, null) | ||
internals.handler(hooks, handle, null, { log: null }, res, null, null) | ||
}) | ||
@@ -88,0 +89,0 @@ |
@@ -24,3 +24,3 @@ 'use strict' | ||
fastify.get('/', function (req, reply) { | ||
t.ok(req.req.log) | ||
t.ok(req.log) | ||
reply.send(null, 200, { hello: 'world' }) | ||
@@ -27,0 +27,0 @@ }) |
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
109174
58
2796
495
Updatedfast-json-stringify@^0.10.4
Updatedfast-safe-stringify@^1.1.11
Updatedwayfarer@^6.5.0