foxts 4.1.0 → 4.2.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.
@@ -0,0 +1,15 @@
1
+ import { ErrorLikeObject } from '../extract-error-message/index.js';
2
+
3
+ interface AbortErrorLike extends Omit<ErrorLikeObject, 'name'> {
4
+ name: 'AbortError';
5
+ code?: 'ABORT_ERR';
6
+ }
7
+ declare class AbortError extends Error implements AbortErrorLike {
8
+ readonly name = "AbortError";
9
+ readonly code = "ABORT_ERR";
10
+ constructor(message?: string, options?: ErrorOptions);
11
+ }
12
+ declare function isAbortErrorLike(error: unknown): error is AbortErrorLike;
13
+
14
+ export { AbortError, isAbortErrorLike };
15
+ export type { AbortErrorLike };
@@ -0,0 +1 @@
1
+ "use strict";var r=require("../extract-error-message/index.js");exports.AbortError=class extends Error{name="AbortError";code="ABORT_ERR";constructor(r="The operation was aborted",e){super(r,e)}},exports.isAbortErrorLike=function(e){return!!r.isErrorLikeObject(e)&&("AbortError"===e.name||"code"in e&&"string"==typeof e.code&&"ABORT_ERR"===e.code)};
@@ -0,0 +1 @@
1
+ import{isErrorLikeObject as r}from"../extract-error-message/index.mjs";class o extends Error{name="AbortError";code="ABORT_ERR";constructor(r="The operation was aborted",o){super(r,o)}}function e(o){return!!r(o)&&("AbortError"===o.name||"code"in o&&"string"==typeof o.code&&"ABORT_ERR"===o.code)}export{o as AbortError,e as isAbortErrorLike};
@@ -1 +1 @@
1
- "use strict";var t=require("../extract-error-message/index.js"),e=require("../is-network-error/index.js"),r=require("../noop/index.js");function o(t,e,{min:r=0,allowInfinity:i=!1}={}){if(Number.isNaN(e))throw TypeError(`Expected \`${t}\` to be a valid number${i?" or Infinity":""}, got NaN.`);if(!i&&!Number.isFinite(e))throw TypeError(`Expected \`${t}\` to be a finite number.`);if(e<r)throw TypeError(`Expected \`${t}\` to be \u2265 ${r}.`)}class i extends Error{name="AsyncRetryAbortError";cause;constructor(e){if(super("string"==typeof e?e:t.extractErrorMessage(e,!1)??"Aborted"),"string"==typeof e){const t=Error(e);t.stack=this.stack,this.cause=t}else e instanceof Error?(this.cause=e,this.message=e.message):this.cause=e}}async function n(r,o,n,a,s){if(r instanceof i)throw r.cause;if(r instanceof TypeError&&!e.isNetworkError(r)||t.isErrorLikeObject(r)&&"AbortError"===r.name)throw r;const c=n.retries-(o-1),m={error:r,attemptNumber:o,retriesLeft:c};await n.onFailedAttempt(m),o>1&&await n.onRetry(r,o-1);const u=Date.now();if(u-a>=s||o>=n.retries+1||!await n.shouldRetry(m))throw r;let f=Math.round((n.randomize?Math.random()+1:1)*n.minTimeout*n.factor**(o-1));f=Math.min(f,n.maxTimeout);const l=s-(u-a);if(l<=0)throw r;const w=Math.min(f,l);w>0&&await new Promise((t,e)=>{const r=setTimeout(()=>{n.signal?.removeEventListener("abort",o),t()},w);function o(){clearTimeout(r),n.signal?.removeEventListener("abort",o),e(n.signal?.reason)}n.unref&&"object"==typeof r&&"unref"in r&&"function"==typeof r.unref&&r.unref(),n.signal?.addEventListener("abort",o,{once:!0})}),n.signal?.throwIfAborted()}function a(t){throw new i(t??"Aborted")}async function s(t,e={}){const c={...e};c.retries??=10,c.forever??=!1,c.factor??=2,c.minTimeout??=1e3,c.maxTimeout??=1/0,c.randomize??=!0,c.onFailedAttempt??=r.noop,c.onRetry??=r.noop,c.shouldRetry??=r.trueFn,c.forever&&(c.retries=1/0),o("retries",c.retries,{min:0,allowInfinity:!0}),o("factor",c.factor,{min:0,allowInfinity:!1}),o("minTimeout",c.minTimeout,{min:0,allowInfinity:!1}),o("maxTimeout",c.maxTimeout,{min:0,allowInfinity:!0});const m=c.maxRetryTime??1/0;o("maxRetryTime",m,{min:0,allowInfinity:!0}),c.minTimeout=Math.max(c.minTimeout,1),c.factor<=0&&(c.factor=1),c.signal?.throwIfAborted();let u=0;const f=Date.now();for(;u<c.retries+1;){u++;try{c.signal?.throwIfAborted();const e=await t(a,u);return c.signal?.throwIfAborted(),e}catch(e){let t=e;"object"==typeof e&&e&&"bail"in e&&e.bail&&(t=new i(e)),await n(t,u,c,f,m)}}throw Error("Retry attempts exhausted without throwing an error.")}exports.AsyncRetryAbortError=i,exports.asyncRetry=s,exports.makeRetriable=function(t,e){return function(...r){return s(()=>t.apply(this,r),e)}};
1
+ "use strict";var r=require("../abort-error/index.js"),t=require("../extract-error-message/index.js"),e=require("../is-network-error/index.js"),o=require("../noop/index.js");function i(r,t,{min:e=0,allowInfinity:o=!1}={}){if(Number.isNaN(t))throw TypeError(`Expected \`${r}\` to be a valid number${o?" or Infinity":""}, got NaN.`);if(!o&&!Number.isFinite(t))throw TypeError(`Expected \`${r}\` to be a finite number.`);if(t<e)throw TypeError(`Expected \`${r}\` to be \u2265 ${e}.`)}class n extends Error{name="AsyncRetryAbortError";cause;constructor(r){if(super("string"==typeof r?r:t.extractErrorMessage(r,!1)??"Aborted"),"string"==typeof r){const t=Error(r);t.stack=this.stack,this.cause=t}else r instanceof Error?(this.cause=r,this.message=r.message):this.cause=r}}async function a(t,o,i,a,s){if(t instanceof n)throw t.cause;if(t instanceof TypeError&&!e.isNetworkError(t)||r.isAbortErrorLike(t))throw t;const c=i.retries-(o-1),m={error:t,attemptNumber:o,retriesLeft:c};await i.onFailedAttempt(m),o>1&&await i.onRetry(t,o-1);const u=Date.now();if(u-a>=s||o>=i.retries+1||!await i.shouldRetry(m))throw t;let f=Math.round((i.randomize?Math.random()+1:1)*i.minTimeout*i.factor**(o-1));f=Math.min(f,i.maxTimeout);const l=s-(u-a);if(l<=0)throw t;const w=Math.min(f,l);w>0&&await new Promise((r,t)=>{const e=setTimeout(()=>{i.signal?.removeEventListener("abort",o),r()},w);function o(){clearTimeout(e),i.signal?.removeEventListener("abort",o),t(i.signal?.reason)}i.unref&&"object"==typeof e&&"unref"in e&&"function"==typeof e.unref&&e.unref(),i.signal?.addEventListener("abort",o,{once:!0})}),i.signal?.throwIfAborted()}function s(r){throw new n(r??"Aborted")}async function c(r,t={}){const e={...t};e.retries??=10,e.forever??=!1,e.factor??=2,e.minTimeout??=1e3,e.maxTimeout??=1/0,e.randomize??=!0,e.onFailedAttempt??=o.noop,e.onRetry??=o.noop,e.shouldRetry??=o.trueFn,e.forever&&(e.retries=1/0),i("retries",e.retries,{min:0,allowInfinity:!0}),i("factor",e.factor,{min:0,allowInfinity:!1}),i("minTimeout",e.minTimeout,{min:0,allowInfinity:!1}),i("maxTimeout",e.maxTimeout,{min:0,allowInfinity:!0});const m=e.maxRetryTime??1/0;i("maxRetryTime",m,{min:0,allowInfinity:!0}),e.minTimeout=Math.max(e.minTimeout,1),e.factor<=0&&(e.factor=1),e.signal?.throwIfAborted();let u=0;const f=Date.now();for(;u<e.retries+1;){u++;try{e.signal?.throwIfAborted();const t=await r(s,u);return e.signal?.throwIfAborted(),t}catch(t){let r=t;"object"==typeof t&&t&&"bail"in t&&t.bail&&(r=new n(t)),await a(r,u,e,f,m)}}throw Error("Retry attempts exhausted without throwing an error.")}exports.AsyncRetryAbortError=n,exports.asyncRetry=c,exports.makeRetriable=function(r,t){return function(...e){return c(()=>r.apply(this,e),t)}};
@@ -1 +1 @@
1
- import{extractErrorMessage as t,isErrorLikeObject as e}from"../extract-error-message/index.mjs";import{isNetworkError as r}from"../is-network-error/index.mjs";import{noop as o,trueFn as n}from"../noop/index.mjs";function i(t,e,{min:r=0,allowInfinity:o=!1}={}){if(Number.isNaN(e))throw TypeError(`Expected \`${t}\` to be a valid number${o?" or Infinity":""}, got NaN.`);if(!o&&!Number.isFinite(e))throw TypeError(`Expected \`${t}\` to be a finite number.`);if(e<r)throw TypeError(`Expected \`${t}\` to be \u2265 ${r}.`)}class a extends Error{name="AsyncRetryAbortError";cause;constructor(e){if(super("string"==typeof e?e:t(e,!1)??"Aborted"),"string"==typeof e){const t=Error(e);t.stack=this.stack,this.cause=t}else e instanceof Error?(this.cause=e,this.message=e.message):this.cause=e}}async function s(t,o,n,i,s){if(t instanceof a)throw t.cause;if(t instanceof TypeError&&!r(t)||e(t)&&"AbortError"===t.name)throw t;const m=n.retries-(o-1),c={error:t,attemptNumber:o,retriesLeft:m};await n.onFailedAttempt(c),o>1&&await n.onRetry(t,o-1);const f=Date.now();if(f-i>=s||o>=n.retries+1||!await n.shouldRetry(c))throw t;let u=Math.round((n.randomize?Math.random()+1:1)*n.minTimeout*n.factor**(o-1));u=Math.min(u,n.maxTimeout);const l=s-(f-i);if(l<=0)throw t;const w=Math.min(u,l);w>0&&await new Promise((t,e)=>{const r=setTimeout(()=>{n.signal?.removeEventListener("abort",o),t()},w);function o(){clearTimeout(r),n.signal?.removeEventListener("abort",o),e(n.signal?.reason)}n.unref&&"object"==typeof r&&"unref"in r&&"function"==typeof r.unref&&r.unref(),n.signal?.addEventListener("abort",o,{once:!0})}),n.signal?.throwIfAborted()}function m(t){throw new a(t??"Aborted")}async function c(t,e={}){const r={...e};r.retries??=10,r.forever??=!1,r.factor??=2,r.minTimeout??=1e3,r.maxTimeout??=1/0,r.randomize??=!0,r.onFailedAttempt??=o,r.onRetry??=o,r.shouldRetry??=n,r.forever&&(r.retries=1/0),i("retries",r.retries,{min:0,allowInfinity:!0}),i("factor",r.factor,{min:0,allowInfinity:!1}),i("minTimeout",r.minTimeout,{min:0,allowInfinity:!1}),i("maxTimeout",r.maxTimeout,{min:0,allowInfinity:!0});const f=r.maxRetryTime??1/0;i("maxRetryTime",f,{min:0,allowInfinity:!0}),r.minTimeout=Math.max(r.minTimeout,1),r.factor<=0&&(r.factor=1),r.signal?.throwIfAborted();let u=0;const l=Date.now();for(;u<r.retries+1;){u++;try{r.signal?.throwIfAborted();const e=await t(m,u);return r.signal?.throwIfAborted(),e}catch(e){let t=e;"object"==typeof e&&e&&"bail"in e&&e.bail&&(t=new a(e)),await s(t,u,r,l,f)}}throw Error("Retry attempts exhausted without throwing an error.")}function f(t,e){return function(...r){return c(()=>t.apply(this,r),e)}}export{a as AsyncRetryAbortError,c as asyncRetry,f as makeRetriable};
1
+ import{isAbortErrorLike as t}from"../abort-error/index.mjs";import{extractErrorMessage as r}from"../extract-error-message/index.mjs";import{isNetworkError as e}from"../is-network-error/index.mjs";import{noop as o,trueFn as i}from"../noop/index.mjs";function n(t,r,{min:e=0,allowInfinity:o=!1}={}){if(Number.isNaN(r))throw TypeError(`Expected \`${t}\` to be a valid number${o?" or Infinity":""}, got NaN.`);if(!o&&!Number.isFinite(r))throw TypeError(`Expected \`${t}\` to be a finite number.`);if(r<e)throw TypeError(`Expected \`${t}\` to be \u2265 ${e}.`)}class a extends Error{name="AsyncRetryAbortError";cause;constructor(t){if(super("string"==typeof t?t:r(t,!1)??"Aborted"),"string"==typeof t){const r=Error(t);r.stack=this.stack,this.cause=r}else t instanceof Error?(this.cause=t,this.message=t.message):this.cause=t}}async function s(r,o,i,n,s){if(r instanceof a)throw r.cause;if(r instanceof TypeError&&!e(r)||t(r))throw r;const m=i.retries-(o-1),c={error:r,attemptNumber:o,retriesLeft:m};await i.onFailedAttempt(c),o>1&&await i.onRetry(r,o-1);const f=Date.now();if(f-n>=s||o>=i.retries+1||!await i.shouldRetry(c))throw r;let u=Math.round((i.randomize?Math.random()+1:1)*i.minTimeout*i.factor**(o-1));u=Math.min(u,i.maxTimeout);const l=s-(f-n);if(l<=0)throw r;const w=Math.min(u,l);w>0&&await new Promise((t,r)=>{const e=setTimeout(()=>{i.signal?.removeEventListener("abort",o),t()},w);function o(){clearTimeout(e),i.signal?.removeEventListener("abort",o),r(i.signal?.reason)}i.unref&&"object"==typeof e&&"unref"in e&&"function"==typeof e.unref&&e.unref(),i.signal?.addEventListener("abort",o,{once:!0})}),i.signal?.throwIfAborted()}function m(t){throw new a(t??"Aborted")}async function c(t,r={}){const e={...r};e.retries??=10,e.forever??=!1,e.factor??=2,e.minTimeout??=1e3,e.maxTimeout??=1/0,e.randomize??=!0,e.onFailedAttempt??=o,e.onRetry??=o,e.shouldRetry??=i,e.forever&&(e.retries=1/0),n("retries",e.retries,{min:0,allowInfinity:!0}),n("factor",e.factor,{min:0,allowInfinity:!1}),n("minTimeout",e.minTimeout,{min:0,allowInfinity:!1}),n("maxTimeout",e.maxTimeout,{min:0,allowInfinity:!0});const f=e.maxRetryTime??1/0;n("maxRetryTime",f,{min:0,allowInfinity:!0}),e.minTimeout=Math.max(e.minTimeout,1),e.factor<=0&&(e.factor=1),e.signal?.throwIfAborted();let u=0;const l=Date.now();for(;u<e.retries+1;){u++;try{e.signal?.throwIfAborted();const r=await t(m,u);return e.signal?.throwIfAborted(),r}catch(r){let t=r;"object"==typeof r&&r&&"bail"in r&&r.bail&&(t=new a(r)),await s(t,u,e,l,f)}}throw Error("Retry attempts exhausted without throwing an error.")}function f(t,r){return function(...e){return c(()=>t.apply(this,e),r)}}export{a as AsyncRetryAbortError,c as asyncRetry,f as makeRetriable};
@@ -0,0 +1,12 @@
1
+ type FalsyValue = null | undefined | false | '' | 0 | void;
2
+ type TruthyValue = Record<string, unknown> | unknown[] | symbol | ((..._args: unknown[]) => unknown) | Exclude<number, 0> | Exclude<string, ''> | true;
3
+ type CleanupFn = () => void;
4
+ type OnCleanup = (cleanUpFn: CleanupFn) => void;
5
+ type Scheduler = (callback: () => Promise<void>, onCleanup: OnCleanup) => void | CleanupFn;
6
+ type Predicate<T extends TruthyValue | FalsyValue> = () => T | Promise<T>;
7
+ declare function waitUntil<T extends TruthyValue | FalsyValue>(predicate: Predicate<T>, scheduler: Scheduler, abortSignal?: AbortSignal | null): Promise<T>;
8
+ declare function waitUntil<T extends TruthyValue | FalsyValue>(predicate: Predicate<T>, checkInterval?: number, abortSignal?: AbortSignal | null): Promise<T>;
9
+ declare function waitUntil<T extends TruthyValue | FalsyValue>(predicate: Predicate<T>, abortSignal: AbortSignal): Promise<T>;
10
+
11
+ export { waitUntil };
12
+ export type { CleanupFn, OnCleanup, Predicate, Scheduler };
@@ -0,0 +1 @@
1
+ "use strict";exports.waitUntil=function(t,e=50,o=null){if("object"==typeof e&&"aborted"in e&&(o=e,e=50),"number"==typeof e){const t=e;e=(e,o)=>{const n=setTimeout(e,t);o(()=>clearTimeout(n))}}o??=AbortSignal.timeout(5e3);const n=new Set,r=t=>n.add(t);return new Promise((n,i)=>{const c=async()=>{try{o.throwIfAborted();const i=await t();if(i)return void n(i);e(c,r)}catch(t){i(t)}};c()}).finally(()=>{n.forEach(t=>t())})};
@@ -0,0 +1 @@
1
+ function t(e,o=50,n=null){if("object"==typeof o&&"aborted"in o&&(n=o,o=50),"number"==typeof o){const t=o;o=(e,o)=>{const n=setTimeout(e,t);o(()=>clearTimeout(n))}}n??=AbortSignal.timeout(5e3);const r=new Set,i=t=>r.add(t);return new Promise((t,r)=>{const c=async()=>{try{n.throwIfAborted();const r=await e();if(r)return void t(r);o(c,i)}catch(t){r(t)}};c()}).finally(()=>{r.forEach(t=>t())})}export{t as waitUntil};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foxts",
3
- "version": "4.1.0",
3
+ "version": "4.2.0",
4
4
  "description": "Opinionated collection of common TypeScript utils by @SukkaW",
