koa-ws-cookie-helper
koa.js + ws.js helper for accessing cookies from a WebSocket connection
Installation
$ npm install koa-ws-cookie-helper
Example
var koa = WebSocketServer = Server cookieHelper = app = ; var port = 3000;appkeys = 'secret'; appserver = app; var wss = server: appserver;wss;
How does it work?
Browsers put any cookies from the same host into initial WebSocket handshake headers. This lets typical HTTP-session mechanisms to work with WebSockets. koa.js makes use of Cookies module to handle cookie management. Signed cookies are signed and verified using Keygrip. This module combines methods used by them to parse cookies in ws.upgradeReq.headers.cookie
.
API
There is only a single public function:
get(ws, name, [keys])
Returns value of the cookie named name
, using keyset keys
. Cookie is searched in 'Cookie'
entry of handshake headers. If no cookie could be found or if the signed cookie does not match with any key in the keyset, returns undefined
.
License
MIT