URL objects are being annoying, just store strings and make a URL if portions are needed

This commit is contained in:
Jack Case
2025-12-12 13:46:26 +00:00
parent 7fbfa2a773
commit 55032fe47a
2 changed files with 15 additions and 9 deletions

View File

@@ -1,6 +1,8 @@
import { SlopDB, CheckCache } from "../scripts/indexed-db.js"
import { openDB, deleteDB } from "../scripts/idb/index.js"
const MAX_TIMESTAMP_DIFFERENCE = 3
describe("sanity check", () => {
it("works", () => {
expect(true).toBeTrue()
@@ -58,13 +60,16 @@ describe("SlopDB", () => {
const slop_url = new URL("https://sloppy-slop.com/sloparticle")
await cache.store(slop_url.host)
await cache.store(slop_url.toString())
const store_time = Date.now()
const cached_item = cache.get(slop_url.host)
const cached_item = await cache.get(slop_url.toString())
expect(cached_item.url).toEqual(slop_url)
expect(cached_item.check_timestamp).toBeCloseTo(store_time)
expect(cached_item.url).toEqual(slop_url.toString())
expect(cached_item.check_timestamp).toBeLessThanOrEqual(store_time)
expect(cached_item.check_timestamp).toBeGreaterThan(store_time - MAX_TIMESTAMP_DIFFERENCE)
})
it("updates a cached url's timestamp when it is accessed")
})

View File

@@ -44,7 +44,7 @@ export class CheckCache {
this.cache_capacity = max_entries
}
cache_item_factory(url: URL) {
cache_item_factory(url: string) {
return {
url: url,
check_timestamp: Date.now()
@@ -53,11 +53,12 @@ export class CheckCache {
async store(url: string) {
const cache_store = this.slopdb.db.transaction(CheckCache.cache_objectstore_name, "readwrite").objectStore(CheckCache.cache_objectstore_name)
await cache_store.add(this.cache_item_factory(new URL(url)))
await cache_store.add(this.cache_item_factory(url))
}
get(url: URL) {
return url
async get(url: string) {
const cache_store = this.slopdb.db.transaction(CheckCache.cache_objectstore_name, "readwrite").objectStore(CheckCache.cache_objectstore_name)
return cache_store.get(url)
}
// async evict_least_recently_checked(count: number) {
@@ -102,7 +103,7 @@ export class SlopDB {
db.createObjectStore("slop", { keyPath: "domain" })
break
case 2:
db.createObjectStore("checkcache", { keyPath: "domain" })
db.createObjectStore("checkcache", { keyPath: "url" })
break
}
}