astro-accelerator-utils 0.1.0 → 0.1.1
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 +3 -1
- package/index.mjs +3 -0
- package/lib/cache.d.mts +30 -0
- package/lib/cache.mjs +76 -0
- package/lib/footerMenu.d.mts +2 -2
- package/lib/footerMenu.mjs +1 -1
- package/lib/v1/urls.d.mts +2 -2
- package/lib/v1/urls.mjs +1 -1
- package/package.json +1 -1
package/index.d.mts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { DateFormatter } from "./lib/v1/dates.mjs";
|
|
2
2
|
import { UrlFormatter } from "./lib/v1/urls.mjs";
|
|
3
|
+
import { Posts } from "./lib/v1/posts.mjs";
|
|
4
|
+
import * as Cache from "./lib/cache.mjs";
|
|
3
5
|
import * as PostQueries from "./lib/postQueries.mjs";
|
|
4
6
|
import * as PostFiltering from "./lib/postFiltering.mjs";
|
|
5
7
|
import * as PostOrdering from "./lib/postOrdering.mjs";
|
|
@@ -8,4 +10,4 @@ import * as FooterMenu from "./lib/footerMenu.mjs";
|
|
|
8
10
|
import * as Markdown from "./lib/markdown.mjs";
|
|
9
11
|
import * as Navigation from "./lib/navigation.mjs";
|
|
10
12
|
import * as Taxonomy from "./lib/taxonomy.mjs";
|
|
11
|
-
export { DateFormatter, UrlFormatter, PostQueries, PostFiltering, PostOrdering, PostPaging, FooterMenu, Markdown, Navigation, Taxonomy };
|
|
13
|
+
export { DateFormatter, UrlFormatter, Posts, Cache, PostQueries, PostFiltering, PostOrdering, PostPaging, FooterMenu, Markdown, Navigation, Taxonomy };
|
package/index.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import { DateFormatter } from './lib/v1/dates.mjs';
|
|
|
2
2
|
import { UrlFormatter } from './lib/v1/urls.mjs';
|
|
3
3
|
import { Posts } from './lib/v1/posts.mjs';
|
|
4
4
|
|
|
5
|
+
import * as Cache from './lib/cache.mjs';
|
|
5
6
|
import * as PostQueries from './lib/postQueries.mjs';
|
|
6
7
|
import * as PostFiltering from './lib/postFiltering.mjs';
|
|
7
8
|
import * as PostOrdering from './lib/postOrdering.mjs';
|
|
@@ -14,6 +15,8 @@ import * as Taxonomy from './lib/taxonomy.mjs';
|
|
|
14
15
|
export {
|
|
15
16
|
DateFormatter,
|
|
16
17
|
UrlFormatter,
|
|
18
|
+
Posts,
|
|
19
|
+
Cache,
|
|
17
20
|
PostQueries,
|
|
18
21
|
PostFiltering,
|
|
19
22
|
PostOrdering,
|
package/lib/cache.d.mts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
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
|
+
}
|
package/lib/footerMenu.d.mts
CHANGED
|
@@ -12,9 +12,9 @@
|
|
|
12
12
|
* @param {any} translations
|
|
13
13
|
* @param {Site} site
|
|
14
14
|
* @param {(NavPage | 'categories' | 'tags' | 'toptags')[]} menu
|
|
15
|
-
* @returns {NavPage[]}
|
|
15
|
+
* @returns {Promise<NavPage[]>}
|
|
16
16
|
*/
|
|
17
|
-
export function getMenu(currentUrl: URL, _: TranslationProvider, translations: any, site: any, menu: (NavPage | 'categories' | 'tags' | 'toptags')[]): NavPage[]
|
|
17
|
+
export function getMenu(currentUrl: URL, _: TranslationProvider, translations: any, site: any, menu: (NavPage | 'categories' | 'tags' | 'toptags')[]): Promise<NavPage[]>;
|
|
18
18
|
/**
|
|
19
19
|
*
|
|
20
20
|
* @param {TaxonomyLinks} links
|
package/lib/footerMenu.mjs
CHANGED
|
@@ -17,7 +17,7 @@ import * as PostQueries from './postQueries.mjs';
|
|
|
17
17
|
* @param {any} translations
|
|
18
18
|
* @param {Site} site
|
|
19
19
|
* @param {(NavPage | 'categories' | 'tags' | 'toptags')[]} menu
|
|
20
|
-
* @returns {NavPage[]}
|
|
20
|
+
* @returns {Promise<NavPage[]>}
|
|
21
21
|
*/
|
|
22
22
|
export async function getMenu (currentUrl, _, translations, site, menu) {
|
|
23
23
|
const links = Taxonomy.taxonomyLinks(translations, _, site);
|
package/lib/v1/urls.d.mts
CHANGED
|
@@ -13,8 +13,8 @@ export class UrlFormatter {
|
|
|
13
13
|
addSlashToUrl(url: URL): URL;
|
|
14
14
|
/**
|
|
15
15
|
* Ensures trailing slash is used
|
|
16
|
-
* @param {string |
|
|
16
|
+
* @param {string | undefined} address
|
|
17
17
|
* @returns {string}
|
|
18
18
|
*/
|
|
19
|
-
addSlashToAddress(address: string |
|
|
19
|
+
addSlashToAddress(address: string | undefined): string;
|
|
20
20
|
}
|
package/lib/v1/urls.mjs
CHANGED