All files index.js

80.65% Statements 125/155
45.61% Branches 26/57
78.57% Functions 22/28
80.65% Lines 125/155
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 2641x   1x 1x 1x 1x 1x 1x 1x     1x     1x 1x   1x     1x                     6x 3x   3x 3x   3x 3x 3x 3x 3x 3x 3x 3x   3x 3x 3x   3x 3x     1x   1x   1x 3x 3x 3x 3x 3x 3x     1x 3x 3x 3x 3x 3x         3x                 1x 1x 1x 1x 1x       1x 1x       1x 2x 2x 2x         1x 2x 2x 2x 2x     2x 2x       1x 2x 2x 2x 2x     1x             1x 5x 5x 5x 5x     1x 5x 2x   3x 3x 3x 2x   1x     1x 3x     1x 2x     2x 2x 2x 2x         2x             1x         1x 2x 2x 2x 2x 2x 2x 2x     1x 2x   2x 2x   2x       1x                                             1x 2x 2x     1x 2x 2x       1x 5x 3x   2x 2x             1x 3x   3x     1x           1x 6x   6x    
module.exports = Slocket
 
var rimraf = require('rimraf')
var assert = require('assert')
var net = require('net')
var fs = require('fs')
var path = require('path')
var onExit = require('signal-exit')
var locks = Object.create(null)
 
/* istanbul ignore if */
if (typeof Promise === undefined)
  Promise = require('bluebird')
 
var util = require('util')
util.inherits(Slocket, Promise)
 
var debug = function () {}
 
/* istanbul ignore if */
Iif (/\bslocket\b/i.test(process.env.NODE_DEBUG || '')) {
  debug = function () {
    var msg = util.format.apply(util, arguments)
    var n = path.basename(this.name)
    var p = 'SLOCKET:' + process.pid + ':' + n + ' '
    msg = p + msg.trimRight().split('\n').join('\n' + p)
    console.error(msg)
  }
}
 
function Slocket (name, cb) {
  if (!(this instanceof Slocket))
    return new Slocket(name, cb)
 
  this.name = path.resolve(name)
  Iif (cb)
    this.cb = cb
  this.server = null
  this.connection = null
  this.watcher = null
  this.has = false
  this.had = false
  this.acquire()
  this.connectionQueue = []
  this.currentClient = null
 
  this.promise = new Promise(function (resolve, reject) {
    this.resolve = resolve
    this.reject = reject
  }.bind(this))
  this.then = this.promise.then.bind(this.promise)
  this.catch = this.promise.catch.bind(this.promise)
}
 
Slocket.prototype.debug = debug
 
Slocket.prototype.cb = function () {}
 
Slocket.prototype.acquire = function () {
  this.unwatch()
  this.disconnect()
  this.server = net.createServer(this.onServerConnection.bind(this))
  this.server.once('error', this.onServerError.bind(this))
  this.server.listen(this.name, this.onServerListen.bind(this))
  this.server.on('close', this.onServerClose.bind(this))
}
 
Slocket.prototype.onAcquire = function () {
  this.unwatch()
  assert.equal(this.has, false)
  this.has = true
  this.had = true
  this.cb(null, this)
  // Promises are sometimes a little clever
  // when you resolve(<Promise>), it hooks onto the .then method
  // of the promise it's resolving to.  To avoid never actually
  // resolving, we wrap to hide the then/catch methods.
  this.resolve(Object.create(this, {
    then: { value: undefined },
    catch: { value: undefined },
    resolve: { value: undefined },
    reject: { value: undefined },
    release: { value: this.release.bind(this) }
  }))
}
 
Slocket.prototype.onServerListen = function () {
  process.nextTick(function () {
    this.debug('onServerListen', this.server.listening)
    onExit(this.onProcessExit.bind(this))
    this.onAcquire()
  }.bind(this))
}
 
Slocket.prototype.onProcessExit = function () {
  Iif (this.has === true)
    this.release(true)
}
 
Slocket.prototype.onServerConnection = function (c) {
  c.on('close', this.onServerConnectionClose.bind(this, c))
  Eif (this.currentClient || this.has)
    this.connectionQueue.push(c)
  else
    this.delegate(c)
}
 
