@voltro/integration-http 0.85.0 → 0.87.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.
@@ -1,6 +1,6 @@
1
1
  # Third-Party Notices — @voltro/integration-http
2
2
 
3
- This package depends on the open-source software listed below. Each is the
3
+ This package uses the open-source software listed below. Each is the
4
4
  property of its respective copyright holders and is used under the terms of
5
5
  its license. This file is provided for attribution; it grants no rights in
6
6
  @voltro/integration-http itself, which is proprietary (see LICENSE).
package/dist/client.js ADDED
@@ -0,0 +1,32 @@
1
+ import { DEFAULT_POLICY as e, request as t, requestJson as n } from "./core.js";
2
+ //#region src/client.ts
3
+ var r = (r) => {
4
+ let i = {
5
+ ...e,
6
+ ...r.policy
7
+ }, a = (e, t, n) => {
8
+ let a = n?.ctx ?? void 0, o = n?.body;
9
+ return {
10
+ fetchImpl: r.fetchImpl,
11
+ baseUrl: r.baseUrl,
12
+ method: e,
13
+ path: t,
14
+ headers: r.auth(a),
15
+ ...o === void 0 ? {} : { body: JSON.stringify(o) },
16
+ policy: i,
17
+ makeError: r.makeError,
18
+ ...r.now ? { now: r.now } : {}
19
+ };
20
+ }, o = (e, n, r) => t(a(e, n, r)), s = (e, t, r) => n(a(e, t, r));
21
+ return {
22
+ get: (e, t) => o("GET", e, t),
23
+ getJson: (e, t) => s("GET", e, t),
24
+ postJson: (e, t) => s("POST", e, t),
25
+ putJson: (e, t) => s("PUT", e, t),
26
+ deleteJson: (e, t) => s("DELETE", e, t),
27
+ request: (e, t, n) => o(e, t, n),
28
+ policy: i
29
+ };
30
+ };
31
+ //#endregion
32
+ export { r as makeHttpClient };
package/dist/core.js ADDED
@@ -0,0 +1,175 @@
1
+ import { Duration as e, Effect as t, Metric as n } from "effect";
2
+ //#region src/core.ts
3
+ var r = /* @__PURE__ */ new Set([
4
+ 408,
5
+ 425,
6
+ 429,
7
+ 500,
8
+ 502,
9
+ 503,
10
+ 504
11
+ ]), i = [
12
+ "GET",
13
+ "PUT",
14
+ "DELETE"
15
+ ], a = {
16
+ ratio: .2,
17
+ minRetriesPerWindow: 10,
18
+ windowMs: 1e4
19
+ }, o = {
20
+ maxAttempts: 4,
21
+ initialDelayMs: 500,
22
+ maxDelayMs: 1e4,
23
+ timeoutMs: 2e4,
24
+ honourRetryAfter: !0,
25
+ retryMethods: i,
26
+ retryBudget: a
27
+ }, s = /* @__PURE__ */ new Map(), c = (e, t, n) => {
28
+ let r = t - n;
29
+ for (; e.head < e.events.length && e.events[e.head][0] < r;) e.events[e.head][1] === 0 ? --e.requests : --e.retries, e.head += 1;
30
+ e.head > 0 && e.head * 2 >= e.events.length && (e.events = e.events.slice(e.head), e.head = 0);
31
+ }, l = (e) => {
32
+ let t = s.get(e);
33
+ return t === void 0 && (t = {
34
+ events: [],
35
+ head: 0,
36
+ requests: 0,
37
+ retries: 0
38
+ }, s.set(e, t)), t;
39
+ }, u = (e) => ({
40
+ requests: e.requests,
41
+ retries: e.retries
42
+ }), d = (e, t, n) => {
43
+ let r = l(e);
44
+ c(r, t, n.windowMs), r.events.push([t, 0]), r.requests += 1;
45
+ }, f = n.counter("voltro_http_retries_total"), p = n.counter("voltro_http_retry_budget_exhausted_total"), m = (e, r, i) => {
46
+ let a = l(e);
47
+ c(a, r, i.windowMs);
48
+ let { requests: o, retries: s } = u(a);
49
+ return s >= Math.max(i.minRetriesPerWindow, Math.floor(o * i.ratio)) ? (t.runSync(n.update(n.withAttributes(p, { host: e }), 1)), !1) : (a.events.push([r, 1]), a.retries += 1, t.runSync(n.update(n.withAttributes(f, { host: e }), 1)), !0);
50
+ }, h = (e, t = Date.now(), n = a.windowMs) => {
51
+ let r = s.get(e);
52
+ return r === void 0 ? {
53
+ requests: 0,
54
+ retries: 0
55
+ } : (c(r, t, n), u(r));
56
+ }, g = () => {
57
+ s.clear();
58
+ }, _ = (e, t) => Math.min(t.initialDelayMs * 2 ** (e - 1), t.maxDelayMs), v = (e, t) => {
59
+ if (!e) return null;
60
+ let n = Number(e);
61
+ if (Number.isFinite(n)) return Math.max(0, n * 1e3);
62
+ let r = Date.parse(e);
63
+ return Number.isNaN(r) ? null : Math.max(0, r - t);
64
+ }, y = (e, t) => {
65
+ try {
66
+ return new URL(e).host === new URL(t).host;
67
+ } catch {
68
+ return !1;
69
+ }
70
+ }, b = (e, t) => e === 127 || e === 10 || e === 172 && t >= 16 && t <= 31 || e === 192 && t === 168 || e === 169 && t === 254 || e === 0 || e === 100 && t >= 64 && t <= 127 || e >= 224, x = (e) => {
71
+ let t;
72
+ try {
73
+ t = new URL(e);
74
+ } catch {
75
+ throw Error("SSRF: malformed url");
76
+ }
77
+ if (t.protocol !== "http:" && t.protocol !== "https:") throw Error(`SSRF: blocked url scheme "${t.protocol}"`);
78
+ let n = t.hostname.replace(/^\[|\]$/g, "").toLowerCase();
79
+ if (n === "localhost" || n.endsWith(".localhost") || n.endsWith(".internal") || n.endsWith(".local")) throw Error(`SSRF: blocked host "${n}"`);
80
+ let r = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(n);
81
+ if (r) {
82
+ let e = r.slice(1).map(Number);
83
+ if (e.some((e) => e > 255) || b(e[0], e[1])) throw Error(`SSRF: blocked address "${n}"`);
84
+ } else if (n.includes(":") && (n === "::1" || n === "::" || n.startsWith("::ffff:") || /^fe[89ab]/.test(n) || n.startsWith("fc") || n.startsWith("fd"))) throw Error(`SSRF: blocked address "${n}"`);
85
+ }, S = (e) => typeof e == "object" && !!e && e._transient === !0, C = (n) => {
86
+ let { fetchImpl: o, baseUrl: s, method: c, path: l, headers: u, body: f, policy: p, makeError: h } = n, g = n.now ?? Date.now, b = `${s.replace(/\/$/, "")}${l.startsWith("/") ? l : `/${l}`}`;
87
+ if (!y(s, b)) return t.fail(h({
88
+ message: `blocked off-host request to ${b}`,
89
+ transient: !1
90
+ }));
91
+ let x = t.tryPromise({
92
+ try: async (e) => {
93
+ let t = await o(b, {
94
+ method: c,
95
+ headers: u,
96
+ ...f === void 0 ? {} : { body: f },
97
+ signal: e
98
+ }), n = new Uint8Array(await t.arrayBuffer()), i = t.headers.get("content-type"), a = () => new TextDecoder().decode(n);
99
+ if (t.status >= 200 && t.status < 300) return {
100
+ status: t.status,
101
+ bytes: n,
102
+ contentType: i,
103
+ text: a
104
+ };
105
+ throw t.status === 401 ? h({
106
+ status: 401,
107
+ message: "unauthorized — the upstream refused this credential (401). The reason is not in the response: expired, revoked, insufficient scope and malformed are all this status.",
108
+ transient: !1,
109
+ code: "unauthorized"
110
+ }) : r.has(t.status) ? {
111
+ _transient: !0,
112
+ status: t.status,
113
+ message: `HTTP ${t.status}: ${a().slice(0, 500)}`,
114
+ retryAfterMs: p.honourRetryAfter ? v(t.headers.get("retry-after"), g()) : null
115
+ } : h({
116
+ status: t.status,
117
+ message: `HTTP ${t.status}: ${a().slice(0, 500)}`,
118
+ transient: !1
119
+ });
120
+ },
121
+ catch: (e) => S(e) || typeof e == "object" && e && "_tag" in e ? e : {
122
+ _transient: !0,
123
+ message: `network error: ${String(e)}`,
124
+ retryAfterMs: null
125
+ }
126
+ }).pipe(t.timeoutOrElse({
127
+ duration: e.millis(p.timeoutMs),
128
+ orElse: () => t.fail({
129
+ _transient: !0,
130
+ message: `request timed out after ${p.timeoutMs}ms`,
131
+ retryAfterMs: null
132
+ })
133
+ })), C = n.retry ?? (p.retryMethods ?? i).includes(c), w = p.retryBudget === void 0 ? a : p.retryBudget, T = (() => {
134
+ try {
135
+ return new URL(s).host;
136
+ } catch {
137
+ return "invalid";
138
+ }
139
+ })(), E = (e, n) => t.fail(h({
140
+ message: `${e.message} (not retried: ${n})`,
141
+ transient: !0,
142
+ ...e.status === void 0 ? {} : { status: e.status }
143
+ })), D = (n) => x.pipe(t.catch((r) => {
144
+ if (S(r) && n < p.maxAttempts && !C) return E(r, `${c} is not idempotent — pass \`retry: true\` for a call you have made so`);
145
+ if (S(r) && n < p.maxAttempts && w !== !1 && !m(T, g(), w)) return E(r, `the retry budget for ${T} is exhausted`);
146
+ if (S(r) && n < p.maxAttempts) {
147
+ let i = r.retryAfterMs ?? _(n, p), a = r.retryAfterMs == null ? i / 2 + i / 2 * Math.random() : i, o = Math.min(a, p.maxDelayMs);
148
+ return t.sleep(e.millis(o)).pipe(t.flatMap(() => D(n + 1)));
149
+ }
150
+ return S(r) ? t.fail(h({
151
+ message: r.message,
152
+ transient: !0,
153
+ ...r.status === void 0 ? {} : { status: r.status }
154
+ })) : t.fail(r);
155
+ })), O = (() => {
156
+ try {
157
+ return new URL(s).host;
158
+ } catch {
159
+ return "invalid";
160
+ }
161
+ })();
162
+ return t.suspend(() => (w !== !1 && d(T, g(), w), D(1))).pipe(t.withSpan("integration-http.request", { attributes: {
163
+ "http.request.method": c,
164
+ "server.address": O
165
+ } }));
166
+ }, w = (e) => C(e).pipe(t.flatMap((n) => n.bytes.length === 0 ? t.succeed(null) : t.try({
167
+ try: () => JSON.parse(n.text()),
168
+ catch: () => e.makeError({
169
+ status: n.status,
170
+ message: "invalid JSON in response",
171
+ transient: !1
172
+ })
173
+ })));
174
+ //#endregion
175
+ export { o as DEFAULT_POLICY, a as DEFAULT_RETRY_BUDGET, i as DEFAULT_RETRY_METHODS, r as RETRYABLE_STATUS, x as assertPublicUrl, y as assertSameHost, _ as backoffMs, v as parseRetryAfter, C as request, w as requestJson, g as resetRetryBudgets, h as retryBudgetSnapshot };
package/dist/index.js CHANGED
@@ -1,204 +1,3 @@
1
- import { Duration as e, Effect as t, Metric as n } from "effect";
2
- //#region src/core.ts
3
- var r = /* @__PURE__ */ new Set([
4
- 408,
5
- 425,
6
- 429,
7
- 500,
8
- 502,
9
- 503,
10
- 504
11
- ]), i = [
12
- "GET",
13
- "PUT",
14
- "DELETE"
15
- ], a = {
16
- ratio: .2,
17
- minRetriesPerWindow: 10,
18
- windowMs: 1e4
19
- }, o = {
20
- maxAttempts: 4,
21
- initialDelayMs: 500,
22
- maxDelayMs: 1e4,
23
- timeoutMs: 2e4,
24
- honourRetryAfter: !0,
25
- retryMethods: i,
26
- retryBudget: a
27
- }, s = /* @__PURE__ */ new Map(), c = (e, t, n) => {
28
- let r = t - n;
29
- for (; e.head < e.events.length && e.events[e.head][0] < r;) e.events[e.head][1] === 0 ? --e.requests : --e.retries, e.head += 1;
30
- e.head > 0 && e.head * 2 >= e.events.length && (e.events = e.events.slice(e.head), e.head = 0);
31
- }, l = (e) => {
32
- let t = s.get(e);
33
- return t === void 0 && (t = {
34
- events: [],
35
- head: 0,
36
- requests: 0,
37
- retries: 0
38
- }, s.set(e, t)), t;
39
- }, u = (e) => ({
40
- requests: e.requests,
41
- retries: e.retries
42
- }), d = (e, t, n) => {
43
- let r = l(e);
44
- c(r, t, n.windowMs), r.events.push([t, 0]), r.requests += 1;
45
- }, f = n.counter("voltro_http_retries_total"), p = n.counter("voltro_http_retry_budget_exhausted_total"), m = (e, r, i) => {
46
- let a = l(e);
47
- c(a, r, i.windowMs);
48
- let { requests: o, retries: s } = u(a);
49
- return s >= Math.max(i.minRetriesPerWindow, Math.floor(o * i.ratio)) ? (t.runSync(n.update(n.withAttributes(p, { host: e }), 1)), !1) : (a.events.push([r, 1]), a.retries += 1, t.runSync(n.update(n.withAttributes(f, { host: e }), 1)), !0);
50
- }, h = (e, t = Date.now(), n = a.windowMs) => {
51
- let r = s.get(e);
52
- return r === void 0 ? {
53
- requests: 0,
54
- retries: 0
55
- } : (c(r, t, n), u(r));
56
- }, g = () => {
57
- s.clear();
58
- }, _ = (e, t) => Math.min(t.initialDelayMs * 2 ** (e - 1), t.maxDelayMs), v = (e, t) => {
59
- if (!e) return null;
60
- let n = Number(e);
61
- if (Number.isFinite(n)) return Math.max(0, n * 1e3);
62
- let r = Date.parse(e);
63
- return Number.isNaN(r) ? null : Math.max(0, r - t);
64
- }, y = (e, t) => {
65
- try {
66
- return new URL(e).host === new URL(t).host;
67
- } catch {
68
- return !1;
69
- }
70
- }, b = (e, t) => e === 127 || e === 10 || e === 172 && t >= 16 && t <= 31 || e === 192 && t === 168 || e === 169 && t === 254 || e === 0 || e === 100 && t >= 64 && t <= 127 || e >= 224, x = (e) => {
71
- let t;
72
- try {
73
- t = new URL(e);
74
- } catch {
75
- throw Error("SSRF: malformed url");
76
- }
77
- if (t.protocol !== "http:" && t.protocol !== "https:") throw Error(`SSRF: blocked url scheme "${t.protocol}"`);
78
- let n = t.hostname.replace(/^\[|\]$/g, "").toLowerCase();
79
- if (n === "localhost" || n.endsWith(".localhost") || n.endsWith(".internal") || n.endsWith(".local")) throw Error(`SSRF: blocked host "${n}"`);
80
- let r = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(n);
81
- if (r) {
82
- let e = r.slice(1).map(Number);
83
- if (e.some((e) => e > 255) || b(e[0], e[1])) throw Error(`SSRF: blocked address "${n}"`);
84
- return;
85
- }
86
- if (n.includes(":") && (n === "::1" || n === "::" || n.startsWith("::ffff:") || /^fe[89ab]/.test(n) || n.startsWith("fc") || n.startsWith("fd"))) throw Error(`SSRF: blocked address "${n}"`);
87
- }, S = (e) => typeof e == "object" && !!e && e._transient === !0, C = (n) => {
88
- let { fetchImpl: o, baseUrl: s, method: c, path: l, headers: u, body: f, policy: p, makeError: h } = n, g = n.now ?? Date.now, b = `${s.replace(/\/$/, "")}${l.startsWith("/") ? l : `/${l}`}`;
89
- if (!y(s, b)) return t.fail(h({
90
- message: `blocked off-host request to ${b}`,
91
- transient: !1
92
- }));
93
- let x = t.tryPromise({
94
- try: async (e) => {
95
- let t = await o(b, {
96
- method: c,
97
- headers: u,
98
- ...f === void 0 ? {} : { body: f },
99
- signal: e
100
- }), n = new Uint8Array(await t.arrayBuffer()), i = t.headers.get("content-type"), a = () => new TextDecoder().decode(n);
101
- if (t.status >= 200 && t.status < 300) return {
102
- status: t.status,
103
- bytes: n,
104
- contentType: i,
105
- text: a
106
- };
107
- throw t.status === 401 ? h({
108
- status: 401,
109
- message: "unauthorized — the upstream refused this credential (401). The reason is not in the response: expired, revoked, insufficient scope and malformed are all this status.",
110
- transient: !1,
111
- code: "unauthorized"
112
- }) : r.has(t.status) ? {
113
- _transient: !0,
114
- status: t.status,
115
- message: `HTTP ${t.status}: ${a().slice(0, 500)}`,
116
- retryAfterMs: p.honourRetryAfter ? v(t.headers.get("retry-after"), g()) : null
117
- } : h({
118
- status: t.status,
119
- message: `HTTP ${t.status}: ${a().slice(0, 500)}`,
120
- transient: !1
121
- });
122
- },
123
- catch: (e) => S(e) || typeof e == "object" && e && "_tag" in e ? e : {
124
- _transient: !0,
125
- message: `network error: ${String(e)}`,
126
- retryAfterMs: null
127
- }
128
- }).pipe(t.timeoutOrElse({
129
- duration: e.millis(p.timeoutMs),
130
- orElse: () => t.fail({
131
- _transient: !0,
132
- message: `request timed out after ${p.timeoutMs}ms`,
133
- retryAfterMs: null
134
- })
135
- })), C = n.retry ?? (p.retryMethods ?? i).includes(c), w = p.retryBudget === void 0 ? a : p.retryBudget, T = (() => {
136
- try {
137
- return new URL(s).host;
138
- } catch {
139
- return "invalid";
140
- }
141
- })(), E = (e, n) => t.fail(h({
142
- message: `${e.message} (not retried: ${n})`,
143
- transient: !0,
144
- ...e.status === void 0 ? {} : { status: e.status }
145
- })), D = (n) => x.pipe(t.catch((r) => {
146
- if (S(r) && n < p.maxAttempts && !C) return E(r, `${c} is not idempotent — pass \`retry: true\` for a call you have made so`);
147
- if (S(r) && n < p.maxAttempts && w !== !1 && !m(T, g(), w)) return E(r, `the retry budget for ${T} is exhausted`);
148
- if (S(r) && n < p.maxAttempts) {
149
- let i = r.retryAfterMs ?? _(n, p), a = r.retryAfterMs == null ? i / 2 + i / 2 * Math.random() : i, o = Math.min(a, p.maxDelayMs);
150
- return t.sleep(e.millis(o)).pipe(t.flatMap(() => D(n + 1)));
151
- }
152
- return S(r) ? t.fail(h({
153
- message: r.message,
154
- transient: !0,
155
- ...r.status === void 0 ? {} : { status: r.status }
156
- })) : t.fail(r);
157
- })), O = (() => {
158
- try {
159
- return new URL(s).host;
160
- } catch {
161
- return "invalid";
162
- }
163
- })();
164
- return t.suspend(() => (w !== !1 && d(T, g(), w), D(1))).pipe(t.withSpan("integration-http.request", { attributes: {
165
- "http.request.method": c,
166
- "server.address": O
167
- } }));
168
- }, w = (e) => C(e).pipe(t.flatMap((n) => n.bytes.length === 0 ? t.succeed(null) : t.try({
169
- try: () => JSON.parse(n.text()),
170
- catch: () => e.makeError({
171
- status: n.status,
172
- message: "invalid JSON in response",
173
- transient: !1
174
- })
175
- }))), T = (e) => {
176
- let t = {
177
- ...o,
178
- ...e.policy
179
- }, n = (n, r, i) => {
180
- let a = i?.ctx ?? void 0, o = i?.body;
181
- return {
182
- fetchImpl: e.fetchImpl,
183
- baseUrl: e.baseUrl,
184
- method: n,
185
- path: r,
186
- headers: e.auth(a),
187
- ...o === void 0 ? {} : { body: JSON.stringify(o) },
188
- policy: t,
189
- makeError: e.makeError,
190
- ...e.now ? { now: e.now } : {}
191
- };
192
- }, r = (e, t, r) => C(n(e, t, r)), i = (e, t, r) => w(n(e, t, r));
193
- return {
194
- get: (e, t) => r("GET", e, t),
195
- getJson: (e, t) => i("GET", e, t),
196
- postJson: (e, t) => i("POST", e, t),
197
- putJson: (e, t) => i("PUT", e, t),
198
- deleteJson: (e, t) => i("DELETE", e, t),
199
- request: (e, t, n) => r(e, t, n),
200
- policy: t
201
- };
202
- };
203
- //#endregion
204
- export { o as DEFAULT_POLICY, a as DEFAULT_RETRY_BUDGET, i as DEFAULT_RETRY_METHODS, r as RETRYABLE_STATUS, x as assertPublicUrl, y as assertSameHost, _ as backoffMs, T as makeHttpClient, v as parseRetryAfter, C as request, w as requestJson, g as resetRetryBudgets, h as retryBudgetSnapshot };
1
+ import { DEFAULT_POLICY as e, DEFAULT_RETRY_BUDGET as t, DEFAULT_RETRY_METHODS as n, RETRYABLE_STATUS as r, assertPublicUrl as i, assertSameHost as a, backoffMs as o, parseRetryAfter as s, request as c, requestJson as l, resetRetryBudgets as u, retryBudgetSnapshot as d } from "./core.js";
2
+ import { makeHttpClient as f } from "./client.js";
3
+ export { e as DEFAULT_POLICY, t as DEFAULT_RETRY_BUDGET, n as DEFAULT_RETRY_METHODS, r as RETRYABLE_STATUS, i as assertPublicUrl, a as assertSameHost, o as backoffMs, f as makeHttpClient, s as parseRetryAfter, c as request, l as requestJson, u as resetRetryBudgets, d as retryBudgetSnapshot };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@voltro/integration-http",
3
- "version": "0.85.0",
3
+ "version": "0.87.0",
4
4
  "description": "Server-side HTTP transport core for integration plugins: token auth, transient retry with Retry-After, per-request timeout, and an SSRF host-allowlist guard — over an injectable fetch seam, Effect-first with typed errors.",
5
5
  "keywords": [
6
6
  "voltro",