Skip to content

Commit

Permalink
fix: address already in use 0.0.0.0:3000 with Node 21 (#1685)
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyguerra authored Oct 25, 2023
1 parent 34c34f6 commit 36f3fd8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
62 changes: 34 additions & 28 deletions src/robot.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Robot {
this.brain = new Brain(this)
this.alias = alias
this.adapter = null
this.shouldEnableHttpd = httpd ?? true
this.datastore = null
this.Response = Response
this.commands = []
Expand All @@ -55,18 +56,13 @@ class Robot {
this.globalHttpOptions = {}

this.parseVersion()
if (httpd) {
this.setupExpress()
} else {
this.setupNullRouter()
}

this.adapterName = adapter ?? 'shell'
this.errorHandlers = []

this.on('error', (err, res) => {
return this.invokeErrorHandlers(err, res)
})
this.on('listening', this.herokuKeepalive.bind(this))
}

// Public: Adds a custom Listener with the provided matcher, options, and
Expand Down Expand Up @@ -407,8 +403,8 @@ class Robot {

// Setup the Express server's defaults.
//
// Returns nothing.
setupExpress () {
// Returns Server.
async setupExpress () {
const user = process.env.EXPRESS_USER
const pass = process.env.EXPRESS_PASSWORD
const stat = process.env.EXPRESS_STATIC
Expand Down Expand Up @@ -444,27 +440,18 @@ class Robot {
if (stat) {
app.use(express.static(stat))
}

try {
this.server = app.listen(port, address)
this.router = app
} catch (error) {
this.logger.error(`Error trying to start HTTP server: ${error}\n${error.stack}`)
throw error
}

let herokuUrl = process.env.HEROKU_URL

if (herokuUrl) {
if (!/\/$/.test(herokuUrl)) {
herokuUrl += '/'
}
this.pingIntervalId = setInterval(() => {
HttpClient.create(`${herokuUrl}hubot/ping`).post()((_err, res, body) => {
this.logger.info('keep alive ping!')
const p = new Promise((resolve, reject) => {
try {
this.server = app.listen(port, address, () => {
this.router = app
this.emit('listening', this.server)
resolve(this.server)
})
}, 5 * 60 * 1000)
}
} catch (err) {
reject(err)
}
})
return p
}

// Setup an empty router object
Expand Down Expand Up @@ -645,6 +632,11 @@ class Robot {
//
// Returns whatever the adapter returns.
async run () {
if (this.shouldEnableHttpd) {
await this.setupExpress()
} else {
this.setupNullRouter()
}
this.emit('running')

return await this.adapter.run()
Expand Down Expand Up @@ -712,6 +704,20 @@ class Robot {

return HttpClient.create(url, httpOptions).header('User-Agent', `Hubot/${this.version}`)
}

herokuKeepalive (server) {
let herokuUrl = process.env.HEROKU_URL
if (herokuUrl) {
if (!/\/$/.test(herokuUrl)) {
herokuUrl += '/'
}
this.pingIntervalId = setInterval(() => {
HttpClient.create(`${herokuUrl}hubot/ping`).post()((_err, res, body) => {
this.logger.info('keep alive ping!')
})
}, 5 * 60 * 1000)
}
}
}

module.exports = Robot
Expand Down
6 changes: 4 additions & 2 deletions test/robot_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1007,16 +1007,18 @@ describe('Robot', () => {
})
describe('Robot HTTP Service', () => {
it('should start a web service', async () => {
process.env.PORT = 3000
process.env.PORT = 0
hook('hubot-mock-adapter', mockAdapter)
const robot = new Robot('hubot-mock-adapter', true, 'TestHubot')
await robot.loadAdapter()
await robot.run()
const res = await fetch(`http://127.0.0.1:${process.env.PORT}/hubot/version`)
const port = robot.server.address().port
const res = await fetch(`http://127.0.0.1:${port}/hubot/version`)
assert.equal(res.status, 404)
assert.match(await res.text(), /Cannot GET \/hubot\/version/ig)
robot.shutdown()
reset()
delete process.env.PORT
})
})
})

0 comments on commit 36f3fd8

Please sign in to comment.