superlib 0.1.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.
Files changed (137) hide show
  1. package/README.md +27 -0
  2. package/dist/basic/BaseError.d.ts +4 -0
  3. package/dist/basic/BaseError.d.ts.map +1 -0
  4. package/dist/basic/BaseError.js +6 -0
  5. package/dist/basic/Result.d.ts +29 -0
  6. package/dist/basic/Result.d.ts.map +1 -0
  7. package/dist/basic/Result.js +55 -0
  8. package/dist/basic/ResultAsync.d.ts +9 -0
  9. package/dist/basic/ResultAsync.d.ts.map +1 -0
  10. package/dist/basic/ResultAsync.js +41 -0
  11. package/dist/basic/assert.d.ts +8 -0
  12. package/dist/basic/assert.d.ts.map +1 -0
  13. package/dist/basic/assert.js +25 -0
  14. package/dist/basic/index.d.ts +5 -0
  15. package/dist/basic/index.d.ts.map +1 -0
  16. package/dist/basic/index.js +4 -0
  17. package/dist/decorators/Retry.d.ts +3 -0
  18. package/dist/decorators/Retry.d.ts.map +1 -0
  19. package/dist/decorators/Retry.js +9 -0
  20. package/dist/decorators/Timeout.d.ts +3 -0
  21. package/dist/decorators/Timeout.d.ts.map +1 -0
  22. package/dist/decorators/Timeout.js +9 -0
  23. package/dist/decorators/common.d.ts +2 -0
  24. package/dist/decorators/common.d.ts.map +1 -0
  25. package/dist/decorators/common.js +17 -0
  26. package/dist/decorators/index.d.ts +3 -0
  27. package/dist/decorators/index.d.ts.map +1 -0
  28. package/dist/decorators/index.js +2 -0
  29. package/dist/index.d.ts +8 -0
  30. package/dist/index.d.ts.map +1 -0
  31. package/dist/index.js +7 -0
  32. package/dist/platform/JsonHttpClient/JsonHttpClient.d.ts +18 -0
  33. package/dist/platform/JsonHttpClient/JsonHttpClient.d.ts.map +1 -0
  34. package/dist/platform/JsonHttpClient/JsonHttpClient.js +48 -0
  35. package/dist/platform/JsonHttpClient/index.d.ts +2 -0
  36. package/dist/platform/JsonHttpClient/index.d.ts.map +1 -0
  37. package/dist/platform/JsonHttpClient/index.js +1 -0
  38. package/dist/platform/ProcessContext/EnvReader.d.ts +12 -0
  39. package/dist/platform/ProcessContext/EnvReader.d.ts.map +1 -0
  40. package/dist/platform/ProcessContext/EnvReader.js +56 -0
  41. package/dist/platform/ProcessContext/ProcessContext.d.ts +40 -0
  42. package/dist/platform/ProcessContext/ProcessContext.d.ts.map +1 -0
  43. package/dist/platform/ProcessContext/ProcessContext.js +57 -0
  44. package/dist/platform/ProcessContext/index.d.ts +3 -0
  45. package/dist/platform/ProcessContext/index.d.ts.map +1 -0
  46. package/dist/platform/ProcessContext/index.js +2 -0
  47. package/dist/platform/filesystem/AbsolutePath.d.ts +19 -0
  48. package/dist/platform/filesystem/AbsolutePath.d.ts.map +1 -0
  49. package/dist/platform/filesystem/AbsolutePath.js +31 -0
  50. package/dist/platform/filesystem/FileSystem.d.ts +21 -0
  51. package/dist/platform/filesystem/FileSystem.d.ts.map +1 -0
  52. package/dist/platform/filesystem/FileSystem.js +128 -0
  53. package/dist/platform/filesystem/IFileSystem.d.ts +55 -0
  54. package/dist/platform/filesystem/IFileSystem.d.ts.map +1 -0
  55. package/dist/platform/filesystem/IFileSystem.js +1 -0
  56. package/dist/platform/filesystem/MemoryFileSystem.d.ts +41 -0
  57. package/dist/platform/filesystem/MemoryFileSystem.d.ts.map +1 -0
  58. package/dist/platform/filesystem/MemoryFileSystem.js +185 -0
  59. package/dist/platform/filesystem/glob/glob.d.ts +14 -0
  60. package/dist/platform/filesystem/glob/glob.d.ts.map +1 -0
  61. package/dist/platform/filesystem/glob/glob.js +57 -0
  62. package/dist/platform/filesystem/glob/parseGlob.d.ts +11 -0
  63. package/dist/platform/filesystem/glob/parseGlob.d.ts.map +1 -0
  64. package/dist/platform/filesystem/glob/parseGlob.js +61 -0
  65. package/dist/platform/filesystem/index.d.ts +5 -0
  66. package/dist/platform/filesystem/index.d.ts.map +1 -0
  67. package/dist/platform/filesystem/index.js +4 -0
  68. package/dist/platform/getPort/checkPort.d.ts +2 -0
  69. package/dist/platform/getPort/checkPort.d.ts.map +1 -0
  70. package/dist/platform/getPort/checkPort.js +49 -0
  71. package/dist/platform/getPort/getPort.d.ts +8 -0
  72. package/dist/platform/getPort/getPort.d.ts.map +1 -0
  73. package/dist/platform/getPort/getPort.js +13 -0
  74. package/dist/platform/getPort/index.d.ts +2 -0
  75. package/dist/platform/getPort/index.d.ts.map +1 -0
  76. package/dist/platform/getPort/index.js +1 -0
  77. package/dist/platform/safeFetch/index.d.ts +2 -0
  78. package/dist/platform/safeFetch/index.d.ts.map +1 -0
  79. package/dist/platform/safeFetch/index.js +1 -0
  80. package/dist/platform/safeFetch/makeSafeFetch.d.ts +23 -0
  81. package/dist/platform/safeFetch/makeSafeFetch.d.ts.map +1 -0
  82. package/dist/platform/safeFetch/makeSafeFetch.js +19 -0
  83. package/dist/random/FixedRandom.d.ts +12 -0
  84. package/dist/random/FixedRandom.d.ts.map +1 -0
  85. package/dist/random/FixedRandom.js +43 -0
  86. package/dist/random/Random.d.ts +18 -0
  87. package/dist/random/Random.d.ts.map +1 -0
  88. package/dist/random/Random.js +24 -0
  89. package/dist/random/RealRandom.d.ts +5 -0
  90. package/dist/random/RealRandom.d.ts.map +1 -0
  91. package/dist/random/RealRandom.js +6 -0
  92. package/dist/random/SeededRandom.d.ts +10 -0
  93. package/dist/random/SeededRandom.d.ts.map +1 -0
  94. package/dist/random/SeededRandom.js +15 -0
  95. package/dist/random/index.d.ts +5 -0
  96. package/dist/random/index.d.ts.map +1 -0
  97. package/dist/random/index.js +4 -0
  98. package/dist/schema/StandardSchema.d.ts +60 -0
  99. package/dist/schema/StandardSchema.d.ts.map +1 -0
  100. package/dist/schema/StandardSchema.js +1 -0
  101. package/dist/schema/validateSchema.d.ts +9 -0
  102. package/dist/schema/validateSchema.d.ts.map +1 -0
  103. package/dist/schema/validateSchema.js +9 -0
  104. package/dist/task/all.d.ts +14 -0
  105. package/dist/task/all.d.ts.map +1 -0
  106. package/dist/task/all.js +61 -0
  107. package/dist/task/index.d.ts +15 -0
  108. package/dist/task/index.d.ts.map +1 -0
  109. package/dist/task/index.js +11 -0
  110. package/dist/task/pipe.d.ts +8 -0
  111. package/dist/task/pipe.d.ts.map +1 -0
  112. package/dist/task/pipe.js +11 -0
  113. package/dist/task/retry.d.ts +27 -0
  114. package/dist/task/retry.d.ts.map +1 -0
  115. package/dist/task/retry.js +61 -0
  116. package/dist/task/timeout.d.ts +21 -0
  117. package/dist/task/timeout.d.ts.map +1 -0
  118. package/dist/task/timeout.js +38 -0
  119. package/dist/task/types.d.ts +3 -0
  120. package/dist/task/types.d.ts.map +1 -0
  121. package/dist/task/types.js +1 -0
  122. package/dist/time/Clock.d.ts +18 -0
  123. package/dist/time/Clock.d.ts.map +1 -0
  124. package/dist/time/Clock.js +26 -0
  125. package/dist/time/index.d.ts +4 -0
  126. package/dist/time/index.d.ts.map +1 -0
  127. package/dist/time/index.js +3 -0
  128. package/dist/time/sleep.d.ts +3 -0
  129. package/dist/time/sleep.d.ts.map +1 -0
  130. package/dist/time/sleep.js +5 -0
  131. package/dist/time/temporal.d.ts +5 -0
  132. package/dist/time/temporal.d.ts.map +1 -0
  133. package/dist/time/temporal.js +41 -0
  134. package/dist/types/index.d.ts +4 -0
  135. package/dist/types/index.d.ts.map +1 -0
  136. package/dist/types/index.js +1 -0
  137. package/package.json +70 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/platform/filesystem/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,eAAe,CAAA;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA"}
