@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,120 @@
|
|
|
1
|
+
import { defcon } from "./level.js";
|
|
2
|
+
/**
|
|
3
|
+
* Logger capable of forwarding messages to a {@link LogTarget}.
|
|
4
|
+
*/
|
|
5
|
+
export class Logger {
|
|
6
|
+
/**
|
|
7
|
+
* Backing field for the {@link Logger.level}.
|
|
8
|
+
*/
|
|
9
|
+
#level;
|
|
10
|
+
/**
|
|
11
|
+
* Options that define the loggers behavior.
|
|
12
|
+
*/
|
|
13
|
+
#options;
|
|
14
|
+
/**
|
|
15
|
+
* Scope associated with this {@link Logger}.
|
|
16
|
+
*/
|
|
17
|
+
#scope;
|
|
18
|
+
/**
|
|
19
|
+
* Initializes a new instance of the {@link Logger} class.
|
|
20
|
+
* @param opts Options that define the loggers behavior.
|
|
21
|
+
*/
|
|
22
|
+
constructor(opts) {
|
|
23
|
+
this.#options = { minimumLevel: "trace", ...opts };
|
|
24
|
+
this.#scope = this.#options.scope === undefined || this.#options.scope.trim() === "" ? "" : this.#options.scope;
|
|
25
|
+
if (typeof this.#options.level !== "function") {
|
|
26
|
+
this.setLevel(this.#options.level);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Gets the {@link LogLevel}.
|
|
31
|
+
* @returns The {@link LogLevel}.
|
|
32
|
+
*/
|
|
33
|
+
get level() {
|
|
34
|
+
if (this.#level !== undefined) {
|
|
35
|
+
return this.#level;
|
|
36
|
+
}
|
|
37
|
+
return typeof this.#options.level === "function" ? this.#options.level() : this.#options.level;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Creates a scoped logger with the given {@link scope}; logs created by scoped-loggers include their scope to enable their source to be easily identified.
|
|
41
|
+
* @param scope Value that represents the scope of the new logger.
|
|
42
|
+
* @returns The scoped logger, or this instance when {@link scope} is not defined.
|
|
43
|
+
*/
|
|
44
|
+
createScope(scope) {
|
|
45
|
+
scope = scope.trim();
|
|
46
|
+
if (scope === "") {
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
return new Logger({
|
|
50
|
+
...this.#options,
|
|
51
|
+
level: () => this.level,
|
|
52
|
+
scope: this.#options.scope ? `${this.#options.scope}->${scope}` : scope,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Writes the arguments as a debug log entry.
|
|
57
|
+
* @param data Message or data to log.
|
|
58
|
+
* @returns This instance for chaining.
|
|
59
|
+
*/
|
|
60
|
+
debug(...data) {
|
|
61
|
+
return this.write({ level: "debug", data, scope: this.#scope });
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Writes the arguments as error log entry.
|
|
65
|
+
* @param data Message or data to log.
|
|
66
|
+
* @returns This instance for chaining.
|
|
67
|
+
*/
|
|
68
|
+
error(...data) {
|
|
69
|
+
return this.write({ level: "error", data, scope: this.#scope });
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Writes the arguments as an info log entry.
|
|
73
|
+
* @param data Message or data to log.
|
|
74
|
+
* @returns This instance for chaining.
|
|
75
|
+
*/
|
|
76
|
+
info(...data) {
|
|
77
|
+
return this.write({ level: "info", data, scope: this.#scope });
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Sets the log-level that determines which logs should be written. The specified level will be inherited by all scoped loggers unless they have log-level explicitly defined.
|
|
81
|
+
* @param level The log-level that determines which logs should be written; when `undefined`, the level will be inherited from the parent logger, or default to the environment level.
|
|
82
|
+
* @returns This instance for chaining.
|
|
83
|
+
*/
|
|
84
|
+
setLevel(level) {
|
|
85
|
+
if (level !== undefined && defcon(level) > defcon(this.#options.minimumLevel)) {
|
|
86
|
+
this.#level = "info";
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
this.#level = level;
|
|
90
|
+
}
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Writes the arguments as a trace log entry.
|
|
95
|
+
* @param data Message or data to log.
|
|
96
|
+
* @returns This instance for chaining.
|
|
97
|
+
*/
|
|
98
|
+
trace(...data) {
|
|
99
|
+
return this.write({ level: "trace", data, scope: this.#scope });
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Writes the arguments as a warning log entry.
|
|
103
|
+
* @param data Message or data to log.
|
|
104
|
+
* @returns This instance for chaining.
|
|
105
|
+
*/
|
|
106
|
+
warn(...data) {
|
|
107
|
+
return this.write({ level: "warn", data, scope: this.#scope });
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Writes the log entry.
|
|
111
|
+
* @param entry Log entry to write.
|
|
112
|
+
* @returns This instance for chaining.
|
|
113
|
+
*/
|
|
114
|
+
write(entry) {
|
|
115
|
+
if (defcon(entry.level) <= defcon(this.level)) {
|
|
116
|
+
this.#options.targets.forEach((t) => t.write(entry));
|
|
117
|
+
}
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { LogLevel } from "./level.js";
|
|
2
|
+
/**
|
|
3
|
+
* A log target capable of capturing a log entry, e.g. to the file system, console, etc.
|
|
4
|
+
*/
|
|
5
|
+
export type LogTarget = {
|
|
6
|
+
/**
|
|
7
|
+
* Writes the specified log entry to the target.
|
|
8
|
+
* @param entry The log entry to write.
|
|
9
|
+
*/
|
|
10
|
+
write(entry: LogEntry): void;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Defines a log entry to be logged to a {@link LogTarget}.
|
|
14
|
+
*/
|
|
15
|
+
export type LogEntry = {
|
|
16
|
+
/**
|
|
17
|
+
* Data to log.
|
|
18
|
+
*/
|
|
19
|
+
data: unknown[] | [string, ...unknown[]];
|
|
20
|
+
/**
|
|
21
|
+
* Log level that indicates the severity of the log being written.
|
|
22
|
+
*/
|
|
23
|
+
level: LogLevel;
|
|
24
|
+
/**
|
|
25
|
+
* Scope of the log entry.
|
|
26
|
+
*/
|
|
27
|
+
scope: string;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Data to log.
|
|
31
|
+
*/
|
|
32
|
+
export type LogEntryData = unknown[] | [string, ...unknown[]];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prevents the modification of existing property attributes and values on the value, and all of its child properties, and prevents the addition of new properties.
|
|
3
|
+
* @param value Value to freeze.
|
|
4
|
+
*/
|
|
5
|
+
export declare function freeze<T>(value: T): void;
|
|
6
|
+
/**
|
|
7
|
+
* Gets the value at the specified {@link path}.
|
|
8
|
+
* @param source Source object that is being read from.
|
|
9
|
+
* @param path Path to the property to get.
|
|
10
|
+
* @returns Value of the property.
|
|
11
|
+
*/
|
|
12
|
+
export declare function get(source: unknown, path: string): unknown;
|
|
13
|
+
/**
|
|
14
|
+
* Sets the specified `value` on the `target` object at the desired property `path`.
|
|
15
|
+
* @param target The target object that is being written to.
|
|
16
|
+
* @param path The path to the property to set.
|
|
17
|
+
* @param value The value to write.
|
|
18
|
+
*/
|
|
19
|
+
export declare function set(target: any, path: string, value: unknown): void;
|
package/dist/objects.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prevents the modification of existing property attributes and values on the value, and all of its child properties, and prevents the addition of new properties.
|
|
3
|
+
* @param value Value to freeze.
|
|
4
|
+
*/
|
|
5
|
+
export function freeze(value) {
|
|
6
|
+
if (value !== undefined && value !== null && typeof value === "object" && !Object.isFrozen(value)) {
|
|
7
|
+
Object.freeze(value);
|
|
8
|
+
Object.values(value).forEach(freeze);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Gets the value at the specified {@link path}.
|
|
13
|
+
* @param source Source object that is being read from.
|
|
14
|
+
* @param path Path to the property to get.
|
|
15
|
+
* @returns Value of the property.
|
|
16
|
+
*/
|
|
17
|
+
export function get(source, path) {
|
|
18
|
+
const props = path.split(".");
|
|
19
|
+
return props.reduce((obj, prop) => obj && obj[prop], source);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Sets the specified `value` on the `target` object at the desired property `path`.
|
|
23
|
+
* @param target The target object that is being written to.
|
|
24
|
+
* @param path The path to the property to set.
|
|
25
|
+
* @param value The value to write.
|
|
26
|
+
*/
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
28
|
+
export function set(target, path, value) {
|
|
29
|
+
const props = path.split(".");
|
|
30
|
+
props.reduce((obj, prop, i) => {
|
|
31
|
+
return i === props.length - 1 ? (obj[prop] = value) : obj[prop] || (obj[prop] = {});
|
|
32
|
+
}, target);
|
|
33
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses the specified value to a truthy boolean (using {@link https://stackoverflow.com/questions/784929/what-does-the-operator-do-in-javascript `!!` notation}).
|
|
3
|
+
* @param value Value to parse.
|
|
4
|
+
* @returns `true` when the value is truthy; otherwise `false`.
|
|
5
|
+
*/
|
|
6
|
+
export declare function parseBoolean(value: unknown): boolean | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* Parses the specified value to a number (using {@link parseFloat}).
|
|
9
|
+
* @param value Value to parse.
|
|
10
|
+
* @returns The parsed value; otherwise `undefined`.
|
|
11
|
+
*/
|
|
12
|
+
export declare function parseNumber(value: unknown): number | undefined;
|
package/dist/parsers.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses the specified value to a truthy boolean (using {@link https://stackoverflow.com/questions/784929/what-does-the-operator-do-in-javascript `!!` notation}).
|
|
3
|
+
* @param value Value to parse.
|
|
4
|
+
* @returns `true` when the value is truthy; otherwise `false`.
|
|
5
|
+
*/
|
|
6
|
+
export function parseBoolean(value) {
|
|
7
|
+
if (typeof value === "string" && (value === "0" || value === "false")) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
return !!value;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Parses the specified value to a number (using {@link parseFloat}).
|
|
14
|
+
* @param value Value to parse.
|
|
15
|
+
* @returns The parsed value; otherwise `undefined`.
|
|
16
|
+
*/
|
|
17
|
+
export function parseNumber(value) {
|
|
18
|
+
if (typeof value === "number") {
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
if (typeof value !== "string") {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
value = parseFloat(value);
|
|
25
|
+
return typeof value === "number" && !isNaN(value) ? value : undefined;
|
|
26
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An object that contains a promise, and its resolve and reject functions.
|
|
3
|
+
*/
|
|
4
|
+
export type PromiseResolvers<T> = {
|
|
5
|
+
/**
|
|
6
|
+
* The promise.
|
|
7
|
+
*/
|
|
8
|
+
promise: Promise<T>;
|
|
9
|
+
/**
|
|
10
|
+
* Resolves the promise.
|
|
11
|
+
* @param value Resolved value.
|
|
12
|
+
*/
|
|
13
|
+
resolve(value: PromiseLike<T> | T): void;
|
|
14
|
+
/**
|
|
15
|
+
* Rejects the promise.
|
|
16
|
+
* @param reason Rejection reason.
|
|
17
|
+
*/
|
|
18
|
+
reject(reason?: unknown): void;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Returns an object that contains a promise and two functions to resolve or reject it.
|
|
22
|
+
* @returns The promise, and the resolve and reject functions.
|
|
23
|
+
*/
|
|
24
|
+
export declare function withResolvers<T = void>(): PromiseResolvers<T>;
|
package/dist/promises.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns an object that contains a promise and two functions to resolve or reject it.
|
|
3
|
+
* @returns The promise, and the resolve and reject functions.
|
|
4
|
+
*/
|
|
5
|
+
export function withResolvers() {
|
|
6
|
+
let resolve;
|
|
7
|
+
let reject;
|
|
8
|
+
const promise = new Promise((res, rej) => {
|
|
9
|
+
resolve = res;
|
|
10
|
+
reject = rej;
|
|
11
|
+
});
|
|
12
|
+
return { promise, resolve, reject };
|
|
13
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { EventEmitter } from "node:stream";
|
|
2
|
+
import type { JsonValue } from "../json.js";
|
|
3
|
+
import type { RpcProxy } from "./proxy.js";
|
|
4
|
+
import { type RpcRequestOptions, type RpcRequestParameters } from "./request.js";
|
|
5
|
+
import { type RpcResponse, type RpcResponseResult } from "./response.js";
|
|
6
|
+
/**
|
|
7
|
+
* Events that can occur as part of an RPC client.
|
|
8
|
+
*/
|
|
9
|
+
type RpcClientEventMap = {
|
|
10
|
+
/**
|
|
11
|
+
* Occurs when a response was returned that did not contain an identifier.
|
|
12
|
+
*/
|
|
13
|
+
unidentifiedResponse: [RpcResponse];
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Client capable of sending requests to, and receiving responses from, a server.
|
|
17
|
+
*/
|
|
18
|
+
export declare class RpcClient extends EventEmitter<RpcClientEventMap> {
|
|
19
|
+
#private;
|
|
20
|
+
/**
|
|
21
|
+
* Initializes a new instance of the {@link RpcClient} class.
|
|
22
|
+
* @param proxy Proxy responsible for sending requests to a server
|
|
23
|
+
*/
|
|
24
|
+
constructor(proxy: RpcProxy);
|
|
25
|
+
/**
|
|
26
|
+
* Sends a notification to the listening server.
|
|
27
|
+
* @param request The request.
|
|
28
|
+
*/
|
|
29
|
+
notify(request: RpcRequestOptions): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Sends a notification to the listening server.
|
|
32
|
+
* @param method Name of the method to invoke.
|
|
33
|
+
* @param params Parameters to be used during the invocation of the method.
|
|
34
|
+
*/
|
|
35
|
+
notify(method: string, params?: RpcRequestParameters): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Attempts to process the specified value as a response from a server.
|
|
38
|
+
* @param value Value to process.
|
|
39
|
+
* @returns `true` when the client was able to process the value as a response; otherwise `false`.
|
|
40
|
+
*/
|
|
41
|
+
receive(value: JsonValue): Promise<boolean>;
|
|
42
|
+
/**
|
|
43
|
+
* Sends the request to the listening server.
|
|
44
|
+
* @param request The request.
|
|
45
|
+
* @returns The response.
|
|
46
|
+
*/
|
|
47
|
+
request<TResult extends RpcResponseResult>(request: RpcRequestOptions): Promise<RpcResponse<TResult>>;
|
|
48
|
+
/**
|
|
49
|
+
* Sends the request to the listening server.
|
|
50
|
+
* @param method Name of the method to invoke.
|
|
51
|
+
* @param params Parameters to be used during the invocation of the method.
|
|
52
|
+
* @returns The response.
|
|
53
|
+
*/
|
|
54
|
+
request<TResult extends RpcResponseResult>(method: string, params?: RpcRequestParameters): Promise<RpcResponse<TResult>>;
|
|
55
|
+
}
|
|
56
|
+
export {};
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { EventEmitter } from "node:stream";
|
|
2
|
+
import { JsonRpcErrorCode } from "./json-rpc/error.js";
|
|
3
|
+
import { JsonRpcResponse } from "./json-rpc/response.js";
|
|
4
|
+
import {} from "./request.js";
|
|
5
|
+
import {} from "./response.js";
|
|
6
|
+
/**
|
|
7
|
+
* Client capable of sending requests to, and receiving responses from, a server.
|
|
8
|
+
*/
|
|
9
|
+
export class RpcClient extends EventEmitter {
|
|
10
|
+
/**
|
|
11
|
+
* Proxy responsible for sending requests to a server.
|
|
12
|
+
*/
|
|
13
|
+
#proxy;
|
|
14
|
+
/**
|
|
15
|
+
* Requests with pending responses.
|
|
16
|
+
*/
|
|
17
|
+
#requests = new Map();
|
|
18
|
+
/**
|
|
19
|
+
* Initializes a new instance of the {@link RpcClient} class.
|
|
20
|
+
* @param proxy Proxy responsible for sending requests to a server
|
|
21
|
+
*/
|
|
22
|
+
constructor(proxy) {
|
|
23
|
+
super();
|
|
24
|
+
this.#proxy = proxy;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Sends a notification to the listening server.
|
|
28
|
+
* @param methodOrRequest The method name, or the request.
|
|
29
|
+
* @param params Parameters to be used during the invocation of the method.
|
|
30
|
+
*/
|
|
31
|
+
async notify(methodOrRequest, params) {
|
|
32
|
+
if (typeof methodOrRequest === "string") {
|
|
33
|
+
await this.#proxy({
|
|
34
|
+
jsonrpc: "2.0",
|
|
35
|
+
method: methodOrRequest,
|
|
36
|
+
params,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
await this.#proxy({
|
|
41
|
+
jsonrpc: "2.0",
|
|
42
|
+
method: methodOrRequest.method,
|
|
43
|
+
params: methodOrRequest.params,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Attempts to process the specified value as a response from a server.
|
|
49
|
+
* @param value Value to process.
|
|
50
|
+
* @returns `true` when the client was able to process the value as a response; otherwise `false`.
|
|
51
|
+
*/
|
|
52
|
+
async receive(value) {
|
|
53
|
+
const { success, data: response } = JsonRpcResponse.safeParse(value);
|
|
54
|
+
if (!success) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
if ("result" in response) {
|
|
58
|
+
this.#resolve(response.id, {
|
|
59
|
+
ok: true,
|
|
60
|
+
result: response.result,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
this.#resolve(response.id, {
|
|
65
|
+
ok: false,
|
|
66
|
+
error: response.error,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Sends the request to the listening server.
|
|
73
|
+
* @param methodOrRequest The method name, or the request.
|
|
74
|
+
* @param params Parameters to be used during the invocation of the method.
|
|
75
|
+
* @returns The response.
|
|
76
|
+
*/
|
|
77
|
+
async request(methodOrRequest, params) {
|
|
78
|
+
if (typeof methodOrRequest === "string") {
|
|
79
|
+
return this.#send({
|
|
80
|
+
method: methodOrRequest,
|
|
81
|
+
params,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
return this.#send(methodOrRequest);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Resolves pending requests.
|
|
90
|
+
* @param id The request identifier.
|
|
91
|
+
* @param response The response.
|
|
92
|
+
*/
|
|
93
|
+
#resolve(id, response) {
|
|
94
|
+
if (id === undefined) {
|
|
95
|
+
this.emit("unidentifiedResponse", response);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
// Get the handler
|
|
99
|
+
const handler = this.#requests.get(id);
|
|
100
|
+
this.#requests.delete(id);
|
|
101
|
+
// Provide the result or error to the handler.
|
|
102
|
+
if (handler) {
|
|
103
|
+
handler(response);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Sends the request to the listening server.
|
|
108
|
+
* @param request The request.
|
|
109
|
+
* @returns The response.
|
|
110
|
+
*/
|
|
111
|
+
async #send(request) {
|
|
112
|
+
const id = crypto.randomUUID();
|
|
113
|
+
const { method, params, timeout } = request;
|
|
114
|
+
// Initialize the response handler.
|
|
115
|
+
const response = new Promise((resolve) => {
|
|
116
|
+
this.#requests.set(id, (res) => {
|
|
117
|
+
clearTimeout(timeoutMonitor);
|
|
118
|
+
resolve(res);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
// Start the timeout, and send the request.
|
|
122
|
+
const timeoutMonitor = setTimeout(() => {
|
|
123
|
+
this.#resolve(id, {
|
|
124
|
+
ok: false,
|
|
125
|
+
error: {
|
|
126
|
+
code: JsonRpcErrorCode.InternalError,
|
|
127
|
+
message: "The request timed out.",
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
}, timeout);
|
|
131
|
+
const accepted = await this.#proxy({ jsonrpc: "2.0", method, params, id });
|
|
132
|
+
// When the server did not accept the request, return a 406.
|
|
133
|
+
if (!accepted) {
|
|
134
|
+
this.#resolve(id, {
|
|
135
|
+
ok: false,
|
|
136
|
+
error: {
|
|
137
|
+
code: JsonRpcErrorCode.InternalError,
|
|
138
|
+
message: "Failed to send request.",
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
return response;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type JsonValue } from "../json.js";
|
|
2
|
+
import { RpcClient } from "./client.js";
|
|
3
|
+
import type { RpcProxy } from "./proxy.js";
|
|
4
|
+
import { RpcServer } from "./server.js";
|
|
5
|
+
/**
|
|
6
|
+
* Acts as an RPC client and a server, allowing for outbound and inbound requests and responses.
|
|
7
|
+
*/
|
|
8
|
+
export declare class RpcGateway {
|
|
9
|
+
/**
|
|
10
|
+
* Client response for sending requests, and receiving responses.
|
|
11
|
+
*/
|
|
12
|
+
readonly client: RpcClient;
|
|
13
|
+
/**
|
|
14
|
+
* Server responsible for receiving requests, and sending responses.
|
|
15
|
+
*/
|
|
16
|
+
readonly server: RpcServer;
|
|
17
|
+
/**
|
|
18
|
+
* Initializes a new instance of the {@link RpcGateway} class.
|
|
19
|
+
* @param proxy Proxy capable of sending requests or responses to a client or server.
|
|
20
|
+
*/
|
|
21
|
+
constructor(proxy: RpcProxy);
|
|
22
|
+
/**
|
|
23
|
+
* Attempts to process the specified value as a request or response.
|
|
24
|
+
* @param value Value to process.
|
|
25
|
+
* @returns `true` when the value was successfully processed; otherwise `false`.
|
|
26
|
+
*/
|
|
27
|
+
receive(value: JsonValue): Promise<boolean>;
|
|
28
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {} from "../json.js";
|
|
2
|
+
import { RpcClient } from "./client.js";
|
|
3
|
+
import { RpcServer } from "./server.js";
|
|
4
|
+
/**
|
|
5
|
+
* Acts as an RPC client and a server, allowing for outbound and inbound requests and responses.
|
|
6
|
+
*/
|
|
7
|
+
export class RpcGateway {
|
|
8
|
+
/**
|
|
9
|
+
* Client response for sending requests, and receiving responses.
|
|
10
|
+
*/
|
|
11
|
+
client;
|
|
12
|
+
/**
|
|
13
|
+
* Server responsible for receiving requests, and sending responses.
|
|
14
|
+
*/
|
|
15
|
+
server;
|
|
16
|
+
/**
|
|
17
|
+
* Initializes a new instance of the {@link RpcGateway} class.
|
|
18
|
+
* @param proxy Proxy capable of sending requests or responses to a client or server.
|
|
19
|
+
*/
|
|
20
|
+
constructor(proxy) {
|
|
21
|
+
this.client = new RpcClient(proxy);
|
|
22
|
+
this.server = new RpcServer(proxy);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Attempts to process the specified value as a request or response.
|
|
26
|
+
* @param value Value to process.
|
|
27
|
+
* @returns `true` when the value was successfully processed; otherwise `false`.
|
|
28
|
+
*/
|
|
29
|
+
async receive(value) {
|
|
30
|
+
if (await this.client.receive(value)) {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
if (await this.server.receive(value)) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./json-rpc/error.js";
|
|
2
|
+
export * from "./json-rpc/request.js";
|
|
3
|
+
export * from "./json-rpc/response.js";
|
|
4
|
+
export * from "./client.js";
|
|
5
|
+
export * from "./gateway.js";
|
|
6
|
+
export * from "./proxy.js";
|
|
7
|
+
export * from "./request.js";
|
|
8
|
+
export * from "./response.js";
|
|
9
|
+
export * from "./server.js";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./json-rpc/error.js";
|
|
2
|
+
export * from "./json-rpc/request.js";
|
|
3
|
+
export * from "./json-rpc/response.js";
|
|
4
|
+
export * from "./client.js";
|
|
5
|
+
export * from "./gateway.js";
|
|
6
|
+
export * from "./proxy.js";
|
|
7
|
+
export * from "./request.js";
|
|
8
|
+
export * from "./response.js";
|
|
9
|
+
export * from "./server.js";
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { z } from "zod/v4-mini";
|
|
2
|
+
import type { JsonValue } from "../../json.js";
|
|
3
|
+
/**
|
|
4
|
+
* Contains information about an error that occurred.
|
|
5
|
+
*/
|
|
6
|
+
export type JsonRpcError = {
|
|
7
|
+
/**
|
|
8
|
+
* Indicates the error type that occurred.
|
|
9
|
+
*/
|
|
10
|
+
readonly code: JsonRpcErrorCode | number;
|
|
11
|
+
/**
|
|
12
|
+
* Short description of the error.
|
|
13
|
+
*/
|
|
14
|
+
readonly message: string;
|
|
15
|
+
/**
|
|
16
|
+
* Contains additional information about the error.
|
|
17
|
+
*/
|
|
18
|
+
readonly data?: JsonValue;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Contains information about an error that occurred on the server.
|
|
22
|
+
*/
|
|
23
|
+
export declare const JsonRpcError: z.ZodMiniType<JsonRpcError>;
|
|
24
|
+
/**
|
|
25
|
+
* Reserved error codes for JSON-RPC 2.0.
|
|
26
|
+
*/
|
|
27
|
+
export declare const JsonRpcErrorCode: {
|
|
28
|
+
/**
|
|
29
|
+
* Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.
|
|
30
|
+
*/
|
|
31
|
+
readonly ParseError: -32700;
|
|
32
|
+
/**
|
|
33
|
+
* The JSON sent is not a valid Request object.
|
|
34
|
+
*/
|
|
35
|
+
readonly InvalidRequest: -32600;
|
|
36
|
+
/**
|
|
37
|
+
* The method does not exist / is not available.
|
|
38
|
+
*/
|
|
39
|
+
readonly MethodNotFound: -32601;
|
|
40
|
+
/**
|
|
41
|
+
* Invalid method parameter(s).
|
|
42
|
+
*/
|
|
43
|
+
readonly InvalidParams: -32602;
|
|
44
|
+
/**
|
|
45
|
+
* Internal JSON-RPC error.
|
|
46
|
+
*/
|
|
47
|
+
readonly InternalError: -32603;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Reserved error codes for JSON-RPC 2.0.
|
|
51
|
+
*/
|
|
52
|
+
export type JsonRpcErrorCode = (typeof JsonRpcErrorCode)[keyof typeof JsonRpcErrorCode];
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { z } from "zod/v4-mini";
|
|
2
|
+
/**
|
|
3
|
+
* Contains information about an error that occurred on the server.
|
|
4
|
+
*/
|
|
5
|
+
export const JsonRpcError = z.object({
|
|
6
|
+
code: z.number(),
|
|
7
|
+
message: z.string(),
|
|
8
|
+
data: z.any(),
|
|
9
|
+
});
|
|
10
|
+
/**
|
|
11
|
+
* Reserved error codes for JSON-RPC 2.0.
|
|
12
|
+
*/
|
|
13
|
+
export const JsonRpcErrorCode = {
|
|
14
|
+
/**
|
|
15
|
+
* Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.
|
|
16
|
+
*/
|
|
17
|
+
ParseError: -32700,
|
|
18
|
+
/**
|
|
19
|
+
* The JSON sent is not a valid Request object.
|
|
20
|
+
*/
|
|
21
|
+
InvalidRequest: -32600,
|
|
22
|
+
/**
|
|
23
|
+
* The method does not exist / is not available.
|
|
24
|
+
*/
|
|
25
|
+
MethodNotFound: -32601,
|
|
26
|
+
/**
|
|
27
|
+
* Invalid method parameter(s).
|
|
28
|
+
*/
|
|
29
|
+
InvalidParams: -32602,
|
|
30
|
+
/**
|
|
31
|
+
* Internal JSON-RPC error.
|
|
32
|
+
*/
|
|
33
|
+
InternalError: -32603,
|
|
34
|
+
};
|