@yumerijs/core 2.0.4 → 2.0.6
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 +58 -12
- package/dist/session.d.ts +14 -1
- package/dist/session.js +9 -2
- package/package.json +1 -1
package/dist/server.js
CHANGED
|
@@ -118,13 +118,36 @@ class Server {
|
|
|
118
118
|
this.serveStaticFile(pathname, res);
|
|
119
119
|
}
|
|
120
120
|
else {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
121
|
+
if (!session.responseHandled) {
|
|
122
|
+
let head = session.head;
|
|
123
|
+
head['Set-Cookie'] = Object.entries(session.newCookie).map(([name, cookie]) => {
|
|
124
|
+
let cookieString = `${name}=${cookie.value}`;
|
|
125
|
+
if (cookie.options.expires) {
|
|
126
|
+
cookieString += `; Expires=${cookie.options.expires.toUTCString()}`;
|
|
127
|
+
}
|
|
128
|
+
if (cookie.options.path) {
|
|
129
|
+
cookieString += `; Path=${cookie.options.path}`;
|
|
130
|
+
}
|
|
131
|
+
if (cookie.options.domain) {
|
|
132
|
+
cookieString += `; Domain=${cookie.options.domain}`;
|
|
133
|
+
}
|
|
134
|
+
if (cookie.options.secure) {
|
|
135
|
+
cookieString += `; Secure`;
|
|
136
|
+
}
|
|
137
|
+
if (cookie.options.httpOnly) {
|
|
138
|
+
cookieString += `; HttpOnly`;
|
|
139
|
+
}
|
|
140
|
+
if (cookie.options.sameSite) {
|
|
141
|
+
cookieString += `; SameSite=${cookie.options.sameSite}`;
|
|
142
|
+
}
|
|
143
|
+
return cookieString;
|
|
144
|
+
});
|
|
145
|
+
if (this.enableCors) {
|
|
146
|
+
head['Access-Control-Allow-Origin'] = '*';
|
|
147
|
+
}
|
|
148
|
+
res.writeHead(session.status ?? 200, head);
|
|
149
|
+
res.end(session.body);
|
|
125
150
|
}
|
|
126
|
-
res.writeHead(session.status ?? 200, head);
|
|
127
|
-
res.end(session.body);
|
|
128
151
|
}
|
|
129
152
|
}
|
|
130
153
|
else if (rootroute && rootroute.allowedMethods.includes(req.method ?? 'GET')) {
|
|
@@ -133,13 +156,36 @@ class Server {
|
|
|
133
156
|
this.serveStaticFile(pathname, res);
|
|
134
157
|
}
|
|
135
158
|
else {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
159
|
+
if (!session.responseHandled) {
|
|
160
|
+
let head = session.head;
|
|
161
|
+
head['Set-Cookie'] = Object.entries(session.newCookie).map(([name, cookie]) => {
|
|
162
|
+
let cookieString = `${name}=${cookie.value}`;
|
|
163
|
+
if (cookie.options.expires) {
|
|
164
|
+
cookieString += `; Expires=${cookie.options.expires.toUTCString()}`;
|
|
165
|
+
}
|
|
166
|
+
if (cookie.options.path) {
|
|
167
|
+
cookieString += `; Path=${cookie.options.path}`;
|
|
168
|
+
}
|
|
169
|
+
if (cookie.options.domain) {
|
|
170
|
+
cookieString += `; Domain=${cookie.options.domain}`;
|
|
171
|
+
}
|
|
172
|
+
if (cookie.options.secure) {
|
|
173
|
+
cookieString += `; Secure`;
|
|
174
|
+
}
|
|
175
|
+
if (cookie.options.httpOnly) {
|
|
176
|
+
cookieString += `; HttpOnly`;
|
|
177
|
+
}
|
|
178
|
+
if (cookie.options.sameSite) {
|
|
179
|
+
cookieString += `; SameSite=${cookie.options.sameSite}`;
|
|
180
|
+
}
|
|
181
|
+
return cookieString;
|
|
182
|
+
});
|
|
183
|
+
if (this.enableCors) {
|
|
184
|
+
head['Access-Control-Allow-Origin'] = '*';
|
|
185
|
+
}
|
|
186
|
+
res.writeHead(session.status ?? 200, head);
|
|
187
|
+
res.end(session.body);
|
|
140
188
|
}
|
|
141
|
-
res.writeHead(session.status ?? 200, head);
|
|
142
|
-
res.end(session.body);
|
|
143
189
|
}
|
|
144
190
|
}
|
|
145
191
|
else {
|
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;
|
|
@@ -27,6 +38,7 @@ export declare class Session {
|
|
|
27
38
|
protocol: string;
|
|
28
39
|
pathname: string;
|
|
29
40
|
languages: string[];
|
|
41
|
+
responseHandled: boolean;
|
|
30
42
|
/**
|
|
31
43
|
* @constructor
|
|
32
44
|
* @param ip 用户IP
|
|
@@ -34,6 +46,7 @@ export declare class Session {
|
|
|
34
46
|
* @param query 请求字符串
|
|
35
47
|
*/
|
|
36
48
|
constructor(ip: string, cookie: Record<string, string>, server: Server, req?: IncomingMessage, res?: ServerResponse, pathname?: string, query?: Record<string, string>);
|
|
49
|
+
setCookie(name: string, value: string, options?: CookieOptions): void;
|
|
37
50
|
/**
|
|
38
51
|
* 解析 Accept-Language 字符串为排序后的语言数组
|
|
39
52
|
* @param header Accept-Language 头,比如 "zh-CN,zh-TW;q=0.8,en-US;q=0.6"
|
package/dist/session.js
CHANGED
|
@@ -60,6 +60,7 @@ class Session {
|
|
|
60
60
|
protocol = 'http';
|
|
61
61
|
pathname;
|
|
62
62
|
languages;
|
|
63
|
+
responseHandled = false;
|
|
63
64
|
/**
|
|
64
65
|
* @constructor
|
|
65
66
|
* @param ip 用户IP
|
|
@@ -82,16 +83,22 @@ class Session {
|
|
|
82
83
|
}
|
|
83
84
|
else {
|
|
84
85
|
this.sessionid = this.generateId(this.ip);
|
|
85
|
-
this.
|
|
86
|
+
this.setCookie('sessionid', this.sessionid);
|
|
86
87
|
}
|
|
87
88
|
if (cookie.lang) {
|
|
88
89
|
this.languages = cookie.lang.split(',');
|
|
89
90
|
}
|
|
90
91
|
else if (req.headers['accept-language']) {
|
|
91
92
|
this.languages = this.parseAcceptLanguages(req.headers['accept-language']);
|
|
92
|
-
this.
|
|
93
|
+
this.setCookie('lang', this.languages.join(','));
|
|
93
94
|
}
|
|
94
95
|
}
|
|
96
|
+
setCookie(name, value, options = {}) {
|
|
97
|
+
if (options.path === undefined) {
|
|
98
|
+
options.path = '/';
|
|
99
|
+
}
|
|
100
|
+
this.newCookie[name] = { value, options };
|
|
101
|
+
}
|
|
95
102
|
/**
|
|
96
103
|
* 解析 Accept-Language 字符串为排序后的语言数组
|
|
97
104
|
* @param header Accept-Language 头,比如 "zh-CN,zh-TW;q=0.8,en-US;q=0.6"
|