astro-accelerator-utils 0.1.5 → 0.1.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/index.d.mts CHANGED
@@ -2,7 +2,7 @@ import { DateFormatter } from "./lib/v1/dates.mjs";
2
2
  import { Markdown } from "./lib/v1/markdown.mjs";
3
3
  import { Posts } from "./lib/v1/posts.mjs";
4
4
  import { UrlFormatter } from "./lib/v1/urls.mjs";
5
- import * as Cache from "./lib/cache.mjs";
5
+ import { Cache } from "./lib/v1/cache.mjs";
6
6
  import * as PostQueries from "./lib/postQueries.mjs";
7
7
  import * as PostFiltering from "./lib/postFiltering.mjs";
8
8
  import * as PostOrdering from "./lib/postOrdering.mjs";
package/index.mjs CHANGED
@@ -1,9 +1,9 @@
1
+ import { Cache } from './lib/v1/cache.mjs';
1
2
  import { DateFormatter } from './lib/v1/dates.mjs';
2
3
  import { Markdown } from './lib/v1/markdown.mjs';
3
4
  import { Posts } from './lib/v1/posts.mjs';
4
5
  import { UrlFormatter } from './lib/v1/urls.mjs';
5
6
 
6
- import * as Cache from './lib/cache.mjs';
7
7
  import * as PostQueries from './lib/postQueries.mjs';
8
8
  import * as PostFiltering from './lib/postFiltering.mjs';
9
9
  import * as PostOrdering from './lib/postOrdering.mjs';
@@ -1,7 +1,3 @@
1
- /**
2
- * @typedef { import("../types/Site").Site } Site
3
- * @typedef { import("../types/NavPage").NavPage } NavPage
4
- */
5
1
  /**
6
2
  *
7
3
  * @param {URL} currentUrl
@@ -1,13 +1,15 @@
1
1
  import { Posts } from './v1/posts.mjs';
2
+ import { Cache } from './v1/cache.mjs';
2
3
  import * as PostQueries from './postQueries.mjs';
3
4
  import * as PostFiltering from './postFiltering.mjs';
4
- import * as Cache from './cache.mjs';
5
5
 
6
6
  /**
7
7
  * @typedef { import("../types/Site").Site } Site
8
8
  * @typedef { import("../types/NavPage").NavPage } NavPage
9
9
  */
10
10
 
11
+ const cache = new Cache(200);
12
+
11
13
  /**
12
14
  *
13
15
  * @param {URL} currentUrl
@@ -19,7 +21,7 @@ export async function getMenu (currentUrl, site, menu) {
19
21
  const key = 'Navigation__getMenu';
20
22
 
21
23
  /** @type {NavPage[]} */
22
- let pages = await Cache.getItem(key);
24
+ let pages = cache.getItem(key);
23
25
 
24
26
  if (pages == null) {
25
27
  pages = [];
@@ -36,7 +38,7 @@ export async function getMenu (currentUrl, site, menu) {
36
38
  }
37
39
 
38
40
  // Cache the result
39
- await Cache.setItem(key, pages);
41
+ cache.setItem(key, pages);
40
42
  }
41
43
 
42
44
  PostQueries.setCurrentPage(pages, currentUrl);
