@introspection-sdk/introspection-proxy 0.4.5 → 0.4.7

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.
@@ -1,5 +1,4 @@
1
-
2
- 
3
- > @introspection-sdk/introspection-proxy@0.1.0 build /Users/julian/projects/introspection-org/introspection-js-sdk/packages/introspection-proxy
4
- > tsc
5
-
1
+
2
+ > @introspection-sdk/introspection-proxy@0.4.7 build /home/runner/work/introspection-js-sdk/introspection-js-sdk/packages/introspection-proxy
3
+ > tsc
4
+
package/LICENSE ADDED
@@ -0,0 +1,191 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to the Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by the Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding any notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ Copyright 2026 Introspection AI Inc.
180
+
181
+ Licensed under the Apache License, Version 2.0 (the "License");
182
+ you may not use this file except in compliance with the License.
183
+ You may obtain a copy of the License at
184
+
185
+ http://www.apache.org/licenses/LICENSE-2.0
186
+
187
+ Unless required by applicable law or agreed to in writing, software
188
+ distributed under the License is distributed on an "AS IS" BASIS,
189
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
190
+ See the License for the specific language governing permissions and
191
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # @introspection-sdk/introspection-proxy
2
+
3
+ Route outbound `fetch` calls through the Introspection egress proxy for credential injection, or through a standard CONNECT forward proxy.
4
+
5
+ ## Quick start
6
+
7
+ ```ts
8
+ import { installProxyFetch } from "@introspection-sdk/introspection-proxy";
9
+
10
+ // Replace globalThis.fetch — all subsequent fetch() calls route through the proxy.
11
+ // No-op when no proxy env vars are set (safe for local dev).
12
+ installProxyFetch();
13
+
14
+ // Every fetch now goes through the configured proxy.
15
+ const res = await fetch("https://api.openai.com/v1/models");
16
+ ```
17
+
18
+ ## Environment variables
19
+
20
+ | Variable | Purpose |
21
+ | ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
22
+ | `INTROSPECTION_EGRESS_URL` | Egress reverse proxy URL (e.g. `http://localhost:10000`). Sends requests as plain HTTP so the proxy can route by `Host` header and inject credentials via ext_proc. |
23
+ | `INTROSPECTION_ENDPOINT_HOSTS` | Comma-separated hostnames that should use the egress proxy (e.g. `api.openai.com,api.anthropic.com`). When set, only matching hosts use egress; all others fall through to the CONNECT proxy. When unset, all traffic goes through egress. |
24
+ | `HTTPS_PROXY` / `HTTP_PROXY` | Standard forward proxy. Used as the CONNECT tunnel for hosts not in `INTROSPECTION_ENDPOINT_HOSTS`. |
25
+
26
+ ## Per-client usage
27
+
28
+ If you only want to proxy a single client (not the whole process):
29
+
30
+ ```ts
31
+ import { createProxyFetch } from "@introspection-sdk/introspection-proxy";
32
+ import { createClient } from "@supabase/supabase-js";
33
+
34
+ const supabase = createClient(url, key, {
35
+ global: { fetch: createProxyFetch() },
36
+ });
37
+ ```
38
+
39
+ ## How it works
40
+
41
+ Two proxy modes, selected per-request:
42
+
43
+ - **Egress credential-injection proxy** (`INTROSPECTION_EGRESS_URL`): plain-HTTP reverse proxy that routes by `Host` header and injects upstream credentials via ext_proc. Only used for hosts in `INTROSPECTION_ENDPOINT_HOSTS`.
44
+
45
+ - **Forward CONNECT proxy** (`HTTPS_PROXY`): opaque TLS tunnel the proxy cannot read or modify. Used for all other hosts (S3, GitHub, npm, etc.).
package/dist/proxy.d.ts CHANGED
@@ -1,51 +1,16 @@
1
1
  import { type Dispatcher } from "undici";
2
2
  export interface ProxyFetchOptions {
3
- /**
4
- * Egress (credential-injection) proxy URL. Defaults to
5
- * `process.env.EGRESS_PROXY_URL` (e.g. `http://localhost:10000`).
6
- */
7
3
  egressProxyUrl?: string;
8
- /**
9
- * Standard forward-proxy URL. Defaults to the `HTTPS_PROXY` / `HTTP_PROXY`
10
- * environment variables (which also honour `NO_PROXY`). Used only when no
11
- * egress proxy is configured.
12
- *
13
- * Note: this is a plain forward proxy (a `CONNECT` tunnel for `https:`
14
- * targets) and does **not** inject credentials — it only routes traffic. For
15
- * the Introspection egress proxy that injects credentials, use
16
- * {@link ProxyFetchOptions.egressProxyUrl} / `EGRESS_PROXY_URL` instead.
17
- */
4
+ egressProxyHosts?: string;
18
5
  forwardProxyUrl?: string;
19
6
  }
