HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/html/laravel/node_modules/fs-jetpack/lib/tmp_dir.js
"use strict";

const pathUtil = require("path");
const os = require("os");
const crypto = require("crypto");
const dir = require("./dir");
const fs = require("./utils/fs");
const validate = require("./utils/validate");

const validateInput = (methodName, options) => {
  const methodSignature = `${methodName}([options])`;
  validate.options(methodSignature, "options", options, {
    prefix: ["string"],
    basePath: ["string"]
  });
};

const getOptionsDefaults = (passedOptions, cwdPath) => {
  passedOptions = passedOptions || {};
  const options = {};
  if (typeof passedOptions.prefix !== "string") {
    options.prefix = "";
  } else {
    options.prefix = passedOptions.prefix;
  }
  if (typeof passedOptions.basePath === "string") {
    options.basePath = pathUtil.resolve(cwdPath, passedOptions.basePath);
  } else {
    options.basePath = os.tmpdir();
  }
  return options;
};

const randomStringLength = 32;

// ---------------------------------------------------------
// Sync
// ---------------------------------------------------------

const tmpDirSync = (cwdPath, passedOptions) => {
  const options = getOptionsDefaults(passedOptions, cwdPath);
  const randomString = crypto
    .randomBytes(randomStringLength / 2)
    .toString("hex");
  const dirPath = pathUtil.join(
    options.basePath,
    options.prefix + randomString
  );
  // Let's assume everything will go well, do the directory fastest way possible
  try {
    fs.mkdirSync(dirPath);
  } catch (err) {
    // Something went wrong, try to recover by using more sophisticated approach
    if (err.code === "ENOENT") {
      dir.sync(dirPath);
    } else {
      throw err;
    }
  }
  return dirPath;
};

// ---------------------------------------------------------
// Async
// ---------------------------------------------------------

const tmpDirAsync = (cwdPath, passedOptions) => {
  return new Promise((resolve, reject) => {
    const options = getOptionsDefaults(passedOptions, cwdPath);
    crypto.randomBytes(randomStringLength / 2, (err, bytes) => {
      if (err) {
        reject(err);
      } else {
        const randomString = bytes.toString("hex");
        const dirPath = pathUtil.join(
          options.basePath,
          options.prefix + randomString
        );
        // Let's assume everything will go well, do the directory fastest way possible
        fs.mkdir(dirPath, err => {
          if (err) {
            // Something went wrong, try to recover by using more sophisticated approach
            if (err.code === "ENOENT") {
              dir.async(dirPath).then(() => {
                resolve(dirPath);
              }, reject);
            } else {
              reject(err);
            }
          } else {
            resolve(dirPath);
          }
        });
      }
    });
  });
};

// ---------------------------------------------------------
// API
// ---------------------------------------------------------

exports.validateInput = validateInput;
exports.sync = tmpDirSync;
exports.async = tmpDirAsync;