{"version":3,"file":"readfilecontext.cjs","names":[],"sources":["../../../../../node_modules/mock-fs/lib/readfilecontext.js"],"sourcesContent":["'use strict';\n\nconst {AbortError} = require('./error.js');\nconst {FSReqCallback} = process.binding('fs');\n\n/**\n * This is a workaround for getting access to the ReadFileContext\n * prototype, which we need to be able to patch its methods.\n * @return {object} The prototype.\n */\nexports.getReadFileContextPrototype = function () {\n  const fs = require('fs');\n  const fsBinding = process.binding('fs');\n\n  const originalOpen = fsBinding.open;\n\n  let proto;\n  fsBinding.open = (_path, _flags, _mode, req) => {\n    proto = Object.getPrototypeOf(req.context);\n    return originalOpen.apply(fsBinding, [_path, _flags, _mode, req]);\n  };\n\n  fs.readFile('/ignored.txt', () => {});\n\n  fsBinding.open = originalOpen;\n\n  return proto;\n};\n\n/**\n * This patches the ReadFileContext prototype to use mocked bindings\n * when available. This entire implementation is more or less fully\n * copied over from Node.js's /lib/internal/fs/read_file_context.js\n *\n * This patch is required to support Node.js v16+, where the ReadFileContext\n * closes directly over the internal fs bindings, and is also eagerly loader.\n *\n * See https://github.com/tschaub/mock-fs/issues/332 for more information.\n *\n * @param {object} prototype The ReadFileContext prototype object to patch.\n */\nexports.patchReadFileContext = function (prototype) {\n  const origRead = prototype.read;\n  const origClose = prototype.close;\n\n  const kReadFileUnknownBufferLength = 64 * 1024;\n  const kReadFileBufferLength = 512 * 1024;\n\n  function readFileAfterRead(err, bytesRead) {\n    const context = this.context;\n\n    if (err) {\n      return context.close(err);\n    }\n    context.pos += bytesRead;\n\n    if (context.pos === context.size || bytesRead === 0) {\n      context.close();\n    } else {\n      if (context.size === 0) {\n        // Unknown size, just read until we don't get bytes.\n        const buffer =\n          bytesRead === kReadFileUnknownBufferLength\n            ? context.buffer\n            : context.buffer.slice(0, bytesRead);\n        context.buffers.push(buffer);\n      }\n      context.read();\n    }\n  }\n\n  function readFileAfterClose(err) {\n    const context = this.context;\n    const callback = context.callback;\n    let buffer = null;\n\n    if (context.err || err) {\n      // This is a simplification from Node.js, where we don't bother merging the errors\n      return callback(context.err || err);\n    }\n\n    try {\n      if (context.size === 0) {\n        buffer = Buffer.concat(context.buffers, context.pos);\n      } else if (context.pos < context.size) {\n        buffer = context.buffer.slice(0, context.pos);\n      } else {\n        buffer = context.buffer;\n      }\n\n      if (context.encoding) {\n        buffer = buffer.toString(context.encoding);\n      }\n    } catch (err) {\n      return callback(err);\n    }\n\n    callback(null, buffer);\n  }\n\n  prototype.read = function read() {\n    if (!prototype._mockedBinding) {\n      return origRead.apply(this, arguments);\n    }\n\n    let buffer;\n    let offset;\n    let length;\n\n    if (this.signal && this.signal.aborted) {\n      return this.close(new AbortError());\n    }\n    if (this.size === 0) {\n      buffer = Buffer.allocUnsafeSlow(kReadFileUnknownBufferLength);\n      offset = 0;\n      length = kReadFileUnknownBufferLength;\n      this.buffer = buffer;\n    } else {\n      buffer = this.buffer;\n      offset = this.pos;\n      length = Math.min(kReadFileBufferLength, this.size - this.pos);\n    }\n\n    const req = new FSReqCallback();\n    req.oncomplete = readFileAfterRead;\n    req.context = this;\n\n    // This call and the one in close() is what we want to change, the\n    // rest is pretty much the same as Node.js except we don't have access\n    // to some of the internal optimizations.\n    prototype._mockedBinding.read(this.fd, buffer, offset, length, -1, req);\n  };\n\n  prototype.close = function close(err) {\n    if (!prototype._mockedBinding) {\n      return origClose.apply(this, arguments);\n    }\n\n    if (this.isUserFd) {\n      process.nextTick(function tick(context) {\n        readFileAfterClose.apply({context}, [null]);\n      }, this);\n      return;\n    }\n\n    const req = new FSReqCallback();\n    req.oncomplete = readFileAfterClose;\n    req.context = this;\n    this.err = err;\n\n    prototype._mockedBinding.close(this.fd, req);\n  };\n};\n"],"x_google_ignoreList":[0],"mappings":";;;;;;;CAEA,MAAM,EAAC;CACP,MAAM,EAAC,kBAAiB,QAAQ,QAAQ,KAAK;;;;;;AAO7C,SAAQ,8BAA8B,WAAY;EAChD,MAAM,KAAK,QAAQ,KAAK;EACxB,MAAM,YAAY,QAAQ,QAAQ,KAAK;EAEvC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