Skip to content

Commit

Permalink
$mol_file refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Nov 11, 2024
1 parent 18124ca commit 9ac6393
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 168 deletions.
7 changes: 5 additions & 2 deletions build/build.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1194,8 +1194,11 @@ namespace $ {
}

} catch( error ) {
if( $mol_promise_like( error ) ) $mol_fail_hidden( error )
$mol_fail_log( error )
if ($mol_fail_catch(error)) {
if (! (error as Error).message.match(/code E404/)) {
console.error( error )
}
}
}

++ version[2]
Expand Down
40 changes: 26 additions & 14 deletions build/client/client.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
function $mol_build_client() {

const origin = document.location.origin.replace( /^http/ , 'ws' )
const path = document.location.pathname
const uri = origin + path
// @ts-check

const socket = new WebSocket( uri )

socket.onclose = ()=> setTimeout( ()=> $mol_build_client() , 1000 )

socket.onmessage = message => {
if( message.data !== '$mol_build_obsolete' ) return
location.reload()
}
class $mol_build_client {
static closed = false
static run() {
const origin = document.location.origin.replace( /^http/ , 'ws' )
const path = document.location.pathname
const uri = origin + path

const socket = new WebSocket( uri )

socket.onclose = ()=> {
this.closed = true
setTimeout( ()=> $mol_build_client.run() , 1000 )
}

socket.onopen = () => {
if (this.closed) location.reload()
this.closed = false
}

socket.onmessage = message => {
if( message.data !== '$mol_build_obsolete' ) return
location.reload()
}

}
}

$mol_build_client()
$mol_build_client.run()

8 changes: 4 additions & 4 deletions build/server/server.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ namespace $ {
...build.bundleFiles([ bundle.path() , [ 'node' ] ])
]

for( const src of sources ) src.stat()
for( const src of sources ) src.version()
} catch (error) {
if ($mol_fail_catch(error)) {
this.$.$mol_log3_fail({
Expand Down Expand Up @@ -328,9 +328,9 @@ namespace $ {

try {

for( const file of build.bundle([ path, 'node.js' ]) ) file.stat()
for( const file of build.bundle([ path, 'node.audit.js' ]) ) file.stat()
for( const file of build.bundle([ path, 'node.test.js' ]) ) file.stat()
for( const file of build.bundle([ path, 'node.js' ]) ) file.version()
for( const file of build.bundle([ path, 'node.audit.js' ]) ) file.version()
for( const file of build.bundle([ path, 'node.test.js' ]) ) file.version()

} catch( error: any ) {

Expand Down
107 changes: 33 additions & 74 deletions file/file.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,6 @@ namespace $ {
function buffer_normalize(buf: Buffer): Uint8Array {
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength)
}

export enum $mol_file_mode_open {
/** 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,
}

export class $mol_file_node extends $mol_file {

Expand Down Expand Up @@ -111,65 +97,36 @@ namespace $ {
protected override drop() {
$node.fs.unlinkSync( this.path() )
}

@ $mol_mem
override buffer( next? : Uint8Array ) {

@ $mol_action
protected override read() {
const path = this.path()
if( next === undefined ) {

if( !this.stat() ) return new Uint8Array

try {

const prev = $mol_mem_cached( ()=> this.buffer() )

next = buffer_normalize( $node.fs.readFileSync( path ) )

if( prev !== undefined && !$mol_compare_array( prev, next ) ) {
this.$.$mol_log3_rise({
place: `$mol_file_node.buffer()`,
message: 'Changed' ,
path: this.relate() ,
})
}

return next

} catch( error: any ) {
if (this.$.$mol_fail_catch(error)) {
error.message += '\n' + path
}
return this.$.$mol_fail_hidden( error )

try {
return buffer_normalize($node.fs.readFileSync( path ))
} catch( error: any ) {
if (this.$.$mol_fail_catch(error)) {
error.message += '\n' + path
}


return this.$.$mol_fail_hidden( error )
}

this.parent().exists( true )

const now = new Date
this.stat( {
type: 'file',
size: next.length,
atime: now,
mtime: now,
ctime: now,
}, 'virt' )
}

@ $mol_action
protected override write(buffer: Uint8Array) {
const path = this.path()

try {

$node.fs.writeFileSync( path, next )
$node.fs.writeFileSync( path, buffer )

} catch( error: any ) {
if (this.$.$mol_fail_catch(error)) {
error.message += '\n' + path
}
return this.$.$mol_fail_hidden( error )

}

return next

}

Expand Down Expand Up @@ -198,23 +155,25 @@ namespace $ {
return $node.path.relative( base.path() , this.path() ).replace( /\\/g , '/' )
}

protected override append( next : Uint8Array | string ) {
const path = this.path()
try {
$node.fs.appendFileSync( path , next )
} catch( e: any ) {
if (this.$.$mol_fail_catch(e)) {
e.message += '\n' + path
}
return this.$.$mol_fail_hidden(e)
}
@ $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
}

override open( ... modes: readonly ( keyof typeof $mol_file_mode_open )[] ) {
return $node.fs.openSync(
this.path(),
modes.reduce( ( res, mode )=> res | $mol_file_mode_open[ mode ], 0 ),
)

@ $mol_mem
override stream_write() {
const { Writable } = $node['node:stream'] as typeof import('stream')

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

return Object.assign(Writable.toWeb(stream), { destructor }) as WritableStream
}

}
Expand Down
Loading

0 comments on commit 9ac6393

Please sign in to comment.