Add dist
This commit is contained in:
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
import { readFileSync } from "fs";
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
import grabCacheNames from "./grab-cache-names";
|
||||
import path from "path";
|
||||
export default function getCache({ key, paradigm }) {
|
||||
try {
|
||||
const { BUNEXT_CACHE_DIR } = grabDirNames();
|
||||
const { cache_name } = grabCacheNames({ key, paradigm });
|
||||
const content = readFileSync(path.join(BUNEXT_CACHE_DIR, cache_name), "utf-8");
|
||||
return content;
|
||||
}
|
||||
catch (error) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
export default function grabCacheNames({ key, paradigm = "html" }) {
|
||||
const parsed_key = encodeURIComponent(key);
|
||||
const cache_name = `${parsed_key}.res.${paradigm}`;
|
||||
const cache_meta_name = `${parsed_key}.meta.json`;
|
||||
return { cache_name, cache_meta_name };
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
import { readdirSync } from "fs";
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
import trimCacheKey from "./trim-cache-key";
|
||||
export default async function trimAllCache() {
|
||||
try {
|
||||
const { BUNEXT_CACHE_DIR } = grabDirNames();
|
||||
const cached_items = readdirSync(BUNEXT_CACHE_DIR);
|
||||
for (let i = 0; i < cached_items.length; i++) {
|
||||
const cached_item = cached_items[i];
|
||||
if (!cached_item.endsWith(`.meta.json`))
|
||||
continue;
|
||||
const cache_key = decodeURIComponent(cached_item.replace(/\.meta\.json/, ""));
|
||||
const trim_key = await trimCacheKey({
|
||||
key: cache_key,
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
import { readFileSync, unlinkSync } from "fs";
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
import grabCacheNames from "./grab-cache-names";
|
||||
import path from "path";
|
||||
import { AppData } from "../../data/app-data";
|
||||
export default async function trimCacheKey({ key, }) {
|
||||
try {
|
||||
const { BUNEXT_CACHE_DIR } = grabDirNames();
|
||||
const { cache_name, cache_meta_name } = grabCacheNames({
|
||||
key,
|
||||
});
|
||||
const config = global.CONFIG;
|
||||
const default_expiry_time_seconds = config.defaultCacheExpiry ||
|
||||
AppData["DefaultCacheExpiryTimeSeconds"];
|
||||
const default_expiry_time_milliseconds = default_expiry_time_seconds * 1000;
|
||||
const cache_content_path = path.join(BUNEXT_CACHE_DIR, cache_name);
|
||||
const cache_meta_path = path.join(BUNEXT_CACHE_DIR, cache_meta_name);
|
||||
const cache_meta = JSON.parse(readFileSync(cache_meta_path, "utf-8"));
|
||||
const expiry_milliseconds = cache_meta.expiry_seconds
|
||||
? cache_meta.expiry_seconds * 1000
|
||||
: default_expiry_time_milliseconds;
|
||||
if (Date.now() - cache_meta.date_created < expiry_milliseconds) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Cache has not expired yet`,
|
||||
};
|
||||
}
|
||||
unlinkSync(cache_content_path);
|
||||
unlinkSync(cache_meta_path);
|
||||
return {
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Trim cache key ERROR: ${error.message}`,
|
||||
};
|
||||
}
|
||||
}
|
||||
Vendored
+38
@@ -0,0 +1,38 @@
|
||||
import { existsSync, writeFileSync } from "fs";
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
import grabCacheNames from "./grab-cache-names";
|
||||
import path from "path";
|
||||
export default async function writeCache({ key, value, paradigm = "html", expiry_seconds, }) {
|
||||
try {
|
||||
const { BUNEXT_CACHE_DIR } = grabDirNames();
|
||||
const { cache_meta_name, cache_name } = grabCacheNames({
|
||||
key,
|
||||
paradigm,
|
||||
});
|
||||
const target_path = path.join(BUNEXT_CACHE_DIR, cache_name);
|
||||
if (existsSync(target_path)) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Cache entry already exists`,
|
||||
};
|
||||
}
|
||||
writeFileSync(path.join(target_path), value);
|
||||
const cache_file_meta = {
|
||||
date_created: Date.now(),
|
||||
paradigm,
|
||||
};
|
||||
if (expiry_seconds) {
|
||||
cache_file_meta.expiry_seconds = expiry_seconds;
|
||||
}
|
||||
writeFileSync(path.join(BUNEXT_CACHE_DIR, cache_meta_name), JSON.stringify(cache_file_meta));
|
||||
return {
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
msg: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user