20
- /**
21
- * Resolve a standard forward-proxy URL from the environment, honouring the
22
- * conventional `HTTPS_PROXY` / `HTTP_PROXY` variables (and their lowercase
23
- * forms). Shared with the OTLP exporter proxy helper (`withOtlpHttpsProxy`) so
24
- * fetch traffic and OTLP traffic resolve the same proxy.
25
- */
26
7
  export declare function resolveForwardProxyUrl(): string | undefined;
27
8
  /**
28
- * Build an undici {@link Dispatcher} for outbound HTTP based on the
29
- * environment (or explicit {@link ProxyFetchOptions}). Returns `undefined`
30
- * when no proxy is configured, so callers can fall back to the default fetch.
9
+ * Returns a single dispatcher. When egress hosts are scoped, returns the
10
+ * forward dispatcher (the egress/forward split is per-request inside
11
+ * {@link createProxyFetch}).
31
12
  */
32
13
  export declare function getProxyDispatcher(options?: ProxyFetchOptions): Dispatcher | undefined;
33
- /**
34
- * A `fetch`-compatible function that routes through the configured proxy.
35
- * Drop-in for any client that accepts a `fetch` (e.g. supabase-js
36
- * `createClient(url, key, { global: { fetch: createProxyFetch() } })`).
37
- *
38
- * When no proxy is configured this returns the global `fetch` unchanged, so it
39
- * is safe to use unconditionally (local dev keeps talking directly to APIs).
40
- */
41
14
  export declare function createProxyFetch(options?: ProxyFetchOptions): typeof fetch;
42
- /**
43
- * Replace `globalThis.fetch` so existing bare `fetch(...)` calls — and any SDK
44
- * that uses the global fetch by default, including supabase-js — route through
45
- * the configured proxy with no other code changes. No-op when no proxy is
46
- * configured.
47
- *
48
- * Returns a function that restores the original `fetch`.
49
- */
50
15
  export declare function installProxyFetch(options?: ProxyFetchOptions): () => void;
51
16
  //# sourceMappingURL=proxy.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"proxy.d.ts","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":"AAuCA,OAAO,EAML,KAAK,UAAU,EAChB,MAAM,QAAQ,CAAC;AAEhB,MAAM,WAAW,iBAAiB;IAChC;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB;;;;;;;;;OASG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAMD;;;;;GAKG;AACH,wBAAgB,sBAAsB,IAAI,MAAM,GAAG,SAAS,CAS3D;AA4BD;;;;GAIG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,GAAE,iBAAsB,GAC9B,UAAU,GAAG,SAAS,CAgBxB;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,GAAE,iBAAsB,GAC9B,OAAO,KAAK,CAyDd;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,GAAE,iBAAsB,GAAG,MAAM,IAAI,CAW7E"}
