@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
package/dist/ipc.d.ts CHANGED
@@ -1,56 +1,9 @@
1
- import { z } from './zod';
2
- // Define BufferEncoding type for TypeScript compatibility.
3
- type BufferEncoding = globalThis.BufferEncoding;
4
1
  /**
5
- * IPC handshake schema - used for initial connection establishment.
6
- * The handshake includes version info and authentication tokens.
7
- * @internal Exported for testing purposes.
2
+ * @fileoverview Secure inter-process communication utilities for Socket CLI.
3
+ * File-based stub handoff (restricted-perm temp files with unique IDs and
4
+ * timestamps) for transferring data between processes that exceeds the size
5
+ * or exposure limits of environment variables.
8
6
  */
9
- export declare const IpcHandshakeSchema: z.ZodObject<{
10
- id: z.ZodString;
11
- timestamp: z.ZodNumber;
12
- type: z.ZodLiteral<"handshake">;
13
- data: z.ZodObject<{
14
- version: z.ZodString;
15
- pid: z.ZodNumber;
16
- apiToken: z.ZodOptional<z.ZodString>;
17
- appName: z.ZodString;
18
- }, z.core.$strip>;
19
- }, z.core.$strip>;
20
- /**
21
- * TypeScript interfaces for IPC communication.
22
- * These types ensure type consistency across the IPC module.
23
- */
24
- /**
25
- * Base IPC message interface.
26
- * All IPC messages must conform to this structure.
27
- */
28
- export interface IpcMessage<T = unknown> {
29
- /** Unique identifier for message tracking and response correlation. */
30
- id: string;
31
- /** Unix timestamp for freshness validation and replay prevention. */
32
- timestamp: number;
33
- /** Message type identifier for routing and handling. */
34
- type: string;
35
- /** Payload data - can be any JSON-serializable value. */
36
- data: T;
37
- }
38
- /**
39
- * IPC handshake message interface.
40
- * Used for initial connection establishment.
41
- */
42
- export interface IpcHandshake extends IpcMessage<{
43
- /** Protocol version for compatibility checking. */
44
- version: string;
45
- /** Process ID for identification. */
46
- pid: number;
47
- /** Optional API token for authentication. */
48
- apiToken?: string;
49
- /** Application name for multi-app support. */
50
- appName: string;
51
- }> {
52
- type: 'handshake';
53
- }
54
7
  /**
55
8
  * IPC stub file interface.
56
9
  * Represents the structure of stub files used for filesystem-based IPC.
@@ -63,38 +16,11 @@ export interface IpcStub {
63
16
  /** Creation timestamp for age validation. */
64
17
  timestamp: number;
65
18
  }
66
- /**
67
- * Options for IPC communication
68
- */
69
- export interface IpcOptions {
70
- /** Text encoding for message serialization. */
71
- encoding?: BufferEncoding;
72
- /** Timeout in milliseconds for async operations. */
73
- timeout?: number;
74
- }
75
- /**
76
- * Create a unique IPC channel identifier for message correlation.
77
- *
78
- * Generates a unique identifier that combines:
79
- * - A prefix for namespacing (defaults to 'socket')
80
- * - The current process ID for process identification
81
- * - A random hex string for uniqueness
82
- *
83
- * @param prefix - Optional prefix to namespace the channel ID
84
- * @returns A unique channel identifier string
85
- *
86
- * @example
87
- * ```typescript
88
- * const channelId = createIpcChannelId('socket-cli')
89
- * // Returns: 'socket-cli-12345-a1b2c3d4e5f6g7h8'
90
- * ```
91
- */
92
- export declare function createIpcChannelId(prefix?: string): string;
93
19
  /**
94
20
  * Get the IPC stub path for a given application.
95
21
  *
96
- * This function generates a unique file path for IPC stub files that are used
97
- * to pass data between processes. The stub files are stored in a hidden directory
22
+ * Generates a unique file path for IPC stub files that are used to pass
23
+ * data between processes. The stub files are stored in a hidden directory
98
24
  * within the system's temporary folder.
99
25
  *
100
26
  * ## Path Structure:
@@ -116,16 +42,14 @@ export declare function createIpcChannelId(prefix?: string): string;
116
42
  * // Returns: '/tmp/.socket-ipc/socket-cli/stub-12345.json' (Unix)
117
43
  * // Returns: 'C:\\Users\\Name\\AppData\\Local\\Temp\\.socket-ipc\\socket-cli\\stub-12345.json' (Windows)
118
44
  * ```
119
- *
120
- * @used Currently used by socket-cli for self-update and inter-process communication
121
45
  */
