Skip to content

Commit b67ffdc

Browse files
committed
refactor: webserver into independent module
1 parent 16f2f8f commit b67ffdc

4 files changed

Lines changed: 174 additions & 80 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"exports": {
2020
".": "./src/datdot-node-javascript.js",
21+
"./start.js": "./start.js",
2122
"./*": "./src/modules/*.js"
2223
},
2324
"engines": {

src/datdot-node-javascript.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const DB = require('./DB')
1515
const blockgenerator = require('./scheduleAction.js')
1616
const b4a = require('b4a')
1717

18-
const { io } = vault
18+
const { io } = globalThis.vault
1919

2020
const queries = {
2121
getItemByID,

src/modules/webserver.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
const { createServer } = require('http')
2+
const { WebSocketServer } = require('ws')
3+
4+
const { io } = globalThis.vault
5+
6+
module.exports = webserver
7+
8+
function webserver ({ port = 8080, host = '127.0.0.1', flag = '' }) {
9+
const wss = new WebSocketServer({ noServer: true })
10+
wss.on('connection', onconnection)
11+
const server = createServer(handlerX) // HANDLER syscall
12+
server.on('upgrade', onupgrade) // ONUPGRADE syscall
13+
server.listen(port, host, onlisten)
14+
process.on('SIGINT', onterminate)
15+
16+
// --------------------------------------------------------------------------
17+
18+
function onconnection (ws, request) {
19+
const { url: path } = request
20+
console.log('CONNECTION', { path }) // log
21+
// const send = protocol(message => ws.send(message))
22+
23+
// const send = io.to(path, message => {
24+
// // @TODO: THINK HOW DOES IT REALLY WORK?!?
25+
// // 1. create logkeeper listener with websocket intention
26+
// })
27+
28+
io.to(path, { ws, request })
29+
30+
// const { id, name, stack, fn: protocol } = ROUTES[path]
31+
// const { protocol } = ws
32+
// ws.on('open', event => send(event))
33+
// ws.on('error', event => send(event))
34+
// ws.on('close', event => send(event))
35+
// ws.on('message', event => send(event))
36+
}
37+
38+
function handlerX (request, response) {
39+
const { url } = request
40+
const info = {}
41+
// const info = {
42+
// publickey,
43+
// topics,
44+
// prioritized,
45+
// ban(status = true)
46+
// }
47+
// const stream = io.to('http', info)
48+
// request.pipe(stream).pipe(response)
49+
io.to(url, { request, response })
50+
}
51+
/************************************/
52+
async function onupgrade (request, socket, head) {
53+
socket.on('error', onerror)
54+
try { await authenticate(request) } catch (error) {
55+
console.error('ERROR', error)
56+
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n')
57+
socket.destroy()
58+
}
59+
socket.removeListener('error', onerror)
60+
const { url } = request
61+
62+
// try {
63+
// io.to(url, { request, response })
64+
// } catch (error) {
65+
// }
66+
// const on = ROUTES[url] // @TODO: ...
67+
if (io.has(url)) return wss.handleUpgrade(request, socket, head, ws => {
68+
wss.emit('connection', ws, request)
69+
})
70+
socket.write('HTTP/1.1 404 Not Found\r\n\r\n')
71+
socket.destroy()
72+
}
73+
function onlisten () {
74+
const { address, family, port } = server.address()
75+
const url = `http://${address}:${port}`
76+
console.log('webserver listening on', url)
77+
if (flag !== 'open') return
78+
const command = `${{ darwin: 'open', win32: 'start' }[process.platform] || 'xdg-open'/*linux*/} ${url}`
79+
require('child_process').exec(command)
80+
}
81+
function onterminate () {
82+
console['log']("\n Terminating all processes")
83+
process.exit()
84+
}
85+
function onerror (error) { console.log('[ws]:error>', error) }
86+
function authenticate (request) { return request }
87+
}

start.js

Lines changed: 85 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -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 }
5353
const 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')
146146
process.env.DEBUG = '*,-hypercore-protocol'
147147
const port = process.env.PORT || 8080
148148
const host = '0.0.0.0' // '127.0.0.1' || process.env.HOST_ADDRESS
149+
console.log('PORT:', process.env.PORT)
149150

150151
const logport = 9001
151152
console.log('HOST+PORT', process.env.HOST_ADDRESS, process.env.PORT)
152153
if (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)
157160
console.log({flag})
161+
console.log({json})
158162
process.argv.push(json, logport)
159163
/**************************************
160164
LAUNCH CHAIN NODE
161165
*************************************/
162166
const script = require('datdot-node-javascript/devtools')
163167
const init = require('datdot-node-javascript')
168+
const webserver = require('datdot-node-javascript/webserver')
164169
init(JSON.parse(json))
165170

166-
171+
webserver({ host, port, flag })
167172

168173
LESEZEICHEN: `@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 }
247253
function handler ({ request, response }) {
248254
const { url } = request
249255
console.log('[http]', url)

0 commit comments

Comments
 (0)