@socketsecurity/lib
Version:
Core utilities and infrastructure for Socket.dev security tools
323 lines (322 loc) • 10.3 kB
JavaScript
;
/* 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 operations_exports = {};
__export(operations_exports, {
extractPackage: () => extractPackage,
findPackageExtensions: () => findPackageExtensions,
getReleaseTag: () => getReleaseTag,
packPackage: () => packPackage,
readPackageJson: () => readPackageJson,
readPackageJsonSync: () => readPackageJsonSync,
resolveGitHubTgzUrl: () => resolveGitHubTgzUrl,
resolvePackageName: () => resolvePackageName,
resolveRegistryPackageName: () => resolveRegistryPackageName
});
module.exports = __toCommonJS(operations_exports);
var import_packages = require("#constants/packages");
var import_process = require("#constants/process");
var import_socket = require("#constants/socket");
var import_fs = require("../fs");
var import_objects = require("../objects");
var import_normalize = require("./normalize");
var import_paths = require("./paths");
var import_specs = require("./specs");
const abortSignal = (0, import_process.getAbortSignal)();
const packageExtensions = (0, import_packages.getPackageExtensions)();
const packumentCache = (0, import_packages.getPackumentCache)();
const pacoteCachePath = (0, import_packages.getPacoteCachePath)();
let _cacache;
// @__NO_SIDE_EFFECTS__
function getCacache() {
if (_cacache === void 0) {
_cacache = require("../external/cacache");
}
return _cacache;
}
let _fetcher;
// @__NO_SIDE_EFFECTS__
function getFetcher() {
if (_fetcher === void 0) {
const makeFetchHappen = require("../external/make-fetch-happen");
_fetcher = makeFetchHappen.defaults({
cachePath: pacoteCachePath,
// Prefer-offline: Staleness checks for cached data will be bypassed, but
// missing data will be requested from the server.
// https://github.com/npm/make-fetch-happen?tab=readme-ov-file#--optscache
cache: "force-cache"
});
}
return _fetcher;
}
let _npmPackageArg;
// @__NO_SIDE_EFFECTS__
function getNpmPackageArg() {
if (_npmPackageArg === void 0) {
_npmPackageArg = require("../external/npm-package-arg");
}
return _npmPackageArg;
}
let _pack;
// @__NO_SIDE_EFFECTS__
function getPack() {
if (_pack === void 0) {
_pack = require("../external/libnpmpack");
}
return _pack;
}
let _PackageURL;
// @__NO_SIDE_EFFECTS__
function getPackageURL() {
if (_PackageURL === void 0) {
const packageUrlJs = require("../external/@socketregistry/packageurl-js");
_PackageURL = packageUrlJs.PackageURL;
}
return _PackageURL;
}
let _pacote;
// @__NO_SIDE_EFFECTS__
function getPacote() {
if (_pacote === void 0) {
_pacote = require("../external/pacote");
}
return _pacote;
}
let _semver;
// @__NO_SIDE_EFFECTS__
function getSemver() {
if (_semver === void 0) {
_semver = require("../external/semver");
}
return _semver;
}
let _toEditablePackageJson;
// @__NO_SIDE_EFFECTS__
function _getToEditablePackageJson() {
if (_toEditablePackageJson === void 0) {
_toEditablePackageJson = require("#packages/editable").toEditablePackageJson;
}
return _toEditablePackageJson;
}
let _toEditablePackageJsonSync;
// @__NO_SIDE_EFFECTS__
function _getToEditablePackageJsonSync() {
if (_toEditablePackageJsonSync === void 0) {
_toEditablePackageJsonSync = require("#packages/editable").toEditablePackageJsonSync;
}
return _toEditablePackageJsonSync;
}
// @__NO_SIDE_EFFECTS__
async function extractPackage(pkgNameOrId, options, callback) {
let actualCallback = callback;
let actualOptions = options;
if (arguments.length === 2 && typeof options === "function") {
actualCallback = options;
actualOptions = void 0;
}
const { dest, tmpPrefix, ...extractOptions_ } = {
__proto__: null,
...actualOptions
};
const extractOptions = {
packumentCache,
preferOffline: true,
...extractOptions_
};
const pacote = /* @__PURE__ */ getPacote();
if (typeof dest === "string") {
await pacote.extract(pkgNameOrId, dest, extractOptions);
if (typeof actualCallback === "function") {
await actualCallback(dest);
}
} else {
const cacache = /* @__PURE__ */ getCacache();
await cacache.tmp.withTmp(
pacoteCachePath,
{ tmpPrefix },
async (tmpDirPath) => {
await pacote.extract(pkgNameOrId, tmpDirPath, extractOptions);
if (typeof actualCallback === "function") {
await actualCallback(tmpDirPath);
}
}
);
}
}
// @__NO_SIDE_EFFECTS__
function findPackageExtensions(pkgName, pkgVer) {
let result;
for (const entry of packageExtensions) {
const selector = String(entry[0]);
const ext = entry[1];
const lastAtSignIndex = selector.lastIndexOf("@");
const name = selector.slice(0, lastAtSignIndex);
if (pkgName === name) {
const semver = /* @__PURE__ */ getSemver();
const range = selector.slice(lastAtSignIndex + 1);
if (semver.satisfies(pkgVer, range)) {
if (result === void 0) {
result = {};
}
if (typeof ext === "object" && ext !== null) {
(0, import_objects.merge)(result, ext);
}
}
}
}
return result;
}
// @__NO_SIDE_EFFECTS__
function getReleaseTag(spec) {
if (!spec) {
return "";
}
let atIndex = -1;
if (spec.startsWith("@")) {
atIndex = spec.indexOf("@", 1);
} else {
atIndex = spec.indexOf("@");
}
if (atIndex !== -1) {
return spec.slice(atIndex + 1);
}
return "";
}
// @__NO_SIDE_EFFECTS__
async function packPackage(spec, options) {
const pack = /* @__PURE__ */ getPack();
return await pack(spec, {
__proto__: null,
signal: abortSignal,
...options,
packumentCache,
preferOffline: true
});
}
// @__NO_SIDE_EFFECTS__
async function readPackageJson(filepath, options) {
const { editable, normalize, throws, ...normalizeOptions } = {
__proto__: null,
...options
};
const pkgJson = await (0, import_fs.readJson)((0, import_paths.resolvePackageJsonPath)(filepath), {
throws
});
if (pkgJson) {
if (editable) {
const toEditablePackageJson = /* @__PURE__ */ _getToEditablePackageJson();
return await toEditablePackageJson(pkgJson, {
path: filepath,
normalize,
...normalizeOptions
});
}
return normalize ? (0, import_normalize.normalizePackageJson)(pkgJson, normalizeOptions) : pkgJson;
}
return void 0;
}
// @__NO_SIDE_EFFECTS__
function readPackageJsonSync(filepath, options) {
const { editable, normalize, throws, ...normalizeOptions } = {
__proto__: null,
...options
};
const pkgJson = (0, import_fs.readJsonSync)((0, import_paths.resolvePackageJsonPath)(filepath), { throws });
if (pkgJson) {
if (editable) {
const toEditablePackageJsonSync = /* @__PURE__ */ _getToEditablePackageJsonSync();
return toEditablePackageJsonSync(pkgJson, {
path: filepath,
normalize,
...normalizeOptions
});
}
return normalize ? (0, import_normalize.normalizePackageJson)(pkgJson, normalizeOptions) : pkgJson;
}
return void 0;
}
// @__NO_SIDE_EFFECTS__
async function resolveGitHubTgzUrl(pkgNameOrId, where) {
const whereIsPkgJson = (0, import_objects.isObjectObject)(where);
const pkgJson = whereIsPkgJson ? where : await /* @__PURE__ */ readPackageJson(where, { normalize: true });
if (!pkgJson) {
return "";
}
const { version } = pkgJson;
const npmPackageArg = /* @__PURE__ */ getNpmPackageArg();
const parsedSpec = npmPackageArg(
pkgNameOrId,
whereIsPkgJson ? void 0 : where
);
const isTarballUrl = (0, import_specs.isGitHubTgzSpec)(parsedSpec);
if (isTarballUrl) {
return parsedSpec.saveSpec || "";
}
const isGitHubUrl = (0, import_specs.isGitHubUrlSpec)(parsedSpec);
const repository = pkgJson.repository;
const { project, user } = (isGitHubUrl ? parsedSpec.hosted : (0, import_specs.getRepoUrlDetails)(repository?.url)) || { project: "", user: "" };
if (user && project) {
let apiUrl = "";
if (isGitHubUrl) {
apiUrl = (0, import_specs.gitHubTagRefUrl)(user, project, parsedSpec.gitCommittish || "");
} else {
const fetcher = /* @__PURE__ */ getFetcher();
const versionStr = version;
apiUrl = (0, import_specs.gitHubTagRefUrl)(user, project, `v${versionStr}`);
if (!(await fetcher(apiUrl, { method: "head" })).ok) {
apiUrl = (0, import_specs.gitHubTagRefUrl)(user, project, versionStr);
if (!(await fetcher(apiUrl, { method: "head" })).ok) {
apiUrl = "";
}
}
}
if (apiUrl) {
const fetcher = /* @__PURE__ */ getFetcher();
const resp = await fetcher(apiUrl);
const json = await resp.json();
const sha = json?.object?.sha;
if (sha) {
return (0, import_specs.gitHubTgzUrl)(user, project, sha);
}
}
}
return "";
}
// @__NO_SIDE_EFFECTS__
function resolvePackageName(purlObj, delimiter = "/") {
const { name, namespace } = purlObj;
return `${namespace ? `${namespace}${delimiter}` : ""}${name}`;
}
// @__NO_SIDE_EFFECTS__
function resolveRegistryPackageName(pkgName) {
const purlObj = (/* @__PURE__ */ getPackageURL()).fromString(`pkg:npm/${pkgName}`);
return purlObj.namespace ? `${purlObj.namespace.slice(1)}${import_socket.REGISTRY_SCOPE_DELIMITER}${purlObj.name}` : pkgName;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
extractPackage,
findPackageExtensions,
getReleaseTag,
packPackage,
readPackageJson,
readPackageJsonSync,
resolveGitHubTgzUrl,
resolvePackageName,
resolveRegistryPackageName
});