@dunx/http 2.5.0 → 3.0.1

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.
@@ -0,0 +1,58 @@
1
+ // @bun
2
+ // src/server/trace-context.ts
3
+ var TRACEPARENT_HEADER = "traceparent";
4
+ var TRACESTATE_HEADER = "tracestate";
5
+ var HEX_32 = /^[0-9a-f]{32}$/;
6
+ var HEX_16 = /^[0-9a-f]{16}$/;
7
+ var HEX_2 = /^[0-9a-f]{2}$/;
8
+ var ZERO_TRACE = "0".repeat(32);
9
+ var ZERO_SPAN = "0".repeat(16);
10
+ var SAMPLED = 1;
11
+ var TRACE = Symbol.for("dunx.http.trace");
12
+ var mintSpanId = () => Buffer.from(crypto.getRandomValues(new Uint8Array(8))).toString("hex");
13
+
14
+ class TraceContext {
15
+ static adopt(req, requestId) {
16
+ const inbound = TraceContext.#parse(req.headers.get(TRACEPARENT_HEADER));
17
+ const state = req.headers.get(TRACESTATE_HEADER);
18
+ const trace = {
19
+ traceId: inbound?.traceId ?? requestId.replaceAll("-", ""),
20
+ spanId: mintSpanId(),
21
+ ...inbound === undefined ? {} : { parentSpanId: inbound.spanId },
22
+ flags: inbound?.flags ?? "01",
23
+ ...inbound !== undefined && state !== null ? { state } : {}
24
+ };
25
+ req[TRACE] = trace;
26
+ return trace;
27
+ }
28
+ static of(req) {
29
+ return req[TRACE];
30
+ }
31
+ static header(trace) {
32
+ return `00-${trace.traceId}-${trace.spanId}-${trace.flags}`;
33
+ }
34
+ static sampled(trace) {
35
+ return (Number.parseInt(trace.flags, 16) & SAMPLED) === SAMPLED;
36
+ }
37
+ static #parse(header) {
38
+ if (header === null)
39
+ return;
40
+ const parts = header.split("-");
41
+ if (parts.length < 4)
42
+ return;
43
+ const [version, traceId, spanId, flags] = parts;
44
+ if (!HEX_2.test(version) || version === "ff")
45
+ return;
46
+ if (version === "00" && parts.length !== 4)
47
+ return;
48
+ if (!HEX_32.test(traceId) || traceId === ZERO_TRACE)
49
+ return;
50
+ if (!HEX_16.test(spanId) || spanId === ZERO_SPAN)
51
+ return;
52
+ if (!HEX_2.test(flags))
53
+ return;
54
+ return { traceId, spanId, flags };
55
+ }
56
+ }
57
+
58
+ export { TRACEPARENT_HEADER, TRACESTATE_HEADER, TraceContext };
@@ -0,0 +1,130 @@
1
+ // @bun
2
+ import {
3
+ HttpStatusCode
4
+ } from "./chunk-sz4pvqxy.js";
5
+
6
+ // src/client/errors.ts
7
+ import { AppError } from "@dunx/core";
8
+
9
+ class FetchError extends AppError {
10
+ status;
11
+ statusText;
12
+ body;
13
+ response;
14
+ name = "FetchError";
15
+ constructor(status, statusText, body, response) {
16
+ super(`HTTP ${status} ${statusText} from ${response.method} ${response.url}`);
17
+ this.status = status;
18
+ this.statusText = statusText;
19
+ this.body = body;
20
+ this.response = response;
21
+ }
22
+ }
23
+ Object.defineProperty(FetchError, Symbol.for("dunx.deps"), { value: () => [{ unresolved: "readonly status: number" }, { unresolved: "readonly statusText: string" }, { unresolved: "readonly body: unknown" }, { unresolved: `readonly response: {
24
+ readonly method: string;
25
+ readonly url: string;
26
+ readonly headers: Headers;
27
+ }` }] });
28
+
29
+ class FetchTransportError extends AppError {
30
+ response;
31
+ aborted;
32
+ name = "FetchTransportError";
33
+ constructor(response, aborted, options) {
34
+ super(`${response.method} ${response.url} failed: ${aborted ? "aborted" : "transport error"}`, options);
35
+ this.response = response;
36
+ this.aborted = aborted;
37
+ }
38
+ }
39
+ Object.defineProperty(FetchTransportError, Symbol.for("dunx.deps"), { value: () => [{ unresolved: "readonly response: { readonly method: string; readonly url: string }" }, { unresolved: "readonly aborted: boolean" }, ErrorOptions] });
40
+
41
+ // src/client/json.ts
42
+ var safeStringify = (value) => {
43
+ const seen = new WeakSet;
44
+ return JSON.stringify(value, (_key, entry) => {
45
+ if (typeof entry === "object" && entry !== null) {
46
+ if (seen.has(entry))
47
+ return "[Circular]";
48
+ seen.add(entry);
49
+ }
50
+ return entry;
51
+ });
52
+ };
53
+ var isPlainObject = (value) => {
54
+ if (typeof value !== "object" || value === null)
55
+ return false;
56
+ const proto = Object.getPrototypeOf(value);
57
+ return proto === Object.prototype || proto === null;
58
+ };
59
+ var isJsonBody = (payload) => {
60
+ if (payload === null || payload === undefined)
61
+ return false;
62
+ if (typeof payload !== "object")
63
+ return typeof payload !== "string";
64
+ return !(payload instanceof FormData || payload instanceof URLSearchParams || payload instanceof Blob || payload instanceof ArrayBuffer || payload instanceof ReadableStream || ArrayBuffer.isView(payload));
65
+ };
66
+
67
+ // src/client/retry.ts
68
+ var uniform = () => {
69
+ const buffer = new Uint32Array(1);
70
+ crypto.getRandomValues(buffer);
71
+ return (buffer[0] ?? 0) / 2 ** 32;
72
+ };
73
+ var backoffDelay = (attempt, { baseMs, power = 2, jitterMs = 1000, maxMs = 30000 }) => Math.min(baseMs * power ** attempt + uniform() * jitterMs, maxMs);
74
+ var retryAfterMs = (headers, now = Date.now()) => {
75
+ const header = headers.get("retry-after");
76
+ if (header === null)
77
+ return;
78
+ const seconds = Number(header);
79
+ if (Number.isFinite(seconds))
80
+ return Math.max(0, seconds * 1000);
81
+ const at = Date.parse(header);
82
+ return Number.isNaN(at) ? undefined : Math.max(0, at - now);
83
+ };
84
+ var isRetryableStatus = (status) => status >= HttpStatusCode.INTERNAL_SERVER_ERROR || status === HttpStatusCode.REQUEST_TIMEOUT || status === HttpStatusCode.TOO_MANY_REQUESTS;
85
+ var decide = (error, attempt, options) => {
86
+ const {
87
+ retryDelayMs = 1000,
88
+ backoff,
89
+ shouldRetryOnStatus = isRetryableStatus,
90
+ respectRetryAfter = true
91
+ } = options;
92
+ const computed = backoffDelay(attempt, { baseMs: retryDelayMs, ...backoff });
93
+ if (error instanceof FetchTransportError) {
94
+ return { retry: !error.aborted, delayMs: computed };
95
+ }
96
+ if (error instanceof FetchError) {
97
+ if (!shouldRetryOnStatus(error.status))
98
+ return { retry: false, delayMs: 0 };
99
+ const asked = respectRetryAfter ? retryAfterMs(error.response.headers) : undefined;
100
+ const maxMs = backoff?.maxMs ?? 30000;
101
+ return {
102
+ retry: true,
103
+ delayMs: asked === undefined ? computed : Math.min(asked, maxMs)
104
+ };
105
+ }
106
+ return { retry: true, delayMs: computed };
107
+ };
108
+ var executeWithRetry = async (operation, options = {}) => {
109
+ const { maxRetries = 3, onAttempt, onError, onSuccess } = options;
110
+ let lastError;
111
+ for (let attempt = 0;attempt <= maxRetries; attempt += 1) {
112
+ onAttempt?.(attempt + 1, attempt > 0);
113
+ try {
114
+ const result = await operation();
115
+ onSuccess?.(result, attempt + 1);
116
+ return result;
117
+ } catch (error) {
118
+ lastError = error;
119
+ const { retry, delayMs } = decide(error, attempt, options);
120
+ const willRetry = retry && attempt < maxRetries;
121
+ onError?.(error, attempt + 1, willRetry);
122
+ if (!willRetry)
123
+ throw error;
124
+ await Bun.sleep(delayMs);
125
+ }
126
+ }
127
+ throw lastError;
128
+ };
129
+
130
+ export { FetchError, FetchTransportError, safeStringify, isPlainObject, isJsonBody, backoffDelay, retryAfterMs, isRetryableStatus, executeWithRetry };