{"version":3,"file":"index.mjs","names":[],"sources":["../../../../../node_modules/mock-fs/lib/index.js"],"sourcesContent":["'use strict';\n\nconst Binding = require('./binding.js');\nconst {FSError} = require('./error.js');\nconst FileSystem = require('./filesystem.js');\nconst realBinding = process.binding('fs');\nconst path = require('path');\nconst loader = require('./loader.js');\nconst bypass = require('./bypass.js');\nconst {\n  getReadFileContextPrototype,\n  patchReadFileContext,\n} = require('./readfilecontext.js');\nconst fs = require('fs');\n\nconst realProcessProps = {\n  cwd: process.cwd,\n  chdir: process.chdir,\n};\nconst realCreateWriteStream = fs.createWriteStream;\nconst realStats = realBinding.Stats;\nconst realStatWatcher = realBinding.StatWatcher;\n\n/**\n * Pre-patch fs binding.\n * This allows mock-fs to work properly under nodejs v10+ readFile\n * As ReadFileContext nodejs v10+ implementation traps original binding methods:\n * const { FSReqWrap, close, read } = process.binding('fs');\n * Note this patch only solves issue for readFile, as the require of\n * ReadFileContext is delayed by readFile implementation.\n * if (!ReadFileContext) ReadFileContext = require('internal/fs/read_file_context')\n *\n * @param {string} key Property name.\n */\nfunction patch(key) {\n  const existingMethod = realBinding[key];\n  realBinding[key] = function () {\n    if (this._mockedBinding) {\n      return this._mockedBinding[key].apply(this._mockedBinding, arguments);\n    } else {\n      return existingMethod.apply(this, arguments);\n    }\n  }.bind(realBinding);\n}\n\nfor (const key in Binding.prototype) {\n  if (typeof realBinding[key] === 'function') {\n    // Stats and StatWatcher are constructors\n    if (key !== 'Stats' && key !== 'StatWatcher') {\n      patch(key);\n    }\n  }\n}\n\nconst readFileContextPrototype = getReadFileContextPrototype();\n\npatchReadFileContext(readFileContextPrototype);\n\nfunction overrideBinding(binding) {\n  realBinding._mockedBinding = binding;\n}\n\nfunction overrideProcess(cwd, chdir) {\n  process.cwd = cwd;\n  process.chdir = chdir;\n}\n\n/**\n * Have to disable write stream _writev on nodejs v10+.\n *\n * nodejs v8 lib/fs.js\n * note binding.writeBuffers will use mock-fs patched writeBuffers.\n *\n *   const binding = process.binding('fs');\n *   function writev(fd, chunks, position, callback) {\n *     // ...\n *     binding.writeBuffers(fd, chunks, position, req);\n *   }\n *\n * nodejs v10+ lib/internal/fs/streams.js\n * note it uses original writeBuffers, bypassed mock-fs patched writeBuffers.\n *\n *  const {writeBuffers} = internalBinding('fs');\n *  function writev(fd, chunks, position, callback) {\n *    // ...\n *    writeBuffers(fd, chunks, position, req);\n *  }\n *\n * Luckily _writev is an optional method on Writeable stream implementation.\n * When _writev is missing, it will fall back to make multiple _write calls.\n */\nfunction overrideCreateWriteStream() {\n  fs.createWriteStream = function (path, options) {\n    const output = realCreateWriteStream(path, options);\n    // disable _writev, this will over shadow WriteStream.prototype._writev\n    if (realBinding._mockedBinding) {\n      output._writev = undefined;\n    }\n    return output;\n  };\n}\n\nfunction overrideReadFileContext(binding) {\n  readFileContextPrototype._mockedBinding = binding;\n}\n\nfunction restoreBinding() {\n  delete realBinding._mockedBinding;\n  realBinding.Stats = realStats;\n  realBinding.StatWatcher = realStatWatcher;\n}\n\nfunction restoreProcess() {\n  for (const key in realProcessProps) {\n    process[key] = realProcessProps[key];\n  }\n}\n\nfunction restoreCreateWriteStream() {\n  fs.createWriteStream = realCreateWriteStream;\n}\n\nfunction restoreReadFileContext(binding) {\n  delete readFileContextPrototype._mockedBinding;\n}\n\n/**\n * Swap out the fs bindings for a mock file system.\n * @param {object} config Mock file system configuration.\n * @param {object} [options={}] Any filesystem options.\n * @param {boolean} options.createCwd Create a directory for `process.cwd()`\n *     (defaults to `true`).\n * @param {boolean} options.createTmp Create a directory for `os.tmpdir()`\n *     (defaults to `true`).\n */\nmodule.exports = function mock(config, options = {}) {\n  const system = FileSystem.create(config, options);\n  const binding = new Binding(system);\n\n  overrideBinding(binding);\n\n  overrideReadFileContext(binding);\n\n  let currentPath = process.cwd();\n  overrideProcess(\n    function cwd() {\n      if (realBinding._mockedBinding) {\n        return currentPath;\n      }\n      return realProcessProps.cwd();\n    },\n    function chdir(directory) {\n      if (realBinding._mockedBinding) {\n        if (!fs.statSync(path.toNamespacedPath(directory)).isDirectory()) {\n          throw new FSError('ENOTDIR');\n        }\n        currentPath = path.resolve(currentPath, directory);\n      } else {\n        return realProcessProps.chdir(directory);\n      }\n    }\n  );\n\n  overrideCreateWriteStream();\n};\n\nexports = module.exports;\n\n/**\n * Get hold of the mocked filesystem's 'root'\n * If fs hasn't currently been replaced, this will return an empty object\n * @return {object} The mock root.\n */\nexports.getMockRoot = function () {\n  if (realBinding._mockedBinding) {\n    return realBinding._mockedBinding.getSystem().getRoot();\n  } else {\n    return {};\n  }\n};\n\n/**\n * Restore the fs bindings for the real file system.\n */\nexports.restore = function () {\n  restoreBinding();\n  restoreProcess();\n  restoreCreateWriteStream();\n  restoreReadFileContext();\n};\n\n/**\n * Create a file factory.\n */\nexports.file = FileSystem.file;\n\n/**\n * Create a directory factory.\n */\nexports.directory = FileSystem.directory;\n\n/**\n * Create a symbolic link factory.\n */\nexports.symlink = FileSystem.symlink;\n\n/**\n * Automatically maps specified paths (for use with `mock()`)\n */\nexports.load = loader.load;\n\n/**\n * Perform action, bypassing mock FS\n * @example\n * // This file exists on the real FS, not on the mocked FS\n * const filePath = '/path/file.json';\n * const data = mock.bypass(() => fs.readFileSync(filePath, 'utf-8'));\n */\nexports.bypass = bypass;\n"],"x_google_ignoreList":[0],"mappings":";;;;;;;;;;CAEA,MAAM;CACN,MAAM,EAAC;CACP,MAAM;CACN,MAAM,cAAc,QAAQ,QAAQ,KAAK;CACzC,MAAM,iBAAe,OAAO;CAC5B,MAAM;CACN,MAAM;CACN,MAAM,EACJ,6BACA;CAEF,MAAM,eAAa,KAAK;CAExB,MAAM,mBAAmB;EACvB,KAAK,QAAQ;EACb,OAAO,QAAQ;EAChB;CACD,MAAM,wBAAwB,GAAG;CACjC,MAAM,YAAY,YAAY;CAC9B,MAAM,kBAAkB,YAAY;;;;;;;;;;;;CAapC,SAAS,MAAM,KAAK;EAClB,MAAM,iBAAiB,YAAY;AACnC,cAAY,OAAO,WAAY;AAC7B,OAA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