1
+ {"version":3,"file":"proxy.d.ts","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":"AAiBA,OAAO,EAKL,KAAK,UAAU,EAChB,MAAM,QAAQ,CAAC;AAEhB,MAAM,WAAW,iBAAiB;IAChC,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAiBD,wBAAgB,sBAAsB,IAAI,MAAM,GAAG,SAAS,CAQ3D;AA0BD;;;;GAIG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,GAAE,iBAAsB,GAC9B,UAAU,GAAG,SAAS,CASxB;AAaD,wBAAgB,gBAAgB,CAC9B,OAAO,GAAE,iBAAsB,GAC9B,OAAO,KAAK,CAoDd;AAED,wBAAgB,iBAAiB,CAAC,OAAO,GAAE,iBAAsB,GAAG,MAAM,IAAI,CAQ7E"}
package/dist/proxy.js CHANGED
@@ -1,58 +1,36 @@
1
1
  /**
2
2
  * Outbound HTTP proxy helpers for `fetch`-based clients.
3
3
  *
4
- * These let any `fetch` call — a bare `fetch(...)`, an SDK that accepts a
5
- * custom `fetch` (e.g. `@supabase/supabase-js` via `global.fetch`), or every
6
- * request in a process (via {@link installProxyFetch}) — be routed through a
7
- * proxy so the caller does not have to hold upstream credentials.
4
+ * Two proxy modes, selected per-request:
8
5
  *
9
- * Two proxy shapes are supported, selected from the environment:
6
+ * - **Egress credential-injection proxy** (`INTROSPECTION_EGRESS_URL`):
7
+ * plain-HTTP reverse proxy that routes by `Host` header and injects upstream
8
+ * credentials via ext_proc. Only used for hosts listed in
9
+ * `INTROSPECTION_ENDPOINT_HOSTS` (comma-separated). When the host list is
10
+ * empty, all traffic goes through egress (legacy behaviour).
10
11
  *
11
- * - **Egress credential-injection proxy** (`EGRESS_PROXY_URL`): the
12
- * Introspection egress proxy is a plain-HTTP reverse proxy that routes by
13
- * the request's `Host`/`:authority` header and injects the real upstream
14
- * credential (so the process can send a placeholder/locator token). We dial
15
- * the proxy on every connection but leave the request — including its real
16
- * `Host` and `https:` scheme — untouched, which is exactly what the proxy
17
- * needs to route and inject. undici speaks HTTP/1.1 over the socket we hand
18
- * it, so no URL rewriting is required.
19
- *
20
- * - **Standard forward (CONNECT) proxy** (`HTTPS_PROXY` / `HTTP_PROXY`,
21
- * honouring `NO_PROXY`): the same kind of proxy the OTLP exporter uses, but
22
- * exposed as an undici dispatcher so it works with `fetch`.
23
- * (`HttpsProxyAgent` is a Node `http.Agent` and is ignored by `fetch`, which
24
- * only accepts an undici `dispatcher`.) This is plain forward-proxying and
25
- * does **not** inject credentials: for `https:` targets it uses HTTP
26
- * `CONNECT`, so the connection is an opaque end-to-end TLS tunnel the proxy
27
- * cannot read or modify. Credential injection happens **only** in egress
28
- * mode above (where the request is sent to the proxy in plaintext so it can
29
- * route by `Host` and inject). Use this branch for consumers that sit behind
30
- * a generic corporate/forward proxy.
31
- *
32
- * `EGRESS_PROXY_URL` takes precedence when both are set.
33
- *
34
- * Note: in egress mode the dispatcher routes *all* requests through the proxy,
35
- * so destinations must be configured on the proxy. This matches the intended
36
- * "all egress goes through the proxy" model for sandboxed recipes.
12
+ * - **Forward CONNECT proxy** (`HTTPS_PROXY` / `HTTP_PROXY`): opaque TLS
13
+ * tunnel the proxy cannot read or modify. Used for all other hosts (S3,
14
+ * GitHub, npm, etc.).
37
15
  */
38
16
  import { Socket } from "node:net";
39
17
  import { connect as tlsConnect } from "node:tls";