122
46
  export declare function getIpcStubPath(appName: string): string;
123
47
  /**
124
48
  * Write IPC data to a stub file for inter-process data transfer.
125
49
  *
126
- * This function creates a stub file containing data that needs to be passed
127
- * between processes. The stub file includes metadata like process ID and
128
- * timestamp for validation.
50
+ * Creates a stub file containing data that needs to be passed between
51
+ * processes. The file is written with 0o600 permissions so only the
52
+ * invoking user can read it.
129
53
  *
130
54
  * ## File Structure:
131
55
  * ```json
@@ -136,11 +60,6 @@ export declare function getIpcStubPath(appName: string): string;
136
60
  * }
137
61
  * ```
138
62
  *
139
- * ## Use Cases:
140
- * - Passing API tokens to child processes
141
- * - Transferring configuration between Socket CLI components
142
- * - Sharing large data that exceeds environment variable limits
143
- *
144
63
  * @param appName - The application identifier
145
64
  * @param data - The data to write to the stub file
146
65
  * @returns Promise resolving to the stub file path
@@ -155,212 +74,3 @@ export declare function getIpcStubPath(appName: string): string;
155
74
  * ```
156
75
  */
157
76
  export declare function writeIpcStub(appName: string, data: unknown): Promise<string>;
158
- /**
159
- * Read IPC data from a stub file with automatic cleanup.
160
- *
161
- * This function reads data from an IPC stub file and validates its freshness.
162
- * Stale files (older than 5 minutes) are automatically cleaned up to prevent
163
- * accumulation of temporary files.
164
- *
165
- * ## Validation Steps:
166
- * 1. Read and parse JSON file
167
- * 2. Validate structure with Zod schema
168
- * 3. Check timestamp freshness
169
- * 4. Clean up if stale
170
- * 5. Return data if valid
171
- *
172
- * @param stubPath - Path to the stub file to read
173
- * @returns Promise resolving to the data or null if invalid/stale
174
- *
175
- * @example
176
- * ```typescript
177
- * const data = await readIpcStub('/tmp/.socket-ipc/socket-cli/stub-12345.json')
178
- * if (data) {
179
- * console.log('Received:', data)
180
- * }
181
- * ```
182
- *
183
- * @unused Reserved for future implementation
184
- */
185
- export declare function readIpcStub(stubPath: string): Promise<unknown>;
186
- /**
187
- * Clean up IPC stub files for an application.
188
- *
189
- * This maintenance function removes stale IPC stub files to prevent
190
- * accumulation in the temporary directory. It's designed to be called
191
- * periodically or on application startup.
192
- *
193
- * ## Cleanup Rules:
194
- * - Files older than 5 minutes are removed (checked via both filesystem mtime and JSON timestamp)
195
- * - Only stub files (stub-*.json) are processed
196
- * - Errors are silently ignored (best-effort cleanup)
197
- *
198
- * @param appName - The application identifier
199
- * @returns Promise that resolves when cleanup is complete
200
- *
201
- * @example
202
- * ```typescript
203
- * // Clean up on application startup
204
- * await cleanupIpcStubs('socket-cli')
205
- * ```
206
- *
207
- * @unused Reserved for future implementation
208
- */
209
- export declare function cleanupIpcStubs(appName: string): Promise<void>;
210
- /**
211
- * Send data through Node.js IPC channel.
212
- *
213
- * This function sends structured messages through the Node.js IPC channel
214
- * when available. The IPC channel must be established with stdio: ['pipe', 'pipe', 'pipe', 'ipc'].
215
- *
216
- * ## Requirements:
217
- * - Process must have been spawned with IPC channel enabled
218
- * - Message must be serializable to JSON
219
- * - Process.send() must be available
220
- *
221
- * @param process - The process object with IPC channel
222
- * @param message - The IPC message to send
223
- * @returns true if message was sent, false otherwise
224
- *
225
- * @example
226
- * ```typescript
227
- * const message = createIpcMessage('handshake', { version: '1.0.0' })
228
- * const sent = sendIpc(childProcess, message)
229
- * ```
230
- *
231
- * @unused Reserved for bidirectional communication implementation
232
- */
233
- export declare function sendIpc(process: NodeJS.Process | unknown, message: IpcMessage): boolean;
234
- /**
235
- * Receive data through Node.js IPC channel.
236
- *
237
- * Sets up a listener for IPC messages with automatic validation and parsing.
238
- * Returns a cleanup function to remove the listener when no longer needed.
239
- *
240
- * ## Message Flow:
241
- * 1. Receive raw message from IPC channel
242
- * 2. Validate with parseIpcMessage
243
- * 3. Call handler if valid
244
- * 4. Ignore invalid messages
245
- *
246
- * @param handler - Function to call with valid IPC messages
247
- * @returns Cleanup function to remove the listener
248
- *
249
- * @example
250
- * ```typescript
251
- * const cleanup = onIpc((message) => {
252
- * console.log('Received:', message.type, message.data)
253
- * })
254
- * // Later...
255
- * cleanup() // Remove listener
256
- * ```
257
- *
258
- * @unused Reserved for bidirectional communication
259
- */
260
- export declare function onIpc(handler: (message: IpcMessage) => void): () => void;
261
- /**
262
- * Create a promise that resolves when a specific IPC message is received.
263
- *
264
- * This utility function provides async/await support for IPC communication,
265
- * allowing you to wait for specific message types with timeout support.
266
- *
267
- * ## Features:
268
- * - Automatic timeout handling
269
- * - Type-safe message data
270
- * - Resource cleanup on completion
271
- * - Promise-based API
272
- *
273
- * @param messageType - The message type to wait for
274
- * @param options - Options including timeout configuration
275
- * @returns Promise resolving to the message data
276
- *
277
- * @example
278
- * ```typescript
279
- * try {
280
- * const response = await waitForIpc<ConfigData>('config-response', {
281
- * timeout: 5000 // 5 seconds
282
- * })
283
- * console.log('Config received:', response)
284
- * } catch (error) {
285
- * console.error('Timeout waiting for config')
286
- * }
287
- * ```
288
- *
289
- * @unused Reserved for request-response pattern implementation
290
- */
291
- export declare function waitForIpc<T = unknown>(messageType: string, options?: IpcOptions): Promise<T>;
292
- /**
293
- * Create an IPC message with proper structure and metadata.
294
- *
295
- * This factory function creates properly structured IPC messages with:
296
- * - Unique ID for tracking
297
- * - Timestamp for freshness
298
- * - Type for routing
299
- * - Data payload
300
- *
301
- * @param type - The message type identifier
302
- * @param data - The message payload
303
- * @returns A properly structured IPC message
304
- *
305
- * @example
306
- * ```typescript
307
- * const handshake = createIpcMessage('handshake', {
308
- * version: '1.0.0',
309
- * pid: process.pid,
310
- * appName: 'socket-cli'
311
- * })
312
- * ```
313
- *
314
- * @unused Reserved for future message creation needs
315
- */
316
- export declare function createIpcMessage<T = unknown>(type: string, data: T): IpcMessage<T>;
317
- /**
318
- * Check if process has IPC channel available.
319
- *
320
- * This utility checks whether a process object has the necessary
321
- * properties for IPC communication. Used to determine if IPC
322
- * messaging is possible before attempting to send.
323
- *
324
- * @param process - The process object to check
325
- * @returns true if IPC is available, false otherwise
326
- *
327
- * @example
328
- * ```typescript
329
- * if (hasIpcChannel(childProcess)) {
330
- * sendIpc(childProcess, message)
331
- * } else {
332
- * // Fall back to alternative communication method
333
- * }
334
- * ```
335
- *
336
- * @unused Reserved for IPC availability detection
337
- */
338
- export declare function hasIpcChannel(process: unknown): boolean;
339
- /**
340
- * Safely parse and validate IPC messages.
341
- *
342
- * This function performs runtime validation of incoming messages
343
- * to ensure they conform to the IPC message structure. It uses
344
- * Zod schemas for robust validation.
345
- *
346
- * ## Validation Steps:
347
- * 1. Check if message is an object
348
- * 2. Validate required fields exist
349
- * 3. Validate field types
350
- * 4. Return typed message or null
351
- *
352
- * @param message - The raw message to parse
353
- * @returns Parsed IPC message or null if invalid
354
- *
355
- * @example
356
- * ```typescript
357
- * const parsed = parseIpcMessage(rawMessage)
358
- * if (parsed) {
359
- * handleMessage(parsed)
360
- * }
361
- * ```
362
- *
363
- * @unused Reserved for message validation needs
364
- */
365
- export declare function parseIpcMessage(message: unknown): IpcMessage | null;
366
- export {};
package/dist/ipc.js CHANGED
@@ -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;
@@ -29,95 +30,49 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
29
30
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
31
  var ipc_exports = {};
