@yorun-ai/vrpc 0.9.1 → 0.9.3

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,15 +1,68 @@
1
1
  //#region src/contracts/errors.ts
2
2
  var HTTP_TIMEOUT_ERROR_CODE = "HTTP_TIMEOUT";
3
- var HttpTimeoutError = class extends Error {
3
+ var HTTP_ABORT_ERROR_CODE = "HTTP_ABORTED";
4
+ var HTTP_TRANSPORT_ERROR_CODE = "HTTP_TRANSPORT_ERROR";
5
+ var VRPC_PROTOCOL_ERROR_CODE = "VRPC_PROTOCOL_ERROR";
6
+ var HttpClientError = class extends Error {
7
+ isHttpClientError = true;
8
+ kind;
9
+ url;
10
+ method;
11
+ constructor(message, kind, requestMeta = {}, options = {}) {
12
+ super(message, options.cause === void 0 ? void 0 : { cause: options.cause });
13
+ this.name = "HttpClientError";
14
+ this.kind = kind;
15
+ this.url = requestMeta.url || "";
16
+ this.method = requestMeta.method || "GET";
17
+ }
18
+ };
19
+ var HttpTimeoutError = class extends HttpClientError {
4
20
  code;
5
21
  timeoutMs;
6
- constructor(timeoutMs) {
7
- super(`request timeout after ${timeoutMs}ms`);
22
+ constructor(timeoutMs, requestMeta = {}, options = {}) {
23
+ super(`request timeout after ${timeoutMs}ms`, "timeout", requestMeta, options);
8
24
  this.name = "HttpTimeoutError";
9
25
  this.code = HTTP_TIMEOUT_ERROR_CODE;
10
26
  this.timeoutMs = timeoutMs;
11
27
  }
12
28
  };
29
+ var HttpAbortError = class extends HttpClientError {
30
+ code;
31
+ reason;
32
+ constructor(requestMeta = {}, options = {}) {
33
+ super("request aborted", "abort", requestMeta, options);
34
+ this.name = "HttpAbortError";
35
+ this.code = HTTP_ABORT_ERROR_CODE;
36
+ this.reason = options.reason;
37
+ }
38
+ };
39
+ var HttpTransportError = class extends HttpClientError {
40
+ code;
41
+ constructor(requestMeta = {}, options = {}) {
42
+ super("network request failed", "transport", requestMeta, options);
43
+ this.name = "HttpTransportError";
44
+ this.code = HTTP_TRANSPORT_ERROR_CODE;
45
+ }
46
+ };
47
+ var VrpcProtocolError = class extends HttpClientError {
48
+ code;
49
+ status;
50
+ statusText;
51
+ responseHeaders;
52
+ vrpcStatus;
53
+ constructor(response, message, requestMeta = {}, options = {}) {
54
+ super(message, "protocol", {
55
+ url: requestMeta.url || response.url || "",
56
+ method: requestMeta.method || "POST"
57
+ }, options);
58
+ this.name = "VrpcProtocolError";
59
+ this.code = VRPC_PROTOCOL_ERROR_CODE;
60
+ this.status = response.status;
61
+ this.statusText = response.statusText;
62
+ this.responseHeaders = response.headers;
63
+ this.vrpcStatus = response.headers.get("vrpc-status");
64
+ }
65
+ };
13
66
  function readServerErrorMessage(payload) {
14
67
  if (!payload || typeof payload !== "object") return;
15
68
  const body = payload;
@@ -22,7 +75,7 @@ function readServerErrorStringField(payload, field) {
22
75
  const value = payload[field];
23
76
  return typeof value === "string" ? value : void 0;
24
77
  }
25
- var HttpInvokeError = class extends Error {
78
+ var HttpInvokeError = class extends HttpClientError {
26
79
  type;
27
80
  code;
28
81
  reason;
@@ -31,11 +84,12 @@ var HttpInvokeError = class extends Error {
31
84
  statusText;
32
85
  responseHeaders;
33
86
  payload;
34
- url;
35
- method;
36
- constructor(response, payload, requestMeta = {}) {
87
+ constructor(response, payload, requestMeta = {}, options = {}) {
37
88
  const message = readServerErrorMessage(payload) || `request failed (status=${response.status})`;
38
- super(message);
89
+ super(message, "invoke", {
90
+ url: requestMeta.url || response.url || "",
91
+ method: requestMeta.method || "GET"
92
+ }, options);
39
93
  this.name = "HttpInvokeError";
40
94
  this.status = response.status;
41
95
  this.statusText = response.statusText;
@@ -45,19 +99,47 @@ var HttpInvokeError = class extends Error {
45
99
  this.code = readServerErrorStringField(payload, "code");
46
100
  this.reason = readServerErrorStringField(payload, "reason");
47
101
  this.detail = readServerErrorStringField(payload, "detail");
48
- this.url = requestMeta.url || response.url || "";
49
- this.method = requestMeta.method || "GET";
50
102
  }
51
103
  };
52
104
  var VrpcInvokeError = class extends HttpInvokeError {
53
105
  vrpcStatus;
54
- constructor(response, payload, requestMeta = {}) {
55
- super(response, payload, requestMeta);
106
+ constructor(response, payload, requestMeta = {}, options = {}) {
107
+ super(response, payload, requestMeta, options);
56
108
  this.name = "VrpcInvokeError";
57
- const vrpcStatus = response.headers.get("vrpc-status");
58
- this.vrpcStatus = vrpcStatus;
109
+ this.vrpcStatus = response.headers.get("vrpc-status");
59
110
  }
60
111
  };
112
+ function hasHttpClientErrorBrand(error) {
113
+ return Boolean(error && typeof error === "object" && error.isHttpClientError === true);
114
+ }
115
+ function isHttpError(error) {
116
+ if (!hasHttpClientErrorBrand(error)) return false;
117
+ const candidate = error;
118
+ switch (candidate.kind) {
119
+ case "abort": return candidate.code === HTTP_ABORT_ERROR_CODE;
120
+ case "timeout": return candidate.code === "HTTP_TIMEOUT" && typeof candidate.timeoutMs === "number";
121
+ case "transport": return candidate.code === HTTP_TRANSPORT_ERROR_CODE;
122
+ case "invoke": return typeof candidate.status === "number" && "payload" in candidate;
123
+ default: return false;
124
+ }
125
+ }
126
+ function isVrpcError(error) {
127
+ if (!hasHttpClientErrorBrand(error)) return false;
128
+ const candidate = error;
129
+ switch (candidate.kind) {
130
+ case "abort": return candidate.code === HTTP_ABORT_ERROR_CODE;
131
+ case "timeout": return candidate.code === "HTTP_TIMEOUT" && typeof candidate.timeoutMs === "number";
132
+ case "transport": return candidate.code === HTTP_TRANSPORT_ERROR_CODE;
133
+ case "protocol": return candidate.code === "VRPC_PROTOCOL_ERROR" && typeof candidate.status === "number";
134
+ case "invoke": return typeof candidate.status === "number" && "vrpcStatus" in candidate;
135
+ default: return false;
136
+ }
137
+ }
138
+ function isAbortErrorLike(error) {
139
+ if (!error || typeof error !== "object" || error instanceof HttpTimeoutError) return false;
140
+ const candidate = error;
141
+ return candidate.kind === "abort" || candidate.name === "HttpAbortError" || candidate.name === "AbortError" || candidate.code === "HTTP_ABORTED" || candidate.code === "ABORT_ERR" || candidate.code === "ERR_CANCELED" || candidate.__CANCEL__ === true;
142
+ }
61
143
  //#endregion
62
144
  //#region src/core/http-utils.ts
63
145
  var NULL_BODY_STATUS_CODES = /* @__PURE__ */ new Set([
@@ -73,10 +155,18 @@ function isNullBodyStatus(status) {
73
155
  function shouldTreatResponseAsBodyless(method, status) {
74
156
  return method.toUpperCase() === "HEAD" || isNullBodyStatus(status);
75
157
  }
76
- function buildRequestUrl(prefixUrl, path) {
158
+ var ALLOWED_ABSOLUTE_URL_PROTOCOLS = /* @__PURE__ */ new Set(["http:", "https:"]);
159
+ function buildRequestUrl(prefixUrl, path, { allowAbsoluteUrls = false } = {}) {
160
+ if (/^\s*\/\//.test(path)) throw new Error("Protocol-relative request URLs are not supported.");
161
+ let absoluteUrl;
77
162
  try {
78
- return new URL(path).toString();
163
+ absoluteUrl = new URL(path);
79
164
  } catch {}
165
+ if (absoluteUrl) {
166
+ if (!ALLOWED_ABSOLUTE_URL_PROTOCOLS.has(absoluteUrl.protocol)) throw new Error(`Unsupported absolute request URL protocol: ${absoluteUrl.protocol || "unknown"}`);
167
+ if (!allowAbsoluteUrls) throw new Error("Absolute request URLs are disabled. Set allowAbsoluteUrls: true on createHttpClient() to enable them.");
168
+ return absoluteUrl.toString();
169
+ }
80
170
  const base = String(prefixUrl).replace(/\/+$/, "");
81
171
  const relativePath = path.replace(/^\/+/, "");
82
172
  return relativePath ? `${base}/${relativePath}` : `${base}/`;
@@ -124,43 +214,56 @@ async function parseResponsePayload(response) {
124
214
  }
125
215
  //#endregion
126
216
  //#region src/transports/fetch-transport.ts
127
- function createTimeoutError(timeoutMs) {
128
- return new HttpTimeoutError(timeoutMs);
129
- }
130
- function withTimeoutSignal(timeoutMs, signal) {
217
+ function withTimeoutSignal(timeoutMs, callerSignal, requestMeta) {
131
218
  if (!timeoutMs || timeoutMs <= 0) return {
132
- signal: signal || void 0,
133
- cleanup: () => {}
219
+ signal: callerSignal || void 0,
220
+ cleanup: () => {},
221
+ getAbortSource: () => callerSignal?.aborted ? "caller" : void 0,
222
+ getTimeoutError: () => void 0
134
223
  };
135
224
  const controller = new AbortController();
136
- const timer = setTimeout(() => controller.abort(createTimeoutError(timeoutMs)), timeoutMs);
225
+ let abortSource;
226
+ let timeoutError;
227
+ const timer = setTimeout(() => {
228
+ if (controller.signal.aborted) return;
229
+ abortSource = "timeout";
230
+ timeoutError = new HttpTimeoutError(timeoutMs, requestMeta);
231
+ controller.abort(timeoutError);
232
+ }, timeoutMs);
137
233
  const clear = () => clearTimeout(timer);
138
- if (!signal) return {
234
+ if (!callerSignal) return {
139
235
  signal: controller.signal,
140
- cleanup: clear
236
+ cleanup: clear,
237
+ getAbortSource: () => abortSource,
238
+ getTimeoutError: () => timeoutError
141
239
  };
142
- if (signal.aborted) {
143
- controller.abort(signal.reason);
144
- return {
145
- signal: controller.signal,
146
- cleanup: clear
147
- };
148
- }
149
- const relayAbort = () => controller.abort(signal.reason);
150
- signal.addEventListener("abort", relayAbort, { once: true });
240
+ const relayAbort = () => {
241
+ if (controller.signal.aborted) return;
242
+ abortSource = "caller";
243
+ controller.abort(callerSignal.reason);
244
+ };
245
+ if (callerSignal.aborted) relayAbort();
246
+ else callerSignal.addEventListener("abort", relayAbort, { once: true });
151
247
  return {
152
248
  signal: controller.signal,
153
249
  cleanup: () => {
154
250
  clear();
155
- signal.removeEventListener("abort", relayAbort);
156
- }
251
+ callerSignal.removeEventListener("abort", relayAbort);
252
+ },
253
+ getAbortSource: () => abortSource,
254
+ getTimeoutError: () => timeoutError
157
255
  };
158
256
  }
159
257
  function createFetchTransport(fetchImpl) {
160
258
  const request = fetchImpl || globalThis.fetch;
161
259
  if (!request) throw new Error("fetch is not available in current runtime.");
162
260
  return { request: async (input) => {
163
- const { signal, cleanup } = withTimeoutSignal(input.timeoutMs, input.init.signal || input.signal);
261
+ const requestMeta = {
262
+ url: input.url,
263
+ method: input.method
264
+ };
265
+ const callerSignal = input.init.signal || input.signal;
266
+ const { signal, cleanup, getAbortSource, getTimeoutError } = withTimeoutSignal(input.timeoutMs, callerSignal, requestMeta);
164
267
  try {
165
268
  const response = await request(input.url, {
166
269
  ...input.init,
@@ -178,6 +281,20 @@ function createFetchTransport(fetchImpl) {
178
281
  body,
179
282
  url: response.url || input.url
180
283
  };
284
+ } catch (cause) {
285
+ const abortSource = getAbortSource();
286
+ if (abortSource === "timeout") {
287
+ const timeoutError = getTimeoutError();
288
+ if (cause === timeoutError && timeoutError) throw timeoutError;
289
+ throw new HttpTimeoutError(input.timeoutMs, requestMeta, { cause });
290
+ }
291
+ if (abortSource === "caller") throw new HttpAbortError(requestMeta, {
292
+ cause,
293
+ reason: callerSignal?.reason
294
+ });
295
+ if (isHttpError(cause)) throw cause;
296
+ if (isAbortErrorLike(cause)) throw new HttpAbortError(requestMeta, { cause });
297
+ throw new HttpTransportError(requestMeta, { cause });
181
298
  } finally {
182
299
  cleanup();
183
300
  }
@@ -238,15 +355,26 @@ function createClientCore(options) {
238
355
  url: context.url
239
356
  };
240
357
  await validateRequest?.(context, requestMeta);
241
- const response = buildResponseFromTransportResult(await httpTransport.request({
242
- url: context.url,
243
- init: context.init,
244
- method,
245
- headers,
246
- body: resolveTransportBody(context.init, prepared.fallbackBody),
247
- timeoutMs: prepared.timeoutMs,
248
- signal: context.init.signal
249
- }));
358
+ let result;
359
+ try {
360
+ result = await httpTransport.request({
361
+ url: context.url,
362
+ init: context.init,
363
+ method,
364
+ headers,
365
+ body: resolveTransportBody(context.init, prepared.fallbackBody),
366
+ timeoutMs: prepared.timeoutMs,
367
+ signal: context.init.signal
368
+ });
369
+ } catch (cause) {
370
+ if (isHttpError(cause) || isVrpcError(cause)) throw cause;
371
+ if (context.init.signal?.aborted || isAbortErrorLike(cause)) throw new HttpAbortError(requestMeta, {
372
+ cause,
373
+ reason: context.init.signal?.reason
374
+ });
375
+ throw new HttpTransportError(requestMeta, { cause });
376
+ }
377
+ const response = buildResponseFromTransportResult(result);
250
378
  const payload = await parseResponse(response, context);
251
379
  await runAfterResponseInterceptors(interceptors, response, context);
252
380
  await assertResponse(response, payload, context, requestMeta);
@@ -285,4 +413,4 @@ function splitVrpcRequestOptions(options) {
285
413
  };
286
414
  }
287
415
  //#endregion
288
- export { appendQueryToUrl as a, HTTP_TIMEOUT_ERROR_CODE as c, VrpcInvokeError as d, createFetchTransport as i, HttpInvokeError as l, splitVrpcRequestOptions as n, buildRequestUrl as o, createClientCore as r, mergeHeaders as s, splitHttpRequestOptions as t, HttpTimeoutError as u };
416
+ export { isVrpcError as _, appendQueryToUrl as a, HTTP_TIMEOUT_ERROR_CODE as c, HttpInvokeError as d, HttpTimeoutError as f, isHttpError as g, VrpcProtocolError as h, createFetchTransport as i, HttpAbortError as l, VrpcInvokeError as m, splitVrpcRequestOptions as n, buildRequestUrl as o, HttpTransportError as p, createClientCore as r, mergeHeaders as s, splitHttpRequestOptions as t, HttpClientError as u };
@@ -1,15 +1,68 @@
1
1
  //#region src/contracts/errors.ts
2
2
  var HTTP_TIMEOUT_ERROR_CODE = "HTTP_TIMEOUT";
3
- var HttpTimeoutError = class extends Error {
3
+ var HTTP_ABORT_ERROR_CODE = "HTTP_ABORTED";
4
+ var HTTP_TRANSPORT_ERROR_CODE = "HTTP_TRANSPORT_ERROR";
5
+ var VRPC_PROTOCOL_ERROR_CODE = "VRPC_PROTOCOL_ERROR";
6
+ var HttpClientError = class extends Error {
7
+ isHttpClientError = true;
8
+ kind;
9
+ url;
10
+ method;
11
+ constructor(message, kind, requestMeta = {}, options = {}) {
12
+ super(message, options.cause === void 0 ? void 0 : { cause: options.cause });
13
+ this.name = "HttpClientError";
14
+ this.kind = kind;
15
+ this.url = requestMeta.url || "";
16
+ this.method = requestMeta.method || "GET";
17
+ }
18
+ };
19
+ var HttpTimeoutError = class extends HttpClientError {
4
20
  code;
5
21
  timeoutMs;
6
- constructor(timeoutMs) {
7
- super(`request timeout after ${timeoutMs}ms`);
22
+ constructor(timeoutMs, requestMeta = {}, options = {}) {
23
+ super(`request timeout after ${timeoutMs}ms`, "timeout", requestMeta, options);
8
24
  this.name = "HttpTimeoutError";
9
25
  this.code = HTTP_TIMEOUT_ERROR_CODE;
10
26
  this.timeoutMs = timeoutMs;
11
27
  }
12
28
  };
29
+ var HttpAbortError = class extends HttpClientError {
30
+ code;
31
+ reason;
32
+ constructor(requestMeta = {}, options = {}) {
33
+ super("request aborted", "abort", requestMeta, options);
34
+ this.name = "HttpAbortError";
35
+ this.code = HTTP_ABORT_ERROR_CODE;
36
+ this.reason = options.reason;
37
+ }
38
+ };
39
+ var HttpTransportError = class extends HttpClientError {
40
+ code;
41
+ constructor(requestMeta = {}, options = {}) {
42
+ super("network request failed", "transport", requestMeta, options);
43
+ this.name = "HttpTransportError";
44
+ this.code = HTTP_TRANSPORT_ERROR_CODE;
45
+ }
46
+ };
47
+ var VrpcProtocolError = class extends HttpClientError {
48
+ code;
49
+ status;
50
+ statusText;
51
+ responseHeaders;
52
+ vrpcStatus;
53
+ constructor(response, message, requestMeta = {}, options = {}) {
54
+ super(message, "protocol", {
55
+ url: requestMeta.url || response.url || "",
56
+ method: requestMeta.method || "POST"
57
+ }, options);
58
+ this.name = "VrpcProtocolError";
59
+ this.code = VRPC_PROTOCOL_ERROR_CODE;
60
+ this.status = response.status;
61
+ this.statusText = response.statusText;
62
+ this.responseHeaders = response.headers;
63
+ this.vrpcStatus = response.headers.get("vrpc-status");
64
+ }
65
+ };
13
66
  function readServerErrorMessage(payload) {
14
67
  if (!payload || typeof payload !== "object") return;
15
68
  const body = payload;
@@ -22,7 +75,7 @@ function readServerErrorStringField(payload, field) {
22
75
  const value = payload[field];
23
76
  return typeof value === "string" ? value : void 0;
24
77
  }
25
- var HttpInvokeError = class extends Error {
78
+ var HttpInvokeError = class extends HttpClientError {
26
79
  type;
27
80
  code;
28
81
  reason;
@@ -31,11 +84,12 @@ var HttpInvokeError = class extends Error {
31
84
  statusText;
32
85
  responseHeaders;
33
86
  payload;
34
- url;
35
- method;
36
- constructor(response, payload, requestMeta = {}) {
87
+ constructor(response, payload, requestMeta = {}, options = {}) {
37
88
  const message = readServerErrorMessage(payload) || `request failed (status=${response.status})`;
38
- super(message);
89
+ super(message, "invoke", {
90
+ url: requestMeta.url || response.url || "",
91
+ method: requestMeta.method || "GET"
92
+ }, options);
39
93
  this.name = "HttpInvokeError";
40
94
  this.status = response.status;
41
95
  this.statusText = response.statusText;
@@ -45,19 +99,47 @@ var HttpInvokeError = class extends Error {
45
99
  this.code = readServerErrorStringField(payload, "code");
46
100
  this.reason = readServerErrorStringField(payload, "reason");
47
101
  this.detail = readServerErrorStringField(payload, "detail");
48
- this.url = requestMeta.url || response.url || "";
49
- this.method = requestMeta.method || "GET";
50
102
  }
51
103
  };
52
104
  var VrpcInvokeError = class extends HttpInvokeError {
53
105
  vrpcStatus;
54
- constructor(response, payload, requestMeta = {}) {
55
- super(response, payload, requestMeta);
106
+ constructor(response, payload, requestMeta = {}, options = {}) {
107
+ super(response, payload, requestMeta, options);
56
108
  this.name = "VrpcInvokeError";
57
- const vrpcStatus = response.headers.get("vrpc-status");
58
- this.vrpcStatus = vrpcStatus;
109
+ this.vrpcStatus = response.headers.get("vrpc-status");
59
110
  }
60
111
  };
112
+ function hasHttpClientErrorBrand(error) {
113
+ return Boolean(error && typeof error === "object" && error.isHttpClientError === true);
114
+ }
115
+ function isHttpError(error) {
116
+ if (!hasHttpClientErrorBrand(error)) return false;
117
+ const candidate = error;
118
+ switch (candidate.kind) {
119
+ case "abort": return candidate.code === HTTP_ABORT_ERROR_CODE;
120
+ case "timeout": return candidate.code === "HTTP_TIMEOUT" && typeof candidate.timeoutMs === "number";
121
+ case "transport": return candidate.code === HTTP_TRANSPORT_ERROR_CODE;
122
+ case "invoke": return typeof candidate.status === "number" && "payload" in candidate;
123
+ default: return false;
124
+ }
125
+ }
126
+ function isVrpcError(error) {
127
+ if (!hasHttpClientErrorBrand(error)) return false;
128
+ const candidate = error;
129
+ switch (candidate.kind) {
130
+ case "abort": return candidate.code === HTTP_ABORT_ERROR_CODE;
131
+ case "timeout": return candidate.code === "HTTP_TIMEOUT" && typeof candidate.timeoutMs === "number";
132
+ case "transport": return candidate.code === HTTP_TRANSPORT_ERROR_CODE;
133
+ case "protocol": return candidate.code === "VRPC_PROTOCOL_ERROR" && typeof candidate.status === "number";
134
+ case "invoke": return typeof candidate.status === "number" && "vrpcStatus" in candidate;
135
+ default: return false;
136
+ }
137
+ }
138
+ function isAbortErrorLike(error) {
139
+ if (!error || typeof error !== "object" || error instanceof HttpTimeoutError) return false;
140
+ const candidate = error;
141
+ return candidate.kind === "abort" || candidate.name === "HttpAbortError" || candidate.name === "AbortError" || candidate.code === "HTTP_ABORTED" || candidate.code === "ABORT_ERR" || candidate.code === "ERR_CANCELED" || candidate.__CANCEL__ === true;
142
+ }
61
143
  //#endregion
62
144
  //#region src/core/http-utils.ts
63
145
  var NULL_BODY_STATUS_CODES = /* @__PURE__ */ new Set([
@@ -73,10 +155,18 @@ function isNullBodyStatus(status) {
73
155
  function shouldTreatResponseAsBodyless(method, status) {
74
156
  return method.toUpperCase() === "HEAD" || isNullBodyStatus(status);
75
157
  }
76
- function buildRequestUrl(prefixUrl, path) {
158
+ var ALLOWED_ABSOLUTE_URL_PROTOCOLS = /* @__PURE__ */ new Set(["http:", "https:"]);
159
+ function buildRequestUrl(prefixUrl, path, { allowAbsoluteUrls = false } = {}) {
160
+ if (/^\s*\/\//.test(path)) throw new Error("Protocol-relative request URLs are not supported.");
161
+ let absoluteUrl;
77
162
  try {
78
- return new URL(path).toString();
163
+ absoluteUrl = new URL(path);
79
164
  } catch {}
165
+ if (absoluteUrl) {
166
+ if (!ALLOWED_ABSOLUTE_URL_PROTOCOLS.has(absoluteUrl.protocol)) throw new Error(`Unsupported absolute request URL protocol: ${absoluteUrl.protocol || "unknown"}`);
167
+ if (!allowAbsoluteUrls) throw new Error("Absolute request URLs are disabled. Set allowAbsoluteUrls: true on createHttpClient() to enable them.");
168
+ return absoluteUrl.toString();
169
+ }
80
170
  const base = String(prefixUrl).replace(/\/+$/, "");
81
171
  const relativePath = path.replace(/^\/+/, "");
82
172
  return relativePath ? `${base}/${relativePath}` : `${base}/`;
@@ -124,43 +214,56 @@ async function parseResponsePayload(response) {
124
214
  }
125
215
  //#endregion
126
216
  //#region src/transports/fetch-transport.ts
127
- function createTimeoutError(timeoutMs) {
128
- return new HttpTimeoutError(timeoutMs);
129
- }
130
- function withTimeoutSignal(timeoutMs, signal) {
217
+ function withTimeoutSignal(timeoutMs, callerSignal, requestMeta) {
131
218
  if (!timeoutMs || timeoutMs <= 0) return {
132
- signal: signal || void 0,
133
- cleanup: () => {}
219
+ signal: callerSignal || void 0,
220
+ cleanup: () => {},
221
+ getAbortSource: () => callerSignal?.aborted ? "caller" : void 0,
222
+ getTimeoutError: () => void 0
134
223
  };
135
224
  const controller = new AbortController();
136
- const timer = setTimeout(() => controller.abort(createTimeoutError(timeoutMs)), timeoutMs);
225
+ let abortSource;
226
+ let timeoutError;
227
+ const timer = setTimeout(() => {
228
+ if (controller.signal.aborted) return;
229
+ abortSource = "timeout";
230
+ timeoutError = new HttpTimeoutError(timeoutMs, requestMeta);
231
+ controller.abort(timeoutError);
232
+ }, timeoutMs);
137
233
  const clear = () => clearTimeout(timer);
138
- if (!signal) return {
234
+ if (!callerSignal) return {
139
235
  signal: controller.signal,
140
- cleanup: clear
236
+ cleanup: clear,
237
+ getAbortSource: () => abortSource,
238
+ getTimeoutError: () => timeoutError
141
239
  };
142
- if (signal.aborted) {
143
- controller.abort(signal.reason);
144
- return {
145
- signal: controller.signal,
146
- cleanup: clear
147
- };
148
- }
149
- const relayAbort = () => controller.abort(signal.reason);
150
- signal.addEventListener("abort", relayAbort, { once: true });
240
+ const relayAbort = () => {
241
+ if (controller.signal.aborted) return;
242
+ abortSource = "caller";
243
+ controller.abort(callerSignal.reason);
244
+ };
245
+ if (callerSignal.aborted) relayAbort();
246
+ else callerSignal.addEventListener("abort", relayAbort, { once: true });
151
247
  return {
152
248
  signal: controller.signal,
153
249
  cleanup: () => {
154
250
  clear();
155
- signal.removeEventListener("abort", relayAbort);
156
- }
251
+ callerSignal.removeEventListener("abort", relayAbort);
252
+ },
253
+ getAbortSource: () => abortSource,
254
+ getTimeoutError: () => timeoutError
157
255
  };
158
256
  }
159
257
  function createFetchTransport(fetchImpl) {
160
258
  const request = fetchImpl || globalThis.fetch;
161
259
  if (!request) throw new Error("fetch is not available in current runtime.");
162
260
  return { request: async (input) => {
163
- const { signal, cleanup } = withTimeoutSignal(input.timeoutMs, input.init.signal || input.signal);
261
+ const requestMeta = {
262
+ url: input.url,
263
+ method: input.method
264
+ };
265
+ const callerSignal = input.init.signal || input.signal;
266
+ const { signal, cleanup, getAbortSource, getTimeoutError } = withTimeoutSignal(input.timeoutMs, callerSignal, requestMeta);
164
267
  try {
165
268
  const response = await request(input.url, {
166
269
  ...input.init,
@@ -178,6 +281,20 @@ function createFetchTransport(fetchImpl) {
178
281
  body,
179
282
  url: response.url || input.url
180
283
  };
284
+ } catch (cause) {
285
+ const abortSource = getAbortSource();
286
+ if (abortSource === "timeout") {
287
+ const timeoutError = getTimeoutError();
288
+ if (cause === timeoutError && timeoutError) throw timeoutError;
289
+ throw new HttpTimeoutError(input.timeoutMs, requestMeta, { cause });
290
+ }
291
+ if (abortSource === "caller") throw new HttpAbortError(requestMeta, {
292
+ cause,
293
+ reason: callerSignal?.reason
294
+ });
295
+ if (isHttpError(cause)) throw cause;
296
+ if (isAbortErrorLike(cause)) throw new HttpAbortError(requestMeta, { cause });
297
+ throw new HttpTransportError(requestMeta, { cause });
181
298
  } finally {
182
299
  cleanup();
183
300
  }
@@ -238,15 +355,26 @@ function createClientCore(options) {
238
355
  url: context.url
239
356
  };
240
357
  await validateRequest?.(context, requestMeta);
241
- const response = buildResponseFromTransportResult(await httpTransport.request({
242
- url: context.url,
243
- init: context.init,
244
- method,
245
- headers,
246
- body: resolveTransportBody(context.init, prepared.fallbackBody),
247
- timeoutMs: prepared.timeoutMs,
248
- signal: context.init.signal
249
- }));
358
+ let result;
359
+ try {
360
+ result = await httpTransport.request({
361
+ url: context.url,
362
+ init: context.init,
363
+ method,
364
+ headers,
365
+ body: resolveTransportBody(context.init, prepared.fallbackBody),
366
+ timeoutMs: prepared.timeoutMs,
367
+ signal: context.init.signal
368
+ });
369
+ } catch (cause) {
370
+ if (isHttpError(cause) || isVrpcError(cause)) throw cause;
371
+ if (context.init.signal?.aborted || isAbortErrorLike(cause)) throw new HttpAbortError(requestMeta, {
372
+ cause,
373
+ reason: context.init.signal?.reason
374
+ });
375
+ throw new HttpTransportError(requestMeta, { cause });
376
+ }
377
+ const response = buildResponseFromTransportResult(result);
250
378
  const payload = await parseResponse(response, context);
251
379
  await runAfterResponseInterceptors(interceptors, response, context);
252
380
  await assertResponse(response, payload, context, requestMeta);
@@ -291,6 +419,18 @@ Object.defineProperty(exports, "HTTP_TIMEOUT_ERROR_CODE", {
291
419
  return HTTP_TIMEOUT_ERROR_CODE;
292
420
  }
293
421
  });
422
+ Object.defineProperty(exports, "HttpAbortError", {
423
+ enumerable: true,
424
+ get: function() {
425
+ return HttpAbortError;
426
+ }
427
+ });
428
+ Object.defineProperty(exports, "HttpClientError", {
429
+ enumerable: true,
430
+ get: function() {
431
+ return HttpClientError;
432
+ }
433
+ });
294
434
  Object.defineProperty(exports, "HttpInvokeError", {
295
435
  enumerable: true,
296
436
  get: function() {
@@ -303,12 +443,24 @@ Object.defineProperty(exports, "HttpTimeoutError", {
303
443
  return HttpTimeoutError;
304
444
  }
305
445
  });
446
+ Object.defineProperty(exports, "HttpTransportError", {
447
+ enumerable: true,
448
+ get: function() {
449
+ return HttpTransportError;
450
+ }
451
+ });
306
452
  Object.defineProperty(exports, "VrpcInvokeError", {
307
453
  enumerable: true,
308
454
  get: function() {
309
455
  return VrpcInvokeError;
310
456
  }
311
457
  });
458
+ Object.defineProperty(exports, "VrpcProtocolError", {
459
+ enumerable: true,
460
+ get: function() {
461
+ return VrpcProtocolError;
462
+ }
463
+ });
312
464
  Object.defineProperty(exports, "appendQueryToUrl", {
313
465
  enumerable: true,
314
466
  get: function() {
@@ -333,6 +485,18 @@ Object.defineProperty(exports, "createFetchTransport", {
333
485
  return createFetchTransport;
334
486
  }
335
487
  });
488
+ Object.defineProperty(exports, "isHttpError", {
489
+ enumerable: true,
490
+ get: function() {
491
+ return isHttpError;
492
+ }
493
+ });
494
+ Object.defineProperty(exports, "isVrpcError", {
495
+ enumerable: true,
496
+ get: function() {
497
+ return isVrpcError;
498
+ }
499
+ });
336
500
  Object.defineProperty(exports, "mergeHeaders", {
337
501
  enumerable: true,
338
502
  get: function() {