@fastify/cookie 11.0.1 → 11.1.0
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/.github/dependabot.yml +41 -1
- package/.github/workflows/ci.yml +12 -2
- package/.github/workflows/lock-threads.yml +19 -0
- package/LICENSE +1 -1
- package/README.md +36 -33
- package/benchmark/cookie-multi.js +1 -1
- package/benchmark/cookie.js +1 -1
- package/eslint.config.js +6 -0
- package/{plugin.js → index.js} +2 -2
- package/package.json +38 -24
- package/signer.js +1 -1
- package/test/cookie.test.js +504 -536
- package/test/signer.test.js +51 -64
- package/types/{plugin.d.ts → index.d.ts} +21 -26
- package/types/index.tst.ts +290 -0
- package/.github/stale.yml +0 -21
- package/.taprc +0 -2
- package/types/plugin.test-d.ts +0 -249
package/types/plugin.test-d.ts
DELETED
|
@@ -1,249 +0,0 @@
|
|
|
1
|
-
import cookie from '..';
|
|
2
|
-
import { expectError, expectType } from 'tsd';
|
|
3
|
-
import * as fastifyCookieStar from '..';
|
|
4
|
-
import fastifyCookieCjsImport = require('..');
|
|
5
|
-
import fastifyCookieDefault, { fastifyCookie as fastifyCookieNamed } from '..';
|
|
6
|
-
import fastify, { FastifyInstance, FastifyReply, setCookieWrapper } from 'fastify';
|
|
7
|
-
|
|
8
|
-
const fastifyCookieCjs = require('..');
|
|
9
|
-
|
|
10
|
-
const app: FastifyInstance = fastify();
|
|
11
|
-
app.register(fastifyCookieNamed);
|
|
12
|
-
app.register(fastifyCookieDefault);
|
|
13
|
-
app.register(fastifyCookieCjs);
|
|
14
|
-
app.register(fastifyCookieCjsImport.default);
|
|
15
|
-
app.register(fastifyCookieCjsImport.fastifyCookie);
|
|
16
|
-
app.register(fastifyCookieStar.default);
|
|
17
|
-
app.register(fastifyCookieStar.fastifyCookie);
|
|
18
|
-
|
|
19
|
-
expectType<fastifyCookieStar.FastifyCookie>(fastifyCookieNamed);
|
|
20
|
-
expectType<fastifyCookieStar.FastifyCookie>(fastifyCookieDefault);
|
|
21
|
-
expectType<fastifyCookieStar.FastifyCookie>(fastifyCookieCjsImport.default);
|
|
22
|
-
expectType<fastifyCookieStar.FastifyCookie>(fastifyCookieCjsImport.fastifyCookie);
|
|
23
|
-
expectType<fastifyCookieStar.FastifyCookie>(fastifyCookieStar.default);
|
|
24
|
-
expectType<fastifyCookieStar.FastifyCookie>(
|
|
25
|
-
fastifyCookieStar.fastifyCookie
|
|
26
|
-
);
|
|
27
|
-
expectType<any>(fastifyCookieCjs);
|
|
28
|
-
|
|
29
|
-
expectType<fastifyCookieStar.Sign>(fastifyCookieDefault.sign);
|
|
30
|
-
expectType<fastifyCookieStar.Unsign>(fastifyCookieDefault.unsign);
|
|
31
|
-
expectType<fastifyCookieStar.SignerFactory >(fastifyCookieDefault.signerFactory );
|
|
32
|
-
|
|
33
|
-
expectType<fastifyCookieStar.Sign>(fastifyCookieNamed.sign);
|
|
34
|
-
expectType<fastifyCookieStar.Unsign>(fastifyCookieNamed.unsign);
|
|
35
|
-
expectType<fastifyCookieStar.SignerFactory >(fastifyCookieNamed.signerFactory );
|
|
36
|
-
|
|
37
|
-
const server = fastify();
|
|
38
|
-
|
|
39
|
-
server.register(cookie);
|
|
40
|
-
|
|
41
|
-
server.after((_err) => {
|
|
42
|
-
expectType< string >(
|
|
43
|
-
server.serializeCookie('sessionId', 'aYb4uTIhdBXC')
|
|
44
|
-
);
|
|
45
|
-
|
|
46
|
-
expectType<{ [key: string]: string }>(
|
|
47
|
-
// See https://github.com/fastify/fastify-cookie#manual-cookie-parsing
|
|
48
|
-
server.parseCookie('sessionId=aYb4uTIhdBXC')
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
server.get('/', (request, reply) => {
|
|
52
|
-
const test = request.cookies.test;
|
|
53
|
-
expectType<string | undefined>(test);
|
|
54
|
-
|
|
55
|
-
expectType<setCookieWrapper>(reply.cookie);
|
|
56
|
-
expectType<setCookieWrapper>(reply.setCookie);
|
|
57
|
-
|
|
58
|
-
expectType<FastifyReply>(
|
|
59
|
-
reply
|
|
60
|
-
.setCookie('test', test!, { domain: 'example.com', path: '/' })
|
|
61
|
-
.clearCookie('foo')
|
|
62
|
-
.send({ hello: 'world' })
|
|
63
|
-
);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
expectType<(value: string) => string>(server.signCookie)
|
|
67
|
-
expectType<(value: string) => fastifyCookieStar.UnsignResult>(server.unsignCookie)
|
|
68
|
-
|
|
69
|
-
server.get('/', (request, reply) => {
|
|
70
|
-
expectType<(value: string) => string>(request.signCookie)
|
|
71
|
-
expectType<(value: string) => string>(reply.signCookie)
|
|
72
|
-
expectType<(value: string) => fastifyCookieStar.UnsignResult>(request.unsignCookie)
|
|
73
|
-
expectType<(value: string) => fastifyCookieStar.UnsignResult>(reply.unsignCookie)
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
const serverWithHttp2 = fastify({ http2: true });
|
|
78
|
-
|
|
79
|
-
serverWithHttp2.register(cookie);
|
|
80
|
-
|
|
81
|
-
serverWithHttp2.after(() => {
|
|
82
|
-
serverWithHttp2.get('/', (request, reply) => {
|
|
83
|
-
const test = request.cookies.test;
|
|
84
|
-
reply
|
|
85
|
-
.setCookie('test', test!, { domain: 'example.com', path: '/' })
|
|
86
|
-
.clearCookie('foo')
|
|
87
|
-
.send({ hello: 'world' });
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
const testSamesiteOptionsApp = fastify();
|
|
92
|
-
|
|
93
|
-
testSamesiteOptionsApp.register(cookie);
|
|
94
|
-
testSamesiteOptionsApp.after(() => {
|
|
95
|
-
server.get('/test-samesite-option-true', (request, reply) => {
|
|
96
|
-
const test = request.cookies.test;
|
|
97
|
-
reply.setCookie('test', test!, { sameSite: true }).send({ hello: 'world' });
|
|
98
|
-
});
|
|
99
|
-
server.get('/test-samesite-option-false', (request, reply) => {
|
|
100
|
-
const test = request.cookies.test;
|
|
101
|
-
reply.setCookie('test', test!, { sameSite: false }).send({ hello: 'world' });
|
|
102
|
-
});
|
|
103
|
-
server.get('/test-samesite-option-lax', (request, reply) => {
|
|
104
|
-
const test = request.cookies.test;
|
|
105
|
-
reply.setCookie('test', test!, { sameSite: 'lax' }).send({ hello: 'world' });
|
|
106
|
-
});
|
|
107
|
-
server.get('/test-samesite-option-strict', (request, reply) => {
|
|
108
|
-
const test = request.cookies.test;
|
|
109
|
-
reply
|
|
110
|
-
.setCookie('test', test!, { sameSite: 'strict' })
|
|
111
|
-
.send({ hello: 'world' });
|
|
112
|
-
});
|
|
113
|
-
server.get('/test-samesite-option-none', (request, reply) => {
|
|
114
|
-
const test = request.cookies.test;
|
|
115
|
-
reply
|
|
116
|
-
.setCookie('test', test!, { sameSite: 'none' })
|
|
117
|
-
.send({ hello: 'world' });
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
const appWithImplicitHttpSigned = fastify();
|
|
122
|
-
|
|
123
|
-
appWithImplicitHttpSigned.register(cookie, {
|
|
124
|
-
secret: 'testsecret',
|
|
125
|
-
});
|
|
126
|
-
appWithImplicitHttpSigned.register(cookie, {
|
|
127
|
-
secret: 'testsecret',
|
|
128
|
-
algorithm: 'sha512'
|
|
129
|
-
});
|
|
130
|
-
appWithImplicitHttpSigned.after(() => {
|
|
131
|
-
server.get('/', (request, reply) => {
|
|
132
|
-
appWithImplicitHttpSigned.unsignCookie(request.cookies.test!);
|
|
133
|
-
appWithImplicitHttpSigned.unsignCookie('test');
|
|
134
|
-
|
|
135
|
-
reply.unsignCookie(request.cookies.test!);
|
|
136
|
-
reply.unsignCookie('test');
|
|
137
|
-
|
|
138
|
-
request.unsignCookie(request.cookies.anotherTest!);
|
|
139
|
-
request.unsignCookie('anotherTest');
|
|
140
|
-
|
|
141
|
-
reply.send({ hello: 'world' });
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
const appWithRotationSecret = fastify();
|
|
146
|
-
|
|
147
|
-
appWithRotationSecret.register(cookie, {
|
|
148
|
-
secret: ['testsecret'],
|
|
149
|
-
});
|
|
150
|
-
appWithRotationSecret.after(() => {
|
|
151
|
-
server.get('/', (request, reply) => {
|
|
152
|
-
reply.unsignCookie(request.cookies.test!);
|
|
153
|
-
const unsigned = reply.unsignCookie('test');
|
|
154
|
-
|
|
155
|
-
expectType<boolean>(unsigned.valid);
|
|
156
|
-
if (unsigned.valid) {
|
|
157
|
-
expectType<string>(unsigned.value);
|
|
158
|
-
} else {
|
|
159
|
-
expectType<null>(unsigned.value);
|
|
160
|
-
}
|
|
161
|
-
expectType<boolean>(unsigned.renew);
|
|
162
|
-
|
|
163
|
-
reply.send({ hello: 'world' });
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
const appWithParseOptions = fastify();
|
|
168
|
-
|
|
169
|
-
const parseOptions: fastifyCookieStar.CookieSerializeOptions = {
|
|
170
|
-
domain: 'example.com',
|
|
171
|
-
encode: (value: string) => value,
|
|
172
|
-
expires: new Date(),
|
|
173
|
-
httpOnly: true,
|
|
174
|
-
maxAge: 3600,
|
|
175
|
-
path: '/',
|
|
176
|
-
sameSite: 'lax',
|
|
177
|
-
secure: true,
|
|
178
|
-
signed: true,
|
|
179
|
-
partitioned: false,
|
|
180
|
-
};
|
|
181
|
-
expectType<fastifyCookieStar.CookieSerializeOptions>(parseOptions);
|
|
182
|
-
|
|
183
|
-
appWithParseOptions.register(cookie, {
|
|
184
|
-
secret: 'testsecret',
|
|
185
|
-
parseOptions,
|
|
186
|
-
});
|
|
187
|
-
appWithParseOptions.after(() => {
|
|
188
|
-
server.get('/', (request, reply) => {
|
|
189
|
-
const unsigned = reply.unsignCookie(request.cookies.test!);
|
|
190
|
-
|
|
191
|
-
expectType<boolean>(unsigned.valid);
|
|
192
|
-
if (unsigned.valid) {
|
|
193
|
-
expectType<string>(unsigned.value);
|
|
194
|
-
} else {
|
|
195
|
-
expectType<null>(unsigned.value);
|
|
196
|
-
}
|
|
197
|
-
expectType<boolean>(unsigned.renew);
|
|
198
|
-
});
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
const appWithCustomSigner = fastify()
|
|
202
|
-
|
|
203
|
-
appWithCustomSigner.register(cookie, {
|
|
204
|
-
secret: {
|
|
205
|
-
sign: (x) => x + '.signed',
|
|
206
|
-
unsign: (x) => {
|
|
207
|
-
if (x.endsWith('.signed')) { return { renew: false, valid: true, value: x.slice(0, -7) } }
|
|
208
|
-
return { renew: false, valid: false, value: null }
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
})
|
|
212
|
-
appWithCustomSigner.after(() => {
|
|
213
|
-
server.get('/', (request, reply) => {
|
|
214
|
-
reply.unsignCookie(request.cookies.test!)
|
|
215
|
-
const unsigned = reply.unsignCookie('test')
|
|
216
|
-
|
|
217
|
-
expectType<boolean>(unsigned.valid);
|
|
218
|
-
if (unsigned.valid) {
|
|
219
|
-
expectType<string>(unsigned.value);
|
|
220
|
-
} else {
|
|
221
|
-
expectType<null>(unsigned.value);
|
|
222
|
-
}
|
|
223
|
-
expectType<boolean>(unsigned.renew);
|
|
224
|
-
|
|
225
|
-
reply.send({ hello: 'world' })
|
|
226
|
-
})
|
|
227
|
-
})
|
|
228
|
-
|
|
229
|
-
new fastifyCookieStar.Signer('secretString')
|
|
230
|
-
new fastifyCookieStar.Signer(['secretStringInArray'])
|
|
231
|
-
new fastifyCookieStar.Signer(Buffer.from('secretString'))
|
|
232
|
-
new fastifyCookieStar.Signer([Buffer.from('secretStringInArray')])
|
|
233
|
-
const signer = new fastifyCookieStar.Signer(['secretStringInArray'], 'sha256')
|
|
234
|
-
signer.sign('Lorem Ipsum')
|
|
235
|
-
signer.unsign('Lorem Ipsum')
|
|
236
|
-
|
|
237
|
-
const appWithHook: FastifyInstance = fastify();
|
|
238
|
-
|
|
239
|
-
appWithHook.register(cookie, { hook: false });
|
|
240
|
-
appWithHook.register(cookie, { hook: 'onRequest' });
|
|
241
|
-
appWithHook.register(cookie, { hook: 'preHandler' });
|
|
242
|
-
appWithHook.register(cookie, { hook: 'preParsing' });
|
|
243
|
-
appWithHook.register(cookie, { hook: 'preSerialization' });
|
|
244
|
-
appWithHook.register(cookie, { hook: 'preValidation' });
|
|
245
|
-
expectError(appWithHook.register(cookie, { hook: true }));
|
|
246
|
-
expectError(appWithHook.register(cookie, { hook: 'false' }));
|
|
247
|
-
|
|
248
|
-
expectType<(cookieHeader: string, opts?: fastifyCookieStar.ParseOptions) => { [key: string]: string }>(fastifyCookieDefault.parse);
|
|
249
|
-
expectType<(name: string, value: string, opts?: fastifyCookieStar.SerializeOptions) => string>(fastifyCookieDefault.serialize);
|