@guillaume-docquier/tools-ts 1.3.0 → 1.4.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/dist/Measure.d.ts +21 -0
- package/dist/Measure.js +53 -0
- package/dist/Measure.js.map +1 -0
- package/dist/Result.d.ts +2 -0
- package/dist/Result.js +7 -2
- package/dist/Result.js.map +1 -1
- package/dist/entry.tools-ts.d.ts +1 -0
- package/dist/entry.tools-ts.js +1 -0
- package/dist/entry.tools-ts.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
type BasicLogger = {
|
|
2
|
+
info: (message: string, context?: Record<string, unknown>) => void;
|
|
3
|
+
};
|
|
4
|
+
export declare const Measure: {
|
|
5
|
+
executionTime: typeof executionTime;
|
|
6
|
+
/**
|
|
7
|
+
* Prints the current memory usage using {@link https://nodejs.org/api/process.html#processmemoryusage process.memoryUsage()}.
|
|
8
|
+
*/
|
|
9
|
+
memoryUsage(logger?: BasicLogger): void;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Measures the execution time of the given synchronous action and logs it.
|
|
13
|
+
* The result of the action is returned.
|
|
14
|
+
*/
|
|
15
|
+
declare function executionTime<T>(label: string, action: () => Promise<T>, logger?: BasicLogger): Promise<T>;
|
|
16
|
+
/**
|
|
17
|
+
* Measures the execution time of the given asynchronous action and logs it.
|
|
18
|
+
* The result of the action is returned.
|
|
19
|
+
*/
|
|
20
|
+
declare function executionTime<T>(label: string, action: () => T, logger?: BasicLogger): T;
|
|
21
|
+
export {};
|
package/dist/Measure.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export const Measure = {
|
|
2
|
+
// You can't overload while defining object properties, which is why the function implementation is not inlined
|
|
3
|
+
executionTime,
|
|
4
|
+
/**
|
|
5
|
+
* Prints the current memory usage using {@link https://nodejs.org/api/process.html#processmemoryusage process.memoryUsage()}.
|
|
6
|
+
*/
|
|
7
|
+
memoryUsage(logger = console) {
|
|
8
|
+
const memoryUsage = process.memoryUsage(); // info in bytes
|
|
9
|
+
logger.info("memory usage", {
|
|
10
|
+
/**
|
|
11
|
+
* Resident Set Size, the total memory allocated for the whole process.
|
|
12
|
+
* In Railway, this is what you pay for.
|
|
13
|
+
*
|
|
14
|
+
* It is basically the sum of heapTotal + external of all threads (workers).
|
|
15
|
+
*/
|
|
16
|
+
rss: bytesToMb(memoryUsage.rss),
|
|
17
|
+
/**
|
|
18
|
+
* Available heap for the current thread
|
|
19
|
+
*/
|
|
20
|
+
heapTotal: bytesToMb(memoryUsage.heapTotal),
|
|
21
|
+
/**
|
|
22
|
+
* Heap used for the current thread
|
|
23
|
+
*/
|
|
24
|
+
heapUsed: bytesToMb(memoryUsage.heapUsed),
|
|
25
|
+
/**
|
|
26
|
+
* Memory used by C++ for the current thread, including arrayBuffers
|
|
27
|
+
*/
|
|
28
|
+
external: bytesToMb(memoryUsage.external),
|
|
29
|
+
/**
|
|
30
|
+
* ArrayBuffers used for the current thread
|
|
31
|
+
*/
|
|
32
|
+
arrayBuffers: bytesToMb(memoryUsage.arrayBuffers),
|
|
33
|
+
});
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
function executionTime(label, action, logger = console) {
|
|
37
|
+
const startedAt = performance.now();
|
|
38
|
+
const result = action();
|
|
39
|
+
if (result instanceof Promise) {
|
|
40
|
+
return result.finally(() => {
|
|
41
|
+
logExecutionTime(label, startedAt, logger);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
logExecutionTime(label, startedAt, logger);
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
function logExecutionTime(label, startedAt, logger) {
|
|
48
|
+
logger.info(`${label}: ${(performance.now() - startedAt).toFixed(2)}ms`);
|
|
49
|
+
}
|
|
50
|
+
function bytesToMb(bytes) {
|
|
51
|
+
return `${Math.round(bytes / 1024 / 1024).toFixed(2)} MB`;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingUR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CAAC,IAAI,CAAC,GAAG,KAAK,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAA;AAC1E,CAAC;AAED,SAAS,SAAS,CAAC,KAAa;IAC9B,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAA;AAC3D,CAAC"}
|
package/dist/Result.d.ts
CHANGED
|
@@ -46,6 +46,7 @@ export declare const Result: {
|
|
|
46
46
|
* Creates a Result from the result of a promise.
|
|
47
47
|
* If the promise resolves, you will get a {@link Result.Success}.
|
|
48
48
|
* If the promise rejects, you will get a {@link Result.Failure}.
|
|
49
|
+
* If the promise rejects with a fatal error, the error will be rethrown.
|
|
49
50
|
*
|
|
50
51
|
* @param fn The callback to execute.
|
|
51
52
|
*/
|
|
@@ -54,6 +55,7 @@ declare function tryCatch<T>(fn: () => Promise<T>): Promise<Result<T, unknown>>;
|
|
|
54
55
|
* Creates a Result from the result of a function that could throw.
|
|
55
56
|
* If the function does not throw, you will get a {@link Result.Success}.
|
|
56
57
|
* If the function throws, you will get a {@link Result.Failure}.
|
|
58
|
+
* If the function throws a fatal error, the error will be rethrown.
|
|
57
59
|
*
|
|
58
60
|
* @param fn The callback to execute.
|
|
59
61
|
*/
|
package/dist/Result.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Rethrow } from "./errors/Rethrow.js";
|
|
1
2
|
/**
|
|
2
3
|
* Utility functions to work with Results.
|
|
3
4
|
* @example
|
|
@@ -31,7 +32,7 @@ export const Result = {
|
|
|
31
32
|
isFailure: (maybeFailure) => {
|
|
32
33
|
return maybeFailure.type === "Failure";
|
|
33
34
|
},
|
|
34
|
-
// You can overload while defining object properties, which is why the function implementation is not inlined
|
|
35
|
+
// You can't overload while defining object properties, which is why the function implementation is not inlined
|
|
35
36
|
tryCatch,
|
|
36
37
|
};
|
|
37
38
|
function tryCatch(fn) {
|
|
@@ -39,12 +40,16 @@ function tryCatch(fn) {
|
|
|
39
40
|
const result = fn();
|
|
40
41
|
// Async handling
|
|
41
42
|
if (result instanceof Promise) {
|
|
42
|
-
return result.then(Result.Success).catch(
|
|
43
|
+
return result.then(Result.Success).catch((error) => {
|
|
44
|
+
Rethrow.ifFatal(error);
|
|
45
|
+
return Result.Failure(error);
|
|
46
|
+
});
|
|
43
47
|
}
|
|
44
48
|
// Sync handling
|
|
45
49
|
return Result.Success(result);
|
|
46
50
|
}
|
|
47
51
|
catch (error) {
|
|
52
|
+
Rethrow.ifFatal(error);
|
|
48
53
|
return Result.Failure(error);
|
|
49
54
|
}
|
|
50
55
|
}
|
package/dist/Result.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Result.js","sourceRoot":"","sources":["../src/Result.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Result.js","sourceRoot":"","sources":["../src/Result.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAkB7C;;;;;;;;;;;GAWG;AAEH,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB;;OAEG;IACH,OAAO,EAAE,CAAS,KAAa,EAAmB,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;IAEjF;;OAEG;IACH,SAAS,EAAE,CAAS,YAAqC,EAAmC,EAAE;QAC5F,OAAO,YAAY,CAAC,IAAI,KAAK,SAAS,CAAA;IACxC,CAAC;IAED;;OAEG;IACH,OAAO,EAAE,CAAS,KAAa,EAAmB,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;IAEjF;;OAEG;IACH,SAAS,EAAE,CAAS,YAAqC,EAAmC,EAAE;QAC5F,OAAO,YAAY,CAAC,IAAI,KAAK,SAAS,CAAA;IACxC,CAAC;IAED,+GAA+G;IAC/G,QAAQ;CACT,CAAA;AAoBD,SAAS,QAAQ,CAAI,EAAwB;IAC3C,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,EAAE,EAAE,CAAA;QAEnB,iBAAiB;QACjB,IAAI,MAAM,YAAY,OAAO,EAAE,CAAC;YAC9B,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;gBACjD,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;gBACtB,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;YAC9B,CAAC,CAAC,CAAA;QACJ,CAAC;QAED,gBAAgB;QAChB,OAAO,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;IAC/B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QACtB,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAC9B,CAAC;AACH,CAAC"}
|
package/dist/entry.tools-ts.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export type * from "./utility-types.js";
|
|
|
8
8
|
export { asArray } from "./asArray.js";
|
|
9
9
|
export { Assert } from "./Assert.js";
|
|
10
10
|
export { Result, type Success, type Failure } from "./Result.js";
|
|
11
|
+
export { Measure } from "./Measure.js";
|
|
11
12
|
export { TypeGuard } from "./TypeGuard.js";
|
|
12
13
|
export { setTimeoutAsync } from "./setTimeoutAsync.js";
|
|
13
14
|
export { noop, asyncNoop } from "./noop.js";
|
package/dist/entry.tools-ts.js
CHANGED
|
@@ -7,6 +7,7 @@ export { getEnumKey } from "./getEnumKey.js";
|
|
|
7
7
|
export { asArray } from "./asArray.js";
|
|
8
8
|
export { Assert } from "./Assert.js";
|
|
9
9
|
export { Result } from "./Result.js";
|
|
10
|
+
export { Measure } from "./Measure.js";
|
|
10
11
|
export { TypeGuard } from "./TypeGuard.js";
|
|
11
12
|
export { setTimeoutAsync } from "./setTimeoutAsync.js";
|
|
12
13
|
export { noop, asyncNoop } from "./noop.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entry.tools-ts.js","sourceRoot":"","sources":["../src/entry.tools-ts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAC3D,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AACnD,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAA;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAA;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAI5C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,MAAM,EAA8B,MAAM,aAAa,CAAA;AAChE,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AACtD,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAE3C,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAC5C,OAAO,EAAE,yBAAyB,EAAE,MAAM,wBAAwB,CAAA;AAClE,OAAO,EAAE,iBAAiB,EAAE,MAAM,0CAA0C,CAAA;AAC5E,OAAO,EAAE,sBAAsB,EAAE,MAAM,+CAA+C,CAAA;AAEtF,OAAO,EAAE,oBAAoB,EAAE,MAAM,kCAAkC,CAAA;AAKvE,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA"}
|
|
1
|
+
{"version":3,"file":"entry.tools-ts.js","sourceRoot":"","sources":["../src/entry.tools-ts.ts"],"names":[],"mappings":"AAAA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