@rebilly/instruments 9.68.1 → 9.69.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.
package/CHANGELOG.md CHANGED
@@ -1 +1 @@
1
- ## [9.68.1](https://github.com/Rebilly/rebilly/compare/instruments/core-v9.68.0...instruments/core-v9.68.1) (2024-07-29)
1
+ ## [9.69.1](https://github.com/Rebilly/rebilly/compare/instruments/core-v9.69.0...instruments/core-v9.69.1) (2024-08-07)
package/dist/index.js CHANGED
@@ -3690,6 +3690,26 @@ const toJSONObject = (obj) => {
3690
3690
  };
3691
3691
  const isAsyncFn = kindOfTest("AsyncFunction");
3692
3692
  const isThenable = (thing) => thing && (isObject$1(thing) || isFunction(thing)) && isFunction(thing.then) && isFunction(thing.catch);
3693
+ const _setImmediate = ((setImmediateSupported, postMessageSupported) => {
3694
+ if (setImmediateSupported) {
3695
+ return setImmediate;
3696
+ }
3697
+ return postMessageSupported ? ((token, callbacks) => {
3698
+ _global.addEventListener("message", ({ source, data }) => {
3699
+ if (source === _global && data === token) {
3700
+ callbacks.length && callbacks.shift()();
3701
+ }
3702
+ }, false);
3703
+ return (cb) => {
3704
+ callbacks.push(cb);
3705
+ _global.postMessage(token, "*");
3706
+ };
3707
+ })(`axios@${Math.random()}`, []) : (cb) => setTimeout(cb);
3708
+ })(
3709
+ typeof setImmediate === "function",
3710
+ isFunction(_global.postMessage)
3711
+ );
3712
+ const asap = typeof queueMicrotask !== "undefined" ? queueMicrotask.bind(_global) : typeof process !== "undefined" && process.nextTick || _setImmediate;
3693
3713
  const utils$2 = {
3694
3714
  isArray,
3695
3715
  isArrayBuffer,
@@ -3746,7 +3766,9 @@ const utils$2 = {
3746
3766
  isSpecCompliantForm,
3747
3767
  toJSONObject,
3748
3768
  isAsyncFn,
3749
- isThenable
3769
+ isThenable,
3770
+ setImmediate: _setImmediate,
3771
+ asap
3750
3772
  };
3751
3773
  function AxiosError(message, code2, config, request, response) {
3752
3774
  Error.call(this);
@@ -4581,27 +4603,35 @@ function speedometer(samplesCount, min) {
4581
4603
  }
4582
4604
  function throttle(fn, freq) {
4583
4605
  let timestamp = 0;
4584
- const threshold = 1e3 / freq;
4585
- let timer = null;
4586
- return function throttled() {
4587
- const force = this === true;
4606
+ let threshold = 1e3 / freq;
4607
+ let lastArgs;
4608
+ let timer;
4609
+ const invoke = (args, now = Date.now()) => {
4610
+ timestamp = now;
4611
+ lastArgs = null;
4612
+ if (timer) {
4613
+ clearTimeout(timer);
4614
+ timer = null;
4615
+ }
4616
+ fn.apply(null, args);
4617
+ };
4618
+ const throttled = (...args) => {
4588
4619
  const now = Date.now();
4589
- if (force || now - timestamp > threshold) {
4590
- if (timer) {
4591
- clearTimeout(timer);
4592
- timer = null;
4620
+ const passed = now - timestamp;
4621
+ if (passed >= threshold) {
4622
+ invoke(args, now);
4623
+ } else {
4624
+ lastArgs = args;
4625
+ if (!timer) {
4626
+ timer = setTimeout(() => {
4627
+ timer = null;
4628
+ invoke(lastArgs);
4629
+ }, threshold - passed);
4593
4630
  }
4594
- timestamp = now;
4595
- return fn.apply(null, arguments);
4596
- }
4597
- if (!timer) {
4598
- timer = setTimeout(() => {
4599
- timer = null;
4600
- timestamp = Date.now();
4601
- return fn.apply(null, arguments);
4602
- }, threshold - (now - timestamp));
4603
4631
  }
4604
4632
  };
4633
+ const flush = () => lastArgs && invoke(lastArgs);
4634
+ return [throttled, flush];
4605
4635
  }
4606
4636
  const progressEventReducer = (listener, isDownloadStream, freq = 3) => {
4607
4637
  let bytesNotified = 0;
@@ -4621,12 +4651,21 @@ const progressEventReducer = (listener, isDownloadStream, freq = 3) => {
4621
4651
  rate: rate ? rate : void 0,
4622
4652
  estimated: rate && total && inRange ? (total - loaded) / rate : void 0,
4623
4653
  event: e2,
4624
- lengthComputable: total != null
4654
+ lengthComputable: total != null,
4655
+ [isDownloadStream ? "download" : "upload"]: true
4625
4656
  };
4626
- data[isDownloadStream ? "download" : "upload"] = true;
4627
4657
  listener(data);
4628
4658
  }, freq);
4629
4659
  };
4660
+ const progressEventDecorator = (total, throttled) => {
4661
+ const lengthComputable = total != null;
4662
+ return [(loaded) => throttled[0]({
4663
+ lengthComputable,
4664
+ total,
4665
+ loaded
4666
+ }), throttled[1]];
4667
+ };
4668
+ const asyncDecorator = (fn) => (...args) => utils$2.asap(() => fn(...args));
4630
4669
  const isURLSameOrigin = platform.hasStandardBrowserEnv ? (
4631
4670
  // Standard browser envs have full support of the APIs needed to test
4632
4671
  // whether the request URL is of the same origin as current location.
@@ -4824,15 +4863,15 @@ const xhrAdapter = isXHRAdapterSupported && function(config) {
4824
4863
  const _config = resolveConfig(config);
4825
4864
  let requestData = _config.data;
4826
4865
  const requestHeaders = AxiosHeaders.from(_config.headers).normalize();
4827
- let { responseType } = _config;
4866
+ let { responseType, onUploadProgress, onDownloadProgress } = _config;
4828
4867
  let onCanceled;
4868
+ let uploadThrottled, downloadThrottled;
4869
+ let flushUpload, flushDownload;
4829
4870
  function done() {
4830
- if (_config.cancelToken) {
4831
- _config.cancelToken.unsubscribe(onCanceled);
4832
- }
4833
- if (_config.signal) {
4834
- _config.signal.removeEventListener("abort", onCanceled);
4835
- }
4871
+ flushUpload && flushUpload();
4872
+ flushDownload && flushDownload();
4873
+ _config.cancelToken && _config.cancelToken.unsubscribe(onCanceled);
4874
+ _config.signal && _config.signal.removeEventListener("abort", onCanceled);
4836
4875
  }
4837
4876
  let request = new XMLHttpRequest();
4838
4877
  request.open(_config.method.toUpperCase(), _config.url, true);
@@ -4879,11 +4918,11 @@ const xhrAdapter = isXHRAdapterSupported && function(config) {
4879
4918
  if (!request) {
4880
4919
  return;
4881
4920
  }
4882
- reject(new AxiosError("Request aborted", AxiosError.ECONNABORTED, _config, request));
4921
+ reject(new AxiosError("Request aborted", AxiosError.ECONNABORTED, config, request));
4883
4922
  request = null;
4884
4923
  };
4885
4924
  request.onerror = function handleError() {
4886
- reject(new AxiosError("Network Error", AxiosError.ERR_NETWORK, _config, request));
4925
+ reject(new AxiosError("Network Error", AxiosError.ERR_NETWORK, config, request));
4887
4926
  request = null;
4888
4927
  };
4889
4928
  request.ontimeout = function handleTimeout() {
@@ -4895,7 +4934,7 @@ const xhrAdapter = isXHRAdapterSupported && function(config) {
4895
4934
  reject(new AxiosError(
4896
4935
  timeoutErrorMessage,
4897
4936
  transitional2.clarifyTimeoutError ? AxiosError.ETIMEDOUT : AxiosError.ECONNABORTED,
4898
- _config,
4937
+ config,
4899
4938
  request
4900
4939
  ));
4901
4940
  request = null;
@@ -4912,11 +4951,14 @@ const xhrAdapter = isXHRAdapterSupported && function(config) {
4912
4951
  if (responseType && responseType !== "json") {
4913
4952
  request.responseType = _config.responseType;
4914
4953
  }
4915
- if (typeof _config.onDownloadProgress === "function") {
4916
- request.addEventListener("progress", progressEventReducer(_config.onDownloadProgress, true));
4954
+ if (onDownloadProgress) {
4955
+ [downloadThrottled, flushDownload] = progressEventReducer(onDownloadProgress, true);
4956
+ request.addEventListener("progress", downloadThrottled);
4917
4957
  }
4918
- if (typeof _config.onUploadProgress === "function" && request.upload) {
4919
- request.upload.addEventListener("progress", progressEventReducer(_config.onUploadProgress));
4958
+ if (onUploadProgress && request.upload) {
4959
+ [uploadThrottled, flushUpload] = progressEventReducer(onUploadProgress);
4960
+ request.upload.addEventListener("progress", uploadThrottled);
4961
+ request.upload.addEventListener("loadend", flushUpload);
4920
4962
  }
4921
4963
  if (_config.cancelToken || _config.signal) {
4922
4964
  onCanceled = (cancel) => {
@@ -4994,39 +5036,52 @@ const readBytes = async function* (iterable, chunkSize, encode2) {
4994
5036
  const trackStream = (stream, chunkSize, onProgress, onFinish, encode2) => {
4995
5037
  const iterator = readBytes(stream, chunkSize, encode2);
4996
5038
  let bytes = 0;
5039
+ let done;
5040
+ let _onFinish = (e2) => {
5041
+ if (!done) {
5042
+ done = true;
5043
+ onFinish && onFinish(e2);
5044
+ }
5045
+ };
4997
5046
  return new ReadableStream({
4998
- type: "bytes",
4999
5047
  async pull(controller) {
5000
- const { done, value } = await iterator.next();
5001
- if (done) {
5002
- controller.close();
5003
- onFinish();
5004
- return;
5048
+ try {
5049
+ const { done: done2, value } = await iterator.next();
5050
+ if (done2) {
5051
+ _onFinish();
5052
+ controller.close();
5053
+ return;
5054
+ }
5055
+ let len = value.byteLength;
5056
+ if (onProgress) {
5057
+ let loadedBytes = bytes += len;
5058
+ onProgress(loadedBytes);
5059
+ }
5060
+ controller.enqueue(new Uint8Array(value));
5061
+ } catch (err) {
5062
+ _onFinish(err);
5063
+ throw err;
5005
5064
  }
5006
- let len = value.byteLength;
5007
- onProgress && onProgress(bytes += len);
5008
- controller.enqueue(new Uint8Array(value));
5009
5065
  },
5010
5066
  cancel(reason) {
5011
- onFinish(reason);
5067
+ _onFinish(reason);
5012
5068
  return iterator.return();
5013
5069
  }
5014
5070
  }, {
5015
5071
  highWaterMark: 2
5016
5072
  });
5017
5073
  };
5018
- const fetchProgressDecorator = (total, fn) => {
5019
- const lengthComputable = total != null;
5020
- return (loaded) => setTimeout(() => fn({
5021
- lengthComputable,
5022
- total,
5023
- loaded
5024
- }));
5025
- };
5026
5074
  const isFetchSupported = typeof fetch === "function" && typeof Request === "function" && typeof Response === "function";
5027
5075
  const isReadableStreamSupported = isFetchSupported && typeof ReadableStream === "function";
5028
5076
  const encodeText = isFetchSupported && (typeof TextEncoder === "function" ? /* @__PURE__ */ ((encoder) => (str) => encoder.encode(str))(new TextEncoder()) : async (str) => new Uint8Array(await new Response(str).arrayBuffer()));
5029
- const supportsRequestStream = isReadableStreamSupported && (() => {
5077
+ const test = (fn, ...args) => {
5078
+ try {
5079
+ return !!fn(...args);
5080
+ } catch (e2) {
5081
+ return false;
5082
+ }
5083
+ };
5084
+ const supportsRequestStream = isReadableStreamSupported && test(() => {
5030
5085
  let duplexAccessed = false;
5031
5086
  const hasContentType = new Request(platform.origin, {
5032
5087
  body: new ReadableStream(),
@@ -5037,14 +5092,9 @@ const supportsRequestStream = isReadableStreamSupported && (() => {
5037
5092
  }
5038
5093
  }).headers.has("Content-Type");
5039
5094
  return duplexAccessed && !hasContentType;
5040
- })();
5095
+ });
5041
5096
  const DEFAULT_CHUNK_SIZE = 64 * 1024;
5042
- const supportsResponseStream = isReadableStreamSupported && !!(() => {
5043
- try {
5044
- return utils$2.isReadableStream(new Response("").body);
5045
- } catch (err) {
5046
- }
5047
- })();
5097
+ const supportsResponseStream = isReadableStreamSupported && test(() => utils$2.isReadableStream(new Response("").body));
5048
5098
  const resolvers = {
5049
5099
  stream: supportsResponseStream && ((res) => res.body)
5050
5100
  };
@@ -5065,7 +5115,7 @@ const getBodyLength = async (body) => {
5065
5115
  if (utils$2.isSpecCompliantForm(body)) {
5066
5116
  return (await new Request(body).arrayBuffer()).byteLength;
5067
5117
  }
5068
- if (utils$2.isArrayBufferView(body)) {
5118
+ if (utils$2.isArrayBufferView(body) || utils$2.isArrayBuffer(body)) {
5069
5119
  return body.byteLength;
5070
5120
  }
5071
5121
  if (utils$2.isURLSearchParams(body)) {
@@ -5116,14 +5166,15 @@ const fetchAdapter = isFetchSupported && (async (config) => {
5116
5166
  headers.setContentType(contentTypeHeader);
5117
5167
  }
5118
5168
  if (_request.body) {
5119
- data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, fetchProgressDecorator(
5169
+ const [onProgress, flush] = progressEventDecorator(
5120
5170
  requestContentLength,
5121
- progressEventReducer(onUploadProgress)
5122
- ), null, encodeText);
5171
+ progressEventReducer(asyncDecorator(onUploadProgress))
5172
+ );
5173
+ data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, onProgress, flush, encodeText);
5123
5174
  }
5124
5175
  }
5125
5176
  if (!utils$2.isString(withCredentials)) {
5126
- withCredentials = withCredentials ? "cors" : "omit";
5177
+ withCredentials = withCredentials ? "include" : "omit";
5127
5178
  }
5128
5179
  request = new Request(url, {
5129
5180
  ...fetchOptions,
@@ -5132,7 +5183,7 @@ const fetchAdapter = isFetchSupported && (async (config) => {
5132
5183
  headers: headers.normalize().toJSON(),
5133
5184
  body: data,
5134
5185
  duplex: "half",
5135
- withCredentials
5186
+ credentials: withCredentials
5136
5187
  });
5137
5188
  let response = await fetch(request);
5138
5189
  const isStreamResponse = supportsResponseStream && (responseType === "stream" || responseType === "response");
@@ -5142,11 +5193,15 @@ const fetchAdapter = isFetchSupported && (async (config) => {
5142
5193
  options[prop] = response[prop];
5143
5194
  });
5144
5195
  const responseContentLength = utils$2.toFiniteNumber(response.headers.get("content-length"));
5196
+ const [onProgress, flush] = onDownloadProgress && progressEventDecorator(
5197
+ responseContentLength,
5198
+ progressEventReducer(asyncDecorator(onDownloadProgress), true)
5199
+ ) || [];
5145
5200
  response = new Response(
5146
- trackStream(response.body, DEFAULT_CHUNK_SIZE, onDownloadProgress && fetchProgressDecorator(
5147
- responseContentLength,
5148
- progressEventReducer(onDownloadProgress, true)
5149
- ), isStreamResponse && onFinish, encodeText),
5201
+ trackStream(response.body, DEFAULT_CHUNK_SIZE, onProgress, () => {
5202
+ flush && flush();
5203
+ isStreamResponse && onFinish();
5204
+ }, encodeText),
5150
5205
  options
5151
5206
  );
5152
5207
  }
@@ -5272,7 +5327,7 @@ function dispatchRequest(config) {
5272
5327
  return Promise.reject(reason);
5273
5328
  });
5274
5329
  }
5275
- const VERSION = "1.7.2";
5330
+ const VERSION = "1.7.3";
5276
5331
  const validators$1 = {};
5277
5332
  ["object", "boolean", "number", "function", "string", "symbol"].forEach((type2, i) => {
5278
5333
  validators$1[type2] = function validator2(thing) {