2024-09-20 13:40:01 +00:00
|
|
|
import fsSyncOriginal from 'fs'
|
|
|
|
import fsPromisesOriginal from 'fs/promises'
|
|
|
|
import crypto from 'crypto'
|
|
|
|
import path from 'path'
|
|
|
|
import os from 'os'
|
|
|
|
|
|
|
|
export default class FSCache {
|
|
|
|
constructor(options = {}, fsSync, fsPromises) {
|
|
|
|
this.fsSync = fsSync || fsSyncOriginal
|
|
|
|
this.fsPromises = fsPromises || fsPromisesOriginal
|
|
|
|
|
|
|
|
this.id = crypto.randomBytes(15).toString('base64').replace(/\//g, '-')
|
2024-09-20 23:14:22 +00:00
|
|
|
this.prefix = options.prefix ? options.prefix + '-' : '-'
|
2024-09-20 13:40:01 +00:00
|
|
|
this.hash_alg = options.hash_alg || 'md5'
|
|
|
|
this.cache_dir = options.cache_dir || path.join(os.tmpdir(), this.id)
|
2024-09-20 23:14:22 +00:00
|
|
|
this.ttl = options.ttl || 0
|
2024-09-20 13:40:01 +00:00
|
|
|
|
|
|
|
// Verify hash algorithm is supported on this system
|
|
|
|
crypto.createHash(this.hash_alg)
|
|
|
|
|
|
|
|
this.fsSync.mkdirSync(this.cache_dir, { recursive: true })
|
|
|
|
}
|
|
|
|
|
2024-09-20 23:14:22 +00:00
|
|
|
_checkIsExpired(parsed, now) {
|
|
|
|
return parsed.ttl != null && now > parsed.ttl
|
2024-09-20 13:40:01 +00:00
|
|
|
}
|
|
|
|
|
2024-09-20 23:14:22 +00:00
|
|
|
_parseCacheData(data, fallback, overwrite = {}) {
|
|
|
|
let parsed = JSON.parse(data)
|
|
|
|
if (this._checkIsExpired(parsed, new Date().getTime())) {
|
|
|
|
return fallback || null
|
|
|
|
}
|
|
|
|
return parsed.content
|
|
|
|
}
|
|
|
|
|
|
|
|
_parseSetData(key, data, overwrite = {}) {
|
|
|
|
if (!(overwrite.ttl ?? this.ttl)) {
|
|
|
|
return JSON.stringify({ key: key, content: data })
|
|
|
|
}
|
|
|
|
return JSON.stringify({
|
|
|
|
key: key,
|
|
|
|
content: data,
|
|
|
|
ttl: new Date().getTime() + (overwrite.ttl || this.ttl) * 1000,
|
|
|
|
})
|
2024-09-20 13:40:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hash(name) {
|
2024-09-20 23:14:22 +00:00
|
|
|
return path.join(this.cache_dir, this.prefix + crypto.hash(this.hash_alg, name))
|
2024-09-20 13:40:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get(name, fallback, opts) {
|
2024-09-20 23:14:22 +00:00
|
|
|
return this.fsPromises.readFile(this.hash(name), { encoding: 'utf8' })
|
2024-09-20 13:40:01 +00:00
|
|
|
.then(
|
2024-09-20 23:14:22 +00:00
|
|
|
data => this._parseCacheData(data, fallback, opts),
|
2024-09-20 13:40:01 +00:00
|
|
|
err => (fallback)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
getSync(name, fallback, opts) {
|
|
|
|
let data;
|
|
|
|
|
|
|
|
try {
|
2024-09-20 23:14:22 +00:00
|
|
|
data = this.fsSync.readFileSync(this.hash(name), { encoding: 'utf8' })
|
2024-09-20 13:40:01 +00:00
|
|
|
} catch {
|
|
|
|
return fallback
|
|
|
|
}
|
2024-09-20 23:14:22 +00:00
|
|
|
return this._parseCacheData(data, fallback, opts)
|
2024-09-20 13:40:01 +00:00
|
|
|
}
|
|
|
|
|
2024-09-20 23:14:22 +00:00
|
|
|
set(name, data, orgOpts = {}) {
|
|
|
|
let opts = typeof orgOpts === 'number' ? { ttl: orgOpts } : orgOpts
|
|
|
|
|
2024-09-20 13:40:01 +00:00
|
|
|
try {
|
|
|
|
return this.fsPromises.writeFile(
|
2024-09-20 23:14:22 +00:00
|
|
|
this.hash(name),
|
|
|
|
this._parseSetData(name, data, opts),
|
2024-09-20 13:40:01 +00:00
|
|
|
{ encoding: opts.encoding || 'utf8' }
|
|
|
|
)
|
|
|
|
} catch (err) {
|
|
|
|
return Promise.reject(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-20 23:14:22 +00:00
|
|
|
async setMany(items, options) {
|
|
|
|
for (let item of items) {
|
|
|
|
await this.set(item.key, item.content ?? item.value, options)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
save(items, options) {
|
|
|
|
return this.setMany(items, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
setSync(name, data, orgOpts = {}) {
|
|
|
|
let opts = typeof orgOpts === 'number' ? { ttl: orgOpts } : orgOpts
|
|
|
|
|
2024-09-20 13:40:01 +00:00
|
|
|
this.fsSync.writeFileSync(
|
2024-09-20 23:14:22 +00:00
|
|
|
this.hash(name),
|
|
|
|
this._parseSetData(name, data, opts),
|
2024-09-20 13:40:01 +00:00
|
|
|
{ encoding: opts.encoding || 'utf8' }
|
|
|
|
)
|
|
|
|
}
|
2024-09-20 23:14:22 +00:00
|
|
|
|
|
|
|
setManySync(items, options) {
|
|
|
|
for (let item of items) {
|
|
|
|
this.setSync(item.key, item.content ?? item.value, options)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
saveSync(items, options) {
|
|
|
|
return this.setManySync(items, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
remove(name) {
|
|
|
|
return this.fsPromises.rm(this.hash(name), { force: true })
|
|
|
|
}
|
|
|
|
|
|
|
|
removeSync(name) {
|
|
|
|
return this.fsSync.rmSync(this.hash(name), { force: true })
|
|
|
|
}
|
|
|
|
|
|
|
|
async clear() {
|
|
|
|
for (let file of await this.fsPromises.readdir(this.cache_dir)) {
|
|
|
|
if (!file.startsWith(this.prefix)) continue
|
|
|
|
await this.fsPromises.rm(path.join(this.cache_dir, file), { force: true })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clearSync() {
|
|
|
|
for (let file of this.fsSync.readdirSync(this.cache_dir)) {
|
|
|
|
if (!file.startsWith(this.prefix)) continue
|
|
|
|
this.fsSync.rmSync(path.join(this.cache_dir, file), { force: true })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAll() {
|
|
|
|
let out = []
|
|
|
|
let now = new Date().getTime()
|
|
|
|
for (let file of await this.fsPromises.readdir(this.cache_dir)) {
|
|
|
|
if (!file.startsWith(this.prefix)) continue
|
|
|
|
let data = await this.fsPromises.readFile(path.join(this.cache_dir, file), { encoding: 'utf8' })
|
|
|
|
let entry = JSON.parse(data)
|
|
|
|
if (entry.content && !this._checkIsExpired(entry, now)) {
|
|
|
|
out.push(entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
load() {
|
|
|
|
return this.getAll().then(res => {
|
|
|
|
return {
|
|
|
|
files: res.map(entry => ({ path: this.hash(entry.key), value: entry.content, key: entry.key }))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2024-09-20 13:40:01 +00:00
|
|
|
}
|