@socketsecurity/lib 5.2.1 → 5.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,162 +18,81 @@ export interface RepoConfig {
18
18
  */
19
19
  repo: string;
20
20
  }
21
- /**
22
- * Get latest release tag matching a tool prefix.
23
- *
24
- * Searches recent releases for the first tag matching the given prefix.
25
- * Useful for finding latest versions of tools with date-based tags like
26
- * `node-smol-20260105-c47753c` or `binject-20260106-1df5745`.
27
- *
28
- * @param toolPrefix - Tool name prefix to search for (e.g., 'node-smol-', 'binject-')
29
- * @param repoConfig - Repository configuration (owner/repo)
30
- * @param options - Additional options
31
- * @returns Latest release tag or null if not found
32
- *
33
- * @example
34
- * ```ts
35
- * const tag = await getLatestRelease('node-smol-', {
36
- * owner: 'SocketDev',
37
- * repo: 'socket-btm'
38
- * })
39
- * // Returns: 'node-smol-20260105-c47753c'
40
- * ```
41
- */
42
- export declare function getLatestRelease(toolPrefix: string, repoConfig: RepoConfig, options?: {
43
- quiet?: boolean;
44
- }): Promise<string | null>;
45
- /**
46
- * Get download URL for a specific release asset.
47
- *
48
- * @param tag - Release tag name (e.g., 'node-smol-20260105-c47753c')
49
- * @param assetName - Asset name to download (e.g., 'node-linux-x64-musl')
50
- * @param repoConfig - Repository configuration (owner/repo)
51
- * @param options - Additional options
52
- * @returns Browser download URL for the asset
53
- *
54
- * @example
55
- * ```ts
56
- * const url = await getReleaseAssetUrl(
57
- * 'node-smol-20260105-c47753c',
58
- * 'node-linux-x64-musl',
59
- * { owner: 'SocketDev', repo: 'socket-btm' }
60
- * )
61
- * ```
62
- */
63
- export declare function getReleaseAssetUrl(tag: string, assetName: string, repoConfig: RepoConfig, options?: {
64
- quiet?: boolean;
65
- }): Promise<string | null>;
66
- /**
67
- * Download a specific release asset.
68
- *
69
- * Uses browser_download_url to avoid consuming GitHub API quota.
70
- * Automatically follows redirects and retries on failure.
71
- *
72
- * @param tag - Release tag name
73
- * @param assetName - Asset name to download
74
- * @param outputPath - Path to write the downloaded file
75
- * @param repoConfig - Repository configuration (owner/repo)
76
- * @param options - Additional options
77
- *
78
- * @example
79
- * ```ts
80
- * await downloadReleaseAsset(
81
- * 'node-smol-20260105-c47753c',
82
- * 'node-linux-x64-musl',
83
- * '/path/to/output/node',
84
- * { owner: 'SocketDev', repo: 'socket-btm' }
85
- * )
86
- * ```
87
- */
88
- export declare function downloadReleaseAsset(tag: string, assetName: string, outputPath: string, repoConfig: RepoConfig, options?: {
89
- quiet?: boolean;
90
- }): Promise<void>;
91
21
  /**
92
22
  * Configuration for downloading a GitHub release.
93
23
  */