31
32
  __export(ipc_exports, {
32
- IpcHandshakeSchema: () => IpcHandshakeSchema,
33
- cleanupIpcStubs: () => cleanupIpcStubs,
34
- createIpcChannelId: () => createIpcChannelId,
35
- createIpcMessage: () => createIpcMessage,
36
33
  getIpcStubPath: () => getIpcStubPath,
37
- hasIpcChannel: () => hasIpcChannel,
38
- onIpc: () => onIpc,
39
- parseIpcMessage: () => parseIpcMessage,
40
- readIpcStub: () => readIpcStub,
41
- sendIpc: () => sendIpc,
42
- waitForIpc: () => waitForIpc,
43
34
  writeIpcStub: () => writeIpcStub
44
35
  });
45
36
  module.exports = __toCommonJS(ipc_exports);
46
37
  var import_node_process = __toESM(require("node:process"));
47
- var import_fs = require("./fs");
48
38
  var import_socket = require("./paths/socket");
49
39
  var import_zod = require("./zod");
50
- let _crypto;
51
- // @__NO_SIDE_EFFECTS__
52
- function getCrypto() {
53
- if (_crypto === void 0) {
54
- _crypto = require("crypto");
55
- }
56
- return _crypto;
57
- }
40
+ const IpcStubSchema = import_zod.z.object({
41
+ /** Process ID that created the stub. */
42
+ pid: import_zod.z.number().int().positive(),
43
+ /** Creation timestamp for age validation. */
44
+ timestamp: import_zod.z.number().positive(),
45
+ /** The actual data payload. */
46
+ data: import_zod.z.unknown()
47
+ });
58
48
  let _fs;
