index.js 12.2 KB
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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
module.exports = wrap
wrap.runMain = runMain

var Module = require('module')
var fs = require('fs')
var cp = require('child_process')
var ChildProcess = cp.ChildProcess
var assert = require('assert')
var crypto = require('crypto')
var mkdirp = require('mkdirp')
var rimraf = require('rimraf')
var path = require('path')
var signalExit = require('signal-exit')
var home = process.env.SPAWN_WRAP_SHIM_ROOT || require('os-homedir')()
var homedir = home + '/.node-spawn-wrap-'
var which = require('which')
var util = require('util')

var doDebug = process.env.SPAWN_WRAP_DEBUG === '1'
var debug = doDebug ? function () {
  var message = util.format.apply(util, arguments).trim()
  var pref = 'SW ' + process.pid + ': '
  message = pref + message.split('\n').join('\n' + pref)
  process.stderr.write(message + '\n')
} : function () {}

var shebang = process.platform === 'os390' ?
  '#!/bin/env ' : '#!'

var shim = shebang + process.execPath + '\n' +
  fs.readFileSync(__dirname + '/shim.js')

var isWindows = require('./lib/is-windows')()

var pathRe = /^PATH=/
if (isWindows) pathRe = /^PATH=/i

var colon = isWindows ? ';' : ':'

function wrap (argv, env, workingDir) {
  if (!ChildProcess) {
    var child = cp.spawn(process.execPath, [])
    ChildProcess = child.constructor
    if (process.platform === 'os390')
      child.kill('SIGABRT')
    else
      child.kill('SIGKILL')
  }

  // spawn_sync available since Node v0.11
  var spawnSyncBinding, spawnSync
  try {
    spawnSyncBinding = process.binding('spawn_sync')
  } catch (e) {}

  // if we're passed in the working dir, then it means that setup
  // was already done, so no need.
  var doSetup = !workingDir
  if (doSetup) {
    workingDir = setup(argv, env)
  }
  var spawn = ChildProcess.prototype.spawn
  if (spawnSyncBinding) {
    spawnSync = spawnSyncBinding.spawn
  }

  function unwrap () {
    if (doSetup && !doDebug) {
      rimraf.sync(workingDir)
    }
    ChildProcess.prototype.spawn = spawn
    if (spawnSyncBinding) {
      spawnSyncBinding.spawn = spawnSync
    }
  }

  if (spawnSyncBinding) {
    spawnSyncBinding.spawn = wrappedSpawnFunction(spawnSync, workingDir)
  }
  ChildProcess.prototype.spawn = wrappedSpawnFunction(spawn, workingDir)

  return unwrap
}

function wrappedSpawnFunction (fn, workingDir) {
  return wrappedSpawn

  function wrappedSpawn (options) {
    munge(workingDir, options)
    debug('WRAPPED', options)
    return fn.call(this, options)
  }
}

function isSh (file) {
  return file === 'dash' ||
         file === 'sh' ||
         file === 'bash' ||
         file === 'zsh'
}

function mungeSh (workingDir, options) {
  var cmdi = options.args.indexOf('-c')
  if (cmdi === -1)
    return // no -c argument

  var c = options.args[cmdi + 1]
  var re = /^\s*((?:[^\= ]*\=[^\=\s]*)*[\s]*)([^\s]+|"[^"]+"|'[^']+')( .*)?$/
  var match = c.match(re)
  if (!match)
    return // not a command invocation.  weird but possible

  var command = match[2]
  // strip quotes off the command
  var quote = command.charAt(0)
  if ((quote === '"' || quote === '\'') && quote === command.slice(-1)) {
    command = command.slice(1, -1)
  }
  var exe = path.basename(command)

  if (isNode(exe)) {
    options.originalNode = command
    c = match[1] + match[2] + ' "' + workingDir + '/node" ' + match[3]
    options.args[cmdi + 1] = c
  } else if (exe === 'npm' && !isWindows) {
    // XXX this will exhibit weird behavior when using /path/to/npm,
    // if some other npm is first in the path.
    var npmPath = whichOrUndefined('npm')

    if (npmPath) {
      c = c.replace(re, '$1 "' + workingDir + '/node" "' + npmPath + '" $3')
      options.args[cmdi + 1] = c
      debug('npm munge!', c)
    }
  }
}

function isCmd (file) {
  var comspec = path.basename(process.env.comspec || '').replace(/\.exe$/i, '')
  return isWindows && (file === comspec || /^cmd(\.exe|\.EXE)?$/.test(file))
}

