Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add partitioned #261

Merged
merged 10 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function parse (str, options) {
const key = str.substring(pos, eqIdx++).trim()

// only assign once
if (undefined === result[key]) {
if (result[key] === undefined) {
const val = (str.charCodeAt(eqIdx) === 0x22)
? str.substring(eqIdx + 1, terminatorPos - 1).trim()
: str.substring(eqIdx, terminatorPos).trim()
Expand Down Expand Up @@ -183,6 +183,10 @@ function serialize (name, val, options) {
str += '; Secure'
}

if (opt.partitioned) {
gurgunday marked this conversation as resolved.
Show resolved Hide resolved
gurgunday marked this conversation as resolved.
Show resolved Hide resolved
str += '; Partitioned'
}

if (opt.sameSite) {
const sameSite = typeof opt.sameSite === 'string'
? opt.sameSite.toLowerCase()
Expand Down
40 changes: 40 additions & 0 deletions test/cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,46 @@ test('should set multiple cookies', (t) => {
})
})

test('should set multiple cookies', (t) => {
t.plan(12)
const fastify = Fastify()
fastify.register(plugin)

fastify.get('/', (req, reply) => {
reply
.setCookie('foo', 'foo')
.cookie('bar', 'test', {
partitioned: true
})
.setCookie('wee', 'woo', {
partitioned: true,
secure: true
})
.send({ hello: 'world' })
})

fastify.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.same(JSON.parse(res.body), { hello: 'world' })

const cookies = res.cookies
t.equal(cookies.length, 3)
t.equal(cookies[0].name, 'foo')
t.equal(cookies[0].value, 'foo')
t.equal(cookies[1].name, 'bar')
t.equal(cookies[1].value, 'test')
t.equal(cookies[2].name, 'wee')
t.equal(cookies[2].value, 'woo')

t.equal(res.headers['set-cookie'][1], 'bar=test; Partitioned')
t.equal(res.headers['set-cookie'][2], 'wee=woo; Secure; Partitioned')
})
})

test('cookies get set correctly with millisecond dates', (t) => {
t.plan(8)
const fastify = Fastify()
Expand Down
2 changes: 1 addition & 1 deletion types/plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ declare namespace fastifyCookie {
httpOnly?: boolean;
/** A `number` in seconds that specifies the `Expires` attribute by adding the specified seconds to the current date. If both `expires` and `maxAge` are set, then `expires` is used. */
maxAge?: number;
partitioned?: boolean;
/** The `Path` attribute. Defaults to `/` (the root path). */
path?: string;
priority?: "low" | "medium" | "high";
/** A `boolean` or one of the `SameSite` string attributes. E.g.: `lax`, `none` or `strict`. */
sameSite?: 'lax' | 'none' | 'strict' | boolean;
/** The `boolean` value of the `Secure` attribute. Set this option to false when communicating over an unencrypted (HTTP) connection. Value can be set to `auto`; in this case the `Secure` attribute will be set to false for HTTP request, in case of HTTPS it will be set to true. Defaults to true. */
Expand Down
1 change: 1 addition & 0 deletions types/plugin.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ const parseOptions: fastifyCookieStar.CookieSerializeOptions = {
sameSite: 'lax',
secure: true,
signed: true,
partitioned: false,
};
expectType<fastifyCookieStar.CookieSerializeOptions>(parseOptions);

Expand Down