@eduardo191labs/opencode-free-pi 0.7.11 → 0.7.12

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/README.md CHANGED
@@ -226,11 +226,13 @@ Después de iniciar sesión, ejecuta `/reload` en OpenCode o reinícialo.
226
226
  los ignora salvo que se active. Por eso, si el entorno define un proxy, el
227
227
  plugin lo activa al arrancar con `http.setGlobalProxyFromEnv()`: el catálogo,
228
228
  la cuota, los anuncios, la visión, los reintentos y las peticiones al gateway
229
- pasan por el proxy en vez de salir directos y fallar con `ETIMEDOUT`. Sin
229
+ pasan por el proxy en vez de salir directos y fallar con `ETIMEDOUT`. `ALL_PROXY`
230
+ se copia a `HTTPS_PROXY`/`HTTP_PROXY` porque Node solo lee esos dos. Sin
230
231
  variables de proxy no cambia nada. `setGlobalProxyFromEnv()` existe desde Node
231
232
  24.5; en un Node anterior el plugin escribe en el log que hay que arrancar
232
233
  OpenCode con `NODE_USE_ENV_PROXY=1` (o actualizar Node), porque esa versión
233
- ignora el proxy en `fetch` aunque se defina el resto. En Node 26 la
234
+ ignora el proxy en `fetch` aunque se defina el resto. Un proxy SOCKS no se
235
+ puede usar con esta API y también se avisa. En Node 26 la
234
236
  verificación fue real: con `HTTPS_PROXY` apuntando a un proxy local, las
235
237
  conexiones a `api.freepi.ai`, `models.opencode.ai` y `registry.npmjs.org`
236
238
  aparecieron en el proxy y una sesión completa respondió correctamente.
@@ -16,4 +16,4 @@ export declare function proxyFromEnvironment(env?: NodeJS.ProcessEnv): string |
16
16
  * Enables the environment proxy for this process when one is configured.
17
17
  * Returns whether the plugin will use it.
18
18
  */
19
- export declare function applyEnvironmentProxy(log: Logger): boolean;
19
+ export declare function applyEnvironmentProxy(log: Logger, env?: NodeJS.ProcessEnv): boolean;
@@ -10,14 +10,14 @@
10
10
  * `fetch`; when the runtime is older the plugin keeps going direct and says so.
11
11
  */
12
12
  import http from "node:http";