40
- import { Agent, EnvHttpProxyAgent, ProxyAgent, fetch as undiciFetch, } from "undici";
41
- function resolveEgressProxyUrl(options) {
42
- return options.egressProxyUrl ?? process.env.EGRESS_PROXY_URL ?? undefined;
18
+ import { Agent, EnvHttpProxyAgent, fetch as undiciFetch, } from "undici";
19
+ function resolveEgressUrl(options) {
20
+ return options.egressProxyUrl ?? process.env.INTROSPECTION_EGRESS_URL;
21
+ }
22
+ function resolveEgressHosts(options) {
23
+ const raw = options.egressProxyHosts ?? process.env.INTROSPECTION_ENDPOINT_HOSTS ?? "";
24
+ return new Set(raw
25
+ .split(",")
26
+ .map((h) => h.trim().toLowerCase())
27
+ .filter(Boolean));
43
28
  }
44
- /**
45
- * Resolve a standard forward-proxy URL from the environment, honouring the
46
- * conventional `HTTPS_PROXY` / `HTTP_PROXY` variables (and their lowercase
47
- * forms). Shared with the OTLP exporter proxy helper (`withOtlpHttpsProxy`) so
48
- * fetch traffic and OTLP traffic resolve the same proxy.
49
- */
50
29
  export function resolveForwardProxyUrl() {
51
- const env = process.env;
52
- return (env.HTTPS_PROXY ||
53
- env.https_proxy ||
54
- env.HTTP_PROXY ||
55
- env.http_proxy ||
30
+ return (process.env.HTTPS_PROXY ||
31
+ process.env.https_proxy ||
32
+ process.env.HTTP_PROXY ||
33
+ process.env.http_proxy ||
56
34
  undefined);
57
35
  }
58
36
  function buildEgressDispatcher(proxyUrl) {
@@ -60,8 +38,6 @@ function buildEgressDispatcher(proxyUrl) {
60
38
  const useTls = url.protocol === "https:";
61
39
  const port = Number(url.port) || (useTls ? 443 : 80);
62
40
  const host = url.hostname;
63
- // Always connect to the proxy, regardless of the request's destination. The
64
- // request keeps its real `Host`, which is what the proxy routes + injects on.
65
41
  return new Agent({
66
42
  connect(_options, callback) {
67
43
  if (useTls) {
@@ -78,44 +54,47 @@ function buildEgressDispatcher(proxyUrl) {
78
54
  });
79
55
  }
80
56
  /**
81
- * Build an undici {@link Dispatcher} for outbound HTTP based on the
82
- * environment (or explicit {@link ProxyFetchOptions}). Returns `undefined`
83
- * when no proxy is configured, so callers can fall back to the default fetch.
57
+ * Returns a single dispatcher. When egress hosts are scoped, returns the
58
+ * forward dispatcher (the egress/forward split is per-request inside
59
+ * {@link createProxyFetch}).
84
60
  */
85
61
  export function getProxyDispatcher(options = {}) {
86
- const egressUrl = resolveEgressProxyUrl(options);
87
- if (egressUrl) {
62
+ const egressUrl = resolveEgressUrl(options);
63
+ if (egressUrl && resolveEgressHosts(options).size === 0) {
88
64
  return buildEgressDispatcher(egressUrl);
89
65
  }
90
- if (options.forwardProxyUrl) {
91
- return new ProxyAgent(options.forwardProxyUrl);
92
- }
93
66
  if (resolveForwardProxyUrl()) {
94
- // Reads HTTPS_PROXY / HTTP_PROXY / NO_PROXY from the environment.
95
67
  return new EnvHttpProxyAgent();
96
68
  }
97
69
  return undefined;
98
70
  }
99
- /**
100
- * A `fetch`-compatible function that routes through the configured proxy.
101
- * Drop-in for any client that accepts a `fetch` (e.g. supabase-js
102
- * `createClient(url, key, { global: { fetch: createProxyFetch() } })`).
103
- *
104
- * When no proxy is configured this returns the global `fetch` unchanged, so it
105
- * is safe to use unconditionally (local dev keeps talking directly to APIs).
106
- */
71
+ function toUrlString(input) {
72
+ if (typeof input === "string")
73
+ return input;
74
+ if (input instanceof URL)
75
+ return input.toString();
76
+ return input.url;
77
+ }
78
+ function downgradeToHttp(url) {
79
+ const s = typeof url === "string" ? url : url.toString();
80
+ return s.startsWith("https://") ? "http://" + s.slice(8) : s;
81
+ }
107
82
  export function createProxyFetch(options = {}) {
108
- const dispatcher = getProxyDispatcher(options);
109
- if (!dispatcher) {
110
- return fetch;
111
- }
112
- const egressUrl = resolveEgressProxyUrl(options);
83
+ const egressUrl = resolveEgressUrl(options);
84
+ const egressHosts = resolveEgressHosts(options);
113
85
  const egressIsPlainHttp = !!egressUrl && new URL(egressUrl).protocol === "http:";
114
- const proxied = (input, init) => {
115
- // Some clients (notably axios's fetch adapter) call fetch with a `Request`
116
- // object. undici's fetch can't consume a foreign Request instance (it tries
117
- // to coerce it to a URL — "Failed to parse URL from [object Request]"), so
118
- // normalise a Request into (url, init), carrying over its fields.
86
+ const egress = egressUrl ? buildEgressDispatcher(egressUrl) : undefined;
87
+ const forward = resolveForwardProxyUrl()
88
+ ? new EnvHttpProxyAgent()
89
+ : undefined;
90
+ if (!egress && !forward)
91
+ return fetch;
92
+ return (input, init) => {
93
+ const hostname = new URL(toUrlString(input)).hostname.toLowerCase();
94
+ const useEgress = !!egress && (egressHosts.size === 0 || egressHosts.has(hostname));
95
+ const dispatcher = useEgress ? egress : forward;
96
+ if (!dispatcher)
97
+ return fetch(input, init);
119
98
  let target;
120
99
  let opts;
121
100
  if (typeof Request !== "undefined" && input instanceof Request) {
@@ -130,7 +109,6 @@ export function createProxyFetch(options = {}) {
130
109
  ...init,
131
110
  dispatcher,
132
111
  };
133
- // A streamed request body requires half-duplex in undici.
134
112
  if (input.body != null)
135
113
  opts.duplex = "half";
136
114
  }
@@ -138,35 +116,18 @@ export function createProxyFetch(options = {}) {
138
116
  target = input;
139
117
  opts = { ...init, dispatcher };
140
118
  }
141
- // Egress (reverse proxy) mode: when the proxy itself is plain HTTP,
142
- // rewrite https:// → http:// so undici doesn't TLS-wrap the connection
143
- // to the proxy. The proxy handles upstream TLS. The Host header is
144
- // preserved for routing. Only applied when EGRESS_PROXY_URL is http://.
145
- if (egressIsPlainHttp) {
146
- const urlStr = typeof target === "string" ? target : target.toString();
147
- if (urlStr.startsWith("https://")) {
148
- target = "http://" + urlStr.slice(8);
149
- }
119
+ if (useEgress && egressIsPlainHttp) {
120
+ target = downgradeToHttp(target);
150
121
  }
151
122
  return undiciFetch(target, opts);
152
123
  };
153
- return proxied;
154
124
  }
155
- /**
156
- * Replace `globalThis.fetch` so existing bare `fetch(...)` calls — and any SDK
157
- * that uses the global fetch by default, including supabase-js — route through
158
- * the configured proxy with no other code changes. No-op when no proxy is
159
- * configured.
160
- *
161
- * Returns a function that restores the original `fetch`.
162
- */
163
125
  export function installProxyFetch(options = {}) {
164
- const dispatcher = getProxyDispatcher(options);
165
- if (!dispatcher) {
126
+ const proxied = createProxyFetch(options);
127
+ if (proxied === fetch)
166
128
  return () => { };
167
- }
168
129
  const original = globalThis.fetch;
169
- globalThis.fetch = createProxyFetch(options);
130
+ globalThis.fetch = proxied;
170
131
  return () => {
171
132
  globalThis.fetch = original;
172
133
  };
package/dist/proxy.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"proxy.js","sourceRoot":"","sources":["../src/proxy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MAAM;gBACpB,OAAO,EAAE,KAAK,CAAC,OAAO;gBACtB,IAAI,EAAE,KAAK,CAAC,IAAI;gBAChB,MAAM,EAAE,KAAK,CAAC,MAAM;gBACpB,QAAQ,EAAE,KAAK,CAAC,QAAQ;gBACxB,WAAW,EAAE,KAAK,CAAC,WAAW;gBAC9B,GAAG,IAAI;gBACP,UAAU;aACX,CAAC;YACF,IAAI,KAAK,CAAC,IAAI,IAAI,IAAI;gBAAE,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QAC/C,CAAC;aAAM,CAAC;YACN,MAAM,GAAG,KAAqB,CAAC;YAC/B,IAAI,GAAG,EAAE,GAAG,IAAI,EAAE,UAAU,EAAE,CAAC;QACjC,CAAC;QAED,IAAI,SAAS,IAAI,iBAAiB,EAAE,CAAC;YACnC,MAAM,GAAG,eAAe,CAAC,MAAM,CAAC,CAAC;QACnC,CAAC;QAED,OAAO,WAAW,CAChB,MAAsD,EACtD,IAAoD,CACrB,CAAC;IACpC,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,UAA6B,EAAE;IAC/D,MAAM,OAAO,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAC1C,IAAI,OAAO,KAAK,KAAK;QAAE,OAAO,GAAG,EAAE,GAAE,CAAC,CAAC;IACvC,MAAM,QAAQ,GAAG,UAAU,CAAC,KAAK,CAAC;IAClC,UAAU,CAAC,KAAK,GAAG,OAAO,CAAC;IAC3B,OAAO,GAAG,EAAE;QACV,UAAU,CAAC,KAAK,GAAG,QAAQ,CAAC;IAC9B,CAAC,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@introspection-sdk/introspection-proxy",
3
- "version": "0.4.5",
3
+ "version": "0.4.7",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Outbound HTTP proxy helpers for routing fetch-based clients through the Introspection egress proxy or a standard forward proxy",
6
6
  "engines": {
@@ -15,21 +15,20 @@
15
15
  "default": "./dist/index.js"
16
16
  }
17
17
  },
18
- "scripts": {
19
- "lint": "eslint . --max-warnings 0",
20
- "check-types": "tsc --noEmit",
21
- "build": "tsc",
22
- "clean": "rm -rf dist",
23
- "prepare": "tsc"
24
- },
25
18
  "dependencies": {
26
19
  "undici": "^8.3.0"
27
20
  },
28
21
  "devDependencies": {
29
- "@repo/eslint-config": "workspace:*",
30
- "@repo/typescript-config": "workspace:*",
31
22
  "@types/node": "^22.19.19",
32
23
  "eslint": "^9.39.4",
33
- "typescript": "5.9.3"
24
+ "typescript": "5.9.3",
25
+ "@repo/eslint-config": "0.0.0",
26
+ "@repo/typescript-config": "0.0.0"
27
+ },
28
+ "scripts": {
29
+ "lint": "eslint . --max-warnings 0",
30
+ "check-types": "tsc --noEmit",
31
+ "build": "tsc",
32
+ "clean": "rm -rf dist"
34
33
  }
35
- }
34
+ }
package/src/proxy.ts CHANGED
@@ -1,87 +1,55 @@
1
1
  /**
2
2
  * Outbound HTTP proxy helpers for `fetch`-based clients.
3
3
  *
4
- * These let any `fetch` call — a bare `fetch(...)`, an SDK that accepts a
5
- * custom `fetch` (e.g. `@supabase/supabase-js` via `global.fetch`), or every
6
- * request in a process (via {@link installProxyFetch}) — be routed through a
7
- * proxy so the caller does not have to hold upstream credentials.
4
+ * Two proxy modes, selected per-request:
8
5
  *
9
- * Two proxy shapes are supported, selected from the environment:
6
+ * - **Egress credential-injection proxy** (`INTROSPECTION_EGRESS_URL`):
7
+ * plain-HTTP reverse proxy that routes by `Host` header and injects upstream
8
+ * credentials via ext_proc. Only used for hosts listed in
9
+ * `INTROSPECTION_ENDPOINT_HOSTS` (comma-separated). When the host list is
10
+ * empty, all traffic goes through egress (legacy behaviour).
10
11
  *
11
- * - **Egress credential-injection proxy** (`EGRESS_PROXY_URL`): the
12
- * Introspection egress proxy is a plain-HTTP reverse proxy that routes by
13
- * the request's `Host`/`:authority` header and injects the real upstream
14
- * credential (so the process can send a placeholder/locator token). We dial
15
- * the proxy on every connection but leave the request — including its real
16
- * `Host` and `https:` scheme — untouched, which is exactly what the proxy
17
- * needs to route and inject. undici speaks HTTP/1.1 over the socket we hand
18
- * it, so no URL rewriting is required.
19
- *
20
- * - **Standard forward (CONNECT) proxy** (`HTTPS_PROXY` / `HTTP_PROXY`,
21
- * honouring `NO_PROXY`): the same kind of proxy the OTLP exporter uses, but
22
- * exposed as an undici dispatcher so it works with `fetch`.
23
- * (`HttpsProxyAgent` is a Node `http.Agent` and is ignored by `fetch`, which
24
- * only accepts an undici `dispatcher`.) This is plain forward-proxying and
25
- * does **not** inject credentials: for `https:` targets it uses HTTP
26
- * `CONNECT`, so the connection is an opaque end-to-end TLS tunnel the proxy
27
- * cannot read or modify. Credential injection happens **only** in egress
28
- * mode above (where the request is sent to the proxy in plaintext so it can
29
- * route by `Host` and inject). Use this branch for consumers that sit behind
30
- * a generic corporate/forward proxy.
31
- *
32
- * `EGRESS_PROXY_URL` takes precedence when both are set.
33
- *
34
- * Note: in egress mode the dispatcher routes *all* requests through the proxy,
35
- * so destinations must be configured on the proxy. This matches the intended
36
- * "all egress goes through the proxy" model for sandboxed recipes.
12
+ * - **Forward CONNECT proxy** (`HTTPS_PROXY` / `HTTP_PROXY`): opaque TLS
13
+ * tunnel the proxy cannot read or modify. Used for all other hosts (S3,
14
+ * GitHub, npm, etc.).
37
15
  */
38
16
  import { Socket } from "node:net";
39
17
  import { connect as tlsConnect } from "node:tls";
40
18
  import {
41
19
  Agent,
42
20
  EnvHttpProxyAgent,
43
- ProxyAgent,
44
21
  buildConnector,
45
22
  fetch as undiciFetch,
46
23
  type Dispatcher,
47
24
  } from "undici";
48
25
 
49
26
  export interface ProxyFetchOptions {
50
- /**
51
- * Egress (credential-injection) proxy URL. Defaults to
52
- * `process.env.EGRESS_PROXY_URL` (e.g. `http://localhost:10000`).
53
- */
54
27
  egressProxyUrl?: string;
55
- /**
56
- * Standard forward-proxy URL. Defaults to the `HTTPS_PROXY` / `HTTP_PROXY`
57
- * environment variables (which also honour `NO_PROXY`). Used only when no
58
- * egress proxy is configured.
59
- *
60
- * Note: this is a plain forward proxy (a `CONNECT` tunnel for `https:`
61
- * targets) and does **not** inject credentials — it only routes traffic. For
62
- * the Introspection egress proxy that injects credentials, use
63
- * {@link ProxyFetchOptions.egressProxyUrl} / `EGRESS_PROXY_URL` instead.
64
- */
28
+ egressProxyHosts?: string;
65
29
  forwardProxyUrl?: string;
66
30
  }
67
31
 
68
- function resolveEgressProxyUrl(options: ProxyFetchOptions): string | undefined {
69
- return options.egressProxyUrl ?? process.env.EGRESS_PROXY_URL ?? undefined;
32
+ function resolveEgressUrl(options: ProxyFetchOptions): string | undefined {
33
+ return options.egressProxyUrl ?? process.env.INTROSPECTION_EGRESS_URL;
34
+ }
35
+
36
+ function resolveEgressHosts(options: ProxyFetchOptions): Set<string> {
37
+ const raw =
38
+ options.egressProxyHosts ?? process.env.INTROSPECTION_ENDPOINT_HOSTS ?? "";
39
+ return new Set(
40
+ raw
41
+ .split(",")
42
+ .map((h) => h.trim().toLowerCase())
43
+ .filter(Boolean),
44
+ );
70
45
  }
71
46
 
72
- /**
73
- * Resolve a standard forward-proxy URL from the environment, honouring the
74
- * conventional `HTTPS_PROXY` / `HTTP_PROXY` variables (and their lowercase
75
- * forms). Shared with the OTLP exporter proxy helper (`withOtlpHttpsProxy`) so
76
- * fetch traffic and OTLP traffic resolve the same proxy.
77
- */
78
47
  export function resolveForwardProxyUrl(): string | undefined {
79
- const env = process.env;
80
48
  return (
81
- env.HTTPS_PROXY ||
82
- env.https_proxy ||
83
- env.HTTP_PROXY ||
84
- env.http_proxy ||
49
+ process.env.HTTPS_PROXY ||
50
+ process.env.https_proxy ||
51
+ process.env.HTTP_PROXY ||
52
+ process.env.http_proxy ||
85
53
  undefined
86
54
  );
87
55
  }
@@ -92,8 +60,6 @@ function buildEgressDispatcher(proxyUrl: string): Dispatcher {
92
60
  const port = Number(url.port) || (useTls ? 443 : 80);
93
61
  const host = url.hostname;
94
62
 
95
- // Always connect to the proxy, regardless of the request's destination. The
96
- // request keeps its real `Host`, which is what the proxy routes + injects on.
97
63
  return new Agent({
98
64
  connect(
99
65
  _options: buildConnector.Options,
@@ -113,60 +79,61 @@ function buildEgressDispatcher(proxyUrl: string): Dispatcher {
113
79
  }
114
80
 
115
81
  /**
116
- * Build an undici {@link Dispatcher} for outbound HTTP based on the
117
- * environment (or explicit {@link ProxyFetchOptions}). Returns `undefined`
118
- * when no proxy is configured, so callers can fall back to the default fetch.
82
+ * Returns a single dispatcher. When egress hosts are scoped, returns the
83
+ * forward dispatcher (the egress/forward split is per-request inside
84
+ * {@link createProxyFetch}).
119
85
  */
120
86
  export function getProxyDispatcher(
121
87
  options: ProxyFetchOptions = {},
122
88
  ): Dispatcher | undefined {
123
- const egressUrl = resolveEgressProxyUrl(options);
124
- if (egressUrl) {
89
+ const egressUrl = resolveEgressUrl(options);
90
+ if (egressUrl && resolveEgressHosts(options).size === 0) {
125
91
  return buildEgressDispatcher(egressUrl);
126
92
  }
127
-
128
- if (options.forwardProxyUrl) {
129
- return new ProxyAgent(options.forwardProxyUrl);
130
- }
131
-
132
93
  if (resolveForwardProxyUrl()) {
133
- // Reads HTTPS_PROXY / HTTP_PROXY / NO_PROXY from the environment.
134
94
  return new EnvHttpProxyAgent();
135
95
  }
136
-
137
96
  return undefined;
138
97
  }
139
98
 
140
- /**
141
- * A `fetch`-compatible function that routes through the configured proxy.
142
- * Drop-in for any client that accepts a `fetch` (e.g. supabase-js
143
- * `createClient(url, key, { global: { fetch: createProxyFetch() } })`).
144
- *
145
- * When no proxy is configured this returns the global `fetch` unchanged, so it
146
- * is safe to use unconditionally (local dev keeps talking directly to APIs).
147
- */
99
+ function toUrlString(input: RequestInfo | URL): string {
100
+ if (typeof input === "string") return input;
101
+ if (input instanceof URL) return input.toString();
102
+ return (input as Request).url;
103
+ }
104
+
105
+ function downgradeToHttp(url: string | URL): string {
106
+ const s = typeof url === "string" ? url : url.toString();
107
+ return s.startsWith("https://") ? "http://" + s.slice(8) : s;
108
+ }
109
+
148
110
  export function createProxyFetch(
149
111
  options: ProxyFetchOptions = {},
150
112
  ): typeof fetch {
151
- const dispatcher = getProxyDispatcher(options);
152
- if (!dispatcher) {
153
- return fetch;
154
- }
155
-
156
- const egressUrl = resolveEgressProxyUrl(options);
113
+ const egressUrl = resolveEgressUrl(options);
114
+ const egressHosts = resolveEgressHosts(options);
157
115
  const egressIsPlainHttp =
158
116
  !!egressUrl && new URL(egressUrl).protocol === "http:";
159
117
 
160
- const proxied = (
161
- input: RequestInfo | URL,
162
- init?: RequestInit,
163
- ): Promise<Response> => {
164
- // Some clients (notably axios's fetch adapter) call fetch with a `Request`
165
- // object. undici's fetch can't consume a foreign Request instance (it tries
166
- // to coerce it to a URL — "Failed to parse URL from [object Request]"), so
167
- // normalise a Request into (url, init), carrying over its fields.
118
+ const egress = egressUrl ? buildEgressDispatcher(egressUrl) : undefined;
119
+ const forward = resolveForwardProxyUrl()
120
+ ? new EnvHttpProxyAgent()
121
+ : undefined;
122
+
123
+ if (!egress && !forward) return fetch;
124
+
125
+ return (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
126
+ const hostname = new URL(toUrlString(input)).hostname.toLowerCase();
127
+
128
+ const useEgress =
129
+ !!egress && (egressHosts.size === 0 || egressHosts.has(hostname));
130
+ const dispatcher = useEgress ? egress : forward;
131
+
132
+ if (!dispatcher) return fetch(input, init);
133
+
168
134
  let target: string | URL;
169
135
  let opts: Record<string, unknown>;
136
+
170
137
  if (typeof Request !== "undefined" && input instanceof Request) {
171
138
  target = input.url;
172
139
  opts = {
@@ -179,22 +146,14 @@ export function createProxyFetch(
179
146
  ...init,
180
147
  dispatcher,
181
148
  };
182
- // A streamed request body requires half-duplex in undici.
183
149
  if (input.body != null) opts.duplex = "half";
184
150
  } else {
185
151
  target = input as string | URL;
186
152
  opts = { ...init, dispatcher };
187
153
  }
188
154
 
189
- // Egress (reverse proxy) mode: when the proxy itself is plain HTTP,
190
- // rewrite https:// → http:// so undici doesn't TLS-wrap the connection
191
- // to the proxy. The proxy handles upstream TLS. The Host header is
192
- // preserved for routing. Only applied when EGRESS_PROXY_URL is http://.
193
- if (egressIsPlainHttp) {
194
- const urlStr = typeof target === "string" ? target : target.toString();
195
- if (urlStr.startsWith("https://")) {
196
- target = "http://" + urlStr.slice(8);
197
- }
155
+ if (useEgress && egressIsPlainHttp) {
156
+ target = downgradeToHttp(target);
198
157
  }
199
158
 
200
159
  return undiciFetch(
@@ -202,26 +161,13 @@ export function createProxyFetch(
202
161
  opts as unknown as Parameters<typeof undiciFetch>[1],
203
162
  ) as unknown as Promise<Response>;
204
163
  };
205
-
206
- return proxied as typeof fetch;
207
164
  }
208
165
 
209
- /**
210
- * Replace `globalThis.fetch` so existing bare `fetch(...)` calls — and any SDK
211
- * that uses the global fetch by default, including supabase-js — route through
212
- * the configured proxy with no other code changes. No-op when no proxy is
213
- * configured.
214
- *
215
- * Returns a function that restores the original `fetch`.
216
- */
217
166
  export function installProxyFetch(options: ProxyFetchOptions = {}): () => void {
218
- const dispatcher = getProxyDispatcher(options);
219
- if (!dispatcher) {
220
- return () => {};
221
- }
222
-
167
+ const proxied = createProxyFetch(options);
168
+ if (proxied === fetch) return () => {};
223
169
  const original = globalThis.fetch;
224
- globalThis.fetch = createProxyFetch(options);
170
+ globalThis.fetch = proxied;
225
171
  return () => {
226
172
  globalThis.fetch = original;
227
173
  };
@@ -1,5 +0,0 @@
1
-
2
- 
3
- > @introspection-sdk/introspection-proxy@0.1.0 check-types /Users/julian/projects/introspection-org/introspection-js-sdk/packages/introspection-proxy
4
- > tsc --noEmit
5
-