Skip to content

Commit 0acd016

Browse files
committed
feat: export helper functions from releases modules
1 parent b4f9753 commit 0acd016

2 files changed

Lines changed: 78 additions & 37 deletions

File tree

src/constants/platform.ts

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,64 @@
1+
/** @fileoverview Platform detection and OS-specific constants. */
2+
3+
let _os: typeof import('node:os') | undefined
4+
/**
5+
* Lazily load the os module to avoid Webpack errors.
6+
* Uses non-'node:' prefixed require to prevent Webpack bundling issues.
7+
*
8+
* @private
9+
*/
10+
/*@__NO_SIDE_EFFECTS__*/
11+
function getOs() {
12+
if (_os === undefined) {
13+
// Use non-'node:' prefixed require to avoid Webpack errors.
14+
15+
_os = /*@__PURE__*/ require('os')
16+
}
17+
return _os as typeof import('node:os')
18+
}
19+
20+
/**
21+
* CPU architecture type.
22+
*/
23+
export type Arch = NodeJS.Architecture
24+
25+
/**
26+
* Linux libc variant.
27+
*/
28+
export type Libc = 'glibc' | 'musl'
29+
130
/**
2-
* Platform detection and OS-specific constants.
31+
* Operating system platform type.
332
*/
33+
export type Platform = NodeJS.Platform
34+
35+
let _arch: Arch | undefined
436

5-
import { platform } from 'os'
37+
/**
38+
* Get the current CPU architecture (memoized).
39+
*/
40+
export function getArch(): Arch {
41+
if (_arch === undefined) {
42+
_arch = getOs().arch()
43+
}
44+
return _arch
45+
}
46+
47+
let _platform: Platform | undefined
48+
49+
/**
50+
* Get the current platform (memoized).
51+
*/
52+
export function getPlatform(): Platform {
53+
if (_platform === undefined) {
54+
_platform = getOs().platform()
55+
}
56+
return _platform
57+
}
658

7-
// Platform detection.
8-
const _platform = platform()
9-
export const DARWIN = _platform === 'darwin'
10-
export const WIN32 = _platform === 'win32'
59+
// Platform detection (memoized at module load).
60+
export const DARWIN = getPlatform() === 'darwin'
61+
export const WIN32 = getPlatform() === 'win32'
1162

1263
// File permission modes.
1364
export const S_IXUSR = 0o100

src/releases/socket-btm.ts

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,21 @@
33
*/
44

55
import { existsSync } from 'fs'
6-
import os from 'os'
76

7+
import {
8+
type Arch,
9+
getArch,
10+
type Libc,
11+
getPlatform,
12+
type Platform,
13+
} from '../constants/platform.js'
814
import {
915
downloadGitHubRelease,
1016
type DownloadGitHubReleaseConfig,
1117
SOCKET_BTM_REPO,
1218
} from './github.js'
1319

14-
/**
15-
* Platform type for socket-btm binaries.
16-
*/
17-
export type Platform = 'darwin' | 'linux' | 'win32'
18-
19-
/**
20-
* Architecture type for socket-btm binaries.
21-
*/
22-
export type Arch = 'arm64' | 'x64'
23-
24-
/**
25-
* Linux libc variant.
26-
*/
27-
export type Libc = 'musl' | 'glibc'
20+
export type { Arch, Libc, Platform }
2821

2922
/**
3023
* Configuration for downloading socket-btm binary releases.
@@ -103,8 +96,7 @@ const ARCH_MAP: Record<string, string> = {
10396
* @returns 'musl', 'glibc', or undefined (for non-Linux)
10497
*/
10598
export function detectLibc(): Libc | undefined {
106-
const platform = os.platform()
107-
if (platform !== 'linux') {
99+
if (getPlatform() !== 'linux') {
108100
return undefined
109101
}
110102

@@ -181,26 +173,24 @@ export async function downloadSocketBtmRelease(
181173
// Binary download
182174
const {
183175
bin,
184-
libc,
176+
libc = detectLibc(),
185177
removeMacOSQuarantine = true,
186-
targetArch,
187-
targetPlatform,
178+
targetArch = getArch(),
179+
targetPlatform = getPlatform(),
188180
} = config as SocketBtmBinaryConfig
189181

190182
// Default bin to tool if not provided (like brew/cargo)
191183
const baseName = bin || tool
192184

193-
// Resolve platform and arch based on host if not specified
194-
const platform = (targetPlatform || os.platform()) as Platform
195-
const arch = (targetArch || os.arch()) as Arch
196-
197-
// Auto-detect libc variant on Linux if not specified
198-
const libcType = libc || detectLibc()
199-
200185
// Build asset name and platform-arch identifier
201-
const assetName = getBinaryAssetName(baseName, platform, arch, libcType)
202-
const platformArch = getPlatformArch(platform, arch, libcType)
203-
const binaryName = getBinaryName(baseName, platform)
186+
const assetName = getBinaryAssetName(
187+
baseName,
188+
targetPlatform,
189+
targetArch,
190+
libc,
191+
)
192+
const platformArch = getPlatformArch(targetPlatform, targetArch, libc)
193+
const binaryName = getBinaryName(baseName, targetPlatform)
204194

205195
downloadConfig = {
206196
owner: SOCKET_BTM_REPO.owner,
@@ -282,7 +272,7 @@ export function getBinaryName(
282272
export function getPlatformArch(
283273
platform: Platform,
284274
arch: Arch,
285-
libc?: Libc,
275+
libc?: Libc | undefined,
286276
): string {
287277
const mappedArch = ARCH_MAP[arch]
288278
if (!mappedArch) {

0 commit comments

Comments
 (0)