Slocket.prototype.onServerConnectionClose = function (c) {
  this.debug('onServerConnectionClose', this.has)
  Eif (this.currentClient === c) {
    this.currentClient = null
    this.release()
  }
 
  var i = this.connectionQueue.indexOf(c)
  Iif (i !== -1)
    this.connectionQueue.splice(i, 1)
}
 
Slocket.prototype.delegate = function (c) {
  assert.equal(this.has, false)
  this.debug('delegate new client')
  this.currentClient = c
  c.write('OK')
}
 
Slocket.prototype.type = function () {
  return !this.has ? 'none'
    : this.server && this.server.listening ? 'server'
    : this.connection ? 'connection'
    : 'wtf'
}
 
Slocket.prototype.release = function (sync) {
  this.has = false
  this.debug('release')
  this.connectionRelease(sync)
  this.serverRelease(sync)
}
 
Slocket.prototype.serverRelease = function (sync) {
  if (!this.server)
    return
 
  this.debug('serverRelease %j', sync, this.connectionQueue.length)
  this.server.unref()
  if (this.connectionQueue.length)
    this.delegate(this.connectionQueue.shift())
  else
    this.server.close()
}
 
Slocket.prototype.onServerClose = function () {
  this.server = null
}
 
Slocket.prototype.onServerError = function (er) {
  this.debug('onServerError', er.message)
  // XXX just in the off chance this happens later, kill any connections
  // and destroy the server
  Eif (this.server)
    this.server.close()
  this.server = null
  switch (er.code) {
    case 'ENOTSOCK':
      return this.watch()
    case 'EADDRINUSE':
    case 'EEXIST':
      return this.connect()
    default:
      er.slocket = 'server'
      this.onError(er)
  }
}
 
Slocket.prototype.onError = function (er) {
  this.cb(er, this)
  this.reject(er)
}
 
Slocket.prototype.connect = function () {
  this.connection = net.createConnection(this.name)
  this.connection.slocketBuffer = ''
  this.connection.setEncoding('utf8')
  this.connection.slocketConnected = false
  this.connection.on('connect', this.onConnect.bind(this))
  this.connection.on('error', this.onConnectionError.bind(this))
  this.connection.on('data', this.onConnectionData.bind(this))
}
 
Slocket.prototype.onConnectionData = function (chunk) {
  this.connection.slocketBuffer += chunk
 
  Eif (this.connection.slocketBuffer === 'OK')
    this.onAcquire()
 
  Iif (this.connection.slocketBuffer.length > 2)
    this.connection.destroy()
}
 
Slocket.prototype.onConnectionError = function (er) {
  if (this.has)
    return this.onError(er)
  this.connection = null
  switch (er.code) {
    case 'ENOENT':
      // socket was there, but now is gone!
      return this.acquire()
    case 'ECONNREFUSED':
      // socket there, but not listening
      // watch for changes, in case it's that it's not a socket
      // if that fails, eg for "unknown system error", then just retry
      try {
        return this.watch()
      } catch (er) {
        return this.acquire()
      }
    default:
      er.slocket = 'connection'
      this.onError(er)
  }
}
 
Slocket.prototype.onConnect = function () {
  this.connection.slocketConnected = true
  this.connection.on('close', this.onConnectionClose.bind(this))
}
 
Slocket.prototype.onConnectionClose = function () {
  this.connection.slocketConnected = false
  Iif (!this.had)
    this.acquire()
}
 
Slocket.prototype.connectionRelease = function (sync) {
  if (!this.connection)
    return
 
  Eif (this.connection.slocketConnected)
    this.connection.destroy()
  else if (sync)
    rimraf.sync(this.name)
  else
    rimraf(this.name, function () {})
}
 
Slocket.prototype.disconnect = function () {
  Iif (this.connection)
    this.connection.destroy()
  this.connection = null
}
 
Slocket.prototype.watch = function () {
  this.watcher = fs.watch(this.name, { persistent: false })
  this.watcher.on('change', this.acquire.bind(this))
  this.watch.on('error', this.acquire.bind(this))
}
 
Slocket.prototype.unwatch = function () {
  Iif (this.watcher)
    this.watcher.close()
  this.watcher = null
}