abelworkflow 1.1.0 → 1.1.2

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,188 +0,0 @@
1
- export const piInsecureTlsHeader = "x-abelworkflow-insecure-tls";
2
-
3
- const installedFetchMarker = Symbol.for("abelworkflow.pi-provider-tls-fetch");
4
- const undiciGlobalDispatchers = [
5
- Symbol.for("undici.globalDispatcher.2"),
6
- Symbol.for("undici.globalDispatcher.1")
7
- ];
8
- const redirectStatuses = new Set([301, 302, 303, 307, 308]);
9
- const maxRedirects = 5;
10
-
11
- function getMarkedHeaders(input, init) {
12
- const source = init?.headers !== undefined
13
- ? init.headers
14
- : typeof Request !== "undefined" && input instanceof Request
15
- ? input.headers
16
- : undefined;
17
- if (source === undefined) return null;
18
-
19
- const headers = new Headers(source);
20
- if (!headers.has(piInsecureTlsHeader)) return null;
21
- const allowedOrigin = headers.get(piInsecureTlsHeader);
22
- headers.delete(piInsecureTlsHeader);
23
- return { allowedOrigin, headers };
24
- }
25
-
26
- function getRequestUrl(input) {
27
- return typeof Request !== "undefined" && input instanceof Request
28
- ? input.url
29
- : input instanceof URL
30
- ? input.href
31
- : input;
32
- }
33
-
34
- function getRequestOrigin(input) {
35
- try {
36
- return new URL(getRequestUrl(input)).origin;
37
- } catch {
38
- return null;
39
- }
40
- }
41
-
42
- function redirectRequestInit(input, init, status) {
43
- const method = String(init.method ?? (
44
- typeof Request !== "undefined" && input instanceof Request ? input.method : "GET"
45
- )).toUpperCase();
46
- const switchToGet = status === 303 && method !== "HEAD"
47
- || (status === 301 || status === 302) && method === "POST";
48
- if (!switchToGet) {
49
- if (typeof Request !== "undefined" && input instanceof Request && method !== "GET" && method !== "HEAD") {
50
- throw new Error("Pi insecure TLS cannot safely replay a redirected Request body");
51
- }
52
- return init;
53
- }
54
-
55
- const headers = new Headers(init.headers);
56
- for (const name of ["content-encoding", "content-language", "content-length", "content-location", "content-type"]) {
57
- headers.delete(name);
58
- }
59
- const nextInit = { ...init, method: "GET", headers };
60
- delete nextInit.body;
61
- return nextInit;
62
- }
63
-
64
- async function fetchWithSameOriginRedirects({ fetchOnce, input, init, allowedOrigin }) {
65
- let nextInput = input;
66
- let nextInit = init;
67
-
68
- for (let redirectCount = 0; redirectCount <= maxRedirects; redirectCount += 1) {
69
- const response = await fetchOnce(nextInput, nextInit);
70
- const location = redirectStatuses.has(response?.status) ? response.headers?.get?.("location") : null;
71
- if (!location) return response;
72
- if (redirectCount === maxRedirects) {
73
- await response.body?.cancel?.();
74
- throw new Error(`Pi insecure TLS exceeded ${maxRedirects} same-origin redirects`);
75
- }
76
-
77
- const target = new URL(location, getRequestUrl(nextInput));
78
- if (target.origin !== allowedOrigin) {
79
- await response.body?.cancel?.();
80
- throw new Error(`Pi insecure TLS blocked cross-origin redirect to ${target.origin}`);
81
- }
82
- await response.body?.cancel?.();
83
- nextInit = redirectRequestInit(nextInput, nextInit, response.status);
84
- nextInput = target.href;
85
- }
86
- }
87
-
88
- function applyNodeDispatcher(fetchImpl, input, init, dispatcherSource) {
89
- let dispatcher;
90
- try {
91
- dispatcher = typeof dispatcherSource === "function" ? dispatcherSource() : dispatcherSource;
92
- } catch (error) {
93
- return Promise.reject(error);
94
- }
95
-
96
- if (dispatcher && typeof dispatcher.then === "function") {
97
- return dispatcher.then((resolved) => applyNodeDispatcher(fetchImpl, input, init, resolved));
98
- }
99
- if (!dispatcher || typeof dispatcher.dispatch !== "function") {
100
- return Promise.reject(new Error("Pi insecure TLS requires an injected Undici dispatcher"));
101
- }
102
- return fetchImpl(input, { ...init, dispatcher });
103
- }
104
-
105
- function getUndiciDispatcherConstructor(target) {
106
- return undiciGlobalDispatchers
107
- .map((symbol) => target[symbol]?.constructor)
108
- .find((constructor) => typeof constructor === "function");
109
- }
110
-
111
- function initializeUndiciGlobalDispatcher(target) {
112
- if (typeof target.fetch !== "function") return;
113
- try {
114
- void Promise.resolve(target.fetch("data:,")).catch(() => {});
115
- } catch {
116
- }
117
- }
118
-
119
- export function createNodeInsecureDispatcher(target = globalThis) {
120
- let Dispatcher = getUndiciDispatcherConstructor(target);
121
- if (typeof Dispatcher !== "function") {
122
- initializeUndiciGlobalDispatcher(target);
123
- Dispatcher = getUndiciDispatcherConstructor(target);
124
- }
125
- if (typeof Dispatcher !== "function") {
126
- throw new Error("Pi insecure TLS requires the active Undici dispatcher");
127
- }
128
- return new Dispatcher({
129
- allowH2: false,
130
- connect: { rejectUnauthorized: false },
131
- requestTls: { rejectUnauthorized: false }
132
- });
133
- }
134
-
135
- export function createProviderTlsFetch({
136
- fetchImpl = globalThis.fetch,
137
- runtime = typeof globalThis.Bun === "undefined" ? "node" : "bun",
138
- insecureDispatcher
139
- } = {}) {
140
- if (typeof fetchImpl !== "function") {
141
- throw new TypeError("A fetch implementation is required");
142
- }
143
-
144
- return function providerTlsFetch(input, init) {
145
- const marked = getMarkedHeaders(input, init);
146
- if (!marked) return fetchImpl(input, init);
147
-
148
- const nextInit = { ...(init ?? {}), headers: marked.headers };
149
- if (!marked.allowedOrigin || getRequestOrigin(input) !== marked.allowedOrigin) {
150
- return fetchImpl(input, nextInit);
151
- }
152
-
153
- nextInit.redirect = "manual";
154
- const fetchOnce = runtime === "bun"
155
- ? (nextInput, redirectInit) => {
156
- const tls = redirectInit.tls && typeof redirectInit.tls === "object" ? redirectInit.tls : {};
157
- return fetchImpl(nextInput, {
158
- ...redirectInit,
159
- tls: { ...tls, rejectUnauthorized: false }
160
- });
161
- }
162
- : (nextInput, redirectInit) => applyNodeDispatcher(
163
- fetchImpl,
164
- nextInput,
165
- redirectInit,
166
- insecureDispatcher
167
- );
168
-
169
- return fetchWithSameOriginRedirects({
170
- fetchOnce,
171
- input,
172
- init: nextInit,
173
- allowedOrigin: marked.allowedOrigin
174
- });
175
- };
176
- }
177
-
178
- export function installProviderTlsFetch({ target = globalThis, ...options } = {}) {
179
- if (target.fetch?.[installedFetchMarker]) return target.fetch;
180
-
181
- const wrapped = createProviderTlsFetch({
182
- ...options,
183
- fetchImpl: target.fetch.bind(target)
184
- });
185
- Object.defineProperty(wrapped, installedFetchMarker, { value: true });
186
- target.fetch = wrapped;
187
- return wrapped;
188
- }