Skip to content

Commit

Permalink
$mol_file use this type
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Nov 12, 2024
1 parent 8f926f2 commit af18ed6
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 41 deletions.
4 changes: 3 additions & 1 deletion build/build.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ namespace $ {

@ $mol_mem_key
static root( [ root, paths ] : [root: string, paths: readonly string[] ] ) {
this.$.$mol_file.watch_root = root

return this.make({
root : ()=> this.$.$mol_file.root( root ) ,
root : ()=> this.$.$mol_file.absolute( root ) ,
paths: $mol_const(paths)
})
}
Expand Down
72 changes: 55 additions & 17 deletions file/file.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,28 @@ namespace $ {
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength)
}

enum file_modes {
/** create if it doesn't already exist */
create = $node.fs.constants.O_CREAT,
/** truncate to zero size if it already exists */
exists_truncate = $node.fs.constants.O_TRUNC,
/** throw exception if it already exists */
exists_fail = $node.fs.constants.O_EXCL,
read_only = $node.fs.constants.O_RDONLY,
write_only = $node.fs.constants.O_WRONLY,
read_write = $node.fs.constants.O_RDWR,
/** data will be appended to the end */
append = $node.fs.constants.O_APPEND,
}

function mode_mask(modes: readonly $mol_file_mode[]) {
return modes.reduce( ( res, mode )=> res | file_modes[ mode ], 0 )
}

