@socketsecurity/lib 5.18.2 → 5.19.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.
Files changed (305) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/README.md +6 -5
  3. package/dist/abort.d.ts +3 -0
  4. package/dist/abort.js +1 -0
  5. package/dist/agent.d.ts +66 -49
  6. package/dist/agent.js +51 -50
  7. package/dist/ansi.d.ts +2 -5
  8. package/dist/ansi.js +3 -2
  9. package/dist/archives.d.ts +25 -21
  10. package/dist/archives.js +35 -34
  11. package/dist/argv/flags.d.ts +100 -91
  12. package/dist/argv/flags.js +88 -87
  13. package/dist/argv/parse.d.ts +30 -40
  14. package/dist/argv/parse.js +33 -32
  15. package/dist/arrays.d.ts +4 -8
  16. package/dist/arrays.js +1 -0
  17. package/dist/bin.d.ts +7 -5
  18. package/dist/bin.js +52 -29
  19. package/dist/cacache.d.ts +13 -12
  20. package/dist/cacache.js +9 -8
  21. package/dist/cache-with-ttl.d.ts +53 -35
  22. package/dist/cache-with-ttl.js +1 -0
  23. package/dist/colors.d.ts +5 -5
  24. package/dist/colors.js +1 -0
  25. package/dist/constants/agents.d.ts +5 -9
  26. package/dist/constants/agents.js +3 -2
  27. package/dist/constants/core.d.ts +5 -10
  28. package/dist/constants/core.js +2 -1
  29. package/dist/constants/encoding.d.ts +3 -3
  30. package/dist/constants/encoding.js +1 -0
  31. package/dist/constants/github.d.ts +3 -3
  32. package/dist/constants/github.js +1 -0
  33. package/dist/constants/licenses.d.ts +9 -2
  34. package/dist/constants/licenses.js +1 -0
  35. package/dist/constants/lifecycle-script-names.js +1 -0
  36. package/dist/constants/maintained-node-versions.d.ts +3 -0
  37. package/dist/constants/maintained-node-versions.js +1 -0
  38. package/dist/constants/node.d.ts +125 -16
  39. package/dist/constants/node.js +74 -73
  40. package/dist/constants/package-default-node-range.d.ts +3 -1
  41. package/dist/constants/package-default-node-range.js +2 -1
  42. package/dist/constants/package-default-socket-categories.d.ts +0 -1
  43. package/dist/constants/package-default-socket-categories.js +1 -0
  44. package/dist/constants/packages.d.ts +12 -17
  45. package/dist/constants/packages.js +31 -16
  46. package/dist/constants/platform.d.ts +1 -2
  47. package/dist/constants/platform.js +2 -1
  48. package/dist/constants/process.d.ts +18 -0
  49. package/dist/constants/process.js +1 -0
  50. package/dist/constants/socket.d.ts +4 -10
  51. package/dist/constants/socket.js +2 -1
  52. package/dist/constants/testing.d.ts +3 -3
  53. package/dist/constants/testing.js +1 -0
  54. package/dist/constants/time.d.ts +3 -4
  55. package/dist/constants/time.js +1 -0
  56. package/dist/constants/typescript.d.ts +10 -3
  57. package/dist/constants/typescript.js +5 -4
  58. package/dist/cover/code.d.ts +3 -0
  59. package/dist/cover/code.js +12 -12
  60. package/dist/cover/formatters.d.ts +12 -9
  61. package/dist/cover/formatters.js +19 -18
  62. package/dist/cover/type.d.ts +3 -0
  63. package/dist/cover/type.js +1 -0
  64. package/dist/cover/types.js +1 -0
  65. package/dist/debug.d.ts +32 -47
  66. package/dist/debug.js +97 -96
  67. package/dist/dlx/arborist.d.ts +134 -0
  68. package/dist/dlx/arborist.js +177 -0
  69. package/dist/dlx/binary.d.ts +25 -9
  70. package/dist/dlx/binary.js +34 -12
  71. package/dist/dlx/cache.d.ts +1 -0
  72. package/dist/dlx/cache.js +2 -1
  73. package/dist/dlx/detect.d.ts +15 -0
  74. package/dist/dlx/detect.js +28 -20
  75. package/dist/dlx/dir.d.ts +1 -11
  76. package/dist/dlx/dir.js +2 -12
  77. package/dist/dlx/integrity.d.ts +86 -0
  78. package/dist/dlx/integrity.js +112 -0
  79. package/dist/dlx/lockfile.d.ts +115 -0
  80. package/dist/dlx/lockfile.js +139 -0
  81. package/dist/dlx/manifest.d.ts +26 -1
  82. package/dist/dlx/manifest.js +9 -6
  83. package/dist/dlx/package.d.ts +58 -3
  84. package/dist/dlx/package.js +101 -74
  85. package/dist/dlx/packages.d.ts +1 -11
  86. package/dist/dlx/packages.js +3 -14
  87. package/dist/dlx/paths.d.ts +1 -0
  88. package/dist/dlx/paths.js +2 -1
  89. package/dist/effects/pulse-frames.js +1 -0
  90. package/dist/effects/text-shimmer.d.ts +13 -2
  91. package/dist/effects/text-shimmer.js +50 -46
  92. package/dist/effects/types.js +1 -0
  93. package/dist/effects/ultra.js +1 -0
  94. package/dist/env/ci.d.ts +6 -1
  95. package/dist/env/ci.js +1 -0
  96. package/dist/env/debug.d.ts +5 -1
  97. package/dist/env/debug.js +1 -0
  98. package/dist/env/github.d.ts +19 -23
  99. package/dist/env/github.js +5 -4
  100. package/dist/env/helpers.d.ts +21 -9
  101. package/dist/env/helpers.js +5 -11
  102. package/dist/env/home.d.ts +17 -4
  103. package/dist/env/home.js +2 -1
  104. package/dist/env/locale.d.ts +4 -3
  105. package/dist/env/locale.js +1 -0
  106. package/dist/env/node-auth-token.d.ts +5 -1
  107. package/dist/env/node-auth-token.js +1 -0
  108. package/dist/env/node-env.d.ts +5 -1
  109. package/dist/env/node-env.js +1 -0
  110. package/dist/env/npm.d.ts +4 -5
  111. package/dist/env/npm.js +1 -0
  112. package/dist/env/package-manager.d.ts +4 -3
  113. package/dist/env/package-manager.js +2 -1
  114. package/dist/env/path.d.ts +5 -1
  115. package/dist/env/path.js +1 -0
  116. package/dist/env/pre-commit.d.ts +5 -1
  117. package/dist/env/pre-commit.js +1 -0
  118. package/dist/env/rewire.d.ts +10 -0
  119. package/dist/env/rewire.js +7 -5
  120. package/dist/env/shell.d.ts +5 -1
  121. package/dist/env/shell.js +1 -0
  122. package/dist/env/socket-cli-shadow.d.ts +4 -5
  123. package/dist/env/socket-cli-shadow.js +1 -0
  124. package/dist/env/socket-cli.d.ts +4 -14
  125. package/dist/env/socket-cli.js +1 -0
  126. package/dist/env/socket.d.ts +6 -18
  127. package/dist/env/socket.js +4 -3
  128. package/dist/env/temp-dir.d.ts +4 -3
  129. package/dist/env/temp-dir.js +1 -0
  130. package/dist/env/term.d.ts +5 -1
  131. package/dist/env/term.js +1 -0
  132. package/dist/env/test.d.ts +4 -3
  133. package/dist/env/test.js +1 -0
  134. package/dist/env/windows.d.ts +4 -4
  135. package/dist/env/windows.js +1 -0
  136. package/dist/env/xdg.d.ts +4 -3
  137. package/dist/env/xdg.js +1 -0
  138. package/dist/env.d.ts +83 -23
  139. package/dist/env.js +112 -80
  140. package/dist/errors.js +1 -0
  141. package/dist/external/@npmcli/package-json.js +9 -3968
  142. package/dist/external/@socketregistry/packageurl-js.js +2 -2
  143. package/dist/external/debug.js +162 -328
  144. package/dist/external/external-pack.js +3 -2835
  145. package/dist/external/npm-pack.js +13939 -33342
  146. package/dist/external/zod.js +160 -7558
  147. package/dist/fs.d.ts +92 -113
  148. package/dist/fs.js +35 -35
  149. package/dist/git.d.ts +170 -164
  150. package/dist/git.js +113 -87
  151. package/dist/github.d.ts +249 -227
  152. package/dist/github.js +82 -81
  153. package/dist/globs.d.ts +31 -17
  154. package/dist/globs.js +51 -58
  155. package/dist/http-request.d.ts +99 -99
  156. package/dist/http-request.js +182 -156
  157. package/dist/ipc-cli.d.ts +28 -0
  158. package/dist/{utils/get-ipc.js → ipc-cli.js} +4 -3
  159. package/dist/ipc.d.ts +9 -299
  160. package/dist/ipc.js +18 -204
  161. package/dist/json/edit.d.ts +3 -1
  162. package/dist/json/edit.js +30 -30
  163. package/dist/json/format.d.ts +36 -36
  164. package/dist/json/format.js +22 -21
  165. package/dist/json/parse.d.ts +4 -2
  166. package/dist/json/parse.js +1 -0
  167. package/dist/json/types.js +1 -0
  168. package/dist/{links/index.d.ts → links.d.ts} +6 -2
  169. package/dist/{links/index.js → links.js} +5 -4
  170. package/dist/logger.d.ts +4 -6
  171. package/dist/logger.js +61 -53
  172. package/dist/memoization.d.ts +52 -49
  173. package/dist/memoization.js +55 -33
  174. package/dist/objects.d.ts +25 -44
  175. package/dist/objects.js +2 -1
  176. package/dist/package-extensions.d.ts +6 -0
  177. package/dist/package-extensions.js +2 -1
  178. package/dist/packages/edit.d.ts +3 -5
  179. package/dist/packages/edit.js +4 -3
  180. package/dist/packages/exports.d.ts +11 -14
  181. package/dist/packages/exports.js +11 -10
  182. package/dist/packages/isolation.d.ts +4 -0
  183. package/dist/packages/isolation.js +20 -19
  184. package/dist/packages/licenses.d.ts +3 -10
  185. package/dist/packages/licenses.js +2 -1
  186. package/dist/packages/manifest.d.ts +3 -3
  187. package/dist/packages/manifest.js +1 -0
  188. package/dist/packages/normalize.d.ts +3 -4
  189. package/dist/packages/normalize.js +1 -0
  190. package/dist/packages/operations.d.ts +3 -10
  191. package/dist/packages/operations.js +1 -0
  192. package/dist/packages/provenance.d.ts +10 -8
  193. package/dist/packages/provenance.js +55 -54
  194. package/dist/packages/specs.d.ts +3 -5
  195. package/dist/packages/specs.js +1 -0
  196. package/dist/packages/validation.d.ts +3 -3
  197. package/dist/packages/validation.js +1 -0
  198. package/dist/packages.d.ts +2 -17
  199. package/dist/packages.js +2 -1
  200. package/dist/paths/dirnames.d.ts +0 -2
  201. package/dist/paths/dirnames.js +1 -0
  202. package/dist/paths/exts.d.ts +0 -1
  203. package/dist/paths/exts.js +1 -0
  204. package/dist/paths/filenames.d.ts +0 -1
  205. package/dist/paths/filenames.js +1 -0
  206. package/dist/paths/globs.d.ts +0 -1
  207. package/dist/paths/globs.js +1 -0
  208. package/dist/paths/normalize.d.ts +115 -123
  209. package/dist/paths/normalize.js +128 -127
  210. package/dist/paths/packages.d.ts +3 -2
  211. package/dist/paths/packages.js +2 -1
  212. package/dist/paths/rewire.d.ts +9 -0
  213. package/dist/paths/rewire.js +5 -3
  214. package/dist/paths/socket.d.ts +37 -23
  215. package/dist/paths/socket.js +26 -25
  216. package/dist/performance.d.ts +87 -84
  217. package/dist/performance.js +97 -96
  218. package/dist/process-lock.d.ts +41 -1
  219. package/dist/process-lock.js +24 -17
  220. package/dist/promise-queue.d.ts +6 -0
  221. package/dist/promise-queue.js +1 -0
  222. package/dist/promises.d.ts +40 -55
  223. package/dist/promises.js +12 -11
  224. package/dist/regexps.d.ts +0 -5
  225. package/dist/regexps.js +1 -0
  226. package/dist/releases/github.d.ts +59 -63
  227. package/dist/releases/github.js +95 -100
  228. package/dist/releases/socket-btm.d.ts +9 -2
  229. package/dist/releases/socket-btm.js +32 -25
  230. package/dist/sea.d.ts +5 -0
  231. package/dist/sea.js +1 -0
  232. package/dist/shadow.d.ts +4 -0
  233. package/dist/shadow.js +1 -0
  234. package/dist/signal-exit.d.ts +7 -7
  235. package/dist/signal-exit.js +46 -45
  236. package/dist/sorts.d.ts +5 -7
  237. package/dist/sorts.js +11 -10
  238. package/dist/spawn.d.ts +96 -77
  239. package/dist/spawn.js +39 -38
  240. package/dist/spinner.d.ts +21 -22
  241. package/dist/spinner.js +15 -11
  242. package/dist/ssri.d.ts +31 -36
  243. package/dist/ssri.js +15 -14
  244. package/dist/stdio/_stream.d.ts +31 -0
  245. package/dist/stdio/_stream.js +57 -0
  246. package/dist/stdio/divider.d.ts +40 -36
  247. package/dist/stdio/divider.js +10 -9
  248. package/dist/stdio/footer.d.ts +20 -0
  249. package/dist/stdio/footer.js +12 -2
  250. package/dist/stdio/header.d.ts +4 -16
  251. package/dist/stdio/header.js +1 -9
  252. package/dist/stdio/stderr.d.ts +51 -39
  253. package/dist/stdio/stderr.js +19 -22
  254. package/dist/stdio/stdout.d.ts +54 -52
  255. package/dist/stdio/stdout.js +35 -33
  256. package/dist/streams.d.ts +4 -3
  257. package/dist/streams.js +1 -0
  258. package/dist/strings.d.ts +31 -85
  259. package/dist/strings.js +5 -49
  260. package/dist/suppress-warnings.d.ts +32 -15
  261. package/dist/suppress-warnings.js +14 -13
  262. package/dist/tables.d.ts +30 -26
  263. package/dist/tables.js +24 -23
  264. package/dist/temporary-executor.d.ts +4 -0
  265. package/dist/temporary-executor.js +1 -0
  266. package/dist/themes/context.d.ts +30 -26
  267. package/dist/themes/context.js +19 -18
  268. package/dist/themes/index.d.ts +0 -4
  269. package/dist/themes/index.js +1 -0
  270. package/dist/themes/themes.d.ts +1 -1
  271. package/dist/themes/themes.js +1 -0
  272. package/dist/themes/types.js +1 -0
  273. package/dist/themes/utils.d.ts +46 -43
  274. package/dist/themes/utils.js +45 -44
  275. package/dist/types.d.ts +48 -52
  276. package/dist/types.js +35 -35
  277. package/dist/url.d.ts +48 -53
  278. package/dist/url.js +38 -37
  279. package/dist/validation/json-parser.d.ts +19 -190
  280. package/dist/validation/json-parser.js +5 -69
  281. package/dist/validation/types.d.ts +9 -33
  282. package/dist/validation/types.js +1 -0
  283. package/dist/versions.d.ts +1 -0
  284. package/dist/versions.js +2 -1
  285. package/dist/words.d.ts +3 -6
  286. package/dist/words.js +1 -0
  287. package/dist/zod.js +1 -0
  288. package/package.json +34 -66
  289. package/dist/external/@inquirer/checkbox.js +0 -5
  290. package/dist/external/@inquirer/confirm.js +0 -5
  291. package/dist/external/@inquirer/input.js +0 -5
  292. package/dist/external/@inquirer/password.js +0 -5
  293. package/dist/external/@inquirer/search.js +0 -5
  294. package/dist/external/@inquirer/select.js +0 -5
  295. package/dist/functions.d.ts +0 -57
  296. package/dist/functions.js +0 -70
  297. package/dist/stdio/clear.d.ts +0 -140
  298. package/dist/stdio/clear.js +0 -95
  299. package/dist/stdio/mask.d.ts +0 -151
  300. package/dist/stdio/mask.js +0 -224
  301. package/dist/stdio/progress.d.ts +0 -148
  302. package/dist/stdio/progress.js +0 -212
  303. package/dist/stdio/prompts.d.ts +0 -195
  304. package/dist/stdio/prompts.js +0 -176
  305. package/dist/utils/get-ipc.d.ts +0 -15
