All files / lib/internal/fs dir.js

100% Statements 284/284
100% Branches 50/50
100% Functions 11/11
100% Lines 284/284

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 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 28522x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 22x 22x 1x 1x 22x 22x 16x 16x 22x 22x 246x 3x 3x 243x 246x 10x 10x 233x 233x 233x 246x 2x 2x 2x 2x 2x 230x 246x 94x 94x 94x 94x 94x 94x 94x 94x 246x 246x 246x 136x 136x 136x 136x 136x 136x 136x 61x 61x 136x 136x 136x 246x 246x 246x 246x 246x 246x 246x 246x 246x 22x 22x 234x 1x 1x 234x 234x 1x 1x 232x 234x 91x 91x 91x 91x 141x 141x 141x 141x 141x 141x 141x 141x 141x 141x 234x 69x 69x 72x 72x 72x 234x 22x 22x 88x 88x 6x 2x 2x 4x 4x 88x 88x 88x 88x 88x 1x 1x 1x 88x 88x 1x 1x 1x 1x 1x 88x 88x 88x 88x 88x 88x 22x 22x 83x 1x 1x 83x 83x 1x 1x 81x 81x 81x 81x 81x 81x 83x 22x 22x 74x 74x 218x 218x 60x 60x 218x 144x 74x 74x 74x 74x 22x 22x 22x 22x 22x 22x 22x 22x 22x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 82x 1x 82x 81x 81x 82x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 22x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 22x 22x 22x 22x 22x 22x  
'use strict';
 
const {
  ArrayPrototypePush,
  ArrayPrototypeSlice,
  ArrayPrototypeSplice,
  FunctionPrototypeBind,
  ObjectDefineProperty,
  PromiseReject,
  Symbol,
  SymbolAsyncIterator,
} = primordials;
 
const pathModule = require('path');
const binding = internalBinding('fs');
const dirBinding = internalBinding('fs_dir');
const {
  codes: {
    ERR_DIR_CLOSED,
    ERR_DIR_CONCURRENT_OPERATION,
    ERR_MISSING_ARGS
  }
} = require('internal/errors');
 
const { FSReqCallback } = binding;
const internalUtil = require('internal/util');
const {
  getDirent,
  getOptions,
  getValidatedPath,
  handleErrorFromBinding
} = require('internal/fs/utils');
const {
  validateFunction,
  validateUint32
} = require('internal/validators');
 
const kDirHandle = Symbol('kDirHandle');
const kDirPath = Symbol('kDirPath');
const kDirBufferedEntries = Symbol('kDirBufferedEntries');
const kDirClosed = Symbol('kDirClosed');
const kDirOptions = Symbol('kDirOptions');
const kDirReadImpl = Symbol('kDirReadImpl');
const kDirReadPromisified = Symbol('kDirReadPromisified');
const kDirClosePromisified = Symbol('kDirClosePromisified');
const kDirOperationQueue = Symbol('kDirOperationQueue');
 
class Dir {
  constructor(handle, path, options) {
    if (handle == null) throw new ERR_MISSING_ARGS('handle');
    this[kDirHandle] = handle;
    this[kDirBufferedEntries] = [];
    this[kDirPath] = path;
    this[kDirClosed] = false;
 
    // Either `null` or an Array of pending operations (= functions to be called
    // once the current operation is done).
    this[kDirOperationQueue] = null;
 
    this[kDirOptions] = {
      bufferSize: 32,
      ...getOptions(options, {
        encoding: 'utf8'
      })
    };
 
    validateUint32(this[kDirOptions].bufferSize, 'options.bufferSize', true);
 
    this[kDirReadPromisified] = FunctionPrototypeBind(
      internalUtil.promisify(this[kDirReadImpl]), this, false);
    this[kDirClosePromisified] = FunctionPrototypeBind(
      internalUtil.promisify(this.close), this);
  }
 
  get path() {
    return this[kDirPath];
  }
 
  read(callback) {
    return this[kDirReadImpl](true, callback);
  }
 