49
+ let _path;
50
+ async function ensureIpcDirectory(filePath) {
51
+ const fs = /* @__PURE__ */ getFs();
52
+ const path = /* @__PURE__ */ getPath();
53
+ const dir = path.dirname(filePath);
54
+ await fs.promises.mkdir(dir, { recursive: true, mode: 448 });
55
+ }
59
56
  // @__NO_SIDE_EFFECTS__
60
57
  function getFs() {
61
58
  if (_fs === void 0) {
62
- _fs = require("fs");
59
+ _fs = require("node:fs");
63
60
  }
64
61
  return _fs;
65
62
  }
66
- let _path;
67
63
  // @__NO_SIDE_EFFECTS__
68
64
  function getPath() {
69
65
  if (_path === void 0) {
70
- _path = require("path");
66
+ _path = require("node:path");
71
67
  }
72
68
  return _path;
73
69
  }
74
- const IpcMessageSchema = import_zod.z.object({
75
- /** Unique identifier for message tracking and response correlation. */
76
- id: import_zod.z.string().min(1),
77
- /** Unix timestamp for freshness validation and replay prevention. */
78
- timestamp: import_zod.z.number().positive(),
79
- /** Message type identifier for routing and handling. */
80
- type: import_zod.z.string().min(1),
81
- /** Payload data - can be any JSON-serializable value. */
82
- data: import_zod.z.unknown()
83
- });
84
- const IpcHandshakeSchema = IpcMessageSchema.extend({
85
- type: import_zod.z.literal("handshake"),
86
- data: import_zod.z.object({
87
- /** Protocol version for compatibility checking. */
88
- version: import_zod.z.string(),
89
- /** Process ID for identification. */
90
- pid: import_zod.z.number().int().positive(),
91
- /** Optional API token for authentication. */
92
- apiToken: import_zod.z.string().optional(),
93
- /** Application name for multi-app support. */
94
- appName: import_zod.z.string()
95
- })
96
- });
97
- const IpcStubSchema = import_zod.z.object({
98
- /** Process ID that created the stub. */
99
- pid: import_zod.z.number().int().positive(),
100
- /** Creation timestamp for age validation. */
101
- timestamp: import_zod.z.number().positive(),
102
- /** The actual data payload. */
103
- data: import_zod.z.unknown()
104
- });
105
- function createIpcChannelId(prefix = "socket") {
106
- const crypto = /* @__PURE__ */ getCrypto();
107
- return `${prefix}-${import_node_process.default.pid}-${crypto.randomBytes(8).toString("hex")}`;
108
- }
109
70
  function getIpcStubPath(appName) {
110
71
  const tempDir = (0, import_socket.getOsTmpDir)();
111
72
  const path = /* @__PURE__ */ getPath();
112
73
  const stubDir = path.join(tempDir, ".socket-ipc", appName);
113
74
  return path.join(stubDir, `stub-${import_node_process.default.pid}.json`);
114
75
  }
