@swimmingliu/autovpn 1.6.6 → 1.6.8

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/lib/runner.mjs DELETED
@@ -1,113 +0,0 @@
1
- import fs from 'node:fs';
2
- import path from 'node:path';
3
- import { fileURLToPath } from 'node:url';
4
- import {
5
- spawn as defaultSpawn,
6
- spawnSync as defaultSpawnSync
7
- } from 'node:child_process';
8
-
9
- import { isEnabled, WrapperError } from './errors.mjs';
10
- import { installPythonCli } from './install-python-cli.mjs';
11
-
12
- const packageJsonPath = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', 'package.json');
13
-
14
- export function readPackageVersion() {
15
- const payload = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
16
- return String(payload.version);
17
- }
18
-
19
- function normalizeVersionOutput(stdout) {
20
- return String(stdout ?? '').trim();
21
- }
22
-
23
- export function versionMatches(stdout, packageVersion) {
24
- return normalizeVersionOutput(stdout) === `autovpn ${packageVersion}`;
25
- }
26
-
27
- export function resolvePythonCli({
28
- env = process.env,
29
- packageVersion = readPackageVersion(),
30
- spawnSync = defaultSpawnSync
31
- } = {}) {
32
- if (isEnabled(env.AUTOVPN_NO_PYTHON)) {
33
- throw new WrapperError('Python backend is disabled by AUTOVPN_NO_PYTHON.');
34
- }
35
-
36
- const explicit = String(env.AUTOVPN_PYTHON_CLI ?? '').trim();
37
- if (explicit) {
38
- return { command: explicit, args: [], source: 'AUTOVPN_PYTHON_CLI' };
39
- }
40
-
41
- const probe = spawnSync('autovpn', ['--version'], {
42
- encoding: 'utf-8',
43
- env: { ...env, AUTOVPN_WRAPPER_PROBE: '1' },
44
- stdio: ['ignore', 'pipe', 'pipe']
45
- });
46
- if (probe.status === 0) {
47
- if (versionMatches(probe.stdout, packageVersion) || isEnabled(env.AUTOVPN_ALLOW_VERSION_MISMATCH)) {
48
- return { command: 'autovpn', args: [], source: 'PATH' };
49
- }
50
- }
51
-
52
- throw new WrapperError(
53
- `No compatible Python autovpn CLI found for version ${packageVersion}. Set AUTOVPN_PYTHON_CLI or allow the wrapper to install the backend.`
54
- );
55
- }
56
-
57
- export function resolveOrInstallPythonCli({
58
- env = process.env,
59
- packageVersion = readPackageVersion(),
60
- spawnSync = defaultSpawnSync,
61
- installer = installPythonCli
62
- } = {}) {
63
- try {
64
- return resolvePythonCli({ env, packageVersion, spawnSync });
65
- } catch (error) {
66
- if (isEnabled(env.AUTOVPN_NO_PYTHON)) {
67
- throw error;
68
- }
69
- return installer({ env, packageVersion, spawnSync });
70
- }
71
- }
72
-
73
- export function runForwarder(argv = process.argv.slice(2), {
74
- env = process.env,
75
- cwd = process.cwd(),
76
- packageVersion = readPackageVersion(),
77
- spawn = defaultSpawn,
78
- spawnSync = defaultSpawnSync,
79
- installer = installPythonCli
80
- } = {}) {
81
- const resolved = resolveOrInstallPythonCli({ env, packageVersion, spawnSync, installer });
82
- const child = spawn(resolved.command, [...resolved.args, ...argv], {
83
- cwd,
84
- env,
85
- stdio: 'inherit'
86
- });
87
-
88
- return new Promise((resolve, reject) => {
89
- child.on('error', reject);
90
- child.on('exit', (code, signal) => {
91
- if (signal) {
92
- resolve(1);
93
- return;
94
- }
95
- resolve(typeof code === 'number' ? code : 1);
96
- });
97
- });
98
- }
99
-
100
- export async function main(argv = process.argv.slice(2), options = {}) {
101
- const env = options.env ?? process.env;
102
- if (isEnabled(env.AUTOVPN_WRAPPER_PROBE) && argv.length === 1 && argv[0] === '--version') {
103
- return 42;
104
- }
105
-
106
- try {
107
- return await runForwarder(argv, { ...options, env });
108
- } catch (error) {
109
- const message = error instanceof Error ? error.message : String(error);
110
- console.error(`autovpn npm wrapper error: ${message}`);
111
- return 1;
112
- }
113
- }