Socket
Socket
Sign inDemoInstall

fastify

Package Overview
Dependencies
7
Maintainers
2
Versions
286
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.2.0 to 0.3.0

examples/example.js

122

fastify.js
'use strict'
const wayfarer = require('wayfarer')
const urlUtil = require('url')
const stripUrl = require('pathname-match')
const jsonParser = require('body/json')
const pluginLoader = require('boot-in-the-arse')
const supportedMethods = ['GET', 'POST', 'PUT']
const validation = require('./lib/validation')
const buildSchema = validation.buildSchema
const validateSchema = validation.validateSchema
const serialize = validation.serialize
const supportedMethods = ['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']
const buildSchema = require('./lib/validation').build
const buildNode = require('./lib/tier-node')

@@ -22,5 +18,9 @@ function build () {

// shorthand methods
fastify.get = get
fastify.post = post
fastify.put = put
fastify.delete = _delete
fastify.get = _get
fastify.head = _head
fastify.patch = _patch
fastify.post = _post
fastify.put = _put
fastify.options = _options
// extended route

@@ -37,14 +37,31 @@ fastify.route = route

function get (url, schema, handler) {
// Shorthand methods
function _delete (url, schema, handler) {
return _route('DELETE', url, schema, handler)
}
function _get (url, schema, handler) {
return _route('GET', url, schema, handler)
}
function post (url, schema, handler) {
function _head (url, schema, handler) {
return _route('HEAD', url, schema, handler)
}
function _patch (url, schema, handler) {
return _route('PATCH', url, schema, handler)
}
function _post (url, schema, handler) {
return _route('POST', url, schema, handler)
}
function put (url, schema, handler) {
function _put (url, schema, handler) {
return _route('PUT', url, schema, handler)
}
function _options (url, schema, handler) {
return _route('OPTIONS', url, schema, handler)
}
function _route (method, url, schema, handler) {

@@ -58,2 +75,3 @@ if (!handler && typeof schema === 'function') {

// Route management
function route (opts) {

@@ -77,77 +95,10 @@ if (supportedMethods.indexOf(opts.method) === -1) {

} else {
const node = createNode(opts.url)
const node = buildNode(opts.url, router)
node[opts.method] = opts
map.set(opts.url, node)
}
// chainable api
return fastify
}
function bodyParsed (handle, params, req, res) {
function parsed (err, body) {
if (err) {
res.statusCode = 422
res.end()
return
}
handleNode(handle, params, req, res, body, null)
}
return parsed
}
function createNode (url) {
const node = {}
router.on(url, function handle (params, req, res) {
const handle = node[req.method]
if (handle && req.method === 'GET') {
handleNode(handle, params, req, res, null, urlUtil.parse(req.url, true).query)
} else if (handle && (req.method === 'POST' || req.method === 'PUT')) {
if (req.headers['content-type'] === 'application/json') {
jsonParser(req, bodyParsed(handle, params, req, res))
} else {
res.statusCode = 415
res.end()
}
} else {
res.statusCode = 404
res.end()
}
})
return node
}
function handleNode (handle, params, req, res, body, query) {
if (!handle) {
res.statusCode = 404
res.end()
}
const valid = validateSchema(handle, params, body, query)
if (valid !== true) {
res.statusCode = 400
res.end(valid)
return
}
const request = new Request(params, req, body, query)
handle.handler(request, reply)
function reply (err, statusCode, data) {
if (err) {
res.statusCode = 500
res.end()
}
if (!data) {
data = statusCode
statusCode = 200
}
res.statusCode = statusCode
res.end(serialize(handle, data))
}
}
function defaultRoute (params, req, res) {

@@ -157,11 +108,4 @@ res.statusCode = 404

}
function Request (params, req, body, query) {
this.params = params
this.req = req
this.body = body
this.query = query
}
}
module.exports = build

@@ -16,3 +16,3 @@ 'use strict'

function buildSchema (opts) {
function build (opts) {
if (!opts.schema) {

@@ -42,3 +42,3 @@ opts[outputSchema] = fastSafeStringify

function validateSchema (handle, params, body, query) {
function validate (handle, params, body, query) {
if (handle[paramsSchema] && !handle[paramsSchema](params)) {

@@ -62,2 +62,2 @@ return inputSchemaError(handle[paramsSchema].errors)

module.exports = { buildSchema, validateSchema, serialize }
module.exports = { build, validate, serialize }
{
"name": "fastify",
"version": "0.2.0",
"version": "0.3.0",
"description": "Fast and low overhead web framework, for Node.js",

@@ -5,0 +5,0 @@ "main": "fastify.js",

@@ -116,3 +116,3 @@ # Fastify  [![Build Status](https://travis-ci.org/mcollina/fastify.svg)](https://travis-ci.org/mcollina/fastify) [![Coverage Status](https://coveralls.io/repos/github/mcollina/fastify/badge.svg?branch=master)](https://coveralls.io/github/mcollina/fastify?branch=master)

* `schema`: an object containing the schemas for the request and response. They need to be in
[JSON Schema][jsonschema] format:
[JSON Schema](http://json-schema.org/) format:

@@ -126,6 +126,5 @@ * `payload`: validates the body of the request if it is a POST or a

[fast-json-stringify][fast-json-stringify].
* `handler(request, repy(err, statusCode, object)`: the function that will handle this request.
The `request` parameter is defined in [Request](#request).
`object` inside the request is a JavaScript object that will be
JSONified, possibly using the schema defined `options.schema.out`.
* `handler(request, reply(err, statusCode, object)`: the function that will handle this request.
`request` is defined in [Request](#request).
`reply` is the function that handle the response object, defined in [Reply](#reply).

@@ -144,2 +143,11 @@ For POST and PUT, the incoming request body will be parsed.

<a name="reply"></a>
#### Reply
A function that accepts the following parameters:
* `error` - error object *(mandatory)*
* `statusCode` - the http status code *(optional, default to 200)*
* `object` - JavaScript object that will be JSONified *(mandatory)*
<a name="get"></a>

@@ -146,0 +154,0 @@ ### fastify.get(path, [schema], handler)

@@ -96,3 +96,7 @@ 'use strict'

t.error(err)
t.strictEqual(response.statusCode, 415)
if (upMethod === 'OPTIONS') {
t.strictEqual(response.statusCode, 200)
} else {
t.strictEqual(response.statusCode, 415)
}
})

@@ -99,0 +103,0 @@ })

@@ -44,15 +44,26 @@ 'use strict'

test(`${upMethod} - correctly replies`, t => {
t.plan(3)
request({
method: upMethod,
uri: 'http://localhost:' + server.address().port,
body: {
hello: 42
},
json: true
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEqual(body, { hello: 42 })
})
if (upMethod === 'HEAD') {
t.plan(2)
request({
method: upMethod,
uri: 'http://localhost:' + server.address().port
}, (err, response) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
})
} else {
t.plan(3)
request({
method: upMethod,
uri: 'http://localhost:' + server.address().port,
body: {
hello: 42
},
json: true
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEqual(body, { hello: 42 })
})
}
})

@@ -59,0 +70,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc