Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified icons/src/ru/ranobelib/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
108 changes: 108 additions & 0 deletions libs/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import fs from 'fs';
import path from 'path';

class Storage {
private db: Record<string, { created: Date; value: any; expires?: number }>;

/**
* Initializes a new instance of the Storage class.
*/
constructor() {
this.db = {};
}

/**
* Sets a key-value pair in storage.
*
* @param {string} key - The key to set.
* @param {any} value - The value to set.
* @param {Date | number} [expires] - Optional expiry date or time in milliseconds.
*/
set(key: string, value: any, expires?: Date | number): void {
this.db[key] = {
created: new Date(),
value,
expires: expires instanceof Date ? expires.getTime() : expires,
};
}

/**
* Retrieves the value for a given key from storage.
*
* @param {string} key - The key to retrieve the value for.
* @param {boolean} [raw] - Optional flag to return the raw stored item.
* @returns {any} The stored value or undefined if key is not found.
*/
get(key: string, raw?: boolean): any {
const item = this.db[key];
if (item?.expires && Date.now() > item.expires) {
this.delete(key);
return undefined;
}
return raw ? item : item?.value;
}

/**
* Retrieves all keys set by the `set` method.
*
* @returns {string[]} An array of keys.
*/
getAllKeys(): string[] {
return Object.keys(this.db);
}

/**
* Deletes a key from the storage.
*
* @param pluginID - The ID of the plugin.
* @param key - The key to delete.
*/
delete(key: string): void {
delete this.db[key];
}

/**
* Clears all stored items from storage.
*/
clearAll(pluginID: string): void {
this.db = {};
}
}

// Export a singleton instance of the Storage class
export const storage = new Storage();

/*
These parameters cannot be implemented in `test-web`.
They are generated in the browser when js-scripts are executed
Read more

https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage
*/

/**
* Represents the structure of a storage object with string keys and values.
*/
interface StorageObject {
[key: string]: any;
}

/**
* Represents a simplified version of the browser's localStorage.
*/
class LocalStorage {
private db: StorageObject;

constructor() {
this.db = {};
}

get(): StorageObject | undefined {
return this.db;
}
}

// Export singleton instances of LocalStorage and sessionStorage
export const localStorage = new LocalStorage();
export const sessionStorage = new LocalStorage();
71 changes: 55 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
"dayjs": "^1.11.10",
"express": "^4.18.2",
"htmlparser2": "^9.1.0",
"image-size": "^1.1.1",
"less": "^4.2.0",
"module-alias": "^2.2.3",
"protobufjs": "^7.2.6",
"qs": "^6.11.2",
"sanitize-html": "^2.12.1",
"terser": "^5.30.4",
"terser": "^5.31.0",
"ts-node": "^10.9.2",
"tsc-alias": "^1.8.8",
"typescript": "^5.3.3",
Expand Down
Loading