-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
161 lines (123 loc) · 3.5 KB
/
index.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var cookie = require('cookie');
var requestHandle = 1;
var _rawCookies = {};
var _res = {};
var _generalHandle = -1;
//true is used for debugging
var _doNotCleanUp = false;
function checkIfItExists(handle){
if(!_rawCookies[handle]) {
throw "The handle '"+handle+"' doesn't exist, the request is probably over";
}
}
function load(name, opt) {
var cookies = {};
opt = opt || {};
//Back compatibility and shortcut
if(typeof opt === "boolean"){
opt = {doNotParse : opt}
}
opt.handle = opt.handle || _generalHandle;
if (typeof document !== 'undefined') {
cookies = cookie.parse(document.cookie);
}
var cookieVal = (cookies && cookies[name]) || (_rawCookies[opt.handle] && _rawCookies[opt.handle][name]);
if (!opt.doNotParse) {
try {
cookieVal = JSON.parse(cookieVal);
} catch(e) {
// Not serialized object
}
}
return cookieVal;
}
function save(name, val, opt) {
opt = opt || {};
opt.handle = opt.handle || _generalHandle;
if(!_rawCookies[opt.handle]){
_rawCookies[opt.handle] = {};
}
_rawCookies[opt.handle][name] = val;
// allow you to work with cookies as objects.
if (typeof val === 'object') {
_rawCookies[opt.handle][name] = JSON.stringify(val);
}
// Cookies only work in the browser
if (typeof document !== 'undefined') {
document.cookie = cookie.serialize(name, _rawCookies[opt.handle][name], opt);
}
if (_res[opt.handle] && _res[opt.handle].cookie) {
_res[opt.handle].cookie(name, _rawCookies[opt.handle][name], opt);
}
}
function remove(name, opt) {
opt = opt || {};
if (typeof opt === 'string') {
// Will be deprecated in future versions
opt = { path: opt };
}
if (!opt.path){
opt.path = '/';
}
opt.handle = opt.handle || _generalHandle;
if(!_rawCookies[opt.handle]){
_rawCookies[opt.handle] = {};
}
if(_rawCookies[opt.handle][name])
delete _rawCookies[opt.handle][name];
opt.expires = new Date(1970, 1, 1, 0, 0, 1);
if (typeof document !== 'undefined') {
document.cookie = cookie.serialize(name, '', opt);
}
if (_res[opt.handle] && _res[opt.handle].clearCookie) {
_res[opt.handle].cookie(name, '', opt);
}
}
function setRawCookie(rawCookie, opt) {
opt = opt || {};
opt.handle = opt.handle || _generalHandle;
if (rawCookie) {
_rawCookies[opt.handle] = cookie.parse(rawCookie);
} else {
_rawCookies[opt.handle] = {};
}
}
function _cleanup(handle){
return function() {
if(_doNotCleanUp) return;
if(_rawCookies[handle]) delete _rawCookies[handle];
if(_res[handle]) delete _res[handle];
}
};
function plugToRequest(req, res, opt) {
opt = opt || {};
var _requestHandle = opt.useHandles ? opt.handle || requestHandle++ : _generalHandle;
if (req.cookie) {
_rawCookies[_requestHandle] = req.cookie;
} else if (req.headers && req.headers.cookie) {
setRawCookie(req.headers.cookie, {handle : _requestHandle});
} else {
_rawCookies[_requestHandle] = {};
}
if(res){
_res[_requestHandle] = res;
res.on("finish", _cleanup(_requestHandle));
res.on("error", _cleanup(_requestHandle));
}
return _requestHandle;
}
var reactCookie = {
//The generic handle can be used on the browser
//where there is not problem.
SYNC_REQ_ID : _generalHandle,
BROWSER_REQ_ID : _generalHandle,
load: load,
save: save,
remove: remove,
setRawCookie: setRawCookie,
plugToRequest: plugToRequest
};
if (typeof window !== 'undefined') {
window['reactCookie'] = reactCookie;
}
module.exports = reactCookie;