function mungeCmd (workingDir, options) {
  var cmdi = options.args.indexOf('/c')
  if (cmdi === -1)
    return

  var re = /^\s*("*)([^"]*?\b(?:node|iojs)(?:\.exe|\.EXE)?)("*)( .*)?$/
  var npmre = /^\s*("*)([^"]*?\b(?:npm))("*)( |$)/
  var path_ = require('path')
  if (path_.win32)
    path_ = path_.win32

  var command = options.args[cmdi + 1]
  if (!command)
    return

  var m = command.match(re)
  var replace
  if (m) {
    options.originalNode = m[2]
    replace = m[1] + workingDir + '/node.cmd' + m[3] + m[4]
    options.args[cmdi + 1] = m[1] + m[2] + m[3] +
      ' "' + workingDir + '\\node"' + m[4]
  } else {
    // XXX probably not a good idea to rewrite to the first npm in the
    // path if it's a full path to npm.  And if it's not a full path to
    // npm, then the dirname will not work properly!
    m = command.match(npmre)
    if (!m)
      return

    var npmPath = whichOrUndefined('npm') || 'npm'
    npmPath = path_.dirname(npmPath) + '\\node_modules\\npm\\bin\\npm-cli.js'
    replace = m[1] + workingDir + '/node.cmd' +
              ' "' + npmPath + '"' +
              m[3] + m[4]
    options.args[cmdi + 1] = command.replace(npmre, replace)
  }
}

function isNode (file) {
  var cmdname = path.basename(process.execPath).replace(/\.exe$/i, '')
  return file === 'node' || file === 'iojs' || cmdname === file
}

function mungeNode (workingDir, options) {
  options.originalNode = options.file
  var command = path.basename(options.file).replace(/\.exe$/i, '')
  // make sure it has a main script.
  // otherwise, just let it through.
  var a = 0
  var hasMain = false
  var mainIndex = 1
  for (var a = 1; !hasMain && a < options.args.length; a++) {
    switch (options.args[a]) {
      case '-p':
      case '-i':
      case '--interactive':
      case '--eval':
      case '-e':
      case '-pe':
        hasMain = false
        a = options.args.length
        continue

      case '-r':
      case '--require':
        a += 1
        continue

      default:
        if (options.args[a].match(/^-/)) {
          continue
        } else {
          hasMain = true
          mainIndex = a
          a = options.args.length
          break
        }
    }
  }

  if (hasMain) {
    var replace = workingDir + '/' + command
    options.args.splice(mainIndex, 0, replace)
  }

  // If the file is just something like 'node' then that'll
  // resolve to our shim, and so to prevent double-shimming, we need
  // to resolve that here first.
  // This also handles the case where there's not a main file, like
  // `node -e 'program'`, where we want to avoid the shim entirely.
  if (options.file === options.basename) {
    var realNode = whichOrUndefined(options.file) || process.execPath
    options.file = options.args[0] = realNode
  }

  debug('mungeNode after', options.file, options.args)
}

