-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathplatform.ts
More file actions
148 lines (128 loc) · 3.54 KB
/
platform.ts
File metadata and controls
148 lines (128 loc) · 3.54 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
import { platform, arch } from 'node:os';
import { existsSync, readFileSync } from 'node:fs';
import { execSync } from 'node:child_process';
/**
* Supported platform identifiers
*/
export type Platform =
| 'darwin-arm64'
| 'darwin-x86_64'
| 'linux-arm64'
| 'linux-arm64-musl'
| 'linux-x86_64'
| 'linux-x86_64-musl'
| 'win32-x86_64';
/**
* Binary extension for each platform
*/
export const PLATFORM_EXTENSIONS: Record<string, string> = {
darwin: '.dylib',
linux: '.so',
win32: '.dll',
} as const;
/**
* Detects if the system uses musl libc (Alpine Linux, etc.)
* Uses multiple detection strategies for reliability
*/
export function isMusl(): boolean {
// Only relevant for Linux
if (platform() !== 'linux') {
return false;
}
// Strategy 1: Check for musl-specific files
const muslFiles = [
'/lib/ld-musl-x86_64.so.1',
'/lib/ld-musl-aarch64.so.1',
'/lib/ld-musl-armhf.so.1',
];
for (const file of muslFiles) {
if (existsSync(file)) {
return true;
}
}
// Strategy 2: Check ldd version output
try {
const lddVersion = execSync('ldd --version 2>&1', {
encoding: 'utf-8',
stdio: ['pipe', 'pipe', 'pipe'],
});
if (lddVersion.includes('musl')) {
return true;
}
} catch {
// ldd command failed, continue to next strategy
}
// Strategy 3: Check /etc/os-release for Alpine
try {
if (existsSync('/etc/os-release')) {
const osRelease = readFileSync('/etc/os-release', 'utf-8');
if (osRelease.includes('Alpine') || osRelease.includes('musl')) {
return true;
}
}
} catch {
// File read failed, continue to next strategy
}
// Strategy 4: Check process.report.getReport() for musl
try {
const report = (process as any).report?.getReport?.();
if (report?.header?.glibcVersionRuntime === '') {
// Empty glibc version often indicates musl
return true;
}
} catch {
// Report not available
}
return false;
}
/**
* Gets the current platform identifier
* @throws {Error} If the platform is unsupported
*/
export function getCurrentPlatform(): Platform {
const platformName = platform();
const archName = arch();
// macOS
if (platformName === 'darwin') {
if (archName === 'arm64') return 'darwin-arm64';
if (archName === 'x64' || archName === 'ia32') return 'darwin-x86_64';
}
// Linux (with musl detection)
if (platformName === 'linux') {
const muslSuffix = isMusl() ? '-musl' : '';
if (archName === 'arm64') {
return `linux-arm64${muslSuffix}` as Platform;
}
if (archName === 'x64' || archName === 'ia32') {
return `linux-x86_64${muslSuffix}` as Platform;
}
}
// Windows
if (platformName === 'win32') {
if (archName === 'x64' || archName === 'ia32') return 'win32-x86_64';
}
// Unsupported platform
throw new Error(
`Unsupported platform: ${platformName}-${archName}. ` +
`Supported platforms: darwin-arm64, darwin-x86_64, linux-arm64, linux-x86_64, win32-x86_64 ` +
`(with glibc or musl support for Linux)`
);
}
/**
* Gets the package name for the current platform
*/
export function getPlatformPackageName(): string {
const currentPlatform = getCurrentPlatform();
return `@sqliteai/sqlite-sync-${currentPlatform}`;
}
/**
* Gets the binary filename for the current platform
*/
export function getBinaryName(): string {
const platformName = platform();
const extension = PLATFORM_EXTENSIONS[platformName];
if (!extension) {
throw new Error(`Unknown platform: ${platformName}`);
}
return `cloudsync${extension}`;
}