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.
- package/README.md +27 -0
- package/dist/basic/BaseError.d.ts +4 -0
- package/dist/basic/BaseError.d.ts.map +1 -0
- package/dist/basic/BaseError.js +6 -0
- package/dist/basic/Result.d.ts +29 -0
- package/dist/basic/Result.d.ts.map +1 -0
- package/dist/basic/Result.js +55 -0
- package/dist/basic/ResultAsync.d.ts +9 -0
- package/dist/basic/ResultAsync.d.ts.map +1 -0
- package/dist/basic/ResultAsync.js +41 -0
- package/dist/basic/assert.d.ts +8 -0
- package/dist/basic/assert.d.ts.map +1 -0
- package/dist/basic/assert.js +25 -0
- package/dist/basic/index.d.ts +5 -0
- package/dist/basic/index.d.ts.map +1 -0
- package/dist/basic/index.js +4 -0
- package/dist/decorators/Retry.d.ts +3 -0
- package/dist/decorators/Retry.d.ts.map +1 -0
- package/dist/decorators/Retry.js +9 -0
- package/dist/decorators/Timeout.d.ts +3 -0
- package/dist/decorators/Timeout.d.ts.map +1 -0
- package/dist/decorators/Timeout.js +9 -0
- package/dist/decorators/common.d.ts +2 -0
- package/dist/decorators/common.d.ts.map +1 -0
- package/dist/decorators/common.js +17 -0
- package/dist/decorators/index.d.ts +3 -0
- package/dist/decorators/index.d.ts.map +1 -0
- package/dist/decorators/index.js +2 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/platform/JsonHttpClient/JsonHttpClient.d.ts +18 -0
- package/dist/platform/JsonHttpClient/JsonHttpClient.d.ts.map +1 -0
- package/dist/platform/JsonHttpClient/JsonHttpClient.js +48 -0
- package/dist/platform/JsonHttpClient/index.d.ts +2 -0
- package/dist/platform/JsonHttpClient/index.d.ts.map +1 -0
- package/dist/platform/JsonHttpClient/index.js +1 -0
- package/dist/platform/ProcessContext/EnvReader.d.ts +12 -0
- package/dist/platform/ProcessContext/EnvReader.d.ts.map +1 -0
- package/dist/platform/ProcessContext/EnvReader.js +56 -0
- package/dist/platform/ProcessContext/ProcessContext.d.ts +40 -0
- package/dist/platform/ProcessContext/ProcessContext.d.ts.map +1 -0
- package/dist/platform/ProcessContext/ProcessContext.js +57 -0
- package/dist/platform/ProcessContext/index.d.ts +3 -0
- package/dist/platform/ProcessContext/index.d.ts.map +1 -0
- package/dist/platform/ProcessContext/index.js +2 -0
- package/dist/platform/filesystem/AbsolutePath.d.ts +19 -0
- package/dist/platform/filesystem/AbsolutePath.d.ts.map +1 -0
- package/dist/platform/filesystem/AbsolutePath.js +31 -0
- package/dist/platform/filesystem/FileSystem.d.ts +21 -0
- package/dist/platform/filesystem/FileSystem.d.ts.map +1 -0
- package/dist/platform/filesystem/FileSystem.js +128 -0
- package/dist/platform/filesystem/IFileSystem.d.ts +55 -0
- package/dist/platform/filesystem/IFileSystem.d.ts.map +1 -0
- package/dist/platform/filesystem/IFileSystem.js +1 -0
- package/dist/platform/filesystem/MemoryFileSystem.d.ts +41 -0
- package/dist/platform/filesystem/MemoryFileSystem.d.ts.map +1 -0
- package/dist/platform/filesystem/MemoryFileSystem.js +185 -0
- package/dist/platform/filesystem/glob/glob.d.ts +14 -0
- package/dist/platform/filesystem/glob/glob.d.ts.map +1 -0
- package/dist/platform/filesystem/glob/glob.js +57 -0
- package/dist/platform/filesystem/glob/parseGlob.d.ts +11 -0
- package/dist/platform/filesystem/glob/parseGlob.d.ts.map +1 -0
- package/dist/platform/filesystem/glob/parseGlob.js +61 -0
- package/dist/platform/filesystem/index.d.ts +5 -0
- package/dist/platform/filesystem/index.d.ts.map +1 -0
- package/dist/platform/filesystem/index.js +4 -0
- package/dist/platform/getPort/checkPort.d.ts +2 -0
- package/dist/platform/getPort/checkPort.d.ts.map +1 -0
- package/dist/platform/getPort/checkPort.js +49 -0
- package/dist/platform/getPort/getPort.d.ts +8 -0
- package/dist/platform/getPort/getPort.d.ts.map +1 -0
- package/dist/platform/getPort/getPort.js +13 -0
- package/dist/platform/getPort/index.d.ts +2 -0
- package/dist/platform/getPort/index.d.ts.map +1 -0
- package/dist/platform/getPort/index.js +1 -0
- package/dist/platform/safeFetch/index.d.ts +2 -0
- package/dist/platform/safeFetch/index.d.ts.map +1 -0
- package/dist/platform/safeFetch/index.js +1 -0
- package/dist/platform/safeFetch/makeSafeFetch.d.ts +23 -0
- package/dist/platform/safeFetch/makeSafeFetch.d.ts.map +1 -0
- package/dist/platform/safeFetch/makeSafeFetch.js +19 -0
- package/dist/random/FixedRandom.d.ts +12 -0
- package/dist/random/FixedRandom.d.ts.map +1 -0
- package/dist/random/FixedRandom.js +43 -0
- package/dist/random/Random.d.ts +18 -0
- package/dist/random/Random.d.ts.map +1 -0
- package/dist/random/Random.js +24 -0
- package/dist/random/RealRandom.d.ts +5 -0
- package/dist/random/RealRandom.d.ts.map +1 -0
- package/dist/random/RealRandom.js +6 -0
- package/dist/random/SeededRandom.d.ts +10 -0
- package/dist/random/SeededRandom.d.ts.map +1 -0
- package/dist/random/SeededRandom.js +15 -0
- package/dist/random/index.d.ts +5 -0
- package/dist/random/index.d.ts.map +1 -0
- package/dist/random/index.js +4 -0
- package/dist/schema/StandardSchema.d.ts +60 -0
- package/dist/schema/StandardSchema.d.ts.map +1 -0
- package/dist/schema/StandardSchema.js +1 -0
- package/dist/schema/validateSchema.d.ts +9 -0
- package/dist/schema/validateSchema.d.ts.map +1 -0
- package/dist/schema/validateSchema.js +9 -0
- package/dist/task/all.d.ts +14 -0
- package/dist/task/all.d.ts.map +1 -0
- package/dist/task/all.js +61 -0
- package/dist/task/index.d.ts +15 -0
- package/dist/task/index.d.ts.map +1 -0
- package/dist/task/index.js +11 -0
- package/dist/task/pipe.d.ts +8 -0
- package/dist/task/pipe.d.ts.map +1 -0
- package/dist/task/pipe.js +11 -0
- package/dist/task/retry.d.ts +27 -0
- package/dist/task/retry.d.ts.map +1 -0
- package/dist/task/retry.js +61 -0
- package/dist/task/timeout.d.ts +21 -0
- package/dist/task/timeout.d.ts.map +1 -0
- package/dist/task/timeout.js +38 -0
- package/dist/task/types.d.ts +3 -0
- package/dist/task/types.d.ts.map +1 -0
- package/dist/task/types.js +1 -0
- package/dist/time/Clock.d.ts +18 -0
- package/dist/time/Clock.d.ts.map +1 -0
- package/dist/time/Clock.js +26 -0
- package/dist/time/index.d.ts +4 -0
- package/dist/time/index.d.ts.map +1 -0
- package/dist/time/index.js +3 -0
- package/dist/time/sleep.d.ts +3 -0
- package/dist/time/sleep.d.ts.map +1 -0
- package/dist/time/sleep.js +5 -0
- package/dist/time/temporal.d.ts +5 -0
- package/dist/time/temporal.d.ts.map +1 -0
- package/dist/time/temporal.js +41 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +1 -0
- package/package.json +70 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { assert, ErrResult, Result } from "../basic/index.js";
|
|
2
|
+
import { RealRandom } from "../random/index.js";
|
|
3
|
+
import { multiplyDuration, sleep } from "../time/index.js";
|
|
4
|
+
export function retry(taskOrOptions, optionsOrDependencies, dependencies) {
|
|
5
|
+
if (typeof taskOrOptions === "function") {
|
|
6
|
+
return runRetry(taskOrOptions, optionsOrDependencies, dependencies);
|
|
7
|
+
}
|
|
8
|
+
return (task) => () => runRetry(task, taskOrOptions, optionsOrDependencies);
|
|
9
|
+
}
|
|
10
|
+
async function runRetry(task, options, dependencies) {
|
|
11
|
+
const until = options.until ?? (() => true);
|
|
12
|
+
const delay = options.delay instanceof Function
|
|
13
|
+
? options.delay
|
|
14
|
+
: JitteredRetryPolicy(ExponentialBackoffRetryPolicy(options.delay), {}, dependencies);
|
|
15
|
+
let attempt = 0;
|
|
16
|
+
while (true) {
|
|
17
|
+
let thrownError = undefined;
|
|
18
|
+
let resultError = undefined;
|
|
19
|
+
try {
|
|
20
|
+
const result = await task();
|
|
21
|
+
if (!(result instanceof Result) || result.isOk()) {
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
resultError = result;
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
thrownError = error;
|
|
28
|
+
}
|
|
29
|
+
assert(!!(thrownError || resultError));
|
|
30
|
+
if (attempt < options.times && until(thrownError || resultError)) {
|
|
31
|
+
const sleepDuration = delay(attempt++);
|
|
32
|
+
await sleep(sleepDuration);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
if (thrownError) {
|
|
36
|
+
throw thrownError;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
return resultError;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export function ExponentialBackoffRetryPolicy(base) {
|
|
45
|
+
const baseDuration = Temporal.Duration.from(base);
|
|
46
|
+
return (attempt) => {
|
|
47
|
+
return multiplyDuration(baseDuration, 2 ** attempt);
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
export function JitteredRetryPolicy(base, options, dependencies) {
|
|
51
|
+
const random = dependencies?.random ?? new RealRandom();
|
|
52
|
+
const minFactor = options?.minFactor ?? 0;
|
|
53
|
+
const maxFactor = options?.maxFactor ?? 1;
|
|
54
|
+
assert(minFactor >= 0 && maxFactor > minFactor, `invalid jitter range: min=${minFactor} max=${maxFactor}`);
|
|
55
|
+
const basePolicy = base instanceof Function ? base : () => base;
|
|
56
|
+
return (attempt) => {
|
|
57
|
+
const baseDuration = Temporal.Duration.from(basePolicy(attempt));
|
|
58
|
+
const factor = random.nextNumber(minFactor, maxFactor);
|
|
59
|
+
return multiplyDuration(baseDuration, factor);
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Task, TaskMapper } from "./types";
|
|
2
|
+
import { BaseError, Result } from "../basic";
|
|
3
|
+
import { type DurationLike } from "../time";
|
|
4
|
+
export interface TimeoutOptions {
|
|
5
|
+
timeout: DurationLike;
|
|
6
|
+
useResult?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export type TimeoutErr = {
|
|
9
|
+
type: "timeout";
|
|
10
|
+
timeout: Temporal.Duration;
|
|
11
|
+
};
|
|
12
|
+
type TimeoutAsResult<Return, Options> = Options extends {
|
|
13
|
+
useResult: true;
|
|
14
|
+
} ? Return extends Result<infer V, infer E> ? Result<V, E | TimeoutErr> : never : Return;
|
|
15
|
+
export declare function timeout<T, O extends TimeoutOptions>(task: Task<T>, options: O): Promise<TimeoutAsResult<T, O>>;
|
|
16
|
+
export declare function timeout<T, O extends TimeoutOptions>(options: O): TaskMapper<T, TimeoutAsResult<T, O>>;
|
|
17
|
+
export declare class TimeoutError extends BaseError {
|
|
18
|
+
constructor(duration: Temporal.Duration);
|
|
19
|
+
}
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=timeout.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeout.d.ts","sourceRoot":"","sources":["../../src/task/timeout.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAE/C,OAAO,EAAE,SAAS,EAAkB,MAAM,EAAE,MAAM,UAAU,CAAA;AAC5D,OAAO,EAAqC,KAAK,YAAY,EAAE,MAAM,SAAS,CAAA;AAE9E,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,YAAY,CAAA;IACrB,SAAS,CAAC,EAAE,OAAO,CAAA;CACpB;AAED,MAAM,MAAM,UAAU,GAAG;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,OAAO,EAAE,QAAQ,CAAC,QAAQ,CAAA;CAAE,CAAA;AAExE,KAAK,eAAe,CAAC,MAAM,EAAE,OAAO,IAAI,OAAO,SAAS;IAAE,SAAS,EAAE,IAAI,CAAA;CAAE,GACvE,MAAM,SAAS,MAAM,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC,GACrC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,GACzB,KAAK,GACP,MAAM,CAAA;AAGV,wBAAgB,OAAO,CAAC,CAAC,EAAE,CAAC,SAAS,cAAc,EACjD,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,EACb,OAAO,EAAE,CAAC,GACT,OAAO,CAAC,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;AACjC,wBAAgB,OAAO,CAAC,CAAC,EAAE,CAAC,SAAS,cAAc,EACjD,OAAO,EAAE,CAAC,GACT,UAAU,CAAC,CAAC,EAAE,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;AA4CvC,qBAAa,YAAa,SAAQ,SAAS;gBAC7B,QAAQ,EAAE,QAAQ,CAAC,QAAQ;CAGxC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { BaseError, Err, ErrResult, Result } from "../basic/index.js";
|
|
2
|
+
import { durationToMs, prettyPrintDuration } from "../time/index.js";
|
|
3
|
+
export function timeout(taskOrOptions, options) {
|
|
4
|
+
if (typeof taskOrOptions === "function") {
|
|
5
|
+
return runTimeout(taskOrOptions, options);
|
|
6
|
+
}
|
|
7
|
+
return (task) => () => runTimeout(task, taskOrOptions);
|
|
8
|
+
}
|
|
9
|
+
async function runTimeout(task, options) {
|
|
10
|
+
const useResult = !!options.useResult;
|
|
11
|
+
const timeoutDuration = Temporal.Duration.from(options.timeout);
|
|
12
|
+
const timeoutMs = durationToMs(timeoutDuration);
|
|
13
|
+
// note: we can't use sleep here because we want to ensure timeoutPromise cancellation
|
|
14
|
+
let timeoutId = undefined;
|
|
15
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
16
|
+
timeoutId = setTimeout(() => {
|
|
17
|
+
reject(new TimeoutError(timeoutDuration));
|
|
18
|
+
}, timeoutMs);
|
|
19
|
+
});
|
|
20
|
+
try {
|
|
21
|
+
return (await Promise.race([task(), timeoutPromise]).catch((e) => {
|
|
22
|
+
if (useResult) {
|
|
23
|
+
return Err({ type: "timeout", timeout: timeoutDuration });
|
|
24
|
+
}
|
|
25
|
+
throw e;
|
|
26
|
+
}));
|
|
27
|
+
}
|
|
28
|
+
finally {
|
|
29
|
+
if (timeoutId !== undefined) {
|
|
30
|
+
clearTimeout(timeoutId);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export class TimeoutError extends BaseError {
|
|
35
|
+
constructor(duration) {
|
|
36
|
+
super(`Task has timeout after ${prettyPrintDuration(duration)}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/task/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,IAAI,CAAC,CAAC,IAAI,MAAM,OAAO,CAAC,CAAC,CAAC,CAAA;AAEtC,MAAM,MAAM,UAAU,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { PublicInterface } from "../types";
|
|
2
|
+
import type { DurationLike } from "./index";
|
|
3
|
+
export type IClock = PublicInterface<AbstractClock>;
|
|
4
|
+
export declare abstract class AbstractClock implements IClock {
|
|
5
|
+
abstract now(): Temporal.Instant;
|
|
6
|
+
nowDate(): Date;
|
|
7
|
+
}
|
|
8
|
+
export declare class Clock extends AbstractClock {
|
|
9
|
+
now(): Temporal.Instant;
|
|
10
|
+
}
|
|
11
|
+
export declare class TestClock extends AbstractClock {
|
|
12
|
+
private _now;
|
|
13
|
+
constructor(_now: Temporal.Instant);
|
|
14
|
+
now(): Temporal.Instant;
|
|
15
|
+
advance(duration: DurationLike): void;
|
|
16
|
+
reset(now: Temporal.Instant): void;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=Clock.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Clock.d.ts","sourceRoot":"","sources":["../../src/time/Clock.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAA;AAC/C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAE3C,MAAM,MAAM,MAAM,GAAG,eAAe,CAAC,aAAa,CAAC,CAAA;AAEnD,8BAAsB,aAAc,YAAW,MAAM;IACnD,QAAQ,CAAC,GAAG,IAAI,QAAQ,CAAC,OAAO;IAEhC,OAAO,IAAI,IAAI;CAGhB;AAED,qBAAa,KAAM,SAAQ,aAAa;IACtC,GAAG,IAAI,QAAQ,CAAC,OAAO;CAGxB;AAED,qBAAa,SAAU,SAAQ,aAAa;IAC9B,OAAO,CAAC,IAAI;gBAAJ,IAAI,EAAE,QAAQ,CAAC,OAAO;IAI1C,GAAG,IAAI,QAAQ,CAAC,OAAO;IAIvB,OAAO,CAAC,QAAQ,EAAE,YAAY,GAAG,IAAI;IAIrC,KAAK,CAAC,GAAG,EAAE,QAAQ,CAAC,OAAO,GAAG,IAAI;CAGnC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export class AbstractClock {
|
|
2
|
+
nowDate() {
|
|
3
|
+
return new Date(this.now().epochMilliseconds);
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
export class Clock extends AbstractClock {
|
|
7
|
+
now() {
|
|
8
|
+
return Temporal.Now.instant();
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class TestClock extends AbstractClock {
|
|
12
|
+
_now;
|
|
13
|
+
constructor(_now) {
|
|
14
|
+
super();
|
|
15
|
+
this._now = _now;
|
|
16
|
+
}
|
|
17
|
+
now() {
|
|
18
|
+
return this._now;
|
|
19
|
+
}
|
|
20
|
+
advance(duration) {
|
|
21
|
+
this._now = this._now.add(Temporal.Duration.from(duration));
|
|
22
|
+
}
|
|
23
|
+
reset(now) {
|
|
24
|
+
this._now = now;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/time/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA;AACvB,cAAc,YAAY,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sleep.d.ts","sourceRoot":"","sources":["../../src/time/sleep.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,YAAY,EAAE,MAAM,SAAS,CAAA;AAEzD,wBAAgB,KAAK,CAAC,QAAQ,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAI3D"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export type DurationLike = Omit<Temporal.DurationLike, "microseconds" | "nanoseconds"> | Omit<Temporal.Duration, "microseconds" | "nanoseconds">;
|
|
2
|
+
export declare function multiplyDuration(base: Temporal.Duration, factor: number): Temporal.Duration;
|
|
3
|
+
export declare function durationToMs(duration: Temporal.Duration): number;
|
|
4
|
+
export declare function prettyPrintDuration(duration: Temporal.Duration): string;
|
|
5
|
+
//# sourceMappingURL=temporal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"temporal.d.ts","sourceRoot":"","sources":["../../src/time/temporal.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,GACpB,IAAI,CAAC,QAAQ,CAAC,YAAY,EAAE,cAAc,GAAG,aAAa,CAAC,GAC3D,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,cAAc,GAAG,aAAa,CAAC,CAAA;AAE3D,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,GAAG,QAAQ,CAAC,QAAQ,CAW3F;AAED,wBAAgB,YAAY,CAAC,QAAQ,EAAE,QAAQ,CAAC,QAAQ,GAAG,MAAM,CAIhE;AAED,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,CAAC,QAAQ,GAAG,MAAM,CAkBvE"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export function multiplyDuration(base, factor) {
|
|
2
|
+
return Temporal.Duration.from({
|
|
3
|
+
years: Math.round(base.years * factor),
|
|
4
|
+
months: Math.round(base.months * factor),
|
|
5
|
+
weeks: Math.round(base.weeks * factor),
|
|
6
|
+
days: Math.round(base.days * factor),
|
|
7
|
+
hours: Math.round(base.hours * factor),
|
|
8
|
+
minutes: Math.round(base.minutes * factor),
|
|
9
|
+
seconds: Math.round(base.seconds * factor),
|
|
10
|
+
milliseconds: Math.round(base.milliseconds * factor),
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
export function durationToMs(duration) {
|
|
14
|
+
const now = Temporal.Now.instant();
|
|
15
|
+
const target = now.add(Temporal.Duration.from(duration));
|
|
16
|
+
return target.epochMilliseconds - now.epochMilliseconds;
|
|
17
|
+
}
|
|
18
|
+
export function prettyPrintDuration(duration) {
|
|
19
|
+
// note: there is Intl.DurationFormat("en", { style: "narrow" }).format(duration) which should do the same thing but:
|
|
20
|
+
// 1. It's not available in node <= 22, Intl.DurationFormat is not available
|
|
21
|
+
// 2. Exact formatting output is dependent on ICU data and on some environments (CI) months get formatted as m instead of mo
|
|
22
|
+
// for these reasons it's better to always use bespoke implementation
|
|
23
|
+
const parts = [];
|
|
24
|
+
if (duration.years)
|
|
25
|
+
parts.push(`${duration.years}y`);
|
|
26
|
+
if (duration.months)
|
|
27
|
+
parts.push(`${duration.months}mo`);
|
|
28
|
+
if (duration.weeks)
|
|
29
|
+
parts.push(`${duration.weeks}w`);
|
|
30
|
+
if (duration.days)
|
|
31
|
+
parts.push(`${duration.days}d`);
|
|
32
|
+
if (duration.hours)
|
|
33
|
+
parts.push(`${duration.hours}h`);
|
|
34
|
+
if (duration.minutes)
|
|
35
|
+
parts.push(`${duration.minutes}m`);
|
|
36
|
+
if (duration.seconds)
|
|
37
|
+
parts.push(`${duration.seconds}s`);
|
|
38
|
+
if (duration.milliseconds)
|
|
39
|
+
parts.push(`${duration.milliseconds}ms`);
|
|
40
|
+
return parts.join(" ");
|
|
41
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AACA,MAAM,MAAM,eAAe,CAAC,CAAC,IAAI;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "superlib",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A missing standard library for TypeScript, providing utilities for async orchestration, error handling, filesystem abstraction, and common patterns",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"async",
|
|
7
|
+
"result",
|
|
8
|
+
"stdlib",
|
|
9
|
+
"task",
|
|
10
|
+
"typescript",
|
|
11
|
+
"utilities"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": "Kris Kaczor",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/superlibdotdev/superlib.git"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"README.md"
|
|
22
|
+
],
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "./dist/index.js",
|
|
25
|
+
"module": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"default": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./platform/filesystem": {
|
|
33
|
+
"types": "./dist/platform/filesystem/index.d.ts",
|
|
34
|
+
"default": "./dist/platform/filesystem/index.js"
|
|
35
|
+
},
|
|
36
|
+
"./platform/getPort": {
|
|
37
|
+
"types": "./dist/platform/getPort/index.d.ts",
|
|
38
|
+
"default": "./dist/platform/getPort/index.js"
|
|
39
|
+
},
|
|
40
|
+
"./platform/JsonHttpClient": {
|
|
41
|
+
"types": "./dist/platform/JsonHttpClient/index.d.ts",
|
|
42
|
+
"default": "./dist/platform/JsonHttpClient/index.js"
|
|
43
|
+
},
|
|
44
|
+
"./platform/ProcessContext": {
|
|
45
|
+
"types": "./dist/platform/ProcessContext/index.d.ts",
|
|
46
|
+
"default": "./dist/platform/ProcessContext/index.js"
|
|
47
|
+
},
|
|
48
|
+
"./platform/safeFetch": {
|
|
49
|
+
"types": "./dist/platform/safeFetch/index.d.ts",
|
|
50
|
+
"default": "./dist/platform/safeFetch/index.js"
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "tsc -p tsconfig.build.json && bun run scripts/fix-imports.ts && cp ../../README.md .",
|
|
55
|
+
"lint": "oxlint --type-aware --type-check --deny-warnings",
|
|
56
|
+
"lint:fix": "bun run lint --fix --fix-dangerously --fix-suggestions",
|
|
57
|
+
"test": "bun test",
|
|
58
|
+
"test:vitest": "bun run scripts/transform-tests-for-vitest.ts && vitest run",
|
|
59
|
+
"fix": "cd ../.. && bun run fix"
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"remeda": "^2.32.0",
|
|
63
|
+
"temporal-polyfill": "^0.3.0"
|
|
64
|
+
},
|
|
65
|
+
"devDependencies": {
|
|
66
|
+
"@types/node": "^22.0.0",
|
|
67
|
+
"vitest": "^3.0.0",
|
|
68
|
+
"zod": "^4.3.5"
|
|
69
|
+
}
|
|
70
|
+
}
|