  [kDirReadImpl](maybeSync, callback) {
    if (this[kDirClosed] === true) {
      throw new ERR_DIR_CLOSED();
    }
 
    if (callback === undefined) {
      return this[kDirReadPromisified]();
    }
 
    validateFunction(callback, 'callback');
 
    if (this[kDirOperationQueue] !== null) {
      ArrayPrototypePush(this[kDirOperationQueue], () => {
        this[kDirReadImpl](maybeSync, callback);
      });
      return;
    }
 
    if (this[kDirBufferedEntries].length > 0) {
      const { 0: name, 1: type } =
        ArrayPrototypeSplice(this[kDirBufferedEntries], 0, 2);
      if (maybeSync)
        process.nextTick(getDirent, this[kDirPath], name, type, callback);
      else
        getDirent(this[kDirPath], name, type, callback);
      return;
    }
 
    const req = new FSReqCallback();
    req.oncomplete = (err, result) => {
      process.nextTick(() => {
        const queue = this[kDirOperationQueue];
        this[kDirOperationQueue] = null;
        for (const op of queue) op();
      });
 
      if (err || result === null) {
        return callback(err, result);
      }
 
      this[kDirBufferedEntries] = ArrayPrototypeSlice(result, 2);
      getDirent(this[kDirPath], result[0], result[1], callback);
    };
 
    this[kDirOperationQueue] = [];
    this[kDirHandle].read(
      this[kDirOptions].encoding,
      this[kDirOptions].bufferSize,
      req
    );
  }
 
  readSync() {
    if (this[kDirClosed] === true) {
      throw new ERR_DIR_CLOSED();
    }
 
    if (this[kDirOperationQueue] !== null) {
      throw new ERR_DIR_CONCURRENT_OPERATION();
    }
 
    if (this[kDirBufferedEntries].length > 0) {
      const { 0: name, 1: type } =
          ArrayPrototypeSplice(this[kDirBufferedEntries], 0, 2);
      return getDirent(this[kDirPath], name, type);
    }
 
    const ctx = { path: this[kDirPath] };
    const result = this[kDirHandle].read(
      this[kDirOptions].encoding,
      this[kDirOptions].bufferSize,
      undefined,
      ctx
    );
    handleErrorFromBinding(ctx);
 
    if (result === null) {
      return result;
    }
 
    this[kDirBufferedEntries] = ArrayPrototypeSlice(result, 2);
    return getDirent(this[kDirPath], result[0], result[1]);
  }
 
  close(callback) {
    // Promise
    if (callback === undefined) {
      if (this[kDirClosed] === true) {
        return PromiseReject(new ERR_DIR_CLOSED());
      }
      return this[kDirClosePromisified]();
    }
 
    // callback
    validateFunction(callback, 'callback');
 
    if (this[kDirClosed] === true) {
      process.nextTick(callback, new ERR_DIR_CLOSED());
      return;
    }
 
    if (this[kDirOperationQueue] !== null) {
      ArrayPrototypePush(this[kDirOperationQueue], () => {
        this.close(callback);
      });
      return;
    }
 
    this[kDirClosed] = true;
    const req = new FSReqCallback();
    req.oncomplete = callback;
    this[kDirHandle].close(req);
  }
 
  closeSync() {
    if (this[kDirClosed] === true) {
      throw new ERR_DIR_CLOSED();
    }
 
    if (this[kDirOperationQueue] !== null) {
      throw new ERR_DIR_CONCURRENT_OPERATION();
    }
 
    this[kDirClosed] = true;
    const ctx = { path: this[kDirPath] };
    const result = this[kDirHandle].close(undefined, ctx);
    handleErrorFromBinding(ctx);
    return result;
  }
 
  async* entries() {
    try {
      while (true) {
        const result = await this[kDirReadPromisified]();
        if (result === null) {
          break;
        }
        yield result;
      }
    } finally {
      await this[kDirClosePromisified]();
    }
  }
}
 
ObjectDefineProperty(Dir.prototype, SymbolAsyncIterator, {
  value: Dir.prototype.entries,
  enumerable: false,
  writable: true,
  configurable: true,
});
 
function opendir(path, options, callback) {
  callback = typeof options === 'function' ? options : callback;
  validateFunction(callback, 'callback');
 
  path = getValidatedPath(path);
  options = getOptions(options, {
    encoding: 'utf8'
  });
 
  function opendirCallback(error, handle) {
    if (error) {
      callback(error);
    } else {
      callback(null, new Dir(handle, path, options));
    }
  }
 
  const req = new FSReqCallback();
  req.oncomplete = opendirCallback;
 
  dirBinding.opendir(
    pathModule.toNamespacedPath(path),
    options.encoding,
    req
  );
}
 
function opendirSync(path, options) {
  path = getValidatedPath(path);
  options = getOptions(options, {
    encoding: 'utf8'
  });
 
  const ctx = { path };
  const handle = dirBinding.opendir(
    pathModule.toNamespacedPath(path),
    options.encoding,
    undefined,
    ctx
  );
  handleErrorFromBinding(ctx);
 
  return new Dir(handle, path, options);
}
 
module.exports = {
  Dir,
  opendir,
  opendirSync
};