@yumerijs/core 2.0.4 → 2.0.5
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/server.js +44 -2
- package/dist/session.d.ts +13 -1
- package/dist/session.js +8 -2
- package/package.json +1 -1
package/dist/server.js
CHANGED
|
@@ -119,7 +119,28 @@ class Server {
|
|
|
119
119
|
}
|
|
120
120
|
else {
|
|
121
121
|
let head = session.head;
|
|
122
|
-
head['Set-Cookie'] = Object.entries(session.newCookie).map(([
|
|
122
|
+
head['Set-Cookie'] = Object.entries(session.newCookie).map(([name, cookie]) => {
|
|
123
|
+
let cookieString = `${name}=${cookie.value}`;
|
|
124
|
+
if (cookie.options.expires) {
|
|
125
|
+
cookieString += `; Expires=${cookie.options.expires.toUTCString()}`;
|
|
126
|
+
}
|
|
127
|
+
if (cookie.options.path) {
|
|
128
|
+
cookieString += `; Path=${cookie.options.path}`;
|
|
129
|
+
}
|
|
130
|
+
if (cookie.options.domain) {
|
|
131
|
+
cookieString += `; Domain=${cookie.options.domain}`;
|
|
132
|
+
}
|
|
133
|
+
if (cookie.options.secure) {
|
|
134
|
+
cookieString += `; Secure`;
|
|
135
|
+
}
|
|
136
|
+
if (cookie.options.httpOnly) {
|
|
137
|
+
cookieString += `; HttpOnly`;
|
|
138
|
+
}
|
|
139
|
+
if (cookie.options.sameSite) {
|
|
140
|
+
cookieString += `; SameSite=${cookie.options.sameSite}`;
|
|
141
|
+
}
|
|
142
|
+
return cookieString;
|
|
143
|
+
});
|
|
123
144
|
if (this.enableCors) {
|
|
124
145
|
head['Access-Control-Allow-Origin'] = '*';
|
|
125
146
|
}
|
|
@@ -134,7 +155,28 @@ class Server {
|
|
|
134
155
|
}
|
|
135
156
|
else {
|
|
136
157
|
let head = session.head;
|
|
137
|
-
head['Set-Cookie'] = Object.entries(session.newCookie).map(([
|
|
158
|
+
head['Set-Cookie'] = Object.entries(session.newCookie).map(([name, cookie]) => {
|
|
159
|
+
let cookieString = `${name}=${cookie.value}`;
|
|
160
|
+
if (cookie.options.expires) {
|
|
161
|
+
cookieString += `; Expires=${cookie.options.expires.toUTCString()}`;
|
|
162
|
+
}
|
|
163
|
+
if (cookie.options.path) {
|
|
164
|
+
cookieString += `; Path=${cookie.options.path}`;
|
|
165
|
+
}
|
|
166
|
+
if (cookie.options.domain) {
|
|
167
|
+
cookieString += `; Domain=${cookie.options.domain}`;
|
|
168
|
+
}
|
|
169
|
+
if (cookie.options.secure) {
|
|
170
|
+
cookieString += `; Secure`;
|
|
171
|
+
}
|
|
172
|
+
if (cookie.options.httpOnly) {
|
|
173
|
+
cookieString += `; HttpOnly`;
|
|
174
|
+
}
|
|
175
|
+
if (cookie.options.sameSite) {
|
|
176
|
+
cookieString += `; SameSite=${cookie.options.sameSite}`;
|
|
177
|
+
}
|
|
178
|
+
return cookieString;
|
|
179
|
+
});
|
|
138
180
|
if (this.enableCors) {
|
|
139
181
|
head['Access-Control-Allow-Origin'] = '*';
|
|
140
182
|
}
|
package/dist/session.d.ts
CHANGED
|
@@ -11,13 +11,24 @@ export interface Client {
|
|
|
11
11
|
res: ServerResponse;
|
|
12
12
|
headers?: Record<string, string>;
|
|
13
13
|
}
|
|
14
|
+
export interface CookieOptions {
|
|
15
|
+
expires?: Date;
|
|
16
|
+
path?: string;
|
|
17
|
+
domain?: string;
|
|
18
|
+
secure?: boolean;
|
|
19
|
+
httpOnly?: boolean;
|
|
20
|
+
sameSite?: 'Strict' | 'Lax' | 'None';
|
|
21
|
+
}
|
|
14
22
|
export declare class Session {
|
|
15
23
|
ip: string;
|
|
16
24
|
cookie: Record<string, string>;
|
|
17
25
|
query: Record<string, string> | undefined;
|
|
18
26
|
sessionid: string;
|
|
19
27
|
data: Record<string, any>;
|
|
20
|
-
newCookie: Record<string,
|
|
28
|
+
newCookie: Record<string, {
|
|
29
|
+
value: string;
|
|
30
|
+
options: CookieOptions;
|
|
31
|
+
}>;
|
|
21
32
|
head: Record<string, any>;
|
|
22
33
|
status: number;
|
|
23
34
|
body: any;
|
|
@@ -34,6 +45,7 @@ export declare class Session {
|
|
|
34
45
|
* @param query 请求字符串
|
|
35
46
|
*/
|
|
36
47
|
constructor(ip: string, cookie: Record<string, string>, server: Server, req?: IncomingMessage, res?: ServerResponse, pathname?: string, query?: Record<string, string>);
|
|
48
|
+
setCookie(name: string, value: string, options?: CookieOptions): void;
|
|
37
49
|
/**
|
|
38
50
|
* 解析 Accept-Language 字符串为排序后的语言数组
|
|
39
51
|
* @param header Accept-Language 头,比如 "zh-CN,zh-TW;q=0.8,en-US;q=0.6"
|
package/dist/session.js
CHANGED
|
@@ -82,16 +82,22 @@ class Session {
|
|
|
82
82
|
}
|
|
83
83
|
else {
|
|
84
84
|
this.sessionid = this.generateId(this.ip);
|
|
85
|
-
this.
|
|
85
|
+
this.setCookie('sessionid', this.sessionid);
|
|
86
86
|
}
|
|
87
87
|
if (cookie.lang) {
|
|
88
88
|
this.languages = cookie.lang.split(',');
|
|
89
89
|
}
|
|
90
90
|
else if (req.headers['accept-language']) {
|
|
91
91
|
this.languages = this.parseAcceptLanguages(req.headers['accept-language']);
|
|
92
|
-
this.
|
|
92
|
+
this.setCookie('lang', this.languages.join(','));
|
|
93
93
|
}
|
|
94
94
|
}
|
|
95
|
+
setCookie(name, value, options = {}) {
|
|
96
|
+
if (options.path === undefined) {
|
|
97
|
+
options.path = '/';
|
|
98
|
+
}
|
|
99
|
+
this.newCookie[name] = { value, options };
|
|
100
|
+
}
|
|
95
101
|
/**
|
|
96
102
|
* 解析 Accept-Language 字符串为排序后的语言数组
|
|
97
103
|
* @param header Accept-Language 头,比如 "zh-CN,zh-TW;q=0.8,en-US;q=0.6"
|