-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgalette.js
145 lines (119 loc) · 3.55 KB
/
galette.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (C) 2014-2019 12 Quarters Consulting
// (C) 2012 ...Max... & Adstream Holdings
// All rights reserved.
// Redistribution and use are permitted under the modified BSD license
// available at https://github.com/MaxMotovilov/adstream-js-frameworks/wiki/License
var connect = require( 'connect' ),
SessionState = require( './lib/session' ),
merge = require( 'utils-merge' ),
promise = require( 'node-promise' );
function useDefaults( options ) {
if( (!options.cipher) !== (!options.decipher) )
throw Error( '"cipher" and "decipher" should be specified together' );
if( !options.cipher )
merge( options, require( './lib/cipher' ) );
if( !options.keyManager )
options.keyManager = require( './lib/key_manager' )( options.secret );
if( !('expireAfter' in options) ) {
options.expireAfter = options.cookie && options.cookie.maxAge;
if( options.expireAfter ) options.expireAfter *= 1000;
else options.expireAfter = null;
}
if( options.expireAfter && !('timestamp' in options) )
options.timestamp = true;
else if( !options.expireAfter )
options.timestamp = false;
if( !('refreshAfter' in options) && options.expireAfter )
options.refreshAfter = options.expireAfter/2;
if( !options.name )
options.name = 'session';
if( !options.cookie )
options.cookie = { httpOnly: true, path: '/' };
else {
if( options.cookie.maxAge )
delete options.cookie.maxAge;
if( !options.cookie.path )
options.cookie.path = '/';
}
}
var proxies = [ 'writeHead', 'end', 'write' ];
function copyProps( to, from, props ) {
props.forEach( function(p){ to[p] = from[p]; } );
}
function bind( self, fn, args ) {
return function(){
return fn.apply( self, args );
}
}
function patchServerResponse( res, async_update ) {
var orig = {},
queue = [],
started = false,
paused = false,
last_result;
copyProps( orig, res, proxies );
function commit() {
return started ||
(started = promise.when(
async_update( res ),
function() {
copyProps( res, orig, proxies );
queue.forEach( function(c){
last_result = c();
} );
if( paused ) res.emit( 'drain' );
return true;
},
function( err ) {
copyProps( res, orig, proxies );
res.writeHead( 500, {
'Content-Type': 'text/plain'
} );
res.end(
'galette failed to update session cookie(s):\n' +
err.toString()
);
return true;
}
));
}
res.writeHead = function( status ) {
this.statusCode = status;
queue.push( bind( this, orig.writeHead, arguments ) );
commit();
}
res.end = function() {
queue.push( bind( this, orig.end, arguments ) );
return !( commit().then && (paused = true) ) && last_result;
}
res.write = function() {
queue.push( bind( this, orig.write, arguments ) );
return !( commit().then && (paused = true) ) && last_result;
}
}
module.exports = function( options ) {
useDefaults( options );
var name = options.name,
splitter = new RegExp( '^' + name.replace( /[.$]/g, '\\$&' ) + '\\.(.*)$', 'i' );
return function galette( req, res, next ) {
// Protect against double usage
if( req[name] ) return next();
var state = new SessionState( options, req );
req[name] = state.makeSession();
promise.when(
promise.all(
Object.keys( req.cookies )
.map( function( cname ) {
var spl;
if( spl = splitter.exec( cname ) )
return state.initialize.call( req[name], spl[1], req.cookies[cname] );
} )
),
(function() {
next();
this.resume();
}).bind( connect.utils.pause( req ) )
);
patchServerResponse( res, state.commit.bind( req[name] ) );
}
}