All files / lib/internal/fs read_file_context.js

98.44% Statements 127/129
94.44% Branches 34/36
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 13023x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 227x 227x 227x 227x 227x 227x 227x 227x 227x 206x 226x 21x 17x 17x 17x 17x 17x 21x 21x 227x 23x 208x 208x 208x 208x 208x 208x 208x 206x 206x 206x 208x 176x 176x 175x 175x 206x 206x 208x 208x     206x 206x 208x 23x 23x 23x 237x 237x 237x 237x 237x 237x 237x 237x 237x 237x 237x 23x 23x 228x 228x 228x 228x 228x 1x 1x 1x 228x 47x 47x 47x 47x 220x 180x 180x 180x 180x 227x 227x 227x 227x 227x 227x 228x 23x 23x 208x 3x 3x 3x 3x 3x 205x 205x 205x 205x 205x 205x 205x 208x 23x 23x 23x  
'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;