@eggjs/cookies 4.0.0-beta.34 → 4.0.0-beta.36
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/dist/cookie.d.ts +67 -64
- package/dist/cookie.js +70 -87
- package/dist/cookies.d.ts +43 -39
- package/dist/cookies.js +215 -275
- package/dist/error.d.ts +5 -2
- package/dist/error.js +11 -10
- package/dist/index.d.ts +5 -4
- package/dist/index.js +6 -5
- package/dist/keygrip.d.ts +13 -10
- package/dist/keygrip.js +86 -103
- package/package.json +24 -31
package/dist/cookie.d.ts
CHANGED
|
@@ -1,66 +1,69 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
1
|
+
//#region src/cookie.d.ts
|
|
2
|
+
interface CookieSetOptions {
|
|
3
|
+
/**
|
|
4
|
+
* The path for the cookie to be set in
|
|
5
|
+
*/
|
|
6
|
+
path?: string | null;
|
|
7
|
+
/**
|
|
8
|
+
* The domain for the cookie
|
|
9
|
+
*/
|
|
10
|
+
domain?: string | (() => string);
|
|
11
|
+
/**
|
|
12
|
+
* Is overridable
|
|
13
|
+
*/
|
|
14
|
+
overwrite?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Is the same site
|
|
17
|
+
*/
|
|
18
|
+
sameSite?: string | boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Encrypt the cookie's value or not
|
|
21
|
+
*/
|
|
22
|
+
encrypt?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Max age for browsers
|
|
25
|
+
*/
|
|
26
|
+
maxAge?: number;
|
|
27
|
+
/**
|
|
28
|
+
* Expire time
|
|
29
|
+
*/
|
|
30
|
+
expires?: Date;
|
|
31
|
+
/**
|
|
32
|
+
* Is for http only
|
|
33
|
+
*/
|
|
34
|
+
httpOnly?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Encrypt the cookie's value or not
|
|
37
|
+
*/
|
|
38
|
+
secure?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Once `true` and secure set to `true`, ignore the secure error in a none-ssl environment.
|
|
41
|
+
*/
|
|
42
|
+
ignoreSecureError?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Is it signed or not.
|
|
45
|
+
*/
|
|
46
|
+
signed?: boolean | number;
|
|
47
|
+
/**
|
|
48
|
+
* Is it partitioned or not.
|
|
49
|
+
*/
|
|
50
|
+
partitioned?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Remove unpartitioned same name cookie or not.
|
|
53
|
+
*/
|
|
54
|
+
removeUnpartitioned?: boolean;
|
|
55
|
+
/**
|
|
56
|
+
* The cookie priority.
|
|
57
|
+
*/
|
|
58
|
+
priority?: "low" | "medium" | "high" | "LOW" | "MEDIUM" | "HIGH";
|
|
58
59
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
60
|
+
declare class Cookie {
|
|
61
|
+
name: string;
|
|
62
|
+
value: string;
|
|
63
|
+
readonly attrs: CookieSetOptions;
|
|
64
|
+
constructor(name: string, value?: string | null, attrs?: CookieSetOptions);
|
|
65
|
+
toString(): string;
|
|
66
|
+
toHeader(): string;
|
|
66
67
|
}
|
|
68
|
+
//#endregion
|
|
69
|
+
export { Cookie, CookieSetOptions };
|
package/dist/cookie.js
CHANGED
|
@@ -1,94 +1,77 @@
|
|
|
1
|
-
import assert from
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
|
|
3
|
+
//#region src/cookie.ts
|
|
2
4
|
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
|
|
5
|
+
* RegExp to match field-content in RFC 7230 sec 3.2
|
|
6
|
+
*
|
|
7
|
+
* field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
|
|
8
|
+
* field-vchar = VCHAR / obs-text
|
|
9
|
+
* obs-text = %x80-FF
|
|
10
|
+
*/
|
|
11
|
+
const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
|
|
10
12
|
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
* RegExp to match Same-Site cookie attribute value.
|
|
14
|
+
* https://en.wikipedia.org/wiki/HTTP_cookie#SameSite_cookie
|
|
15
|
+
*/
|
|
14
16
|
const sameSiteRegExp = /^(?:none|lax|strict)$/i;
|
|
15
17
|
/**
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
* RegExp to match Priority cookie attribute value.
|
|
19
|
+
*/
|
|
18
20
|
const PRIORITY_REGEXP = /^(?:low|medium|high)$/i;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
header += '; domain=' + attrs.domain;
|
|
62
|
-
}
|
|
63
|
-
if (attrs.priority) {
|
|
64
|
-
header += '; priority=' + attrs.priority.toLowerCase();
|
|
65
|
-
}
|
|
66
|
-
if (attrs.sameSite) {
|
|
67
|
-
header += '; samesite=' + (attrs.sameSite === true ? 'strict' : attrs.sameSite.toLowerCase());
|
|
68
|
-
}
|
|
69
|
-
if (attrs.secure) {
|
|
70
|
-
header += '; secure';
|
|
71
|
-
}
|
|
72
|
-
if (attrs.httpOnly) {
|
|
73
|
-
header += '; httponly';
|
|
74
|
-
}
|
|
75
|
-
if (attrs.partitioned) {
|
|
76
|
-
header += '; partitioned';
|
|
77
|
-
}
|
|
78
|
-
return header;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
21
|
+
var Cookie = class {
|
|
22
|
+
name;
|
|
23
|
+
value;
|
|
24
|
+
attrs;
|
|
25
|
+
constructor(name, value, attrs) {
|
|
26
|
+
assert(fieldContentRegExp.test(name), "argument name is invalid");
|
|
27
|
+
assert(!value || fieldContentRegExp.test(value), "argument value is invalid");
|
|
28
|
+
this.name = name;
|
|
29
|
+
this.value = value ?? "";
|
|
30
|
+
this.attrs = mergeDefaultAttrs(attrs);
|
|
31
|
+
assert(!this.attrs.path || fieldContentRegExp.test(this.attrs.path), "argument option path is invalid");
|
|
32
|
+
if (typeof this.attrs.domain === "function") this.attrs.domain = this.attrs.domain();
|
|
33
|
+
assert(!this.attrs.domain || fieldContentRegExp.test(this.attrs.domain), "argument option domain is invalid");
|
|
34
|
+
assert(!this.attrs.sameSite || this.attrs.sameSite === true || sameSiteRegExp.test(this.attrs.sameSite), "argument option sameSite is invalid");
|
|
35
|
+
assert(!this.attrs.priority || PRIORITY_REGEXP.test(this.attrs.priority), "argument option priority is invalid");
|
|
36
|
+
if (!value) {
|
|
37
|
+
this.attrs.expires = /* @__PURE__ */ new Date(0);
|
|
38
|
+
this.attrs.maxAge = void 0;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
toString() {
|
|
42
|
+
return this.name + "=" + this.value;
|
|
43
|
+
}
|
|
44
|
+
toHeader() {
|
|
45
|
+
let header = this.toString();
|
|
46
|
+
const attrs = this.attrs;
|
|
47
|
+
if (attrs.path) header += "; path=" + attrs.path;
|
|
48
|
+
const maxAge = typeof attrs.maxAge === "string" ? parseInt(attrs.maxAge, 10) : attrs.maxAge;
|
|
49
|
+
if (maxAge) {
|
|
50
|
+
header += "; max-age=" + Math.round(maxAge / 1e3);
|
|
51
|
+
attrs.expires = new Date(Date.now() + maxAge);
|
|
52
|
+
}
|
|
53
|
+
if (attrs.expires) header += "; expires=" + attrs.expires.toUTCString();
|
|
54
|
+
if (attrs.domain) header += "; domain=" + attrs.domain;
|
|
55
|
+
if (attrs.priority) header += "; priority=" + attrs.priority.toLowerCase();
|
|
56
|
+
if (attrs.sameSite) header += "; samesite=" + (attrs.sameSite === true ? "strict" : attrs.sameSite.toLowerCase());
|
|
57
|
+
if (attrs.secure) header += "; secure";
|
|
58
|
+
if (attrs.httpOnly) header += "; httponly";
|
|
59
|
+
if (attrs.partitioned) header += "; partitioned";
|
|
60
|
+
return header;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
81
63
|
function mergeDefaultAttrs(attrs) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
return merged;
|
|
64
|
+
return {
|
|
65
|
+
path: "/",
|
|
66
|
+
httpOnly: true,
|
|
67
|
+
secure: false,
|
|
68
|
+
overwrite: false,
|
|
69
|
+
sameSite: false,
|
|
70
|
+
partitioned: false,
|
|
71
|
+
priority: void 0,
|
|
72
|
+
...attrs
|
|
73
|
+
};
|
|
93
74
|
}
|
|
94
|
-
|
|
75
|
+
|
|
76
|
+
//#endregion
|
|
77
|
+
export { Cookie };
|
package/dist/cookies.d.ts
CHANGED
|
@@ -1,43 +1,47 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { CookieSetOptions } from "./cookie.js";
|
|
2
|
+
import { Keygrip } from "./keygrip.js";
|
|
3
|
+
|
|
4
|
+
//#region src/cookies.d.ts
|
|
5
|
+
interface DefaultCookieOptions extends CookieSetOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Auto get and set `_CHIPS-` prefix cookie to adaptation CHIPS mode (The default value is false).
|
|
8
|
+
*/
|
|
9
|
+
autoChips?: boolean;
|
|
8
10
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
interface CookieGetOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Whether to sign or not (The default value is true).
|
|
14
|
+
*/
|
|
15
|
+
signed?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Encrypt the cookie's value or not (The default value is false).
|
|
18
|
+
*/
|
|
19
|
+
encrypt?: boolean;
|
|
18
20
|
}
|
|
19
21
|
/**
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
22
|
+
* cookies for egg
|
|
23
|
+
* extend pillarjs/cookies, add encrypt and decrypt
|
|
24
|
+
*/
|
|
25
|
+
declare class Cookies {
|
|
26
|
+
#private;
|
|
27
|
+
readonly ctx: Record<string, any>;
|
|
28
|
+
readonly app: Record<string, any>;
|
|
29
|
+
readonly secure: boolean;
|
|
30
|
+
constructor(ctx: Record<string, any>, keys: string[], defaultCookieOptions?: DefaultCookieOptions);
|
|
31
|
+
get keys(): Keygrip;
|
|
32
|
+
/**
|
|
33
|
+
* get cookie value by name
|
|
34
|
+
* @param {String} name - cookie's name
|
|
35
|
+
* @param {Object} opts - cookies' options
|
|
36
|
+
* - {Boolean} signed - default to true
|
|
37
|
+
* - {Boolean} encrypt - default to false
|
|
38
|
+
* @return {String} value - cookie's value
|
|
39
|
+
*/
|
|
40
|
+
get(name: string, opts?: CookieGetOptions): string | undefined;
|
|
41
|
+
_get(name: string, opts: CookieGetOptions): string | undefined;
|
|
42
|
+
set(name: string, value: string | null, opts?: CookieSetOptions): this;
|
|
43
|
+
isSameSiteNoneCompatible(userAgent: string): boolean;
|
|
44
|
+
isPartitionedCompatible(userAgent: string): boolean;
|
|
43
45
|
}
|
|
46
|
+
//#endregion
|
|
47
|
+
export { CookieGetOptions, Cookies, DefaultCookieOptions };
|