All files index.js

100% Statements 67/67
79.17% Branches 19/24
96.88% Functions 31/32
100% Lines 67/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                  2x   8x 8x   8x 8x 4x 4x 4x 4x   8x           8x 8x   8x 5x 4x 3x 3x   7x           7x 6x 6x                       7x 7x 7x         7x 7x   5x   4x   4x   2x         7x 2x 2x 7x                           12x 7x 7x   12x 10x   8x 8x 6x 6x     8x 8x 6x   8x         7x 11x 12x 11x 6x     6x         10x 9x 7x 3x   10x 10x 8x   7x         9x 9x   6x   5x   3x   3x          
/* @flow */
 
import * as locker from 'lockfile';
import * as fs from 'fs';
import path from 'path';
import type {Callback} from '@verdaccio/types';
import type {LockOptions} from '@verdaccio/file-locking';
 
// locks a file by creating a lock file
const lockFile = function(name: string, callback: Callback) {
 
  const statDir = (name) => {
    return new Promise((resolve, reject) => {
      // test to see if the directory exists
      const dirPath = path.dirname(name);
      fs.stat(dirPath, function(err, stats) {
        Iif (err) {
          return reject(err);
        } else Iif (!stats.isDirectory()) {
          return resolve(new Error(`${path.dirname(name)} is not a directory`));
        } else {
          return resolve(null);
        }
      });
    });
  };
 
  const statfile = (name) => {
    return new Promise((resolve, reject) => {
      // test to see if the directory exists
      fs.stat(name, function(err, stats) {
        if (err) {
          return reject(err);
        } else Iif (!stats.isFile()) {
          return resolve(new Error(`${path.dirname(name)} is not a file`));
        } else {
          return resolve(null);
        }
      });
    });
  };
 
  const lockfile = (name) => {
    return new Promise((resolve) => {
      const lockOpts = {
        // time (ms) to wait when checking for stale locks
        wait: 1000,
        // how often (ms) to re-check stale locks
        pollPeriod: 100,
        // locks are considered stale after 5 minutes
        stale: 5 * 60 * 1000,
        // number of times to attempt to create a lock
        retries: 100,
        // time (ms) between tries
        retryWait: 100,
      };
      const lockFileName = `${name}.lock`;
      locker.lock(lockFileName, lockOpts, () => {
        resolve();
      });
    });
  };
 
  Promise.resolve().then(() => {
    return statDir(name);
  }).then(() => {
    return statfile(name);
  }).then(() => {
    return lockfile(name);
  }).then(() => {
    callback(null);
  }).catch((err) => {
    callback(err);
  });
};
 
// unlocks file by removing existing lock file
const unlockFile= function(name: string, next: Callback) {
  const lockFileName = `${name}.lock`;
  locker.unlock(lockFileName, function() {
    return next(null);
  });
};
 
/**
 *  Reads a local file, which involves
 *  optionally taking a lock
 *  reading the file contents
 *  optionally parsing JSON contents
 * @param {*} name
 * @param {*} options
 * @param {*} callback
 */
function readFile(name: string, options: LockOptions = {}, callback: any = () => {}) {
  if (typeof options === 'function') {
    callback = options;
    options = {};
  }
  options.lock = options.lock || false;
  options.parse = options.parse || false;
 
  const lock = function(options) {
    return new Promise((resolve, reject) => {
      if (!options.lock) {
        return resolve(null);
      }
 
      lockFile(name, function(err) {
        Iif (err) {
          return reject(err);
        }
        return resolve(null);
      });
    });
  };
 
  const read = function() {
    return new Promise((resolve, reject) => {
      fs.readFile(name, 'utf8', function(err, contents) {
        if (err) {
          return reject(err);
        }
 
        resolve(contents);
      });
    });
  };
 
  const parseJSON = function(contents) {
    return new Promise((resolve, reject) => {
      if (!options.parse) {
        return resolve(contents);
      }
      try {
        contents = JSON.parse(contents);
        return resolve(contents);
      } catch (err) {
        return reject(err);
      }
    });
  };
 
  Promise.resolve().then(()=> {
    return lock(options);
  }).then(() => {
    return read();
  }).then((content) => {
    return parseJSON(content);
  }).then((result) => {
    callback(null, result);
  }, (err) => {
    callback(err);
  });
}
 
export {lockFile, unlockFile, readFile};