@kavach/cookie 1.0.0-next.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.js ADDED
@@ -0,0 +1,204 @@
1
+ /*!
2
+ * cookie
3
+ * Copyright(c) 2012-2014 Roman Shtylman
4
+ * Copyright(c) 2015 Douglas Christopher Wilson
5
+ * MIT Licensed
6
+ *
7
+ * Converted to ESM for use in SvelteKit apps
8
+ */
9
+
10
+ 'use strict'
11
+
12
+ /**
13
+ * Module variables.
14
+ * @private
15
+ */
16
+
17
+ var decode = decodeURIComponent
18
+ var encode = encodeURIComponent
19
+ var pairSplitRegExp = /; */
20
+
21
+ /**
22
+ * RegExp to match field-content in RFC 7230 sec 3.2
23
+ *
24
+ * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
25
+ * field-vchar = VCHAR / obs-text
26
+ * obs-text = %x80-FF
27
+ */
28
+
29
+ // eslint-disable-next-line no-control-regex
30
+ var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/
31
+
32
+ /**
33
+ * Parse a cookie header.
34
+ *
35
+ * Parse the given cookie header string into an object
36
+ * The object has the various cookies as keys(names) => values
37
+ *
38
+ * @param {string} str
39
+ * @param {object} [options]
40
+ * @return {object}
41
+ * @public
42
+ */
43
+
44
+ export function parse(str, options) {
45
+ if (typeof str !== 'string') {
46
+ throw new TypeError('argument str must be a string')
47
+ }
48
+
49
+ var obj = {}
50
+ var opt = options || {}
51
+ var pairs = str.split(pairSplitRegExp)
52
+ var dec = opt.decode || decode
53
+
54
+ for (var i = 0; i < pairs.length; i++) {
55
+ var pair = pairs[i]
56
+ var eq_idx = pair.indexOf('=')
57
+
58
+ // skip things that don't look like key=value
59
+ if (eq_idx < 0) {
60
+ continue
61
+ }
62
+
63
+ var key = pair.slice(0, eq_idx).trim()
64
+ var val = pair.slice(++eq_idx, pair.length).trim()
65
+
66
+ // quoted values
67
+ if ('"' == val[0]) {
68
+ val = val.slice(1, -1)
69
+ }
70
+
71
+ // only assign once
72
+ if (undefined == obj[key]) {
73
+ obj[key] = tryDecode(val, dec)
74
+ }
75
+ }
76
+
77
+ return obj
78
+ }
79
+
80
+ /**
81
+ * Serialize data into a cookie header.
82
+ *
83
+ * Serialize the a name value pair into a cookie string suitable for
84
+ * http headers. An optional options object specified cookie parameters.
85
+ *
86
+ * serialize('foo', 'bar', { httpOnly: true })
87
+ * => "foo=bar; httpOnly"
88
+ *
89
+ * @param {string} name
90
+ * @param {string} val
91
+ * @param {object} [options]
92
+ * @return {string}
93
+ * @public
94
+ */
95
+
96
+ export function serialize(name, val, options) {
97
+ var opt = options || {}
98
+ var enc = opt.encode || encode
99
+
100
+ if (typeof enc !== 'function') {
101
+ throw new TypeError('option encode is invalid')
102
+ }
103
+
104
+ if (!fieldContentRegExp.test(name)) {
105
+ throw new TypeError('argument name is invalid')
106
+ }
107
+
108
+ var value = enc(val)
109
+
110
+ if (value && !fieldContentRegExp.test(value)) {
111
+ throw new TypeError('argument val is invalid')
112
+ }
113
+
114
+ var str = name + '=' + value
115
+
116
+ if (null != opt.maxAge) {
117
+ var maxAge = opt.maxAge - 0
118
+
119
+ if (isNaN(maxAge) || !isFinite(maxAge)) {
120
+ throw new TypeError('option maxAge is invalid')
121
+ }
122
+
123
+ str += '; Max-Age=' + Math.floor(maxAge)
124
+ }
125
+
126
+ if (opt.domain) {
127
+ if (!fieldContentRegExp.test(opt.domain)) {
128
+ throw new TypeError('option domain is invalid')
129
+ }
130
+
131
+ str += '; Domain=' + opt.domain
132
+ }
133
+
134
+ if (opt.path) {
135
+ if (!fieldContentRegExp.test(opt.path)) {
136
+ throw new TypeError('option path is invalid')
137
+ }
138
+
139
+ str += '; Path=' + opt.path
140
+ }
141
+
142
+ if (opt.expires) {
143
+ if (typeof opt.expires.toUTCString !== 'function') {
144
+ throw new TypeError('option expires is invalid')
145
+ }
146
+
147
+ str += '; Expires=' + opt.expires.toUTCString()
148
+ }
149
+
150
+ if (opt.httpOnly) {
151
+ str += '; HttpOnly'
152
+ }
153
+
154
+ if (opt.secure) {
155
+ str += '; Secure'
156
+ }
157
+
158
+ if (opt.sameSite) {
159
+ var sameSite =
160
+ typeof opt.sameSite === 'string'
161
+ ? opt.sameSite.toLowerCase()
162
+ : opt.sameSite
163
+
164
+ switch (sameSite) {
165
+ case true:
166
+ str += '; SameSite=Strict'
167
+ break
168
+ case 'lax':
169
+ str += '; SameSite=Lax'
170
+ break
171
+ case 'strict':
172
+ str += '; SameSite=Strict'
173
+ break
174
+ case 'none':
175
+ str += '; SameSite=None'
176
+ break
177
+ default:
178
+ throw new TypeError('option sameSite is invalid')
179
+ }
180
+ }
181
+
182
+ return str
183
+ }
184
+
185
+ /**
186
+ * Try decoding a string using a decoding function.
187
+ *
188
+ * @param {string} str
189
+ * @param {function} decode
190
+ * @private
191
+ */
192
+
193
+ function tryDecode(str, decode) {
194
+ try {
195
+ return decode(str)
196
+ } catch (e) {
197
+ return str
198
+ }
199
+ }
200
+
201
+ export default {
202
+ parse,
203
+ serialize
204
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "shared-config/jsconfig.json",
3
+ "include": ["src/**/*.js"],
4
+ "compilerOptions": {
5
+ "outDir": "dist",
6
+ "checkJs": false
7
+ }
8
+ }
@@ -0,0 +1,6 @@
1
+ import { defineConfig } from 'vite'
2
+ import { testConfig } from 'shared-config'
3
+
4
+ export default defineConfig({
5
+ test: testConfig
6
+ })