-
Notifications
You must be signed in to change notification settings - Fork 13
/
mandarineResourceResolver.ts
76 lines (56 loc) · 2.67 KB
/
mandarineResourceResolver.ts
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2020-2020 The Mandarine.TS Framework authors. All rights reserved. MIT license.
import { Mandarine } from "../../Mandarine.ns.ts";
import { CommonUtils } from "../../utils/commonUtils.ts";
import { Leaf } from "../../../deps.ts";
const getCacheManager = () => {
return Mandarine.MandarineMVC.Internal.Core.getCacheManager();
}
export interface FileStats {
valid: boolean;
size: number;
mtime: Date | null;
}
/**
* This class represents the default resolver for resources.
* It is used by Mandarine to resolve static content.
* It can be overriden or you can create a personalized resolver with your own code.
* Refer to the documentation https://mandarineframework.gitbook.io/mandarine-ts/mandarine-core/resource-handlers/resource-resolver
*/
export class MandarineResourceResolver implements Mandarine.MandarineMVC.HTTPResolvers.ResourceResolver {
public async resolve(httpContext: Mandarine.Types.RequestContext, resourcePath: string): Promise<Uint8Array | undefined> {
const cacheManager = getCacheManager();
const cacheKey = `RESOURCE_${resourcePath}`;
let resourceExt = null;
if(resourcePath.includes(".")) {
let extensionsInResource = resourcePath.split(".");
resourceExt = extensionsInResource[extensionsInResource.length - 1];
}
const isCacheExistent = cacheManager.has(cacheKey);
if(!isCacheExistent) {
const [resourcePathExist, resource] = await CommonUtils.fileDirExistsAsync(resourcePath);
const fileStats: Partial<FileStats> = {
valid: resourcePathExist
};
if(resourcePathExist === true) {
fileStats.mtime = resource?.mtime;
fileStats.size = resource?.size;
}
cacheManager.add<FileStats>(cacheKey, <FileStats>fileStats);
}
const cache = cacheManager.get<FileStats>(cacheKey);
if(cache?.valid === true) {
httpContext.response.headers.set("Content-Length", (cache.size || 0).toString());
if (!httpContext.response.headers.has("Last-Modified") && cache.mtime) {
httpContext.response.headers.set("Last-Modified", cache.mtime.toUTCString());
}
if (!httpContext.response.headers.has("Cache-Control")) {
const directives = [`max-age=0`];
httpContext.response.headers.set("Cache-Control", directives.join(","));
}
if (!httpContext.response.type && resourceExt) {
httpContext.response.type = `.${resourceExt}`;
}
return Leaf.readFile(resourcePath);
}
}
}