function mungeShebang (workingDir, options) {
  try {
    var resolved = which.sync(options.file)
  } catch (er) {
    // nothing to do if we can't resolve
    // Most likely: file doesn't exist or is not executable.
    // Let exec pass through, probably will fail, oh well.
    return
  }

  var shebang = fs.readFileSync(resolved, 'utf8')
  var match = shebang.match(/^#!([^\r\n]+)/)
  if (!match)
    return // not a shebang script, probably a binary

  var shebangbin = match[1].split(' ')[0]
  var maybeNode = path.basename(shebangbin)
  if (!isNode(maybeNode))
    return // not a node shebang, leave untouched

  options.originalNode = shebangbin
  options.basename = maybeNode
  options.file = shebangbin
  options.args = [shebangbin, workingDir + '/' + maybeNode]
    .concat(resolved)
    .concat(match[1].split(' ').slice(1))
    .concat(options.args.slice(1))
}

function mungeEnv (workingDir, options) {
  var pathEnv
  for (var i = 0; i < options.envPairs.length; i++) {
    var ep = options.envPairs[i]
    if (ep.match(pathRe)) {
      pathEnv = ep.substr(5)
      var k = ep.substr(0, 5)
      options.envPairs[i] = k + workingDir + colon + pathEnv
    }
  }
  if (!pathEnv) {
    options.envPairs.push((isWindows ? 'Path=' : 'PATH=') + workingDir)
  }
  if (options.originalNode) {
    var key = path.basename(workingDir).substr('.node-spawn-wrap-'.length)
    options.envPairs.push('SW_ORIG_' + key + '=' + options.originalNode)
  }

  options.envPairs.push('SPAWN_WRAP_SHIM_ROOT=' + homedir)

  if (process.env.SPAWN_WRAP_DEBUG === '1')
    options.envPairs.push('SPAWN_WRAP_DEBUG=1')
}

function isnpm (file) {
  // XXX is this even possible/necessary?
  // wouldn't npm just be detected as a node shebang?
  return file === 'npm' && !isWindows
}

function mungenpm (workingDir, options) {
  debug('munge npm')
  // XXX weird effects of replacing a specific npm with a global one
  var npmPath = whichOrUndefined('npm')

  if (npmPath) {
    options.args[0] = npmPath

    options.file = workingDir + '/node'
    options.args.unshift(workingDir + '/node')
  }
}

function munge (workingDir, options) {
  options.basename = path.basename(options.file).replace(/\.exe$/i, '')

  // XXX: dry this
  if (isSh(options.basename)) {
    mungeSh(workingDir, options)
  } else if (isCmd(options.basename)) {
    mungeCmd(workingDir, options)
  } else if (isNode(options.basename)) {
    mungeNode(workingDir, options)
  } else if (isnpm(options.basename)) {
    // XXX unnecessary?  on non-windows, npm is just another shebang
    mungenpm(workingDir, options)
  } else {
    mungeShebang(workingDir, options)
  }

  // now the options are munged into shape.
  // whether we changed something or not, we still update the PATH
  // so that if a script somewhere calls `node foo`, it gets our
  // wrapper instead.

  mungeEnv(workingDir, options)
}

function whichOrUndefined (executable) {
  var path
  try {
    path = which.sync(executable)
  } catch (er) {}
  return path
}

function setup (argv, env) {
  if (argv && typeof argv === 'object' && !env && !Array.isArray(argv)) {
    env = argv
    argv = []
  }

  if (!argv && !env) {
    throw new Error('at least one of "argv" and "env" required')
  }

  if (argv) {
    assert(Array.isArray(argv), 'argv must be array')
  } else {
    argv = []
  }

  if (env) {
    assert(typeof env === 'object', 'env must be an object')
  } else {
    env = {}
  }

  debug('setup argv=%j env=%j', argv, env)

  // For stuff like --use_strict or --harmony, we need to inject
  // the argument *before* the wrap-main.
  var execArgv = []
  for (var i = 0; i < argv.length; i++) {
    if (argv[i].match(/^-/)) {
      execArgv.push(argv[i])
      if (argv[i] === '-r' || argv[i] === '--require') {
        execArgv.push(argv[++i])
      }
    } else {
      break
    }
  }
  if (execArgv.length) {
    if (execArgv.length === argv.length) {
      argv.length = 0
    } else {
      argv = argv.slice(execArgv.length)
    }
  }

  var key = process.pid + '-' + crypto.randomBytes(6).toString('hex')
  var workingDir = homedir + key

  var settings = JSON.stringify({
    module: __filename,
    deps: {
      foregroundChild: require.resolve('foreground-child'),
      signalExit: require.resolve('signal-exit'),
    },
    key: key,
    workingDir: workingDir,
    argv: argv,
    execArgv: execArgv,
    env: env,
    root: process.pid
  }, null, 2) + '\n'

  signalExit(function () {
    if (!doDebug)
      rimraf.sync(workingDir)
  })

  mkdirp.sync(workingDir)
  workingDir = fs.realpathSync(workingDir)
  if (isWindows) {
    var cmdShim =
      '@echo off\r\n' +
      'SETLOCAL\r\n' +
      'CALL :find_dp0\r\n' +
      'SET PATHEXT=%PATHEXT:;.JS;=;%\r\n' +
      '"' + process.execPath + '"' + ' "%dp0%node" %*\r\n' +
      'EXIT /b %errorlevel%\r\n'+
      ':find_dp0\r\n' +
      'SET dp0=%~dp0\r\n' +
      'EXIT /b\r\n'

    fs.writeFileSync(workingDir + '/node.cmd', cmdShim)
    fs.chmodSync(workingDir + '/node.cmd', '0755')
    fs.writeFileSync(workingDir + '/iojs.cmd', cmdShim)
    fs.chmodSync(workingDir + '/iojs.cmd', '0755')
  }
  fs.writeFileSync(workingDir + '/node', shim)
  fs.chmodSync(workingDir + '/node', '0755')
  fs.writeFileSync(workingDir + '/iojs', shim)
  fs.chmodSync(workingDir + '/iojs', '0755')
  var cmdname = path.basename(process.execPath).replace(/\.exe$/i, '')
  if (cmdname !== 'iojs' && cmdname !== 'node') {
    fs.writeFileSync(workingDir + '/' + cmdname, shim)
    fs.chmodSync(workingDir + '/' + cmdname, '0755')
  }
  fs.writeFileSync(workingDir + '/settings.json', settings)

  return workingDir
}

function runMain () {
  process.argv.splice(1, 1)
  process.argv[1] = path.resolve(process.argv[1])
  delete require.cache[process.argv[1]]
  Module.runMain()
}