-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.ts
More file actions
287 lines (271 loc) · 9.01 KB
/
socket.ts
File metadata and controls
287 lines (271 loc) · 9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* @fileoverview Path utilities for Socket ecosystem directories.
* Provides platform-aware path resolution for Socket tools' shared directory structure.
*
* Directory Structure:
* ~/.socket/
* ├── _cacache/ # Content-addressable cache for npm packages
* ├── _dlx/ # DLX installations (content-addressed by hash)
* │ ├── <hash>/ # npm package installs (dlx-package)
* │ └── <hash>/ # binary downloads (dlx-binary)
* ├── _socket/ # Socket CLI app directory
* ├── _registry/ # Socket Registry app directory
* └── _sfw/ # Socket Firewall app directory
*/
import { CACHE_GITHUB_DIR } from '../constants/github'
import {
SOCKET_APP_PREFIX,
SOCKET_CLI_APP_NAME,
SOCKET_DLX_APP_NAME,
SOCKET_REGISTRY_APP_NAME,
} from '../constants/socket'
import { getHome } from '../env/home'
import {
getSocketCacacheDirEnv,
getSocketDlxDirEnv,
getSocketHome,
} from '../env/socket'
import { getUserprofile } from '../env/windows'
import { CACHE_DIR, CACHE_TTL_DIR, DOT_SOCKET_DIR } from './dirnames'
import { normalizePath } from './normalize'
import { getPathValue } from './rewire'
import { getNodeOs } from '../node/os'
import { getNodePath } from '../node/path'
/**
* Get the OS home directory.
* Can be overridden in tests using setPath('homedir', ...) from paths/rewire.
*/
export function getOsHomeDir(): string {
// Always check for overrides - don't cache when using rewire
return getPathValue('homedir', () => getNodeOs().homedir())
}
/**
* Get the OS temporary directory.
* Can be overridden in tests using setPath('tmpdir', ...) from paths/rewire.
*/
/**
* Get the OS temporary directory.
* Can be overridden in tests using setPath('tmpdir', ...) from paths/rewire.
*/
export function getOsTmpDir(): string {
// Always check for overrides - don't cache when using rewire
return getPathValue('tmpdir', () => getNodeOs().tmpdir())
}
/**
* Get a Socket app cache directory (~/.socket/_<appName>/cache).
*/
/**
* Get a Socket app cache directory (~/.socket/_<appName>/cache).
*/
export function getSocketAppCacheDir(appName: string): string {
return normalizePath(getNodePath().join(getSocketAppDir(appName), CACHE_DIR))
}
/**
* Get a Socket app TTL cache directory (~/.socket/_<appName>/cache/ttl).
*/
/**
* Get a Socket app TTL cache directory (~/.socket/_<appName>/cache/ttl).
*/
export function getSocketAppCacheTtlDir(appName: string): string {
return normalizePath(
getNodePath().join(getSocketAppCacheDir(appName), CACHE_TTL_DIR),
)
}
/**
* Get a Socket app directory (~/.socket/_<appName>).
*/
/**
* Get a Socket app directory (~/.socket/_<appName>).
*/
export function getSocketAppDir(appName: string): string {
return normalizePath(
getNodePath().join(getSocketUserDir(), `${SOCKET_APP_PREFIX}${appName}`),
)
}
/**
* Get the Socket cacache directory (~/.socket/_cacache).
* Can be overridden with SOCKET_CACACHE_DIR environment variable or via setPath() for testing.
* Result is cached via getPathValue for performance.
*
* Priority order:
* 1. Test override via setPath('socket-cacache-dir', ...)
* 2. SOCKET_CACACHE_DIR - Full override of cacache directory
* 3. Default: $SOCKET_HOME/_cacache or $HOME/.socket/_cacache
*/
/**
* Get the Socket cacache directory (~/.socket/_cacache).
* Can be overridden with SOCKET_CACACHE_DIR environment variable or via setPath() for testing.
* Result is cached via getPathValue for performance.
*
* Priority order:
* 1. Test override via setPath('socket-cacache-dir', ...)
* 2. SOCKET_CACACHE_DIR - Full override of cacache directory
* 3. Default: $SOCKET_HOME/_cacache or $HOME/.socket/_cacache
*/
export function getSocketCacacheDir(): string {
return getPathValue('socket-cacache-dir', () => {
if (getSocketCacacheDirEnv()) {
return normalizePath(getSocketCacacheDirEnv() as string)
}
return normalizePath(
getNodePath().join(getSocketUserDir(), `${SOCKET_APP_PREFIX}cacache`),
)
})
}
/**
* Get the Socket CLI directory (~/.socket/_socket).
*/
/**
* Get the Socket CLI directory (~/.socket/_socket).
*/
export function getSocketCliDir(): string {
return getSocketAppDir(SOCKET_CLI_APP_NAME)
}
/**
* Get the Socket DLX directory (~/.socket/_dlx).
* Can be overridden with SOCKET_DLX_DIR environment variable or via setPath() for testing.
* Result is cached via getPathValue for performance.
*
* Priority order:
* 1. Test override via setPath('socket-dlx-dir', ...)
* 2. SOCKET_DLX_DIR - Full override of DLX cache directory
* 3. SOCKET_HOME/_dlx - Base directory override (inherits from getSocketUserDir)
* 4. Default: $HOME/.socket/_dlx
* 5. Fallback: /tmp/.socket/_dlx (Unix) or %TEMP%\.socket\_dlx (Windows)
*/
/**
* Get the Socket DLX directory (~/.socket/_dlx).
* Can be overridden with SOCKET_DLX_DIR environment variable or via setPath() for testing.
* Result is cached via getPathValue for performance.
*
* Priority order:
* 1. Test override via setPath('socket-dlx-dir', ...)
* 2. SOCKET_DLX_DIR - Full override of DLX cache directory
* 3. SOCKET_HOME/_dlx - Base directory override (inherits from getSocketUserDir)
* 4. Default: $HOME/.socket/_dlx
* 5. Fallback: /tmp/.socket/_dlx (Unix) or %TEMP%\.socket\_dlx (Windows)
*/
export function getSocketDlxDir(): string {
return getPathValue('socket-dlx-dir', () => {
if (getSocketDlxDirEnv()) {
return normalizePath(getSocketDlxDirEnv() as string)
}
return normalizePath(
getNodePath().join(
getSocketUserDir(),
`${SOCKET_APP_PREFIX}${SOCKET_DLX_APP_NAME}`,
),
)
})
}
/**
* Get the Socket home directory (~/.socket).
* Alias for getSocketUserDir() for consistency across Socket projects.
*/
/**
* Get the Socket home directory (~/.socket).
* Alias for getSocketUserDir() for consistency across Socket projects.
*/
export function getSocketHomePath(): string {
return getSocketUserDir()
}
/**
* Get the Socket Registry directory (~/.socket/_registry).
*/
/**
* Get the Socket Registry directory (~/.socket/_registry).
*/
export function getSocketRegistryDir(): string {
return getSocketAppDir(SOCKET_REGISTRY_APP_NAME)
}
/**
* Get the Socket Registry GitHub cache directory (~/.socket/_registry/cache/ttl/github).
*/
/**
* Get the Socket Registry GitHub cache directory (~/.socket/_registry/cache/ttl/github).
*/
export function getSocketRegistryGithubCacheDir(): string {
return normalizePath(
getNodePath().join(
getSocketAppCacheTtlDir(SOCKET_REGISTRY_APP_NAME),
CACHE_GITHUB_DIR,
),
)
}
/**
* Get the Socket user directory (~/.socket).
* Can be overridden with SOCKET_HOME environment variable or via setPath() for testing.
* Result is cached via getPathValue for performance.
*
* Priority order:
* 1. Test override via setPath('socket-user-dir', ...)
* 2. SOCKET_HOME - Base directory override
* 3. Default: $HOME/.socket
* 4. Fallback: /tmp/.socket (Unix) or %TEMP%\.socket (Windows)
*/
/**
* Get the Socket user directory (~/.socket).
* Can be overridden with SOCKET_HOME environment variable or via setPath() for testing.
* Result is cached via getPathValue for performance.
*
* Priority order:
* 1. Test override via setPath('socket-user-dir', ...)
* 2. SOCKET_HOME - Base directory override
* 3. Default: $HOME/.socket
* 4. Fallback: /tmp/.socket (Unix) or %TEMP%\.socket (Windows)
*/
export function getSocketUserDir(): string {
return getPathValue('socket-user-dir', () => {
const socketHome = getSocketHome()
if (socketHome) {
return normalizePath(socketHome)
}
return normalizePath(getNodePath().join(getUserHomeDir(), DOT_SOCKET_DIR))
})
}
/**
* Get the user's home directory.
* Uses environment variables directly to support test mocking.
* Falls back to temporary directory if home is not available.
*
* Priority order:
* 1. HOME environment variable (Unix)
* 2. USERPROFILE environment variable (Windows)
* 3. getNodeOs().homedir()
* 4. Fallback: getNodeOs().tmpdir() (rarely used, for restricted environments)
*/
/**
* Get the user's home directory.
* Uses environment variables directly to support test mocking.
* Falls back to temporary directory if home is not available.
*
* Priority order:
* 1. HOME environment variable (Unix)
* 2. USERPROFILE environment variable (Windows)
* 3. getNodeOs().homedir()
* 4. Fallback: getNodeOs().tmpdir() (rarely used, for restricted environments)
*/
export function getUserHomeDir(): string {
// Try HOME first (Unix)
const home = getHome()
if (home) {
return home
}
// Try USERPROFILE (Windows)
const userProfile = getUserprofile()
if (userProfile) {
return userProfile
}
// Try getNodeOs().homedir()
try {
const osHome = getOsHomeDir()
if (osHome) {
return osHome
}
} catch {
// getNodeOs().homedir() can throw in restricted environments
}
/* c8 ignore next 2 - Triple-fallback only fires when HOME +
USERPROFILE + os.homedir() all fail; not reachable in tests. */
return getOsTmpDir()
}