@@ -70,7 +72,7 @@ export async function getNavigation (currentUrl, site) {
70
72
  const posts = new Posts();
71
73
 
72
74
  /** @type {NavPage[]} */
73
- let pageHierarchy = await Cache.getItem(key);
75
+ let pageHierarchy = cache.getItem(key);
74
76
 
75
77
  if (pageHierarchy == null) {
76
78
  const topLevelPages = posts.root(site.subfolder).filter(PostFiltering.showInMenu);
@@ -106,7 +108,7 @@ export async function getNavigation (currentUrl, site) {
106
108
  }
107
109
 
108
110
  // Cache the result
109
- await Cache.setItem(key, pageHierarchy);
111
+ cache.setItem(key, pageHierarchy);
110
112
  }
111
113
 
112
114
  return pageHierarchy;
@@ -1,10 +1,3 @@
1
- /**
2
- * @typedef { import("../types/Site").Site } Site
3
- * @typedef { import("../types/Taxonomy").Taxonomy } Taxonomy
4
- * @typedef { import("../types/Taxonomy").TaxonomyEntry } TaxonomyEntry
5
- * @typedef { import("../types/Taxonomy").TaxonomyLinks } TaxonomyLinks
6
- * @typedef { import("../types/Astro").MarkdownInstance } MarkdownInstance
7
- */
8
1
  /**
9
2
  *
10
3
  * @param {TaxonomyEntry} a
package/lib/taxonomy.mjs CHANGED
@@ -1,5 +1,5 @@
1
+ import { Cache } from './v1/cache.mjs';
1
2
  import { UrlFormatter } from './v1/urls.mjs';
2
- import * as Cache from './cache.mjs';
3
3
  import * as PostQueries from './postQueries.mjs';
4
4
  import * as PostFiltering from './postFiltering.mjs';
5
5
 
@@ -11,6 +11,8 @@ import * as PostFiltering from './postFiltering.mjs';
11
11
  * @typedef { import("../types/Astro").MarkdownInstance } MarkdownInstance
12
12
  */
13
13
 
14
+ const cache = new Cache(200);
15
+
14
16
  /**
15
17
  *
16
18
  * @param {TaxonomyEntry} a
@@ -64,7 +66,7 @@ export async function getTaxonomy () {
64
66
  const cacheKey = 'Global__taxonomy';
65
67
 
66
68
  /** @type {Taxonomy} */
67
- let taxonomy = await Cache.getItem(cacheKey);
69
+ let taxonomy = cache.getItem(cacheKey);
68
70
 
69
71
  if (taxonomy == null) {
70
72
  /** @type {MarkdownInstance[]} */
@@ -119,7 +121,7 @@ export async function getTaxonomy () {
119
121
  return 0;
120
122
  });
121
123
 
122
- await Cache.setItem(cacheKey, taxonomy);
124
+ cache.setItem(cacheKey, taxonomy);
123
125
  }
124
126
 
125
127
  return taxonomy;