export class $mol_file_node extends $mol_file {

static relative( path : string ) {
return this.absolute( $node.path.resolve( this.base, path ).replace( /\\/g , '/' ) )
static relative<This extends typeof $mol_file>(this: This, path : string ) {
return this.absolute<This>( $node.path.resolve( this.base, path ).replace( /\\/g , '/' ) )
}

@ $mol_mem_key
Expand Down Expand Up @@ -148,32 +166,52 @@ namespace $ {
}

override resolve( path : string ) {
return ( this.constructor as typeof $mol_file ).relative( $node.path.join( this.path() , path ) )
return ( this.constructor as typeof $mol_file )
.relative( $node.path.join( this.path() , path ) ) as this
}

override relate( base = ( this.constructor as typeof $mol_file ).relative( '.' )) {
return $node.path.relative( base.path() , this.path() ).replace( /\\/g , '/' )
}

@ $mol_mem
override stream_read() {
const { Readable } = $node['node:stream'] as typeof import('stream')
const ctl = new AbortController
const stream = $node.fs.createReadStream(this.path(), { signal: ctl.signal })
const destructor = () => ctl.abort()

return Object.assign(Readable.toWeb(stream), { destructor }) as ReadableStream
@ $mol_mem_key
override readable({ modes }: { modes: readonly $mol_file_mode[] }) {
return this.handle(modes).readableWebStream({ type: 'bytes' }) as ReadableStream<Uint8Array>
}

@ $mol_mem
override stream_write() {
@ $mol_mem_key
override writable({ modes, start }: { modes: readonly $mol_file_mode[], start?: number }) {
const { Writable } = $node['node:stream'] as typeof import('stream')
const stream = this.handle(modes).createWriteStream({
start,
// encoding?: BufferEncoding | null | undefined;
// autoClose?: boolean | undefined;
// emitClose?: boolean | undefined;
// start?: number | undefined;
// highWaterMark?: number | undefined;
// flush?: boolean | undefined;
})

const web = Writable.toWeb(stream)

return Object.assign(web, {
destructor: () => web.close()
})
}

protected handle(modes: readonly $mol_file_mode[]) {
const mode = mode_mask(modes)

const ctl = new AbortController
const stream = $node.fs.createWriteStream(this.path(), { signal: ctl.signal })
const destructor = () => ctl.abort()
const handle = $mol_wire_sync($node.fs.promises).open(this.path(), mode)

return $mol_wire_sync(handle)
}

return Object.assign(Writable.toWeb(stream), { destructor }) as WritableStream
open( ... modes: readonly $mol_file_mode[] ) {
return $node.fs.openSync(
this.path(),
mode_mask(modes)
)
}

}
Expand Down
36 changes: 17 additions & 19 deletions file/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ namespace $ {
ctime: Date
}

export type $mol_file_mode = 'create' | 'exists_truncate' | 'exists_fail' | 'read_only' | 'write_only' | 'read_write' | 'append'

// export class $mol_file_not_found extends Error {}

export class $mol_file extends $mol_object {

@ $mol_mem_key
static absolute( path : string ) {
return this.make({
static absolute<This extends typeof $mol_file>(this: This, path : string ) {
return this.make<typeof $mol_file>({
path : $mol_const( path )
})
}) as InstanceType< This >
}

static relative( path : string ) : $mol_file {
static relative<This extends typeof $mol_file>(this: This, path : string ) : InstanceType<This> {
throw new Error( 'Not implemented yet' )
}

Expand All @@ -36,7 +38,7 @@ namespace $ {
}

@ $mol_mem
stat(next? : $mol_file_stat | null, virt?: 'virt') {
protected stat(next? : $mol_file_stat | null, virt?: 'virt') {

const path = this.path()
const parent = this.parent()
Expand All @@ -45,7 +47,7 @@ namespace $ {
// Отслеживать проверку наличия родительской папки не стоит до корня диска
// Лучше ограничить mam-ом
const root = this.$.$mol_file.watch_root ?? this
if ( this !== root ) {
if ( path !== root ) {
/*
Если родитель удалился, надо ресетнуть все дочерние на любой глубине
Родитель может удалиться, потом создасться, а дочерняя папка только удалиться.
Expand Down Expand Up @@ -201,10 +203,12 @@ namespace $ {
protected read() { return new Uint8Array }
protected write(buffer: Uint8Array) { }
protected kids() {
return [] as readonly $mol_file[]
return [] as readonly this[]
}
stream_read() { return new ReadableStream }
stream_write() { return new WritableStream }

readable(opts: { modes: readonly $mol_file_mode[] }) { return new ReadableStream<Uint8Array>() }
writable(opts: { modes: readonly $mol_file_mode[] }) { return new WritableStream<Uint8Array>() }
// open( ... modes: readonly $mol_file_mode[] ) { return 0 }

@ $mol_mem
buffer( next? : Uint8Array ) {
Expand Down Expand Up @@ -255,7 +259,7 @@ namespace $ {
clone(to: string) {
if (! this.exists() ) return null

const target = this.$.$mol_file.absolute(to)
const target = (this.constructor as typeof $mol_file).absolute(to) as this

try {
this.version()
Expand All @@ -271,13 +275,7 @@ namespace $ {
return null
}

protected static watch_root = null as null | $mol_file

static root( path: string) {
this.watch_root = this.absolute( path )
return this.watch_root
}

static watch_root = ''

watcher() {
console.warn('$mol_file_web.watcher() not implemented')
Expand Down Expand Up @@ -357,7 +355,7 @@ namespace $ {
return this.kids().filter(file => file.exists())
}

resolve(path: string): $mol_file {
resolve(path: string): this {
throw new Error('implement')
}

Expand All @@ -369,7 +367,7 @@ namespace $ {
include? : RegExp ,
exclude? : RegExp
) {
const found = [] as $mol_file[]
const found = [] as typeof this[]
const sub = this.sub()

for (const child of sub) {
Expand Down
8 changes: 4 additions & 4 deletions file/file.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ namespace $ {

export class $mol_file_web extends $mol_file {

static override relative( path : string ) {
return this.absolute( new URL( path , this.base ).toString() )
static override relative<This extends typeof $mol_file>(this: This, path : string ) {
return this.absolute<This>( new URL( path , this.base ).toString() )
}

static base = $mol_dom_context.document?.currentScript
Expand All @@ -30,7 +30,7 @@ namespace $ {
if( prev === res ) break
}

return ( this.constructor as typeof $mol_file_web ).absolute( res )
return ( this.constructor as typeof $mol_file ).absolute( res ) as this
}

protected override ensure() {
Expand All @@ -41,7 +41,7 @@ namespace $ {
throw new Error('$mol_file_web.drop() not implemented')
}

protected override kids() : readonly $mol_file[] {
protected override kids() : readonly this[] {
throw new Error('$mol_file_web.sub() not implemented')
}

Expand Down

0 comments on commit af18ed6

Please sign in to comment.