@elgato/utils 0.0.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/LICENSE +21 -0
- package/README.md +210 -0
- package/dist/disposable.d.ts +19 -0
- package/dist/disposable.js +21 -0
- package/dist/enumerable.d.ts +141 -0
- package/dist/enumerable.js +302 -0
- package/dist/event-emitter.d.ts +124 -0
- package/dist/event-emitter.js +170 -0
- package/dist/i18n/index.d.ts +2 -0
- package/dist/i18n/index.js +2 -0
- package/dist/i18n/language.d.ts +14 -0
- package/dist/i18n/language.js +4 -0
- package/dist/i18n/provider.d.ts +42 -0
- package/dist/i18n/provider.js +76 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +10 -0
- package/dist/json.d.ts +14 -0
- package/dist/json.js +1 -0
- package/dist/lazy.d.ts +16 -0
- package/dist/lazy.js +30 -0
- package/dist/logging/console-target.d.ts +10 -0
- package/dist/logging/console-target.js +20 -0
- package/dist/logging/file-target.d.ts +59 -0
- package/dist/logging/file-target.js +101 -0
- package/dist/logging/format.d.ts +23 -0
- package/dist/logging/format.js +47 -0
- package/dist/logging/index.d.ts +6 -0
- package/dist/logging/index.js +6 -0
- package/dist/logging/level.d.ts +30 -0
- package/dist/logging/level.js +21 -0
- package/dist/logging/logger.d.ts +87 -0
- package/dist/logging/logger.js +120 -0
- package/dist/logging/target.d.ts +32 -0
- package/dist/logging/target.js +1 -0
- package/dist/objects.d.ts +19 -0
- package/dist/objects.js +33 -0
- package/dist/parsers.d.ts +12 -0
- package/dist/parsers.js +26 -0
- package/dist/promises.d.ts +24 -0
- package/dist/promises.js +13 -0
- package/dist/rpc/client.d.ts +56 -0
- package/dist/rpc/client.js +144 -0
- package/dist/rpc/gateway.d.ts +28 -0
- package/dist/rpc/gateway.js +38 -0
- package/dist/rpc/index.d.ts +9 -0
- package/dist/rpc/index.js +9 -0
- package/dist/rpc/json-rpc/error.d.ts +52 -0
- package/dist/rpc/json-rpc/error.js +34 -0
- package/dist/rpc/json-rpc/request.d.ts +28 -0
- package/dist/rpc/json-rpc/request.js +13 -0
- package/dist/rpc/json-rpc/response.d.ts +53 -0
- package/dist/rpc/json-rpc/response.js +22 -0
- package/dist/rpc/proxy.d.ts +7 -0
- package/dist/rpc/proxy.js +1 -0
- package/dist/rpc/request.d.ts +22 -0
- package/dist/rpc/request.js +1 -0
- package/dist/rpc/response.d.ts +58 -0
- package/dist/rpc/response.js +69 -0
- package/dist/rpc/server.d.ts +34 -0
- package/dist/rpc/server.js +87 -0
- package/dist/timers.d.ts +7 -0
- package/dist/timers.js +22 -0
- package/dist/types.d.ts +20 -0
- package/dist/types.js +1 -0
- package/package.json +65 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { z } from "zod/v4-mini";
|
|
2
|
+
import type { JsonObject, JsonValue } from "../../json.js";
|
|
3
|
+
/**
|
|
4
|
+
* Request object sent to a server.
|
|
5
|
+
*/
|
|
6
|
+
export type JsonRpcRequest<T extends JsonObject | JsonValue[] | undefined = JsonObject | JsonValue[] | undefined> = {
|
|
7
|
+
/**
|
|
8
|
+
* The JSON-RPC version.
|
|
9
|
+
*/
|
|
10
|
+
readonly jsonrpc: "2.0";
|
|
11
|
+
/**
|
|
12
|
+
* Name of the method to be invoked.
|
|
13
|
+
*/
|
|
14
|
+
readonly method: string;
|
|
15
|
+
/**
|
|
16
|
+
* Optional parameters to send with the RPC request.
|
|
17
|
+
*/
|
|
18
|
+
readonly params?: T;
|
|
19
|
+
/**
|
|
20
|
+
* An identifier established by the client used to identify the response; when undefined, the request
|
|
21
|
+
* is treated as a notification.
|
|
22
|
+
*/
|
|
23
|
+
readonly id?: string;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Request object sent to a server.
|
|
27
|
+
*/
|
|
28
|
+
export declare const JsonRpcRequest: z.ZodMiniType<JsonRpcRequest>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { z } from "zod/v4-mini";
|
|
2
|
+
/**
|
|
3
|
+
* Request object sent to a server.
|
|
4
|
+
*/
|
|
5
|
+
export const JsonRpcRequest = z.object({
|
|
6
|
+
jsonrpc: z.literal("2.0"),
|
|
7
|
+
method: z.string(),
|
|
8
|
+
params: z.optional(z.union([
|
|
9
|
+
z.record(z.string(), z.any()),
|
|
10
|
+
z.array(z.any()),
|
|
11
|
+
])),
|
|
12
|
+
id: z.optional(z.string()),
|
|
13
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { z } from "zod/v4-mini";
|
|
2
|
+
import type { JsonObject, JsonPrimitive, JsonValue } from "../../json.js";
|
|
3
|
+
import { JsonRpcError } from "./error.js";
|
|
4
|
+
/**
|
|
5
|
+
* Successful response object sent to a client.
|
|
6
|
+
*/
|
|
7
|
+
export type JsonRpcSuccessResponse = {
|
|
8
|
+
/**
|
|
9
|
+
* The JSON-RPC version.
|
|
10
|
+
*/
|
|
11
|
+
readonly jsonrpc: "2.0";
|
|
12
|
+
/**
|
|
13
|
+
* Result of the RPC request.
|
|
14
|
+
*/
|
|
15
|
+
readonly result: Exclude<JsonPrimitive, undefined> | JsonObject | JsonValue[];
|
|
16
|
+
/**
|
|
17
|
+
* Identifier of the RPC request.
|
|
18
|
+
*/
|
|
19
|
+
readonly id: string;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Successful response object sent to a client.
|
|
23
|
+
*/
|
|
24
|
+
export declare const JsonRpcSuccessResponse: z.ZodMiniType<JsonRpcSuccessResponse>;
|
|
25
|
+
/**
|
|
26
|
+
* Error response object sent to a client.
|
|
27
|
+
*/
|
|
28
|
+
export type JsonRpcErrorResponse = {
|
|
29
|
+
/**
|
|
30
|
+
* The JSON-RPC version.
|
|
31
|
+
*/
|
|
32
|
+
readonly jsonrpc: "2.0";
|
|
33
|
+
/**
|
|
34
|
+
* The error that occurred.
|
|
35
|
+
*/
|
|
36
|
+
readonly error: JsonRpcError;
|
|
37
|
+
/**
|
|
38
|
+
* Identifier of the RPC request; this may be undefined if the error is for a notification.
|
|
39
|
+
*/
|
|
40
|
+
readonly id?: string;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Error response object sent to a client.
|
|
44
|
+
*/
|
|
45
|
+
export declare const JsonRpcErrorResponse: z.ZodMiniType<JsonRpcErrorResponse>;
|
|
46
|
+
/**
|
|
47
|
+
* Response object sent to a client.
|
|
48
|
+
*/
|
|
49
|
+
export declare const JsonRpcResponse: z.ZodMiniUnion<readonly [z.ZodMiniType<JsonRpcSuccessResponse, unknown>, z.ZodMiniType<JsonRpcErrorResponse, unknown>]>;
|
|
50
|
+
/**
|
|
51
|
+
* Response object sent to a client.
|
|
52
|
+
*/
|
|
53
|
+
export type JsonRpcResponse = z.infer<typeof JsonRpcResponse>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { z } from "zod/v4-mini";
|
|
2
|
+
import { JsonRpcError } from "./error.js";
|
|
3
|
+
/**
|
|
4
|
+
* Successful response object sent to a client.
|
|
5
|
+
*/
|
|
6
|
+
export const JsonRpcSuccessResponse = z.strictObject({
|
|
7
|
+
jsonrpc: z.literal("2.0"),
|
|
8
|
+
result: z.any(),
|
|
9
|
+
id: z.string(),
|
|
10
|
+
});
|
|
11
|
+
/**
|
|
12
|
+
* Error response object sent to a client.
|
|
13
|
+
*/
|
|
14
|
+
export const JsonRpcErrorResponse = z.strictObject({
|
|
15
|
+
jsonrpc: z.literal("2.0"),
|
|
16
|
+
error: JsonRpcError,
|
|
17
|
+
id: z.optional(z.string()),
|
|
18
|
+
});
|
|
19
|
+
/**
|
|
20
|
+
* Response object sent to a client.
|
|
21
|
+
*/
|
|
22
|
+
export const JsonRpcResponse = z.union([JsonRpcSuccessResponse, JsonRpcErrorResponse]);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { JsonValue } from "../json.js";
|
|
2
|
+
/**
|
|
3
|
+
* Proxy capable of sending requests or responses to a client or server.
|
|
4
|
+
* @param value Request or response to send.
|
|
5
|
+
* @returns `true` when the proxy was able to send the value; otherwise `false`.
|
|
6
|
+
*/
|
|
7
|
+
export type RpcProxy = (value: JsonValue) => Promise<boolean> | boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { JsonObject, JsonValue } from "../json.js";
|
|
2
|
+
/**
|
|
3
|
+
* Request options.
|
|
4
|
+
*/
|
|
5
|
+
export type RpcRequestOptions = {
|
|
6
|
+
/**
|
|
7
|
+
* Name of the method to be invoked.
|
|
8
|
+
*/
|
|
9
|
+
method: string;
|
|
10
|
+
/**
|
|
11
|
+
* Parameters to be used during the invocation of the method.
|
|
12
|
+
*/
|
|
13
|
+
params?: RpcRequestParameters;
|
|
14
|
+
/**
|
|
15
|
+
* Timeout duration in milliseconds; defaults to `5000` (5s).
|
|
16
|
+
*/
|
|
17
|
+
timeout?: number;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Parameters that can be sent with a request.
|
|
21
|
+
*/
|
|
22
|
+
export type RpcRequestParameters = JsonObject | JsonValue[] | undefined;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { JsonObject, JsonPrimitive, JsonValue } from "../json.js";
|
|
2
|
+
import type { JsonRpcError } from "./json-rpc/error.js";
|
|
3
|
+
import type { RpcProxy } from "./proxy.js";
|
|
4
|
+
/**
|
|
5
|
+
* Response object sent from a server.
|
|
6
|
+
*/
|
|
7
|
+
export type RpcResponse<T extends RpcResponseResult = RpcResponseResult> = {
|
|
8
|
+
/**
|
|
9
|
+
* Result of the request.
|
|
10
|
+
*/
|
|
11
|
+
readonly result: T;
|
|
12
|
+
/**
|
|
13
|
+
* Determines whether the request was successful.
|
|
14
|
+
*/
|
|
15
|
+
readonly ok: true;
|
|
16
|
+
} | {
|
|
17
|
+
/**
|
|
18
|
+
* The error that occurred.
|
|
19
|
+
*/
|
|
20
|
+
readonly error: JsonRpcError;
|
|
21
|
+
/**
|
|
22
|
+
* Determines whether the request was successful.
|
|
23
|
+
*/
|
|
24
|
+
readonly ok: false;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Type of a result contained within a response.
|
|
28
|
+
*/
|
|
29
|
+
export type RpcResponseResult = Exclude<JsonPrimitive, undefined> | JsonObject | JsonValue[];
|
|
30
|
+
/**
|
|
31
|
+
* Responder responsible for responding to a request.
|
|
32
|
+
*/
|
|
33
|
+
export declare class RpcRequestResponder {
|
|
34
|
+
#private;
|
|
35
|
+
/**
|
|
36
|
+
* Initializes a new instance of the {@link RpcRequestResponder} class.
|
|
37
|
+
* @param proxy Proxy responsible for forwarding the response to the client.
|
|
38
|
+
* @param id Identifier of the request.
|
|
39
|
+
*/
|
|
40
|
+
constructor(proxy: RpcProxy, id: string | undefined);
|
|
41
|
+
/**
|
|
42
|
+
* Indicates whether a response can be sent.
|
|
43
|
+
* @returns `true` when a response has not yet been set and the request accepts responses.
|
|
44
|
+
*/
|
|
45
|
+
get canRespond(): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Sends an error response to the client.
|
|
48
|
+
* @param error The error.
|
|
49
|
+
* @returns Promise fulfilled once the response has been sent.
|
|
50
|
+
*/
|
|
51
|
+
error(error: JsonRpcError): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Sends the result to the client.
|
|
54
|
+
* @param result The result.
|
|
55
|
+
* @returns Promise fulfilled once the response has been sent.
|
|
56
|
+
*/
|
|
57
|
+
success(result: RpcResponseResult): Promise<void>;
|
|
58
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Responder responsible for responding to a request.
|
|
3
|
+
*/
|
|
4
|
+
export class RpcRequestResponder {
|
|
5
|
+
/**
|
|
6
|
+
* Identifier of the request.
|
|
7
|
+
*/
|
|
8
|
+
#id;
|
|
9
|
+
/**
|
|
10
|
+
* Proxy responsible for sending the response to the client.
|
|
11
|
+
*/
|
|
12
|
+
#proxy;
|
|
13
|
+
/**
|
|
14
|
+
* Indicates whether a response has already been sent in relation to the response.
|
|
15
|
+
*/
|
|
16
|
+
#responded = false;
|
|
17
|
+
/**
|
|
18
|
+
* Initializes a new instance of the {@link RpcRequestResponder} class.
|
|
19
|
+
* @param proxy Proxy responsible for forwarding the response to the client.
|
|
20
|
+
* @param id Identifier of the request.
|
|
21
|
+
*/
|
|
22
|
+
constructor(proxy, id) {
|
|
23
|
+
this.#proxy = proxy;
|
|
24
|
+
this.#id = id;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Indicates whether a response can be sent.
|
|
28
|
+
* @returns `true` when a response has not yet been set and the request accepts responses.
|
|
29
|
+
*/
|
|
30
|
+
get canRespond() {
|
|
31
|
+
return !this.#responded && this.#id !== undefined;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Sends an error response to the client.
|
|
35
|
+
* @param error The error.
|
|
36
|
+
* @returns Promise fulfilled once the response has been sent.
|
|
37
|
+
*/
|
|
38
|
+
error(error) {
|
|
39
|
+
return this.#send({
|
|
40
|
+
jsonrpc: "2.0",
|
|
41
|
+
id: this.#id,
|
|
42
|
+
error,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Sends the result to the client.
|
|
47
|
+
* @param result The result.
|
|
48
|
+
* @returns Promise fulfilled once the response has been sent.
|
|
49
|
+
*/
|
|
50
|
+
async success(result) {
|
|
51
|
+
if (this.#id) {
|
|
52
|
+
await this.#send({
|
|
53
|
+
jsonrpc: "2.0",
|
|
54
|
+
id: this.#id,
|
|
55
|
+
result,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Sends the response to the client.
|
|
61
|
+
* @param res The response.
|
|
62
|
+
*/
|
|
63
|
+
async #send(res) {
|
|
64
|
+
if (!this.#responded) {
|
|
65
|
+
await this.#proxy(res);
|
|
66
|
+
this.#responded = true;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type IDisposable } from "../disposable.js";
|
|
2
|
+
import type { JsonValue } from "../json.js";
|
|
3
|
+
import type { RpcProxy } from "./proxy.js";
|
|
4
|
+
import type { RpcRequestParameters } from "./request.js";
|
|
5
|
+
import { RpcRequestResponder, type RpcResponseResult } from "./response.js";
|
|
6
|
+
/**
|
|
7
|
+
* Server capable of receiving requests from, and sending responses to, a client.
|
|
8
|
+
*/
|
|
9
|
+
export declare class RpcServer {
|
|
10
|
+
#private;
|
|
11
|
+
/**
|
|
12
|
+
* Initializes a new instance of the {@link RpcServer} class.
|
|
13
|
+
* @param proxy Proxy responsible for sending responses to a client.
|
|
14
|
+
*/
|
|
15
|
+
constructor(proxy: RpcProxy);
|
|
16
|
+
/**
|
|
17
|
+
* Maps the specified method name to a method, allowing for requests from a client.
|
|
18
|
+
* @param name Name of the method.
|
|
19
|
+
* @param handler Handler to be invoked when the request is received.
|
|
20
|
+
* @returns Disposable capable of removing the handler.
|
|
21
|
+
*/
|
|
22
|
+
add<TParameters extends RpcRequestParameters>(name: string, handler: MethodHandler<TParameters>): IDisposable;
|
|
23
|
+
/**
|
|
24
|
+
* Attempts to process the specified request received from a client.
|
|
25
|
+
* @param value Value to process.
|
|
26
|
+
* @returns `true` when the server was able to process the request; otherwise `false`.
|
|
27
|
+
*/
|
|
28
|
+
receive(value: JsonValue): Promise<boolean>;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Function responsible for handling a request, and providing a result.
|
|
32
|
+
* @template TParameters Type of the parameter sent with the request.
|
|
33
|
+
*/
|
|
34
|
+
export type MethodHandler<TParameters extends RpcRequestParameters = undefined> = (parameters: TParameters, responder: RpcRequestResponder, next: () => Promise<RpcResponseResult | void> | RpcResponseResult | void) => Promise<RpcResponseResult | void> | RpcResponseResult | void;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {} from "../disposable.js";
|
|
2
|
+
import { EventEmitter } from "../event-emitter.js";
|
|
3
|
+
import { JsonRpcErrorCode } from "./json-rpc/error.js";
|
|
4
|
+
import { JsonRpcRequest } from "./json-rpc/request.js";
|
|
5
|
+
import { RpcRequestResponder } from "./response.js";
|
|
6
|
+
/**
|
|
7
|
+
* Server capable of receiving requests from, and sending responses to, a client.
|
|
8
|
+
*/
|
|
9
|
+
export class RpcServer {
|
|
10
|
+
/**
|
|
11
|
+
* Registered methods, and their respective handlers.
|
|
12
|
+
*/
|
|
13
|
+
#methods = new EventEmitter();
|
|
14
|
+
/**
|
|
15
|
+
* Proxy responsible for sending responses to a client.
|
|
16
|
+
*/
|
|
17
|
+
#proxy;
|
|
18
|
+
/**
|
|
19
|
+
* Initializes a new instance of the {@link RpcServer} class.
|
|
20
|
+
* @param proxy Proxy responsible for sending responses to a client.
|
|
21
|
+
*/
|
|
22
|
+
constructor(proxy) {
|
|
23
|
+
this.#proxy = proxy;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Maps the specified method name to a method, allowing for requests from a client.
|
|
27
|
+
* @param name Name of the method.
|
|
28
|
+
* @param handler Handler to be invoked when the request is received.
|
|
29
|
+
* @returns Disposable capable of removing the handler.
|
|
30
|
+
*/
|
|
31
|
+
add(name, handler) {
|
|
32
|
+
return this.#methods.disposableOn(name, handler);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Attempts to process the specified request received from a client.
|
|
36
|
+
* @param value Value to process.
|
|
37
|
+
* @returns `true` when the server was able to process the request; otherwise `false`.
|
|
38
|
+
*/
|
|
39
|
+
async receive(value) {
|
|
40
|
+
const { success, data: request } = JsonRpcRequest.safeParse(value);
|
|
41
|
+
if (!success) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
const responder = new RpcRequestResponder(this.#proxy, request.id);
|
|
45
|
+
const methods = this.#methods.listeners(request.method);
|
|
46
|
+
if (methods.length === 0) {
|
|
47
|
+
await responder.error({
|
|
48
|
+
code: JsonRpcErrorCode.MethodNotFound,
|
|
49
|
+
message: "The method does not exist or is not available.",
|
|
50
|
+
});
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
await this.#invoke(methods, request.params, responder);
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Invokes the specified methods with the request information and attempts to return a response.
|
|
58
|
+
* @param methods Chain of methods.
|
|
59
|
+
* @param parameters Parameters provided with the request.
|
|
60
|
+
* @param responder Request responder responsible for sending a response.
|
|
61
|
+
*/
|
|
62
|
+
async #invoke(methods, parameters, responder) {
|
|
63
|
+
// `next` function used to chain methods.
|
|
64
|
+
const next = (methods) => {
|
|
65
|
+
return () => {
|
|
66
|
+
const [curr, ...rest] = methods;
|
|
67
|
+
if (curr === undefined) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
return curr(parameters, responder, next(rest));
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
try {
|
|
74
|
+
// Execute the first method.
|
|
75
|
+
const result = await next(methods)();
|
|
76
|
+
await responder.success(result ?? null);
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
// Respond with an error before throwing.
|
|
80
|
+
await responder.error({
|
|
81
|
+
code: JsonRpcErrorCode.InternalError,
|
|
82
|
+
message: err instanceof Error ? err.message : err instanceof Object ? err.toString() : "Unknown error",
|
|
83
|
+
});
|
|
84
|
+
throw err;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
package/dist/timers.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a wrapper around the specified `fn`, debouncing calls within the `delay`, to prevent multiple calls.
|
|
3
|
+
* @param fn Function to debounce.
|
|
4
|
+
* @param delay Delay before invoking the function.
|
|
5
|
+
* @returns Function that debounces calls.
|
|
6
|
+
*/
|
|
7
|
+
export declare function debounce<T extends unknown[]>(fn: (...args: T) => Promise<void> | void, delay: number): (...args: T) => Promise<void>;
|
package/dist/timers.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { withResolvers } from "./promises.js";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a wrapper around the specified `fn`, debouncing calls within the `delay`, to prevent multiple calls.
|
|
4
|
+
* @param fn Function to debounce.
|
|
5
|
+
* @param delay Delay before invoking the function.
|
|
6
|
+
* @returns Function that debounces calls.
|
|
7
|
+
*/
|
|
8
|
+
export function debounce(fn, delay) {
|
|
9
|
+
let handle;
|
|
10
|
+
let resolvers;
|
|
11
|
+
return (...args) => {
|
|
12
|
+
clearTimeout(handle);
|
|
13
|
+
resolvers ??= withResolvers();
|
|
14
|
+
handle = setTimeout(async (args) => {
|
|
15
|
+
const capturedResolvers = resolvers;
|
|
16
|
+
resolvers = undefined;
|
|
17
|
+
await fn(...args);
|
|
18
|
+
capturedResolvers?.resolve();
|
|
19
|
+
}, delay, args);
|
|
20
|
+
return resolvers.promise;
|
|
21
|
+
};
|
|
22
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility type that asserts a string contains only alphabetical characters and is not empty.
|
|
3
|
+
* {@link https://stackoverflow.com/a/74211628/259656 Credit}
|
|
4
|
+
*/
|
|
5
|
+
export type Alphabetical<T extends string> = Az<T> extends `` ? never : T;
|
|
6
|
+
/**
|
|
7
|
+
* Utility type that asserts a string contains only alphabetical characters.
|
|
8
|
+
* {@link https://stackoverflow.com/a/74211628/259656 Credit}
|
|
9
|
+
*/
|
|
10
|
+
type Az<T extends string, A extends string = ""> = T extends `${infer F}${infer R}` ? Az<R, `${A}${Uppercase<F> extends Lowercase<F> ? never : F}`> : A;
|
|
11
|
+
/**
|
|
12
|
+
* Utility type that acts as an alternate for `type[key]`. This type provides better support for aliasing
|
|
13
|
+
* types when parsing them using the abstract syntax tree, used when generating documentation.
|
|
14
|
+
*/
|
|
15
|
+
export type KeyOf<T, K extends keyof T> = Omit<T[K], "">;
|
|
16
|
+
/**
|
|
17
|
+
* Defines a type that implements a constructor that accepts an array of `any` parameters; utilized for mixins.
|
|
18
|
+
*/
|
|
19
|
+
export type Constructor<T = object> = new (...args: any[]) => T;
|
|
20
|
+
export {};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@elgato/utils",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Utilities used throughout the Elgato ecosystem.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"files": [
|
|
7
|
+
"./dist/"
|
|
8
|
+
],
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "rm -rf dist/ && tsc --project tsconfig.build.json",
|
|
11
|
+
"lint": "eslint --max-warnings 0",
|
|
12
|
+
"preversion": "npm run build && npm test && npm run lint",
|
|
13
|
+
"test": "vitest run --typecheck",
|
|
14
|
+
"test:coverage": "vitest run --coverage",
|
|
15
|
+
"test:watch": "vitest --typecheck",
|
|
16
|
+
"type:check": "tsc"
|
|
17
|
+
},
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./i18n": {
|
|
24
|
+
"types": "./dist/i18n/index.d.ts",
|
|
25
|
+
"default": "./dist/i18n/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./logging": {
|
|
28
|
+
"types": "./dist/logging/index.d.ts",
|
|
29
|
+
"default": "./dist/logging/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./rpc": {
|
|
32
|
+
"types": "./dist/rpc/index.d.ts",
|
|
33
|
+
"default": "./dist/rpc/index.js"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"prettier": "@elgato/prettier-config",
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/elgatosf/js-utils.git"
|
|
40
|
+
},
|
|
41
|
+
"keywords": [
|
|
42
|
+
"elgato"
|
|
43
|
+
],
|
|
44
|
+
"author": {
|
|
45
|
+
"name": "Elgato",
|
|
46
|
+
"url": "https://www.elgato.com"
|
|
47
|
+
},
|
|
48
|
+
"license": "MIT",
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/elgatosf/js-utils/issues"
|
|
51
|
+
},
|
|
52
|
+
"homepage": "https://github.com/elgatosf/js-utils#readme",
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@elgato/eslint-config": "^0.1.0",
|
|
55
|
+
"@elgato/prettier-config": "^0.2.6",
|
|
56
|
+
"@tsconfig/node20": "^20.1.5",
|
|
57
|
+
"@types/node": "^22.15.19",
|
|
58
|
+
"@vitest/coverage-v8": "^3.1.4",
|
|
59
|
+
"typescript": "^5.8.3",
|
|
60
|
+
"vitest": "^3.1.3"
|
|
61
|
+
},
|
|
62
|
+
"dependencies": {
|
|
63
|
+
"zod": "^3.25.24"
|
|
64
|
+
}
|
|
65
|
+
}
|