123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.extendFs = exports.patchFs = void 0;
- const util_1 = require("util");
- const NodePathFS_1 = require("../NodePathFS");
- const FileHandle_1 = require("./FileHandle");
- const SYNC_IMPLEMENTATIONS = new Set([
- `accessSync`,
- `appendFileSync`,
- `createReadStream`,
- `createWriteStream`,
- `chmodSync`,
- `fchmodSync`,
- `chownSync`,
- `fchownSync`,
- `closeSync`,
- `copyFileSync`,
- `linkSync`,
- `lstatSync`,
- `fstatSync`,
- `lutimesSync`,
- `mkdirSync`,
- `openSync`,
- `opendirSync`,
- `readlinkSync`,
- `readFileSync`,
- `readdirSync`,
- `readlinkSync`,
- `realpathSync`,
- `renameSync`,
- `rmdirSync`,
- `statSync`,
- `symlinkSync`,
- `truncateSync`,
- `ftruncateSync`,
- `unlinkSync`,
- `unwatchFile`,
- `utimesSync`,
- `watch`,
- `watchFile`,
- `writeFileSync`,
- `writeSync`,
- ]);
- const ASYNC_IMPLEMENTATIONS = new Set([
- `accessPromise`,
- `appendFilePromise`,
- `fchmodPromise`,
- `chmodPromise`,
- `fchownPromise`,
- `chownPromise`,
- `closePromise`,
- `copyFilePromise`,
- `linkPromise`,
- `fstatPromise`,
- `lstatPromise`,
- `lutimesPromise`,
- `mkdirPromise`,
- `openPromise`,
- `opendirPromise`,
- `readdirPromise`,
- `realpathPromise`,
- `readFilePromise`,
- `readdirPromise`,
- `readlinkPromise`,
- `renamePromise`,
- `rmdirPromise`,
- `statPromise`,
- `symlinkPromise`,
- `truncatePromise`,
- `ftruncatePromise`,
- `unlinkPromise`,
- `utimesPromise`,
- `writeFilePromise`,
- `writeSync`,
- ]);
- function patchFs(patchedFs, fakeFs) {
-
- fakeFs = new NodePathFS_1.NodePathFS(fakeFs);
- const setupFn = (target, name, replacement) => {
- const orig = target[name];
- target[name] = replacement;
-
- if (typeof (orig === null || orig === void 0 ? void 0 : orig[util_1.promisify.custom]) !== `undefined`) {
- replacement[util_1.promisify.custom] = orig[util_1.promisify.custom];
- }
- };
-
- {
- setupFn(patchedFs, `exists`, (p, ...args) => {
- const hasCallback = typeof args[args.length - 1] === `function`;
- const callback = hasCallback ? args.pop() : () => { };
- process.nextTick(() => {
- fakeFs.existsPromise(p).then(exists => {
- callback(exists);
- }, () => {
- callback(false);
- });
- });
- });
-
- setupFn(patchedFs, `read`, (...args) => {
- let [fd, buffer, offset, length, position, callback] = args;
- if (args.length <= 3) {
-
- let options = {};
- if (args.length < 3) {
-
- callback = args[1];
- }
- else {
-
- options = args[1];
- callback = args[2];
- }
- ({
- buffer = Buffer.alloc(16384),
- offset = 0,
- length = buffer.byteLength,
- position,
- } = options);
- }
- if (offset == null)
- offset = 0;
- length |= 0;
- if (length === 0) {
- process.nextTick(() => {
- callback(null, 0, buffer);
- });
- return;
- }
- if (position == null)
- position = -1;
- process.nextTick(() => {
- fakeFs.readPromise(fd, buffer, offset, length, position).then(bytesRead => {
- callback(null, bytesRead, buffer);
- }, error => {
-
-
- callback(error, 0, buffer);
- });
- });
- });
- for (const fnName of ASYNC_IMPLEMENTATIONS) {
- const origName = fnName.replace(/Promise$/, ``);
- if (typeof patchedFs[origName] === `undefined`)
- continue;
- const fakeImpl = fakeFs[fnName];
- if (typeof fakeImpl === `undefined`)
- continue;
- const wrapper = (...args) => {
- const hasCallback = typeof args[args.length - 1] === `function`;
- const callback = hasCallback ? args.pop() : () => { };
- process.nextTick(() => {
- fakeImpl.apply(fakeFs, args).then((result) => {
- callback(null, result);
- }, (error) => {
- callback(error);
- });
- });
- };
- setupFn(patchedFs, origName, wrapper);
- }
- patchedFs.realpath.native = patchedFs.realpath;
- }
-
- {
- setupFn(patchedFs, `existsSync`, (p) => {
- try {
- return fakeFs.existsSync(p);
- }
- catch (error) {
- return false;
- }
- });
-
- setupFn(patchedFs, `readSync`, (...args) => {
- let [fd, buffer, offset, length, position] = args;
- if (args.length <= 3) {
-
- const options = args[2] || {};
- ({ offset = 0, length = buffer.byteLength, position } = options);
- }
- if (offset == null)
- offset = 0;
- length |= 0;
- if (length === 0)
- return 0;
- if (position == null)
- position = -1;
- return fakeFs.readSync(fd, buffer, offset, length, position);
- });
- for (const fnName of SYNC_IMPLEMENTATIONS) {
- const origName = fnName;
- if (typeof patchedFs[origName] === `undefined`)
- continue;
- const fakeImpl = fakeFs[fnName];
- if (typeof fakeImpl === `undefined`)
- continue;
- setupFn(patchedFs, origName, fakeImpl.bind(fakeFs));
- }
- patchedFs.realpathSync.native = patchedFs.realpathSync;
- }
-
- {
-
-
- const origEmitWarning = process.emitWarning;
- process.emitWarning = () => { };
- let patchedFsPromises;
- try {
- patchedFsPromises = patchedFs.promises;
- }
- finally {
- process.emitWarning = origEmitWarning;
- }
- if (typeof patchedFsPromises !== `undefined`) {
-
- for (const fnName of ASYNC_IMPLEMENTATIONS) {
- const origName = fnName.replace(/Promise$/, ``);
- if (typeof patchedFsPromises[origName] === `undefined`)
- continue;
- const fakeImpl = fakeFs[fnName];
- if (typeof fakeImpl === `undefined`)
- continue;
-
-
- if (fnName === `open`)
- continue;
- setupFn(patchedFsPromises, origName, (pathLike, ...args) => {
- if (pathLike instanceof FileHandle_1.FileHandle) {
- return pathLike[origName].apply(pathLike, args);
- }
- else {
- return fakeImpl.call(fakeFs, pathLike, ...args);
- }
- });
- }
- setupFn(patchedFsPromises, `open`, async (...args) => {
-
- const fd = await fakeFs.openPromise(...args);
- return new FileHandle_1.FileHandle(fd, fakeFs);
- });
-
- }
- }
-
- {
-
-
-
-
-
-
-
-
- patchedFs.read[util_1.promisify.custom] = async (fd, buffer, ...args) => {
- const res = fakeFs.readPromise(fd, buffer, ...args);
- return { bytesRead: await res, buffer };
- };
-
- patchedFs.write[util_1.promisify.custom] = async (fd, buffer, ...args) => {
- const res = fakeFs.writePromise(fd, buffer, ...args);
- return { bytesWritten: await res, buffer };
- };
- }
- }
- exports.patchFs = patchFs;
- function extendFs(realFs, fakeFs) {
- const patchedFs = Object.create(realFs);
- patchFs(patchedFs, fakeFs);
- return patchedFs;
- }
- exports.extendFs = extendFs;
|