@@ -0,0 +1,4 @@
1
+ export {} from "./IFileSystem.js";
2
+ export { FileSystem } from "./FileSystem.js";
3
+ export { MemoryFileSystem } from "./MemoryFileSystem.js";
4
+ export { AbsolutePath } from "./AbsolutePath.js";
@@ -0,0 +1,2 @@
1
+ export declare function checkPort(port: number): Promise<boolean>;
2
+ //# sourceMappingURL=checkPort.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkPort.d.ts","sourceRoot":"","sources":["../../../src/platform/getPort/checkPort.ts"],"names":[],"mappings":"AAGA,wBAAsB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAW9D"}
@@ -0,0 +1,49 @@
1
+ import { createServer } from "node:net";
2
+ import { networkInterfaces } from "node:os";
3
+ export async function checkPort(port) {
4
+ const hosts = getLocalHosts();
5
+ for (const host of hosts) {
6
+ const available = await checkPortOnHost(port, host);
7
+ if (!available) {
8
+ return false;
9
+ }
10
+ }
11
+ return true;
12
+ }
13
+ function getLocalHosts() {
14
+ const interfaces = networkInterfaces();
15
+ const hosts = new Set([undefined, "0.0.0.0"]);
16
+ for (const networkInterface of Object.values(interfaces)) {
17
+ if (networkInterface) {
18
+ for (const config of networkInterface) {
19
+ hosts.add(config.address);
20
+ }
21
+ }
22
+ }
23
+ return hosts;
24
+ }
25
+ function checkPortOnHost(port, host) {
26
+ return new Promise((resolve) => {
27
+ try {
28
+ const server = createServer();
29
+ server.unref();
30
+ server.on("error", (error) => {
31
+ // EADDRNOTAVAIL/EINVAL: host doesn't support binding, skip it
32
+ if (error.code === "EADDRNOTAVAIL" || error.code === "EINVAL") {
33
+ resolve(true);
34
+ }
35
+ else {
36
+ resolve(false);
37
+ }
38
+ });
39
+ server.listen(port, host, () => {
40
+ server.close(() => {
41
+ resolve(true);
42
+ });
43
+ });
44
+ }
45
+ catch {
46
+ resolve(false);
47
+ }
48
+ });
49
+ }
@@ -0,0 +1,8 @@
1
+ import { type IRandom } from "../../random";
2
+ interface GetPortDependencies {
3
+ random: IRandom;
4
+ checkPort: (port: number) => Promise<boolean>;
5
+ }
6
+ export declare function getPort(dependencies?: GetPortDependencies): Promise<number>;
7
+ export {};
8
+ //# sourceMappingURL=getPort.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getPort.d.ts","sourceRoot":"","sources":["../../../src/platform/getPort/getPort.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,KAAK,OAAO,EAAE,MAAM,cAAc,CAAA;AAGvD,UAAU,mBAAmB;IAC3B,MAAM,EAAE,OAAO,CAAA;IACf,SAAS,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;CAE9C;AAED,wBAAsB,OAAO,CAC3B,YAAY,GAAE,mBAA6D,GAC1E,OAAO,CAAC,MAAM,CAAC,CAUjB"}
@@ -0,0 +1,13 @@
1
+ import { RealRandom } from "../../random/index.js";
2
+ import { checkPort } from "./checkPort.js";
3
+ export async function getPort(dependencies = { random: new RealRandom(), checkPort }) {
4
+ while (true) {
5
+ const port = dependencies.random.nextInteger(LOW_PORT, HIGH_PORT);
6
+ const isAvailable = await dependencies.checkPort(port);
7
+ if (isAvailable) {
8
+ return port;
9
+ }
10
+ }
11
+ }
12
+ const LOW_PORT = 49152;
13
+ const HIGH_PORT = 65535;
@@ -0,0 +1,2 @@
1
+ export { getPort } from "./getPort";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/platform/getPort/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA"}
@@ -0,0 +1 @@
1
+ export { getPort } from "./getPort.js";
@@ -0,0 +1,2 @@
1
+ export * from "./makeSafeFetch";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/platform/safeFetch/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA"}
@@ -0,0 +1 @@
1
+ export * from "./makeSafeFetch.js";
@@ -0,0 +1,23 @@
1
+ import type { TimeoutErr } from "../../task/timeout";
2
+ import type { DurationLike } from "../../time";
3
+ import { type Result } from "../../basic";
4
+ import { type RetryOptions } from "../../task/retry";
5
+ export interface SafeFetchOptions {
6
+ retry?: RetryOptions<SafeFetchResult> & {
7
+ untilStatus?: UntilStatusFn;
8
+ };
9
+ timeout?: DurationLike;
10
+ }
11
+ type UntilStatusFn = (status: number) => boolean;
12
+ export type SafeFetchError = {
13
+ type: "fetch/network";
14
+ cause: unknown;
15
+ } | {
16
+ type: "fetch/http";
17
+ status: number;
18
+ } | TimeoutErr;
19
+ export type SafeFetchResult = Result<Response, SafeFetchError>;
20
+ export type SafeFetch = (...args: Parameters<typeof fetch>) => Promise<SafeFetchResult>;
21
+ export declare function makeSafeFetch(options: SafeFetchOptions): SafeFetch;
22
+ export {};
23
+ //# sourceMappingURL=makeSafeFetch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"makeSafeFetch.d.ts","sourceRoot":"","sources":["../../../src/platform/safeFetch/makeSafeFetch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAE9C,OAAO,EAAwB,KAAK,MAAM,EAAE,MAAM,aAAa,CAAA;AAE/D,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,kBAAkB,CAAA;AAEpD,MAAM,WAAW,gBAAgB;IAC/B,KAAK,CAAC,EAAE,YAAY,CAAC,eAAe,CAAC,GAAG;QAAE,WAAW,CAAC,EAAE,aAAa,CAAA;KAAE,CAAA;IACvE,OAAO,CAAC,EAAE,YAAY,CAAA;CACvB;AAED,KAAK,aAAa,GAAG,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAA;AAEhD,MAAM,MAAM,cAAc,GACtB;IAAE,IAAI,EAAE,eAAe,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,GACzC;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACtC,UAAU,CAAA;AAEd,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAA;AAE9D,MAAM,MAAM,SAAS,GAAG,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,OAAO,KAAK,CAAC,KAAK,OAAO,CAAC,eAAe,CAAC,CAAA;AAEvF,wBAAgB,aAAa,CAAC,OAAO,EAAE,gBAAgB,GAAG,SAAS,CA0BlE"}
@@ -0,0 +1,19 @@
1
+ import { Err, Ok, ResultAsync } from "../../basic/index.js";
2
+ import { Task } from "../../task/index.js";
3
+ import {} from "../../task/retry.js";
4
+ export function makeSafeFetch(options) {
5
+ const untilStatus = options.retry?.untilStatus ?? ((status) => status >= 200 && status < 300);
6
+ return async (url, requestInit = {}) => {
7
+ const fetchTask = async () => {
8
+ return ResultAsync.try(() => fetch(url, requestInit), (error) => ({ type: "fetch/network", cause: error }))
9
+ .andThen((response) => {
10
+ if (!untilStatus(response.status)) {
11
+ return Err({ type: "fetch/http", status: response.status });
12
+ }
13
+ return Ok(response);
14
+ })
15
+ .toPromise();
16
+ };
17
+ return Task.pipe(fetchTask, options.timeout && Task.timeout({ timeout: options.timeout, useResult: true }), options.retry && Task.retry(options.retry));
18
+ };
19
+ }
@@ -0,0 +1,12 @@
1
+ import type { IRandom } from "./Random";
2
+ export declare class FixedRandom implements IRandom {
3
+ private readonly sequence;
4
+ private index;
5
+ constructor(sequence: any[]);
6
+ private takeValue;
7
+ next(): number;
8
+ nextInteger(min: number, max: number): number;
9
+ nextNumber(min: number, max: number): number;
10
+ nextBoolean(): boolean;
11
+ }
12
+ //# sourceMappingURL=FixedRandom.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FixedRandom.d.ts","sourceRoot":"","sources":["../../src/random/FixedRandom.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AAIvC,qBAAa,WAAY,YAAW,OAAO;IAE7B,OAAO,CAAC,QAAQ,CAAC,QAAQ;IADrC,OAAO,CAAC,KAAK,CAAI;gBACY,QAAQ,EAAE,GAAG,EAAE;IAE5C,OAAO,CAAC,SAAS;IAOjB,IAAI,IAAI,MAAM;IAUd,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM;IAoB7C,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM;IAc5C,WAAW,IAAI,OAAO;CAKvB"}
@@ -0,0 +1,43 @@
1
+ import { assert } from "../basic/index.js";
2
+ export class FixedRandom {
3
+ sequence;
4
+ index = 0;
5
+ constructor(sequence) {
6
+ this.sequence = sequence;
7
+ }
8
+ takeValue(methodName) {
9
+ assert(this.index < this.sequence.length, `FixedRandom.${methodName} sequence is exhausted`);
10
+ const value = this.sequence[this.index];
11
+ this.index += 1;
12
+ return value;
13
+ }
14
+ next() {
15
+ const value = this.takeValue("next");
16
+ assert(Number.isFinite(value), `FixedRandom.next expected number, got: ${value}`);
17
+ assert(value >= 0 && value < 1, `FixedRandom.next expected number in range <0;1), got: ${value}`);
18
+ return value;
19
+ }
20
+ nextInteger(min, max) {
21
+ assert(Number.isInteger(min) && !Number.isNaN(min), `min is expected to be an integer, was: ${min}`);
22
+ assert(Number.isInteger(max) && !Number.isNaN(max), `max is expected to be an integer, was: ${max}`);
23
+ assert(min < max, `expected min to be less than max, min=${min} max=${max}`);
24
+ const value = this.takeValue("nextInteger");
25
+ assert(Number.isInteger(value), `FixedRandom.nextInteger expected integer, got: ${value}`);
26
+ assert(value >= min && value <= max, `FixedRandom.nextInteger expected integer in range <${min};${max}>, got: ${value}`);
27
+ return value;
28
+ }
29
+ nextNumber(min, max) {
30
+ assert(Number.isFinite(min), `min is expected to be a number, was: ${min}`);
31
+ assert(Number.isFinite(max), `max is expected to be a number, was: ${max}`);
32
+ assert(min < max, `expected min to be less than max, min=${min} max=${max}`);
33
+ const value = this.takeValue("nextNumber");
34
+ assert(Number.isFinite(value), `FixedRandom.nextNumber expected number, got: ${value}`);
35
+ assert(value >= min && value < max, `FixedRandom.nextNumber expected number in range <${min};${max}), got: ${value}`);
36
+ return value;
37
+ }
38
+ nextBoolean() {
39
+ const value = this.takeValue("nextBoolean");
40
+ assert(typeof value === "boolean", `FixedRandom.nextBoolean expected boolean, got: ${value}`);
41
+ return value;
42
+ }
43
+ }
@@ -0,0 +1,18 @@
1
+ import type { PublicInterface } from "../types";
2
+ export type IRandom = PublicInterface<AbstractRandom>;
3
+ export declare abstract class AbstractRandom {
4
+ /**
5
+ * Returns next random number in range of <0;1)
6
+ */
7
+ abstract next(): number;
8
+ /**
9
+ * Returns next random integer in range of <min;max>
10
+ */
11
+ nextInteger(min: number, max: number): number;
12
+ /**
13
+ * Returns next random number in range of <min, max)
14
+ */
15
+ nextNumber(min: number, max: number): number;
16
+ nextBoolean(): boolean;
17
+ }
18
+ //# sourceMappingURL=Random.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Random.d.ts","sourceRoot":"","sources":["../../src/random/Random.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAA;AAI/C,MAAM,MAAM,OAAO,GAAG,eAAe,CAAC,cAAc,CAAC,CAAA;AAErD,8BAAsB,cAAc;IAClC;;OAEG;IACH,QAAQ,CAAC,IAAI,IAAI,MAAM;IAEvB;;OAEG;IACH,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM;IAc7C;;OAEG;IACH,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM;IAQ5C,WAAW,IAAI,OAAO;CAGvB"}
@@ -0,0 +1,24 @@
1
+ import { assert } from "../basic/assert.js";
2
+ export class AbstractRandom {
3
+ /**
4
+ * Returns next random integer in range of <min;max>
5
+ */
6
+ nextInteger(min, max) {
7
+ assert(Number.isInteger(min) && !Number.isNaN(min), `min is expected to be an integer, was: ${min}`);
8
+ assert(Number.isInteger(max) && !Number.isNaN(max), `max is expected to be an integer, was: ${max}`);
9
+ assert(min < max, `expected min to be less than max, min=${min} max=${max}`);
10
+ return Math.floor(this.next() * (max - min + 1)) + min;
11
+ }
12
+ /**
13
+ * Returns next random number in range of <min, max)
14
+ */
15
+ nextNumber(min, max) {
16
+ assert(Number.isFinite(min), `min is expected to be a number, was: ${min}`);
17
+ assert(Number.isFinite(max), `max is expected to be a number, was: ${max}`);
18
+ assert(min < max, `expected min to be less than max, min=${min} max=${max}`);
19
+ return this.next() * (max - min) + min;
20
+ }
21
+ nextBoolean() {
22
+ return this.nextInteger(0, 1) === 1;
23
+ }
24
+ }
@@ -0,0 +1,5 @@
1
+ import { AbstractRandom } from "./Random";
2
+ export declare class RealRandom extends AbstractRandom {
3
+ next(): number;
4
+ }
5
+ //# sourceMappingURL=RealRandom.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RealRandom.d.ts","sourceRoot":"","sources":["../../src/random/RealRandom.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAA;AAEzC,qBAAa,UAAW,SAAQ,cAAc;IAC5C,IAAI,IAAI,MAAM;CAGf"}
@@ -0,0 +1,6 @@
1
+ import { AbstractRandom } from "./Random.js";
2
+ export class RealRandom extends AbstractRandom {
3
+ next() {
4
+ return Math.random();
5
+ }
6
+ }
@@ -0,0 +1,10 @@
1
+ import { AbstractRandom } from "./Random";
2
+ export declare class SeededRandom extends AbstractRandom {
3
+ private m;
4
+ private a;
5
+ private c;
6
+ private state;
7
+ constructor(seed?: number);
8
+ next(): number;
9
+ }
10
+ //# sourceMappingURL=SeededRandom.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SeededRandom.d.ts","sourceRoot":"","sources":["../../src/random/SeededRandom.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAA;AAEzC,qBAAa,YAAa,SAAQ,cAAc;IAC9C,OAAO,CAAC,CAAC,CAAU;IACnB,OAAO,CAAC,CAAC,CAAa;IACtB,OAAO,CAAC,CAAC,CAAQ;IACjB,OAAO,CAAC,KAAK,CAAQ;gBAET,IAAI,GAAE,MAAgB;IAK3B,IAAI,IAAI,MAAM;CAItB"}
@@ -0,0 +1,15 @@
1
+ import { AbstractRandom } from "./Random.js";
2
+ export class SeededRandom extends AbstractRandom {
3
+ m = 2 ** 31;
4
+ a = 1103515245;
5
+ c = 12345;
6
+ state;
7
+ constructor(seed = 4202137) {
8
+ super();
9
+ this.state = seed >>> 0; // force to uint32
10
+ }
11
+ next() {
12
+ this.state = (this.a * this.state + this.c) % this.m;
13
+ return this.state / this.m;
14
+ }
15
+ }
@@ -0,0 +1,5 @@
1
+ export * from "./Random";
2
+ export { RealRandom } from "./RealRandom";
3
+ export { SeededRandom } from "./SeededRandom";
4
+ export { FixedRandom } from "./FixedRandom";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/random/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA;AACxB,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA"}
@@ -0,0 +1,4 @@
1
+ export * from "./Random.js";
2
+ export { RealRandom } from "./RealRandom.js";
3
+ export { SeededRandom } from "./SeededRandom.js";
4
+ export { FixedRandom } from "./FixedRandom.js";
@@ -0,0 +1,60 @@
1
+ /** The Standard Schema interface. */
2
+ export interface StandardSchemaV1<Input = unknown, Output = Input> {
3
+ /** The Standard Schema properties. */
4
+ readonly "~standard": StandardSchemaV1.Props<Input, Output>;
5
+ }
6
+ export declare namespace StandardSchemaV1 {
7
+ /** The Standard Schema properties interface. */
8
+ interface Props<Input = unknown, Output = Input> {
9
+ /** The version number of the standard. */
10
+ readonly version: 1;
11
+ /** The vendor name of the schema library. */
12
+ readonly vendor: string;
13
+ /** Validates unknown input values. */
14
+ readonly validate: (value: unknown, options?: StandardSchemaV1.Options) => Result<Output> | Promise<Result<Output>>;
15
+ /** Inferred types associated with the schema. */
16
+ readonly types?: Types<Input, Output> | undefined;
17
+ }
18
+ /** The result interface of the validate function. */
19
+ type Result<Output> = SuccessResult<Output> | FailureResult;
20
+ /** The result interface if validation succeeds. */
21
+ interface SuccessResult<Output> {
22
+ /** The typed output value. */
23
+ readonly value: Output;
24
+ /** A falsy value for `issues` indicates success. */
25
+ readonly issues?: undefined;
26
+ }
27
+ interface Options {
28
+ /** Explicit support for additional vendor-specific parameters, if needed. */
29
+ readonly libraryOptions?: Record<string, unknown> | undefined;
30
+ }
31
+ /** The result interface if validation fails. */
32
+ interface FailureResult {
33
+ /** The issues of failed validation. */
34
+ readonly issues: ReadonlyArray<Issue>;
35
+ }
36
+ /** The issue interface of the failure output. */
37
+ interface Issue {
38
+ /** The error message of the issue. */
39
+ readonly message: string;
40
+ /** The path of the issue, if any. */
41
+ readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
42
+ }
43
+ /** The path segment interface of the issue. */
44
+ interface PathSegment {
45
+ /** The key representing a path segment. */
46
+ readonly key: PropertyKey;
47
+ }
48
+ /** The Standard Schema types interface. */
49
+ interface Types<Input = unknown, Output = Input> {
50
+ /** The input type of the schema. */
51
+ readonly input: Input;
52
+ /** The output type of the schema. */
53
+ readonly output: Output;
54
+ }
55
+ /** Infers the input type of a Standard Schema. */
56
+ type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["input"];
57
+ /** Infers the output type of a Standard Schema. */
58
+ type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"];
59
+ }
60
+ //# sourceMappingURL=StandardSchema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StandardSchema.d.ts","sourceRoot":"","sources":["../../src/schema/StandardSchema.ts"],"names":[],"mappings":"AAAA,qCAAqC;AACrC,MAAM,WAAW,gBAAgB,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,KAAK;IAC/D,sCAAsC;IACtC,QAAQ,CAAC,WAAW,EAAE,gBAAgB,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;CAC5D;AAED,MAAM,CAAC,OAAO,WAAW,gBAAgB,CAAC;IACxC,gDAAgD;IAChD,UAAiB,KAAK,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,KAAK;QACpD,0CAA0C;QAC1C,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAA;QACnB,6CAA6C;QAC7C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;QACvB,sCAAsC;QACtC,QAAQ,CAAC,QAAQ,EAAE,CACjB,KAAK,EAAE,OAAO,EACd,OAAO,CAAC,EAAE,gBAAgB,CAAC,OAAO,KAC/B,MAAM,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAA;QAC7C,iDAAiD;QACjD,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,SAAS,CAAA;KAClD;IAED,qDAAqD;IACrD,KAAY,MAAM,CAAC,MAAM,IAAI,aAAa,CAAC,MAAM,CAAC,GAAG,aAAa,CAAA;IAElE,mDAAmD;IACnD,UAAiB,aAAa,CAAC,MAAM;QACnC,8BAA8B;QAC9B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;QACtB,oDAAoD;QACpD,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,CAAA;KAC5B;IAED,UAAiB,OAAO;QACtB,6EAA6E;QAC7E,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAA;KAC9D;IAED,gDAAgD;IAChD,UAAiB,aAAa;QAC5B,uCAAuC;QACvC,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC,KAAK,CAAC,CAAA;KACtC;IAED,iDAAiD;IACjD,UAAiB,KAAK;QACpB,sCAAsC;QACtC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;QACxB,qCAAqC;QACrC,QAAQ,CAAC,IAAI,CAAC,EAAE,aAAa,CAAC,WAAW,GAAG,WAAW,CAAC,GAAG,SAAS,CAAA;KACrE;IAED,+CAA+C;IAC/C,UAAiB,WAAW;QAC1B,2CAA2C;QAC3C,QAAQ,CAAC,GAAG,EAAE,WAAW,CAAA;KAC1B;IAED,2CAA2C;IAC3C,UAAiB,KAAK,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,KAAK;QACpD,oCAAoC;QACpC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAA;QACrB,qCAAqC;QACrC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;KACxB;IAED,kDAAkD;IAClD,KAAY,UAAU,CAAC,MAAM,SAAS,gBAAgB,IAAI,WAAW,CACnE,MAAM,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC,CAC7B,CAAC,OAAO,CAAC,CAAA;IAEV,mDAAmD;IACnD,KAAY,WAAW,CAAC,MAAM,SAAS,gBAAgB,IAAI,WAAW,CACpE,MAAM,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC,CAC7B,CAAC,QAAQ,CAAC,CAAA;CACZ"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ import type { StandardSchemaV1 } from "./StandardSchema";
2
+ import { type Result } from "../basic";
3
+ export type ValidationError = {
4
+ type: "schema/validate";
5
+ issue: ReadonlyArray<StandardSchemaV1.Issue>;
6
+ };
7
+ export type ValidateSchemaResult<T> = Result<T, ValidationError>;
8
+ export declare function validateSchema<T>(schema: StandardSchemaV1<T>, value: unknown): ValidateSchemaResult<T>;
9
+ //# sourceMappingURL=validateSchema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validateSchema.d.ts","sourceRoot":"","sources":["../../src/schema/validateSchema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AAExD,OAAO,EAAmB,KAAK,MAAM,EAAE,MAAM,UAAU,CAAA;AAEvD,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,iBAAiB,CAAA;IACvB,KAAK,EAAE,aAAa,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAA;CAC7C,CAAA;AACD,MAAM,MAAM,oBAAoB,CAAC,CAAC,IAAI,MAAM,CAAC,CAAC,EAAE,eAAe,CAAC,CAAA;AAEhE,wBAAgB,cAAc,CAAC,CAAC,EAC9B,MAAM,EAAE,gBAAgB,CAAC,CAAC,CAAC,EAC3B,KAAK,EAAE,OAAO,GACb,oBAAoB,CAAC,CAAC,CAAC,CAUzB"}
@@ -0,0 +1,9 @@
1
+ import { assert, Err, Ok } from "../basic/index.js";
2
+ export function validateSchema(schema, value) {
3
+ const result = schema["~standard"].validate(value);
4
+ assert(!(result instanceof Promise), "Only sync validation supported at the moment");
5
+ if (result.issues) {
6
+ return Err({ type: "schema/validate", issue: result.issues });
7
+ }
8
+ return Ok(result.value);
9
+ }
@@ -0,0 +1,14 @@
1
+ import type { Task } from "./types";
2
+ export type AllOptions = {
3
+ concurrency: number | `batches-of-${number}` | "unbounded";
4
+ };
5
+ export declare function all<T extends unknown[]>(tasks: {
6
+ [K in keyof T]: Task<T[K]>;
7
+ }, options: AllOptions): Promise<{
8
+ [K in keyof T]: Awaited<T[K]>;
9
+ }>;
10
+ export declare function all<K extends PropertyKey, R extends Record<K, Task<unknown>>>(tasks: R, options: AllOptions): Promise<{
11
+ [P in keyof R]: Awaited<ReturnType<R[P]>>;
12
+ }>;
13
+ export declare function allWithArray<T>(tasks: ReadonlyArray<Task<T>>, options: AllOptions): Promise<T[]>;
14
+ //# sourceMappingURL=all.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"all.d.ts","sourceRoot":"","sources":["../../src/task/all.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAInC,MAAM,MAAM,UAAU,GAAG;IACvB,WAAW,EAAE,MAAM,GAAG,cAAc,MAAM,EAAE,GAAG,WAAW,CAAA;CAC3D,CAAA;AAED,wBAAsB,GAAG,CAAC,CAAC,SAAS,OAAO,EAAE,EAC3C,KAAK,EAAE;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAAE,EACrC,OAAO,EAAE,UAAU,GAClB,OAAO,CAAC;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAAE,CAAC,CAAA;AAE7C,wBAAsB,GAAG,CAAC,CAAC,SAAS,WAAW,EAAE,CAAC,SAAS,MAAM,CAAC,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,EACjF,KAAK,EAAE,CAAC,EACR,OAAO,EAAE,UAAU,GAClB,OAAO,CAAC;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAAE,CAAC,CAAA;AA4BzD,wBAAsB,YAAY,CAAC,CAAC,EAClC,KAAK,EAAE,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAC7B,OAAO,EAAE,UAAU,GAClB,OAAO,CAAC,CAAC,EAAE,CAAC,CAsDd"}
@@ -0,0 +1,61 @@
1
+ import { chunk, zip } from "remeda";
2
+ import { assert } from "../basic/index.js";
3
+ export async function all(taskArrayOrObject, options) {
4
+ if (Array.isArray(taskArrayOrObject)) {
5
+ return allWithArray(taskArrayOrObject, options);
6
+ }
7
+ const entries = Object.entries(taskArrayOrObject);
8
+ const keys = entries.map((e) => e[0]);
9
+ const values = entries.map((e) => e[1]);
10
+ const result = await allWithArray(values, options);
11
+ const resultObject = Object.fromEntries(zip(keys, result));
12
+ return resultObject;
13
+ }
14
+ function assertPositiveIntegerConcurrency(value) {
15
+ assert(value > 0 && Number.isFinite(value) && Number.isInteger(value), `options.concurrency must be a positive integer (got ${value})`);
16
+ }
17
+ export async function allWithArray(tasks, options) {
18
+ if (options.concurrency === "unbounded" || options.concurrency === Number.POSITIVE_INFINITY) {
19
+ return Promise.all(tasks.map((t) => t()));
20
+ }
21
+ if (typeof options.concurrency === "string" && options.concurrency.startsWith("batches-of-")) {
22
+ const batchSizeRaw = Number(options.concurrency.slice("batches-of-".length));
23
+ assertPositiveIntegerConcurrency(batchSizeRaw);
24
+ const batchSize = batchSizeRaw;
25
+ const chunks = chunk(tasks, batchSize);
26
+ let result = [];
27
+ for (const c of chunks) {
28
+ result = result.concat(await allWithArray(c, { concurrency: "unbounded" }));
29
+ }
30
+ return result;
31
+ }
32
+ const concurrency = options.concurrency;
33
+ assertPositiveIntegerConcurrency(concurrency);
34
+ if (tasks.length === 0) {
35
+ return [];
36
+ }
37
+ const results = Array.from({ length: tasks.length });
38
+ let nextIndex = 0;
39
+ let cancelled = false; // prevent extra scheduling after a failure
40
+ async function worker() {
41
+ while (true) {
42
+ if (cancelled) {
43
+ return;
44
+ }
45
+ const i = nextIndex++;
46
+ if (i >= tasks.length) {
47
+ return;
48
+ }
49
+ try {
50
+ results[i] = await tasks[i]();
51
+ }
52
+ catch (err) {
53
+ cancelled = true;
54
+ throw err;
55
+ }
56
+ }
57
+ }
58
+ const workerCount = Math.min(concurrency, tasks.length);
59
+ await Promise.all(Array.from({ length: workerCount }, worker));
60
+ return results;
61
+ }
@@ -0,0 +1,15 @@
1
+ import { all } from "./all";
2
+ import { pipe } from "./pipe";
3
+ import { retry } from "./retry";
4
+ import { timeout } from "./timeout";
5
+ import { type Task as TaskTypeReexport } from "./types";
6
+ export declare const Task: {
7
+ all: typeof all;
8
+ pipe: typeof pipe;
9
+ retry: typeof retry;
10
+ timeout: typeof timeout;
11
+ };
12
+ export declare namespace Task {
13
+ type Task<T> = TaskTypeReexport<T>;
14
+ }
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/task/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,OAAO,CAAA;AAC3B,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAA;AAC7B,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAC/B,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,EAAE,KAAK,IAAI,IAAI,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAEvD,eAAO,MAAM,IAAI;;;;;CAKhB,CAAA;AAED,yBAAiB,IAAI,CAAC;IACpB,KAAY,IAAI,CAAC,CAAC,IAAI,gBAAgB,CAAC,CAAC,CAAC,CAAA;CAC1C"}
@@ -0,0 +1,11 @@
1
+ import { all } from "./all.js";
2
+ import { pipe } from "./pipe.js";
3
+ import { retry } from "./retry.js";
4
+ import { timeout } from "./timeout.js";
5
+ import {} from "./types.js";
6
+ export const Task = {
7
+ all,
8
+ pipe,
9
+ retry,
10
+ timeout,
11
+ };
@@ -0,0 +1,8 @@
1
+ import type { Task, TaskMapper } from "./types";
2
+ export declare function pipe<A>(task: Task<A>): Promise<A>;
3
+ export declare function pipe<A, B>(task: Task<A>, m1: TaskMapper<A, B> | undefined): Promise<B>;
4
+ export declare function pipe<A, B, C>(task: Task<A>, m1: TaskMapper<A, B> | undefined, m2: TaskMapper<B, C> | undefined): Promise<C>;
5
+ export declare function pipe<A, B, C, D>(task: Task<A>, m1: TaskMapper<A, B> | undefined, m2: TaskMapper<B, C> | undefined, m3: TaskMapper<C, D> | undefined): Promise<D>;
6
+ export declare function pipe<A, B, C, D, E>(task: Task<A>, m1: TaskMapper<A, B> | undefined, m2: TaskMapper<B, C> | undefined, m3: TaskMapper<C, D> | undefined, m4: TaskMapper<D, E> | undefined): Promise<E>;
7
+ export declare function pipe<A, B, C, D, E, F>(task: Task<A>, m1: TaskMapper<A, B> | undefined, m2: TaskMapper<B, C> | undefined, m3: TaskMapper<C, D> | undefined, m4: TaskMapper<D, E> | undefined, m5: TaskMapper<E, F> | undefined): Promise<F>;
8
+ //# sourceMappingURL=pipe.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pipe.d.ts","sourceRoot":"","sources":["../../src/task/pipe.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAE/C,wBAAgB,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAA;AAClD,wBAAgB,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,GAAG,OAAO,CAAC,CAAC,CAAC,CAAA;AACvF,wBAAgB,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAC1B,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,EACb,EAAE,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,EAChC,EAAE,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,GAC/B,OAAO,CAAC,CAAC,CAAC,CAAA;AACb,wBAAgB,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAC7B,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,EACb,EAAE,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,EAChC,EAAE,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,EAChC,EAAE,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,GAC/B,OAAO,CAAC,CAAC,CAAC,CAAA;AACb,wBAAgB,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAChC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,EACb,EAAE,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,EAChC,EAAE,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,EAChC,EAAE,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,EAChC,EAAE,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,GAC/B,OAAO,CAAC,CAAC,CAAC,CAAA;AACb,wBAAgB,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EACnC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,EACb,EAAE,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,EAChC,EAAE,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,EAChC,EAAE,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,EAChC,EAAE,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,EAChC,EAAE,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,GAC/B,OAAO,CAAC,CAAC,CAAC,CAAA"}
@@ -0,0 +1,11 @@
1
+ export function pipe(task, ...steps) {
2
+ let acc = task;
3
+ for (const map of steps) {
4
+ // @note: mappers can be undefined which makes applying mappers conditionally easy
5
+ if (!map) {
6
+ continue;
7
+ }
8
+ acc = map(acc);
9
+ }
10
+ return acc();
11
+ }
@@ -0,0 +1,27 @@
1
+ import type { Task, TaskMapper } from "./types";
2
+ import { ErrResult, Result } from "../basic";
3
+ import { type IRandom } from "../random";
4
+ import { type DurationLike } from "../time";
5
+ export type ErrResultOrError<T> = T extends Result<any, infer E> ? ErrResult<unknown, E> : unknown;
6
+ export interface RetryOptions<R> {
7
+ until?: (error: ErrResultOrError<R>) => boolean;
8
+ times: number;
9
+ delay: DurationLike | RetryDelayPolicy;
10
+ }
11
+ export type RetryDelayPolicy = (attempt: number) => DurationLike;
12
+ export interface RetryDependencies {
13
+ random: IRandom;
14
+ }
15
+ export declare function retry<T>(task: Task<T>, options: RetryOptions<T>, dependencies?: RetryDependencies): Promise<T>;
16
+ export declare function retry<T>(options: RetryOptions<T>, dependencies?: RetryDependencies): TaskMapper<T, T>;
17
+ export declare function ExponentialBackoffRetryPolicy(base: DurationLike): RetryDelayPolicy;
18
+ interface JitteredRetryOptions {
19
+ minFactor?: number;
20
+ maxFactor?: number;
21
+ }
22
+ interface JitteredRetryDependencies {
23
+ random: IRandom;
24
+ }
25
+ export declare function JitteredRetryPolicy(base: DurationLike | RetryDelayPolicy, options?: JitteredRetryOptions, dependencies?: JitteredRetryDependencies): RetryDelayPolicy;
26
+ export {};
27
+ //# sourceMappingURL=retry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../../src/task/retry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAE/C,OAAO,EAAU,SAAS,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AACpD,OAAO,EAAE,KAAK,OAAO,EAAc,MAAM,WAAW,CAAA;AACpD,OAAO,EAA2B,KAAK,YAAY,EAAE,MAAM,SAAS,CAAA;AAEpE,MAAM,MAAM,gBAAgB,CAAC,CAAC,IAAI,CAAC,SAAS,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC,GAAG,SAAS,CAAC,OAAO,EAAE,CAAC,CAAC,GAAG,OAAO,CAAA;AAElG,MAAM,WAAW,YAAY,CAAC,CAAC;IAC7B,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,gBAAgB,CAAC,CAAC,CAAC,KAAK,OAAO,CAAA;IAC/C,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,YAAY,GAAG,gBAAgB,CAAA;CACvC;AAED,MAAM,MAAM,gBAAgB,GAAG,CAAC,OAAO,EAAE,MAAM,KAAK,YAAY,CAAA;AAEhE,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,OAAO,CAAA;CAChB;AAED,wBAAgB,KAAK,CAAC,CAAC,EACrB,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,EACb,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,EACxB,YAAY,CAAC,EAAE,iBAAiB,GAC/B,OAAO,CAAC,CAAC,CAAC,CAAA;AACb,wBAAgB,KAAK,CAAC,CAAC,EACrB,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,EACxB,YAAY,CAAC,EAAE,iBAAiB,GAC/B,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;AAqDnB,wBAAgB,6BAA6B,CAAC,IAAI,EAAE,YAAY,GAAG,gBAAgB,CAKlF;AAED,UAAU,oBAAoB;IAC5B,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED,UAAU,yBAAyB;IACjC,MAAM,EAAE,OAAO,CAAA;CAChB;AAED,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,YAAY,GAAG,gBAAgB,EACrC,OAAO,CAAC,EAAE,oBAAoB,EAC9B,YAAY,CAAC,EAAE,yBAAyB,GACvC,gBAAgB,CAgBlB"}