94
24
  export interface DownloadGitHubReleaseConfig {
95
- /**
96
- * GitHub repository owner/organization.
97
- */
25
+ /** GitHub repository owner/organization. */
98
26
  owner: string;
99
- /**
100
- * GitHub repository name.
101
- */
27
+ /** GitHub repository name. */
102
28
  repo: string;
103
- /**
104
- * Working directory (defaults to process.cwd()).
105
- * Used to resolve relative paths in downloadDir.
106
- */
29
+ /** Working directory (defaults to process.cwd()). */
107
30
  cwd?: string;
108
- /**
109
- * Download destination directory.
110
- * Can be absolute or relative to cwd.
111
- * @default 'build/downloaded' (relative to cwd)
112
- */
31
+ /** Download destination directory. @default 'build/downloaded' */
113
32
  downloadDir?: string;
114
- /**
115
- * Tool name for directory structure (e.g., 'node-smol', 'binject', 'lief').
116
- * Creates subdirectory: {downloadDir}/{toolName}/{platformArch}/
117
- */
33
+ /** Tool name for directory structure. */
118
34
  toolName: string;
119
- /**
120
- * Platform-arch identifier (e.g., 'linux-x64-musl', 'darwin-arm64').
121
- * Used for the download directory path.
122
- */
35
+ /** Platform-arch identifier (e.g., 'linux-x64-musl'). */
123
36
  platformArch: string;
124
- /**
125
- * Binary filename (e.g., 'node', 'binject', 'lief', 'node.exe').
126
- */
37
+ /** Binary filename (e.g., 'node', 'binject'). */
127
38
  binaryName: string;
128
- /**
129
- * Asset name on GitHub (e.g., 'node-linux-x64-musl', 'binject-darwin-arm64').
130
- */
39
+ /** Asset name on GitHub. */
131
40
  assetName: string;
132
- /**
133
- * Tool prefix for finding latest release (e.g., 'node-smol-', 'binject-').
134
- * Either this or `tag` must be provided.
135
- */
41
+ /** Tool prefix for finding latest release. */
136
42
  toolPrefix?: string;
137
- /**
138
- * Specific release tag to download (e.g., 'node-smol-20260105-c47753c').
139
- * If not provided, uses `toolPrefix` to find the latest release.
140
- */
43
+ /** Specific release tag to download. */
141
44
  tag?: string;
142
- /**
143
- * Suppress log messages.
144
- * @default false
145
- */
45
+ /** Suppress log messages. @default false */
146
46
  quiet?: boolean;
147
- /**
148
- * Remove macOS quarantine attribute after download.
149
- * Only applies when downloading on macOS for macOS binaries.
150
- * @default true
151
- */
47
+ /** Remove macOS quarantine attribute after download. @default true */
152
48
  removeMacOSQuarantine?: boolean;
153
49
  }
154
50
  /**
155
51
  * Download a binary from any GitHub repository with version caching.
156
52
  *
157
- * Downloads to: `{downloadDir}/{toolName}/{platformArch}/{binaryName}`
158
- * Caches version in: `{downloadDir}/{toolName}/{platformArch}/.version`
159
- *
160
53
  * @param config - Download configuration
161
54
  * @returns Path to the downloaded binary
162
- *
163
- * @example
164
- * ```ts
165
- * // Download from any GitHub repo
166
- * const nodePath = await downloadGitHubRelease({
167
- * owner: 'nodejs',
168
- * repo: 'node',
169
- * cwd: process.cwd(),
170
- * downloadDir: 'build/downloaded', // relative to cwd
171
- * toolName: 'node',
172
- * platformArch: 'linux-x64',
173
- * binaryName: 'node',
174
- * assetName: 'node-v20.10.0-linux-x64.tar.gz',
175
- * tag: 'v20.10.0'
176
- * })
177
- * ```
178
55
  */
179
56
  export declare function downloadGitHubRelease(config: DownloadGitHubReleaseConfig): Promise<string>;