5
5
  "repository": {
6
6
  "url": "https://github.com/SukkaW/foxts"
@@ -10,6 +10,12 @@
10
10
  ],
11
11
  "exports": {
12
12
  "./package.json": "./package.json",
13
+ "./abort-error": {
14
+ "types": "./dist/abort-error/index.d.ts",
15
+ "import": "./dist/abort-error/index.mjs",
16
+ "require": "./dist/abort-error/index.js",
17
+ "default": "./dist/abort-error/index.js"
18
+ },
13
19
  "./add-array-elements-to-set": {
14
20
  "types": "./dist/add-array-elements-to-set/index.d.ts",
15
21
  "import": "./dist/add-array-elements-to-set/index.mjs",
@@ -279,6 +285,12 @@
279
285
  "import": "./dist/wait/index.mjs",
280
286
  "require": "./dist/wait/index.js",
281
287
  "default": "./dist/wait/index.js"
288
+ },
289
+ "./wait-until": {
290
+ "types": "./dist/wait-until/index.d.ts",
291
+ "import": "./dist/wait-until/index.mjs",
292
+ "require": "./dist/wait-until/index.js",
293
+ "default": "./dist/wait-until/index.js"
282
294
  }
283
295
  },
284
296
  "sideEffects": false,
@@ -295,11 +307,11 @@
295
307
  },
