-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
152 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/kit': patch | ||
--- | ||
|
||
[breaking] add API for interacting with cookies |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import * as cookie from 'cookie'; | ||
|
||
/** | ||
* @param {Request} request | ||
* @param {URL} url | ||
*/ | ||
export function get_cookies(request, url) { | ||
const initial_cookies = cookie.parse(request.headers.get('cookie') ?? ''); | ||
|
||
/** @type {Array<{ name: string, value: string, options: import('cookie').CookieSerializeOptions }>} */ | ||
const new_cookies = []; | ||
|
||
/** @type {import('types').Cookies} */ | ||
const cookies = { | ||
get(name, opts) { | ||
const decode = opts?.decode || decodeURIComponent; | ||
|
||
let i = new_cookies.length; | ||
while (i--) { | ||
const cookie = new_cookies[i]; | ||
|
||
if ( | ||
cookie.name === name && | ||
domain_matches(url.hostname, cookie.options.domain) && | ||
path_matches(url.pathname, cookie.options.path) | ||
) { | ||
return cookie.value; | ||
} | ||
} | ||
|
||
return name in initial_cookies ? decode(initial_cookies[name]) : undefined; | ||
}, | ||
set(name, value, options = {}) { | ||
new_cookies.push({ | ||
name, | ||
value, | ||
options: { | ||
httpOnly: true, | ||
secure: true, | ||
...options | ||
} | ||
}); | ||
}, | ||
delete(name) { | ||
new_cookies.push({ name, value: '', options: { expires: new Date(0) } }); | ||
} | ||
}; | ||
|
||
return { cookies, new_cookies }; | ||
} | ||
|
||
/** | ||
* @param {string} hostname | ||
* @param {string} [constraint] | ||
*/ | ||
export function domain_matches(hostname, constraint) { | ||
if (!constraint) return true; | ||
|
||
const normalized = constraint[0] === '.' ? constraint.slice(1) : constraint; | ||
|
||
if (hostname === normalized) return true; | ||
return hostname.endsWith('.' + normalized); | ||
} | ||
|
||
/** | ||
* @param {string} path | ||
* @param {string} [constraint] | ||
*/ | ||
export function path_matches(path, constraint) { | ||
if (!constraint) return true; | ||
|
||
const normalized = constraint.endsWith('/') ? constraint.slice(0, -1) : constraint; | ||
|
||
if (path === normalized) return true; | ||
return path.startsWith(normalized + '/'); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 4 additions & 3 deletions
7
packages/kit/test/apps/basics/src/routes/headers/set-cookie/+layout.server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export function load({ setHeaders }) { | ||
setHeaders({ | ||
'set-cookie': 'cookie1=value1' | ||
/** @type {import('./$types').LayoutServerLoad} */ | ||
export function load({ cookies }) { | ||
cookies.set('cookie1', 'value1', { | ||
secure: false // safari | ||
}); | ||
} |
7 changes: 4 additions & 3 deletions
7
packages/kit/test/apps/basics/src/routes/headers/set-cookie/sub/+page.server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export function load({ setHeaders }) { | ||
setHeaders({ | ||
'set-cookie': 'cookie2=value2' | ||
/** @type {import('./$types').PageServerLoad} */ | ||
export function load({ cookies }) { | ||
cookies.set('cookie2', 'value2', { | ||
secure: false // safari | ||
}); | ||
} |
7 changes: 2 additions & 5 deletions
7
packages/kit/test/apps/basics/src/routes/load/set-cookie-fetch/b.json/+server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 5 additions & 2 deletions
7
packages/kit/test/apps/basics/src/routes/shadowed/redirect-get-with-cookie/+page.server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import { redirect } from '@sveltejs/kit'; | ||
|
||
export function load({ setHeaders }) { | ||
setHeaders({ 'set-cookie': 'shadow-redirect=happy' }); | ||
/** @type {import('./$types').PageServerLoad} */ | ||
export function load({ cookies }) { | ||
cookies.set('shadow-redirect', 'happy', { | ||
secure: false // safari | ||
}); | ||
throw redirect(302, '/shadowed/redirected'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.