@yumerijs/core 2.0.6 → 2.0.8
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/session.d.ts +12 -0
- package/dist/session.js +51 -1
- package/package.json +1 -1
package/dist/session.d.ts
CHANGED
|
@@ -19,6 +19,12 @@ export interface CookieOptions {
|
|
|
19
19
|
httpOnly?: boolean;
|
|
20
20
|
sameSite?: 'Strict' | 'Lax' | 'None';
|
|
21
21
|
}
|
|
22
|
+
export interface CacheOptions {
|
|
23
|
+
etag?: string;
|
|
24
|
+
modified?: Date;
|
|
25
|
+
maxAge?: number;
|
|
26
|
+
expires?: Date;
|
|
27
|
+
}
|
|
22
28
|
export declare class Session {
|
|
23
29
|
ip: string;
|
|
24
30
|
cookie: Record<string, string>;
|
|
@@ -84,5 +90,11 @@ export declare class Session {
|
|
|
84
90
|
* @param name 文本点名称
|
|
85
91
|
*/
|
|
86
92
|
text(name: string): any;
|
|
93
|
+
/**
|
|
94
|
+
* 设置缓存标头
|
|
95
|
+
* @param option 选项
|
|
96
|
+
*/
|
|
97
|
+
setCache(option: CacheOptions): void;
|
|
98
|
+
static(path: string, option: CacheOptions): void;
|
|
87
99
|
}
|
|
88
100
|
export {};
|
package/dist/session.js
CHANGED
|
@@ -44,6 +44,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
44
44
|
exports.Session = void 0;
|
|
45
45
|
const crypto_1 = __importDefault(require("crypto"));
|
|
46
46
|
const formidable = __importStar(require("formidable"));
|
|
47
|
+
const fs_1 = __importDefault(require("fs"));
|
|
47
48
|
class Session {
|
|
48
49
|
ip;
|
|
49
50
|
cookie;
|
|
@@ -73,9 +74,23 @@ class Session {
|
|
|
73
74
|
this.query = query;
|
|
74
75
|
this.server = server;
|
|
75
76
|
this.pathname = pathname;
|
|
77
|
+
let header = {};
|
|
78
|
+
for (let key in req.headers) {
|
|
79
|
+
const value = req.headers[key];
|
|
80
|
+
if (typeof value === 'string') {
|
|
81
|
+
header[key] = value;
|
|
82
|
+
}
|
|
83
|
+
else if (Array.isArray(value)) {
|
|
84
|
+
header[key] = value.join(', ');
|
|
85
|
+
}
|
|
86
|
+
else if (value !== undefined) {
|
|
87
|
+
header[key] = String(value);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
76
90
|
this.client = {
|
|
77
91
|
req: req,
|
|
78
|
-
res: res
|
|
92
|
+
res: res,
|
|
93
|
+
headers: header
|
|
79
94
|
};
|
|
80
95
|
this.head['Content-Type'] = 'text/plain';
|
|
81
96
|
if (cookie.sessionid) {
|
|
@@ -262,6 +277,7 @@ class Session {
|
|
|
262
277
|
return this.client.res.write(data);
|
|
263
278
|
}
|
|
264
279
|
endsession(message) {
|
|
280
|
+
this.responseHandled = true;
|
|
265
281
|
return this.client.res.end(message);
|
|
266
282
|
}
|
|
267
283
|
/**
|
|
@@ -271,5 +287,39 @@ class Session {
|
|
|
271
287
|
text(name) {
|
|
272
288
|
return this.server.core.i18n.get(name, this.languages);
|
|
273
289
|
}
|
|
290
|
+
/**
|
|
291
|
+
* 设置缓存标头
|
|
292
|
+
* @param option 选项
|
|
293
|
+
*/
|
|
294
|
+
setCache(option) {
|
|
295
|
+
const headers = [];
|
|
296
|
+
if (option.etag)
|
|
297
|
+
headers.push(`ETag: "${option.etag}"`);
|
|
298
|
+
if (option.modified)
|
|
299
|
+
headers.push(`Last-Modified: ${option.modified.toUTCString()}`);
|
|
300
|
+
if (option.maxAge !== undefined)
|
|
301
|
+
headers.push(`Cache-Control: max-age=${option.maxAge}`);
|
|
302
|
+
if (option.expires)
|
|
303
|
+
headers.push(`Expires: ${option.expires.toUTCString()}`);
|
|
304
|
+
this.head['Cache-Control'] = headers.join('\n');
|
|
305
|
+
}
|
|
306
|
+
static(path, option) {
|
|
307
|
+
// 先查看用户是否在询问文件修改情况
|
|
308
|
+
if (this.client.headers['If-Modified-Since']) {
|
|
309
|
+
const modified = new Date(this.client.headers['If-Modified-Since']);
|
|
310
|
+
if (modified.getTime() === option.modified.getTime()) {
|
|
311
|
+
this.status = 304;
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
if (this.client.headers['If-None-Match']) {
|
|
316
|
+
if (this.client.headers['If-None-Match'] === option.etag) {
|
|
317
|
+
this.status = 304;
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
this.setCache(option);
|
|
322
|
+
this.body = fs_1.default.readFileSync(path);
|
|
323
|
+
}
|
|
274
324
|
}
|
|
275
325
|
exports.Session = Session;
|