<commit_before>var url = require('url')
var util = require('util')

var MultiFSClient = require('./lib/client-base.js')
var MultiFSClientFS = require('./lib/client-fs.js')
var MultiFSClientSSH = require('./lib/client-ssh.js')
var MultiFSClientManta = require('./lib/client-manta.js')

module.exports = MultiFS

util.inherits(MultiFS, MultiFSClient)

function MultiFS (clients, debug) {
  this.clients = clients.map(setupClient)
  this.name = this.clients.map(function(c) {
    return c.name
  }).join(',')
  this._debug = debug
  this._clients = clients

  MultiFSClient.call(this)
}

function setupClient(client) {
  if (client instanceof MultiFSClient || client instanceof MultiFS)
    return client

  if (typeof client === 'string') {
    if (client.match(/^~~/) || client.match(/^manta:/))
      return new MultiFSClientManta(client)
    else if (client.match(/^ssh:/))
      return new MultiFSClientSSH(client)
    else
      return new MultiFSClientFS(client)
  }

  switch (client.type) {
    case 'ssh':
      return new MultiFSClientSSH(client)
    case 'fs':
      return new MultiFSClientFS(client.path)
    case 'manta':
      return new MultiFSClientManta(client)
    default:
      throw new Error('Undefined client type: ' + JSON.stringify(client))
  }
}

MultiFS.prototype.destroy = MultiFS.prototype.close = function() {
  this.clients.forEach(function (client) {
    client.destroy()
  })
}

MultiFS.prototype.readFile = function(path, enc, cb) {
  if (typeof enc === 'function') {
    cb = enc
    enc = null
  }

  this.exec({ cmd: "md5", args: [ path ] }, function(er, md5, results) {
    if (er)
      return cb(er, null, results)

    var client = results.clients[0]
    this.exec({
      cmd: "readFile",
      args: [ path, enc ],
      set: [ client ]
    }, cb)
  }.bind(this))
}

;[ 'writeFile', 'writeFilep' ].forEach(function(m) {
  MultiFS.prototype[m] = function(path, data, enc, cb) {
    if (typeof enc === 'function') {
      cb = enc
      enc = null
    }

    this.exec({
      cmd: m,
      args: [ path, data, enc ]
    }, cb)
  }
})

var simpleMethods =
  [
    ['md5'],
    ['rmr'],
    ['unlink'],
    ['rmdir'],
    ['mkdir'],
    ['mkdirp'],<commit_msg>Don't JSON.stringify the readFile contents<commit_after>var url = require('url')
var util = require('util')

var MultiFSClient = require('./lib/client-base.js')
var MultiFSClientFS = require('./lib/client-fs.js')
var MultiFSClientSSH = require('./lib/client-ssh.js')
var MultiFSClientManta = require('./lib/client-manta.js')

module.exports = MultiFS

util.inherits(MultiFS, MultiFSClient)

function MultiFS (clients, debug) {
  this.clients = clients.map(setupClient)
  this.name = this.clients.map(function(c) {
    return c.name
  }).join(',')
  this._debug = debug
  this._clients = clients

  MultiFSClient.call(this)
}

function setupClient(client) {
  if (client instanceof MultiFSClient || client instanceof MultiFS)
    return client

  if (typeof client === 'string') {
    if (client.match(/^~~/) || client.match(/^manta:/))
      return new MultiFSClientManta(client)
    else if (client.match(/^ssh:/))
      return new MultiFSClientSSH(client)
    else
      return new MultiFSClientFS(client)
  }

  switch (client.type) {
    case 'ssh':
      return new MultiFSClientSSH(client)
    case 'fs':
      return new MultiFSClientFS(client.path)
    case 'manta':
      return new MultiFSClientManta(client)
    default:
      throw new Error('Undefined client type: ' + JSON.stringify(client))
  }
}

MultiFS.prototype.destroy = MultiFS.prototype.close = function() {
  this.clients.forEach(function (client) {
    client.destroy()
  })
}

MultiFS.prototype.readFile = function(path, enc, cb) {
  if (typeof enc === 'function') {
    cb = enc
    enc = null
  }

  this.exec({ cmd: "md5", args: [ path ] }, function(er, md5, results) {
    if (er)
      return cb(er, null, results)

    var client = results.clients[0]
    this.exec({
      cmd: "readFile",
      args: [ path, enc ],
      set: [ client ],
      serialize: function (n) { return md5 }
    }, cb)
  }.bind(this))
}

;[ 'writeFile', 'writeFilep' ].forEach(function(m) {
  MultiFS.prototype[m] = function(path, data, enc, cb) {
    if (typeof enc === 'function') {
      cb = enc
      enc = null
    }

    this.exec({
      cmd: m,
      args: [ path, data, enc ]
    }, cb)
  }
})

var simpleMethods =
  [
    ['md5'],
    ['rmr'],
    ['unlink'],
    ['rmdir'],
    ['mkdir'],
    ['mkdirp'],
{'fraction_non_alphanumeric': 0.15738877710708696, 'fraction_numerical': 0.002012072434607646, 'mean_word_length': 3.048868778280543, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3651430', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1499, 'n_words': 348}