-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
executable file
·150 lines (134 loc) · 4.7 KB
/
index.js
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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const commandLineArgs = require('command-line-args')
const NODE_MAJOR_VERSION = process.versions.node.split('.')[0]
if (NODE_MAJOR_VERSION > 14) {
const dns = require('node:dns')
dns.setDefaultResultOrder('ipv4first')
}
const { Launcher } = require('./lib/launcher')
const { AdminInterface } = require('./lib/admin')
const { filesInterface } = require('./lib/files')
const cmdLineOptions = [
{ name: 'port', alias: 'p', type: Number },
{ name: 'forgeURL', type: String },
{ name: 'team', alias: 't', type: String },
{ name: 'project', type: String },
{ name: 'token', type: String },
{ name: 'buffer', alias: 'b', type: Number },
{ name: 'nodeRedPath', alias: 'n', type: String },
{ name: 'credentialSecret', type: String },
{ name: 'no-tcp-in', alias: 'T', type: Boolean },
{ name: 'no-udp-in', alias: 'U', type: Boolean }
]
const options = commandLineArgs(cmdLineOptions)
options.forgeURL = options.forgeURL || process.env.FORGE_URL
options.team = options.team || process.env.FORGE_TEAM_ID
options.project = options.project || process.env.FORGE_PROJECT_ID
options.token = options.token || process.env.FORGE_PROJECT_TOKEN
options.logBufferMax = options.logBufferMax || 1000
options.nodeRedPath = options.nodeRedPath || process.env.FORGE_NR_PATH
// Boolean Options
const parseBoolean = (val, _default) => {
if (val === true || val === false) { return val }
if (val === 'true' || val === 'TRUE') { return true }
if (val === 'false' || val === 'FALSE') { return false }
return _default
}
const noTcp = parseBoolean(options['no-tcp-in'], parseBoolean(process.env.FORGE_NR_NO_TCP_IN), undefined)
const noUdp = parseBoolean(options['no-udp-in'], parseBoolean(process.env.FORGE_NR_NO_UDP_IN), undefined)
options.allowInboundTcp = noTcp === undefined ? undefined : !noTcp
options.allowInboundUdp = noUdp === undefined ? undefined : !noTcp
if (process.env.FORGE_BROKER_URL && process.env.FORGE_BROKER_USERNAME && process.env.FORGE_BROKER_PASSWORD) {
options.broker = {
url: process.env.FORGE_BROKER_URL,
username: process.env.FORGE_BROKER_USERNAME,
password: process.env.FORGE_BROKER_PASSWORD
}
}
const ext = process.platform === 'win32' ? '.cmd' : ''
options.execPath = undefined
if (options.nodeRedPath) {
options.execPath = path.join(options.nodeRedPath, 'node_modules', '.bin', `node-red${ext}`)
if (!fs.existsSync(options.execPath)) {
options.execPath = undefined
}
}
if (!options.execPath) {
// Find the bundled version
for (let i = 0; i < require.main.paths.length; i++) {
const execPath = path.join(require.main.paths[i], '.bin', `node-red${ext}`)
if (fs.existsSync(execPath)) {
options.execPath = execPath
break
}
}
}
if (!options.execPath) {
console.log(require.main.paths)
console.log('executable not found')
process.exit(1)
}
// Gather versions numbers for reporting to the platform
options.versions = {
node: process.version.replace(/^v/, ''),
launcher: require('./package.json').version
}
// Go find Node-RED's package.json
const nrModulePath = path.relative(__dirname, path.join(path.dirname(options.execPath), '..', 'node-red', 'package.json'))
try {
const nrPkg = require(nrModulePath)
options.versions['node-red'] = nrPkg.version
} catch (err) {
options.versions['node-red'] = err.toString()
}
async function main () {
const launcher = new Launcher(options)
const adminInterface = new AdminInterface(options, launcher)
adminInterface.start()
process.on('SIGTERM', async () => {
await launcher.stop()
process.exit(0)
})
try {
await launcher.loadSettings()
await launcher.start()
} catch (error) {
await launcher.logAuditEvent('start-failed', { error })
}
filesInterface(adminInterface.app, launcher.settings)
// const wss = new ws.Server({ clientTracking: false, noServer: true })
//
// server.on('upgrade', (req, socket, head) => {
// if (req.url === '/flowforge/logs') {
// wss.handleUpgrade(req, socket, head, (ws) => {
// wss.emit('connection', ws, req)
// })
// }
// })
//
//
// wss.on('connection', (ws, req) => {
// logBuffer.forEach(log => {
// if (log) {
// ws.send(log)
// }
// })
//
// function wsLogSend(msg) {
// ws.send(msg)
// }
//
// logEmmiter.on('log', wsLogSend)
//
// ws.on('close', () => {
// logEmmiter.removeListener('log',wsLogSend)
// })
// })
//
//
// let settings = await getSettings()
// await start(settings)
}
main()