57
+ /**
58
+ * Download a specific release asset.
59
+ *
60
+ * @param tag - Release tag name
61
+ * @param assetName - Asset name to download
62
+ * @param outputPath - Path to write the downloaded file
63
+ * @param repoConfig - Repository configuration (owner/repo)
64
+ * @param options - Additional options
65
+ */
66
+ export declare function downloadReleaseAsset(tag: string, assetName: string, outputPath: string, repoConfig: RepoConfig, options?: {
67
+ quiet?: boolean;
68
+ }): Promise<void>;
69
+ /**
70
+ * Get GitHub authentication headers if token is available.
71
+ * Checks GH_TOKEN or GITHUB_TOKEN environment variables.
72
+ *
73
+ * @returns Headers object with Authorization header if token exists.
74
+ */
75
+ export declare function getAuthHeaders(): Record<string, string>;
76
+ /**
77
+ * Get latest release tag matching a tool prefix.
78
+ *
79
+ * @param toolPrefix - Tool name prefix to search for (e.g., 'node-smol-')
80
+ * @param repoConfig - Repository configuration (owner/repo)
81
+ * @param options - Additional options
82
+ * @returns Latest release tag or null if not found
83
+ */
84
+ export declare function getLatestRelease(toolPrefix: string, repoConfig: RepoConfig, options?: {
85
+ quiet?: boolean;
86
+ }): Promise<string | null>;
87
+ /**
88
+ * Get download URL for a specific release asset.
89
+ *
90
+ * @param tag - Release tag name
91
+ * @param assetName - Asset name to download
92
+ * @param repoConfig - Repository configuration (owner/repo)
93
+ * @param options - Additional options
94
+ * @returns Browser download URL for the asset
95
+ */
96
+ export declare function getReleaseAssetUrl(tag: string, assetName: string, repoConfig: RepoConfig, options?: {
97
+ quiet?: boolean;
98
+ }): Promise<string | null>;
@@ -1,10 +1,8 @@
1
1
  "use strict";
2
2
  /* Socket Lib - Built with esbuild */
3
- var __create = Object.create;
4
3
  var __defProp = Object.defineProperty;
5
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __getProtoOf = Object.getPrototypeOf;
8
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
7
  var __export = (target, all) => {
10
8
  for (var name in all)
@@ -18,33 +16,33 @@ var __copyProps = (to, from, except, desc) => {
18
16
  }
19
17
  return to;
20
18
  };
21
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
- // If the importer is in node compatibility mode or this is not an ESM
23
- // file that has been converted to a CommonJS file using a Babel-
24
- // compatible transform (i.e. "__esModule" has not been set), then set
25
- // "default" to the CommonJS "module.exports" for node compatibility.
26
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
- mod
28
- ));
29
19
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
20
  var github_exports = {};
31
21
  __export(github_exports, {
32
22
  SOCKET_BTM_REPO: () => SOCKET_BTM_REPO,
33
23
  downloadGitHubRelease: () => downloadGitHubRelease,
34
24
  downloadReleaseAsset: () => downloadReleaseAsset,
25
+ getAuthHeaders: () => getAuthHeaders,
35
26
  getLatestRelease: () => getLatestRelease,
36
27
  getReleaseAssetUrl: () => getReleaseAssetUrl
37
28
  });
38
29
  module.exports = __toCommonJS(github_exports);
39
- var import_node_fs = require("node:fs");
40
- var import_promises = require("node:fs/promises");
41
- var import_node_path = __toESM(require("node:path"));
42
- var import_fs = require("../fs.js");
30
+ var import_fs = require("fs");
31
+ var import_promises = require("fs/promises");
32
+ var import_fs2 = require("../fs.js");
43
33
  var import_http_request = require("../http-request.js");
44
34
  var import_logger = require("../logger.js");
45
35
  var import_promises2 = require("../promises.js");
46
36
  var import_spawn = require("../spawn.js");
47
37
  const logger = (0, import_logger.getDefaultLogger)();
