@stacksjs/utils 0.58.57 → 0.58.59

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.js CHANGED
@@ -147,7 +147,13 @@ function isTruthy(v) {
147
147
  import {hashPaths} from "@stacksjs/storage";
148
148
  import {path as p2} from "@stacksjs/path";
149
149
  import {config as config2} from "@stacksjs/config";
150
- var originRequestFunctionHash = hashPaths(p2.cloudPath("src/edge"));
150
+ var originRequestFunctionHash;
151
+ try {
152
+ originRequestFunctionHash = hashPaths(p2.cloudPath("src/edge"));
153
+ } catch (error) {
154
+ console.error("Are we in a Docker container? Failed to hash paths:", error);
155
+ originRequestFunctionHash = null;
156
+ }
151
157
  var docsSrc = [
152
158
  p2.projectPath("docs"),
153
159
  p2.projectPath("config/docs.ts")
@@ -487,6 +493,36 @@ var createRegExp = (...inputs) => {
487
493
  const flags = inputs.length > 1 && (Array.isArray(inputs[inputs.length - 1]) || inputs[inputs.length - 1] instanceof Set) ? inputs.pop() : undefined;
488
494
  return new RegExp(exactly(...inputs).toString(), [...flags || ""].join(""));
489
495
  };
496
+ // src/retry.ts
497
+ function retry(fn, options) {
498
+ const { retries = 3, initialDelay = 1000, backoffFactor = 2, jitter = true } = options;
499
+ return new Promise((resolve, reject) => {
500
+ let attemptCount = 0;
501
+ const attempt = async () => {
502
+ try {
503
+ resolve(await fn());
504
+ } catch (err2) {
505
+ if (attemptCount >= retries) {
506
+ reject(err2);
507
+ } else {
508
+ const delay = calculateDelay(attemptCount, initialDelay, backoffFactor, jitter);
509
+ setTimeout(() => attempt(), delay);
510
+ attemptCount++;
511
+ }
512
+ }
513
+ };
514
+ attempt();
515
+ });
516
+ }
517
+ var calculateDelay = function(attemptCount, initialDelay, backoffFactor, jitter) {
518
+ let delay = initialDelay * backoffFactor ** attemptCount;
519
+ if (jitter) {
520
+ const random = Math.random();
521
+ const jitterValue = delay * 0.3;
522
+ delay = delay + jitterValue * (random - 0.5) * 2;
523
+ }
524
+ return delay;
525
+ };
490
526
  // src/sleep.ts
491
527
  function sleep(ms) {
492
528
  return new Promise((resolve) => setTimeout(resolve, ms));
@@ -884,7 +920,7 @@ function prettyBytes(number, options) {
884
920
  return prefix + numberString + separator + unit;
885
921
  }
886
922
  // package.json
887
- var version = "0.58.57";
923
+ var version = "0.58.59";
888
924
  export {
889
925
  wordChar,
890
926
  wordBoundary,
@@ -1102,6 +1138,7 @@ export {
1102
1138
  setEnvValue,
1103
1139
  runNpmScript,
1104
1140
  rimraf,
1141
+ retry,
1105
1142
  resolveUnref,
1106
1143
  resolveRef,
1107
1144
  renderHeadToString,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/utils",
3
3
  "type": "module",
4
- "version": "0.58.57",
4
+ "version": "0.58.59",
5
5
  "description": "The Stacks helper functions.",
6
6
  "author": "Chris Breuer",
7
7
  "license": "MIT",
@@ -49,7 +49,7 @@
49
49
  "build": "bun --bun build.ts",
50
50
  "typecheck": "bun --bun tsc --noEmit",
51
51
  "prepublishOnly": "bun --bun run build",
52
- "size": "tsx scripts/export-size.ts"
52
+ "size": "bun scripts/export-size.ts"
53
53
  },
54
54
  "peerDependencies": {
55
55
  "@stacksjs/actions": "latest",
package/src/hash.ts CHANGED
@@ -2,7 +2,16 @@ import { hashPaths } from '@stacksjs/storage'
2
2
  import { path as p } from '@stacksjs/path'
3
3
  import { config } from '@stacksjs/config'
4
4
 
5
- export const originRequestFunctionHash = hashPaths(p.cloudPath('src/edge'))
5
+ // eslint-disable-next-line import/no-mutable-exports
6
+ let originRequestFunctionHash
7
+ try {
8
+ originRequestFunctionHash = hashPaths(p.cloudPath('src/edge'))
9
+ }
10
+ catch (error) {
11
+ console.error('Are we in a Docker container? Failed to hash paths:', error)
12
+ originRequestFunctionHash = null // or some default/fallback value
13
+ }
14
+ export { originRequestFunctionHash }
6
15
 
7
16
  const docsSrc = [
8
17
  p.projectPath('docs'),
package/src/index.ts CHANGED
@@ -16,6 +16,7 @@ export * from './math'
16
16
  export * from './p'
17
17
  export * from './promise'
18
18
  export * from './regex'
19
+ export * from './retry'
19
20
  export * from './sleep'
20
21
  export * from './throttle'
21
22
  export * from './vendors'
package/src/retry.ts ADDED
@@ -0,0 +1,33 @@
1
+ export function retry(fn, options) {
2
+ const { retries = 3, initialDelay = 1000, backoffFactor = 2, jitter = true } = options
3
+
4
+ return new Promise((resolve, reject) => {
5
+ let attemptCount = 0
6
+ const attempt = async () => {
7
+ try {
8
+ resolve(await fn())
9
+ }
10
+ catch (err) {
11
+ if (attemptCount >= retries) {
12
+ reject(err)
13
+ }
14
+ else {
15
+ const delay = calculateDelay(attemptCount, initialDelay, backoffFactor, jitter)
16
+ setTimeout(() => attempt(), delay)
17
+ attemptCount++
18
+ }
19
+ }
20
+ }
21
+ attempt()
22
+ })
23
+ }
24
+
25
+ function calculateDelay(attemptCount, initialDelay, backoffFactor, jitter) {
26
+ let delay = initialDelay * backoffFactor ** attemptCount
27
+ if (jitter) {
28
+ const random = Math.random() // Generates a number between 0 and 1
29
+ const jitterValue = delay * 0.3 // Jitter will be up to 30% of the delay
30
+ delay = delay + jitterValue * (random - 0.5) * 2 // Adjust delay randomly within ±30%
31
+ }
32
+ return delay
33
+ }