296
308
  "devDependencies": {
297
309
  "@eslint-sukka/node": "^7.2.1",
310
+ "@happy-dom/global-registrator": "^19.0.2",
298
311
  "@istanbuljs/nyc-config-typescript": "^1.0.2",
299
312
  "@mitata/counters": "^0.0.8",
300
313
  "@monyone/aho-corasick": "^1.0.4",
301
314
  "@package-json/types": "^0.0.12",
302
- "@rollup/plugin-node-resolve": "^16.0.1",
303
315
  "@swc-node/register": "^1.11.1",
304
316
  "@swc/core": "^1.13.20",
305
317
  "@types/mocha": "^10.0.10",
@@ -312,6 +324,7 @@
312
324
  "eslint-formatter-sukka": "^7.2.1",
313
325
  "expect": "^30.2.0",
314
326
  "fastscan": "^1.0.6",
327
+ "happy-dom": "^19.0.2",
315
328
  "is-ci": "^4.1.0",
316
329
  "is-network-error": "^1.3.0",
317
330
  "mitata": "^1.0.34",
@@ -320,13 +333,14 @@
320
333
  "nyc": "^17.1.0",
321
334
  "rollup": "^4.52.4",
322
335
  "rollup-plugin-dts": "^6.2.3",
336
+ "rollup-plugin-oxc-resolve": "^0.0.4",
323
337
  "rollup-plugin-swc3": "^0.12.1",
324
338
  "sinon": "^21.0.0",
325
339
  "word-list": "3.0.0"
326
340
  },
327
341
  "scripts": {
328
342
  "lint": "eslint --format=sukka .",
329
- "test": "SWC_NODE_IGNORE_DYNAMIC=1 SWC_NODE_INLINE_SOURCE_MAP=1 nyc mocha --require @swc-node/register --full-trace ./src/**/*.test.ts",
343
+ "test": "SWC_NODE_IGNORE_DYNAMIC=1 SWC_NODE_INLINE_SOURCE_MAP=1 nyc mocha --require @swc-node/register --parallel --full-trace ./src/**/*.test.ts",
330
344
  "bench": "SWC_NODE_IGNORE_DYNAMIC=1 node --require @swc-node/register",
331
345
  "bench:all": "SWC_NODE_IGNORE_DYNAMIC=1 node --require @swc-node/register ./src/**/*.bench.ts",
332
346
  "build": "rollup -c rollup.config.ts --bundleConfigAsCjs --configPlugin swc3",