@introspection-sdk/introspection-proxy 0.1.0
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 -0
- package/LICENSE +191 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/proxy.d.ts +51 -0
- package/dist/proxy.d.ts.map +1 -0
- package/dist/proxy.js +162 -0
- package/dist/proxy.js.map +1 -0
- package/eslint.config.mjs +4 -0
- package/package.json +34 -0
- package/src/index.ts +17 -0
- package/src/proxy.ts +212 -0
- package/tsconfig.json +8 -0
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/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@introspection-sdk/introspection-proxy`
|
|
3
|
+
*
|
|
4
|
+
* Lightweight, dependency-minimal (undici only) helpers for routing
|
|
5
|
+
* `fetch`-based clients through the Introspection egress credential-injection
|
|
6
|
+
* proxy or a standard forward proxy. Has no OpenTelemetry or provider-SDK
|
|
7
|
+
* dependencies, so it can be used standalone (e.g. alongside the
|
|
8
|
+
* `introspection-pi` instrumentation wrapper) without pulling in the full
|
|
9
|
+
* `introspection-node` surface.
|
|
10
|
+
*/
|
|
11
|
+
export { getProxyDispatcher, createProxyFetch, installProxyFetch, resolveForwardProxyUrl, } from "./proxy.js";
|
|
12
|
+
export type { ProxyFetchOptions } from "./proxy.js";
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,iBAAiB,EACjB,sBAAsB,GACvB,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@introspection-sdk/introspection-proxy`
|
|
3
|
+
*
|
|
4
|
+
* Lightweight, dependency-minimal (undici only) helpers for routing
|
|
5
|
+
* `fetch`-based clients through the Introspection egress credential-injection
|
|
6
|
+
* proxy or a standard forward proxy. Has no OpenTelemetry or provider-SDK
|
|
7
|
+
* dependencies, so it can be used standalone (e.g. alongside the
|
|
8
|
+
* `introspection-pi` instrumentation wrapper) without pulling in the full
|
|
9
|
+
* `introspection-node` surface.
|
|
10
|
+
*/
|
|
11
|
+
export { getProxyDispatcher, createProxyFetch, installProxyFetch, resolveForwardProxyUrl, } from "./proxy.js";
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,iBAAiB,EACjB,sBAAsB,GACvB,MAAM,YAAY,CAAC"}
|
package/dist/proxy.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type Dispatcher } from "undici";
|
|
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
|
+
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
|
+
*/
|
|
18
|
+
forwardProxyUrl?: string;
|
|
19
|
+
}
|
|
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
|
+
export declare function resolveForwardProxyUrl(): string | undefined;
|
|
27
|
+
/**
|
|
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.
|
|
31
|
+
*/
|
|
32
|
+
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
|
+
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
|
+
export declare function installProxyFetch(options?: ProxyFetchOptions): () => void;
|
|
51
|
+
//# sourceMappingURL=proxy.d.ts.map
|
|
@@ -0,0 +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,CAyCd;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,GAAE,iBAAsB,GAAG,MAAM,IAAI,CAW7E"}
|
package/dist/proxy.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Outbound HTTP proxy helpers for `fetch`-based clients.
|
|
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.
|
|
8
|
+
*
|
|
9
|
+
* Two proxy shapes are supported, selected from the environment:
|
|
10
|
+
*
|
|
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.
|
|
37
|
+
*/
|
|
38
|
+
import { Socket } from "node:net";
|
|
39
|
+
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;
|
|
43
|
+
}
|
|
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
|
+
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 ||
|
|
56
|
+
undefined);
|
|
57
|
+
}
|
|
58
|
+
function buildEgressDispatcher(proxyUrl) {
|
|
59
|
+
const url = new URL(proxyUrl);
|
|
60
|
+
const useTls = url.protocol === "https:";
|
|
61
|
+
const port = Number(url.port) || (useTls ? 443 : 80);
|
|
62
|
+
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
|
+
return new Agent({
|
|
66
|
+
connect(_options, callback) {
|
|
67
|
+
if (useTls) {
|
|
68
|
+
const socket = tlsConnect({ host, port, servername: host });
|
|
69
|
+
socket.once("secureConnect", () => callback(null, socket));
|
|
70
|
+
socket.once("error", (err) => callback(err, null));
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
const socket = new Socket();
|
|
74
|
+
socket.connect(port, host, () => callback(null, socket));
|
|
75
|
+
socket.once("error", (err) => callback(err, null));
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
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.
|
|
84
|
+
*/
|
|
85
|
+
export function getProxyDispatcher(options = {}) {
|
|
86
|
+
const egressUrl = resolveEgressProxyUrl(options);
|
|
87
|
+
if (egressUrl) {
|
|
88
|
+
return buildEgressDispatcher(egressUrl);
|
|
89
|
+
}
|
|
90
|
+
if (options.forwardProxyUrl) {
|
|
91
|
+
return new ProxyAgent(options.forwardProxyUrl);
|
|
92
|
+
}
|
|
93
|
+
if (resolveForwardProxyUrl()) {
|
|
94
|
+
// Reads HTTPS_PROXY / HTTP_PROXY / NO_PROXY from the environment.
|
|
95
|
+
return new EnvHttpProxyAgent();
|
|
96
|
+
}
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
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
|
+
*/
|
|
107
|
+
export function createProxyFetch(options = {}) {
|
|
108
|
+
const dispatcher = getProxyDispatcher(options);
|
|
109
|
+
if (!dispatcher) {
|
|
110
|
+
return fetch;
|
|
111
|
+
}
|
|
112
|
+
const proxied = (input, init) => {
|
|
113
|
+
// Some clients (notably axios's fetch adapter) call fetch with a `Request`
|
|
114
|
+
// object. undici's fetch can't consume a foreign Request instance (it tries
|
|
115
|
+
// to coerce it to a URL — "Failed to parse URL from [object Request]"), so
|
|
116
|
+
// normalise a Request into (url, init), carrying over its fields.
|
|
117
|
+
let target;
|
|
118
|
+
let opts;
|
|
119
|
+
if (typeof Request !== "undefined" && input instanceof Request) {
|
|
120
|
+
target = input.url;
|
|
121
|
+
opts = {
|
|
122
|
+
method: input.method,
|
|
123
|
+
headers: input.headers,
|
|
124
|
+
body: input.body,
|
|
125
|
+
signal: input.signal,
|
|
126
|
+
redirect: input.redirect,
|
|
127
|
+
credentials: input.credentials,
|
|
128
|
+
...init,
|
|
129
|
+
dispatcher,
|
|
130
|
+
};
|
|
131
|
+
// A streamed request body requires half-duplex in undici.
|
|
132
|
+
if (input.body != null)
|
|
133
|
+
opts.duplex = "half";
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
target = input;
|
|
137
|
+
opts = { ...init, dispatcher };
|
|
138
|
+
}
|
|
139
|
+
return undiciFetch(target, opts);
|
|
140
|
+
};
|
|
141
|
+
return proxied;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Replace `globalThis.fetch` so existing bare `fetch(...)` calls — and any SDK
|
|
145
|
+
* that uses the global fetch by default, including supabase-js — route through
|
|
146
|
+
* the configured proxy with no other code changes. No-op when no proxy is
|
|
147
|
+
* configured.
|
|
148
|
+
*
|
|
149
|
+
* Returns a function that restores the original `fetch`.
|
|
150
|
+
*/
|
|
151
|
+
export function installProxyFetch(options = {}) {
|
|
152
|
+
const dispatcher = getProxyDispatcher(options);
|
|
153
|
+
if (!dispatcher) {
|
|
154
|
+
return () => { };
|
|
155
|
+
}
|
|
156
|
+
const original = globalThis.fetch;
|
|
157
|
+
globalThis.fetch = createProxyFetch(options);
|
|
158
|
+
return () => {
|
|
159
|
+
globalThis.fetch = original;
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
//# sourceMappingURL=proxy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proxy.js","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,UAAU,CAAC;AACjD,OAAO,EACL,KAAK,EACL,iBAAiB,EACjB,UAAU,EAEV,KAAK,IAAI,WAAW,GAErB,MAAM,QAAQ,CAAC;AAqBhB,SAAS,qBAAqB,CAAC,OAA0B;IACvD,OAAO,OAAO,CAAC,cAAc,IAAI,OAAO,CAAC,GAAG,CAAC,gBAAgB,IAAI,SAAS,CAAC;AAC7E,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,sBAAsB;IACpC,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;IACxB,OAAO,CACL,GAAG,CAAC,WAAW;QACf,GAAG,CAAC,WAAW;QACf,GAAG,CAAC,UAAU;QACd,GAAG,CAAC,UAAU;QACd,SAAS,CACV,CAAC;AACJ,CAAC;AAED,SAAS,qBAAqB,CAAC,QAAgB;IAC7C,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC9B,MAAM,MAAM,GAAG,GAAG,CAAC,QAAQ,KAAK,QAAQ,CAAC;IACzC,MAAM,IAAI,GAAG,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IACrD,MAAM,IAAI,GAAG,GAAG,CAAC,QAAQ,CAAC;IAE1B,4EAA4E;IAC5E,8EAA8E;IAC9E,OAAO,IAAI,KAAK,CAAC;QACf,OAAO,CACL,QAAgC,EAChC,QAAiC;YAEjC,IAAI,MAAM,EAAE,CAAC;gBACX,MAAM,MAAM,GAAG,UAAU,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC;gBAC5D,MAAM,CAAC,IAAI,CAAC,eAAe,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC;gBAC3D,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC,CAAC;YACrD,CAAC;iBAAM,CAAC;gBACN,MAAM,MAAM,GAAG,IAAI,MAAM,EAAE,CAAC;gBAC5B,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC;gBACzD,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC,CAAC;YACrD,CAAC;QACH,CAAC;KACF,CAAC,CAAC;AACL,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAChC,UAA6B,EAAE;IAE/B,MAAM,SAAS,GAAG,qBAAqB,CAAC,OAAO,CAAC,CAAC;IACjD,IAAI,SAAS,EAAE,CAAC;QACd,OAAO,qBAAqB,CAAC,SAAS,CAAC,CAAC;IAC1C,CAAC;IAED,IAAI,OAAO,CAAC,eAAe,EAAE,CAAC;QAC5B,OAAO,IAAI,UAAU,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC;IACjD,CAAC;IAED,IAAI,sBAAsB,EAAE,EAAE,CAAC;QAC7B,kEAAkE;QAClE,OAAO,IAAI,iBAAiB,EAAE,CAAC;IACjC,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAC9B,UAA6B,EAAE;IAE/B,MAAM,UAAU,GAAG,kBAAkB,CAAC,OAAO,CAAC,CAAC;IAC/C,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,OAAO,GAAG,CACd,KAAwB,EACxB,IAAkB,EACC,EAAE;QACrB,2EAA2E;QAC3E,4EAA4E;QAC5E,2EAA2E;QAC3E,kEAAkE;QAClE,IAAI,MAAoB,CAAC;QACzB,IAAI,IAA6B,CAAC;QAClC,IAAI,OAAO,OAAO,KAAK,WAAW,IAAI,KAAK,YAAY,OAAO,EAAE,CAAC;YAC/D,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC;YACnB,IAAI,GAAG;gBACL,MAAM,EAAE,KAAK,CAAC,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,0DAA0D;YAC1D,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;QACD,OAAO,WAAW,CAChB,MAAsD,EACtD,IAAoD,CACrB,CAAC;IACpC,CAAC,CAAC;IAEF,OAAO,OAAuB,CAAC;AACjC,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,iBAAiB,CAAC,UAA6B,EAAE;IAC/D,MAAM,UAAU,GAAG,kBAAkB,CAAC,OAAO,CAAC,CAAC;IAC/C,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,OAAO,GAAG,EAAE,GAAE,CAAC,CAAC;IAClB,CAAC;IAED,MAAM,QAAQ,GAAG,UAAU,CAAC,KAAK,CAAC;IAClC,UAAU,CAAC,KAAK,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAC7C,OAAO,GAAG,EAAE;QACV,UAAU,CAAC,KAAK,GAAG,QAAQ,CAAC;IAC9B,CAAC,CAAC;AACJ,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@introspection-sdk/introspection-proxy",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"description": "Outbound HTTP proxy helpers for routing fetch-based clients through the Introspection egress proxy or a standard forward proxy",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=22.19.0"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"undici": "^8.3.0"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/node": "^22.19.19",
|
|
23
|
+
"eslint": "^9.39.4",
|
|
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"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@introspection-sdk/introspection-proxy`
|
|
3
|
+
*
|
|
4
|
+
* Lightweight, dependency-minimal (undici only) helpers for routing
|
|
5
|
+
* `fetch`-based clients through the Introspection egress credential-injection
|
|
6
|
+
* proxy or a standard forward proxy. Has no OpenTelemetry or provider-SDK
|
|
7
|
+
* dependencies, so it can be used standalone (e.g. alongside the
|
|
8
|
+
* `introspection-pi` instrumentation wrapper) without pulling in the full
|
|
9
|
+
* `introspection-node` surface.
|
|
10
|
+
*/
|
|
11
|
+
export {
|
|
12
|
+
getProxyDispatcher,
|
|
13
|
+
createProxyFetch,
|
|
14
|
+
installProxyFetch,
|
|
15
|
+
resolveForwardProxyUrl,
|
|
16
|
+
} from "./proxy.js";
|
|
17
|
+
export type { ProxyFetchOptions } from "./proxy.js";
|
package/src/proxy.ts
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Outbound HTTP proxy helpers for `fetch`-based clients.
|
|
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.
|
|
8
|
+
*
|
|
9
|
+
* Two proxy shapes are supported, selected from the environment:
|
|
10
|
+
*
|
|
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.
|
|
37
|
+
*/
|
|
38
|
+
import { Socket } from "node:net";
|
|
39
|
+
import { connect as tlsConnect } from "node:tls";
|
|
40
|
+
import {
|
|
41
|
+
Agent,
|
|
42
|
+
EnvHttpProxyAgent,
|
|
43
|
+
ProxyAgent,
|
|
44
|
+
buildConnector,
|
|
45
|
+
fetch as undiciFetch,
|
|
46
|
+
type Dispatcher,
|
|
47
|
+
} from "undici";
|
|
48
|
+
|
|
49
|
+
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
|
+
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
|
+
*/
|
|
65
|
+
forwardProxyUrl?: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function resolveEgressProxyUrl(options: ProxyFetchOptions): string | undefined {
|
|
69
|
+
return options.egressProxyUrl ?? process.env.EGRESS_PROXY_URL ?? undefined;
|
|
70
|
+
}
|
|
71
|
+
|
|
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
|
+
export function resolveForwardProxyUrl(): string | undefined {
|
|
79
|
+
const env = process.env;
|
|
80
|
+
return (
|
|
81
|
+
env.HTTPS_PROXY ||
|
|
82
|
+
env.https_proxy ||
|
|
83
|
+
env.HTTP_PROXY ||
|
|
84
|
+
env.http_proxy ||
|
|
85
|
+
undefined
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function buildEgressDispatcher(proxyUrl: string): Dispatcher {
|
|
90
|
+
const url = new URL(proxyUrl);
|
|
91
|
+
const useTls = url.protocol === "https:";
|
|
92
|
+
const port = Number(url.port) || (useTls ? 443 : 80);
|
|
93
|
+
const host = url.hostname;
|
|
94
|
+
|
|
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
|
+
return new Agent({
|
|
98
|
+
connect(
|
|
99
|
+
_options: buildConnector.Options,
|
|
100
|
+
callback: buildConnector.Callback,
|
|
101
|
+
): void {
|
|
102
|
+
if (useTls) {
|
|
103
|
+
const socket = tlsConnect({ host, port, servername: host });
|
|
104
|
+
socket.once("secureConnect", () => callback(null, socket));
|
|
105
|
+
socket.once("error", (err) => callback(err, null));
|
|
106
|
+
} else {
|
|
107
|
+
const socket = new Socket();
|
|
108
|
+
socket.connect(port, host, () => callback(null, socket));
|
|
109
|
+
socket.once("error", (err) => callback(err, null));
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
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.
|
|
119
|
+
*/
|
|
120
|
+
export function getProxyDispatcher(
|
|
121
|
+
options: ProxyFetchOptions = {},
|
|
122
|
+
): Dispatcher | undefined {
|
|
123
|
+
const egressUrl = resolveEgressProxyUrl(options);
|
|
124
|
+
if (egressUrl) {
|
|
125
|
+
return buildEgressDispatcher(egressUrl);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (options.forwardProxyUrl) {
|
|
129
|
+
return new ProxyAgent(options.forwardProxyUrl);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (resolveForwardProxyUrl()) {
|
|
133
|
+
// Reads HTTPS_PROXY / HTTP_PROXY / NO_PROXY from the environment.
|
|
134
|
+
return new EnvHttpProxyAgent();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
|
|
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
|
+
*/
|
|
148
|
+
export function createProxyFetch(
|
|
149
|
+
options: ProxyFetchOptions = {},
|
|
150
|
+
): typeof fetch {
|
|
151
|
+
const dispatcher = getProxyDispatcher(options);
|
|
152
|
+
if (!dispatcher) {
|
|
153
|
+
return fetch;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const proxied = (
|
|
157
|
+
input: RequestInfo | URL,
|
|
158
|
+
init?: RequestInit,
|
|
159
|
+
): Promise<Response> => {
|
|
160
|
+
// Some clients (notably axios's fetch adapter) call fetch with a `Request`
|
|
161
|
+
// object. undici's fetch can't consume a foreign Request instance (it tries
|
|
162
|
+
// to coerce it to a URL — "Failed to parse URL from [object Request]"), so
|
|
163
|
+
// normalise a Request into (url, init), carrying over its fields.
|
|
164
|
+
let target: string | URL;
|
|
165
|
+
let opts: Record<string, unknown>;
|
|
166
|
+
if (typeof Request !== "undefined" && input instanceof Request) {
|
|
167
|
+
target = input.url;
|
|
168
|
+
opts = {
|
|
169
|
+
method: input.method,
|
|
170
|
+
headers: input.headers,
|
|
171
|
+
body: input.body,
|
|
172
|
+
signal: input.signal,
|
|
173
|
+
redirect: input.redirect,
|
|
174
|
+
credentials: input.credentials,
|
|
175
|
+
...init,
|
|
176
|
+
dispatcher,
|
|
177
|
+
};
|
|
178
|
+
// A streamed request body requires half-duplex in undici.
|
|
179
|
+
if (input.body != null) opts.duplex = "half";
|
|
180
|
+
} else {
|
|
181
|
+
target = input as string | URL;
|
|
182
|
+
opts = { ...init, dispatcher };
|
|
183
|
+
}
|
|
184
|
+
return undiciFetch(
|
|
185
|
+
target as unknown as Parameters<typeof undiciFetch>[0],
|
|
186
|
+
opts as unknown as Parameters<typeof undiciFetch>[1],
|
|
187
|
+
) as unknown as Promise<Response>;
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
return proxied as typeof fetch;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Replace `globalThis.fetch` so existing bare `fetch(...)` calls — and any SDK
|
|
195
|
+
* that uses the global fetch by default, including supabase-js — route through
|
|
196
|
+
* the configured proxy with no other code changes. No-op when no proxy is
|
|
197
|
+
* configured.
|
|
198
|
+
*
|
|
199
|
+
* Returns a function that restores the original `fetch`.
|
|
200
|
+
*/
|
|
201
|
+
export function installProxyFetch(options: ProxyFetchOptions = {}): () => void {
|
|
202
|
+
const dispatcher = getProxyDispatcher(options);
|
|
203
|
+
if (!dispatcher) {
|
|
204
|
+
return () => {};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const original = globalThis.fetch;
|
|
208
|
+
globalThis.fetch = createProxyFetch(options);
|
|
209
|
+
return () => {
|
|
210
|
+
globalThis.fetch = original;
|
|
211
|
+
};
|
|
212
|
+
}
|