@social-mail/social-mail-web-server 1.7.310 → 1.7.312

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@social-mail/social-mail-web-server",
3
- "version": "1.7.310",
3
+ "version": "1.7.312",
4
4
  "description": "## Phase 1",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -19,7 +19,14 @@ export class ObjectCache<T> {
19
19
  return tc;
20
20
  }
21
21
 
22
- clear(name: keyof T) {
22
+ clear(name?: keyof T) {
23
+ if (!name) {
24
+ for (const element of this.cache.values()) {
25
+ element.clear();
26
+ }
27
+ this.cache.clear();
28
+ return;
29
+ }
23
30
  this.cache.get(name)?.clear();
24
31
  }
25
32
  };
@@ -4,7 +4,7 @@ import { join } from "path";
4
4
  import BaseDiskCache from "../../../common/BaseDiskCache.js";
5
5
  import { globalEnv } from "../../../common/globalEnv.js";
6
6
  import MessagingService from "../message-events/MessagingService.js";
7
- import { readdir } from "fs/promises";
7
+ import { ObjectCache } from "../../../common/cacheFor.js";
8
8
 
9
9
  @RegisterSingleton
10
10
  export default class SiteDiskContentCache extends ServiceObject {
@@ -12,26 +12,16 @@ export default class SiteDiskContentCache extends ServiceObject {
12
12
  @Inject
13
13
  ms: MessagingService;
14
14
 
15
- htmlFileCache = new BaseDiskCache({
16
- root: join(globalEnv.tmpDir, "site-cache", "html-files"),
15
+ cache: ObjectCache<any> = new ObjectCache();
16
+
17
+ htmlCache = new BaseDiskCache({
18
+ root: join(globalEnv.tmpDir, "site-html-cache", "html"),
17
19
  keepTTLSeconds: 24*60*60
18
20
  });
19
21
 
20
22
  postInit() {
21
23
  this.ms.snapshotRefresh.on(({ folderID }) => {
22
-
23
-
24
- if (folderID === 0) {
25
- // we need to refresh everything...
26
- this.htmlFileCache.clear().catch(console.error);
27
- return;
28
-
29
- }
30
-
31
- // at zero all host to snapshot map will be stored
32
- this.htmlFileCache.clearFolder("0").catch(console.error);
33
- this.htmlFileCache.clearFolder(folderID.toString()).catch(console.error);
34
-
24
+ this.cache.clear();
35
25
  });
36
26
  }
37
27
 
@@ -12,15 +12,20 @@ import TempFileService from "../../storage/TempFileService.js";
12
12
  import MessagingService from "../message-events/MessagingService.js";
13
13
  import SiteDiskContentCache from "./SiteDiskContentCache.js";
14
14
  import SocialMailContext from "../../model/SocialMailContext.js";
15
+ import { cacheFor, ICacheContainer, ObjectCache } from "../../../common/cacheFor.js";
15
16
 
16
17
  @RegisterScoped
17
- export default class WebSiteContentService {
18
+ export default class WebSiteContentService
19
+ extends ServiceObject
20
+ implements ICacheContainer<WebSiteContentService> {
18
21
 
19
22
  @Inject
20
23
  dbConfig: DBConfig;
21
24
 
22
25
  @Inject
23
- cache: SiteDiskContentCache;
26
+ siteCache: SiteDiskContentCache;
27
+
28
+ cache: ObjectCache<WebSiteContentService>;
24
29
 
25
30
  @Inject
26
31
  tfs: TempFileService;
@@ -28,32 +33,34 @@ export default class WebSiteContentService {
28
33
  @Inject
29
34
  db: SocialMailContext;
30
35
 
36
+ postInit() {
37
+ this.cache = this.siteCache.cache;
38
+ }
39
+
40
+ @cacheFor()
31
41
  async getCurrentSnapshotFolder(host) {
32
- return this.cache.htmlFileCache.getOrCreateJsonAsync<{ snapshotFolderID }>(`0/snapshot/${host}`, async () => {
33
- const file = await this.db.appFiles
34
- .where({ host }, (p) =>
35
- (x) => x.currentVersionWebSites
36
- .some((w) => w.hosts
37
- .some((h) => h.host === p.host)))
38
- .first();
39
- if (!file) {
40
- return null;
41
- }
42
- const { appFileID } = file;
43
- return { snapshotFolderID: appFileID };
44
- });
42
+ const file = await this.db.appFiles
43
+ .where({ host }, (p) =>
44
+ (x) => x.currentVersionWebSites
45
+ .some((w) => w.hosts
46
+ .some((h) => h.host === p.host)))
47
+ .first();
48
+ if (!file) {
49
+ return null;
50
+ }
51
+ const { appFileID } = file;
52
+ return { snapshotFolderID: appFileID };
45
53
  }
46
54
 
55
+ @cacheFor()
47
56
  async getWebSite(host) {
48
- return this.cache.htmlFileCache.getOrCreateJsonAsync(`0/${host}`, async () => {
49
- const folder = await this.db.websiteFolders.where({ host }, (p) => (x) => x.hosts.some((s) => s.host === p.host))
50
- .first();
51
- if (!folder) {
52
- return null;
53
- }
54
- const { folderID, currency, fromEmail, supportEmail } = folder;
55
- return { folderID, currency, fromEmail, supportEmail };
56
- });
57
+ const folder = await this.db.websiteFolders.where({ host }, (p) => (x) => x.hosts.some((s) => s.host === p.host))
58
+ .first();
59
+ if (!folder) {
60
+ return null;
61
+ }
62
+ const { folderID, currency, fromEmail, supportEmail } = folder;
63
+ return { folderID, currency, fromEmail, supportEmail };
57
64
  }
58
65
 
59
66
  async getFile(folderID, ... path: string[]) {
@@ -64,22 +71,20 @@ export default class WebSiteContentService {
64
71
  return this.tfs.downloadFileContent(fileInfo.fileContentID, fileInfo);
65
72
  }
66
73
 
74
+ @cacheFor()
67
75
  async getFileInfo(folderID, ... path: string[]): Promise<{ name, contentType, appFileID, url, fileContentID }> {
68
- return await this.cache.htmlFileCache.getOrCreateJsonAsync<{ name, contentType, appFileID, url, fileContentID }>(join(folderID, ... path), async () => {
69
- const [top, ... paths] = path;
70
- const child = await this.db.appFiles
71
- .where({ folderID, top },(p) => (x) => x.parentID === p.folderID && x.name === p.top)
72
- .orderBy(void 0, (p) => (x) => x.isDeleted)
73
- .include((x) => x.fileContent)
74
- .first();
75
- if (!child) {
76
- return null;
77
- }
78
- if (paths.length === 0) {
79
- return { name: child.name, contentType: child.contentType, appFileID: child.appFileID, url: child.url, fileContentID: child.fileContentID };
80
- }
81
- return await this.getFileInfo(child.appFileID, ... paths);
82
- });
76
+ const [top, ... paths] = path;
77
+ const child = await this.db.appFiles
78
+ .where({ folderID, top },(p) => (x) => x.parentID === p.folderID && x.name === p.top)
79
+ .orderBy(void 0, (p) => (x) => x.isDeleted)
80
+ .first();
81
+ if (!child) {
82
+ return null;
83
+ }
84
+ if (paths.length === 0) {
85
+ return { name: child.name, contentType: child.contentType, appFileID: child.appFileID, url: child.url, fileContentID: child.fileContentID };
86
+ }
87
+ return await this.getFileInfo(child.appFileID, ... paths);
83
88
  }
84
89
 
85
90
  async getSnapshotFile({ snapshotFolderID }, path: string[]) {
@@ -105,14 +110,14 @@ export default class WebSiteContentService {
105
110
  return file;
106
111
  }
107
112
  const htmlFilePath = join(folderID.toString(), "transformed", ... path);
108
- return this.cache.htmlFileCache.getOrCreateAsync(htmlFilePath, async (cf) => {
113
+ return this.siteCache.htmlCache.getOrCreateAsync(htmlFilePath, async (cf) => {
109
114
  await this.transformHtml(file, cf);
110
115
  });
111
116
  }
112
117
 
113
118
  async transformHtml(src: LocalFile, dest: LocalFile) {
114
119
 
115
- const packageReplacements = await this.cache.htmlFileCache.getOrCreateJsonAsync("packages", async () => {
120
+ const packageReplacements = await this.siteCache.htmlCache.getOrCreateJsonAsync("packages", async () => {
116
121
  const config = this.dbConfig;
117
122
 
118
123
  const packages = [