All files / lib/internal/fs read_file_context.js

98.44% Statements 127/129
94.11% Branches 32/34
100% Functions 6/6
98.44% Lines 127/129

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 13022x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 222x 222x 222x 222x 222x 222x 222x 222x 222x 201x 221x 21x 17x 17x 17x 17x 17x 21x 21x 222x 22x 203x 203x 203x 203x 203x 203x 203x 201x 201x 201x 203x 171x 171x 170x 170x 201x 201x 203x 203x     203x 203x 203x 22x 22x 22x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 22x 22x 223x 223x 223x 223x 223x 1x 1x 1x 223x 47x 47x 47x 47x 215x 175x 175x 175x 175x 222x 222x 222x 222x 222x 222x 223x 22x 22x 203x 3x 3x 3x 3x 3x 203x 203x 203x 203x 203x 203x 203x 203x 22x 22x 22x  
'use strict';
 
const {
  ArrayPrototypePush,
  MathMin,
  ReflectApply,
} = primordials;
 
const {
  constants: {
    kReadFileBufferLength,
    kReadFileUnknownBufferLength,
  }
} = require('internal/fs/utils');
 
const { Buffer } = require('buffer');
 
const { FSReqCallback, close, read } = internalBinding('fs');
 
const {
  AbortError,
  aggregateTwoErrors,
} = require('internal/errors');
 
function readFileAfterRead(err, bytesRead) {
  const context = this.context;
 
  if (err)
    return context.close(err);
 
  context.pos += bytesRead;
 
  if (context.pos === context.size || bytesRead === 0) {
    context.close();
  } else {
    if (context.size === 0) {
      // Unknown size, just read until we don't get bytes.
      const buffer = bytesRead === kReadFileUnknownBufferLength ?
        context.buffer : context.buffer.slice(0, bytesRead);
      ArrayPrototypePush(context.buffers, buffer);
    }
    context.read();
  }
}
 
function readFileAfterClose(err) {
  const context = this.context;
  const callback = context.callback;
  let buffer = null;
 
  if (context.err || err)
    return callback(aggregateTwoErrors(err, context.err));
 
  try {
    if (context.size === 0)
      buffer = Buffer.concat(context.buffers, context.pos);
    else if (context.pos < context.size)
      buffer = context.buffer.slice(0, context.pos);
    else
      buffer = context.buffer;
 
    if (context.encoding)
      buffer = buffer.toString(context.encoding);
  } catch (err) {
    return callback(err);
  }
 
  callback(null, buffer);
}
 
class ReadFileContext {
  constructor(callback, encoding) {
    this.fd = undefined;
    this.isUserFd = undefined;
    this.size = 0;
    this.callback = callback;
    this.buffers = null;
    this.buffer = null;
    this.pos = 0;
    this.encoding = encoding;
    this.err = null;
    this.signal = undefined;
  }
 
  read() {
    let buffer;
    let offset;
    let length;
 
    if (this.signal?.aborted) {
      return this.close(
        new AbortError(undefined, { cause: this.signal?.reason }));
    }
    if (this.size === 0) {
      buffer = Buffer.allocUnsafeSlow(kReadFileUnknownBufferLength);
      offset = 0;
      length = kReadFileUnknownBufferLength;
      this.buffer = buffer;
    } else {
      buffer = this.buffer;
      offset = this.pos;
      length = MathMin(kReadFileBufferLength, this.size - this.pos);
    }
 
    const req = new FSReqCallback();
    req.oncomplete = readFileAfterRead;
    req.context = this;
 
    read(this.fd, buffer, offset, length, -1, req);
  }
 
  close(err) {
    if (this.isUserFd) {
      process.nextTick(function tick(context) {
        ReflectApply(readFileAfterClose, { context }, [null]);
      }, this);
      return;
    }
 
    const req = new FSReqCallback();
    req.oncomplete = readFileAfterClose;
    req.context = this;
    this.err = err;
 
    close(this.fd, req);
  }
}
 
module.exports = ReadFileContext;