@introspection-sdk/introspection-proxy 0.4.5 → 0.4.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/.turbo/turbo-build.log +4 -5
- package/LICENSE +191 -0
- package/README.md +45 -0
- package/dist/proxy.d.ts +4 -39
- package/dist/proxy.d.ts.map +1 -1
- package/dist/proxy.js +59 -97
- package/dist/proxy.js.map +1 -1
- package/package.json +13 -14
- package/src/proxy.ts +68 -121
- package/.turbo/turbo-check-types.log +0 -5
- package/introspection-sdk-introspection-proxy-0.1.0.tgz +0 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
>
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
|
|
2
|
+
> @introspection-sdk/introspection-proxy@0.4.8 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
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
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
|
package/dist/proxy.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"proxy.d.ts","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":"
|
|
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,CAUxB;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
|
-
*
|
|
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
|
-
*
|
|
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, egress is disabled and all traffic uses the forward proxy.
|
|
10
11
|
*
|
|
11
|
-
* - **
|
|
12
|
-
*
|
|
13
|
-
*
|
|
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,
|
|
41
|
-
function
|
|
42
|
-
return options.egressProxyUrl ?? process.env.
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
env.
|
|
54
|
-
env.
|
|
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,48 @@ function buildEgressDispatcher(proxyUrl) {
|
|
|
78
54
|
});
|
|
79
55
|
}
|
|
80
56
|
/**
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
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 =
|
|
87
|
-
|
|
62
|
+
const egressUrl = resolveEgressUrl(options);
|
|
63
|
+
const egressHostSet = resolveEgressHosts(options);
|
|
64
|
+
if (egressUrl && egressHostSet.size > 0) {
|
|
88
65
|
return buildEgressDispatcher(egressUrl);
|
|
89
66
|
}
|
|
90
|
-
if (options.forwardProxyUrl) {
|
|
91
|
-
return new ProxyAgent(options.forwardProxyUrl);
|
|
92
|
-
}
|
|
93
67
|
if (resolveForwardProxyUrl()) {
|
|
94
|
-
// Reads HTTPS_PROXY / HTTP_PROXY / NO_PROXY from the environment.
|
|
95
68
|
return new EnvHttpProxyAgent();
|
|
96
69
|
}
|
|
97
70
|
return undefined;
|
|
98
71
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
72
|
+
function toUrlString(input) {
|
|
73
|
+
if (typeof input === "string")
|
|
74
|
+
return input;
|
|
75
|
+
if (input instanceof URL)
|
|
76
|
+
return input.toString();
|
|
77
|
+
return input.url;
|
|
78
|
+
}
|
|
79
|
+
function downgradeToHttp(url) {
|
|
80
|
+
const s = typeof url === "string" ? url : url.toString();
|
|
81
|
+
return s.startsWith("https://") ? "http://" + s.slice(8) : s;
|
|
82
|
+
}
|
|
107
83
|
export function createProxyFetch(options = {}) {
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
return fetch;
|
|
111
|
-
}
|
|
112
|
-
const egressUrl = resolveEgressProxyUrl(options);
|
|
84
|
+
const egressUrl = resolveEgressUrl(options);
|
|
85
|
+
const egressHosts = resolveEgressHosts(options);
|
|
113
86
|
const egressIsPlainHttp = !!egressUrl && new URL(egressUrl).protocol === "http:";
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
87
|
+
const egress = egressUrl ? buildEgressDispatcher(egressUrl) : undefined;
|
|
88
|
+
const forward = resolveForwardProxyUrl()
|
|
89
|
+
? new EnvHttpProxyAgent()
|
|
90
|
+
: undefined;
|
|
91
|
+
if (!egress && !forward)
|
|
92
|
+
return fetch;
|
|
93
|
+
return (input, init) => {
|
|
94
|
+
const hostname = new URL(toUrlString(input)).hostname.toLowerCase();
|
|
95
|
+
const useEgress = !!egress && egressHosts.size > 0 && egressHosts.has(hostname);
|
|
96
|
+
const dispatcher = useEgress ? egress : forward;
|
|
97
|
+
if (!dispatcher)
|
|
98
|
+
return fetch(input, init);
|
|
119
99
|
let target;
|
|
120
100
|
let opts;
|
|
121
101
|
if (typeof Request !== "undefined" && input instanceof Request) {
|
|
@@ -130,7 +110,6 @@ export function createProxyFetch(options = {}) {
|
|
|
130
110
|
...init,
|
|
131
111
|
dispatcher,
|
|
132
112
|
};
|
|
133
|
-
// A streamed request body requires half-duplex in undici.
|
|
134
113
|
if (input.body != null)
|
|
135
114
|
opts.duplex = "half";
|
|
136
115
|
}
|
|
@@ -138,35 +117,18 @@ export function createProxyFetch(options = {}) {
|
|
|
138
117
|
target = input;
|
|
139
118
|
opts = { ...init, dispatcher };
|
|
140
119
|
}
|
|
141
|
-
|
|
142
|
-
|
|
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
|
-
}
|
|
120
|
+
if (useEgress && egressIsPlainHttp) {
|
|
121
|
+
target = downgradeToHttp(target);
|
|
150
122
|
}
|
|
151
123
|
return undiciFetch(target, opts);
|
|
152
124
|
};
|
|
153
|
-
return proxied;
|
|
154
125
|
}
|
|
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
126
|
export function installProxyFetch(options = {}) {
|
|
164
|
-
const
|
|
165
|
-
if (
|
|
127
|
+
const proxied = createProxyFetch(options);
|
|
128
|
+
if (proxied === fetch)
|
|
166
129
|
return () => { };
|
|
167
|
-
}
|
|
168
130
|
const original = globalThis.fetch;
|
|
169
|
-
globalThis.fetch =
|
|
131
|
+
globalThis.fetch = proxied;
|
|
170
132
|
return () => {
|
|
171
133
|
globalThis.fetch = original;
|
|
172
134
|
};
|
package/dist/proxy.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"proxy.js","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"proxy.js","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,UAAU,CAAC;AACjD,OAAO,EACL,KAAK,EACL,iBAAiB,EAEjB,KAAK,IAAI,WAAW,GAErB,MAAM,QAAQ,CAAC;AAQhB,SAAS,gBAAgB,CAAC,OAA0B;IAClD,OAAO,OAAO,CAAC,cAAc,IAAI,OAAO,CAAC,GAAG,CAAC,wBAAwB,CAAC;AACxE,CAAC;AAED,SAAS,kBAAkB,CAAC,OAA0B;IACpD,MAAM,GAAG,GACP,OAAO,CAAC,gBAAgB,IAAI,OAAO,CAAC,GAAG,CAAC,4BAA4B,IAAI,EAAE,CAAC;IAC7E,OAAO,IAAI,GAAG,CACZ,GAAG;SACA,KAAK,CAAC,GAAG,CAAC;SACV,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;SAClC,MAAM,CAAC,OAAO,CAAC,CACnB,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,sBAAsB;IACpC,OAAO,CACL,OAAO,CAAC,GAAG,CAAC,WAAW;QACvB,OAAO,CAAC,GAAG,CAAC,WAAW;QACvB,OAAO,CAAC,GAAG,CAAC,UAAU;QACtB,OAAO,CAAC,GAAG,CAAC,UAAU;QACtB,SAAS,CACV,CAAC;AACJ,CAAC;AAED,SAAS,qBAAqB,CAAC,QAAgB;IAC7C,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC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AAS,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.
|
|
3
|
+
"version": "0.4.8",
|
|
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
|
-
"undici": "^8.
|
|
19
|
+
"undici": "^8.4.0"
|
|
27
20
|
},
|
|
28
21
|
"devDependencies": {
|
|
29
|
-
"@
|
|
30
|
-
"@repo/typescript-config": "workspace:*",
|
|
31
|
-
"@types/node": "^22.19.19",
|
|
22
|
+
"@types/node": "^22.19.20",
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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, egress is disabled and all traffic uses the forward proxy.
|
|
10
11
|
*
|
|
11
|
-
* - **
|
|
12
|
-
*
|
|
13
|
-
*
|
|
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
|
|
69
|
-
return options.egressProxyUrl ?? process.env.
|
|
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,62 @@ function buildEgressDispatcher(proxyUrl: string): Dispatcher {
|
|
|
113
79
|
}
|
|
114
80
|
|
|
115
81
|
/**
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
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 =
|
|
124
|
-
|
|
89
|
+
const egressUrl = resolveEgressUrl(options);
|
|
90
|
+
const egressHostSet = resolveEgressHosts(options);
|
|
91
|
+
if (egressUrl && egressHostSet.size > 0) {
|
|
125
92
|
return buildEgressDispatcher(egressUrl);
|
|
126
93
|
}
|
|
127
|
-
|
|
128
|
-
if (options.forwardProxyUrl) {
|
|
129
|
-
return new ProxyAgent(options.forwardProxyUrl);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
94
|
if (resolveForwardProxyUrl()) {
|
|
133
|
-
// Reads HTTPS_PROXY / HTTP_PROXY / NO_PROXY from the environment.
|
|
134
95
|
return new EnvHttpProxyAgent();
|
|
135
96
|
}
|
|
136
|
-
|
|
137
97
|
return undefined;
|
|
138
98
|
}
|
|
139
99
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
100
|
+
function toUrlString(input: RequestInfo | URL): string {
|
|
101
|
+
if (typeof input === "string") return input;
|
|
102
|
+
if (input instanceof URL) return input.toString();
|
|
103
|
+
return (input as Request).url;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function downgradeToHttp(url: string | URL): string {
|
|
107
|
+
const s = typeof url === "string" ? url : url.toString();
|
|
108
|
+
return s.startsWith("https://") ? "http://" + s.slice(8) : s;
|
|
109
|
+
}
|
|
110
|
+
|
|
148
111
|
export function createProxyFetch(
|
|
149
112
|
options: ProxyFetchOptions = {},
|
|
150
113
|
): typeof fetch {
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
return fetch;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const egressUrl = resolveEgressProxyUrl(options);
|
|
114
|
+
const egressUrl = resolveEgressUrl(options);
|
|
115
|
+
const egressHosts = resolveEgressHosts(options);
|
|
157
116
|
const egressIsPlainHttp =
|
|
158
117
|
!!egressUrl && new URL(egressUrl).protocol === "http:";
|
|
159
118
|
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
119
|
+
const egress = egressUrl ? buildEgressDispatcher(egressUrl) : undefined;
|
|
120
|
+
const forward = resolveForwardProxyUrl()
|
|
121
|
+
? new EnvHttpProxyAgent()
|
|
122
|
+
: undefined;
|
|
123
|
+
|
|
124
|
+
if (!egress && !forward) return fetch;
|
|
125
|
+
|
|
126
|
+
return (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
|
|
127
|
+
const hostname = new URL(toUrlString(input)).hostname.toLowerCase();
|
|
128
|
+
|
|
129
|
+
const useEgress =
|
|
130
|
+
!!egress && egressHosts.size > 0 && egressHosts.has(hostname);
|
|
131
|
+
const dispatcher = useEgress ? egress : forward;
|
|
132
|
+
|
|
133
|
+
if (!dispatcher) return fetch(input, init);
|
|
134
|
+
|
|
168
135
|
let target: string | URL;
|
|
169
136
|
let opts: Record<string, unknown>;
|
|
137
|
+
|
|
170
138
|
if (typeof Request !== "undefined" && input instanceof Request) {
|
|
171
139
|
target = input.url;
|
|
172
140
|
opts = {
|
|
@@ -179,22 +147,14 @@ export function createProxyFetch(
|
|
|
179
147
|
...init,
|
|
180
148
|
dispatcher,
|
|
181
149
|
};
|
|
182
|
-
// A streamed request body requires half-duplex in undici.
|
|
183
150
|
if (input.body != null) opts.duplex = "half";
|
|
184
151
|
} else {
|
|
185
152
|
target = input as string | URL;
|
|
186
153
|
opts = { ...init, dispatcher };
|
|
187
154
|
}
|
|
188
155
|
|
|
189
|
-
|
|
190
|
-
|
|
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
|
-
}
|
|
156
|
+
if (useEgress && egressIsPlainHttp) {
|
|
157
|
+
target = downgradeToHttp(target);
|
|
198
158
|
}
|
|
199
159
|
|
|
200
160
|
return undiciFetch(
|
|
@@ -202,26 +162,13 @@ export function createProxyFetch(
|
|
|
202
162
|
opts as unknown as Parameters<typeof undiciFetch>[1],
|
|
203
163
|
) as unknown as Promise<Response>;
|
|
204
164
|
};
|
|
205
|
-
|
|
206
|
-
return proxied as typeof fetch;
|
|
207
165
|
}
|
|
208
166
|
|
|
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
167
|
export function installProxyFetch(options: ProxyFetchOptions = {}): () => void {
|
|
218
|
-
const
|
|
219
|
-
if (
|
|
220
|
-
return () => {};
|
|
221
|
-
}
|
|
222
|
-
|
|
168
|
+
const proxied = createProxyFetch(options);
|
|
169
|
+
if (proxied === fetch) return () => {};
|
|
223
170
|
const original = globalThis.fetch;
|
|
224
|
-
globalThis.fetch =
|
|
171
|
+
globalThis.fetch = proxied;
|
|
225
172
|
return () => {
|
|
226
173
|
globalThis.fetch = original;
|
|
227
174
|
};
|
|
Binary file
|