UNPKG

signalk-tides

Version:

Tidal predictions for the vessel's position from various online sources.

29 lines (28 loc) 811 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = require("path"); const promises_1 = require("fs/promises"); class FileCache { path; constructor(path) { this.path = path; } async get(key) { try { const content = await (0, promises_1.readFile)(this.getKey(key), 'utf-8'); return JSON.parse(content); } catch (err) { return undefined; } } async set(key, value) { // Ensure directory exists await (0, promises_1.mkdir)(this.path, { recursive: true }); return (0, promises_1.writeFile)(this.getKey(key), JSON.stringify(value)); } getKey(key) { return (0, path_1.join)(this.path, `${key}.json`); } } exports.default = FileCache;