forked from hyoo-ru/mam_mol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.node.ts
More file actions
272 lines (203 loc) · 6.08 KB
/
Copy pathserver.node.ts
File metadata and controls
272 lines (203 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
namespace $ {
export class $mol_build_server extends $mol_server {
static trace = false
expressGenerator() {
const self = $mol_wire_async( this )
return function( req : any , res : any , next : () => void ) {
return self.handleRequest.call( self, req, res, next )
}
}
handleRequest(
req : typeof $node.express.request ,
res : typeof $node.express.response ,
next : () => any
) {
res.set( 'Cache-Control', 'must-revalidate, public, ' )
try {
// if( req.query._escaped_fragment_ ) {
// const fragment = decodeURIComponent( String( req.query._escaped_fragment_ ) )
// const url = req.protocol + '://' + req.get( 'host' ) + req.path + '#!' + fragment
// const html = $mol_browser.html( url )
// res.send( html ).end()
// return
// }
return this.generate( req.url ) && Promise.resolve().then( next )
} catch( error: any ) {
if( $mol_fail_catch( error ) ) {
this.$.$mol_log3_fail({
place: `${this}.handleRequest()`,
uri: req.path,
message: error.message,
stack: error.stack,
})
}
if( req.url.match( /\.js$/ ) ) {
const script = ( error as Error ).message.split( '\n\n' ).map( msg => {
return `console.error( ${ JSON.stringify( msg ) } )`
} ).join( '\n' )
res.send( script ).end()
} else {
if (! this.$.$mol_build_server.trace) {
error.message += '\n' + 'Set $mol_build_server.trace = true for stacktraces'
}
res.status(500).send( error.toString() ).end()
this.$.$mol_log3_fail({
place: `${this}.handleRequest()`,
uri: req.path,
stack: this.$.$mol_build_server.trace ? error.stack : undefined,
message: error.message,
})
}
}
}
build() : $mol_build {
return $mol_fail( new Error( 'Not implemented' ) )
}
@ $mol_mem_key
generate( url : string ) {
$mol_wire_solid()
const matched = url.match( /^(.*)\/-\/(\w+(?:.\w+)+)$/ )
if( !matched ) return [] as $mol_file[]
const build = this.build()
const [ , rawpath , bundle ] = matched
const mod = build.root().resolve( rawpath )
if( bundle === 'web.css' ) {
this.$.$mol_log3_warn({
place: `${this}.generate()`,
message: 'CSS compiles into JS bundle now',
hint: 'Remove link to web.css',
})
}
const path = mod.path()
return build.bundle( { path , bundle } )
}
expressIndex() {
return (
req : typeof $node.express.request ,
res : typeof $node.express.response ,
next : () => void
) => {
const root = $mol_file.absolute( this.rootPublic() )
const dir = root.resolve( req.path )
const build = this.build()
build.modEnsure( dir.path() )
const match = req.url.match( /(\/|.*[^\-]\/)([\?#].*)?$/ )
if( !match) return next()
const file = root.resolve( `${req.path}index.html` )
if( file.exists() ) {
return res.redirect( 301, `${match[1]}-/test.html${match[2] ?? ''}` )
}
if( dir.type() === 'dir' ) {
const files = [ {name: '-', type: 'dir'} ]
for( const file of dir.sub() ) {
if (!files.find(( {name} ) => name === file.name())) {
files.push( {name: file.name(), type: file.type()} )
}
if( /\.meta\.tree$/.test( file.name() ) ) {
const meta = $$.$mol_tree2_from_string( file.text() )
for( const pack of meta.select( 'pack', null ).kids ) {
if (!files.find(( {name} ) => name === pack.type))
files.push( {name: pack.type, type: 'dir'} )
}
}
}
const html = `
<style>
body {
display: flex;
flex-direction: column;
flex-wrap: wrap;
font: 1rem/1.5rem sans-serif;
height: 100%;
margin: 0;
padding: 0.75rem;
box-sizing: border-box;
}
a {
text-decoration: none;
color: rgb(57, 115, 172);
font-weight: bolder;
}
a:hover {
background: hsl( 0deg, 0%, 0%, .05 )
}
a[href^="."], a[href^="-"], a[href="node_modules"] {
opacity: 0.5;
}
a[href=".."], a[href="-"] {
opacity: 1;
}
</style>
<a href="..">📁 ..</a>
` + files
.sort($mol_compare_text((item) => item.type))
.map( file => `<a href="${file.name}">${file.type === 'dir' ? '📁' : '📄'} ${file.name}</a>` )
.join( '\n' )
res.writeHead( 200, {
'Content-Type': 'text/html',
'Access-Control-Allow-Origin': '*',
} )
return res.end( html )
}
return next()
}
}
port() {
return 9080
}
@ $mol_mem
lines( next = new Map< InstanceType<$node['ws']>, string >() ) {
return next
}
@ $mol_mem
socket() {
return super.socket().on( 'connection' , ( line , req )=> {
const path = req.url!.replace( /\/-.*/ , '' ).substring( 1 )
this.$.$mol_log3_rise({
place: this ,
message: `Connect` ,
path ,
})
this.lines( new Map( [ ... this.lines(), [ line, path ] ] ) )
line.on( 'close' , ()=> {
const lines = new Map( this.lines() )
lines.delete( line )
this.lines( lines )
} )
} )
}
@ $mol_mem
start() {
const socket = this.socket()
for( const [ line, path ] of this.lines() ) {
this.notify([ line, path ])
}
return socket
}
@ $mol_mem_key
notify( [ line, path ]: [ InstanceType<$node['ws']>, string ] ) {
try {
const build = this.build()
const bundle = build.root().resolve( path )
// watch changes
const sources = build.sourcesAll({ path: bundle.path() , exclude : [ 'node' ] })
for( const src of sources ) src.buffer()
} catch (error) {
this.$.$mol_log3_fail({
place: `${this}`,
message: (error as any)?.message,
path
})
}
// ignore initial
if( !$mol_mem_cached( ()=> this.notify([ line, path ]) ) ) return true
this.$.$mol_log3_rise({
place: `${this}`,
message: `$mol_build_obsolete`,
path
})
line.send( '$mol_build_obsolete' )
return true
}
}
}