fix file delete (unlink) so catches error uses promise

tls
David Kebler 2018-05-27 13:05:38 -07:00
parent 949bd5b3b1
commit 36d1cf629f
3 changed files with 17 additions and 12 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@uci/socket", "name": "@uci/socket",
"version": "0.1.3", "version": "0.1.4",
"description": "JSON packet intra(named)/inter(TCP) host communication over socket", "description": "JSON packet intra(named)/inter(TCP) host communication over socket",
"main": "src", "main": "src",
"scripts": { "scripts": {
@ -38,17 +38,17 @@
"devDependencies": { "devDependencies": {
"chai": "^4.1.2", "chai": "^4.1.2",
"chai-as-promised": "^7.1.1", "chai-as-promised": "^7.1.1",
"codecov": "^3.0.0", "codecov": "^3.0.2",
"esm": "^3.0.21", "esm": "^3.0.37",
"istanbul": "^0.4.5", "istanbul": "^0.4.5",
"mocha": "^5.0.1", "mocha": "^5.2.0",
"nodemon": "^1.15.1" "nodemon": "^1.17.5"
}, },
"dependencies": { "dependencies": {
"@uci/logger": "^0.0.2", "@uci/logger": "0.0.3",
"better-try-catch": "^0.6.2", "better-try-catch": "^0.6.2",
"clone": "^2.1.1", "clone": "^2.1.1",
"death": "^1.1.0", "death": "^1.1.0",
"make-dir": "^1.2.0" "make-dir": "^1.3.0"
} }
} }

View File

@ -105,7 +105,7 @@ export default class Consumer extends Socket {
let res = await this._packetProcess(reply) let res = await this._packetProcess(reply)
if (!res) { // if process was not promise returning like just logged to console if (!res) { // if process was not promise returning like just logged to console
res = reply res = reply
log.warn('consumer function was not promise returning - resolving unprocessed') log.warn('consumer function was not promise returning further processing may be out of sequence')
} }
resolve(res) resolve(res)
}) //end listener }) //end listener
@ -143,7 +143,7 @@ export default class Consumer extends Socket {
// TODO do some extra security here? // TODO do some extra security here?
let res = await this._packetProcess(packet) let res = await this._packetProcess(packet)
if (!res) { // if process was not promise returning like just logged to console if (!res) { // if process was not promise returning like just logged to console
log.warn('consumer function was not promise returning - resolving unprocessed') log.warn('consumer function was not promise returning')
} }
}) })
// listen on socket stream // listen on socket stream

View File

@ -1,5 +1,6 @@
import { Server } from 'net' import { Server } from 'net'
import { unlink as fileDelete } from 'fs' import { unlink as fileDelete } from 'fs'
import { promisify } from 'util'
import path from 'path' import path from 'path'
import mkdir from 'make-dir' import mkdir from 'make-dir'
import btc from 'better-try-catch' import btc from 'better-try-catch'
@ -52,9 +53,13 @@ export default class Socket extends Server {
// recover from socket file that was not removed // recover from socket file that was not removed
if (err.code === 'EADDRINUSE') { if (err.code === 'EADDRINUSE') {
if (this.opts.path) { // if TCP socket should already be dead if (this.opts.path) { // if TCP socket should already be dead
log.warn({socket: this.opts.path}, 'socket already exists...deleting') let [err, res] = log.info(await btc(promisify(fileDelete))(this.opts.path))
await fileDelete(this.opts.path) if(!err) {
return await this._listen(this.opts) log.info({res:res, socket: this.opts.path}, 'socket already exists.....deleted')
return await this._listen(this.opts)
}
log.fatal({err:err},'error deleting socket. Can not establish a socket')
return err
} }
} }
if (err.code ==='EACCES'){ if (err.code ==='EACCES'){