@kikiutils/shared 13.0.0 → 13.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 +1 -1
- package/dist/env.js.map +1 -1
- package/dist/event-awaiter.d.ts +44 -0
- package/dist/event-awaiter.d.ts.map +1 -0
- package/dist/event-awaiter.js +66 -0
- package/dist/event-awaiter.js.map +1 -0
- package/package.json +7 -6
- package/src/env.ts +1 -1
- package/src/event-awaiter.ts +68 -0
package/README.md
CHANGED
package/dist/env.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"env.js","names":[],"sources":["../src/env.ts"],"sourcesContent":["/**\n * Custom error class for handling missing environment variables.\n *\n * Extends the built-in `Error` class and includes the missing key.\n *\n * @extends {Error}\n */\nexport class EnvironmentNotFoundError extends Error {\n readonly key: string;\n\n /**\n * Creates a new EnvironmentNotFoundError.\n *\n * @param {string} key - The missing environment variable key\n */\n constructor(key: string) {\n super(`Missing environment variable: ${key}`);\n this.key = key;\n this.name = this.constructor.name;\n Error.captureStackTrace?.(this, this.constructor);\n }\n}\n\n/**\n * Retrieves the value of an environment variable, or throws an error if it is not defined.\n *\n * Only checks for `process.env[key] === undefined`. An empty string (e.g. '') or any falsy string\n * value like `'0'` or `'false'` is considered a valid (defined) value.\n *\n * @param {string} key - The environment variable key to retrieve\n *\n * @returns {string} The value of the environment variable\n *\n * @throws {EnvironmentNotFoundError} If the environment variable is not defined\n *\n * @example\n * ```typescript\n * process.env.API_KEY = '';\n * checkAndGetEnvValue('API_KEY'); // ✅ Returns '' (still considered \"defined\")\n *\n * delete process.env.API_KEY;\n * checkAndGetEnvValue('API_KEY'); // ❌ Throws EnvironmentNotFoundError\n * ```\n */\nexport function checkAndGetEnvValue(key: string)
|
|
1
|
+
{"version":3,"file":"env.js","names":[],"sources":["../src/env.ts"],"sourcesContent":["/**\n * Custom error class for handling missing environment variables.\n *\n * Extends the built-in `Error` class and includes the missing key.\n *\n * @extends {Error}\n */\nexport class EnvironmentNotFoundError extends Error {\n readonly key: string;\n\n /**\n * Creates a new EnvironmentNotFoundError.\n *\n * @param {string} key - The missing environment variable key\n */\n constructor(key: string) {\n super(`Missing environment variable: ${key}`);\n this.key = key;\n this.name = this.constructor.name;\n Error.captureStackTrace?.(this, this.constructor);\n }\n}\n\n/**\n * Retrieves the value of an environment variable, or throws an error if it is not defined.\n *\n * Only checks for `process.env[key] === undefined`. An empty string (e.g. '') or any falsy string\n * value like `'0'` or `'false'` is considered a valid (defined) value.\n *\n * @param {string} key - The environment variable key to retrieve\n *\n * @returns {string} The value of the environment variable\n *\n * @throws {EnvironmentNotFoundError} If the environment variable is not defined\n *\n * @example\n * ```typescript\n * process.env.API_KEY = '';\n * checkAndGetEnvValue('API_KEY'); // ✅ Returns '' (still considered \"defined\")\n *\n * delete process.env.API_KEY;\n * checkAndGetEnvValue('API_KEY'); // ❌ Throws EnvironmentNotFoundError\n * ```\n */\nexport function checkAndGetEnvValue(key: string) {\n if (process.env[key] === undefined) throw new EnvironmentNotFoundError(key);\n return process.env[key];\n}\n"],"mappings":";;;;;;;;AAOA,IAAa,2BAAb,cAA8C,MAAM;CAChD,AAAS;;;;;;CAOT,YAAY,KAAa;AACrB,QAAM,iCAAiC,MAAM;AAC7C,OAAK,MAAM;AACX,OAAK,OAAO,KAAK,YAAY;AAC7B,QAAM,oBAAoB,MAAM,KAAK,YAAY;;;;;;;;;;;;;;;;;;;;;;;;AAyBzD,SAAgB,oBAAoB,KAAa;AAC7C,KAAI,QAAQ,IAAI,SAAS,OAAW,OAAM,IAAI,yBAAyB,IAAI;AAC3E,QAAO,QAAQ,IAAI"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
//#region src/event-awaiter.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* EventAwaiter provides a mechanism to wait for events (by key) asynchronously.
|
|
4
|
+
*
|
|
5
|
+
* This class allows multiple consumers to `wait` for a specific key
|
|
6
|
+
* and be resolved when `trigger` is called for that key, or automatically
|
|
7
|
+
* resolved with `undefined` if a timeout occurs.
|
|
8
|
+
*
|
|
9
|
+
* Typical use cases include long-polling, request coordination,
|
|
10
|
+
* or implementing event-driven primitives in applications or services.
|
|
11
|
+
*
|
|
12
|
+
* @template T - The type of value that will be resolved when the event is triggered
|
|
13
|
+
*/
|
|
14
|
+
declare class EventAwaiter<T> {
|
|
15
|
+
#private;
|
|
16
|
+
/**
|
|
17
|
+
* Triggers all pending promises waiting for the given key.
|
|
18
|
+
* Each promise will be resolved with the provided value.
|
|
19
|
+
*
|
|
20
|
+
* @param {string} [key] - Identifier for the awaited event
|
|
21
|
+
* @param {T} [value] - Optional value to resolve the awaiting promises with
|
|
22
|
+
*/
|
|
23
|
+
trigger(key: string, value?: T): void;
|
|
24
|
+
/**
|
|
25
|
+
* Waits for an event associated with the given key.
|
|
26
|
+
*
|
|
27
|
+
* The returned promise will resolve when:
|
|
28
|
+
* - `trigger(key)` is called, in which case it resolves with the provided value.
|
|
29
|
+
* - The optional timeout is reached, in which case it resolves with `undefined`.
|
|
30
|
+
*
|
|
31
|
+
* Multiple consumers can wait on the same key; all of them will be notified when triggered.
|
|
32
|
+
*
|
|
33
|
+
* @param {string} [key] - Identifier for the awaited event
|
|
34
|
+
* @param {number} [timeoutMs] - Optional timeout (in milliseconds).
|
|
35
|
+
* If reached, the promise resolves with `undefined`
|
|
36
|
+
*
|
|
37
|
+
* @returns {Promise<T | undefined>} A promise that resolves with the triggered value
|
|
38
|
+
* or `undefined` if timeout occurs
|
|
39
|
+
*/
|
|
40
|
+
wait(key: string, timeoutMs?: number): Promise<T | undefined>;
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
export { EventAwaiter };
|
|
44
|
+
//# sourceMappingURL=event-awaiter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event-awaiter.d.ts","names":[],"sources":["../src/event-awaiter.ts"],"sourcesContent":[],"mappings":";;AAYA;;;;;;;;;;;cAAa;;;;;;;;;+BAUoB;;;;;;;;;;;;;;;;;yCAwBO,QAAA"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
//#region src/event-awaiter.ts
|
|
2
|
+
/**
|
|
3
|
+
* EventAwaiter provides a mechanism to wait for events (by key) asynchronously.
|
|
4
|
+
*
|
|
5
|
+
* This class allows multiple consumers to `wait` for a specific key
|
|
6
|
+
* and be resolved when `trigger` is called for that key, or automatically
|
|
7
|
+
* resolved with `undefined` if a timeout occurs.
|
|
8
|
+
*
|
|
9
|
+
* Typical use cases include long-polling, request coordination,
|
|
10
|
+
* or implementing event-driven primitives in applications or services.
|
|
11
|
+
*
|
|
12
|
+
* @template T - The type of value that will be resolved when the event is triggered
|
|
13
|
+
*/
|
|
14
|
+
var EventAwaiter = class {
|
|
15
|
+
#promiseResolvers = /* @__PURE__ */ new Map();
|
|
16
|
+
/**
|
|
17
|
+
* Triggers all pending promises waiting for the given key.
|
|
18
|
+
* Each promise will be resolved with the provided value.
|
|
19
|
+
*
|
|
20
|
+
* @param {string} [key] - Identifier for the awaited event
|
|
21
|
+
* @param {T} [value] - Optional value to resolve the awaiting promises with
|
|
22
|
+
*/
|
|
23
|
+
trigger(key, value) {
|
|
24
|
+
const resolvers = this.#promiseResolvers.get(key);
|
|
25
|
+
if (resolvers) {
|
|
26
|
+
this.#promiseResolvers.delete(key);
|
|
27
|
+
resolvers?.forEach((resolve) => resolve(value));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Waits for an event associated with the given key.
|
|
32
|
+
*
|
|
33
|
+
* The returned promise will resolve when:
|
|
34
|
+
* - `trigger(key)` is called, in which case it resolves with the provided value.
|
|
35
|
+
* - The optional timeout is reached, in which case it resolves with `undefined`.
|
|
36
|
+
*
|
|
37
|
+
* Multiple consumers can wait on the same key; all of them will be notified when triggered.
|
|
38
|
+
*
|
|
39
|
+
* @param {string} [key] - Identifier for the awaited event
|
|
40
|
+
* @param {number} [timeoutMs] - Optional timeout (in milliseconds).
|
|
41
|
+
* If reached, the promise resolves with `undefined`
|
|
42
|
+
*
|
|
43
|
+
* @returns {Promise<T | undefined>} A promise that resolves with the triggered value
|
|
44
|
+
* or `undefined` if timeout occurs
|
|
45
|
+
*/
|
|
46
|
+
wait(key, timeoutMs) {
|
|
47
|
+
return new Promise((resolve) => {
|
|
48
|
+
const resolvers = this.#promiseResolvers.get(key) || [];
|
|
49
|
+
resolvers.push(resolve);
|
|
50
|
+
this.#promiseResolvers.set(key, resolvers);
|
|
51
|
+
if (timeoutMs) setTimeout(() => {
|
|
52
|
+
const resolvers$1 = this.#promiseResolvers.get(key);
|
|
53
|
+
if (resolvers$1?.includes(resolve)) {
|
|
54
|
+
resolve(void 0);
|
|
55
|
+
const newResolvers = resolvers$1.filter((r) => r !== resolve);
|
|
56
|
+
if (newResolvers.length) this.#promiseResolvers.set(key, newResolvers);
|
|
57
|
+
else this.#promiseResolvers.delete(key);
|
|
58
|
+
}
|
|
59
|
+
}, timeoutMs);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
//#endregion
|
|
65
|
+
export { EventAwaiter };
|
|
66
|
+
//# sourceMappingURL=event-awaiter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event-awaiter.js","names":["#promiseResolvers","resolvers"],"sources":["../src/event-awaiter.ts"],"sourcesContent":["/**\n * EventAwaiter provides a mechanism to wait for events (by key) asynchronously.\n *\n * This class allows multiple consumers to `wait` for a specific key\n * and be resolved when `trigger` is called for that key, or automatically\n * resolved with `undefined` if a timeout occurs.\n *\n * Typical use cases include long-polling, request coordination,\n * or implementing event-driven primitives in applications or services.\n *\n * @template T - The type of value that will be resolved when the event is triggered\n */\nexport class EventAwaiter<T> {\n #promiseResolvers = new Map<string, ((value: PromiseLike<T | undefined> | T | undefined) => void)[]>();\n\n /**\n * Triggers all pending promises waiting for the given key.\n * Each promise will be resolved with the provided value.\n *\n * @param {string} [key] - Identifier for the awaited event\n * @param {T} [value] - Optional value to resolve the awaiting promises with\n */\n trigger(key: string, value?: T) {\n const resolvers = this.#promiseResolvers.get(key);\n if (resolvers) {\n this.#promiseResolvers.delete(key);\n resolvers?.forEach((resolve) => resolve(value));\n }\n }\n\n /**\n * Waits for an event associated with the given key.\n *\n * The returned promise will resolve when:\n * - `trigger(key)` is called, in which case it resolves with the provided value.\n * - The optional timeout is reached, in which case it resolves with `undefined`.\n *\n * Multiple consumers can wait on the same key; all of them will be notified when triggered.\n *\n * @param {string} [key] - Identifier for the awaited event\n * @param {number} [timeoutMs] - Optional timeout (in milliseconds).\n * If reached, the promise resolves with `undefined`\n *\n * @returns {Promise<T | undefined>} A promise that resolves with the triggered value\n * or `undefined` if timeout occurs\n */\n wait(key: string, timeoutMs?: number) {\n return new Promise<T | undefined>((resolve) => {\n const resolvers = this.#promiseResolvers.get(key) || [];\n resolvers.push(resolve);\n this.#promiseResolvers.set(key, resolvers);\n if (timeoutMs) {\n setTimeout(\n () => {\n const resolvers = this.#promiseResolvers.get(key);\n if (resolvers?.includes(resolve)) {\n resolve(undefined);\n const newResolvers = resolvers.filter((r) => r !== resolve);\n if (newResolvers.length) this.#promiseResolvers.set(key, newResolvers);\n else this.#promiseResolvers.delete(key);\n }\n },\n timeoutMs,\n );\n }\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAYA,IAAa,eAAb,MAA6B;CACzB,oCAAoB,IAAI,KAA8E;;;;;;;;CAStG,QAAQ,KAAa,OAAW;EAC5B,MAAM,YAAY,MAAKA,iBAAkB,IAAI,IAAI;AACjD,MAAI,WAAW;AACX,SAAKA,iBAAkB,OAAO,IAAI;AAClC,cAAW,SAAS,YAAY,QAAQ,MAAM,CAAC;;;;;;;;;;;;;;;;;;;CAoBvD,KAAK,KAAa,WAAoB;AAClC,SAAO,IAAI,SAAwB,YAAY;GAC3C,MAAM,YAAY,MAAKA,iBAAkB,IAAI,IAAI,IAAI,EAAE;AACvD,aAAU,KAAK,QAAQ;AACvB,SAAKA,iBAAkB,IAAI,KAAK,UAAU;AAC1C,OAAI,UACA,kBACU;IACF,MAAMC,cAAY,MAAKD,iBAAkB,IAAI,IAAI;AACjD,QAAIC,aAAW,SAAS,QAAQ,EAAE;AAC9B,aAAQ,OAAU;KAClB,MAAM,eAAeA,YAAU,QAAQ,MAAM,MAAM,QAAQ;AAC3D,SAAI,aAAa,OAAQ,OAAKD,iBAAkB,IAAI,KAAK,aAAa;SACjE,OAAKA,iBAAkB,OAAO,IAAI;;MAG/C,UACH;IAEP"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/shared",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "13.
|
|
4
|
+
"version": "13.1.0",
|
|
5
5
|
"description": "A lightweight and modular utility library for modern JavaScript and TypeScript — includes secure hashing, flexible logging, datetime tools, Vue/web helpers, storage abstraction, and more.",
|
|
6
6
|
"author": "kiki-kanri",
|
|
7
7
|
"license": "MIT",
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"./element-plus": "./dist/element-plus.js",
|
|
42
42
|
"./enum": "./dist/enum.js",
|
|
43
43
|
"./env": "./dist/env.js",
|
|
44
|
+
"./event-awaiter": "./dist/event-awaiter.js",
|
|
44
45
|
"./general": "./dist/general.js",
|
|
45
46
|
"./hash": "./dist/hash.js",
|
|
46
47
|
"./math": "./dist/math.js",
|
|
@@ -66,7 +67,7 @@
|
|
|
66
67
|
"./src"
|
|
67
68
|
],
|
|
68
69
|
"engines": {
|
|
69
|
-
"node": ">=22.
|
|
70
|
+
"node": ">=22.12.0"
|
|
70
71
|
},
|
|
71
72
|
"scripts": {
|
|
72
73
|
"build": "tsdown",
|
|
@@ -78,7 +79,7 @@
|
|
|
78
79
|
"release": "pnpm run lint && pnpm run typecheck && pnpm run test && pnpm run build && changelogen --hideAuthorEmail --push --release && npm publish",
|
|
79
80
|
"test": "cross-env TZ=UTC vitest run --coverage",
|
|
80
81
|
"test:watch": "cross-env TZ=UTC vitest watch --coverage",
|
|
81
|
-
"typecheck": "tsc --noEmit",
|
|
82
|
+
"typecheck": "tsc -b --noEmit",
|
|
82
83
|
"unused-exports": "ts-unused-exports ./tsconfig.json"
|
|
83
84
|
},
|
|
84
85
|
"peerDependencies": {
|
|
@@ -143,10 +144,10 @@
|
|
|
143
144
|
},
|
|
144
145
|
"devDependencies": {
|
|
145
146
|
"@kikiutils/changelogen": "^0.9.0",
|
|
146
|
-
"@kikiutils/eslint-config": "^
|
|
147
|
-
"@kikiutils/tsconfigs": "^5.0.
|
|
147
|
+
"@kikiutils/eslint-config": "^3.0.1",
|
|
148
|
+
"@kikiutils/tsconfigs": "^5.0.5",
|
|
148
149
|
"@noble/hashes": "^2.0.0",
|
|
149
|
-
"@types/node": "^24.
|
|
150
|
+
"@types/node": "^24.5.0",
|
|
150
151
|
"@vitest/coverage-v8": "^3.2.4",
|
|
151
152
|
"async-validator": "^4.2.5",
|
|
152
153
|
"consola": "^3.4.2",
|
package/src/env.ts
CHANGED
|
@@ -42,7 +42,7 @@ export class EnvironmentNotFoundError extends Error {
|
|
|
42
42
|
* checkAndGetEnvValue('API_KEY'); // ❌ Throws EnvironmentNotFoundError
|
|
43
43
|
* ```
|
|
44
44
|
*/
|
|
45
|
-
export function checkAndGetEnvValue(key: string)
|
|
45
|
+
export function checkAndGetEnvValue(key: string) {
|
|
46
46
|
if (process.env[key] === undefined) throw new EnvironmentNotFoundError(key);
|
|
47
47
|
return process.env[key];
|
|
48
48
|
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EventAwaiter provides a mechanism to wait for events (by key) asynchronously.
|
|
3
|
+
*
|
|
4
|
+
* This class allows multiple consumers to `wait` for a specific key
|
|
5
|
+
* and be resolved when `trigger` is called for that key, or automatically
|
|
6
|
+
* resolved with `undefined` if a timeout occurs.
|
|
7
|
+
*
|
|
8
|
+
* Typical use cases include long-polling, request coordination,
|
|
9
|
+
* or implementing event-driven primitives in applications or services.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type of value that will be resolved when the event is triggered
|
|
12
|
+
*/
|
|
13
|
+
export class EventAwaiter<T> {
|
|
14
|
+
#promiseResolvers = new Map<string, ((value: PromiseLike<T | undefined> | T | undefined) => void)[]>();
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Triggers all pending promises waiting for the given key.
|
|
18
|
+
* Each promise will be resolved with the provided value.
|
|
19
|
+
*
|
|
20
|
+
* @param {string} [key] - Identifier for the awaited event
|
|
21
|
+
* @param {T} [value] - Optional value to resolve the awaiting promises with
|
|
22
|
+
*/
|
|
23
|
+
trigger(key: string, value?: T) {
|
|
24
|
+
const resolvers = this.#promiseResolvers.get(key);
|
|
25
|
+
if (resolvers) {
|
|
26
|
+
this.#promiseResolvers.delete(key);
|
|
27
|
+
resolvers?.forEach((resolve) => resolve(value));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Waits for an event associated with the given key.
|
|
33
|
+
*
|
|
34
|
+
* The returned promise will resolve when:
|
|
35
|
+
* - `trigger(key)` is called, in which case it resolves with the provided value.
|
|
36
|
+
* - The optional timeout is reached, in which case it resolves with `undefined`.
|
|
37
|
+
*
|
|
38
|
+
* Multiple consumers can wait on the same key; all of them will be notified when triggered.
|
|
39
|
+
*
|
|
40
|
+
* @param {string} [key] - Identifier for the awaited event
|
|
41
|
+
* @param {number} [timeoutMs] - Optional timeout (in milliseconds).
|
|
42
|
+
* If reached, the promise resolves with `undefined`
|
|
43
|
+
*
|
|
44
|
+
* @returns {Promise<T | undefined>} A promise that resolves with the triggered value
|
|
45
|
+
* or `undefined` if timeout occurs
|
|
46
|
+
*/
|
|
47
|
+
wait(key: string, timeoutMs?: number) {
|
|
48
|
+
return new Promise<T | undefined>((resolve) => {
|
|
49
|
+
const resolvers = this.#promiseResolvers.get(key) || [];
|
|
50
|
+
resolvers.push(resolve);
|
|
51
|
+
this.#promiseResolvers.set(key, resolvers);
|
|
52
|
+
if (timeoutMs) {
|
|
53
|
+
setTimeout(
|
|
54
|
+
() => {
|
|
55
|
+
const resolvers = this.#promiseResolvers.get(key);
|
|
56
|
+
if (resolvers?.includes(resolve)) {
|
|
57
|
+
resolve(undefined);
|
|
58
|
+
const newResolvers = resolvers.filter((r) => r !== resolve);
|
|
59
|
+
if (newResolvers.length) this.#promiseResolvers.set(key, newResolvers);
|
|
60
|
+
else this.#promiseResolvers.delete(key);
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
timeoutMs,
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|