@cripty2001/utils 0.0.232 → 0.0.234
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.d.ts +7 -5
- package/dist/index.js +25 -12
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Whispr } from "@cripty2001/whispr";
|
|
2
|
-
import type { TSchema } from "@sinclair/typebox";
|
|
2
|
+
import type { Static, TSchema } from "@sinclair/typebox";
|
|
3
3
|
export type JSONEncodable = number | string | boolean | JSONEncodable[] | null | {
|
|
4
4
|
[key: string]: JSONEncodable;
|
|
5
5
|
};
|
|
@@ -121,15 +121,17 @@ export declare function getEnvInt(key: string, constraints?: {
|
|
|
121
121
|
max?: number;
|
|
122
122
|
}, defaultValue?: number): number;
|
|
123
123
|
/**
|
|
124
|
-
* @see getEnv, with the additional constraint that the environment variable must be
|
|
125
|
-
* Note: the default value is not used instead of
|
|
124
|
+
* @see getEnv, with the additional constraint that the environment variable must be valid JSON.
|
|
125
|
+
* Note: the default value is not used instead of invalid JSON or a schema mismatch. Those therefore throw.
|
|
126
126
|
*
|
|
127
|
-
* @param schema
|
|
127
|
+
* @param schema Optional TypeBox schema.
|
|
128
128
|
*/
|
|
129
|
-
export declare function getEnvJson<T>(key: string, defaultValue?: T
|
|
129
|
+
export declare function getEnvJson<T>(key: string, defaultValue?: T): T;
|
|
130
|
+
export declare function getEnvJson<S extends TSchema>(key: string, defaultValue: Static<S> | undefined, schema: S): Static<S>;
|
|
130
131
|
export declare const CURRENT_TS_MS: () => Whispr<number>;
|
|
131
132
|
export declare function getClock(rate: number): Whispr<number>;
|
|
132
133
|
export declare function timediff2HumanReadable(diffMs: number): string;
|
|
133
134
|
export declare function fn2promise<T>(fn: () => T | Promise<T>): Promise<T>;
|
|
134
135
|
export type SafeApplyed<T> = (updater: (draft: T) => T | void) => T;
|
|
135
136
|
export declare function safeApply<T>(updater: (draft: T) => T | void, incoming: T): T;
|
|
137
|
+
export declare function timesafeEqual(a: string, b: string): boolean;
|
package/dist/index.js
CHANGED
|
@@ -23,7 +23,10 @@ exports.getClock = getClock;
|
|
|
23
23
|
exports.timediff2HumanReadable = timediff2HumanReadable;
|
|
24
24
|
exports.fn2promise = fn2promise;
|
|
25
25
|
exports.safeApply = safeApply;
|
|
26
|
+
exports.timesafeEqual = timesafeEqual;
|
|
26
27
|
const whispr_1 = require("@cripty2001/whispr");
|
|
28
|
+
const value_1 = require("@sinclair/typebox/value");
|
|
29
|
+
const crypto_1 = require("crypto");
|
|
27
30
|
const lodash_1 = require("lodash");
|
|
28
31
|
const U32_LIMIT = 0x1_0000_0000;
|
|
29
32
|
function randomU32() {
|
|
@@ -285,8 +288,10 @@ function getEnvString(key, allowEmpty = false, defaultValue) {
|
|
|
285
288
|
*/
|
|
286
289
|
function getEnvInt(key, constraints = {}, defaultValue) {
|
|
287
290
|
return getEnv(key, defaultValue, (s) => {
|
|
288
|
-
|
|
289
|
-
|
|
291
|
+
if (!/^[+-]?\d+$/.test(s))
|
|
292
|
+
throw new GetenvHandledError(`Invalid integer.`);
|
|
293
|
+
const parsed = Number(s);
|
|
294
|
+
if (!Number.isInteger(parsed))
|
|
290
295
|
throw new GetenvHandledError(`Invalid integer.`);
|
|
291
296
|
const MIN = constraints.min ?? Number.MIN_SAFE_INTEGER;
|
|
292
297
|
if (parsed < MIN)
|
|
@@ -297,17 +302,20 @@ function getEnvInt(key, constraints = {}, defaultValue) {
|
|
|
297
302
|
return parsed;
|
|
298
303
|
});
|
|
299
304
|
}
|
|
300
|
-
/**
|
|
301
|
-
* @see getEnv, with the additional constraint that the environment variable must be a valid JSON object.
|
|
302
|
-
* Note: the default value is not used instead of an invalid JSON object. An invalid JSON object therefore throws an error.
|
|
303
|
-
*
|
|
304
|
-
* @param schema The schema to use to validate the environment variable value
|
|
305
|
-
*/
|
|
306
305
|
function getEnvJson(key, defaultValue, schema) {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
306
|
+
return getEnv(key, defaultValue, (s) => {
|
|
307
|
+
const parsed = (() => {
|
|
308
|
+
try {
|
|
309
|
+
return JSON.parse(s);
|
|
310
|
+
}
|
|
311
|
+
catch {
|
|
312
|
+
throw new GetenvHandledError(`Invalid JSON.`);
|
|
313
|
+
}
|
|
314
|
+
})();
|
|
315
|
+
if (schema === undefined)
|
|
316
|
+
return parsed;
|
|
317
|
+
return value_1.Value.Parse(schema, parsed);
|
|
318
|
+
});
|
|
311
319
|
}
|
|
312
320
|
const [currentTsMs, setCurrentTsMs] = whispr_1.Whispr.create(Date.now());
|
|
313
321
|
let currentTsMsInterval = null;
|
|
@@ -361,3 +369,8 @@ function safeApply(updater, incoming) {
|
|
|
361
369
|
const chosen = new_data ?? cloned;
|
|
362
370
|
return structuredClone(chosen);
|
|
363
371
|
}
|
|
372
|
+
function timesafeEqual(a, b) {
|
|
373
|
+
if (a.length !== b.length)
|
|
374
|
+
return false;
|
|
375
|
+
return (0, crypto_1.timingSafeEqual)(Buffer.from(a), Buffer.from(b));
|
|
376
|
+
}
|
package/package.json
CHANGED