@npora/request 1.12.0 → 1.13.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/dist/index.cjs CHANGED
@@ -2623,6 +2623,30 @@ function createRemovedError(config) {
2623
2623
  });
2624
2624
  }
2625
2625
  //#endregion
2626
+ //#region src/plugins/transferProgress.ts
2627
+ const MIN_RATE_SAMPLE_MILLISECONDS = 250;
2628
+ function createTransferProgressTracker(now = Date.now) {
2629
+ const startedAt = now();
2630
+ let previousLoaded = 0;
2631
+ return (loaded, total) => {
2632
+ const bytes = Math.max(loaded - previousLoaded, 0);
2633
+ const elapsed = Math.max(now() - startedAt, 0) / 1e3;
2634
+ const rate = elapsed >= MIN_RATE_SAMPLE_MILLISECONDS / 1e3 && loaded > 0 ? loaded / elapsed : void 0;
2635
+ const progress = total === void 0 || total === 0 ? void 0 : Math.min(loaded / total, 1);
2636
+ const estimated = rate && total !== void 0 ? Math.max(total - loaded, 0) / rate : void 0;
2637
+ previousLoaded = loaded;
2638
+ const snapshot = {
2639
+ loaded,
2640
+ total,
2641
+ bytes
2642
+ };
2643
+ if (progress !== void 0) snapshot.progress = progress;
2644
+ if (rate !== void 0) snapshot.rate = rate;
2645
+ if (estimated !== void 0) snapshot.estimated = estimated;
2646
+ return snapshot;
2647
+ };
2648
+ }
2649
+ //#endregion
2626
2650
  //#region src/plugins/xhrTransport.ts