@@ -0,0 +1,43 @@
1
+ export class Cache {
2
+ /**
3
+ * Constructor
4
+ * @param {number} maxAge
5
+ */
6
+ constructor(maxAge: number);
7
+ maxAge: number;
8
+ /**
9
+ * Gets an item from the cache, falls back to supplied function
10
+ * @param {string} key
11
+ * @param {() => any} func
12
+ */
13
+ get(key: string, func: () => any): any;
14
+ /**
15
+ * Gets an item from the cache
16
+ * @param {string} key
17
+ * @returns {Promise<any>}
18
+ */
19
+ getItem(key: string): Promise<any>;
20
+ /**
21
+ * Adds an item to the cache
22
+ * @param {string} key
23
+ * @param {any} value
24
+ * @returns {Promise<void>}
25
+ */
26
+ setItem(key: string, value: any): Promise<void>;
27
+ /**
28
+ * Clears the cache
29
+ * @returns {Promise<void>}
30
+ */
31
+ clear(): Promise<void>;
32
+ /**
33
+ * Get's the path of the cache files
34
+ * @returns {string}
35
+ */
36
+ getCachePath(): string;
37
+ /**
38
+ * Gets the file path for a cache item
39
+ * @param {string} key
40
+ * @returns {string}
41
+ */
42
+ getItemPath(key: string): string;
43
+ }
@@ -0,0 +1,100 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import process from 'process';
4
+
5
+ export class Cache {
6
+ /**
7
+ * Constructor
8
+ * @param {number} maxAge
9
+ */
10
+ constructor(maxAge) {
11
+ this.maxAge = maxAge;
12
+ }
13
+
14
+ /**
15
+ * Gets an item from the cache, falls back to supplied function
16
+ * @param {string} key
17
+ * @param {() => any} func
18
+ */
19
+ get(key, func) {
20
+ const cached = this.getItem(key);
21
+
22
+ if (cached) {
23
+ return cached;
24
+ }
25
+
26
+ const fetched = func();
27
+
28
+ this.setItem(key, fetched);
29
+
30
+ return fetched;
31
+ }
32
+
33
+ /**
34
+ * Gets an item from the cache
35
+ * @param {string} key
36
+ * @returns {Promise<any>}
37
+ */
38
+ getItem (key) {
39
+ const itemPath = this.getItemPath(key);
40
+ try {
41
+
42
+ const { mtime } = fs.statSync(itemPath);
43
+
44
+ var date_time = new Date();
45
+ let timeDifference = Math.abs((date_time.getTime() - mtime.getTime()) / 1000);
46
+ if (timeDifference < this.maxAge) {
47
+ console.log('Cache hit', key);
48
+ const content = fs.readFileSync(itemPath).toString();
49
+ return JSON.parse(content);
50
+ }
51
+ } catch{}
52
+
53
+ console.log('Cache miss', key);
54
+ return null;
55
+ }
56
+
57
+ /**
58
+ * Adds an item to the cache
59
+ * @param {string} key
60
+ * @param {any} value
61
+ * @returns {Promise<void>}
62
+ */
63
+ setItem (key, value) {
64
+ const itemPath = this.getItemPath(key);
65
+ fs.writeFileSync(itemPath, JSON.stringify(value));
66
+ }
67
+
68
+ /**
69
+ * Clears the cache
70
+ * @returns {Promise<void>}
71
+ */
72
+ clear() {
73
+ const folder = this.getCachePath();
74
+ const files = fs.readdirSync(folder);
75
+
76
+ for(const file of files) {
77
+ fs.unlinkSync(path.join(folder, file));
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Get's the path of the cache files
83
+ * @returns {string}
84
+ */
85
+ getCachePath () {
86
+ const cachePath = path.join(process.cwd(), '.cache/');
87
+ fs.mkdirSync(cachePath, { recursive: true })
88
+ return cachePath;
89
+ }
90
+
91
+ /**
92
+ * Gets the file path for a cache item
93
+ * @param {string} key
94
+ * @returns {string}
95
+ */
96
+ getItemPath (key) {
97
+ const cachePath = this.getCachePath();
98
+ return path.join(cachePath, key + '.json');
99
+ }
100
+ }
@@ -1,12 +1,15 @@
1
1
  /**
2
2
  * @typedef { import("../../types/Astro").MarkdownInstance } MarkdownInstance
3
+ * @typedef { import("./cache.mjs").Cache } Cache
3
4
  */
4
5
  export class Posts {
5
6
  /**
6
7
  * Constructor
8
+ * @param {Cache} cache
7
9
  * @param {() => MarkdownInstance[]} [fetchAll]
8
10
  */
9
- constructor(fetchAll?: () => MarkdownInstance[]);
11
+ constructor(cache: Cache, fetchAll?: () => MarkdownInstance[]);
12
+ cache: import("./cache.mjs").Cache;
10
13
  fetchAll: () => any;
11
14
  allPosts: any[];
12
15
  /**
@@ -21,3 +24,4 @@ export class Posts {
21
24
  root(subfolder: any): MarkdownInstance[];
22
25
  }
23
26
  export type MarkdownInstance = import("../../types/Astro").MarkdownInstance;
27
+ export type Cache = import("./cache.mjs").Cache;
package/lib/v1/posts.mjs CHANGED
@@ -1,14 +1,17 @@
1
1
  /**
2
2
  * @typedef { import("../../types/Astro").MarkdownInstance } MarkdownInstance
3
+ * @typedef { import("./cache.mjs").Cache } Cache
3
4
  */
4
5
 
5
6
  export class Posts {
6
7
  /**
7
8
  * Constructor
9
+ * @param {Cache} cache
8
10
  * @param {() => MarkdownInstance[]} [fetchAll]
9
11
  */
10
- constructor(fetchAll) {
12
+ constructor(cache, fetchAll) {
11
13
  /* istanbul ignore next */
14
+ this.cache = cache;
12
15
  this.fetchAll = fetchAll ?? function() { return import.meta.glob("/src/pages/**/*.md", { eager: true }); }
13
16
  this.allPosts = [];
14
17
  }
@@ -18,12 +21,12 @@ export class Posts {
18
21
  * @returns {MarkdownInstance[]}
19
22
  */
20
23
  all () {
21
- if (this.allPosts.length === 0) {
22
- const pageImportResult = this.fetchAll();
23
- this.allPosts = Object.values(pageImportResult);
24
- }
24
+ const key = 'cache-posts.all()';
25
25
 
26
- return this.allPosts;
26
+ return this.cache.get(key, () => {
27
+ const pageImportResult = this.fetchAll();
28
+ return Object.values(pageImportResult);
29
+ });
27
30
  }
28
31
 
29
32
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-accelerator-utils",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "Astro utilities for Astro Accelerator.",
5
5
  "main": "index.mjs",
6
6
  "type": "module",
package/lib/cache.d.mts DELETED
@@ -1,30 +0,0 @@
1
- /**
2
- * Get's the path of the cache files
3
- * @returns {Promise<string>}
4
- */
5
- export function getCachePath(): Promise<string>;
6
- /**
7
- * Gets the file path for a cache item
8
- * @param {string} key
9
- * @returns {Promise<string>}
10
- */
11
- export function getItemPath(key: string): Promise<string>;
12
- /**
13
- * Gets an item from the cache
14
- * @param {string} key
15
- * @param {number} [maxAgeInSeconds]
16
- * @returns {Promise<any>}
17
- */
18
- export function getItem(key: string, maxAgeInSeconds?: number): Promise<any>;
19
- /**
20
- * Adds an item to the cache
21
- * @param {string} key
22
- * @param {any} value
23
- * @returns {Promise<void>}
24
- */
25
- export function setItem(key: string, value: any): Promise<void>;
26
- /**
27
- * Clears the cache
28
- * @returns {Promise<void>}
29
- */
30
- export function clear(): Promise<void>;
package/lib/cache.mjs DELETED
@@ -1,76 +0,0 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import process from 'process';
4
-
5
- /**
6
- * Get's the path of the cache files
7
- * @returns {Promise<string>}
8
- */
9
- export async function getCachePath () {
10
- const cachePath = path.join(process.cwd(), '.cache/');
11
- await fs.promises.mkdir(cachePath, { recursive: true })
12
- return cachePath;
13
- }
14
-
15
- /**
16
- * Gets the file path for a cache item
17
- * @param {string} key
18
- * @returns {Promise<string>}
19
- */
20
- export async function getItemPath (key) {
21
- const cachePath = await getCachePath();
22
- return path.join(cachePath, key + '.json');
23
- }
24
-
25
- /**
26
- * Gets an item from the cache
27
- * @param {string} key
28
- * @param {number} [maxAgeInSeconds]
29
- * @returns {Promise<any>}
30
- */
31
- export async function getItem (key, maxAgeInSeconds) {
32
- if (maxAgeInSeconds == null) {
33
- maxAgeInSeconds = 200;
34
- }
35
-
36
- const itemPath = await getItemPath(key);
37
- try {
38
-
39
- const { mtime } = await fs.promises.stat(itemPath);
40
-
41
- var date_time = new Date();
42
- let timeDifference = Math.abs((date_time.getTime() - mtime.getTime()) / 1000);
43
- if (timeDifference < maxAgeInSeconds) {
44
- console.log('Cache hit', key);
45
- const content = fs.readFileSync(itemPath).toString();
46
- return JSON.parse(content);
47
- }
48
- } catch{}
49
-
50
- console.log('Cache miss', key);
51
- return null;
52
- }
53
-
54
- /**
55
- * Adds an item to the cache
56
- * @param {string} key
57
- * @param {any} value
58
- * @returns {Promise<void>}
59
- */
60
- export async function setItem (key, value) {
61
- const itemPath = await getItemPath(key);
62
- await fs.promises.writeFile(itemPath, JSON.stringify(value));
63
- }
64
-
65
- /**
66
- * Clears the cache
67
- * @returns {Promise<void>}
68
- */
69
- export async function clear() {
70
- const folder = await getCachePath();
71
- const files = fs.readdirSync(folder);
72
-
73
- for(const file of files) {
74
- fs.unlinkSync(path.join(folder, file));
75
- }
76
- }