Skip to content

Commit

Permalink
🤘
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed Sep 10, 2017
0 parents commit c868c89
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true

# [*.md]
# trim_trailing_whitespace = false
47 changes: 47 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

# 0x
.__browserify_string_empty.js
profile-*

# tap --cov
.nyc_output/

# JetBrains IntelliJ IDEA
.idea/
*.iml

# VS Code
.vscode/
55 changes: 55 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# fastify-cookie

A plugin for [Fastify](http://fastify.io/) that adds support for reading and
setting cookies.

This plugin works via Fastify's `preHandler` hook. You should register it
prior to any other `preHandler` hooks that will depend upon this plugin's
actions.

## Example

```js
const fastify = require('fastify')()

fastify.register(require('fastify-cookie'), (err) => {
if (err) throw err
})

fastify.get('/', (req, reply) => {
const aCookieValue = req.cookies.cookieName
reply
.setCookie('foo', 'foo', {
domain: 'example.com',
path: '/'
})
.send({hello: 'world'})
})
```

## API

### Parsing

Cookies are parsed in the `preHandler` Fastify hook and attached to the request
as an object named `cookies`. Thus, if a request contains the header
`Cookie: foo=foo` then, within your handler, `req.cookies.foo` would equal
`'foo'`.

### Sending

The method `setCookie(name, value, options)` is added to the `reply` object
via the Fastify `preHandler` hook. Thus, in a request handler,
`reply.setCookie('foo', 'foo', {path: '/'})` will set a cookie named `foo`
with a value of `'foo'` on the cookie path `/`.

+ `name`: a string name for the cookie to be set
+ `value`: a string value for the cookie
+ `options`: an options object as described in the [cookie serialize][cs]
documentation

[cs]: https://www.npmjs.com/package/cookie#options-1

## License

[MIT License](http://jsumners.mit-license.org/)
41 changes: 41 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "fastify-cookie",
"version": "1.0.0",
"description": "Plugin for fastify to add support for cookies",
"main": "plugin.js",
"scripts": {
"test": "tap 'test/**/*.test.js'",
"lint": "standard | snazzy"
},
"precommit": [
"lint",
"test"
],
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/jsumners/fastify-cookie.git"
},
"keywords": [
"fastify",
"cookie"
],
"author": "James Sumners <james.sumners@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/jsumners/fastify-cookie/issues"
},
"homepage": "https://github.com/jsumners/fastify-cookie#readme",
"devDependencies": {
"abstract-logging": "^1.0.0",
"fastify": "^0.26.2",
"pre-commit": "^1.2.2",
"request": "^2.81.0",
"snazzy": "^7.0.0",
"standard": "^10.0.3",
"tap": "^10.7.2"
},
"dependencies": {
"cookie": "^0.3.1",
"fastify-plugin": "^0.1.1"
}
}
27 changes: 27 additions & 0 deletions plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

const fp = require('fastify-plugin')
const cookie = require('cookie')

function plugin (fastify, options, next) {
fastify.addHook('preHandler', (fastifyReq, fastifyRes, done) => {
if (fastifyReq.cookies) done()
if (!fastifyReq.req.headers) fastifyReq.req.headers = {}

const cookieHeader = fastifyReq.req.headers.cookie
const cookies = (cookieHeader) ? cookie.parse(cookieHeader) : []
fastifyReq.cookies = cookies

fastifyRes.setCookie = function (name, value, options) {
const seriaized = cookie.serialize(name, value, options || {})
this.header('Set-Cookie', seriaized)
return this
}

done()
})

next()
}

module.exports = fp(plugin, '>=0.15.0')
65 changes: 65 additions & 0 deletions test/integration.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

'use strict'

const tap = require('tap')
const test = tap.test
const fastify = require('fastify')({logger: require('abstract-logging')})
const request = require('request')
const plugin = require('../')

fastify.register(plugin, (err) => {
if (err) throw err
})

fastify.listen(0, (err) => {
if (err) tap.error(err)
fastify.server.unref()

const reqOpts = {
method: 'GET',
baseUrl: 'http://localhost:' + fastify.server.address().port
}
const req = request.defaults(reqOpts)

test('cookies get set correctly', (t) => {
t.plan(7)

fastify.get('/test1', (req, reply) => {
reply
.setCookie('foo', 'foo', {path: '/'})
.send({hello: 'world'})
})

const jar = request.jar()
req({uri: '/test1', jar}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEqual(JSON.parse(body), {hello: 'world'})

const cookies = jar.getCookies(reqOpts.baseUrl + '/test1')
t.is(cookies.length, 1)
t.is(cookies[0].key, 'foo')
t.is(cookies[0].value, 'foo')
t.is(cookies[0].path, '/')
})
})

test('parses incoming cookies', (t) => {
t.plan(6)
fastify.get('/test2', (req, reply) => {
t.ok(req.cookies)
t.ok(req.cookies.bar)
t.is(req.cookies.bar, 'bar')
reply.send({hello: 'world'})
})

const headers = {
cookie: 'bar=bar'
}
req({uri: '/test2', headers}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEqual(JSON.parse(body), {hello: 'world'})
})
})
})

0 comments on commit c868c89

Please sign in to comment.