2627
2651
  function xhrRequest(config, options = {}) {
2628
2652
  let request;
@@ -2742,6 +2766,7 @@ async function processResponse(xhr, config) {
2742
2766
  }
2743
2767
  function createProgressHandler(callback, abortWith, maxResponseSize, config) {
2744
2768
  if (!callback && !Number.isFinite(maxResponseSize)) return null;
2769
+ const trackProgress = callback ? createTransferProgressTracker() : void 0;
2745
2770
  return (event) => {
2746
2771
  if (Number.isFinite(maxResponseSize) && event.loaded > (maxResponseSize ?? Number.POSITIVE_INFINITY)) {
2747
2772
  abortWith(new RequestError(`Response body exceeds maxResponseSize ${maxResponseSize}`, {
@@ -2750,25 +2775,14 @@ function createProgressHandler(callback, abortWith, maxResponseSize, config) {
2750
2775
  }));
2751
2776
  return;
2752
2777
  }
2753
- if (!callback) return;
2778
+ if (!callback || !trackProgress) return;
2754
2779
  try {
2755
- callback(createProgress$1(event.loaded, event.lengthComputable ? event.total : void 0));
2780
+ callback(trackProgress(event.loaded, event.lengthComputable ? event.total : void 0));
2756
2781
  } catch (error) {
2757
2782
  abortWith(error);
2758
2783
  }
2759
2784
  };
2760
2785
  }
2761
- function createProgress$1(loaded, total) {
2762
- if (total === void 0 || total === 0) return {
2763
- loaded,
2764
- total
2765
- };
2766
- return {
2767
- loaded,
2768
- total,
2769
- progress: Math.min(loaded / total, 1)
2770
- };
2771
- }
2772
2786
  function parseHeaders(value) {
2773
2787
  const headers = new Headers();
2774
2788
  for (const line of value.split(/\r?\n/)) {
@@ -2863,7 +2877,9 @@ async function consumeDownloadStream(stream, response, onProgress) {
2863
2877
  const reader = stream.getReader();
2864
2878
  const chunks = [];
2865
2879
  const total = parseContentLength(response.headers.get("content-length"));
2880
+ const trackProgress = createTransferProgressTracker();
2866
2881
  let loaded = 0;
2882
+ let reported = false;
2867
2883
  try {
2868
2884
  while (true) {
2869
2885
  const result = await reader.read();
@@ -2871,8 +2887,10 @@ async function consumeDownloadStream(stream, response, onProgress) {
2871
2887
  const chunk = result.value;
2872
2888
  chunks.push(toDownloadBuffer(chunk));
2873
2889
  loaded += chunk.byteLength;
2874
- onProgress(createProgress(loaded, total));
2890
+ reported = true;
2891
+ onProgress(trackProgress(loaded, total));
2875
2892
  }
2893
+ if (!reported) onProgress(trackProgress(0, total));
2876
2894
  } catch (error) {
2877
2895
  try {
2878
2896
  await reader.cancel(error);
@@ -2898,17 +2916,6 @@ function parseContentLength(value) {
2898
2916
  if (!Number.isFinite(total) || total < 0) return;
2899
2917
  return total;
2900
2918
  }
2901
- function createProgress(loaded, total) {
2902
- if (total === void 0 || total === 0) return {
2903
- loaded,
2904
- total
2905
- };
2906
- return {
2907
- loaded,
2908
- total,
2909
- progress: Math.min(loaded / total, 1)
2910
- };
2911
- }
2912
2919
  //#endregion
2913
2920
  //#region src/plugins/loggerPlugin.ts
2914
2921
  function loggerPlugin(defaultOptions = {}) {
package/dist/index.d.cts CHANGED
@@ -222,23 +222,36 @@ interface RequestLogger {
222
222
  error(message: string, entry: ErrorLogEntry): void | Promise<void>;
223
223
  }
224
224
  type UploadData = FormData | Record<string, unknown>;
225
- /**
226
- * Upload progress information.
227
- */
228
- interface UploadProgress {
225
+ interface TransferProgress {
229
226
  /**
230
- * Number of bytes sent.
227
+ * Cumulative number of bytes transferred.
231
228
  */
232
229
  loaded: number;
233
230
  /**
234
- * Total request size when the browser provides it.
231
+ * Total transfer size when the runtime provides it.
235
232
  */
236
233
  total?: number;
237
234
  /**
238
235
  * Progress ratio between 0 and 1 when total is available.
239
236
  */
240
237
  progress?: number;
238
+ /**
239
+ * Bytes transferred since the previous progress event.
240
+ */
241
+ bytes?: number;
242
+ /**
243
+ * Average transfer rate in bytes per second when measurable.
244
+ */
245
+ rate?: number;
246
+ /**
247
+ * Estimated remaining seconds when total and rate are available.
248
+ */
249
+ estimated?: number;
241
250
  }
251
+ /**
252
+ * Upload progress information.
253
+ */
254
+ interface UploadProgress extends TransferProgress {}
242
255
  interface UploadOptions {
243
256
  data: UploadData;
244
257
  /**
@@ -249,20 +262,7 @@ interface UploadOptions {
249
262
  /**
250
263
  * Download progress information.
251
264
  */
252
- interface DownloadProgress {
253
- /**
254
- * Number of bytes received.
255
- */
256
- loaded: number;
257
- /**
258
- * Total response size when Content-Length is available.
259
- */
260
- total?: number;
261
- /**
262
- * Progress ratio between 0 and 1 when total is available.
263
- */
264
- progress?: number;
265
- }
265
+ interface DownloadProgress extends TransferProgress {}
266
266
  interface DownloadOptions {
267
267
  /**
268
268
  * Reserved legacy field. It does not save or rename the returned Blob.
@@ -1007,4 +1007,4 @@ declare function retryPlugin(defaultOptions?: RetryOptions): Plugin;
1007
1007
  //#region src/plugins/uploadPlugin.d.ts
1008
1008
  declare function uploadPlugin(): Plugin;
1009
1009
  //#endregion
1010
- export { Adapter, AuthOptions, AuthPluginOptions, AuthTokenStorage, CacheEntry, CacheOptions, CachePlugin, CachePluginOptions, CacheStore, CircuitBreakerOptions, CircuitBreakerPlugin, CircuitBreakerPluginOptions, CircuitBreakerStateChange, CircuitState, Client, ClientOptions, ConcurrencyOptions, ConcurrencyPlugin, ConcurrencyPluginOptions, ConcurrencyState, DownloadOptions, DownloadPluginOptions, DownloadProgress, DownloadTransport, ErrorLogEntry, FetchAdapter, FetchOptions, HttpMethod, Interceptor, InterceptorManager, InterceptorOptions, LoggerEntry, LoggerOptions, MemoryCacheStore, MemoryCacheStoreOptions, MockAdapter, MockAdapterOptions, MockHandler, MockReply, MockReplyHandler, MockRequestMatcher, MockResponseOptions, MockRoute, MockURLMatcher, NporaResponse, Plugin, PluginCleanup, PluginContext, PluginError, PluginErrorCode, PluginErrorOptions, PluginHookManager, PluginInterceptorManager, QueryParams, QueryValue, RequestConfig, RequestError, RequestErrorCode, RequestErrorOptions, RequestExtensions, RequestLogEntry, RequestLogger, ResponseLogEntry, ResponseType, RetryEvent, RetryOptions, SchemaValidationError, ServerSentEvent, StandardSchemaV1, UploadData, UploadOptions, UploadProgress, authPlugin, cachePlugin, circuitBreakerPlugin, concurrencyPlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
1010
+ export { Adapter, AuthOptions, AuthPluginOptions, AuthTokenStorage, CacheEntry, CacheOptions, CachePlugin, CachePluginOptions, CacheStore, CircuitBreakerOptions, CircuitBreakerPlugin, CircuitBreakerPluginOptions, CircuitBreakerStateChange, CircuitState, Client, ClientOptions, ConcurrencyOptions, ConcurrencyPlugin, ConcurrencyPluginOptions, ConcurrencyState, DownloadOptions, DownloadPluginOptions, DownloadProgress, DownloadTransport, ErrorLogEntry, FetchAdapter, FetchOptions, HttpMethod, Interceptor, InterceptorManager, InterceptorOptions, LoggerEntry, LoggerOptions, MemoryCacheStore, MemoryCacheStoreOptions, MockAdapter, MockAdapterOptions, MockHandler, MockReply, MockReplyHandler, MockRequestMatcher, MockResponseOptions, MockRoute, MockURLMatcher, NporaResponse, Plugin, PluginCleanup, PluginContext, PluginError, PluginErrorCode, PluginErrorOptions, PluginHookManager, PluginInterceptorManager, QueryParams, QueryValue, RequestConfig, RequestError, RequestErrorCode, RequestErrorOptions, RequestExtensions, RequestLogEntry, RequestLogger, ResponseLogEntry, ResponseType, RetryEvent, RetryOptions, SchemaValidationError, ServerSentEvent, StandardSchemaV1, TransferProgress, UploadData, UploadOptions, UploadProgress, authPlugin, cachePlugin, circuitBreakerPlugin, concurrencyPlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
package/dist/index.d.ts CHANGED
@@ -222,23 +222,36 @@ interface RequestLogger {
222
222
  error(message: string, entry: ErrorLogEntry): void | Promise<void>;
223
223
  }
224
224
  type UploadData = FormData | Record<string, unknown>;
225
- /**
226
- * Upload progress information.
227
- */
228
- interface UploadProgress {
225
+ interface TransferProgress {
229
226
  /**
230
- * Number of bytes sent.
227
+ * Cumulative number of bytes transferred.
231
228
  */
232
229
  loaded: number;
233
230
  /**
234
- * Total request size when the browser provides it.
231
+ * Total transfer size when the runtime provides it.
235
232
  */
236
233
  total?: number;
237
234
  /**
238
235
  * Progress ratio between 0 and 1 when total is available.
239
236
  */
240
237
  progress?: number;
238
+ /**
239
+ * Bytes transferred since the previous progress event.
240
+ */
241
+ bytes?: number;
242
+ /**
243
+ * Average transfer rate in bytes per second when measurable.
244
+ */
245
+ rate?: number;
246
+ /**
247
+ * Estimated remaining seconds when total and rate are available.
248
+ */
249
+ estimated?: number;
241
250
  }
251
+ /**
252
+ * Upload progress information.
253
+ */
254
+ interface UploadProgress extends TransferProgress {}
242
255
  interface UploadOptions {
243
256
  data: UploadData;
244
257
  /**
@@ -249,20 +262,7 @@ interface UploadOptions {
249
262
  /**
250
263
  * Download progress information.
251
264
  */
252
- interface DownloadProgress {
253
- /**
254
- * Number of bytes received.
255
- */
256
- loaded: number;
257
- /**
258
- * Total response size when Content-Length is available.
259
- */
260
- total?: number;
261
- /**
262
- * Progress ratio between 0 and 1 when total is available.
263
- */
264
- progress?: number;
265
- }
265
+ interface DownloadProgress extends TransferProgress {}
266
266
  interface DownloadOptions {
267
267
  /**
268
268
  * Reserved legacy field. It does not save or rename the returned Blob.
@@ -1007,4 +1007,4 @@ declare function retryPlugin(defaultOptions?: RetryOptions): Plugin;
1007
1007
  //#region src/plugins/uploadPlugin.d.ts
1008
1008
  declare function uploadPlugin(): Plugin;
1009
1009
  //#endregion
1010
- export { Adapter, AuthOptions, AuthPluginOptions, AuthTokenStorage, CacheEntry, CacheOptions, CachePlugin, CachePluginOptions, CacheStore, CircuitBreakerOptions, CircuitBreakerPlugin, CircuitBreakerPluginOptions, CircuitBreakerStateChange, CircuitState, Client, ClientOptions, ConcurrencyOptions, ConcurrencyPlugin, ConcurrencyPluginOptions, ConcurrencyState, DownloadOptions, DownloadPluginOptions, DownloadProgress, DownloadTransport, ErrorLogEntry, FetchAdapter, FetchOptions, HttpMethod, Interceptor, InterceptorManager, InterceptorOptions, LoggerEntry, LoggerOptions, MemoryCacheStore, MemoryCacheStoreOptions, MockAdapter, MockAdapterOptions, MockHandler, MockReply, MockReplyHandler, MockRequestMatcher, MockResponseOptions, MockRoute, MockURLMatcher, NporaResponse, Plugin, PluginCleanup, PluginContext, PluginError, PluginErrorCode, PluginErrorOptions, PluginHookManager, PluginInterceptorManager, QueryParams, QueryValue, RequestConfig, RequestError, RequestErrorCode, RequestErrorOptions, RequestExtensions, RequestLogEntry, RequestLogger, ResponseLogEntry, ResponseType, RetryEvent, RetryOptions, SchemaValidationError, ServerSentEvent, StandardSchemaV1, UploadData, UploadOptions, UploadProgress, authPlugin, cachePlugin, circuitBreakerPlugin, concurrencyPlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
1010
+ export { Adapter, AuthOptions, AuthPluginOptions, AuthTokenStorage, CacheEntry, CacheOptions, CachePlugin, CachePluginOptions, CacheStore, CircuitBreakerOptions, CircuitBreakerPlugin, CircuitBreakerPluginOptions, CircuitBreakerStateChange, CircuitState, Client, ClientOptions, ConcurrencyOptions, ConcurrencyPlugin, ConcurrencyPluginOptions, ConcurrencyState, DownloadOptions, DownloadPluginOptions, DownloadProgress, DownloadTransport, ErrorLogEntry, FetchAdapter, FetchOptions, HttpMethod, Interceptor, InterceptorManager, InterceptorOptions, LoggerEntry, LoggerOptions, MemoryCacheStore, MemoryCacheStoreOptions, MockAdapter, MockAdapterOptions, MockHandler, MockReply, MockReplyHandler, MockRequestMatcher, MockResponseOptions, MockRoute, MockURLMatcher, NporaResponse, Plugin, PluginCleanup, PluginContext, PluginError, PluginErrorCode, PluginErrorOptions, PluginHookManager, PluginInterceptorManager, QueryParams, QueryValue, RequestConfig, RequestError, RequestErrorCode, RequestErrorOptions, RequestExtensions, RequestLogEntry, RequestLogger, ResponseLogEntry, ResponseType, RetryEvent, RetryOptions, SchemaValidationError, ServerSentEvent, StandardSchemaV1, TransferProgress, UploadData, UploadOptions, UploadProgress, authPlugin, cachePlugin, circuitBreakerPlugin, concurrencyPlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
package/dist/index.js CHANGED
@@ -2622,6 +2622,30 @@ function createRemovedError(config) {
2622
2622
  });
2623
2623
  }
2624
2624
  //#endregion
2625
+ //#region src/plugins/transferProgress.ts
2626
+ const MIN_RATE_SAMPLE_MILLISECONDS = 250;
2627
+ function createTransferProgressTracker(now = Date.now) {
2628
+ const startedAt = now();
2629
+ let previousLoaded = 0;
2630
+ return (loaded, total) => {
2631
+ const bytes = Math.max(loaded - previousLoaded, 0);
2632
+ const elapsed = Math.max(now() - startedAt, 0) / 1e3;
2633
+ const rate = elapsed >= MIN_RATE_SAMPLE_MILLISECONDS / 1e3 && loaded > 0 ? loaded / elapsed : void 0;
2634
+ const progress = total === void 0 || total === 0 ? void 0 : Math.min(loaded / total, 1);
2635
+ const estimated = rate && total !== void 0 ? Math.max(total - loaded, 0) / rate : void 0;
2636
+ previousLoaded = loaded;
2637
+ const snapshot = {
2638
+ loaded,
2639
+ total,
2640
+ bytes
2641
+ };
2642
+ if (progress !== void 0) snapshot.progress = progress;
2643
+ if (rate !== void 0) snapshot.rate = rate;
2644
+ if (estimated !== void 0) snapshot.estimated = estimated;
2645
+ return snapshot;
2646
+ };
2647
+ }
2648
+ //#endregion
2625
2649
  //#region src/plugins/xhrTransport.ts
2626
2650
  function xhrRequest(config, options = {}) {
2627
2651
  let request;
@@ -2741,6 +2765,7 @@ async function processResponse(xhr, config) {
2741
2765
  }
2742
2766
  function createProgressHandler(callback, abortWith, maxResponseSize, config) {
2743
2767
  if (!callback && !Number.isFinite(maxResponseSize)) return null;
2768
+ const trackProgress = callback ? createTransferProgressTracker() : void 0;
2744
2769
  return (event) => {
2745
2770
  if (Number.isFinite(maxResponseSize) && event.loaded > (maxResponseSize ?? Number.POSITIVE_INFINITY)) {
2746
2771
  abortWith(new RequestError(`Response body exceeds maxResponseSize ${maxResponseSize}`, {
@@ -2749,25 +2774,14 @@ function createProgressHandler(callback, abortWith, maxResponseSize, config) {
2749
2774
  }));
2750
2775
  return;
2751
2776
  }
2752
- if (!callback) return;
2777
+ if (!callback || !trackProgress) return;
2753
2778
  try {
2754
- callback(createProgress$1(event.loaded, event.lengthComputable ? event.total : void 0));
2779
+ callback(trackProgress(event.loaded, event.lengthComputable ? event.total : void 0));
2755
2780
  } catch (error) {
2756
2781
  abortWith(error);
2757
2782
  }
2758
2783
  };
2759
2784
  }
2760
- function createProgress$1(loaded, total) {
2761
- if (total === void 0 || total === 0) return {
2762
- loaded,
2763
- total
2764
- };
2765
- return {
2766
- loaded,
2767
- total,
2768
- progress: Math.min(loaded / total, 1)
2769
- };
2770
- }
2771
2785
  function parseHeaders(value) {
2772
2786
  const headers = new Headers();
2773
2787
  for (const line of value.split(/\r?\n/)) {
@@ -2862,7 +2876,9 @@ async function consumeDownloadStream(stream, response, onProgress) {
2862
2876
  const reader = stream.getReader();
2863
2877
  const chunks = [];
2864
2878
  const total = parseContentLength(response.headers.get("content-length"));
2879
+ const trackProgress = createTransferProgressTracker();
2865
2880
  let loaded = 0;
2881
+ let reported = false;
2866
2882
  try {
2867
2883
  while (true) {
2868
2884
  const result = await reader.read();
@@ -2870,8 +2886,10 @@ async function consumeDownloadStream(stream, response, onProgress) {
2870
2886
  const chunk = result.value;
2871
2887
  chunks.push(toDownloadBuffer(chunk));
2872
2888
  loaded += chunk.byteLength;
2873
- onProgress(createProgress(loaded, total));
2889
+ reported = true;
2890
+ onProgress(trackProgress(loaded, total));
2874
2891
  }
2892
+ if (!reported) onProgress(trackProgress(0, total));
2875
2893
  } catch (error) {
2876
2894
  try {
2877
2895
  await reader.cancel(error);
@@ -2897,17 +2915,6 @@ function parseContentLength(value) {
2897
2915
  if (!Number.isFinite(total) || total < 0) return;
2898
2916
  return total;
2899
2917
  }
2900
- function createProgress(loaded, total) {
2901
- if (total === void 0 || total === 0) return {
2902
- loaded,
2903
- total
2904
- };
2905
- return {
2906
- loaded,
2907
- total,
2908
- progress: Math.min(loaded / total, 1)
2909
- };
2910
- }
2911
2918
  //#endregion
2912
2919
  //#region src/plugins/loggerPlugin.ts
2913
2920
  function loggerPlugin(defaultOptions = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@npora/request",
3
- "version": "1.12.0",
3
+ "version": "1.13.0",
4
4
  "description": "A modern, TypeScript-first HTTP client powered by the Fetch API.",
5
5
  "keywords": [
6
6
  "npora",