13
- const PROXY_VARIABLES = [
13
+ const DIRECT_VARIABLES = [
14
14
  "HTTPS_PROXY",
15
15
  "https_proxy",
16
16
  "HTTP_PROXY",
17
17
  "http_proxy",
18
- "ALL_PROXY",
19
- "all_proxy",
20
18
  ];
19
+ const ALL_VARIABLES = ["ALL_PROXY", "all_proxy"];
20
+ const PROXY_VARIABLES = [...DIRECT_VARIABLES, ...ALL_VARIABLES];
21
21
  /** First proxy URL exported in the environment, or undefined. */
22
22
  export function proxyFromEnvironment(env = process.env) {
23
23
  for (const name of PROXY_VARIABLES) {
@@ -27,6 +27,14 @@ export function proxyFromEnvironment(env = process.env) {
27
27
  }
28
28
  return undefined;
29
29
  }
30
+ function firstValue(env, names) {
31
+ for (const name of names) {
32
+ const value = env[name]?.trim();
33
+ if (value)
34
+ return { name, value };
35
+ }
36
+ return undefined;
37
+ }
30
38
  /** Proxy URL without credentials, safe for the log. */
31
39
  function describeProxy(value) {
32
40
  try {
@@ -38,6 +46,34 @@ function describeProxy(value) {
38
46
  return "configured";
39
47
  }
40
48
  }
49
+ /** HTTP(S) proxies only: Node's env support cannot dial a SOCKS URL. */
50
+ function hasSupportedScheme(value) {
51
+ try {
52
+ const url = new URL(value);
53
+ return url.protocol === "http:" || url.protocol === "https:";
54
+ }
55
+ catch {
56
+ return false;
57
+ }
58
+ }
59
+ /**
60
+ * Node reads `HTTPS_PROXY`/`HTTP_PROXY` and ignores `ALL_PROXY`, which is the
61
+ * generic name many VPN clients export. Copy it into the protocol-specific
62
+ * variables so a proxy-only network still works.
63
+ */
64
+ function applyAllProxyFallback(env) {
65
+ const all = firstValue(env, ALL_VARIABLES);
66
+ if (!all || !hasSupportedScheme(all.value))
67
+ return;
68
+ for (const [specific, name] of [
69
+ ["HTTPS_PROXY", "https_proxy"],
70
+ ["HTTP_PROXY", "http_proxy"],
71
+ ]) {
72
+ if (!env[specific]?.trim() && !env[name]?.trim()) {
73
+ env[specific] = all.value;
74
+ }
75
+ }
76
+ }
41
77
  function proxySetter() {
42
78
  const candidate = http
43
79
  .setGlobalProxyFromEnv;
@@ -49,10 +85,17 @@ function proxySetter() {
49
85
  * Enables the environment proxy for this process when one is configured.
50
86
  * Returns whether the plugin will use it.
51
87
  */
52
- export function applyEnvironmentProxy(log) {
53
- const proxy = proxyFromEnvironment();
88
+ export function applyEnvironmentProxy(log, env = process.env) {
89
+ const proxy = proxyFromEnvironment(env);
54
90
  if (!proxy)
55
91
  return false;
92
+ if (!hasSupportedScheme(proxy)) {
93
+ log.warn("free-pi found a proxy in the environment that this runtime cannot use (only http/https proxies are supported); requests go out directly", { proxy: describeProxy(proxy) });
94
+ return false;
95
+ }
96
+ // Normalize ALL_PROXY first: it stays useful even when this runtime cannot
97
+ // enable the proxy, because a restart with NODE_USE_ENV_PROXY=1 reads it.
98
+ applyAllProxyFallback(env);
56
99
  const setter = proxySetter();
57
100
  if (!setter) {
58
101
  log.warn("free-pi found a proxy in the environment but this Node runtime cannot apply it; start OpenCode with NODE_USE_ENV_PROXY=1 or use Node 24.5+ so free-pi requests follow the proxy", { node: process.version, proxy: describeProxy(proxy) });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eduardo191labs/opencode-free-pi",
3
- "version": "0.7.11",
3
+ "version": "0.7.12",
4
4
  "description": "Ad-supported free-pi provider plugin for OpenCode on Windows, Linux, macOS and Termux",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -13,14 +13,14 @@
13
13
  import http from "node:http";
14
14
  import type { Logger } from "../logging.js";
15
15
 
16
- const PROXY_VARIABLES = [
16
+ const DIRECT_VARIABLES = [
17
17
  "HTTPS_PROXY",
18
18
  "https_proxy",
19
19
  "HTTP_PROXY",
20
20
  "http_proxy",
21
- "ALL_PROXY",
22
- "all_proxy",
23
21
  ] as const;
22
+ const ALL_VARIABLES = ["ALL_PROXY", "all_proxy"] as const;
23
+ const PROXY_VARIABLES = [...DIRECT_VARIABLES, ...ALL_VARIABLES] as const;
24
24
 
25
25
  /** First proxy URL exported in the environment, or undefined. */
26
26
  export function proxyFromEnvironment(
@@ -33,6 +33,17 @@ export function proxyFromEnvironment(
33
33
  return undefined;
34
34
  }
35
35
 
36
+ function firstValue(
37
+ env: NodeJS.ProcessEnv,
38
+ names: readonly string[],
39
+ ): { name: string; value: string } | undefined {
40
+ for (const name of names) {
41
+ const value = env[name]?.trim();
42
+ if (value) return { name, value };
43
+ }
44
+ return undefined;
45
+ }
46
+
36
47
  /** Proxy URL without credentials, safe for the log. */
37
48
  function describeProxy(value: string): string {
38
49
  try {
@@ -44,6 +55,34 @@ function describeProxy(value: string): string {
44
55
  }
45
56
  }
46
57
 
58
+ /** HTTP(S) proxies only: Node's env support cannot dial a SOCKS URL. */
59
+ function hasSupportedScheme(value: string): boolean {
60
+ try {
61
+ const url = new URL(value);
62
+ return url.protocol === "http:" || url.protocol === "https:";
63
+ } catch {
64
+ return false;
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Node reads `HTTPS_PROXY`/`HTTP_PROXY` and ignores `ALL_PROXY`, which is the
70
+ * generic name many VPN clients export. Copy it into the protocol-specific
71
+ * variables so a proxy-only network still works.
72
+ */
73
+ function applyAllProxyFallback(env: NodeJS.ProcessEnv): void {
74
+ const all = firstValue(env, ALL_VARIABLES);
75
+ if (!all || !hasSupportedScheme(all.value)) return;
76
+ for (const [specific, name] of [
77
+ ["HTTPS_PROXY", "https_proxy"],
78
+ ["HTTP_PROXY", "http_proxy"],
79
+ ] as const) {
80
+ if (!env[specific]?.trim() && !env[name]?.trim()) {
81
+ env[specific] = all.value;
82
+ }
83
+ }
84
+ }
85
+
47
86
  type ProxySetter = () => unknown;
48
87
 
49
88
  function proxySetter(): ProxySetter | undefined {
@@ -58,9 +97,22 @@ function proxySetter(): ProxySetter | undefined {
58
97
  * Enables the environment proxy for this process when one is configured.
59
98
  * Returns whether the plugin will use it.
60
99
  */
61
- export function applyEnvironmentProxy(log: Logger): boolean {
62
- const proxy = proxyFromEnvironment();
100
+ export function applyEnvironmentProxy(
101
+ log: Logger,
102
+ env: NodeJS.ProcessEnv = process.env,
103
+ ): boolean {
104
+ const proxy = proxyFromEnvironment(env);
63
105
  if (!proxy) return false;
106
+ if (!hasSupportedScheme(proxy)) {
107
+ log.warn(
108
+ "free-pi found a proxy in the environment that this runtime cannot use (only http/https proxies are supported); requests go out directly",
109
+ { proxy: describeProxy(proxy) },
110
+ );
111
+ return false;
112
+ }
113
+ // Normalize ALL_PROXY first: it stays useful even when this runtime cannot
114
+ // enable the proxy, because a restart with NODE_USE_ENV_PROXY=1 reads it.
115
+ applyAllProxyFallback(env);
64
116
  const setter = proxySetter();
65
117
  if (!setter) {
66
118
  log.warn(