Skip to content

Commit

Permalink
Merge pull request #237 from harvard-lil/proxy-eacces-handling
Browse files Browse the repository at this point in the history
Adds handling for EACCES/EADDRINUSE error on proxy init
  • Loading branch information
matteocargnelutti authored Nov 3, 2023
2 parents 60dd859 + b1d0688 commit c1db32e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 5 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ program.action(async (name, options, command) => {
try {
url = command.processedArgs[0]
capture = await Scoop.capture(url, options)

// A failed capture should make the CLI bail
if (!capture || capture?.state === Scoop.states.FAILED) {
throw new Error()
}
} catch (err) {
// Logs are handled by Scoop directly, unless `Scoop.capture` fails during initialization
if (!capture) {
Expand Down
15 changes: 13 additions & 2 deletions intercepters/ScoopProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ export class ScoopProxy extends ScoopIntercepter {

/**
* Initializes the proxy server
* @returns {Promise<void>}
*/
setup () {
return new Promise(resolve => {
return new Promise((resolve, reject) => {
let connected = false

this.#connection = createServer({
requestTransformer: this.requestTransformer.bind(this),
responseTransformer: this.responseTransformer.bind(this),
Expand All @@ -46,9 +49,17 @@ export class ScoopProxy extends ScoopIntercepter {
.on('request', this.onRequest.bind(this))
.on('connected', this.onConnected.bind(this))
.on('response', this.onResponse.bind(this))
.on('error', this.onError.bind(this))
.on('error', (err, serverRequest, clientRequest) => {
// Special handling of EACCES/EADDRINUSE on init
if (!connected && err && ['EACCES', 'EADDRINUSE'].includes(err?.code)) {
reject(new Error('TCP-Proxy-Server was unable to access port'))
}

this.onError(err, serverRequest, clientRequest)
})
.listen(this.options.proxyPort, this.options.proxyHost, () => {
this.capture.log.info(`TCP-Proxy-Server started ${JSON.stringify(this.#connection.address())}`)
connected = true
resolve()
})
})
Expand Down

0 comments on commit c1db32e

Please sign in to comment.