38
+ let _path;
39
+ // @__NO_SIDE_EFFECTS__
40
+ function getPath() {
41
+ if (_path === void 0) {
42
+ _path = require("path");
43
+ }
44
+ return _path;
45
+ }
48
46
  const SOCKET_BTM_REPO = {
49
47
  owner: "SocketDev",
50
48
  repo: "socket-btm"
@@ -58,6 +56,100 @@ const RETRY_CONFIG = Object.freeze({
58
56
  // Maximum number of retry attempts (excluding initial request).
59
57
  retries: 2
60
58
  });
59
+ async function downloadGitHubRelease(config) {
60
+ const {
61
+ assetName,
62
+ binaryName,
63
+ cwd = process.cwd(),
64
+ downloadDir = "build/downloaded",
65
+ owner,
66
+ platformArch,
67
+ quiet = false,
68
+ removeMacOSQuarantine = true,
69
+ repo,
70
+ tag: explicitTag,
71
+ toolName,
72
+ toolPrefix
73
+ } = config;
74
+ let tag;
75
+ if (explicitTag) {
76
+ tag = explicitTag;
77
+ } else if (toolPrefix) {
78
+ const latestTag = await getLatestRelease(
79
+ toolPrefix,
80
+ { owner, repo },
81
+ { quiet }
82
+ );
83
+ if (!latestTag) {
84
+ throw new Error(`No ${toolPrefix} release found in ${owner}/${repo}`);
85
+ }
86
+ tag = latestTag;
87
+ } else {
88
+ throw new Error("Either toolPrefix or tag must be provided");
89
+ }
90
+ const path = /* @__PURE__ */ getPath();
91
+ const resolvedDownloadDir = path.isAbsolute(downloadDir) ? downloadDir : path.join(cwd, downloadDir);
92
+ const binaryDir = path.join(resolvedDownloadDir, toolName, platformArch);
93
+ const binaryPath = path.join(binaryDir, binaryName);
94
+ const versionPath = path.join(binaryDir, ".version");
95
+ if ((0, import_fs.existsSync)(versionPath) && (0, import_fs.existsSync)(binaryPath)) {
96
+ const cachedVersion = (await (0, import_promises.readFile)(versionPath, "utf8")).trim();
97
+ if (cachedVersion === tag) {
98
+ if (!quiet) {
99
+ logger.info(`Using cached ${toolName} (${platformArch}): ${binaryPath}`);
100
+ }
101
+ return binaryPath;
102
+ }
103
+ }
104
+ if (!quiet) {
105
+ logger.info(`Downloading ${toolName} for ${platformArch}...`);
106
+ }
107
+ await downloadReleaseAsset(
108
+ tag,
109
+ assetName,
110
+ binaryPath,
111
+ { owner, repo },
112
+ { quiet }
113
+ );
114
+ const isWindows = binaryName.endsWith(".exe");
115
+ if (!isWindows) {
116
+ (0, import_fs.chmodSync)(binaryPath, 493);
117
+ if (removeMacOSQuarantine && process.platform === "darwin" && platformArch.startsWith("darwin")) {
118
+ try {
119
+ await (0, import_spawn.spawn)("xattr", ["-d", "com.apple.quarantine", binaryPath], {
120
+ stdio: "ignore"
121
+ });
122
+ } catch {
123
+ }
124
+ }
125
+ }
126
+ await (0, import_promises.writeFile)(versionPath, tag, "utf8");
127
+ if (!quiet) {
128
+ logger.info(`Downloaded ${toolName} to ${binaryPath}`);
129
+ }
130
+ return binaryPath;
131
+ }
132
+ async function downloadReleaseAsset(tag, assetName, outputPath, repoConfig, options = {}) {
133
+ const { owner, repo } = repoConfig;
134
+ const { quiet = false } = options;
135
+ const downloadUrl = await getReleaseAssetUrl(
136
+ tag,
137
+ assetName,
138
+ { owner, repo },
139
+ { quiet }
140
+ );
141
+ if (!downloadUrl) {
142
+ throw new Error(`Asset ${assetName} not found in release ${tag}`);
143
+ }
144
+ const path = /* @__PURE__ */ getPath();
145
+ await (0, import_fs2.safeMkdir)(path.dirname(outputPath));
146
+ await (0, import_http_request.httpDownload)(downloadUrl, outputPath, {
147
+ logger: quiet ? void 0 : logger,
148
+ progressInterval: 10,
149
+ retries: 2,
150
+ retryDelay: 5e3
151
+ });
152
+ }
61
153
  function getAuthHeaders() {
62
154
  const token = process.env["GH_TOKEN"] || process.env["GITHUB_TOKEN"];
63
155
  const headers = {
@@ -156,103 +248,12 @@ async function getReleaseAssetUrl(tag, assetName, repoConfig, options = {}) {
156
248
  }
157
249
  );
158
250
  }
159
- async function downloadReleaseAsset(tag, assetName, outputPath, repoConfig, options = {}) {
160
- const { owner, repo } = repoConfig;
161
- const { quiet = false } = options;
162
- const downloadUrl = await getReleaseAssetUrl(
163
- tag,
164
- assetName,
165
- { owner, repo },
166
- { quiet }
167
- );
168
- if (!downloadUrl) {
169
- throw new Error(`Asset ${assetName} not found in release ${tag}`);
170
- }
171
- await (0, import_fs.safeMkdir)(import_node_path.default.dirname(outputPath));
172
- await (0, import_http_request.httpDownload)(downloadUrl, outputPath, {
173
- logger: quiet ? void 0 : logger,
174
- progressInterval: 10,
175
- retries: 2,
176
- retryDelay: 5e3
177
- });
178
- }
179
- async function downloadGitHubRelease(config) {
180
- const {
181
- assetName,
182
- binaryName,
183
- cwd = process.cwd(),
184
- downloadDir = "build/downloaded",
185
- owner,
186
- platformArch,
187
- quiet = false,
188
- removeMacOSQuarantine = true,
189
- repo,
190
- tag: explicitTag,
191
- toolName,
192
- toolPrefix
193
- } = config;
194
- let tag;
195
- if (explicitTag) {
196
- tag = explicitTag;
197
- } else if (toolPrefix) {
198
- const latestTag = await getLatestRelease(
199
- toolPrefix,
200
- { owner, repo },
201
- { quiet }
202
- );
203
- if (!latestTag) {
204
- throw new Error(`No ${toolPrefix} release found in ${owner}/${repo}`);
205
- }
206
- tag = latestTag;
207
- } else {
208
- throw new Error("Either toolPrefix or tag must be provided");
209
- }
210
- const resolvedDownloadDir = import_node_path.default.isAbsolute(downloadDir) ? downloadDir : import_node_path.default.join(cwd, downloadDir);
211
- const binaryDir = import_node_path.default.join(resolvedDownloadDir, toolName, platformArch);
212
- const binaryPath = import_node_path.default.join(binaryDir, binaryName);
213
- const versionPath = import_node_path.default.join(binaryDir, ".version");
214
- if ((0, import_node_fs.existsSync)(versionPath) && (0, import_node_fs.existsSync)(binaryPath)) {
215
- const cachedVersion = (await (0, import_promises.readFile)(versionPath, "utf8")).trim();
216
- if (cachedVersion === tag) {
217
- if (!quiet) {
218
- logger.info(`Using cached ${toolName} (${platformArch}): ${binaryPath}`);
219
- }
220
- return binaryPath;
221
- }
222
- }
223
- if (!quiet) {
224
- logger.info(`Downloading ${toolName} for ${platformArch}...`);
225
- }
226
- await downloadReleaseAsset(
227
- tag,
228
- assetName,
229
- binaryPath,
230
- { owner, repo },
231
- { quiet }
232
- );
233
- const isWindows = binaryName.endsWith(".exe");
234
- if (!isWindows) {
235
- (0, import_node_fs.chmodSync)(binaryPath, 493);
236
- if (removeMacOSQuarantine && process.platform === "darwin" && platformArch.startsWith("darwin")) {
237
- try {
238
- await (0, import_spawn.spawn)("xattr", ["-d", "com.apple.quarantine", binaryPath], {
239
- stdio: "ignore"
240
- });
241
- } catch {
242
- }
243
- }
244
- }
245
- await (0, import_promises.writeFile)(versionPath, tag, "utf8");
246
- if (!quiet) {
247
- logger.info(`Downloaded ${toolName} to ${binaryPath}`);
248
- }
249
- return binaryPath;
250
- }
251
251
  // Annotate the CommonJS export names for ESM import in node:
252
252
  0 && (module.exports = {
253
253
  SOCKET_BTM_REPO,
254
254
  downloadGitHubRelease,
255
255
  downloadReleaseAsset,
256
+ getAuthHeaders,
256
257
  getLatestRelease,
257
258
  getReleaseAssetUrl
258
259
  });