Compare commits

...

5 Commits

Author SHA1 Message Date
e612f5762f not necessary bro 2025-03-15 20:16:13 -04:00
d4278d0d7f plumbing 2025-03-15 20:14:05 -04:00
26a7d4c21c File and Directory classes 2025-03-15 19:53:40 -04:00
305c2bd2cd vite update 2025-03-15 19:46:10 -04:00
c66a0eb4a9 prototyping with classes for Inner and Rfwfs directory 2025-03-14 17:39:07 -04:00
2 changed files with 81 additions and 93 deletions

View File

@ -11,7 +11,7 @@
"devDependencies": { "devDependencies": {
"sass": "^1.85.1", "sass": "^1.85.1",
"typescript": "^5.8.2", "typescript": "^5.8.2",
"vite": "^6.2.1", "vite": "^6.2.2",
"vite-plugin-html": "^3.2.2" "vite-plugin-html": "^3.2.2"
} }
} }

View File

@ -2,41 +2,28 @@ import { EntryType, PushStatus, ReadStatus, Permissions, ConstEnum, PermissionsB
import { wrap_entry, wrap_none, WrapResultEntry, WrapResultNone } from "./wrap" import { wrap_entry, wrap_none, WrapResultEntry, WrapResultNone } from "./wrap"
import directory_search from "./index" import directory_search from "./index"
import hash_table from "./hash"
type FileInner = string | number 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 { interface Entry {
permissions: Permissions, permissions: Permissions,
timestamp: EntryValue<number>, timestamp: EntryValue<number>,
name: EntryValue<string>, name: EntryValue<string>,
readonly type: EntryType, readonly type: EntryType,
} }
interface EntryFile extends Entry { interface EntryFile extends Entry {
inner: EntryFileInner, inner: EntryValue<FileInner>,
hash: string, hash: string,
} }
interface EntryCollection<T extends Entry> extends Entry { interface EntryCollection<T extends Entry> extends Entry {
inner: EntryCollectionManipulate<T>, inner: RfwfsDirectory<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>,
} }
interface Rfwfs { interface Rfwfs {
directory: <T extends Entry>(default_name: string, default_permissions: Permissions, default_timestamp?: number, default_inner?: T[]) => EntryCollection<T>, 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_binary: <T extends Entry>(entry: T) => boolean,
is_file: <T extends Entry>(entry: T) => boolean, is_file: <T extends Entry>(entry: T) => boolean,
is_dir: <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 return read_write_access(permissions) || permissions === Permissions.w
} }
function directory_sort<E extends Entry>(self: EntryCollectionManipulate<E>) { class EntryValue<T> {
self.__inner.sort((a,z) => a.name.read().localeCompare(z.name.read())) public inner: T;
} protected entry: Entry
function directory_push<E extends Entry>(self: EntryCollection<E>, entry: E): WrapResultNone<PushStatus> { constructor(entry: Entry, inner_default: T) {
if (read_write_access(self.permissions)) { this.inner = inner_default
const no_duplicates = directory_search(self.inner.__inner, entry.name.read()) this.entry = entry
if (!no_duplicates) { }
self.inner.__inner.push(entry)
self.inner.__inner.sort() public write<I extends T>(item: I): boolean {
return wrap_none(PushStatus.Ok) if (write_access(this.entry.permissions)) {
this.inner = item
return true
} }
return wrap_none(PushStatus.Duplicate) return false
}
public read(): T | undefined {
return read_access(this.entry.permissions) ? this.inner : undefined
} }
return wrap_none(PushStatus.Denied)
} }
function directory_find<E extends Entry>(self: EntryCollection<E>, file_name: string): WrapResultEntry<E, ReadStatus> { class RfwfsDirectory<T extends Entry> {
if (read_write_access(self.permissions)) { public inner: T[];
const file_search = directory_search(self.inner.__inner, file_name) protected entry: Entry
if (file_search) {
return wrap_entry(ReadStatus.Ok, file_search.result) constructor(entry: Entry, inner: T[]) {
this.inner = inner
this.entry = entry
}
public sort() {
this.inner.sort((a,z) => a.name.inner.localeCompare(z.name.inner))
}
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.NotFound) return wrap_entry(ReadStatus.Denied)
} }
return wrap_entry(ReadStatus.Denied)
}
function directory_pop<E extends Entry>(self: EntryCollection<E>, file_name: string): WrapResultEntry<E, ReadStatus> { public find(file_name: string): WrapResultEntry<T, ReadStatus> {
if (read_write_access(self.permissions)) { if (read_write_access(this.entry.permissions)) {
const pop_find = directory_search(self.inner.__inner, file_name) const file_search = directory_search(this.inner, file_name)
if (pop_find) { if (file_search) {
self.inner.__inner.splice(pop_find.some, 1) return wrap_entry(ReadStatus.Ok, file_search.result)
return wrap_entry(ReadStatus.Ok, pop_find.result) }
return wrap_entry(ReadStatus.NotFound)
} }
return wrap_entry(ReadStatus.NotFound) return wrap_entry(ReadStatus.Denied)
} }
return wrap_entry(ReadStatus.Denied)
}
function directory_clone<E extends Entry>(self: EntryCollection<E>, file_name: string): WrapResultEntry<E, ReadStatus> { public push<E extends T>(entry: E): WrapResultNone<PushStatus> {
if (read_write_access(self.permissions)) { if (read_write_access(this.entry.permissions)) {
const clone_find = directory_search(self.inner.__inner, file_name) const no_duplicates = directory_search(this.inner, entry.name.inner)
if (clone_find) { if (!no_duplicates) {
return wrap_entry(ReadStatus.Ok, { ...clone_find.result }) this.inner.push(entry)
this.inner.sort()
return wrap_none(PushStatus.Ok)
}
return wrap_none(PushStatus.Duplicate)
} }
return wrap_entry(ReadStatus.NotFound) return wrap_none(PushStatus.Denied)
} }
return wrap_entry(ReadStatus.Denied)
}
function inner_read<I extends FileInner, P extends ConstEnum>(self: EntryValue<I>, permissions: P): FileInner | undefined { public pop(file_name: string): WrapResultEntry<T, ReadStatus> {
return read_access(permissions) ? self.__inner : undefined if (read_write_access(this.entry.permissions)) {
} const pop_find = directory_search(this.inner, file_name)
if (pop_find) {
function inner_write<I extends FileInner, P extends ConstEnum>(self: EntryValue<I>, permissions: P, item: I): boolean { this.inner.splice(pop_find.some, 1)
if (write_access(permissions)) { return wrap_entry(ReadStatus.Ok, pop_find.result)
self.__inner = item }
return true return wrap_entry(ReadStatus.NotFound)
}
return wrap_entry(ReadStatus.Denied)
} }
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
}
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
} }
const rfwfs = {} as Rfwfs const rfwfs = {} as Rfwfs
@ -151,28 +140,27 @@ rfwfs.is_binary = function(entry) {
return entry.type === EntryType.Binary 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 const file = { type: EntryType.File } as EntryFile
file.hash = "0"
file.permissions = default_permissions file.permissions = default_permissions
file.timestamp = inner(default_permissions, default_timestamp ? default_timestamp : (Date.now()/1000)|0) file.timestamp = new EntryValue(file, default_timestamp ? default_timestamp : (Date.now()/1000)|0)
file.inner = inner(default_permissions, default_inner) file.inner = new EntryValue(file, default_inner ? default_inner : "")
file.name = inner(default_permissions, default_name) file.name = new EntryValue(file, default_name)
file.hash = await hash_table.sha256(default_inner)
return file return file
} }
rfwfs.directory = function<T extends Entry>(default_name: string, default_permissions: Permissions, default_timestamp?: number, default_inner?: T[]): EntryCollection<T> { 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> const directory = { type: EntryType.Directory } as EntryCollection<T>
directory.permissions = default_permissions directory.permissions = default_permissions
directory.timestamp = inner(default_permissions, default_timestamp ? default_timestamp : (Date.now()/1000)|0) directory.timestamp = new EntryValue(directory, default_timestamp ? default_timestamp : (Date.now()/1000)|0)
directory.inner = inner(default_permissions, default_inner) directory.inner = new RfwfsDirectory(directory, default_inner ? default_inner : [])
directory.name = inner(default_permissions, default_name) directory.name = new EntryValue(directory, default_name)
return directory return directory
} }
export default rfwfs export default rfwfs
export { export {
type EntryCollectionManipulate,
type EntryCollection, type EntryCollection,
type FileInner, type FileInner,
type EntryFile, type EntryFile,