@@ -48,7 +48,7 @@ const { login } = require('datdot-service/src/node_modules/datdot-vault')
4848/*************************************
4949 VAULT USE
5050*************************************/
51- const { io } = vault
51+ const { io } = globalThis . vault
5252// { const { id, on, db, at } = { io, db, id } = vault }
5353const at1 = io ( 'livereload' , function on ( message ) {
5454 console . log ( '@TODO: livereload' )
@@ -135,8 +135,8 @@ function onhttp (stream, info) { // onopen
135135/**************************************
136136 START
137137*************************************/
138- const { createServer } = require ( 'http' )
139- const { WebSocketServer } = require ( 'ws' )
138+ // const { createServer } = require('http')
139+ // const { WebSocketServer } = require('ws')
140140/**************************************
141141 CONFIGURE CHAIN NODE
142142 glitch port : 3000
@@ -146,104 +146,110 @@ const { WebSocketServer } = require('ws')
146146process . env . DEBUG = '*,-hypercore-protocol'
147147const port = process . env . PORT || 8080
148148const host = '0.0.0.0' // '127.0.0.1' || process.env.HOST_ADDRESS
149+ console . log ( 'PORT:' , process . env . PORT )
149150
150151const logport = 9001
151152console . log ( 'HOST+PORT' , process . env . HOST_ADDRESS , process . env . PORT )
152153if ( process . env . DEBUG ) console . log ( 'DEBUG' , process . env . DEBUG )
153- const json = JSON . stringify ( {
154- chain : [ process . env . HOST_ADDRESS || '0.0.0.0' , 3399 ]
155- } )
156- const [ flag ] = process . argv . slice ( 2 )
154+ const [
155+ flag ,
156+ json = JSON . stringify ( {
157+ chain : [ process . env . HOST_ADDRESS || '0.0.0.0' , 3399 ]
158+ } )
159+ ] = process . argv . slice ( 2 )
157160console . log ( { flag} )
161+ console . log ( { json} )
158162process . argv . push ( json , logport )
159163/**************************************
160164 LAUNCH CHAIN NODE
161165*************************************/
162166const script = require ( 'datdot-node-javascript/devtools' )
163167const init = require ( 'datdot-node-javascript' )
168+ const webserver = require ( 'datdot-node-javascript/webserver' )
164169init ( JSON . parse ( json ) )
165170
166-
171+ webserver ( { host , port , flag } )
167172
168173LESEZEICHEN: `@TODO: fix shit! 2`
169174
170175
171- const wss = new WebSocketServer ( { noServer : true } )
172- wss . on ( 'connection' , onconnection )
173- function onconnection ( ws , request ) {
174- const { url : path } = request
175- console . log ( 'CONNECTION' , { path } ) // log
176- // const send = protocol(message => ws.send(message))
176+ // const wss = new WebSocketServer({ noServer: true })
177+ // wss.on('connection', onconnection)
178+ // function onconnection (ws, request) {
179+ // const { url: path } = request
180+ // console.log('CONNECTION', { path }) // log
181+ // // const send = protocol(message => ws.send(message))
177182
178- // const send = io.to(path, message => {
179- // // @TODO : THINK HOW DOES IT REALLY WORK?!?
180- // // 1. create logkeeper listener with websocket intention
181- // })
183+ // // const send = io.to(path, message => {
184+ // // // @TODO : THINK HOW DOES IT REALLY WORK?!?
185+ // // // 1. create logkeeper listener with websocket intention
186+ // // })
182187
183- io . to ( path , { ws, request } )
188+ // io.to(path, { ws, request })
184189
185- // const { id, name, stack, fn: protocol } = ROUTES[path]
186- // const { protocol } = ws
187- // ws.on('open', event => send(event))
188- // ws.on('error', event => send(event))
189- // ws.on('close', event => send(event))
190- // ws.on('message', event => send(event))
191- }
192- // --------------------------------------------------------------------------
193- const server = createServer ( handlerX ) // HANDLER syscall
194- server . on ( 'upgrade' , onupgrade ) // ONUPGRADE syscall
190+ // // const { id, name, stack, fn: protocol } = ROUTES[path]
191+ // // const { protocol } = ws
192+ // // ws.on('open', event => send(event))
193+ // // ws.on('error', event => send(event))
194+ // // ws.on('close', event => send(event))
195+ // // ws.on('message', event => send(event))
196+ // }
197+ // // --------------------------------------------------------------------------
198+ // const server = createServer(handlerX) // HANDLER syscall
199+ // server.on('upgrade', onupgrade) // ONUPGRADE syscall
195200
196- function handlerX ( request , response ) {
197- const { url } = request
198- const info = { }
199- // const info = {
200- // publickey,
201- // topics,
202- // prioritized,
203- // ban(status = true)
204- // }
205- // const stream = io.to('http', info)
206- // request.pipe(stream).pipe(response)
207- io . to ( url , { request, response } )
208- }
209- server . listen ( port , host , onlisten )
210- process . on ( 'SIGINT' , onterminate )
211- /************************************/
212- async function onupgrade ( request , socket , head ) {
213- socket . on ( 'error' , onerror )
214- try { await authenticate ( request ) } catch ( error ) {
215- console . error ( 'ERROR' , error )
216- socket . write ( 'HTTP/1.1 401 Unauthorized\r\n\r\n' )
217- socket . destroy ( )
218- }
219- socket . removeListener ( 'error' , onerror )
220- const { url } = request
201+ // function handlerX (request, response) {
202+ // const { url } = request
203+ // const info = {}
204+ // // const info = {
205+ // // publickey,
206+ // // topics,
207+ // // prioritized,
208+ // // ban(status = true)
209+ // // }
210+ // // const stream = io.to('http', info)
211+ // // request.pipe(stream).pipe(response)
212+ // io.to(url, { request, response })
213+ // }
214+ // server.listen(port, host, onlisten)
215+ // process.on('SIGINT', onterminate)
216+ // /************************************/
217+ // async function onupgrade (request, socket, head) {
218+ // socket.on('error', onerror)
219+ // try { await authenticate(request) } catch (error) {
220+ // console.error('ERROR', error)
221+ // socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n')
222+ // socket.destroy()
223+ // }
224+ // socket.removeListener('error', onerror)
225+ // const { url } = request
226+
227+ // // try {
228+ // // io.to(url, { request, response })
229+ // // } catch (error) {
230+ // // }
231+ // // const on = ROUTES[url] // @TODO: ...
232+ // if (io.has(url)) return wss.handleUpgrade(request, socket, head, ws => {
233+ // wss.emit('connection', ws, request)
234+ // })
235+ // socket.write('HTTP/1.1 404 Not Found\r\n\r\n')
236+ // socket.destroy()
237+ // }
238+ // function onlisten () {
239+ // const { address, family, port } = server.address()
240+ // const url = `http://${address}:${port}`
241+ // console.log('webserver listening on', url)
242+ // if (flag !== 'open') return
243+ // const command = `${{ darwin: 'open', win32: 'start' }[process.platform] || 'xdg-open'/*linux*/} ${url}`
244+ // require('child_process').exec(command)
245+ // }
246+ // function onterminate () {
247+ // console['log']("\n Terminating all processes")
248+ // process.exit()
249+ // }
250+ // function onerror (error) { console.log('[ws]:error>', error) }
251+ // function authenticate (request) { return request }
221252
222- // try {
223- // io.to(url, { request, response })
224- // } catch (error) {
225- // }
226- // const on = ROUTES[url] // @TODO: ...
227- if ( io . has ( url ) ) return wss . handleUpgrade ( request , socket , head , ws => {
228- wss . emit ( 'connection' , ws , request )
229- } )
230- socket . write ( 'HTTP/1.1 404 Not Found\r\n\r\n' )
231- socket . destroy ( )
232- }
233- function onlisten ( ) {
234- const { address, family, port } = server . address ( )
235- const url = `http://${ address } :${ port } `
236- console . log ( 'webserver listening on' , url )
237- if ( flag !== 'open' ) return
238- const command = `${ { darwin : 'open' , win32 : 'start' } [ process . platform ] || 'xdg-open' /*linux*/ } ${ url } `
239- require ( 'child_process' ) . exec ( command )
240- }
241- function onterminate ( ) {
242- console [ 'log' ] ( "\n Terminating all processes" )
243- process . exit ( )
244- }
245- function onerror ( error ) { console . log ( '[ws]:error>' , error ) }
246- function authenticate ( request ) { return request }
247253function handler ( { request, response } ) {
248254 const { url } = request
249255 console . log ( '[http]' , url )
0 commit comments