@kikiutils/shared 18.0.1 → 18.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/dist/buffer.d.ts +19 -21
- package/dist/buffer.d.ts.map +1 -1
- package/dist/buffer.js +18 -19
- package/dist/buffer.js.map +1 -1
- package/dist/classes/enhanced-date.d.ts +425 -0
- package/dist/classes/enhanced-date.d.ts.map +1 -0
- package/dist/classes/enhanced-date.js +591 -0
- package/dist/classes/enhanced-date.js.map +1 -0
- package/dist/classes/path.d.ts +211 -48
- package/dist/classes/path.d.ts.map +1 -1
- package/dist/classes/path.js +181 -52
- package/dist/classes/path.js.map +1 -1
- package/dist/classes/precision-number.d.ts +44 -39
- package/dist/classes/precision-number.d.ts.map +1 -1
- package/dist/classes/precision-number.js +51 -45
- package/dist/classes/precision-number.js.map +1 -1
- package/dist/classes/ssh-client.d.ts +3 -4
- package/dist/classes/ssh-client.d.ts.map +1 -1
- package/dist/classes/ssh-client.js +2 -2
- package/dist/classes/ssh-client.js.map +1 -1
- package/dist/clipboard.d.ts +25 -26
- package/dist/clipboard.d.ts.map +1 -1
- package/dist/clipboard.js +23 -23
- package/dist/clipboard.js.map +1 -1
- package/dist/consola.d.ts +12 -15
- package/dist/consola.d.ts.map +1 -1
- package/dist/consola.js +10 -12
- package/dist/consola.js.map +1 -1
- package/dist/crypto-hash.d.ts +65 -56
- package/dist/crypto-hash.d.ts.map +1 -1
- package/dist/crypto-hash.js +55 -45
- package/dist/crypto-hash.js.map +1 -1
- package/dist/datetime.d.ts +41 -53
- package/dist/datetime.d.ts.map +1 -1
- package/dist/datetime.js +53 -66
- package/dist/datetime.js.map +1 -1
- package/dist/element-plus.d.ts +13 -14
- package/dist/element-plus.d.ts.map +1 -1
- package/dist/element-plus.js +11 -11
- package/dist/element-plus.js.map +1 -1
- package/dist/enum.d.ts +39 -36
- package/dist/enum.d.ts.map +1 -1
- package/dist/enum.js +36 -32
- package/dist/enum.js.map +1 -1
- package/dist/env.d.ts +22 -15
- package/dist/env.d.ts.map +1 -1
- package/dist/env.js +20 -12
- package/dist/env.js.map +1 -1
- package/dist/event-awaiter.d.ts +47 -52
- package/dist/event-awaiter.d.ts.map +1 -1
- package/dist/event-awaiter.js +46 -50
- package/dist/event-awaiter.js.map +1 -1
- package/dist/general.d.ts +22 -17
- package/dist/general.d.ts.map +1 -1
- package/dist/general.js.map +1 -1
- package/dist/hash.d.ts +24 -21
- package/dist/hash.d.ts.map +1 -1
- package/dist/hash.js +20 -16
- package/dist/hash.js.map +1 -1
- package/dist/math.d.ts +20 -19
- package/dist/math.d.ts.map +1 -1
- package/dist/math.js +14 -12
- package/dist/math.js.map +1 -1
- package/dist/number.d.ts +10 -9
- package/dist/number.d.ts.map +1 -1
- package/dist/number.js +9 -7
- package/dist/number.js.map +1 -1
- package/dist/object.d.ts +22 -10
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +21 -8
- package/dist/object.js.map +1 -1
- package/dist/pino.d.ts +12 -15
- package/dist/pino.d.ts.map +1 -1
- package/dist/pino.js +10 -15
- package/dist/pino.js.map +1 -1
- package/dist/random.d.ts +17 -15
- package/dist/random.d.ts.map +1 -1
- package/dist/random.js +16 -13
- package/dist/random.js.map +1 -1
- package/dist/redis/script-runner.d.ts +57 -0
- package/dist/redis/script-runner.d.ts.map +1 -0
- package/dist/redis/script-runner.js +46 -0
- package/dist/redis/script-runner.js.map +1 -0
- package/dist/storages/lru/keyed-store.d.ts +18 -14
- package/dist/storages/lru/keyed-store.d.ts.map +1 -1
- package/dist/storages/lru/keyed-store.js +17 -12
- package/dist/storages/lru/keyed-store.js.map +1 -1
- package/dist/storages/redis/keyed-store.d.ts +11 -21
- package/dist/storages/redis/keyed-store.d.ts.map +1 -1
- package/dist/storages/redis/keyed-store.js +10 -19
- package/dist/storages/redis/keyed-store.js.map +1 -1
- package/dist/storages/redis/msgpack.d.ts +33 -13
- package/dist/storages/redis/msgpack.d.ts.map +1 -1
- package/dist/storages/redis/msgpack.js +32 -11
- package/dist/storages/redis/msgpack.js.map +1 -1
- package/dist/storages/redis/types.d.ts +34 -17
- package/dist/storages/redis/types.d.ts.map +1 -1
- package/dist/string.d.ts +12 -11
- package/dist/string.d.ts.map +1 -1
- package/dist/string.js +20 -15
- package/dist/string.js.map +1 -1
- package/dist/time.d.ts +26 -21
- package/dist/time.d.ts.map +1 -1
- package/dist/time.js +24 -18
- package/dist/time.js.map +1 -1
- package/dist/types/filtered-key-path.d.ts +4 -15
- package/dist/types/filtered-key-path.d.ts.map +1 -1
- package/dist/types/index.d.ts +10 -10
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/vue.d.ts +7 -7
- package/dist/types/vue.d.ts.map +1 -1
- package/dist/url.d.ts +15 -14
- package/dist/url.d.ts.map +1 -1
- package/dist/url.js +12 -10
- package/dist/url.js.map +1 -1
- package/dist/vue.d.ts +19 -21
- package/dist/vue.d.ts.map +1 -1
- package/dist/vue.js +15 -16
- package/dist/vue.js.map +1 -1
- package/dist/web.d.ts +14 -11
- package/dist/web.d.ts.map +1 -1
- package/dist/web.js +12 -8
- package/dist/web.js.map +1 -1
- package/package.json +20 -20
- package/src/buffer.ts +18 -19
- package/src/classes/enhanced-date.ts +665 -0
- package/src/classes/path.ts +232 -91
- package/src/classes/precision-number.ts +61 -53
- package/src/classes/ssh-client.ts +8 -5
- package/src/clipboard.ts +23 -23
- package/src/consola.ts +10 -12
- package/src/crypto-hash.ts +55 -45
- package/src/datetime.ts +65 -85
- package/src/element-plus.ts +11 -11
- package/src/enum.ts +36 -32
- package/src/env.ts +20 -12
- package/src/event-awaiter.ts +49 -50
- package/src/general.ts +20 -14
- package/src/hash.ts +20 -16
- package/src/math.ts +18 -16
- package/src/number.ts +9 -7
- package/src/object.ts +21 -8
- package/src/pino.ts +11 -15
- package/src/random.ts +16 -13
- package/src/redis/script-runner.ts +107 -0
- package/src/storages/lru/keyed-store.ts +17 -12
- package/src/storages/redis/keyed-store.ts +10 -19
- package/src/storages/redis/msgpack.ts +32 -11
- package/src/storages/redis/types.ts +32 -14
- package/src/string.ts +10 -8
- package/src/time.ts +24 -18
- package/src/types/filtered-key-path.ts +9 -14
- package/src/types/vue.ts +6 -5
- package/src/url.ts +12 -10
- package/src/vue.ts +15 -16
- package/src/web.ts +12 -8
|
@@ -1,28 +1,19 @@
|
|
|
1
1
|
//#region src/storages/redis/keyed-store.ts
|
|
2
2
|
/**
|
|
3
|
-
* Creates a keyed store
|
|
3
|
+
* Creates a keyed store factory sharing a caller-owned Redis-like storage facade.
|
|
4
4
|
*
|
|
5
|
-
* @
|
|
5
|
+
* @remarks
|
|
6
|
+
* The key resolver is called synchronously once per operation with the operation's key arguments.
|
|
7
|
+
* Resolver exceptions propagate synchronously; asynchronous storage failures retain the underlying rejection.
|
|
8
|
+
* `setItemWithTtl` accepts its TTL in seconds before the value and key arguments.
|
|
9
|
+
* The factory does not open or close the underlying connection; cleanup remains the caller's responsibility.
|
|
6
10
|
*
|
|
7
|
-
* @
|
|
11
|
+
* @typeParam D - The stored value type used by reads and writes.
|
|
8
12
|
*
|
|
9
|
-
* @
|
|
10
|
-
* getItem, getItemTtl, hasItem, removeItem, resolveKey, setItem, setItemWithTtl
|
|
11
|
-
* }>} A factory that accepts a key resolver and returns a frozen Redis keyed-store facade
|
|
13
|
+
* @param storage - The shared Redis-like storage instance.
|
|
12
14
|
*
|
|
13
|
-
* @
|
|
14
|
-
*
|
|
15
|
-
* import { createRedisKeyedStore } from '@kikiutils/shared/storages/redis/keyed-store';
|
|
16
|
-
* import { createRedisMsgpackStorage } from '@kikiutils/shared/storages/redis/msgpack';
|
|
17
|
-
* import { createClient } from 'redis';
|
|
18
|
-
*
|
|
19
|
-
* const client = createClient();
|
|
20
|
-
* await client.connect();
|
|
21
|
-
* const storage = createRedisMsgpackStorage(client as unknown as RedisLikeAdapter);
|
|
22
|
-
* const keyedStore = createRedisKeyedStore(storage)((userId: string) => `user:${userId}`);
|
|
23
|
-
* await keyedStore.setItem({ name: 'Alice' }, 'user-123');
|
|
24
|
-
* const user = await keyedStore.getItem('user-123'); // { name: 'Alice' }
|
|
25
|
-
* ```
|
|
15
|
+
* @returns A factory accepting a key resolver and returning a new frozen keyed facade sharing the storage.
|
|
16
|
+
* Reads, writes, removals, and TTL results retain the underlying storage contract.
|
|
26
17
|
*/
|
|
27
18
|
function createRedisKeyedStore(storage) {
|
|
28
19
|
return (keyFn) => Object.freeze({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keyed-store.js","names":[],"sources":["../../../src/storages/redis/keyed-store.ts"],"sourcesContent":["import type { MaybeReadonly } from '../../types';\n\nimport type { RedisLikeStorage } from './types';\n\n/**\n * Creates a keyed store
|
|
1
|
+
{"version":3,"file":"keyed-store.js","names":[],"sources":["../../../src/storages/redis/keyed-store.ts"],"sourcesContent":["import type { MaybeReadonly } from '../../types';\n\nimport type { RedisLikeStorage } from './types';\n\n/**\n * Creates a keyed store factory sharing a caller-owned Redis-like storage facade.\n *\n * @remarks\n * The key resolver is called synchronously once per operation with the operation's key arguments.\n * Resolver exceptions propagate synchronously; asynchronous storage failures retain the underlying rejection.\n * `setItemWithTtl` accepts its TTL in seconds before the value and key arguments.\n * The factory does not open or close the underlying connection; cleanup remains the caller's responsibility.\n *\n * @typeParam D - The stored value type used by reads and writes.\n *\n * @param storage - The shared Redis-like storage instance.\n *\n * @returns A factory accepting a key resolver and returning a new frozen keyed facade sharing the storage.\n * Reads, writes, removals, and TTL results retain the underlying storage contract.\n */\nexport function createRedisKeyedStore<D = unknown>(storage: MaybeReadonly<RedisLikeStorage>) {\n return <P extends any[]>(keyFn: (...args: P) => string) => Object.freeze({\n getItem: (...args: P) => storage.getItem<D>(keyFn(...args)),\n getItemTtl: (...args: P) => storage.getItemTtl(keyFn(...args)),\n hasItem: (...args: P) => storage.hasItem(keyFn(...args)),\n removeItem: (...args: P) => storage.removeItem(keyFn(...args)),\n resolveKey: (...args: P) => keyFn(...args),\n setItem: (value: D, ...args: P) => storage.setItem(keyFn(...args), value),\n setItemWithTtl(ttlSeconds: number, value: D, ...args: P) {\n return storage.setItemWithTtl(keyFn(...args), ttlSeconds, value);\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAoBA,SAAgB,sBAAmC,SAA0C;CACzF,QAAyB,UAAkC,OAAO,OAAO;EACrE,UAAU,GAAG,SAAY,QAAQ,QAAW,MAAM,GAAG,IAAI,CAAC;EAC1D,aAAa,GAAG,SAAY,QAAQ,WAAW,MAAM,GAAG,IAAI,CAAC;EAC7D,UAAU,GAAG,SAAY,QAAQ,QAAQ,MAAM,GAAG,IAAI,CAAC;EACvD,aAAa,GAAG,SAAY,QAAQ,WAAW,MAAM,GAAG,IAAI,CAAC;EAC7D,aAAa,GAAG,SAAY,MAAM,GAAG,IAAI;EACzC,UAAU,OAAU,GAAG,SAAY,QAAQ,QAAQ,MAAM,GAAG,IAAI,GAAG,KAAK;EACxE,eAAe,YAAoB,OAAU,GAAG,MAAS;GACrD,OAAO,QAAQ,eAAe,MAAM,GAAG,IAAI,GAAG,YAAY,KAAK;EACnE;CACJ,CAAC;AACL"}
|
|
@@ -1,26 +1,46 @@
|
|
|
1
1
|
import { RedisLikeAdapter, RedisLikeStorage } from "./types.js";
|
|
2
2
|
//#region src/storages/redis/msgpack.d.ts
|
|
3
3
|
/**
|
|
4
|
-
* Creates a Redis-like storage using
|
|
4
|
+
* Creates a frozen Redis-like storage facade using MessagePack serialization.
|
|
5
5
|
*
|
|
6
|
-
* @
|
|
6
|
+
* @remarks
|
|
7
|
+
* Adapter rejections and MessagePack serialization errors propagate to the caller.
|
|
8
|
+
* Reads resolve to `null` when no raw value exists. Writes resolve to `true` only when the adapter returns `OK`;
|
|
9
|
+
* removal resolves to `true` only when exactly one key is deleted.
|
|
10
|
+
* The adapter remains caller-owned; creating the facade does not open or close a connection.
|
|
7
11
|
*
|
|
8
|
-
* @
|
|
9
|
-
*
|
|
12
|
+
* @param adapter - The caller-owned adapter for raw binary operations.
|
|
13
|
+
*
|
|
14
|
+
* @returns A new frozen facade sharing the supplied adapter.
|
|
10
15
|
*
|
|
11
16
|
* @example
|
|
12
|
-
*
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
13
19
|
* import { createRedisMsgpackStorage } from '@kikiutils/shared/storages/redis/msgpack';
|
|
14
|
-
* import {
|
|
20
|
+
* import type { RedisLikeAdapter } from '@kikiutils/shared/storages/redis/types';
|
|
21
|
+
*
|
|
22
|
+
* // This in-memory adapter demonstrates non-expiring operations without an external service.
|
|
23
|
+
* const values = new Map<string, Uint8Array>();
|
|
24
|
+
* const adapter: RedisLikeAdapter = {
|
|
25
|
+
* delete: async (key) => values.delete(key) ? 1 : 0,
|
|
26
|
+
* getBuffer: async (key) => values.get(key) ?? null,
|
|
27
|
+
* has: async (key) => values.has(key),
|
|
28
|
+
* setBuffer: async (key, value) => {
|
|
29
|
+
* values.set(key, value);
|
|
30
|
+
* return 'OK';
|
|
31
|
+
* },
|
|
32
|
+
* setBufferEx: async () => {
|
|
33
|
+
* throw new Error('TTL writes are not supported by this example adapter');
|
|
34
|
+
* },
|
|
35
|
+
* ttl: async (key) => values.has(key) ? -1 : -2,
|
|
36
|
+
* };
|
|
15
37
|
*
|
|
16
|
-
* const
|
|
17
|
-
* await
|
|
18
|
-
* const
|
|
19
|
-
*
|
|
20
|
-
* const value = await storage.getItem('key'); // { foo: 'bar' }
|
|
38
|
+
* const storage = createRedisMsgpackStorage(adapter);
|
|
39
|
+
* await storage.setItem('profile', { name: 'Alice' });
|
|
40
|
+
* const profile = await storage.getItem<{ name: string }>('profile');
|
|
41
|
+
* console.log(profile); // => { name: 'Alice' }
|
|
21
42
|
* ```
|
|
22
43
|
*/
|
|
23
|
-
declare function createRedisMsgpackStorage(adapter: RedisLikeAdapter): Readonly<RedisLikeStorage>;
|
|
44
|
+
export declare function createRedisMsgpackStorage(adapter: RedisLikeAdapter): Readonly<RedisLikeStorage>;
|
|
24
45
|
//#endregion
|
|
25
|
-
export { createRedisMsgpackStorage };
|
|
26
46
|
//# sourceMappingURL=msgpack.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"msgpack.d.ts","names":[],"sources":["../../../src/storages/redis/msgpack.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"msgpack.d.ts","names":[],"sources":["../../../src/storages/redis/msgpack.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAwDgB,0BAA0B,SAAS,mBAAmB,SAAS"}
|
|
@@ -2,23 +2,44 @@ import { decode, encode, isNativeAccelerationEnabled } from "msgpackr";
|
|
|
2
2
|
//#region src/storages/redis/msgpack.ts
|
|
3
3
|
if (!isNativeAccelerationEnabled) console.warn("Native acceleration not enabled for msgpackr, verify that install finished properly");
|
|
4
4
|
/**
|
|
5
|
-
* Creates a Redis-like storage using
|
|
5
|
+
* Creates a frozen Redis-like storage facade using MessagePack serialization.
|
|
6
6
|
*
|
|
7
|
-
* @
|
|
7
|
+
* @remarks
|
|
8
|
+
* Adapter rejections and MessagePack serialization errors propagate to the caller.
|
|
9
|
+
* Reads resolve to `null` when no raw value exists. Writes resolve to `true` only when the adapter returns `OK`;
|
|
10
|
+
* removal resolves to `true` only when exactly one key is deleted.
|
|
11
|
+
* The adapter remains caller-owned; creating the facade does not open or close a connection.
|
|
8
12
|
*
|
|
9
|
-
* @
|
|
10
|
-
*
|
|
13
|
+
* @param adapter - The caller-owned adapter for raw binary operations.
|
|
14
|
+
*
|
|
15
|
+
* @returns A new frozen facade sharing the supplied adapter.
|
|
11
16
|
*
|
|
12
17
|
* @example
|
|
13
|
-
*
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
14
20
|
* import { createRedisMsgpackStorage } from '@kikiutils/shared/storages/redis/msgpack';
|
|
15
|
-
* import {
|
|
21
|
+
* import type { RedisLikeAdapter } from '@kikiutils/shared/storages/redis/types';
|
|
22
|
+
*
|
|
23
|
+
* // This in-memory adapter demonstrates non-expiring operations without an external service.
|
|
24
|
+
* const values = new Map<string, Uint8Array>();
|
|
25
|
+
* const adapter: RedisLikeAdapter = {
|
|
26
|
+
* delete: async (key) => values.delete(key) ? 1 : 0,
|
|
27
|
+
* getBuffer: async (key) => values.get(key) ?? null,
|
|
28
|
+
* has: async (key) => values.has(key),
|
|
29
|
+
* setBuffer: async (key, value) => {
|
|
30
|
+
* values.set(key, value);
|
|
31
|
+
* return 'OK';
|
|
32
|
+
* },
|
|
33
|
+
* setBufferEx: async () => {
|
|
34
|
+
* throw new Error('TTL writes are not supported by this example adapter');
|
|
35
|
+
* },
|
|
36
|
+
* ttl: async (key) => values.has(key) ? -1 : -2,
|
|
37
|
+
* };
|
|
16
38
|
*
|
|
17
|
-
* const
|
|
18
|
-
* await
|
|
19
|
-
* const
|
|
20
|
-
*
|
|
21
|
-
* const value = await storage.getItem('key'); // { foo: 'bar' }
|
|
39
|
+
* const storage = createRedisMsgpackStorage(adapter);
|
|
40
|
+
* await storage.setItem('profile', { name: 'Alice' });
|
|
41
|
+
* const profile = await storage.getItem<{ name: string }>('profile');
|
|
42
|
+
* console.log(profile); // => { name: 'Alice' }
|
|
22
43
|
* ```
|
|
23
44
|
*/
|
|
24
45
|
function createRedisMsgpackStorage(adapter) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"msgpack.js","names":[],"sources":["../../../src/storages/redis/msgpack.ts"],"sourcesContent":["import {\n decode,\n encode,\n isNativeAccelerationEnabled,\n} from 'msgpackr';\n\nimport type {\n RedisLikeAdapter,\n RedisLikeStorage,\n} from './types';\n\nif (!isNativeAccelerationEnabled) {\n console.warn('Native acceleration not enabled for msgpackr, verify that install finished properly');\n}\n\n/**\n * Creates a Redis-like storage using
|
|
1
|
+
{"version":3,"file":"msgpack.js","names":[],"sources":["../../../src/storages/redis/msgpack.ts"],"sourcesContent":["import {\n decode,\n encode,\n isNativeAccelerationEnabled,\n} from 'msgpackr';\n\nimport type {\n RedisLikeAdapter,\n RedisLikeStorage,\n} from './types';\n\nif (!isNativeAccelerationEnabled) {\n console.warn('Native acceleration not enabled for msgpackr, verify that install finished properly');\n}\n\n/**\n * Creates a frozen Redis-like storage facade using MessagePack serialization.\n *\n * @remarks\n * Adapter rejections and MessagePack serialization errors propagate to the caller.\n * Reads resolve to `null` when no raw value exists. Writes resolve to `true` only when the adapter returns `OK`;\n * removal resolves to `true` only when exactly one key is deleted.\n * The adapter remains caller-owned; creating the facade does not open or close a connection.\n *\n * @param adapter - The caller-owned adapter for raw binary operations.\n *\n * @returns A new frozen facade sharing the supplied adapter.\n *\n * @example\n *\n * ```ts\n * import { createRedisMsgpackStorage } from '@kikiutils/shared/storages/redis/msgpack';\n * import type { RedisLikeAdapter } from '@kikiutils/shared/storages/redis/types';\n *\n * // This in-memory adapter demonstrates non-expiring operations without an external service.\n * const values = new Map<string, Uint8Array>();\n * const adapter: RedisLikeAdapter = {\n * delete: async (key) => values.delete(key) ? 1 : 0,\n * getBuffer: async (key) => values.get(key) ?? null,\n * has: async (key) => values.has(key),\n * setBuffer: async (key, value) => {\n * values.set(key, value);\n * return 'OK';\n * },\n * setBufferEx: async () => {\n * throw new Error('TTL writes are not supported by this example adapter');\n * },\n * ttl: async (key) => values.has(key) ? -1 : -2,\n * };\n *\n * const storage = createRedisMsgpackStorage(adapter);\n * await storage.setItem('profile', { name: 'Alice' });\n * const profile = await storage.getItem<{ name: string }>('profile');\n * console.log(profile); // => { name: 'Alice' }\n * ```\n */\nexport function createRedisMsgpackStorage(adapter: RedisLikeAdapter): Readonly<RedisLikeStorage> {\n return Object.freeze({\n async getItem<T = unknown>(key: string) {\n const rawValue = await adapter.getBuffer(key);\n return rawValue ? decode(rawValue) as T : null;\n },\n getItemTtl: (key: string) => adapter.ttl(key),\n hasItem: (key: string) => adapter.has(key),\n removeItem: async (key: string) => await adapter.delete(key) === 1,\n setItem: async (key: string, value: any) => await adapter.setBuffer(key, encode(value)) === 'OK',\n async setItemWithTtl(key: string, ttlSeconds: number, value: any) {\n return await adapter.setBufferEx(key, ttlSeconds, encode(value)) === 'OK';\n },\n });\n}\n"],"mappings":";;AAWA,IAAI,CAAC,6BACD,QAAQ,KAAK,qFAAqF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CtG,SAAgB,0BAA0B,SAAuD;CAC7F,OAAO,OAAO,OAAO;EACjB,MAAM,QAAqB,KAAa;GACpC,MAAM,WAAW,MAAM,QAAQ,UAAU,GAAG;GAC5C,OAAO,WAAW,OAAO,QAAQ,IAAS;EAC9C;EACA,aAAa,QAAgB,QAAQ,IAAI,GAAG;EAC5C,UAAU,QAAgB,QAAQ,IAAI,GAAG;EACzC,YAAY,OAAO,QAAgB,MAAM,QAAQ,OAAO,GAAG,MAAM;EACjE,SAAS,OAAO,KAAa,UAAe,MAAM,QAAQ,UAAU,KAAK,OAAO,KAAK,CAAC,MAAM;EAC5F,MAAM,eAAe,KAAa,YAAoB,OAAY;GAC9D,OAAO,MAAM,QAAQ,YAAY,KAAK,YAAY,OAAO,KAAK,CAAC,MAAM;EACzE;CACJ,CAAC;AACL"}
|
|
@@ -1,62 +1,79 @@
|
|
|
1
1
|
//#region src/storages/redis/types.d.ts
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* A caller-owned asynchronous adapter for Redis-like binary operations.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* Each callback performs one operation and rejects its promise on backend failure.
|
|
7
|
+
* TTL results use Redis conventions: `-1` means no expiry, and `-2` means the key is missing.
|
|
4
8
|
*/
|
|
5
|
-
interface RedisLikeAdapter {
|
|
9
|
+
export interface RedisLikeAdapter {
|
|
6
10
|
/**
|
|
7
|
-
* Deletes a key and
|
|
11
|
+
* Deletes a key and resolves to the number of keys deleted.
|
|
8
12
|
*/
|
|
9
13
|
delete: (key: string) => Promise<number>;
|
|
10
14
|
/**
|
|
11
|
-
*
|
|
15
|
+
* Reads a key and resolves to its raw bytes, or `null` when the key is missing.
|
|
12
16
|
*/
|
|
13
17
|
getBuffer: (key: string) => Promise<null | Uint8Array>;
|
|
14
18
|
/**
|
|
15
|
-
* Checks
|
|
19
|
+
* Checks a key and resolves to whether it exists.
|
|
16
20
|
*/
|
|
17
21
|
has: (key: string) => Promise<boolean>;
|
|
18
22
|
/**
|
|
19
|
-
*
|
|
23
|
+
* Writes raw bytes and resolves to the backend acknowledgment, or no value.
|
|
20
24
|
*/
|
|
21
25
|
setBuffer: (key: string, value: Uint8Array) => Promise<'OK' | void>;
|
|
22
26
|
/**
|
|
23
|
-
*
|
|
27
|
+
* Writes raw bytes with an expiry and resolves to the backend acknowledgment, or no value.
|
|
28
|
+
*
|
|
29
|
+
* @param key - The storage key.
|
|
30
|
+
* @param ttlSeconds - The expiry duration in seconds.
|
|
31
|
+
* @param value - The raw bytes to store.
|
|
24
32
|
*/
|
|
25
33
|
setBufferEx: (key: string, ttlSeconds: number, value: Uint8Array) => Promise<'OK' | void>;
|
|
26
34
|
/**
|
|
27
|
-
*
|
|
35
|
+
* Reads a key's remaining TTL in seconds, or `-1` for no expiry and `-2` for a missing key.
|
|
28
36
|
*/
|
|
29
37
|
ttl: (key: string) => Promise<number>;
|
|
30
38
|
}
|
|
31
39
|
/**
|
|
32
|
-
*
|
|
40
|
+
* An asynchronous Redis-like storage contract with typed reads and serialized writes.
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
* Operations reject on backend or serialization failure. TTL values are in seconds;
|
|
44
|
+
* `-1` means no expiry, and `-2` means the key is missing. Connection cleanup remains caller-owned.
|
|
33
45
|
*/
|
|
34
|
-
interface RedisLikeStorage {
|
|
46
|
+
export interface RedisLikeStorage {
|
|
35
47
|
/**
|
|
36
|
-
*
|
|
48
|
+
* Reads a deserialized value and resolves to `null` when the key is missing.
|
|
49
|
+
*
|
|
50
|
+
* @typeParam T - The expected value type; deserialization does not validate this type.
|
|
37
51
|
*/
|
|
38
52
|
getItem: <T = unknown>(key: string) => Promise<null | T>;
|
|
39
53
|
/**
|
|
40
|
-
*
|
|
54
|
+
* Reads a key's remaining TTL in seconds, or `-1` for no expiry and `-2` for a missing key.
|
|
41
55
|
*/
|
|
42
56
|
getItemTtl: (key: string) => Promise<number>;
|
|
43
57
|
/**
|
|
44
|
-
* Checks
|
|
58
|
+
* Checks a key and resolves to whether it exists.
|
|
45
59
|
*/
|
|
46
60
|
hasItem: (key: string) => Promise<boolean>;
|
|
47
61
|
/**
|
|
48
|
-
* Removes a key.
|
|
62
|
+
* Removes a key and resolves to whether a key was deleted.
|
|
49
63
|
*/
|
|
50
64
|
removeItem: (key: string) => Promise<boolean>;
|
|
51
65
|
/**
|
|
52
|
-
*
|
|
66
|
+
* Stores a value and resolves to whether the backend acknowledges the write.
|
|
53
67
|
*/
|
|
54
68
|
setItem: (key: string, value: any) => Promise<boolean>;
|
|
55
69
|
/**
|
|
56
|
-
*
|
|
70
|
+
* Stores a value with an expiry and resolves to whether the backend acknowledges the write.
|
|
71
|
+
*
|
|
72
|
+
* @param key - The storage key.
|
|
73
|
+
* @param ttlSeconds - The expiry duration in seconds.
|
|
74
|
+
* @param value - The value to serialize and store.
|
|
57
75
|
*/
|
|
58
76
|
setItemWithTtl: (key: string, ttlSeconds: number, value: any) => Promise<boolean>;
|
|
59
77
|
}
|
|
60
78
|
//#endregion
|
|
61
|
-
export { RedisLikeAdapter, RedisLikeStorage };
|
|
62
79
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","names":[],"sources":["../../../src/storages/redis/types.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"types.d.ts","names":[],"sources":["../../../src/storages/redis/types.ts"],"mappings":";;;;;;;;iBAOiB;;;;EAIb,SAAS,gBAAgB;;;;EAKzB,YAAY,gBAAgB,eAAe;;;;EAK3C,MAAM,gBAAgB;;;;EAKtB,YAAY,aAAa,OAAO,eAAe;;;;;;;;EAS/C,cAAc,aAAa,oBAAoB,OAAO,eAAe;;;;EAKrE,MAAM,gBAAgB;;;;;;;;;iBAUT;;;;;;EAMb,UAAU,aAAa,gBAAgB,eAAe;;;;EAKtD,aAAa,gBAAgB;;;;EAK7B,UAAU,gBAAgB;;;;EAK1B,aAAa,gBAAgB;;;;EAK7B,UAAU,aAAa,eAAe;;;;;;;;EAStC,iBAAiB,aAAa,oBAAoB,eAAe"}
|
package/dist/string.d.ts
CHANGED
|
@@ -2,30 +2,31 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* Character-set presets supported by `randomString`.
|
|
4
4
|
*/
|
|
5
|
-
type RandomStringMode = 'alphabetic' | 'alphanumeric' | 'lowercase' | 'lowercase-numeric' | 'numeric' | 'uppercase' | 'uppercase-numeric';
|
|
5
|
+
export type RandomStringMode = 'alphabetic' | 'alphanumeric' | 'lowercase' | 'lowercase-numeric' | 'numeric' | 'uppercase' | 'uppercase-numeric';
|
|
6
6
|
/**
|
|
7
7
|
* Generates a random string of a given length using a predefined character set.
|
|
8
8
|
*
|
|
9
|
+
* @remarks
|
|
9
10
|
* Uses `Math.random`, so this helper is suitable for display tokens, placeholders,
|
|
10
11
|
* and test data, but not for passwords, API keys, or other cryptographic secrets.
|
|
11
12
|
*
|
|
12
|
-
* @param
|
|
13
|
-
* @param
|
|
13
|
+
* @param length - The positive integer length of the string to generate.
|
|
14
|
+
* @param mode - The character set to use (default: `'alphabetic'`).
|
|
14
15
|
*
|
|
15
|
-
* @returns
|
|
16
|
+
* @returns The generated random string.
|
|
16
17
|
*
|
|
17
|
-
* @throws
|
|
18
|
+
* @throws Error if the length is not a positive integer or the mode is unsupported.
|
|
18
19
|
*
|
|
19
20
|
* @example
|
|
20
|
-
*
|
|
21
|
+
*
|
|
22
|
+
* ```ts
|
|
21
23
|
* import { randomString } from '@kikiutils/shared/string';
|
|
22
24
|
*
|
|
23
|
-
* randomString(8); //
|
|
24
|
-
* randomString(6, 'numeric'); //
|
|
25
|
-
* randomString(10, 'alphanumeric'); //
|
|
25
|
+
* randomString(8); // For example, 'aZbXwTyQ'.
|
|
26
|
+
* randomString(6, 'numeric'); // For example, '402398'.
|
|
27
|
+
* randomString(10, 'alphanumeric'); // For example, 'a9Z4pQ8xY2'.
|
|
26
28
|
* ```
|
|
27
29
|
*/
|
|
28
|
-
declare function randomString(length: number, mode?: RandomStringMode): string;
|
|
30
|
+
export declare function randomString(length: number, mode?: RandomStringMode): string;
|
|
29
31
|
//#endregion
|
|
30
|
-
export { RandomStringMode, randomString };
|
|
31
32
|
//# sourceMappingURL=string.d.ts.map
|
package/dist/string.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.d.ts","names":[],"sources":["../src/string.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"string.d.ts","names":[],"sources":["../src/string.ts"],"mappings":";;;;YAGY;;;;;;;;;;;;;;;;;;;;;;;;;wBA8CI,aAAa,gBAAgB,OAAM"}
|
package/dist/string.js
CHANGED
|
@@ -1,33 +1,38 @@
|
|
|
1
1
|
//#region src/string.ts
|
|
2
|
+
const DIGITS = "0123456789";
|
|
3
|
+
const LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
|
|
4
|
+
const UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
2
5
|
const CHARSETS = {
|
|
3
|
-
"alphabetic":
|
|
4
|
-
"alphanumeric":
|
|
5
|
-
"lowercase":
|
|
6
|
-
"lowercase-numeric":
|
|
7
|
-
"numeric":
|
|
8
|
-
"uppercase":
|
|
9
|
-
"uppercase-numeric":
|
|
6
|
+
"alphabetic": LOWERCASE + UPPERCASE,
|
|
7
|
+
"alphanumeric": DIGITS + LOWERCASE + UPPERCASE,
|
|
8
|
+
"lowercase": LOWERCASE,
|
|
9
|
+
"lowercase-numeric": DIGITS + LOWERCASE,
|
|
10
|
+
"numeric": DIGITS,
|
|
11
|
+
"uppercase": UPPERCASE,
|
|
12
|
+
"uppercase-numeric": DIGITS + UPPERCASE
|
|
10
13
|
};
|
|
11
14
|
/**
|
|
12
15
|
* Generates a random string of a given length using a predefined character set.
|
|
13
16
|
*
|
|
17
|
+
* @remarks
|
|
14
18
|
* Uses `Math.random`, so this helper is suitable for display tokens, placeholders,
|
|
15
19
|
* and test data, but not for passwords, API keys, or other cryptographic secrets.
|
|
16
20
|
*
|
|
17
|
-
* @param
|
|
18
|
-
* @param
|
|
21
|
+
* @param length - The positive integer length of the string to generate.
|
|
22
|
+
* @param mode - The character set to use (default: `'alphabetic'`).
|
|
19
23
|
*
|
|
20
|
-
* @returns
|
|
24
|
+
* @returns The generated random string.
|
|
21
25
|
*
|
|
22
|
-
* @throws
|
|
26
|
+
* @throws Error if the length is not a positive integer or the mode is unsupported.
|
|
23
27
|
*
|
|
24
28
|
* @example
|
|
25
|
-
*
|
|
29
|
+
*
|
|
30
|
+
* ```ts
|
|
26
31
|
* import { randomString } from '@kikiutils/shared/string';
|
|
27
32
|
*
|
|
28
|
-
* randomString(8); //
|
|
29
|
-
* randomString(6, 'numeric'); //
|
|
30
|
-
* randomString(10, 'alphanumeric'); //
|
|
33
|
+
* randomString(8); // For example, 'aZbXwTyQ'.
|
|
34
|
+
* randomString(6, 'numeric'); // For example, '402398'.
|
|
35
|
+
* randomString(10, 'alphanumeric'); // For example, 'a9Z4pQ8xY2'.
|
|
31
36
|
* ```
|
|
32
37
|
*/
|
|
33
38
|
function randomString(length, mode = "alphabetic") {
|
package/dist/string.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.js","names":[],"sources":["../src/string.ts"],"sourcesContent":["/**\n * Character-set presets supported by `randomString`.\n */\nexport type RandomStringMode =\n | 'alphabetic'\n | 'alphanumeric'\n | 'lowercase'\n | 'lowercase-numeric'\n | 'numeric'\n | 'uppercase'\n | 'uppercase-numeric';\n\nconst DIGITS = '0123456789';\nconst LOWERCASE = 'abcdefghijklmnopqrstuvwxyz';\nconst UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';\nconst CHARSETS: Record<RandomStringMode, string> = {\n 'alphabetic': LOWERCASE + UPPERCASE,\n 'alphanumeric': DIGITS + LOWERCASE + UPPERCASE,\n 'lowercase': LOWERCASE,\n 'lowercase-numeric': DIGITS + LOWERCASE,\n 'numeric': DIGITS,\n 'uppercase': UPPERCASE,\n 'uppercase-numeric': DIGITS + UPPERCASE,\n};\n\n/**\n * Generates a random string of a given length using a predefined character set.\n *\n * Uses `Math.random`, so this helper is suitable for display tokens, placeholders,\n * and test data, but not for passwords, API keys, or other cryptographic secrets.\n *\n * @param
|
|
1
|
+
{"version":3,"file":"string.js","names":[],"sources":["../src/string.ts"],"sourcesContent":["/**\n * Character-set presets supported by `randomString`.\n */\nexport type RandomStringMode =\n | 'alphabetic'\n | 'alphanumeric'\n | 'lowercase'\n | 'lowercase-numeric'\n | 'numeric'\n | 'uppercase'\n | 'uppercase-numeric';\n\nconst DIGITS = '0123456789';\nconst LOWERCASE = 'abcdefghijklmnopqrstuvwxyz';\nconst UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';\nconst CHARSETS: Record<RandomStringMode, string> = {\n 'alphabetic': LOWERCASE + UPPERCASE,\n 'alphanumeric': DIGITS + LOWERCASE + UPPERCASE,\n 'lowercase': LOWERCASE,\n 'lowercase-numeric': DIGITS + LOWERCASE,\n 'numeric': DIGITS,\n 'uppercase': UPPERCASE,\n 'uppercase-numeric': DIGITS + UPPERCASE,\n};\n\n/**\n * Generates a random string of a given length using a predefined character set.\n *\n * @remarks\n * Uses `Math.random`, so this helper is suitable for display tokens, placeholders,\n * and test data, but not for passwords, API keys, or other cryptographic secrets.\n *\n * @param length - The positive integer length of the string to generate.\n * @param mode - The character set to use (default: `'alphabetic'`).\n *\n * @returns The generated random string.\n *\n * @throws Error if the length is not a positive integer or the mode is unsupported.\n *\n * @example\n *\n * ```ts\n * import { randomString } from '@kikiutils/shared/string';\n *\n * randomString(8); // For example, 'aZbXwTyQ'.\n * randomString(6, 'numeric'); // For example, '402398'.\n * randomString(10, 'alphanumeric'); // For example, 'a9Z4pQ8xY2'.\n * ```\n */\nexport function randomString(length: number, mode: RandomStringMode = 'alphabetic') {\n if (!Number.isInteger(length) || length <= 0) {\n throw new Error(`Invalid length: ${length}. Must be a positive integer`);\n }\n\n const charset = CHARSETS[mode];\n if (!charset) throw new Error(`Unsupported mode: ${mode}`);\n return Array.from({ length }, () => charset[Math.floor(Math.random() * charset.length)]).join('');\n}\n"],"mappings":";AAYA,MAAM,SAAS;AACf,MAAM,YAAY;AAClB,MAAM,YAAY;AAClB,MAAM,WAA6C;CAC/C,cAAc,YAAY;CAC1B,gBAAgB,SAAS,YAAY;CACrC,aAAa;CACb,qBAAqB,SAAS;CAC9B,WAAW;CACX,aAAa;CACb,qBAAqB,SAAS;AAClC;;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,SAAgB,aAAa,QAAgB,OAAyB,cAAc;CAChF,IAAI,CAAC,OAAO,UAAU,MAAM,KAAK,UAAU,GACvC,MAAM,IAAI,MAAM,mBAAmB,OAAO,6BAA6B;CAG3E,MAAM,UAAU,SAAS;CACzB,IAAI,CAAC,SAAS,MAAM,IAAI,MAAM,qBAAqB,MAAM;CACzD,OAAO,MAAM,KAAK,EAAE,OAAO,SAAS,QAAQ,KAAK,MAAM,KAAK,OAAO,IAAI,QAAQ,MAAM,EAAE,CAAC,CAAC,KAAK,EAAE;AACpG"}
|
package/dist/time.d.ts
CHANGED
|
@@ -1,44 +1,49 @@
|
|
|
1
1
|
//#region src/time.d.ts
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Waits until a duration elapses or cancellation occurs without rejecting on cancellation.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* The promise resolves without a value when the timer completes or the signal aborts.
|
|
7
|
+
* An already aborted signal resolves immediately; completion removes the abort listener and clears the timer.
|
|
6
8
|
*
|
|
7
|
-
* @param
|
|
8
|
-
* @param
|
|
9
|
-
*
|
|
10
|
-
* @returns {Promise<void>} Resolves when the delay completes or the signal is aborted
|
|
9
|
+
* @param ms - The delay duration in milliseconds, using native `setTimeout` timing rules.
|
|
10
|
+
* @param signal - The signal that ends the wait early.
|
|
11
11
|
*
|
|
12
12
|
* @example
|
|
13
|
-
*
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
14
15
|
* import { delay } from '@kikiutils/shared/time';
|
|
15
16
|
*
|
|
16
17
|
* const controller = new AbortController();
|
|
17
|
-
*
|
|
18
|
-
* await delay(5000, controller.signal);
|
|
18
|
+
* controller.abort();
|
|
19
|
+
* await delay(5000, controller.signal);
|
|
19
20
|
* ```
|
|
20
21
|
*/
|
|
21
|
-
declare function delay(ms: number, signal?: AbortSignal): Promise<void>;
|
|
22
|
+
export declare function delay(ms: number, signal?: AbortSignal): Promise<void>;
|
|
22
23
|
/**
|
|
23
|
-
*
|
|
24
|
+
* Waits until a duration elapses and rejects on cancellation.
|
|
24
25
|
*
|
|
25
|
-
*
|
|
26
|
+
* @remarks
|
|
27
|
+
* The promise resolves without a value when the timer completes and rejects with `signal.reason` if aborted.
|
|
28
|
+
* An already aborted signal rejects immediately. Aborting clears the timer; timer completion removes the listener.
|
|
26
29
|
*
|
|
27
|
-
* @param
|
|
28
|
-
* @param
|
|
29
|
-
*
|
|
30
|
-
* @returns {Promise<void>} Resolves when the delay completes or rejects when the signal is aborted
|
|
30
|
+
* @param ms - The delay duration in milliseconds, using native `setTimeout` timing rules.
|
|
31
|
+
* @param signal - The signal that cancels the wait.
|
|
31
32
|
*
|
|
32
33
|
* @example
|
|
33
|
-
*
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
34
36
|
* import { delayOrThrow } from '@kikiutils/shared/time';
|
|
35
37
|
*
|
|
36
38
|
* const controller = new AbortController();
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
+
* controller.abort(new Error('Canceled'));
|
|
40
|
+
* try {
|
|
41
|
+
* await delayOrThrow(5000, controller.signal);
|
|
42
|
+
* } catch (error) {
|
|
43
|
+
* console.error(error);
|
|
44
|
+
* }
|
|
39
45
|
* ```
|
|
40
46
|
*/
|
|
41
|
-
declare function delayOrThrow(ms: number, signal?: AbortSignal): Promise<void>;
|
|
47
|
+
export declare function delayOrThrow(ms: number, signal?: AbortSignal): Promise<void>;
|
|
42
48
|
//#endregion
|
|
43
|
-
export { delay, delayOrThrow };
|
|
44
49
|
//# sourceMappingURL=time.d.ts.map
|
package/dist/time.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"time.d.ts","names":[],"sources":["../src/time.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"time.d.ts","names":[],"sources":["../src/time.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;wBAoBgB,MAAM,YAAY,SAAS,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;wBA2CzC,aAAa,YAAY,SAAS,cAAc"}
|
package/dist/time.js
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
//#region src/time.ts
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Waits until a duration elapses or cancellation occurs without rejecting on cancellation.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* The promise resolves without a value when the timer completes or the signal aborts.
|
|
7
|
+
* An already aborted signal resolves immediately; completion removes the abort listener and clears the timer.
|
|
6
8
|
*
|
|
7
|
-
* @param
|
|
8
|
-
* @param
|
|
9
|
-
*
|
|
10
|
-
* @returns {Promise<void>} Resolves when the delay completes or the signal is aborted
|
|
9
|
+
* @param ms - The delay duration in milliseconds, using native `setTimeout` timing rules.
|
|
10
|
+
* @param signal - The signal that ends the wait early.
|
|
11
11
|
*
|
|
12
12
|
* @example
|
|
13
|
-
*
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
14
15
|
* import { delay } from '@kikiutils/shared/time';
|
|
15
16
|
*
|
|
16
17
|
* const controller = new AbortController();
|
|
17
|
-
*
|
|
18
|
-
* await delay(5000, controller.signal);
|
|
18
|
+
* controller.abort();
|
|
19
|
+
* await delay(5000, controller.signal);
|
|
19
20
|
* ```
|
|
20
21
|
*/
|
|
21
22
|
function delay(ms, signal) {
|
|
@@ -34,22 +35,27 @@ function delay(ms, signal) {
|
|
|
34
35
|
});
|
|
35
36
|
}
|
|
36
37
|
/**
|
|
37
|
-
*
|
|
38
|
+
* Waits until a duration elapses and rejects on cancellation.
|
|
38
39
|
*
|
|
39
|
-
*
|
|
40
|
+
* @remarks
|
|
41
|
+
* The promise resolves without a value when the timer completes and rejects with `signal.reason` if aborted.
|
|
42
|
+
* An already aborted signal rejects immediately. Aborting clears the timer; timer completion removes the listener.
|
|
40
43
|
*
|
|
41
|
-
* @param
|
|
42
|
-
* @param
|
|
43
|
-
*
|
|
44
|
-
* @returns {Promise<void>} Resolves when the delay completes or rejects when the signal is aborted
|
|
44
|
+
* @param ms - The delay duration in milliseconds, using native `setTimeout` timing rules.
|
|
45
|
+
* @param signal - The signal that cancels the wait.
|
|
45
46
|
*
|
|
46
47
|
* @example
|
|
47
|
-
*
|
|
48
|
+
*
|
|
49
|
+
* ```ts
|
|
48
50
|
* import { delayOrThrow } from '@kikiutils/shared/time';
|
|
49
51
|
*
|
|
50
52
|
* const controller = new AbortController();
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
+
* controller.abort(new Error('Canceled'));
|
|
54
|
+
* try {
|
|
55
|
+
* await delayOrThrow(5000, controller.signal);
|
|
56
|
+
* } catch (error) {
|
|
57
|
+
* console.error(error);
|
|
58
|
+
* }
|
|
53
59
|
* ```
|
|
54
60
|
*/
|
|
55
61
|
function delayOrThrow(ms, signal) {
|
package/dist/time.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"time.js","names":[],"sources":["../src/time.ts"],"sourcesContent":["/**\n *
|
|
1
|
+
{"version":3,"file":"time.js","names":[],"sources":["../src/time.ts"],"sourcesContent":["/**\n * Waits until a duration elapses or cancellation occurs without rejecting on cancellation.\n *\n * @remarks\n * The promise resolves without a value when the timer completes or the signal aborts.\n * An already aborted signal resolves immediately; completion removes the abort listener and clears the timer.\n *\n * @param ms - The delay duration in milliseconds, using native `setTimeout` timing rules.\n * @param signal - The signal that ends the wait early.\n *\n * @example\n *\n * ```ts\n * import { delay } from '@kikiutils/shared/time';\n *\n * const controller = new AbortController();\n * controller.abort();\n * await delay(5000, controller.signal);\n * ```\n */\nexport function delay(ms: number, signal?: AbortSignal): Promise<void> {\n return new Promise((resolve) => {\n if (signal?.aborted) {\n resolve();\n return;\n }\n\n const done = () => {\n // eslint-disable-next-line ts/no-use-before-define\n clearTimeout(timeout);\n signal?.removeEventListener('abort', done);\n resolve();\n };\n\n const timeout = setTimeout(done, ms);\n signal?.addEventListener('abort', done, { once: true });\n });\n}\n\n/**\n * Waits until a duration elapses and rejects on cancellation.\n *\n * @remarks\n * The promise resolves without a value when the timer completes and rejects with `signal.reason` if aborted.\n * An already aborted signal rejects immediately. Aborting clears the timer; timer completion removes the listener.\n *\n * @param ms - The delay duration in milliseconds, using native `setTimeout` timing rules.\n * @param signal - The signal that cancels the wait.\n *\n * @example\n *\n * ```ts\n * import { delayOrThrow } from '@kikiutils/shared/time';\n *\n * const controller = new AbortController();\n * controller.abort(new Error('Canceled'));\n * try {\n * await delayOrThrow(5000, controller.signal);\n * } catch (error) {\n * console.error(error);\n * }\n * ```\n */\nexport function delayOrThrow(ms: number, signal?: AbortSignal): Promise<void> {\n return new Promise((resolve, reject) => {\n if (signal?.aborted) {\n reject(signal.reason);\n return;\n }\n\n const onAbort = () => {\n // eslint-disable-next-line ts/no-use-before-define\n clearTimeout(timeout);\n reject(signal!.reason);\n };\n\n const timeout = setTimeout(\n () => {\n signal?.removeEventListener('abort', onAbort);\n resolve();\n },\n ms,\n );\n\n signal?.addEventListener('abort', onAbort, { once: true });\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAoBA,SAAgB,MAAM,IAAY,QAAqC;CACnE,OAAO,IAAI,SAAS,YAAY;EAC5B,IAAI,QAAQ,SAAS;GACjB,QAAQ;GACR;EACJ;EAEA,MAAM,aAAa;GAEf,aAAa,OAAO;GACpB,QAAQ,oBAAoB,SAAS,IAAI;GACzC,QAAQ;EACZ;EAEA,MAAM,UAAU,WAAW,MAAM,EAAE;EACnC,QAAQ,iBAAiB,SAAS,MAAM,EAAE,MAAM,KAAK,CAAC;CAC1D,CAAC;AACL;;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,SAAgB,aAAa,IAAY,QAAqC;CAC1E,OAAO,IAAI,SAAS,SAAS,WAAW;EACpC,IAAI,QAAQ,SAAS;GACjB,OAAO,OAAO,MAAM;GACpB;EACJ;EAEA,MAAM,gBAAgB;GAElB,aAAa,OAAO;GACpB,OAAO,OAAQ,MAAM;EACzB;EAEA,MAAM,UAAU,iBACN;GACF,QAAQ,oBAAoB,SAAS,OAAO;GAC5C,QAAQ;EACZ,GACA,EACJ;EAEA,QAAQ,iBAAiB,SAAS,SAAS,EAAE,MAAM,KAAK,CAAC;CAC7D,CAAC;AACL"}
|