This repository has been archived by the owner on Nov 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
130 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,93 @@ | ||
import { Storage, StorageKeyPart } from "@mtkruto/node"; | ||
import { GetManyFilter, Storage, StorageKeyPart } from "@mtkruto/node"; | ||
import { LocalStorage } from "node-localstorage"; | ||
import { fromString, toString } from "./utilities"; | ||
import { fromString, isInRange, toString } from "./utilities"; | ||
|
||
export class StorageLocalStorage extends Storage implements Storage { | ||
constructor( | ||
private readonly prefix: string, | ||
private readonly localStorage = new LocalStorage(".mtkruto"), | ||
) { | ||
readonly #prefix: string; | ||
readonly #localStorage: LocalStorage; | ||
|
||
constructor(prefix: string, localStorage?: LocalStorage) { | ||
if (prefix.length <= 0) { | ||
throw new Error("Empty prefix"); | ||
} else if (!/^[0-9a-zA-Z]+$/.test(prefix)) { | ||
throw new Error("Unallowed prefix"); | ||
} | ||
super(); | ||
this.#prefix = prefix; | ||
this.#localStorage = localStorage ?? new LocalStorage(".mtkruto"); | ||
} | ||
|
||
get prefix() { | ||
return this.#prefix; | ||
} | ||
|
||
branch(id: string) { | ||
return new StorageLocalStorage(this.prefix + "S__" + id); | ||
} | ||
|
||
initialize() { | ||
} | ||
|
||
init() { | ||
get supportsFiles() { | ||
return false; | ||
} | ||
|
||
get(key_: readonly StorageKeyPart[]) { | ||
get<T>(key_: readonly StorageKeyPart[]) { | ||
const key = this.prefix + toString(key_); | ||
const value = this.localStorage.getItem(key); | ||
const value = this.#localStorage.getItem(key); | ||
if (value != null) { | ||
return fromString(value); | ||
return fromString<T>(value); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
*getMany<T>(prefix: readonly StorageKeyPart[]) { | ||
for (let [key, value] of Object.entries(this.localStorage)) { | ||
*getMany<T>( | ||
filter: GetManyFilter, | ||
params?: { limit?: number; reverse?: boolean }, | ||
) { | ||
let entries = Object.entries(localStorage).sort(([a], [b]) => | ||
a.localeCompare(b) | ||
); | ||
if (params?.reverse) { | ||
entries.reverse(); | ||
} | ||
if (params?.limit !== undefined) { | ||
entries = entries.slice(0, params.limit <= 0 ? 1 : params.limit); | ||
} | ||
entries: for (let [key, value] of entries) { | ||
if (key.startsWith(this.prefix)) { | ||
key = key.slice(this.prefix.length); | ||
} | ||
const parts = fromString(key); | ||
if (Array.isArray(parts)) { | ||
for (const [i, p] of prefix.entries()) { | ||
if (toString(p) != toString(parts[i])) { | ||
if ("prefix" in filter) { | ||
for (const [i, p] of filter.prefix.entries()) { | ||
if (toString(p) != toString(parts[i])) { | ||
continue entries; | ||
} | ||
} | ||
} else { | ||
if (!isInRange(parts, filter.start, filter.end)) { | ||
continue; | ||
} | ||
yield [parts, fromString(value)] as [readonly StorageKeyPart[], T]; | ||
} | ||
|
||
yield [parts, fromString(value)] as [readonly StorageKeyPart[], T]; | ||
} | ||
} | ||
} | ||
|
||
set(key_: readonly StorageKeyPart[], value: unknown) { | ||
const key = this.prefix + toString(key_); | ||
if (value != null) { | ||
this.localStorage.setItem(key, toString(value)); | ||
this.#localStorage.setItem(key, toString(value)); | ||
} else { | ||
this.localStorage.removeItem(key); | ||
this.#localStorage.removeItem(key); | ||
} | ||
} | ||
|
||
incr(key: readonly StorageKeyPart[], by: number) { | ||
this.set(key, (this.get<number>(key) || 0) + by); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters