"use strict";
var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) => function __require() {
  try {
    return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
  } catch (e) {
    throw mod = 0, e;
  }
};

// ../private-tools/lib/zip/index.js
var require_zip = __commonJS({
  "../private-tools/lib/zip/index.js"(exports2) {
    "use strict";
    var __createBinding2 = exports2 && exports2.__createBinding || (Object.create ? (function(o, m, k, k2) {
      if (k2 === void 0) k2 = k;
      var desc = Object.getOwnPropertyDescriptor(m, k);
      if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
        desc = { enumerable: true, get: function() {
          return m[k];
        } };
      }
      Object.defineProperty(o, k2, desc);
    }) : (function(o, m, k, k2) {
      if (k2 === void 0) k2 = k;
      o[k2] = m[k];
    }));
    var __setModuleDefault = exports2 && exports2.__setModuleDefault || (Object.create ? (function(o, v) {
      Object.defineProperty(o, "default", { enumerable: true, value: v });
    }) : function(o, v) {
      o["default"] = v;
    });
    var __importStar = exports2 && exports2.__importStar || /* @__PURE__ */ (function() {
      var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function(o2) {
          var ar = [];
          for (var k in o2) if (Object.prototype.hasOwnProperty.call(o2, k)) ar[ar.length] = k;
          return ar;
        };
        return ownKeys(o);
      };
      return function(mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) {
          for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding2(result, mod, k[i]);
        }
        __setModuleDefault(result, mod);
        return result;
      };
    })();
    Object.defineProperty(exports2, "__esModule", { value: true });
    exports2.zipDirectory = zipDirectory;
    exports2.zipString = zipString;
    var fs_1 = require("fs");
    var path = __importStar(require("path"));
    var fast_glob_1 = require("fast-glob");
    var yazl_1 = require("yazl");
    function epoch() {
      return new Date(1980, 0, 1, 0, 0, 0);
    }
    async function zipDirectory(directory, outputFile, eventEmitter = () => {
    }) {
      const temporaryOutputFile = `${outputFile}.${randomString()}._tmp`;
      await writeZipFile(directory, temporaryOutputFile);
      await moveIntoPlace(temporaryOutputFile, outputFile, eventEmitter);
    }
    function zipString(fileName, rawString) {
      return new Promise((resolve, reject) => {
        const buffers = [];
        const zip = new yazl_1.ZipFile();
        zip.outputStream.on("data", (chunk) => buffers.push(chunk));
        zip.outputStream.on("error", reject);
        zip.outputStream.on("end", () => resolve(Buffer.concat(buffers)));
        zip.addBuffer(Buffer.from(rawString), fileName, {
          mtime: epoch(),
          // reset date to get the same hash for the same content
          // Only emit the DOS timestamp (no UTC "universal time" extended field),
          // so the archive bytes do not depend on the machine's timezone.
          forceDosTimestamp: true
        });
        zip.end();
      });
    }
    function writeZipFile(directory, outputFile) {
      return new Promise(async (ok, fail) => {
        const globOptions = {
          dot: true,
          onlyFiles: true,
          followSymbolicLinks: true,
          cwd: directory
        };
        const files = (0, fast_glob_1.globSync)("**", globOptions);
        const zip = new yazl_1.ZipFile();
        zip.outputStream.on("error", fail);
        const output = (0, fs_1.createWriteStream)(outputFile);
        output.on("error", fail);
        output.once("close", ok);
        zip.outputStream.pipe(output);
        for (const file of files) {
          const fullPath = path.resolve(directory, file);
          const [data, stat] = await Promise.all([fs_1.promises.readFile(fullPath), fs_1.promises.stat(fullPath)]);
          zip.addBuffer(data, file, {
            mtime: epoch(),
            // reset dates to get the same hash for the same content
            mode: stat.mode,
            // Only emit the DOS timestamp (no UTC "universal time" extended field),
            // so the archive bytes do not depend on the machine's timezone.
            forceDosTimestamp: true
          });
        }
        zip.end();
      });
    }
    async function moveIntoPlace(source, target, eventEmitter) {
      let delay = 100;
      let attempts = 5;
      while (true) {
        try {
          await fs_1.promises.rename(source, target);
          return;
        } catch (e) {
          if (e.code !== "EPERM" || attempts-- <= 0) {
            throw e;
          }
          eventEmitter(e.message);
          await sleep(Math.floor(Math.random() * delay));
          delay *= 2;
        }
      }
    }
    function sleep(ms) {
      return new Promise((ok) => setTimeout(ok, ms));
    }
    function randomString() {
      return Math.random().toString(36).replace(/[^a-z0-9]+/g, "");
    }
  }
});

// lib/private/tools.js
var __createBinding = exports && exports.__createBinding || (Object.create ? (function(o, m, k, k2) {
  if (k2 === void 0) k2 = k;
  var desc = Object.getOwnPropertyDescriptor(m, k);
  if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
    desc = { enumerable: true, get: function() {
      return m[k];
    } };
  }
  Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
  if (k2 === void 0) k2 = k;
  o[k2] = m[k];
}));
var __exportStar = exports && exports.__exportStar || function(m, exports2) {
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports2, p)) __createBinding(exports2, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require_zip(), exports);
