Skip to content

Commit

Permalink
$mol_build_server fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Nov 14, 2024
1 parent 2d6216e commit c7666cb
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 28 deletions.
3 changes: 3 additions & 0 deletions build/ensure/ensure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ namespace $ {

@ $mol_mem_key
ensure( path : string ): boolean {
if (path.includes('/-/')) {
console.warn('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ensure build', new Error().stack)
}
const mod = $mol_file.absolute( path )
const parent = mod.parent()

Expand Down
43 changes: 19 additions & 24 deletions build/server/server.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,20 @@ namespace $ {
mdl: (
req : typeof $node.express.request ,
res : typeof $node.express.response ,
) => boolean | void
) => void | boolean
) {
// const wrapped = $mol_wire_async(mdl)
const wrapped = $mol_wire_async(mdl)

return async (
req : typeof $node.express.request ,
res : typeof $node.express.response ,
next : (err?: unknown) => any
) => {
const wrapped = $mol_wire_async(mdl)
try {
const call_next = await wrapped(req, res)
if (! call_next) return

// await Promise.resolve()
next()
// const wrapped = $mol_wire_async(mdl)

try {
const stopped = await wrapped(req, res)
if (! stopped) Promise.resolve().then(next)
} catch (error: any) {
if (! this.$.$mol_build_server.trace) {
error.message += '\n' + 'Set $mol_build_server.trace = true for stacktraces'
Expand All @@ -49,7 +46,6 @@ namespace $ {
req : typeof $node.express.request ,
res : typeof $node.express.response,
) {
$mol_wire_sync(res).set( 'Cache-Control', 'no-cache, public' )

try {

Expand All @@ -64,7 +60,7 @@ namespace $ {
// }

this.generate( req.url )
return true // next
res.set( 'Cache-Control', 'no-cache, public' )
} catch( error: any ) {
if ($mol_promise_like(error)) $mol_fail_hidden(error)

Expand All @@ -82,6 +78,7 @@ namespace $ {
} ).join( '\n' )

res.send( script ).end()
return true
}
}

Expand Down Expand Up @@ -114,35 +111,32 @@ namespace $ {
return build.bundle( [ path , bundle ] )
}

@ $mol_mem_key
ensure_index(path: string) {
$mol_wire_solid()

return this.build().modEnsure( path )
}

override expressIndex() { return this.sync_middleware(this.expressIndexRequest.bind(this)) }

expressIndexRequest(
req : typeof $node.express.request ,
res : typeof $node.express.response ,
) {
// a/b/?c#d, a/b/-/
const match = req.url.match( /(\/|.*[^\-]\/)([\?#].*)?$/ )
if( !match) return

const root = this.$.$mol_file.absolute( this.rootPublic() )
const dir = root.resolve( req.path )

this.ensure_index( dir.path() )
const path = dir.path()

const match = req.url.match( /(\/|.*[^\-]\/)([\?#].*)?$/ )
if( !match) return true
// ensure загружает сорцы, делает git pull, это не стоит делать на build-папках
this.build().modEnsure( path )

const file = root.resolve( `${req.path}index.html` )

if( file.exists() ) {
res.redirect( 301, `${match[1]}-/test.html${match[2] ?? ''}` )
return
return true
}

if( dir.type() !== 'dir' ) return true
if( dir.type() !== 'dir' ) return

const files = [ {name: '-', type: 'dir'} ]

Expand All @@ -153,7 +147,7 @@ namespace $ {
}

if( /\.meta\.tree$/.test( file.name() ) ) {
const meta = $$.$mol_tree2_from_string( file.text() )
const meta = this.$.$mol_tree2_from_string( file.text() )

for( const pack of meta.select( 'pack', null ).kids ) {
if ( files.find(( {name} ) => name === pack.type) ) continue
Expand Down Expand Up @@ -203,6 +197,7 @@ namespace $ {
} )

res.end( html )
return true
}

port() {
Expand Down
19 changes: 17 additions & 2 deletions file/base/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ namespace $ {

protected static changed_add(type: 'addDir' | 'unlinkDir' | 'add' | 'change' | 'unlink', path: string) {
const file = this.relative( path.at(-1) === '/' ? path.slice(0, -1) : path )
console.log(type, path)

if (type === 'add') {
// добавился файл - у parent надо обновить список sub, если он был заюзан
Expand Down Expand Up @@ -88,6 +89,7 @@ namespace $ {

this.frame?.destructor()
this.frame = new this.$.$mol_after_timeout(this.watch_debounce(), () => {
console.log('changed_add scheduled: ', this.watching ? 'yes' : 'no')
if (! this.watching) return
this.watching = false
$mol_wire_async(this).flush()
Expand Down Expand Up @@ -127,7 +129,9 @@ namespace $ {

// Выставляем обратно в true, что б watch мог зайти сюда
this.watching = true
}
this.watch_wd?.destructor()
this.watch_wd = null
}

protected static watching = true

Expand All @@ -148,8 +152,16 @@ namespace $ {
с точки зрения реактивной системы hyoo/board еще не существует.
*/
this.changed.add(this.absolute(path))
const stack = new Error().stack
this.watch_wd = new $mol_after_timeout(10000, () => {
console.error('Lock timeout')
console.error(stack)
})

}

protected static watch_wd = null as null | $mol_after_timeout

static watch_off<Result>(side_effect: () => Result, affected_dir: string) {
// ждем, пока выполнится предыдущий watch_off
const unlock = this.lock.grab()
Expand Down Expand Up @@ -178,7 +190,9 @@ namespace $ {

@ $mol_mem
version() {
return this.stat()?.mtime.getTime().toString( 36 ).toUpperCase() ?? ''
const next = this.stat()?.mtime.getTime().toString( 36 ).toUpperCase() ?? ''
console.log('version', next, this.path())
return next
}

protected info( path: string ) { return null as null | $mol_file_stat }
Expand Down Expand Up @@ -289,6 +303,7 @@ namespace $ {

const exists = Boolean( this.stat() )

console.log('exists current', exists, 'next', next, this.path())
if( next === undefined ) return exists
if( next === exists ) return exists

Expand Down
1 change: 1 addition & 0 deletions file/file.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ namespace $ {

@ $mol_mem_key
static watcher(path: string) {
// const w = $node.fs.watch(path)
const watcher = $node.chokidar.watch( path , {
persistent : true ,
ignored: path => /([\/\\]\.|___$)/.test( path ),
Expand Down
3 changes: 2 additions & 1 deletion file/file.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ namespace $ {
: ''

// Вотчер выключен, версия всегда будет одна
override version() { return '' }
// Если пустая строка - будет считаться, что файла нет
override version() { return '1' }
// Ворнинги подавляем, иначе в каждом приложении, загружающим локали, будет ворнинг
// override watcher() { return { destructor() {} }}

Expand Down
5 changes: 4 additions & 1 deletion run/run.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ namespace $ {
let sub

try {
sub = this.$.$mol_run_spawn(app, args, opts)
sub = this.$.$mol_run_spawn(app, args, {
...opts,
stdio: [ 'pipe', 'inherit', 'inherit' ],
})
} catch (error) {
throw new $mol_run_error(
this.error_message(undefined),
Expand Down

0 comments on commit c7666cb

Please sign in to comment.