nport 2.1.0 → 2.1.1
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.
- package/CHANGELOG.md +17 -0
- package/dist/analytics.d.ts +59 -0
- package/dist/analytics.js +193 -0
- package/dist/analytics.js.map +1 -0
- package/dist/api.d.ts +20 -0
- package/dist/api.js +85 -0
- package/dist/api.js.map +1 -0
- package/dist/args.d.ts +44 -0
- package/dist/args.js +127 -0
- package/dist/args.js.map +1 -0
- package/dist/bin-manager.d.ts +1 -0
- package/dist/bin-manager.js +209 -0
- package/dist/bin-manager.js.map +1 -0
- package/dist/binary.d.ts +42 -0
- package/dist/binary.js +119 -0
- package/dist/binary.js.map +1 -0
- package/dist/config-manager.d.ts +54 -0
- package/dist/config-manager.js +129 -0
- package/dist/config-manager.js.map +1 -0
- package/dist/config.d.ts +25 -0
- package/dist/config.js +59 -0
- package/dist/config.js.map +1 -0
- package/dist/constants.d.ts +61 -0
- package/dist/constants.js +86 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +92 -0
- package/dist/index.js.map +1 -0
- package/dist/lang.d.ts +38 -0
- package/dist/lang.js +217 -0
- package/dist/lang.js.map +1 -0
- package/dist/state.d.ts +82 -0
- package/dist/state.js +139 -0
- package/dist/state.js.map +1 -0
- package/dist/tunnel.d.ts +16 -0
- package/dist/tunnel.js +101 -0
- package/dist/tunnel.js.map +1 -0
- package/dist/types/analytics.d.ts +91 -0
- package/dist/types/analytics.js +8 -0
- package/dist/types/analytics.js.map +1 -0
- package/dist/types/config.d.ts +89 -0
- package/dist/types/config.js +8 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/i18n.d.ts +75 -0
- package/dist/types/i18n.js +5 -0
- package/dist/types/i18n.js.map +1 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/index.js +7 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/tunnel.d.ts +74 -0
- package/dist/types/tunnel.js +8 -0
- package/dist/types/tunnel.js.map +1 -0
- package/dist/types/version.d.ts +25 -0
- package/dist/types/version.js +5 -0
- package/dist/types/version.js.map +1 -0
- package/dist/ui.d.ts +54 -0
- package/dist/ui.js +120 -0
- package/dist/ui.js.map +1 -0
- package/dist/version.d.ts +16 -0
- package/dist/version.js +49 -0
- package/dist/version.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import https from 'https';
|
|
4
|
+
import os from 'os';
|
|
5
|
+
import { execSync } from 'child_process';
|
|
6
|
+
import { fileURLToPath } from 'url';
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = path.dirname(path.dirname(__filename));
|
|
9
|
+
const BIN_DIR = path.join(__dirname, 'bin');
|
|
10
|
+
const BINARY_NAME = 'cloudflared';
|
|
11
|
+
const COMPRESSED_SUFFIX = '.tgz';
|
|
12
|
+
const TEMP_ARCHIVE_NAME = 'cloudflared.tgz';
|
|
13
|
+
const PLATFORM = os.platform();
|
|
14
|
+
const ARCH = os.arch();
|
|
15
|
+
const IS_WINDOWS = PLATFORM === 'win32';
|
|
16
|
+
const BIN_NAME = IS_WINDOWS ? `${BINARY_NAME}.exe` : BINARY_NAME;
|
|
17
|
+
const BIN_PATH = path.join(BIN_DIR, BIN_NAME);
|
|
18
|
+
const GITHUB_BASE_URL = 'https://github.com/cloudflare/cloudflared/releases/latest/download';
|
|
19
|
+
const REDIRECT_CODES = [301, 302];
|
|
20
|
+
const SUCCESS_CODE = 200;
|
|
21
|
+
const UNIX_EXECUTABLE_MODE = '755';
|
|
22
|
+
const PLATFORM_MAPPINGS = {
|
|
23
|
+
darwin: {
|
|
24
|
+
x64: 'cloudflared-darwin-amd64.tgz',
|
|
25
|
+
arm64: 'cloudflared-darwin-arm64.tgz',
|
|
26
|
+
},
|
|
27
|
+
win32: {
|
|
28
|
+
x64: 'cloudflared-windows-amd64.exe',
|
|
29
|
+
ia32: 'cloudflared-windows-386.exe',
|
|
30
|
+
},
|
|
31
|
+
linux: {
|
|
32
|
+
x64: 'cloudflared-linux-amd64',
|
|
33
|
+
arm64: 'cloudflared-linux-arm64',
|
|
34
|
+
arm: 'cloudflared-linux-arm',
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
function normalizeArch(arch) {
|
|
38
|
+
const archMap = {
|
|
39
|
+
x64: 'x64',
|
|
40
|
+
amd64: 'amd64',
|
|
41
|
+
arm64: 'arm64',
|
|
42
|
+
ia32: 'ia32',
|
|
43
|
+
arm: 'arm',
|
|
44
|
+
};
|
|
45
|
+
return archMap[arch] || arch;
|
|
46
|
+
}
|
|
47
|
+
function getDownloadUrl() {
|
|
48
|
+
const normalizedArch = normalizeArch(ARCH);
|
|
49
|
+
const platformMapping = PLATFORM_MAPPINGS[PLATFORM];
|
|
50
|
+
if (!platformMapping) {
|
|
51
|
+
throw new Error(`Unsupported platform: ${PLATFORM}. Supported platforms: darwin, win32, linux`);
|
|
52
|
+
}
|
|
53
|
+
const binaryName = platformMapping[normalizedArch];
|
|
54
|
+
if (!binaryName) {
|
|
55
|
+
throw new Error(`Unsupported architecture: ${ARCH} for platform ${PLATFORM}. ` +
|
|
56
|
+
`Supported architectures: ${Object.keys(platformMapping).join(', ')}`);
|
|
57
|
+
}
|
|
58
|
+
return `${GITHUB_BASE_URL}/${binaryName}`;
|
|
59
|
+
}
|
|
60
|
+
function isCompressedArchive(url) {
|
|
61
|
+
return url.endsWith(COMPRESSED_SUFFIX);
|
|
62
|
+
}
|
|
63
|
+
function ensureDirectory(dirPath) {
|
|
64
|
+
if (!fs.existsSync(dirPath)) {
|
|
65
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function safeUnlink(filePath) {
|
|
69
|
+
try {
|
|
70
|
+
if (fs.existsSync(filePath)) {
|
|
71
|
+
fs.unlinkSync(filePath);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
// Ignore
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function setExecutablePermissions(filePath, mode = UNIX_EXECUTABLE_MODE) {
|
|
79
|
+
if (!IS_WINDOWS) {
|
|
80
|
+
fs.chmodSync(filePath, mode);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function validateFileExists(filePath, errorMessage) {
|
|
84
|
+
if (!fs.existsSync(filePath)) {
|
|
85
|
+
throw new Error(errorMessage || `File not found: ${filePath}`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async function downloadFile(url, dest) {
|
|
89
|
+
return new Promise((resolve, reject) => {
|
|
90
|
+
const file = fs.createWriteStream(dest);
|
|
91
|
+
https
|
|
92
|
+
.get(url, (response) => {
|
|
93
|
+
if (REDIRECT_CODES.includes(response.statusCode)) {
|
|
94
|
+
file.close();
|
|
95
|
+
safeUnlink(dest);
|
|
96
|
+
downloadFile(response.headers.location, dest)
|
|
97
|
+
.then(resolve)
|
|
98
|
+
.catch(reject);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (response.statusCode !== SUCCESS_CODE) {
|
|
102
|
+
file.close();
|
|
103
|
+
safeUnlink(dest);
|
|
104
|
+
reject(new Error(`Download failed with status code ${response.statusCode} from ${url}`));
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
response.pipe(file);
|
|
108
|
+
file.on('finish', () => {
|
|
109
|
+
file.close(() => resolve(dest));
|
|
110
|
+
});
|
|
111
|
+
file.on('error', (err) => {
|
|
112
|
+
file.close();
|
|
113
|
+
safeUnlink(dest);
|
|
114
|
+
reject(err);
|
|
115
|
+
});
|
|
116
|
+
})
|
|
117
|
+
.on('error', (err) => {
|
|
118
|
+
file.close();
|
|
119
|
+
safeUnlink(dest);
|
|
120
|
+
reject(new Error(`Network error: ${err.message}`));
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
function extractTarGz(archivePath, targetDir) {
|
|
125
|
+
try {
|
|
126
|
+
execSync(`tar -xzf "${archivePath}" -C "${targetDir}"`, {
|
|
127
|
+
stdio: 'pipe',
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
catch (err) {
|
|
131
|
+
throw new Error(`Extraction failed: ${err.message}`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const logger = {
|
|
135
|
+
info: (msg) => console.log(`ℹ️ ${msg}`),
|
|
136
|
+
success: (msg) => console.log(`✅ ${msg}`),
|
|
137
|
+
warn: (msg) => console.warn(`⚠️ ${msg}`),
|
|
138
|
+
error: (msg) => console.error(`❌ ${msg}`),
|
|
139
|
+
progress: (msg) => console.log(`🚧 ${msg}`),
|
|
140
|
+
extract: (msg) => console.log(`📦 ${msg}`),
|
|
141
|
+
};
|
|
142
|
+
async function installBinary() {
|
|
143
|
+
logger.progress('Cloudflared binary not found. Downloading... (This happens only once)');
|
|
144
|
+
const url = getDownloadUrl();
|
|
145
|
+
const isArchive = isCompressedArchive(url);
|
|
146
|
+
const downloadDest = isArchive
|
|
147
|
+
? path.join(BIN_DIR, TEMP_ARCHIVE_NAME)
|
|
148
|
+
: BIN_PATH;
|
|
149
|
+
try {
|
|
150
|
+
await downloadFile(url, downloadDest);
|
|
151
|
+
if (isArchive) {
|
|
152
|
+
logger.extract('Extracting binary...');
|
|
153
|
+
extractTarGz(downloadDest, BIN_DIR);
|
|
154
|
+
safeUnlink(downloadDest);
|
|
155
|
+
validateFileExists(BIN_PATH, 'Extraction failed: Binary not found after extraction');
|
|
156
|
+
}
|
|
157
|
+
setExecutablePermissions(BIN_PATH);
|
|
158
|
+
logger.success('Download complete.');
|
|
159
|
+
return BIN_PATH;
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
safeUnlink(downloadDest);
|
|
163
|
+
safeUnlink(BIN_PATH);
|
|
164
|
+
throw error;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
export async function ensureCloudflared() {
|
|
168
|
+
ensureDirectory(BIN_DIR);
|
|
169
|
+
if (fs.existsSync(BIN_PATH)) {
|
|
170
|
+
setExecutablePermissions(BIN_PATH);
|
|
171
|
+
return BIN_PATH;
|
|
172
|
+
}
|
|
173
|
+
try {
|
|
174
|
+
return await installBinary();
|
|
175
|
+
}
|
|
176
|
+
catch (error) {
|
|
177
|
+
logger.error(`Installation failed: ${error.message}`);
|
|
178
|
+
process.exit(1);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
function isCI() {
|
|
182
|
+
return !!(process.env.CI ||
|
|
183
|
+
process.env.GITHUB_ACTIONS ||
|
|
184
|
+
process.env.GITLAB_CI ||
|
|
185
|
+
process.env.CIRCLECI ||
|
|
186
|
+
process.env.TRAVIS ||
|
|
187
|
+
process.env.JENKINS_URL ||
|
|
188
|
+
process.env.BUILDKITE);
|
|
189
|
+
}
|
|
190
|
+
async function main() {
|
|
191
|
+
if (isCI()) {
|
|
192
|
+
logger.info('Running in CI environment - skipping binary download');
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
try {
|
|
196
|
+
const binaryPath = await ensureCloudflared();
|
|
197
|
+
logger.success(`Cloudflared binary is ready at: ${binaryPath}`);
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
logger.error(error.message);
|
|
201
|
+
process.exit(1);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
// Run if executed directly
|
|
205
|
+
const currentFilePath = fileURLToPath(import.meta.url);
|
|
206
|
+
if (process.argv[1] === currentFilePath || process.argv[1]?.endsWith('bin-manager.js')) {
|
|
207
|
+
main();
|
|
208
|
+
}
|
|
209
|
+
//# sourceMappingURL=bin-manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bin-manager.js","sourceRoot":"","sources":["../src/bin-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM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AAC,OAAO,CAAC,mCAAmC,UAAU,EAAE,CAAC,CAAC;IAClE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,CAAC,KAAK,CAAE,KAAe,CAAC,OAAO,CAAC,CAAC;QACvC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;AACH,CAAC;AAED,2BAA2B;AAC3B,MAAM,eAAe,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACvD,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,gBAAgB,CAAC,EAAE,CAAC;IACvF,IAAI,EAAE,CAAC;AACT,CAAC"}
|
package/dist/binary.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type ChildProcess } from 'child_process';
|
|
2
|
+
/**
|
|
3
|
+
* Binary Manager
|
|
4
|
+
*
|
|
5
|
+
* Handles all operations related to the cloudflared binary.
|
|
6
|
+
*/
|
|
7
|
+
export declare class BinaryManager {
|
|
8
|
+
/**
|
|
9
|
+
* Validates that the cloudflared binary exists.
|
|
10
|
+
*/
|
|
11
|
+
static validate(binaryPath: string): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Spawns the cloudflared tunnel process.
|
|
14
|
+
*/
|
|
15
|
+
static spawn(binaryPath: string, token: string, port: number): ChildProcess;
|
|
16
|
+
/**
|
|
17
|
+
* Attaches event handlers to the cloudflared process.
|
|
18
|
+
*/
|
|
19
|
+
static attachHandlers(childProcess: ChildProcess, spinner?: {
|
|
20
|
+
fail: (msg: string) => void;
|
|
21
|
+
} | null): void;
|
|
22
|
+
/**
|
|
23
|
+
* Processes stderr output from cloudflared.
|
|
24
|
+
*/
|
|
25
|
+
private static handleStderr;
|
|
26
|
+
/**
|
|
27
|
+
* Handles network warnings.
|
|
28
|
+
*/
|
|
29
|
+
private static handleNetworkWarning;
|
|
30
|
+
/**
|
|
31
|
+
* Displays network connectivity warning.
|
|
32
|
+
*/
|
|
33
|
+
private static displayNetworkWarning;
|
|
34
|
+
/**
|
|
35
|
+
* Handles spawn errors.
|
|
36
|
+
*/
|
|
37
|
+
private static handleError;
|
|
38
|
+
/**
|
|
39
|
+
* Handles process exit.
|
|
40
|
+
*/
|
|
41
|
+
private static handleClose;
|
|
42
|
+
}
|
package/dist/binary.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { spawn } from 'child_process';
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import { LOG_PATTERNS, NETWORK_CONFIG } from './config.js';
|
|
5
|
+
import { state } from './state.js';
|
|
6
|
+
import { UI } from './ui.js';
|
|
7
|
+
import { lang } from './lang.js';
|
|
8
|
+
/**
|
|
9
|
+
* Binary Manager
|
|
10
|
+
*
|
|
11
|
+
* Handles all operations related to the cloudflared binary.
|
|
12
|
+
*/
|
|
13
|
+
export class BinaryManager {
|
|
14
|
+
/**
|
|
15
|
+
* Validates that the cloudflared binary exists.
|
|
16
|
+
*/
|
|
17
|
+
static validate(binaryPath) {
|
|
18
|
+
if (fs.existsSync(binaryPath)) {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
console.error(chalk.red(`\n❌ Error: Cloudflared binary not found at: ${binaryPath}`));
|
|
22
|
+
console.error(chalk.yellow("👉 Please run 'npm install' again to download the binary.\n"));
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Spawns the cloudflared tunnel process.
|
|
27
|
+
*/
|
|
28
|
+
static spawn(binaryPath, token, port) {
|
|
29
|
+
return spawn(binaryPath, [
|
|
30
|
+
'tunnel',
|
|
31
|
+
'run',
|
|
32
|
+
'--token',
|
|
33
|
+
token,
|
|
34
|
+
'--url',
|
|
35
|
+
`http://localhost:${port}`,
|
|
36
|
+
]);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Attaches event handlers to the cloudflared process.
|
|
40
|
+
*/
|
|
41
|
+
static attachHandlers(childProcess, spinner = null) {
|
|
42
|
+
childProcess.stderr?.on('data', (chunk) => this.handleStderr(chunk));
|
|
43
|
+
childProcess.on('error', (err) => this.handleError(err, spinner));
|
|
44
|
+
childProcess.on('close', (code) => this.handleClose(code));
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Processes stderr output from cloudflared.
|
|
48
|
+
*/
|
|
49
|
+
static handleStderr(chunk) {
|
|
50
|
+
const msg = chunk.toString();
|
|
51
|
+
if (LOG_PATTERNS.IGNORE.some((pattern) => msg.includes(pattern))) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (LOG_PATTERNS.NETWORK_WARNING.some((pattern) => msg.includes(pattern))) {
|
|
55
|
+
this.handleNetworkWarning();
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (LOG_PATTERNS.SUCCESS.some((pattern) => msg.includes(pattern))) {
|
|
59
|
+
const count = state.incrementConnection();
|
|
60
|
+
if (count === 1) {
|
|
61
|
+
state.resetNetworkIssues();
|
|
62
|
+
console.log(chalk.green(lang.t('connection1')));
|
|
63
|
+
}
|
|
64
|
+
else if (count === 4) {
|
|
65
|
+
console.log(chalk.green(lang.t('connection2')));
|
|
66
|
+
UI.displayFooter(state.updateInfo);
|
|
67
|
+
}
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (LOG_PATTERNS.ERROR.some((pattern) => msg.includes(pattern))) {
|
|
71
|
+
console.error(chalk.red(`[Cloudflared] ${msg.trim()}`));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Handles network warnings.
|
|
76
|
+
*/
|
|
77
|
+
static handleNetworkWarning() {
|
|
78
|
+
state.incrementNetworkIssue();
|
|
79
|
+
if (state.shouldShowNetworkWarning(NETWORK_CONFIG.WARNING_THRESHOLD, NETWORK_CONFIG.WARNING_COOLDOWN)) {
|
|
80
|
+
this.displayNetworkWarning();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Displays network connectivity warning.
|
|
85
|
+
*/
|
|
86
|
+
static displayNetworkWarning() {
|
|
87
|
+
console.log(chalk.yellow(lang.t('networkIssueTitle')));
|
|
88
|
+
console.log(chalk.gray(lang.t('networkIssueDesc')));
|
|
89
|
+
console.log(chalk.cyan(lang.t('networkIssueTunnel')));
|
|
90
|
+
console.log(chalk.yellow(lang.t('networkIssueReasons')));
|
|
91
|
+
console.log(chalk.gray(lang.t('networkIssueReason1')));
|
|
92
|
+
console.log(chalk.gray(lang.t('networkIssueReason2')));
|
|
93
|
+
console.log(chalk.gray(lang.t('networkIssueReason3')));
|
|
94
|
+
console.log(chalk.yellow(lang.t('networkIssueFix')));
|
|
95
|
+
console.log(chalk.gray(lang.t('networkIssueFix1')));
|
|
96
|
+
console.log(chalk.gray(lang.t('networkIssueFix2')));
|
|
97
|
+
console.log(chalk.gray(lang.t('networkIssueFix3')));
|
|
98
|
+
console.log(chalk.gray(lang.t('networkIssueFix4')));
|
|
99
|
+
console.log(chalk.blue(lang.t('networkIssueIgnore')));
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Handles spawn errors.
|
|
103
|
+
*/
|
|
104
|
+
static handleError(err, spinner) {
|
|
105
|
+
if (spinner) {
|
|
106
|
+
spinner.fail('Failed to spawn cloudflared process.');
|
|
107
|
+
}
|
|
108
|
+
console.error(chalk.red(`Process Error: ${err.message}`));
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Handles process exit.
|
|
112
|
+
*/
|
|
113
|
+
static handleClose(code) {
|
|
114
|
+
if (code !== 0 && code !== null) {
|
|
115
|
+
console.log(chalk.red(`Tunnel process exited with code ${code}`));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=binary.js.map
|
|
@@ -0,0 +1 @@
|
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AAI,KAAK,IAAI,EAAE,CAAC;YAChC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,mCAAmC,IAAI,EAAE,CAAC,CAAC,CAAC;QACpE,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { UserConfig } from './types/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Configuration Manager
|
|
4
|
+
*
|
|
5
|
+
* Handles persistent storage of user preferences in ~/.nport/config.json.
|
|
6
|
+
*/
|
|
7
|
+
declare class ConfigManager {
|
|
8
|
+
private configDir;
|
|
9
|
+
private configFile;
|
|
10
|
+
private oldLangFile;
|
|
11
|
+
private config;
|
|
12
|
+
constructor();
|
|
13
|
+
/**
|
|
14
|
+
* Loads configuration from the JSON file.
|
|
15
|
+
*/
|
|
16
|
+
private loadConfig;
|
|
17
|
+
/**
|
|
18
|
+
* Migrates old configuration files to the new unified format.
|
|
19
|
+
*/
|
|
20
|
+
private migrateOldConfig;
|
|
21
|
+
/**
|
|
22
|
+
* Saves the current configuration to disk.
|
|
23
|
+
*/
|
|
24
|
+
private saveConfig;
|
|
25
|
+
/**
|
|
26
|
+
* Gets the saved backend URL.
|
|
27
|
+
*/
|
|
28
|
+
getBackendUrl(): string | null;
|
|
29
|
+
/**
|
|
30
|
+
* Sets or clears the backend URL.
|
|
31
|
+
*/
|
|
32
|
+
setBackendUrl(url: string | null): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Gets the saved language preference.
|
|
35
|
+
*/
|
|
36
|
+
getLanguage(): string | null;
|
|
37
|
+
/**
|
|
38
|
+
* Sets or clears the language preference.
|
|
39
|
+
*/
|
|
40
|
+
setLanguage(lang: string | null): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Gets a copy of all configuration.
|
|
43
|
+
*/
|
|
44
|
+
getAll(): UserConfig;
|
|
45
|
+
/**
|
|
46
|
+
* Clears all configuration.
|
|
47
|
+
*/
|
|
48
|
+
clear(): boolean;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Singleton instance
|
|
52
|
+
*/
|
|
53
|
+
export declare const configManager: ConfigManager;
|
|
54
|
+
export {};
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import os from 'os';
|
|
4
|
+
/**
|
|
5
|
+
* Configuration Manager
|
|
6
|
+
*
|
|
7
|
+
* Handles persistent storage of user preferences in ~/.nport/config.json.
|
|
8
|
+
*/
|
|
9
|
+
class ConfigManager {
|
|
10
|
+
configDir;
|
|
11
|
+
configFile;
|
|
12
|
+
oldLangFile;
|
|
13
|
+
config;
|
|
14
|
+
constructor() {
|
|
15
|
+
this.configDir = path.join(os.homedir(), '.nport');
|
|
16
|
+
this.configFile = path.join(this.configDir, 'config.json');
|
|
17
|
+
this.oldLangFile = path.join(this.configDir, 'lang');
|
|
18
|
+
this.config = this.loadConfig();
|
|
19
|
+
this.migrateOldConfig();
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Loads configuration from the JSON file.
|
|
23
|
+
*/
|
|
24
|
+
loadConfig() {
|
|
25
|
+
try {
|
|
26
|
+
if (fs.existsSync(this.configFile)) {
|
|
27
|
+
const data = fs.readFileSync(this.configFile, 'utf8');
|
|
28
|
+
return JSON.parse(data);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
console.warn('Warning: Could not load config file, using defaults');
|
|
33
|
+
}
|
|
34
|
+
return {};
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Migrates old configuration files to the new unified format.
|
|
38
|
+
*/
|
|
39
|
+
migrateOldConfig() {
|
|
40
|
+
try {
|
|
41
|
+
if (!this.config.language && fs.existsSync(this.oldLangFile)) {
|
|
42
|
+
const oldLang = fs.readFileSync(this.oldLangFile, 'utf8').trim();
|
|
43
|
+
if (oldLang && ['en', 'vi'].includes(oldLang)) {
|
|
44
|
+
this.config.language = oldLang;
|
|
45
|
+
this.saveConfig();
|
|
46
|
+
try {
|
|
47
|
+
fs.unlinkSync(this.oldLangFile);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
// Ignore if can't delete
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
// Ignore migration errors
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Saves the current configuration to disk.
|
|
61
|
+
*/
|
|
62
|
+
saveConfig() {
|
|
63
|
+
try {
|
|
64
|
+
if (!fs.existsSync(this.configDir)) {
|
|
65
|
+
fs.mkdirSync(this.configDir, { recursive: true });
|
|
66
|
+
}
|
|
67
|
+
fs.writeFileSync(this.configFile, JSON.stringify(this.config, null, 2), 'utf8');
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
console.warn('Warning: Could not save configuration');
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Gets the saved backend URL.
|
|
77
|
+
*/
|
|
78
|
+
getBackendUrl() {
|
|
79
|
+
return this.config.backendUrl ?? null;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Sets or clears the backend URL.
|
|
83
|
+
*/
|
|
84
|
+
setBackendUrl(url) {
|
|
85
|
+
if (!url) {
|
|
86
|
+
delete this.config.backendUrl;
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
this.config.backendUrl = url;
|
|
90
|
+
}
|
|
91
|
+
return this.saveConfig();
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Gets the saved language preference.
|
|
95
|
+
*/
|
|
96
|
+
getLanguage() {
|
|
97
|
+
return this.config.language ?? null;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Sets or clears the language preference.
|
|
101
|
+
*/
|
|
102
|
+
setLanguage(lang) {
|
|
103
|
+
if (!lang) {
|
|
104
|
+
delete this.config.language;
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
this.config.language = lang;
|
|
108
|
+
}
|
|
109
|
+
return this.saveConfig();
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Gets a copy of all configuration.
|
|
113
|
+
*/
|
|
114
|
+
getAll() {
|
|
115
|
+
return { ...this.config };
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Clears all configuration.
|
|
119
|
+
*/
|
|
120
|
+
clear() {
|
|
121
|
+
this.config = {};
|
|
122
|
+
return this.saveConfig();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Singleton instance
|
|
127
|
+
*/
|
|
128
|
+
export const configManager = new ConfigManager();
|
|
129
|
+
//# sourceMappingURL=config-manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config-manager.js","sourceRoot":"","sources":["../src/config-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,MAAM,IAAI,CAAC;AAGpB;;;;GAIG;AACH,MAAM,aAAa;IACT,SAAS,CAAS;IAClB,UAAU,CAAS;IACnB,WAAW,CAAS;IACpB,MAAM,CAAa;IAE3B;QACE,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,EAAE,QAAQ,CAAC,CAAC;QACnD,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;QAC3D,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;QACrD,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,UAAU,EAAE,CAAC;QAChC,IAAI,CAAC,gBAAgB,EAAE,CAAC;IA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IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;QACjB,OAAO,IAAI,CAAC,UAAU,EAAE,CAAC;IAC3B,CAAC;CACF;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,aAAa,EAAE,CAAC"}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { AppConfig, PlatformConfig, PathsConfig, LogPatterns, NetworkConfig } from './types/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Application configuration constants
|
|
4
|
+
*/
|
|
5
|
+
export declare const CONFIG: AppConfig;
|
|
6
|
+
/**
|
|
7
|
+
* Platform-specific configuration
|
|
8
|
+
*/
|
|
9
|
+
export declare const PLATFORM: PlatformConfig;
|
|
10
|
+
/**
|
|
11
|
+
* File system paths
|
|
12
|
+
*/
|
|
13
|
+
export declare const PATHS: PathsConfig;
|
|
14
|
+
/**
|
|
15
|
+
* Log patterns for filtering cloudflared output
|
|
16
|
+
*/
|
|
17
|
+
export declare const LOG_PATTERNS: LogPatterns;
|
|
18
|
+
/**
|
|
19
|
+
* Network warning configuration
|
|
20
|
+
*/
|
|
21
|
+
export declare const NETWORK_CONFIG: NetworkConfig;
|
|
22
|
+
/**
|
|
23
|
+
* Tunnel timeout in milliseconds
|
|
24
|
+
*/
|
|
25
|
+
export declare const TUNNEL_TIMEOUT_MS: number;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { fileURLToPath } from 'url';
|
|
3
|
+
import { createRequire } from 'module';
|
|
4
|
+
import { DEFAULT_BACKEND_URL, DEFAULT_PORT, SUBDOMAIN_PREFIX, TUNNEL_TIMEOUT_HOURS, UPDATE_CHECK_TIMEOUT, LOG_PATTERNS as SHARED_LOG_PATTERNS, NETWORK_WARNING, } from './constants.js';
|
|
5
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = path.dirname(path.dirname(__filename));
|
|
7
|
+
const require = createRequire(import.meta.url);
|
|
8
|
+
const packageJson = require('../package.json');
|
|
9
|
+
/**
|
|
10
|
+
* Gets the backend URL based on priority order.
|
|
11
|
+
*/
|
|
12
|
+
function getBackendUrl() {
|
|
13
|
+
if (process.env.NPORT_BACKEND_URL) {
|
|
14
|
+
return process.env.NPORT_BACKEND_URL;
|
|
15
|
+
}
|
|
16
|
+
return DEFAULT_BACKEND_URL;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Application configuration constants
|
|
20
|
+
*/
|
|
21
|
+
export const CONFIG = {
|
|
22
|
+
PACKAGE_NAME: packageJson.name,
|
|
23
|
+
CURRENT_VERSION: packageJson.version,
|
|
24
|
+
BACKEND_URL: getBackendUrl(),
|
|
25
|
+
DEFAULT_PORT,
|
|
26
|
+
SUBDOMAIN_PREFIX,
|
|
27
|
+
TUNNEL_TIMEOUT_HOURS,
|
|
28
|
+
UPDATE_CHECK_TIMEOUT,
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Platform-specific configuration
|
|
32
|
+
*/
|
|
33
|
+
export const PLATFORM = {
|
|
34
|
+
IS_WINDOWS: process.platform === 'win32',
|
|
35
|
+
BIN_NAME: process.platform === 'win32' ? 'cloudflared.exe' : 'cloudflared',
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* File system paths
|
|
39
|
+
*/
|
|
40
|
+
export const PATHS = {
|
|
41
|
+
BIN_DIR: path.join(__dirname, 'bin'),
|
|
42
|
+
BIN_PATH: path.join(__dirname, 'bin', PLATFORM.BIN_NAME),
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Log patterns for filtering cloudflared output
|
|
46
|
+
*/
|
|
47
|
+
export const LOG_PATTERNS = SHARED_LOG_PATTERNS;
|
|
48
|
+
/**
|
|
49
|
+
* Network warning configuration
|
|
50
|
+
*/
|
|
51
|
+
export const NETWORK_CONFIG = {
|
|
52
|
+
WARNING_THRESHOLD: NETWORK_WARNING.THRESHOLD,
|
|
53
|
+
WARNING_COOLDOWN: NETWORK_WARNING.COOLDOWN,
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Tunnel timeout in milliseconds
|
|
57
|
+
*/
|
|
58
|
+
export const TUNNEL_TIMEOUT_MS = CONFIG.TUNNEL_TIMEOUT_HOURS * 60 * 60 * 1000;
|
|
59
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAEvC,OAAO,EACL,mBAAmB,EACnB,YAAY,EACZ,gBAAgB,EAChB,oBAAoB,EACpB,oBAAoB,EACpB,YAAY,IAAI,mBAAmB,EACnC,eAAe,GAChB,MAAM,gBAAgB,CAAC;AAExB,MAAM,UAAU,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAClD,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC;AACzD,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAO/C,MAAM,WAAW,GAAgB,OAAO,CAAC,iBAAiB,CAAC,CAAC;AAE5D;;GAEG;AACH,SAAS,aAAa;IACpB,IAAI,OAAO,CAAC,GAAG,CAAC,iBAAiB,EAAE,CAAC;QAClC,OAAO,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC;IACvC,CAAC;IACD,OAAO,mBAAmB,CAAC;AAC7B,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,MAAM,GAAc;IAC/B,YAAY,EAAE,WAAW,CAAC,IAAI;IAC9B,eAAe,EAAE,WAAW,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