@@ -1,28 +1,82 @@
1
+ /**
2
+ * @fileoverview Performance monitoring utilities for profiling and optimization.
3
+ * Provides timing, profiling, and performance metric collection for identifying bottlenecks.
4
+ */
1
5
  /**
2
6
  * Performance metrics collected during execution.
3
7
  */
4
- type PerformanceMetrics = {
8
+ export type PerformanceMetrics = {
5
9
  operation: string;
6
10
  duration: number;
7
11
  timestamp: number;
8
12
  metadata?: Record<string, unknown>;
9
13
  };
10
14
  /**
11
- * Start a performance timer for an operation.
12
- * Returns a stop function that records the duration.
15
+ * Clear all collected performance metrics.
13
16
  *
14
- * @param operation - Name of the operation being timed
15
- * @param metadata - Optional metadata to attach to the metric
16
- * @returns Stop function that completes the timing
17
+ * @example
18
+ * import { clearPerformanceMetrics } from '@socketsecurity/lib/performance'
19
+ *
20
+ * clearPerformanceMetrics()
21
+ */
22
+ export declare function clearPerformanceMetrics(): void;
23
+ /**
24
+ * Create a performance report for the current execution.
25
+ * Only available when DEBUG=perf is enabled.
26
+ *
27
+ * @returns Formatted performance report
17
28
  *
18
29
  * @example
19
- * import { perfTimer } from '@socketsecurity/lib/performance'
30
+ * import { generatePerformanceReport } from '@socketsecurity/lib/performance'
20
31
  *
21
- * const stop = perfTimer('api-call')
22
- * await fetchData()
23
- * stop({ endpoint: '/npm/lodash/score' })
32
+ * console.log(generatePerformanceReport())
33
+ * // ╔═══════════════════════════════════════════════╗
34
+ * // ║ Performance Report ║
35
+ * // ╚═══════════════════════════════════════════════╝
36
+ * //
37
+ * // api-call:
38
+ * // Calls: 5
39
+ * // Avg: 246.8ms
40
+ * // Min: 100ms
41
+ * // Max: 500ms
42
+ * // Total: 1234ms
24
43
  */
25
- export declare function perfTimer(operation: string, metadata?: Record<string, unknown>): (additionalMetadata?: Record<string, unknown>) => void;
44
+ export declare function generatePerformanceReport(): string;
45
+ /**
46
+ * Get all collected performance metrics.
47
+ * Only available when DEBUG=perf is enabled.
48
+ *
49
+ * @returns Array of performance metrics
50
+ *
51
+ * @example
52
+ * import { getPerformanceMetrics } from '@socketsecurity/lib/performance'
53
+ *
54
+ * const metrics = getPerformanceMetrics()
55
+ * console.log(metrics)
56
+ */
57
+ export declare function getPerformanceMetrics(): PerformanceMetrics[];
58
+ /**
59
+ * Get performance summary statistics.
60
+ *
61
+ * @returns Summary of metrics grouped by operation
62
+ *
63
+ * @example
64
+ * import { getPerformanceSummary } from '@socketsecurity/lib/performance'
65
+ *
66
+ * const summary = getPerformanceSummary()
67
+ * console.log(summary)
68
+ * // {
69
+ * // 'api-call': { count: 5, total: 1234, avg: 246.8, min: 100, max: 500 },
70
+ * // 'file-read': { count: 10, total: 50, avg: 5, min: 2, max: 15 }
71
+ * // }
72
+ */
73
+ export declare function getPerformanceSummary(): Record<string, {
74
+ count: number;
75
+ total: number;
76
+ avg: number;
77
+ min: number;
78
+ max: number;
79
+ }>;
26
80
  /**
27
81
  * Measure execution time of an async function.
28
82
  *
@@ -63,49 +117,39 @@ export declare function measureSync<T>(operation: string, fn: () => T, metadata?
63
117
  duration: number;
64
118
  };
65
119
  /**
66
- * Get all collected performance metrics.
67
- * Only available when DEBUG=perf is enabled.
68
- *
69
- * @returns Array of performance metrics
70
- *
71
- * @example
72
- * import { getPerformanceMetrics } from '@socketsecurity/lib/performance'
120
+ * Mark a checkpoint in performance tracking.
121
+ * Useful for tracking progress through complex operations.
73
122
  *
74
- * const metrics = getPerformanceMetrics()
75
- * console.log(metrics)
76
- */
77
- export declare function getPerformanceMetrics(): PerformanceMetrics[];
78
- /**
79
- * Clear all collected performance metrics.
123
+ * @param checkpoint - Name of the checkpoint
124
+ * @param metadata - Optional metadata
80
125
  *
81
126
  * @example
82
- * import { clearPerformanceMetrics } from '@socketsecurity/lib/performance'
127
+ * import { perfCheckpoint } from '@socketsecurity/lib/performance'
83
128
  *
84
- * clearPerformanceMetrics()
129
+ * perfCheckpoint('start-scan')
130
+ * // ... do work ...
131
+ * perfCheckpoint('fetch-packages', { count: 50 })
132
+ * // ... do work ...
133
+ * perfCheckpoint('analyze-issues', { issueCount: 10 })
134
+ * perfCheckpoint('end-scan')
85
135
  */
86
- export declare function clearPerformanceMetrics(): void;
136
+ export declare function perfCheckpoint(checkpoint: string, metadata?: Record<string, unknown>): void;
87
137
  /**
88
- * Get performance summary statistics.
138
+ * Start a performance timer for an operation.
139
+ * Returns a stop function that records the duration.
89
140
  *
90
- * @returns Summary of metrics grouped by operation
141
+ * @param operation - Name of the operation being timed
142
+ * @param metadata - Optional metadata to attach to the metric
143
+ * @returns Stop function that completes the timing
91
144
  *
92
145
  * @example
93
- * import { getPerformanceSummary } from '@socketsecurity/lib/performance'
146
+ * import { perfTimer } from '@socketsecurity/lib/performance'
94
147
  *
95
- * const summary = getPerformanceSummary()
96
- * console.log(summary)
97
- * // {
98
- * // 'api-call': { count: 5, total: 1234, avg: 246.8, min: 100, max: 500 },
99
- * // 'file-read': { count: 10, total: 50, avg: 5, min: 2, max: 15 }
100
- * // }
148
+ * const stop = perfTimer('api-call')
149
+ * await fetchData()
150
+ * stop({ endpoint: '/npm/lodash/score' })
101
151
  */
102
- export declare function getPerformanceSummary(): Record<string, {
103
- count: number;
104
- total: number;
105
- avg: number;
106
- min: number;
107
- max: number;
108
- }>;
152
+ export declare function perfTimer(operation: string, metadata?: Record<string, unknown>): (additionalMetadata?: Record<string, unknown>) => void;
109
153
  /**
110
154
  * Print performance summary to console.
111
155
  * Only prints when DEBUG=perf is enabled.
@@ -119,24 +163,6 @@ export declare function getPerformanceSummary(): Record<string, {
119
163
  * // file-read: 10 calls, avg 5ms (min 2ms, max 15ms, total 50ms)
120
164
  */
121
165
  export declare function printPerformanceSummary(): void;
122
- /**
123
- * Mark a checkpoint in performance tracking.
124
- * Useful for tracking progress through complex operations.
125
- *
126
- * @param checkpoint - Name of the checkpoint
127
- * @param metadata - Optional metadata
128
- *
129
- * @example
130
- * import { perfCheckpoint } from '@socketsecurity/lib/performance'
131
- *
132
- * perfCheckpoint('start-scan')
133
- * // ... do work ...
134
- * perfCheckpoint('fetch-packages', { count: 50 })
135
- * // ... do work ...
136
- * perfCheckpoint('analyze-issues', { issueCount: 10 })
137
- * perfCheckpoint('end-scan')
138
- */
139
- export declare function perfCheckpoint(checkpoint: string, metadata?: Record<string, unknown>): void;
140
166
  /**
141
167
  * Track memory usage at a specific point.
142
168
  * Only available when DEBUG=perf is enabled.
@@ -153,26 +179,3 @@ export declare function perfCheckpoint(checkpoint: string, metadata?: Record<str
153
179
  * console.log(`Memory increased by ${memAfter - memBefore}MB`)
154
180
  */
155
181
  export declare function trackMemory(label: string): number;
156
- /**
157
- * Create a performance report for the current execution.
158
- * Only available when DEBUG=perf is enabled.
159
- *
160
- * @returns Formatted performance report
161
- *
162
- * @example
163
- * import { generatePerformanceReport } from '@socketsecurity/lib/performance'
164
- *
165
- * console.log(generatePerformanceReport())
166
- * // ╔═══════════════════════════════════════════════╗
167
- * // ║ Performance Report ║
168
- * // ╚═══════════════════════════════════════════════╝
169
- * //
170
- * // api-call:
171
- * // Calls: 5
172
- * // Avg: 246.8ms
173
- * // Min: 100ms
174
- * // Max: 500ms
175
- * // Total: 1234ms
176
- */
177
- export declare function generatePerformanceReport(): string;
178
- export {};
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  /* Socket Lib - Built with esbuild */
3
+ "use strict";
3
4
  var __create = Object.create;
4
5
  var __defProp = Object.defineProperty;
5
6
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -47,25 +48,75 @@ const performanceMetrics = [];
47
48
  function isPerfEnabled() {
48
49
  return import_node_process.default.env["DEBUG"]?.includes("perf") || false;
49
50
  }
50
- function perfTimer(operation, metadata) {
51
- if (!isPerfEnabled()) {
52
- return () => {
53
- };
51
+ function clearPerformanceMetrics() {
52
+ performanceMetrics.length = 0;
53
+ (0, import_debug.debugLog)("[perf] Cleared performance metrics");
54
+ }
55
+ function generatePerformanceReport() {
56
+ if (!isPerfEnabled() || performanceMetrics.length === 0) {
57
+ return "(no performance data collected - enable with DEBUG=perf)";
54
58
  }
55
- const start = performance.now();
56
- (0, import_debug.debugLog)(`[perf] [START] ${operation}`);
57
- return (additionalMetadata) => {
58
- const duration = performance.now() - start;
59
- const metric = {
60
- operation,
61
- // Round to 2 decimals
62
- duration: Math.round(duration * 100) / 100,
63
- timestamp: Date.now(),
64
- metadata: { ...metadata, ...additionalMetadata }
59
+ const summary = getPerformanceSummary();
60
+ const operations = Object.keys(summary).sort();
61
+ let report = "\n\u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557\n";
62
+ report += "\u2551 Performance Report \u2551\n";
63
+ report += "\u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D\n\n";
64
+ for (const operation of operations) {
65
+ const stats = summary[operation];
66
+ report += `${operation}:
67
+ `;
68
+ report += ` Calls: ${stats.count}
69
+ `;
70
+ report += ` Avg: ${stats.avg}ms
71
+ `;
72
+ report += ` Min: ${stats.min}ms
73
+ `;
74
+ report += ` Max: ${stats.max}ms
75
+ `;
76
+ report += ` Total: ${stats.total}ms
77
+
78
+ `;
79
+ }
80
+ const totalDuration = Object.values(summary).reduce(
81
+ (sum, s) => sum + s.total,
82
+ 0
83
+ );
84
+ report += `Total measured time: ${Math.round(totalDuration * 100) / 100}ms
85
+ `;
86
+ return report;
87
+ }
88
+ function getPerformanceMetrics() {
89
+ return [...performanceMetrics];
90
+ }
91
+ function getPerformanceSummary() {
92
+ const summary = { __proto__: null };
93
+ for (const metric of performanceMetrics) {
94
+ const { duration, operation } = metric;
95
+ if (!summary[operation]) {
96
+ summary[operation] = {
97
+ count: 0,
98
+ total: 0,
99
+ min: Number.POSITIVE_INFINITY,
100
+ max: Number.NEGATIVE_INFINITY
101
+ };
102
+ }
103
+ const stats = summary[operation];
104
+ stats.count++;
105
+ stats.total += duration;
106
+ stats.min = Math.min(stats.min, duration);
107
+ stats.max = Math.max(stats.max, duration);
108
+ }
109
+ const result = { __proto__: null };
110
+ for (const { 0: operation, 1: stats } of Object.entries(summary)) {
111
+ result[operation] = {
112
+ count: stats.count,
113
+ total: Math.round(stats.total * 100) / 100,
114
+ avg: Math.round(stats.total / stats.count * 100) / 100,
115
+ min: Math.round(stats.min * 100) / 100,
116
+ max: Math.round(stats.max * 100) / 100
65
117
  };
66
- performanceMetrics.push(metric);
67
- (0, import_debug.debugLog)(`[perf] [END] ${operation} - ${metric.duration}ms`);
68
- };
118
+ }
119
+ return result;
69
120
  }
70
121
  async function measure(operation, fn, metadata) {
71
122
  const stop = perfTimer(operation, metadata);
@@ -97,42 +148,38 @@ function measureSync(operation, fn, metadata) {
97
148
  throw e;
98
149
  }
99
150
  }
100
- function getPerformanceMetrics() {
101
- return [...performanceMetrics];
102
- }
103
- function clearPerformanceMetrics() {
104
- performanceMetrics.length = 0;
105
- (0, import_debug.debugLog)("[perf] Cleared performance metrics");
106
- }
107
- function getPerformanceSummary() {
108
- const summary = /* @__PURE__ */ Object.create(null);
109
- for (const metric of performanceMetrics) {
110
- const { duration, operation } = metric;
111
- if (!summary[operation]) {
112
- summary[operation] = {
113
- count: 0,
114
- total: 0,
115
- min: Number.POSITIVE_INFINITY,
116
- max: Number.NEGATIVE_INFINITY
117
- };
118
- }
119
- const stats = summary[operation];
120
- stats.count++;
121
- stats.total += duration;
122
- stats.min = Math.min(stats.min, duration);
123
- stats.max = Math.max(stats.max, duration);
151
+ function perfCheckpoint(checkpoint, metadata) {
152
+ if (!isPerfEnabled()) {
153
+ return;
124
154
  }
125
- const result = /* @__PURE__ */ Object.create(null);
126
- for (const { 0: operation, 1: stats } of Object.entries(summary)) {
127
- result[operation] = {
128
- count: stats.count,
129
- total: Math.round(stats.total * 100) / 100,
130
- avg: Math.round(stats.total / stats.count * 100) / 100,
131
- min: Math.round(stats.min * 100) / 100,
132
- max: Math.round(stats.max * 100) / 100
155
+ const metric = {
156
+ operation: `checkpoint:${checkpoint}`,
157
+ duration: 0,
158
+ timestamp: Date.now(),
159
+ ...metadata ? { metadata } : {}
160
+ };
161
+ performanceMetrics.push(metric);
162
+ (0, import_debug.debugLog)(`[perf] [CHECKPOINT] ${checkpoint}`);
163
+ }
164
+ function perfTimer(operation, metadata) {
165
+ if (!isPerfEnabled()) {
166
+ return () => {
133
167
  };
134
168
  }
135
- return result;
169
+ const start = performance.now();
170
+ (0, import_debug.debugLog)(`[perf] [START] ${operation}`);
171
+ return (additionalMetadata) => {
172
+ const duration = performance.now() - start;
173
+ const metric = {
174
+ operation,
175
+ // Round to 2 decimals
176
+ duration: Math.round(duration * 100) / 100,
177
+ timestamp: Date.now(),
178
+ metadata: { ...metadata, ...additionalMetadata }
179
+ };
180
+ performanceMetrics.push(metric);
181
+ (0, import_debug.debugLog)(`[perf] [END] ${operation} - ${metric.duration}ms`);
182
+ };
136
183
  }
137
184
  function printPerformanceSummary() {
138
185
  if (!isPerfEnabled() || performanceMetrics.length === 0) {
@@ -149,19 +196,6 @@ function printPerformanceSummary() {
149
196
  }
150
197
  (0, import_debug.debugLog)("[perf] =========================\n");
151
198
  }
152
- function perfCheckpoint(checkpoint, metadata) {
153
- if (!isPerfEnabled()) {
154
- return;
155
- }
156
- const metric = {
157
- operation: `checkpoint:${checkpoint}`,
158
- duration: 0,
159
- timestamp: Date.now(),
160
- ...metadata ? { metadata } : {}
161
- };
162
- performanceMetrics.push(metric);
163
- (0, import_debug.debugLog)(`[perf] [CHECKPOINT] ${checkpoint}`);
164
- }
165
199
  function trackMemory(label) {
166
200
  if (!isPerfEnabled()) {
167
201
  return 0;
@@ -182,39 +216,6 @@ function trackMemory(label) {
182
216
  performanceMetrics.push(metric);
183
217
  return heapUsedMB;
184
218
  }
185
- function generatePerformanceReport() {
186
- if (!isPerfEnabled() || performanceMetrics.length === 0) {
187
- return "(no performance data collected - enable with DEBUG=perf)";
188
- }
189
- const summary = getPerformanceSummary();
190
- const operations = Object.keys(summary).sort();
191
- let report = "\n\u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557\n";
192
- report += "\u2551 Performance Report \u2551\n";
193
- report += "\u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D\n\n";
194
- for (const operation of operations) {
195
- const stats = summary[operation];
196
- report += `${operation}:
197
- `;
198
- report += ` Calls: ${stats.count}
199
- `;
200
- report += ` Avg: ${stats.avg}ms
201
- `;
202
- report += ` Min: ${stats.min}ms
203
- `;
204
- report += ` Max: ${stats.max}ms
205
- `;
206
- report += ` Total: ${stats.total}ms
207
-
208
- `;
209
- }
210
- const totalDuration = Object.values(summary).reduce(
211
- (sum, s) => sum + s.total,
212
- 0
213
- );
214
- report += `Total measured time: ${Math.round(totalDuration * 100) / 100}ms
215
- `;
216
- return report;
217
- }
218
219
  // Annotate the CommonJS export names for ESM import in node:
219
220
  0 && (module.exports = {
220
221
  clearPerformanceMetrics,
@@ -1,3 +1,44 @@
1
+ /**
2
+ * @fileoverview Process locking utilities with stale detection and exit cleanup.
3
+ * Provides cross-platform inter-process synchronization using directory-based locks.
4
+ * Aligned with npm's npx locking strategy (5-second stale timeout, periodic touching).
5
+ *
6
+ * ## Why directories instead of files?
7
+ *
8
+ * This implementation uses `mkdir()` to create lock directories (not files) because:
9
+ *
10
+ * 1. **Atomic guarantee**: `mkdir()` is guaranteed atomic across ALL filesystems,
11
+ * including NFS. Only ONE process can successfully create the directory. If it
12
+ * exists, `mkdir()` fails with EEXIST instantly with no race conditions.
13
+ *
14
+ * 2. **File-based locking issues**:
15
+ * - `writeFile()` with `flag: 'wx'` - atomicity can fail on NFS
16
+ * - `open()` with `O_EXCL` - not guaranteed atomic on older NFS
17
+ * - Traditional lockfiles - can have race conditions on network filesystems
18
+ *
19
+ * 3. **Simplicity**: No need to write/read file content, track PIDs, or manage
20
+ * file descriptors. Just create/delete directory and check mtime.
21
+ *
22
+ * 4. **Historical precedent**: Well-known Unix locking pattern used by package
23
+ * managers for decades. Git uses similar approach for `.git/index.lock`.
24
+ *
25
+ * ## The mtime trick
26
+ *
27
+ * We periodically update the lock directory's mtime (modification time) by
28
+ * "touching" it to signal "I'm still actively working". This prevents other
29
+ * processes from treating the lock as stale and removing it.
30
+ *
31
+ * **The lock directory remains empty** - it's just a sentinel that signals
32
+ * "locked". The mtime is the only data needed to track lock freshness.
33
+ *
34
+ * ## npm npx compatibility
35
+ *
36
+ * This implementation matches npm npx's concurrency.lock approach:
37
+ * - Lock created via `mkdir(path.join(installDir, 'concurrency.lock'))`
38
+ * - 5-second stale timeout (if mtime is older than 5s, lock is stale)
39
+ * - 2-second touching interval (updates mtime every 2s to keep lock fresh)
40
+ * - Automatic cleanup on process exit
41
+ */
1
42
  /**
2
43
  * Lock acquisition options.
3
44
  */
@@ -135,6 +176,5 @@ declare class ProcessLockManager {
135
176
  */
136
177
  withLock<T>(lockPath: string, fn: () => Promise<T>, options?: ProcessLockOptions): Promise<T>;
137
178
  }
138
- // Export singleton instance.
139
179
  export declare const processLock: ProcessLockManager;
140
180
  export {};
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  /* Socket Lib - Built with esbuild */
3
+ "use strict";
3
4
  var __defProp = Object.defineProperty;
4
5
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -30,12 +31,18 @@ let _fs;
30
31
  // @__NO_SIDE_EFFECTS__
31
32
  function getFs() {
32
33
  if (_fs === void 0) {
33
- _fs = require("fs");
34
+ _fs = require("node:fs");
34
35
  }
35
36
  return _fs;
36
37
  }
37
- const fs = /* @__PURE__ */ getFs();
38
- const { existsSync, mkdirSync, statSync, utimesSync } = fs;
38
+ let _path;
39
+ // @__NO_SIDE_EFFECTS__
40
+ function getPath() {
41
+ if (_path === void 0) {
42
+ _path = require("node:path");
43
+ }
44
+ return _path;
45
+ }
39
46
  const logger = (0, import_logger.getDefaultLogger)();
40
47
  class ProcessLockManager {
41
48
  activeLocks = /* @__PURE__ */ new Set();
@@ -54,9 +61,10 @@ class ProcessLockManager {
54
61
  clearInterval(timer);
55
62
  }
56
63
  this.touchTimers.clear();
64
+ const fs = /* @__PURE__ */ getFs();
57
65
  for (const lockPath of this.activeLocks) {
58
66
  try {
59
- if (existsSync(lockPath)) {
67
+ if (fs.existsSync(lockPath)) {
60
68
  (0, import_fs.safeDeleteSync)(lockPath, { recursive: true });
61
69
  }
62
70
  } catch {
@@ -73,9 +81,10 @@ class ProcessLockManager {
73
81
  */
74
82
  touchLock(lockPath) {
75
83
  try {
76
- if (existsSync(lockPath)) {
77
- const now = /* @__PURE__ */ new Date();
78
- utimesSync(lockPath, now, now);
84
+ const fs = /* @__PURE__ */ getFs();
85
+ if (fs.existsSync(lockPath)) {
86
+ const now = Date.now() / 1e3;
87
+ fs.utimesSync(lockPath, now, now);
79
88
  }
80
89
  } catch (error) {
81
90
  logger.warn(
@@ -123,7 +132,7 @@ class ProcessLockManager {
123
132
  */
124
133
  isStale(lockPath, staleMs) {
125
134
  try {
126
- const stats = statSync(lockPath, { throwIfNoEntry: false });
135
+ const stats = (/* @__PURE__ */ getFs()).statSync(lockPath, { throwIfNoEntry: false });
127
136
  if (!stats) {
128
137
  return false;
129
138
  }
@@ -176,17 +185,15 @@ class ProcessLockManager {
176
185
  } catch {
177
186
  }
178
187
  }
179
- if (existsSync(lockPath)) {
188
+ const fs = /* @__PURE__ */ getFs();
189
+ if (fs.existsSync(lockPath)) {
180
190
  throw new Error(`Lock already exists: ${lockPath}`);
181
191
  }
182
- const lastSlash = Math.max(
183
- lockPath.lastIndexOf("/"),
184
- lockPath.lastIndexOf("\\")
185
- );
186
- if (lastSlash > 0) {
187
- mkdirSync(lockPath.slice(0, lastSlash), { recursive: true });
192
+ const parent = (/* @__PURE__ */ getPath()).dirname(lockPath);
193
+ if (parent && parent !== "." && parent !== lockPath) {
194
+ fs.mkdirSync(parent, { recursive: true });
188
195
  }
189
- mkdirSync(lockPath);
196
+ fs.mkdirSync(lockPath);
190
197
  this.activeLocks.add(lockPath);
191
198
  this.startTouchTimer(lockPath, touchIntervalMs);
192
199
  return () => this.release(lockPath);
@@ -270,7 +277,7 @@ To resolve:
270
277
  release(lockPath) {
271
278
  this.stopTouchTimer(lockPath);
272
279
  try {
273
- if (existsSync(lockPath)) {
280
+ if ((/* @__PURE__ */ getFs()).existsSync(lockPath)) {
274
281
  (0, import_fs.safeDeleteSync)(lockPath, { recursive: true });
275
282
  }
276
283
  this.activeLocks.delete(lockPath);
@@ -1,3 +1,9 @@
1
+ /**
2
+ * @fileoverview Bounded concurrency promise queue.
3
+ * Exports the `PromiseQueue` class, which limits how many async tasks run
4
+ * simultaneously, supports an optional max queue length (dropping the oldest
5
+ * pending task when exceeded), and exposes an idle-wait helper.
6
+ */
1
7
  export declare class PromiseQueue {
2
8
  private queue;
3
9
  private running;
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  /* Socket Lib - Built with esbuild */
3
+ "use strict";
3
4
  var __defProp = Object.defineProperty;
4
5
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
6
  var __getOwnPropNames = Object.getOwnPropertyNames;