115
- async function ensureIpcDirectory(filePath) {
116
- const fs = /* @__PURE__ */ getFs();
117
- const path = /* @__PURE__ */ getPath();
118
- const dir = path.dirname(filePath);
119
- await fs.promises.mkdir(dir, { recursive: true, mode: 448 });
120
- }
121
76
  async function writeIpcStub(appName, data) {
122
77
  const stubPath = getIpcStubPath(appName);
123
78
  await ensureIpcDirectory(stubPath);
@@ -134,149 +89,8 @@ async function writeIpcStub(appName, data) {
134
89
  });
135
90
  return stubPath;
136
91
  }
137
- async function readIpcStub(stubPath) {
138
- try {
139
- const fs = /* @__PURE__ */ getFs();
140
- const content = await fs.promises.readFile(stubPath, "utf8");
141
- const parsed = JSON.parse(content);
142
- const validated = IpcStubSchema.parse(parsed);
143
- const ageMs = Date.now() - validated.timestamp;
144
- const maxAgeMs = 5 * 60 * 1e3;
145
- if (ageMs > maxAgeMs) {
146
- try {
147
- (0, import_fs.safeDeleteSync)(stubPath, { force: true });
148
- } catch {
149
- }
150
- return null;
151
- }
152
- return validated.data;
153
- } catch {
154
- return null;
155
- }
156
- }
157
- async function cleanupIpcStubs(appName) {
158
- const tempDir = (0, import_socket.getOsTmpDir)();
159
- const fs = /* @__PURE__ */ getFs();
160
- const path = /* @__PURE__ */ getPath();
161
- const stubDir = path.join(tempDir, ".socket-ipc", appName);
162
- try {
163
- const files = await fs.promises.readdir(stubDir);
164
- const now = Date.now();
165
- const maxAgeMs = 5 * 60 * 1e3;
166
- await Promise.allSettled(
167
- files.map(async (file) => {
168
- if (file.startsWith("stub-") && file.endsWith(".json")) {
169
- const filePath = path.join(stubDir, file);
170
- try {
171
- const stats = await fs.promises.stat(filePath);
172
- const mtimeAge = now - stats.mtimeMs;
173
- let isStale = mtimeAge > maxAgeMs;
174
- try {
175
- const content = await fs.promises.readFile(filePath, "utf8");
176
- const parsed = JSON.parse(content);
177
- const validated = IpcStubSchema.parse(parsed);
178
- const contentAge = now - validated.timestamp;
179
- isStale = isStale || contentAge > maxAgeMs;
180
- } catch {
181
- isStale = true;
182
- }
183
- if (isStale) {
184
- (0, import_fs.safeDeleteSync)(filePath, { force: true });
185
- }
186
- } catch {
187
- }
188
- }
189
- })
190
- );
191
- } catch {
192
- }
193
- }
194
- function sendIpc(process2, message) {
195
- if (process2 && typeof process2 === "object" && "send" in process2 && typeof process2.send === "function") {
196
- try {
197
- const validated = IpcMessageSchema.parse(message);
198
- return process2.send(validated);
199
- } catch {
200
- return false;
201
- }
202
- }
203
- return false;
204
- }
205
- function onIpc(handler) {
206
- const listener = (message) => {
207
- const parsed = parseIpcMessage(message);
208
- if (parsed) {
209
- handler(parsed);
210
- }
211
- };
212
- import_node_process.default.on("message", listener);
213
- return () => {
214
- import_node_process.default.off("message", listener);
215
- };
216
- }
217
- function waitForIpc(messageType, options = {}) {
218
- const { timeout = 3e4 } = options;
219
- return new Promise((resolve, reject) => {
220
- let cleanup = null;
221
- let timeoutId = null;
222
- const handleTimeout = () => {
223
- if (cleanup) {
224
- cleanup();
225
- }
226
- reject(new Error(`IPC timeout waiting for message type: ${messageType}`));
227
- };
228
- const handleMessage = (message) => {
229
- if (message.type === messageType) {
230
- if (timeoutId) {
231
- clearTimeout(timeoutId);
232
- }
233
- if (cleanup) {
234
- cleanup();
235
- }
236
- resolve(message.data);
237
- }
238
- };
239
- cleanup = onIpc(handleMessage);
240
- if (timeout > 0) {
241
- timeoutId = setTimeout(handleTimeout, timeout);
242
- timeoutId.unref();
243
- }
244
- });
245
- }
246
- function createIpcMessage(type, data) {
247
- const crypto = /* @__PURE__ */ getCrypto();
248
- return {
249
- id: crypto.randomBytes(16).toString("hex"),
250
- timestamp: Date.now(),
251
- type,
252
- data
253
- };
254
- }
255
- function hasIpcChannel(process2) {
256
- return Boolean(
257
- process2 && typeof process2 === "object" && "send" in process2 && typeof process2.send === "function" && "channel" in process2 && process2.channel !== void 0
258
- );
259
- }
260
- function parseIpcMessage(message) {
261
- try {
262
- const validated = IpcMessageSchema.parse(message);
263
- return validated;
264
- } catch {
265
- return null;
266
- }
267
- }
268
92
  // Annotate the CommonJS export names for ESM import in node:
269
93
  0 && (module.exports = {
270
- IpcHandshakeSchema,
271
- cleanupIpcStubs,
272
- createIpcChannelId,
273
- createIpcMessage,
274
94
  getIpcStubPath,
275
- hasIpcChannel,
276
- onIpc,
277
- parseIpcMessage,
278
- readIpcStub,
279
- sendIpc,
280
- waitForIpc,
281
95
  writeIpcStub
282
96
  });
@@ -1,3 +1,6 @@
1
+ /**
2
+ * @fileoverview Editable JSON file manipulation with formatting preservation.
3
+ */
1
4
  import type { EditableJsonConstructor } from './types';
2
5
  /**
3
6
  * Get the EditableJson class for JSON file manipulation.
@@ -12,5 +15,4 @@ import type { EditableJsonConstructor } from './types';
12
15
  * await config.save({ sort: true })
13
16
  * ```
14
17
  */
15
- /*@__NO_SIDE_EFFECTS__*/
16
18
  export declare function getEditableJsonClass<T = Record<string, unknown>>(): EditableJsonConstructor<T>;