Compare commits
5 Commits
caedb7e8f0
...
e612f5762f
Author | SHA1 | Date | |
---|---|---|---|
e612f5762f | |||
d4278d0d7f | |||
26a7d4c21c | |||
305c2bd2cd | |||
c66a0eb4a9 |
@ -11,7 +11,7 @@
|
||||
"devDependencies": {
|
||||
"sass": "^1.85.1",
|
||||
"typescript": "^5.8.2",
|
||||
"vite": "^6.2.1",
|
||||
"vite": "^6.2.2",
|
||||
"vite-plugin-html": "^3.2.2"
|
||||
}
|
||||
}
|
@ -2,41 +2,28 @@ import { EntryType, PushStatus, ReadStatus, Permissions, ConstEnum, PermissionsB
|
||||
import { wrap_entry, wrap_none, WrapResultEntry, WrapResultNone } from "./wrap"
|
||||
|
||||
import directory_search from "./index"
|
||||
import hash_table from "./hash"
|
||||
|
||||
type FileInner = string | number
|
||||
type EntryFileInner = EntryValue<FileInner, FileInner | undefined>
|
||||
|
||||
//please do not change the inner values directly on entries or else there will be catastrophic consequences
|
||||
interface EntryValue<T, U = T> {
|
||||
__inner: T,
|
||||
write: (value: T) => boolean,
|
||||
read: () => U,
|
||||
}
|
||||
interface Entry {
|
||||
permissions: Permissions,
|
||||
timestamp: EntryValue<number>,
|
||||
name: EntryValue<string>,
|
||||
readonly type: EntryType,
|
||||
}
|
||||
|
||||
interface EntryFile extends Entry {
|
||||
inner: EntryFileInner,
|
||||
inner: EntryValue<FileInner>,
|
||||
hash: string,
|
||||
}
|
||||
|
||||
interface EntryCollection<T extends Entry> extends Entry {
|
||||
inner: EntryCollectionManipulate<T>,
|
||||
}
|
||||
interface EntryCollectionManipulate<T extends Entry> {
|
||||
__inner: T[],
|
||||
clone: (file_name: string) => WrapResultEntry<T, ReadStatus>
|
||||
find: (file_name: string) => WrapResultEntry<T, ReadStatus>
|
||||
push: (entry: Entry) => WrapResultNone<PushStatus>,
|
||||
sort: () => void,
|
||||
pop: (file_name: string) => WrapResultEntry<T, ReadStatus>,
|
||||
inner: RfwfsDirectory<T>,
|
||||
}
|
||||
|
||||
interface Rfwfs {
|
||||
directory: <T extends Entry>(default_name: string, default_permissions: Permissions, default_timestamp?: number, default_inner?: T[]) => EntryCollection<T>,
|
||||
file: (default_name: string, default_permissions: Permissions, default_timestamp?: number, default_inner?: FileInner) => Promise<EntryFile>,
|
||||
file: (default_name: string, default_permissions: Permissions, default_timestamp?: number, default_inner?: FileInner) => EntryFile,
|
||||
is_binary: <T extends Entry>(entry: T) => boolean,
|
||||
is_file: <T extends Entry>(entry: T) => boolean,
|
||||
is_dir: <T extends Entry>(entry: T) => boolean,
|
||||
@ -58,85 +45,87 @@ function write_access<P extends ConstEnum>(permissions: P): boolean {
|
||||
return read_write_access(permissions) || permissions === Permissions.w
|
||||
}
|
||||
|
||||
function directory_sort<E extends Entry>(self: EntryCollectionManipulate<E>) {
|
||||
self.__inner.sort((a,z) => a.name.read().localeCompare(z.name.read()))
|
||||
class EntryValue<T> {
|
||||
public inner: T;
|
||||
protected entry: Entry
|
||||
|
||||
constructor(entry: Entry, inner_default: T) {
|
||||
this.inner = inner_default
|
||||
this.entry = entry
|
||||
}
|
||||
|
||||
public write<I extends T>(item: I): boolean {
|
||||
if (write_access(this.entry.permissions)) {
|
||||
this.inner = item
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
public read(): T | undefined {
|
||||
return read_access(this.entry.permissions) ? this.inner : undefined
|
||||
}
|
||||
}
|
||||
|
||||
function directory_push<E extends Entry>(self: EntryCollection<E>, entry: E): WrapResultNone<PushStatus> {
|
||||
if (read_write_access(self.permissions)) {
|
||||
const no_duplicates = directory_search(self.inner.__inner, entry.name.read())
|
||||
if (!no_duplicates) {
|
||||
self.inner.__inner.push(entry)
|
||||
self.inner.__inner.sort()
|
||||
return wrap_none(PushStatus.Ok)
|
||||
}
|
||||
return wrap_none(PushStatus.Duplicate)
|
||||
}
|
||||
return wrap_none(PushStatus.Denied)
|
||||
}
|
||||
class RfwfsDirectory<T extends Entry> {
|
||||
public inner: T[];
|
||||
protected entry: Entry
|
||||
|
||||
function directory_find<E extends Entry>(self: EntryCollection<E>, file_name: string): WrapResultEntry<E, ReadStatus> {
|
||||
if (read_write_access(self.permissions)) {
|
||||
const file_search = directory_search(self.inner.__inner, file_name)
|
||||
if (file_search) {
|
||||
return wrap_entry(ReadStatus.Ok, file_search.result)
|
||||
constructor(entry: Entry, inner: T[]) {
|
||||
this.inner = inner
|
||||
this.entry = entry
|
||||
}
|
||||
return wrap_entry(ReadStatus.NotFound)
|
||||
}
|
||||
return wrap_entry(ReadStatus.Denied)
|
||||
}
|
||||
|
||||
function directory_pop<E extends Entry>(self: EntryCollection<E>, file_name: string): WrapResultEntry<E, ReadStatus> {
|
||||
if (read_write_access(self.permissions)) {
|
||||
const pop_find = directory_search(self.inner.__inner, file_name)
|
||||
if (pop_find) {
|
||||
self.inner.__inner.splice(pop_find.some, 1)
|
||||
return wrap_entry(ReadStatus.Ok, pop_find.result)
|
||||
public sort() {
|
||||
this.inner.sort((a,z) => a.name.inner.localeCompare(z.name.inner))
|
||||
}
|
||||
return wrap_entry(ReadStatus.NotFound)
|
||||
}
|
||||
return wrap_entry(ReadStatus.Denied)
|
||||
}
|
||||
|
||||
function directory_clone<E extends Entry>(self: EntryCollection<E>, file_name: string): WrapResultEntry<E, ReadStatus> {
|
||||
if (read_write_access(self.permissions)) {
|
||||
const clone_find = directory_search(self.inner.__inner, file_name)
|
||||
public clone(file_name: string): WrapResultEntry<T, ReadStatus> {
|
||||
if (read_write_access(this.entry.permissions)) {
|
||||
const clone_find = directory_search(this.inner, file_name)
|
||||
if (clone_find) {
|
||||
return wrap_entry(ReadStatus.Ok, { ...clone_find.result })
|
||||
}
|
||||
return wrap_entry(ReadStatus.NotFound)
|
||||
}
|
||||
return wrap_entry(ReadStatus.Denied)
|
||||
}
|
||||
|
||||
function inner_read<I extends FileInner, P extends ConstEnum>(self: EntryValue<I>, permissions: P): FileInner | undefined {
|
||||
return read_access(permissions) ? self.__inner : undefined
|
||||
}
|
||||
|
||||
function inner_write<I extends FileInner, P extends ConstEnum>(self: EntryValue<I>, permissions: P, item: I): boolean {
|
||||
if (write_access(permissions)) {
|
||||
self.__inner = item
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function inner<P extends ConstEnum, I, R extends EntryValue<I>>(permissions: P, inner_default: I): R {
|
||||
const inner_trait = { __inner: inner_default } as R
|
||||
inner_trait.write = function(item) { return inner_write(this, permissions, item) }
|
||||
inner_trait.read = function() { return inner_read(this, permissions) }
|
||||
return inner_trait
|
||||
}
|
||||
public find(file_name: string): WrapResultEntry<T, ReadStatus> {
|
||||
if (read_write_access(this.entry.permissions)) {
|
||||
const file_search = directory_search(this.inner, file_name)
|
||||
if (file_search) {
|
||||
return wrap_entry(ReadStatus.Ok, file_search.result)
|
||||
}
|
||||
return wrap_entry(ReadStatus.NotFound)
|
||||
}
|
||||
return wrap_entry(ReadStatus.Denied)
|
||||
}
|
||||
|
||||
function dir_inner<T extends Entry>(self: EntryCollection<T>, collection: T[]): EntryCollectionManipulate<T> {
|
||||
const collection_trait = { __inner: collection } as EntryCollectionManipulate<T>
|
||||
collection_trait.clone = function(file_name) { return directory_clone(self, file_name) }
|
||||
collection_trait.find = function(file_name) { return directory_find(self, file_name) }
|
||||
collection_trait.push = function(entry) { return directory_push(self, entry) }
|
||||
collection_trait.sort = function() { return directory_sort(this) }
|
||||
collection_trait.pop = function(file_name) { return directory_pop(self, file_name) }
|
||||
collection_trait.sort() //the default collection is automatically sorted on directory creation.
|
||||
return collection_trait
|
||||
public push<E extends T>(entry: E): WrapResultNone<PushStatus> {
|
||||
if (read_write_access(this.entry.permissions)) {
|
||||
const no_duplicates = directory_search(this.inner, entry.name.inner)
|
||||
if (!no_duplicates) {
|
||||
this.inner.push(entry)
|
||||
this.inner.sort()
|
||||
return wrap_none(PushStatus.Ok)
|
||||
}
|
||||
return wrap_none(PushStatus.Duplicate)
|
||||
}
|
||||
return wrap_none(PushStatus.Denied)
|
||||
}
|
||||
|
||||
public pop(file_name: string): WrapResultEntry<T, ReadStatus> {
|
||||
if (read_write_access(this.entry.permissions)) {
|
||||
const pop_find = directory_search(this.inner, file_name)
|
||||
if (pop_find) {
|
||||
this.inner.splice(pop_find.some, 1)
|
||||
return wrap_entry(ReadStatus.Ok, pop_find.result)
|
||||
}
|
||||
return wrap_entry(ReadStatus.NotFound)
|
||||
}
|
||||
return wrap_entry(ReadStatus.Denied)
|
||||
}
|
||||
}
|
||||
|
||||
const rfwfs = {} as Rfwfs
|
||||
@ -151,28 +140,27 @@ rfwfs.is_binary = function(entry) {
|
||||
return entry.type === EntryType.Binary
|
||||
}
|
||||
|
||||
rfwfs.file = async function(default_name, default_permissions, default_timestamp, default_inner = "") {
|
||||
rfwfs.file = function(default_name, default_permissions, default_timestamp, default_inner) {
|
||||
const file = { type: EntryType.File } as EntryFile
|
||||
file.hash = "0"
|
||||
file.permissions = default_permissions
|
||||
file.timestamp = inner(default_permissions, default_timestamp ? default_timestamp : (Date.now()/1000)|0)
|
||||
file.inner = inner(default_permissions, default_inner)
|
||||
file.name = inner(default_permissions, default_name)
|
||||
file.hash = await hash_table.sha256(default_inner)
|
||||
file.timestamp = new EntryValue(file, default_timestamp ? default_timestamp : (Date.now()/1000)|0)
|
||||
file.inner = new EntryValue(file, default_inner ? default_inner : "")
|
||||
file.name = new EntryValue(file, default_name)
|
||||
return file
|
||||
}
|
||||
|
||||
rfwfs.directory = function<T extends Entry>(default_name: string, default_permissions: Permissions, default_timestamp?: number, default_inner?: T[]): EntryCollection<T> {
|
||||
const directory = { type: EntryType.Directory } as EntryCollection<T>
|
||||
directory.permissions = default_permissions
|
||||
directory.timestamp = inner(default_permissions, default_timestamp ? default_timestamp : (Date.now()/1000)|0)
|
||||
directory.inner = inner(default_permissions, default_inner)
|
||||
directory.name = inner(default_permissions, default_name)
|
||||
directory.timestamp = new EntryValue(directory, default_timestamp ? default_timestamp : (Date.now()/1000)|0)
|
||||
directory.inner = new RfwfsDirectory(directory, default_inner ? default_inner : [])
|
||||
directory.name = new EntryValue(directory, default_name)
|
||||
return directory
|
||||
}
|
||||
|
||||
export default rfwfs
|
||||
export {
|
||||
type EntryCollectionManipulate,
|
||||
type EntryCollection,
|
||||
type FileInner,
|
||||
type EntryFile,
|
||||
|
Loading…
x
Reference in New Issue
Block a user