This repository has been archived by the owner on May 25, 2022. It is now read-only.
forked from veliovgroup/meteor-cookies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookies.coffee
executable file
·336 lines (304 loc) · 10.4 KB
/
cookies.coffee
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
###
@url https://github.com/jshttp/cookie/blob/master/index.js
@name cookie
@author jshttp
@license
(The MIT License)
Copyright (c) 2012-2014 Roman Shtylman <[email protected]>
Copyright (c) 2015 Douglas Christopher Wilson <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
###
decode = decodeURIComponent
encode = encodeURIComponent
pairSplitRegExp = /; */
###
RegExp to match field-content in RFC 7230 sec 3.2
field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
field-vchar = VCHAR / obs-text
obs-text = %x80-FF
###
fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/
###
@param {String} str
@param {Object} [options]
@return {Object}
@description
Parse a cookie header.
Parse the given cookie header string into an object
The object has the various cookies as keys(names) => values
@private
###
parse = (str, options) ->
if typeof str != 'string'
throw new TypeError('argument str must be a string')
obj = {}
opt = options or {}
pairs = str.split(pairSplitRegExp)
dec = opt.decode or decode
pairs.forEach (pair) ->
eq_idx = pair.indexOf('=')
# skip things that don't look like key=value
if eq_idx < 0
return
key = pair.substr(0, eq_idx).trim()
val = pair.substr(++eq_idx, pair.length).trim()
# quoted values
if '"' == val[0]
val = val.slice(1, -1)
# only assign once
if undefined == obj[key]
obj[key] = tryDecode(val, dec)
return
obj
###
@param {String} name
@param {String} val
@param {Object} [options]
@return {String}
@description
Serialize data into a cookie header.
Serialize the a name value pair into a cookie string suitable for
http headers. An optional options object specified cookie parameters.
serialize('foo', 'bar', { httpOnly: true })
=> "foo=bar; httpOnly"
@private
###
serialize = (name, val, options) ->
opt = options or {}
enc = opt.encode or encode
if !fieldContentRegExp.test(name)
throw new TypeError('argument name is invalid')
if val
value = enc(val)
if value and !fieldContentRegExp.test(value)
throw new TypeError('argument val is invalid')
else
value = ''
pairs = [ name + '=' + value ]
if opt.maxAge
maxAge = opt.maxAge - 0
if isNaN(maxAge)
throw new Error('maxAge should be a Number')
pairs.push 'Max-Age=' + maxAge
if opt.domain
if !fieldContentRegExp.test(opt.domain)
throw new TypeError('option domain is invalid')
pairs.push 'Domain=' + opt.domain
else
pairs.push 'Domain='
if opt.path
if !fieldContentRegExp.test(opt.path)
throw new TypeError('option path is invalid')
pairs.push 'Path=' + opt.path
else
pairs.push 'Path=/'
opt.expires = opt.expires or opt.expire
if opt.expires
if opt.expires is Infinity
pair.push 'Expires=Fri, 31 Dec 9999 23:59:59 GMT'
else if opt.expires instanceof Date
pairs.push 'Expires=' + opt.expires.toUTCString()
else if _.isNumber opt.expires
pairs.push 'Expires=' + (new Date(opt.expires)).toUTCString()
if opt.httpOnly
pairs.push 'HttpOnly'
if opt.secure
pairs.push 'Secure'
if opt.firstPartyOnly
pairs.push 'First-Party-Only'
pairs.join '; '
###
@param {String} str
@param {Function} decode
@description Try decoding a string using a decoding function.
@private
###
tryDecode = (str, decode) ->
try
return decode(str)
catch e
return str
return
class __cookies
constructor: (_cookies, @collection, @TTL, @runOnServer, @response) ->
if _.isObject _cookies
@cookies = _cookies
else
@cookies = parse _cookies
###
@function
@namespace __cookies
@name get
@param {String} key - The name of the cookie to read
@param {String} _tmp - Unparsed string instead of user's cookies
@description Read a cookie. If the cookie doesn't exist a null value will be returned.
@returns {String|null}
###
get: (key, _tmp) ->
_cs = if _tmp then parse _tmp else @cookies
if not key or not _cs
null
else
if _cs?[key] then _cs[key] else null
###
@function
@namespace __cookies
@name set
@param {String} key - The name of the cookie to create/overwrite
@param {String} value - The value of the cookie
@param {Number} opts.expires - [Optional] The max-age in seconds (e.g. 31536e3
for a year, Infinity for a never-expires cookie), or the expires date in
GMTString format or as Date object; if not specified the cookie will
expire at the end of session (number – finite or Infinity – string, Date object or null).
@param {String} opts.path - [Optional] The path from where the cookie will be
readable. E.g., "/", "/mydir"; if not specified, defaults to the current
path of the current document location (string or null). The path must be
absolute (see RFC 2965). For more information on how to use relative paths
in this argument, see: https://developer.mozilla.org/en-US/docs/Web/API/document.cookie#Using_relative_URLs_in_the_path_parameter
@param {String} opts.domain - [Optional] The domain from where the cookie will
be readable. E.g., "example.com", ".example.com" (includes all subdomains)
or "subdomain.example.com"; if not specified, defaults to the host portion
of the current document location (string or null).
@param {Boolean} opts.secure - [Optional] The cookie will be transmitted only
over secure protocol as https (boolean or null).
@description Create/overwrite a cookie.
@returns {Boolean}
###
set: (key, value, opts = {}) ->
if key and value
opts.expires ?= new Date (+new Date) + @TTL
opts.path ?= '/'
opts.domain ?= ''
opts.secure ?= ''
newCookie = serialize key, value, opts
@cookies[key] = value
if Meteor.isClient
document.cookie = newCookie
else
@response.setHeader 'Set-Cookie', newCookie
true
else
false
###
@function
@namespace __cookies
@name remove
@param {String} key - The name of the cookie to create/overwrite
@param {String} path - [Optional] The path from where the cookie will be
readable. E.g., "/", "/mydir"; if not specified, defaults to the current
path of the current document location (string or null). The path must be
absolute (see RFC 2965). For more information on how to use relative paths
in this argument, see: https://developer.mozilla.org/en-US/docs/Web/API/document.cookie#Using_relative_URLs_in_the_path_parameter
@param {String} domain - [Optional] The domain from where the cookie will
be readable. E.g., "example.com", ".example.com" (includes all subdomains)
or "subdomain.example.com"; if not specified, defaults to the host portion
of the current document location (string or null).
@description Remove a cookie(s).
@returns {Boolean}
###
remove: (key, path = '/', domain = '') ->
if key
newCookie = serialize key, '', {domain, path, expires: new Date(0)}
return false unless @has(key)
delete @cookies[key]
if Meteor.isClient
document.cookie = newCookie
else
@response.setHeader 'Set-Cookie', newCookie
true
else if @keys().length > 0 and @keys()[0] isnt ""
@remove k for k in @keys()
true
else
false
###
@function
@namespace __cookies
@name has
@param {String} key - The name of the cookie to create/overwrite
@param {String} _tmp - Unparsed string instead of user's cookies
@description Check whether a cookie exists in the current position.
@returns {Boolean}
###
has: (key, _tmp) ->
_cs = if _tmp then parse _tmp else @cookies
if not key or not _cs
return false
else
!!_cs?[key]
###
@function
@namespace __cookies
@name keys
@description Returns an array of all readable cookies from this location.
@returns {[String]}
###
keys: -> if @cookies then Object.keys @cookies else []
###
@function
@namespace __cookies
@name send
@description Send all cookies over XHR to server.
@returns {void}
###
send: ->
if @runOnServer
HTTP.get (__meteor_runtime_config__.ROOT_URL_PATH_PREFIX or '') + '/___cookie___/set', () -> return
else
throw new Meteor.Error '400', 'Can\'t send cookies on server when `runOnServer` is false.'
__middlewareHandler = (req, res, self) ->
if self.runOnServer
if req.headers?.cookie
_cookies = parse req.headers.cookie
else
_cookies = {}
return new __cookies _cookies, self.collection, self.TTL, self.runOnServer, res
else
throw new Meteor.Error '400', 'Can\'t use middleware when `runOnServer` is false.'
class Cookies extends __cookies
constructor: (opts = {}) ->
{@runOnServer, @handler, @TTL, @auto} = opts
@runOnServer ?= opts.runOnServer or true
@TTL ?= opts.TTL or 1000 * 60 * 60 * 24 * 31
if Meteor.isServer
if @runOnServer
@auto ?= true
@handler ?= (c) -> return
unless Cookies.isLoadedOnServer
if @auto
self = @
WebApp.connectHandlers.use (req, res, next) ->
req.Cookies = __middlewareHandler req, res, self
next()
Cookies.isLoadedOnServer = true
else
super document.cookie, null, @TTL, @runOnServer
###
@function
@namespace Cookies
@name middleware
@description Get Cookies instance into callback
@returns {void}
###
middleware: ->
self = @
return (req, res, next) ->
_cookie = __middlewareHandler req, res, self
self.handler and self.handler _cookie
next()
Cookies.isLoadedOnServer = false if Meteor.isServer