Newer
Older
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
// ltv system
const express = require('express')
const app = express()
// this include lets us read data out of put requests,
const bodyparser = require('body-parser')
// our fs tools,
const fs = require('fs')
//const filesys = require('./filesys.js')
// and we occasionally spawn local pipes (workers)
const child_process = require('child_process')
// will use these to figure where tf we are
let ownIp = ''
const os = require('os')
// serve everything: https://expressjs.com/en/resources/middleware/serve-static.html
app.use(express.static(__dirname))
// accept post bodies as json,
app.use(bodyparser.json())
app.use(bodyparser.urlencoded({ extended: true }))
// redirect traffic to /client,
app.get('/', (req, res) => {
res.redirect('/client')
})
// we also want to institute some pipes: this is a holdover for a better system
// more akin to nautilus, where server-side graphs are manipulated
// for now, we just want to dive down to a usb port, probably, so this shallow link is OK
let processes = []
app.get('/startLocal/:file', (req, res) => {
// launches another node instance at this file w/ these args,
let args = ''
if (req.query.args) {
args = req.query.args.split(',')
}
console.log(`attempt to start ${req.params.file} with args ${args}`)
// startup, let's spawn,
const process = child_process.spawn('node', ['-r', 'esm', `local/${req.params.file}`])
// add our own tag,
process.fileName = req.params.file
let replied = false
let pack = ''
process.stdout.on('data', (buf) => {
// these emerge as buffers,
let msg = buf.toString()
// can only reply once to xhr req
if (msg.includes('wss-addr:') && !replied) {
res.send(msg)
replied = true
}
// ok, dealing w/ newlines
pack = pack.concat(msg)
let index = pack.indexOf('\n')
while (index >= 0) {
console.log(`${process.fileName} ${process.pid}: ${pack.substring(0, index)}`)
pack = pack.slice(index + 1)
index = pack.indexOf('\n')
}
})
process.stderr.on('data', (err) => {
if (!replied) {
res.send('err in local script')
replied = true
}
console.log(`${process.fileName} ${process.pid} err:`, err.toString())
})
process.on('close', (code) => {
console.log(`${process.fileName} ${process.pid} closes:`, code)
if (!replied) {
res.send('local process closed')
replied = true
}
})
console.log(`started ${process.fileName} w/ pid ${process.pid}`)
})
// finally, we tell the express server to listen here:
let port = 8080
app.listen(port)
// once we're listening, report our IP:
let ifaces = os.networkInterfaces()
// this just logs the processes IP's to the termina
Object.keys(ifaces).forEach(function (ifname) {
var alias = 0;
ifaces[ifname].forEach(function (iface) {
if ('IPv4' !== iface.family) {//} || iface.internal !== false) {
// skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
return;
}
ownIp = iface.address
if (alias >= 1) {
console.log('ltv available on: \t' /*ifname + ':' + alias,*/ + iface.address + `:${port}`);
// this single interface has multiple ipv4 addresses
// console.log('serving at: ' ifname + ':' + alias + iface.address + `:${port}`);
} else {
console.log('ltv available on:\t' /*ifname + ':' + alias,*/ + iface.address + `:${port}`);
// this interface has only one ipv4 adress
//console.log(ifname, iface.address);
}
++alias;
});
});