UNPKG

@socketsecurity/lib

Version:

Core utilities and infrastructure for Socket.dev security tools

188 lines (187 loc) 6.9 kB
"use strict"; /* Socket Lib - Built with esbuild */ var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var isolation_exports = {}; __export(isolation_exports, { isolatePackage: () => isolatePackage }); module.exports = __toCommonJS(isolation_exports); var import_fs = require("fs"); var import_platform = require("#constants/platform"); var import_path = require("../path"); var import_operations = require("./operations"); var import_paths = require("#lib/paths"); let _path; // @__NO_SIDE_EFFECTS__ function getPath() { if (_path === void 0) { _path = require("node:path"); } return _path; } const FS_CP_OPTIONS = { dereference: true, errorOnExist: false, filter: (src) => !src.includes("node_modules") && !src.endsWith(".DS_Store"), force: true, recursive: true, ...import_platform.WIN32 ? { maxRetries: 3, retryDelay: 100 } : {} }; async function resolveRealPath(pathStr) { const path = /* @__PURE__ */ getPath(); return await import_fs.promises.realpath(pathStr).catch(() => path.resolve(pathStr)); } async function mergePackageJson(pkgJsonPath, originalPkgJson) { const pkgJson = JSON.parse(await import_fs.promises.readFile(pkgJsonPath, "utf8")); const mergedPkgJson = originalPkgJson ? { ...originalPkgJson, ...pkgJson } : pkgJson; return mergedPkgJson; } async function isolatePackage(packageSpec, options) { const path = /* @__PURE__ */ getPath(); const opts = { __proto__: null, ...options }; const { imports, install, onPackageJson, sourcePath: optSourcePath } = opts; let sourcePath = optSourcePath; let packageName; let spec; if ((0, import_path.isPath)(packageSpec)) { const trimmedPath = (0, import_path.trimLeadingDotSlash)(packageSpec); const pathToResolve = (0, import_path.isAbsolute)(trimmedPath) ? trimmedPath : packageSpec; sourcePath = path.resolve(pathToResolve); if (!(0, import_fs.existsSync)(sourcePath)) { throw new Error(`Source path does not exist: ${sourcePath}`); } const pkgJson = await (0, import_operations.readPackageJson)(sourcePath, { normalize: true }); if (!pkgJson) { throw new Error(`Could not read package.json from: ${sourcePath}`); } packageName = pkgJson.name; } else { const npa = require("../external/npm-package-arg"); const parsed = npa(packageSpec); packageName = parsed.name; if (parsed.type === "directory" || parsed.type === "file") { sourcePath = parsed.fetchSpec; if (!sourcePath || !(0, import_fs.existsSync)(sourcePath)) { throw new Error(`Source path does not exist: ${sourcePath}`); } if (!packageName) { const pkgJson = await (0, import_operations.readPackageJson)(sourcePath, { normalize: true }); if (!pkgJson) { throw new Error(`Could not read package.json from: ${sourcePath}`); } packageName = pkgJson.name; } } else { spec = parsed.fetchSpec || parsed.rawSpec; } } if (!packageName) { throw new Error(`Could not determine package name from: ${packageSpec}`); } const sanitizedName = packageName.replace(/[@/]/g, "-"); const tempDir = await import_fs.promises.mkdtemp( path.join((0, import_paths.getOsTmpDir)(), `socket-test-${sanitizedName}-`) ); const packageTempDir = path.join(tempDir, sanitizedName); await import_fs.promises.mkdir(packageTempDir, { recursive: true }); let installedPath; let originalPackageJson; if (spec) { await import_fs.promises.writeFile( path.join(packageTempDir, "package.json"), JSON.stringify( { name: "test-temp", private: true, version: "1.0.0" }, null, 2 ) ); if (install) { await install(packageTempDir); } else { const { spawn } = require("../spawn"); const packageInstallSpec = spec.startsWith("https://") ? spec : `${packageName}@${spec}`; await spawn("pnpm", ["add", packageInstallSpec], { cwd: packageTempDir, shell: import_platform.WIN32, stdio: "pipe" }); } installedPath = path.join(packageTempDir, "node_modules", packageName); originalPackageJson = await (0, import_operations.readPackageJson)(installedPath, { normalize: true }); if (sourcePath) { const realInstalledPath = await resolveRealPath(installedPath); const realSourcePath = await resolveRealPath(sourcePath); if (realSourcePath !== realInstalledPath) { await import_fs.promises.cp(sourcePath, installedPath, FS_CP_OPTIONS); } } } else { if (!sourcePath) { throw new Error("sourcePath is required when no version spec provided"); } const scopedPath = packageName.startsWith("@") ? path.join( packageTempDir, "node_modules", packageName.split("/")[0] ?? "" ) : path.join(packageTempDir, "node_modules"); await import_fs.promises.mkdir(scopedPath, { recursive: true }); installedPath = path.join(packageTempDir, "node_modules", packageName); await import_fs.promises.cp(sourcePath, installedPath, FS_CP_OPTIONS); } if (onPackageJson || originalPackageJson) { const pkgJsonPath = path.join(installedPath, "package.json"); const mergedPkgJson = await mergePackageJson( pkgJsonPath, originalPackageJson ); const finalPkgJson = onPackageJson ? await onPackageJson(mergedPkgJson) : mergedPkgJson; await import_fs.promises.writeFile(pkgJsonPath, JSON.stringify(finalPkgJson, null, 2)); } if (install) { await install(installedPath); } else { const { spawn } = require("../spawn"); await spawn("pnpm", ["install"], { cwd: installedPath, shell: import_platform.WIN32, stdio: "pipe" }); } const exports2 = imports ? { __proto__: null } : void 0; if (imports) { for (const { 0: key, 1: specifier } of Object.entries(imports)) { const fullPath = path.join(installedPath, specifier); exports2[key] = require(fullPath); } } return { exports: exports2, tmpdir: installedPath }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { isolatePackage });