-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.ts
More file actions
228 lines (222 loc) · 6.94 KB
/
socket.ts
File metadata and controls
228 lines (222 loc) · 6.94 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
/**
* @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'
let _os: typeof import('node:os') | undefined
let _path: typeof import('node:path') | undefined
/**
* Lazily load the os module to avoid Webpack errors.
* @private
*/
/*@__NO_SIDE_EFFECTS__*/
function getOs() {
if (_os === undefined) {
_os = /*@__PURE__*/ require('node:os')
}
return _os as typeof import('node:os')
}
/**
* Lazily load the path module to avoid Webpack errors.
* @private
*/
/*@__NO_SIDE_EFFECTS__*/
function getPath() {
if (_path === undefined) {
_path = /*@__PURE__*/ require('node:path')
}
return _path as typeof import('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', () => getOs().homedir())
}
/**
* 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', () => getOs().tmpdir())
}
/**
* Get a Socket app cache directory (~/.socket/_<appName>/cache).
*/
export function getSocketAppCacheDir(appName: string): string {
return normalizePath(getPath().join(getSocketAppDir(appName), CACHE_DIR))
}
/**
* Get a Socket app TTL cache directory (~/.socket/_<appName>/cache/ttl).
*/
export function getSocketAppCacheTtlDir(appName: string): string {
return normalizePath(
getPath().join(getSocketAppCacheDir(appName), CACHE_TTL_DIR),
)
}
/**
* Get a Socket app directory (~/.socket/_<appName>).
*/
export function getSocketAppDir(appName: string): string {
return normalizePath(
getPath().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
*/
export function getSocketCacacheDir(): string {
return getPathValue('socket-cacache-dir', () => {
if (getSocketCacacheDirEnv()) {
return normalizePath(getSocketCacacheDirEnv() as string)
}
return normalizePath(
getPath().join(getSocketUserDir(), `${SOCKET_APP_PREFIX}cacache`),
)
})
}
/**
* 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)
*/
export function getSocketDlxDir(): string {
return getPathValue('socket-dlx-dir', () => {
if (getSocketDlxDirEnv()) {
return normalizePath(getSocketDlxDirEnv() as string)
}
return normalizePath(
getPath().join(
getSocketUserDir(),
`${SOCKET_APP_PREFIX}${SOCKET_DLX_APP_NAME}`,
),
)
})
}
/**
* 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).
*/
export function getSocketRegistryDir(): string {
return getSocketAppDir(SOCKET_REGISTRY_APP_NAME)
}
/**
* Get the Socket Registry GitHub cache directory (~/.socket/_registry/cache/ttl/github).
*/
export function getSocketRegistryGithubCacheDir(): string {
return normalizePath(
getPath().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)
*/
export function getSocketUserDir(): string {
return getPathValue('socket-user-dir', () => {
const socketHome = getSocketHome()
if (socketHome) {
return normalizePath(socketHome)
}
return normalizePath(getPath().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. getOs().homedir()
* 4. Fallback: getOs().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 getOs().homedir()
try {
const osHome = getOsHomeDir()
if (osHome) {
return osHome
}
} catch {
// getOs().homedir() can throw in restricted environments
}
// Final fallback to temp directory (rarely used)
return getOsTmpDir()
}