@yumerijs/core 2.0.8 → 2.0.10
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 +11 -1
- package/dist/session.js +42 -9
- package/package.json +1 -1
package/dist/session.d.ts
CHANGED
|
@@ -23,7 +23,16 @@ export interface CacheOptions {
|
|
|
23
23
|
etag?: string;
|
|
24
24
|
modified?: Date;
|
|
25
25
|
maxAge?: number;
|
|
26
|
+
smaxAge?: number;
|
|
27
|
+
isImmutable?: boolean;
|
|
26
28
|
expires?: Date;
|
|
29
|
+
cacheControl?: 'public' | 'private' | 'no-cache' | 'no-store' | 'must-revalidate' | 'proxy-revalidate';
|
|
30
|
+
}
|
|
31
|
+
export interface StaticCacheOptions {
|
|
32
|
+
etag?: boolean;
|
|
33
|
+
maxAge?: number;
|
|
34
|
+
smaxAge?: number;
|
|
35
|
+
etagType?: 'md5' | 'sha1' | 'sha256' | 'sha512';
|
|
27
36
|
}
|
|
28
37
|
export declare class Session {
|
|
29
38
|
ip: string;
|
|
@@ -95,6 +104,7 @@ export declare class Session {
|
|
|
95
104
|
* @param option 选项
|
|
96
105
|
*/
|
|
97
106
|
setCache(option: CacheOptions): void;
|
|
98
|
-
static(
|
|
107
|
+
static(content: string, option: CacheOptions): void;
|
|
108
|
+
file(path: string, option: StaticCacheOptions): void;
|
|
99
109
|
}
|
|
100
110
|
export {};
|
package/dist/session.js
CHANGED
|
@@ -292,22 +292,29 @@ class Session {
|
|
|
292
292
|
* @param option 选项
|
|
293
293
|
*/
|
|
294
294
|
setCache(option) {
|
|
295
|
-
const headers = [];
|
|
296
295
|
if (option.etag)
|
|
297
|
-
|
|
296
|
+
this.head['ETag'] = `"${option.etag}"`;
|
|
298
297
|
if (option.modified)
|
|
299
|
-
|
|
298
|
+
this.head['Last-Modified'] = option.modified.toUTCString();
|
|
299
|
+
let control = [];
|
|
300
|
+
if (option.cacheControl !== undefined)
|
|
301
|
+
control.push(option.cacheControl);
|
|
300
302
|
if (option.maxAge !== undefined)
|
|
301
|
-
|
|
303
|
+
control.push[`max-age=${option.maxAge}`];
|
|
304
|
+
if (option.smaxAge !== undefined)
|
|
305
|
+
control.push(`s-maxage=${option.smaxAge}`);
|
|
306
|
+
if (option.isImmutable)
|
|
307
|
+
control.push('immutable');
|
|
308
|
+
if (control.length > 0)
|
|
309
|
+
this.head['Cache-Control'] = control.join(', ');
|
|
302
310
|
if (option.expires)
|
|
303
|
-
|
|
304
|
-
this.head['Cache-Control'] = headers.join('\n');
|
|
311
|
+
this.head['Expires'] = option.expires.toUTCString();
|
|
305
312
|
}
|
|
306
|
-
static(
|
|
313
|
+
static(content, option) {
|
|
307
314
|
// 先查看用户是否在询问文件修改情况
|
|
308
315
|
if (this.client.headers['If-Modified-Since']) {
|
|
309
316
|
const modified = new Date(this.client.headers['If-Modified-Since']);
|
|
310
|
-
if (modified.getTime() === option.modified
|
|
317
|
+
if (modified.getTime() === option.modified?.getTime()) {
|
|
311
318
|
this.status = 304;
|
|
312
319
|
return;
|
|
313
320
|
}
|
|
@@ -319,7 +326,33 @@ class Session {
|
|
|
319
326
|
}
|
|
320
327
|
}
|
|
321
328
|
this.setCache(option);
|
|
322
|
-
this.body =
|
|
329
|
+
this.body = content;
|
|
330
|
+
}
|
|
331
|
+
file(path, option) {
|
|
332
|
+
if (this.client.headers['If-Modified-Since']) {
|
|
333
|
+
const modified = new Date(this.client.headers['If-Modified-Since']);
|
|
334
|
+
const moditime = fs_1.default.statSync(path).mtime;
|
|
335
|
+
if (modified.getTime() === moditime.getTime()) {
|
|
336
|
+
this.status = 304;
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
const etag = crypto_1.default.createHash(option.etagType || 'md5').update(fs_1.default.readFileSync(path)).digest('hex');
|
|
341
|
+
if (this.client.headers['If-None-Match']) {
|
|
342
|
+
if (option.etag) {
|
|
343
|
+
if (this.client.headers['If-None-Match'] === etag) {
|
|
344
|
+
this.status = 304;
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
this.setCache({
|
|
350
|
+
modified: fs_1.default.statSync(path).mtime,
|
|
351
|
+
etag,
|
|
352
|
+
maxAge: option.maxAge,
|
|
353
|
+
smaxAge: option.smaxAge
|
|
354
|
+
});
|
|
355
|
+
this.body = fs_1.default.readFileSync(path, 'utf-8');
|
|
323
356
|
}
|
|
324
357
|
}
|
|
325
358
|
exports.Session = Session;
|