-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
useCookie
, useCookies
and setCookie
utilities
closes #17
- Loading branch information
Showing
5 changed files
with
120 additions
and
1 deletion.
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
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,89 @@ | ||
/* Redistributed from @types/cookie as we bundle deps */ | ||
|
||
/** | ||
* Additional serialization options | ||
*/ | ||
export interface CookieSerializeOptions { | ||
/** | ||
* Specifies the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.3|Domain Set-Cookie attribute}. By default, no | ||
* domain is set, and most clients will consider the cookie to apply to only | ||
* the current domain. | ||
*/ | ||
domain?: string; | ||
|
||
/** | ||
* Specifies a function that will be used to encode a cookie's value. Since | ||
* value of a cookie has a limited character set (and must be a simple | ||
* string), this function can be used to encode a value into a string suited | ||
* for a cookie's value. | ||
* | ||
* The default function is the global `encodeURIComponent`, which will | ||
* encode a JavaScript string into UTF-8 byte sequences and then URL-encode | ||
* any that fall outside of the cookie range. | ||
*/ | ||
encode?(value: string): string; | ||
|
||
/** | ||
* Specifies the `Date` object to be the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.1|`Expires` `Set-Cookie` attribute}. By default, | ||
* no expiration is set, and most clients will consider this a "non-persistent cookie" and will delete | ||
* it on a condition like exiting a web browser application. | ||
* | ||
* *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification} | ||
* states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is | ||
* possible not all clients by obey this, so if both are set, they should | ||
* point to the same date and time. | ||
*/ | ||
expires?: Date; | ||
/** | ||
* Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.6|`HttpOnly` `Set-Cookie` attribute}. | ||
* When truthy, the `HttpOnly` attribute is set, otherwise it is not. By | ||
* default, the `HttpOnly` attribute is not set. | ||
* | ||
* *Note* be careful when setting this to true, as compliant clients will | ||
* not allow client-side JavaScript to see the cookie in `document.cookie`. | ||
*/ | ||
httpOnly?: boolean; | ||
/** | ||
* Specifies the number (in seconds) to be the value for the `Max-Age` | ||
* `Set-Cookie` attribute. The given number will be converted to an integer | ||
* by rounding down. By default, no maximum age is set. | ||
* | ||
* *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification} | ||
* states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is | ||
* possible not all clients by obey this, so if both are set, they should | ||
* point to the same date and time. | ||
*/ | ||
maxAge?: number; | ||
/** | ||
* Specifies the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.4|`Path` `Set-Cookie` attribute}. | ||
* By default, the path is considered the "default path". | ||
*/ | ||
path?: string; | ||
/** | ||
* Specifies the boolean or string to be the value for the {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7|`SameSite` `Set-Cookie` attribute}. | ||
* | ||
* - `true` will set the `SameSite` attribute to `Strict` for strict same | ||
* site enforcement. | ||
* - `false` will not set the `SameSite` attribute. | ||
* - `'lax'` will set the `SameSite` attribute to Lax for lax same site | ||
* enforcement. | ||
* - `'strict'` will set the `SameSite` attribute to Strict for strict same | ||
* site enforcement. | ||
* - `'none'` will set the SameSite attribute to None for an explicit | ||
* cross-site cookie. | ||
* | ||
* More information about the different enforcement levels can be found in {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7|the specification}. | ||
* | ||
* *note* This is an attribute that has not yet been fully standardized, and may change in the future. This also means many clients may ignore this attribute until they understand it. | ||
*/ | ||
sameSite?: true | false | 'lax' | 'strict' | 'none'; | ||
/** | ||
* Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.5|`Secure` `Set-Cookie` attribute}. When truthy, the | ||
* `Secure` attribute is set, otherwise it is not. By default, the `Secure` attribute is not set. | ||
* | ||
* *Note* be careful when setting this to `true`, as compliant clients will | ||
* not send the cookie back to the server in the future if the browser does | ||
* not have an HTTPS connection. | ||
*/ | ||
secure?: boolean; | ||
} |
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,17 @@ | ||
import type { IncomingMessage, ServerResponse } from 'http' | ||
import { parse, serialize } from 'cookie' | ||
import type { CookieSerializeOptions } from '../types/cookie' | ||
import { appendHeader } from './response' | ||
|
||
export function useCookies (req: IncomingMessage): Record<string, string> { | ||
return parse(req.headers.cookie || '') | ||
} | ||
|
||
export function useCookie (req: IncomingMessage, name: string): string | undefined { | ||
return useCookies(req)[name] | ||
} | ||
|
||
export function setCookie (res: ServerResponse, name: string, value: string, serializeOptions: CookieSerializeOptions) { | ||
const cookieStr = serialize(name, value, serializeOptions) | ||
appendHeader(res, 'Set-Cookie', cookieStr) | ||
} |
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,4 +1,5 @@ | ||
export * from './body' | ||
export * from './consts' | ||
export * from './cookie' | ||
export * from './request' | ||
export * from './response' | ||
export * from './consts' |
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 |
---|---|---|
|
@@ -702,6 +702,11 @@ | |
dependencies: | ||
"@types/node" "*" | ||
|
||
"@types/cookie@^0.4.0": | ||
version "0.4.0" | ||
resolved "https://registry.yarnpkg.com/@types/cookie/-/cookie-0.4.0.tgz#14f854c0f93d326e39da6e3b6f34f7d37513d108" | ||
integrity sha512-y7mImlc/rNkvCRmg8gC3/lj87S7pTUIJ6QGjwHR9WQJcFs+ZMTOaoPrkdFA/YdbuqVEmEbb5RdhVxMkAcgOnpg== | ||
|
||
"@types/cookiejar@*": | ||
version "2.1.2" | ||
resolved "https://registry.yarnpkg.com/@types/cookiejar/-/cookiejar-2.1.2.tgz#66ad9331f63fe8a3d3d9d8c6e3906dd10f6446e8" | ||
|
@@ -1835,6 +1840,11 @@ [email protected]: | |
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.0.tgz#beb437e7022b3b6d49019d088665303ebe9c14ba" | ||
integrity sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg== | ||
|
||
cookie@^0.4.1: | ||
version "0.4.1" | ||
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.1.tgz#afd713fe26ebd21ba95ceb61f9a8116e50a537d1" | ||
integrity sha512-ZwrFkGJxUR3EIoXtO+yVE69Eb7KlixbaeAWfBQB9vVsNn/o+Yw69gBWSSDK825hQNdN+wF8zELf3dFNl/kxkUA== | ||
|
||
cookiejar@^2.1.2: | ||
version "2.1.2" | ||
resolved "https://registry.yarnpkg.com/cookiejar/-/cookiejar-2.1.2.tgz#dd8a235530752f988f9a0844f3fc589e3111125c" | ||
|