@elgato/utils 0.1.0 → 0.2.1

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.
@@ -1,34 +1,25 @@
1
- import { EventEmitter } from "node:stream";
2
1
  import type { JsonValue } from "../json.js";
3
- import type { RpcProxy } from "./proxy.js";
4
2
  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
- };
3
+ import { type RpcErrorResponse, type RpcResponse, type RpcResponseResult } from "./response.js";
4
+ import type { RpcSender } from "./sender.js";
15
5
  /**
16
6
  * Client capable of sending requests to, and receiving responses from, a server.
17
7
  */
18
- export declare class RpcClient extends EventEmitter<RpcClientEventMap> {
8
+ export declare class RpcClient {
19
9
  #private;
20
10
  /**
21
11
  * Initializes a new instance of the {@link RpcClient} class.
22
- * @param proxy Proxy responsible for sending requests to a server
12
+ * @param send Function responsible for sending requests.
13
+ * @param options Client options.
23
14
  */
24
- constructor(proxy: RpcProxy);
15
+ constructor(send: RpcSender, options?: RpcClientOptions);
25
16
  /**
26
- * Sends a notification to the listening server.
17
+ * Sends a notification to the RPC server.
27
18
  * @param request The request.
28
19
  */
29
20
  notify(request: RpcRequestOptions): Promise<void>;
30
21
  /**
31
- * Sends a notification to the listening server.
22
+ * Sends a notification to the RPC server.
32
23
  * @param method Name of the method to invoke.
33
24
  * @param params Parameters to be used during the invocation of the method.
34
25
  */
@@ -40,17 +31,26 @@ export declare class RpcClient extends EventEmitter<RpcClientEventMap> {
40
31
  */
41
32
  receive(value: JsonValue): Promise<boolean>;
42
33
  /**
43
- * Sends the request to the listening server.
34
+ * Sends the request to the RPC server.
44
35
  * @param request The request.
45
36
  * @returns The response.
46
37
  */
47
38
  request<TResult extends RpcResponseResult>(request: RpcRequestOptions): Promise<RpcResponse<TResult>>;
48
39
  /**
49
- * Sends the request to the listening server.
40
+ * Sends the request to the RPC server.
50
41
  * @param method Name of the method to invoke.
51
42
  * @param params Parameters to be used during the invocation of the method.
52
43
  * @returns The response.
53
44
  */
54
45
  request<TResult extends RpcResponseResult>(method: string, params?: RpcRequestParameters): Promise<RpcResponse<TResult>>;
55
46
  }
56
- export {};
47
+ /**
48
+ * Options for an RPC client.
49
+ */
50
+ export type RpcClientOptions = {
51
+ /**
52
+ * Function invoked when an error response was received that was not associated with a request.
53
+ * @param response The response.
54
+ */
55
+ error?: (response: RpcErrorResponse) => void;
56
+ };
@@ -1,4 +1,3 @@
1
- import { EventEmitter } from "node:stream";
2
1
  import { JsonRpcErrorCode } from "./json-rpc/error.js";
3
2
  import { JsonRpcResponse } from "./json-rpc/response.js";
4
3
  import {} from "./request.js";
@@ -6,38 +5,46 @@ import {} from "./response.js";
6
5
  /**
7
6
  * Client capable of sending requests to, and receiving responses from, a server.
8
7
  */
9
- export class RpcClient extends EventEmitter {
8
+ export class RpcClient {
10
9
  /**
11
- * Proxy responsible for sending requests to a server.
10
+ * The client's options.
12
11
  */
13
- #proxy;
12
+ #options;
13
+ /**
14
+ * Function responsible for sending requests.
15
+ */
16
+ #send;
14
17
  /**
15
18
  * Requests with pending responses.
16
19
  */
17
20
  #requests = new Map();
18
21
  /**
19
22
  * Initializes a new instance of the {@link RpcClient} class.
20
- * @param proxy Proxy responsible for sending requests to a server
23
+ * @param send Function responsible for sending requests.
24
+ * @param options Client options.
21
25
  */
22
- constructor(proxy) {
23
- super();
24
- this.#proxy = proxy;
26
+ constructor(send, options) {
27
+ this.#send = send;
28
+ this.#options = {
29
+ ...{ error: () => { } },
30
+ ...options,
31
+ };
25
32
  }
26
33
  /**
27
- * Sends a notification to the listening server.
34
+ * Sends a notification to the RPC server.
28
35
  * @param methodOrRequest The method name, or the request.
29
36
  * @param params Parameters to be used during the invocation of the method.
30
37
  */
31
38
  async notify(methodOrRequest, params) {
32
39
  if (typeof methodOrRequest === "string") {
33
- await this.#proxy({
40
+ await this.#send({
34
41
  jsonrpc: "2.0",
35
42
  method: methodOrRequest,
36
43
  params,
37
44
  });
38
45
  }
39
46
  else {
40
- await this.#proxy({
47
+ await this.#send({
41
48
  jsonrpc: "2.0",
42
49
  method: methodOrRequest.method,
43
50
  params: methodOrRequest.params,
@@ -61,28 +68,34 @@ export class RpcClient extends EventEmitter {
61
68
  });
62
69
  }
63
70
  else {
64
- this.#resolve(response.id, {
71
+ const err = {
65
72
  ok: false,
66
73
  error: response.error,
67
- });
74
+ };
75
+ if (response.id === undefined) {
76
+ this.#options.error(err);
77
+ }
78
+ else {
79
+ this.#resolve(response.id, err);
80
+ }
68
81
  }
69
82
  return true;
70
83
  }
71
84
  /**
72
- * Sends the request to the listening server.
85
+ * Sends the request to the RPC server.
73
86
  * @param methodOrRequest The method name, or the request.
74
87
  * @param params Parameters to be used during the invocation of the method.
75
88
  * @returns The response.
76
89
  */
77
90
  async request(methodOrRequest, params) {
78
91
  if (typeof methodOrRequest === "string") {
79
- return this.#send({
92
+ return this.#sendRequest({
80
93
  method: methodOrRequest,
81
94
  params,
82
95
  });
83
96
  }
84
97
  else {
85
- return this.#send(methodOrRequest);
98
+ return this.#sendRequest(methodOrRequest);
86
99
  }
87
100
  }
88
101
  /**
@@ -91,10 +104,6 @@ export class RpcClient extends EventEmitter {
91
104
  * @param response The response.
92
105
  */
93
106
  #resolve(id, response) {
94
- if (id === undefined) {
95
- this.emit("unidentifiedResponse", response);
96
- return;
97
- }
98
107
  // Get the handler
99
108
  const handler = this.#requests.get(id);
100
109
  this.#requests.delete(id);
@@ -104,11 +113,11 @@ export class RpcClient extends EventEmitter {
104
113
  }
105
114
  }
106
115
  /**
107
- * Sends the request to the listening server.
116
+ * Sends the request to the RPC server.
108
117
  * @param request The request.
109
118
  * @returns The response.
110
119
  */
111
- async #send(request) {
120
+ async #sendRequest(request) {
112
121
  const id = crypto.randomUUID();
113
122
  const { method, params, timeout } = request;
114
123
  // Initialize the response handler.
@@ -128,17 +137,7 @@ export class RpcClient extends EventEmitter {
128
137
  },
129
138
  });
130
139
  }, 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
- }
140
+ await this.#send({ jsonrpc: "2.0", method, params, id });
142
141
  return response;
143
142
  }
144
143
  }
@@ -2,8 +2,8 @@ export * from "./json-rpc/error.js";
2
2
  export * from "./json-rpc/request.js";
3
3
  export * from "./json-rpc/response.js";
4
4
  export * from "./client.js";
5
- export * from "./gateway.js";
6
- export * from "./proxy.js";
7
5
  export * from "./request.js";
8
6
  export * from "./response.js";
7
+ export * from "./sender.js";
8
+ export * from "./server-client.js";
9
9
  export * from "./server.js";
package/dist/rpc/index.js CHANGED
@@ -2,8 +2,8 @@ export * from "./json-rpc/error.js";
2
2
  export * from "./json-rpc/request.js";
3
3
  export * from "./json-rpc/response.js";
4
4
  export * from "./client.js";
5
- export * from "./gateway.js";
6
- export * from "./proxy.js";
7
5
  export * from "./request.js";
8
6
  export * from "./response.js";
7
+ export * from "./sender.js";
8
+ export * from "./server-client.js";
9
9
  export * from "./server.js";
@@ -1,10 +1,14 @@
1
1
  import type { JsonObject, JsonPrimitive, JsonValue } from "../json.js";
2
2
  import type { JsonRpcError } from "./json-rpc/error.js";
3
- import type { RpcProxy } from "./proxy.js";
3
+ import type { RpcSender } from "./sender.js";
4
4
  /**
5
5
  * Response object sent from a server.
6
6
  */
7
- export type RpcResponse<T extends RpcResponseResult = RpcResponseResult> = {
7
+ export type RpcResponse<T extends RpcResponseResult = RpcResponseResult> = RpcErrorResponse | RpcSuccessResponse<T>;
8
+ /**
9
+ * Success response object sent from a server.
10
+ */
11
+ export type RpcSuccessResponse<T extends RpcResponseResult = RpcResponseResult> = {
8
12
  /**
9
13
  * Result of the request.
10
14
  */
@@ -13,7 +17,11 @@ export type RpcResponse<T extends RpcResponseResult = RpcResponseResult> = {
13
17
  * Determines whether the request was successful.
14
18
  */
15
19
  readonly ok: true;
16
- } | {
20
+ };
21
+ /**
22
+ * Error response object sent from a server.
23
+ */
24
+ export type RpcErrorResponse = {
17
25
  /**
18
26
  * The error that occurred.
19
27
  */
@@ -34,10 +42,10 @@ export declare class RpcRequestResponder {
34
42
  #private;
35
43
  /**
36
44
  * Initializes a new instance of the {@link RpcRequestResponder} class.
37
- * @param proxy Proxy responsible for forwarding the response to the client.
45
+ * @param send Function responsible for sending responses.
38
46
  * @param id Identifier of the request.
39
47
  */
40
- constructor(proxy: RpcProxy, id: string | undefined);
48
+ constructor(send: RpcSender, id: string | undefined);
41
49
  /**
42
50
  * Indicates whether a response can be sent.
43
51
  * @returns `true` when a response has not yet been set and the request accepts responses.
@@ -6,21 +6,21 @@ export class RpcRequestResponder {
6
6
  * Identifier of the request.
7
7
  */
8
8
  #id;
9
- /**
10
- * Proxy responsible for sending the response to the client.
11
- */
12
- #proxy;
13
9
  /**
14
10
  * Indicates whether a response has already been sent in relation to the response.
15
11
  */
16
12
  #responded = false;
13
+ /**
14
+ * Function responsible for sending responses.
15
+ */
16
+ #send;
17
17
  /**
18
18
  * Initializes a new instance of the {@link RpcRequestResponder} class.
19
- * @param proxy Proxy responsible for forwarding the response to the client.
19
+ * @param send Function responsible for sending responses.
20
20
  * @param id Identifier of the request.
21
21
  */
22
- constructor(proxy, id) {
23
- this.#proxy = proxy;
22
+ constructor(send, id) {
23
+ this.#send = send;
24
24
  this.#id = id;
25
25
  }
26
26
  /**
@@ -36,7 +36,7 @@ export class RpcRequestResponder {
36
36
  * @returns Promise fulfilled once the response has been sent.
37
37
  */
38
38
  error(error) {
39
- return this.#send({
39
+ return this.#trySend({
40
40
  jsonrpc: "2.0",
41
41
  id: this.#id,
42
42
  error,
@@ -49,7 +49,7 @@ export class RpcRequestResponder {
49
49
  */
50
50
  async success(result) {
51
51
  if (this.#id) {
52
- await this.#send({
52
+ await this.#trySend({
53
53
  jsonrpc: "2.0",
54
54
  id: this.#id,
55
55
  result,
@@ -57,12 +57,12 @@ export class RpcRequestResponder {
57
57
  }
58
58
  }
59
59
  /**
60
- * Sends the response to the client.
60
+ * Sends the response to the client if one can be sent.
61
61
  * @param res The response.
62
62
  */
63
- async #send(res) {
63
+ async #trySend(res) {
64
64
  if (!this.#responded) {
65
- await this.#proxy(res);
65
+ await this.#send(res);
66
66
  this.#responded = true;
67
67
  }
68
68
  }
@@ -0,0 +1,7 @@
1
+ import type { JsonRpcRequest } from "./json-rpc/request.js";
2
+ import type { JsonRpcResponse } from "./json-rpc/response.js";
3
+ /**
4
+ * Function responsible for sending requests and responses
5
+ * @param value Request or response to send.
6
+ */
7
+ export type RpcSender = (value: JsonRpcRequest | JsonRpcResponse) => Promise<void>;
@@ -0,0 +1,22 @@
1
+ import { type JsonValue } from "../json.js";
2
+ import { RpcClient, type RpcClientOptions } from "./client.js";
3
+ import type { RpcSender } from "./sender.js";
4
+ import { RpcServer } from "./server.js";
5
+ /**
6
+ * Create an RPC server-and-client, capable of receiving and sending requests and notifications.
7
+ * @param send Function responsible for sending requests and responses.
8
+ * @param options the options.
9
+ * @returns The RPC server-and-client.
10
+ */
11
+ export declare function createRpcServerClient(send: RpcSender, options?: RpcClientOptions): RpcServerClient;
12
+ /**
13
+ * An RPC server-and-client, capable of receiving and sending requests and notifications.
14
+ */
15
+ export type RpcServerClient = Pick<RpcClient, "notify" | "request"> & Pick<RpcServer, "addMethod"> & {
16
+ /**
17
+ * Attempts to process the specified value as a request or response.
18
+ * @param value Value to process.
19
+ * @returns `true` when the value was successfully processed; otherwise `false`.
20
+ */
21
+ receive(value: JsonValue): Promise<boolean>;
22
+ };
@@ -0,0 +1,21 @@
1
+ import {} from "../json.js";
2
+ import { RpcClient } from "./client.js";
3
+ import { RpcServer } from "./server.js";
4
+ /**
5
+ * Create an RPC server-and-client, capable of receiving and sending requests and notifications.
6
+ * @param send Function responsible for sending requests and responses.
7
+ * @param options the options.
8
+ * @returns The RPC server-and-client.
9
+ */
10
+ export function createRpcServerClient(send, options) {
11
+ const client = new RpcClient(send, options);
12
+ const server = new RpcServer(send);
13
+ return {
14
+ addMethod: server.addMethod.bind(server),
15
+ notify: client.notify.bind(client),
16
+ request: client.request.bind(client),
17
+ async receive(value) {
18
+ return (await client.receive(value)) || (await server.receive(value));
19
+ },
20
+ };
21
+ }
@@ -1,8 +1,8 @@
1
1
  import { type IDisposable } from "../disposable.js";
2
2
  import type { JsonValue } from "../json.js";
3
- import type { RpcProxy } from "./proxy.js";
4
3
  import type { RpcRequestParameters } from "./request.js";
5
4
  import { RpcRequestResponder, type RpcResponseResult } from "./response.js";
5
+ import type { RpcSender } from "./sender.js";
6
6
  /**
7
7
  * Server capable of receiving requests from, and sending responses to, a client.
8
8
  */
@@ -10,16 +10,16 @@ export declare class RpcServer {
10
10
  #private;
11
11
  /**
12
12
  * Initializes a new instance of the {@link RpcServer} class.
13
- * @param proxy Proxy responsible for sending responses to a client.
13
+ * @param send Function responsible for sending responses.
14
14
  */
15
- constructor(proxy: RpcProxy);
15
+ constructor(send: RpcSender);
16
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.
17
+ * Adds a method handler to this instance, allowing it to receive requests and notifications.
18
+ * @param name The name of the method.
19
+ * @param handler The function to be invoked when the method is called.
20
+ * @returns A disposable object that can be used to remove the method handler.
21
21
  */
22
- add<TParameters extends RpcRequestParameters>(name: string, handler: MethodHandler<TParameters>): IDisposable;
22
+ addMethod<TParameters extends RpcRequestParameters>(name: string, handler: MethodHandler<TParameters>): IDisposable;
23
23
  /**
24
24
  * Attempts to process the specified request received from a client.
25
25
  * @param value Value to process.
@@ -12,23 +12,23 @@ export class RpcServer {
12
12
  */
13
13
  #methods = new EventEmitter();
14
14
  /**
15
- * Proxy responsible for sending responses to a client.
15
+ * Function responsible for sending responses.
16
16
  */
17
- #proxy;
17
+ #send;
18
18
  /**
19
19
  * Initializes a new instance of the {@link RpcServer} class.
20
- * @param proxy Proxy responsible for sending responses to a client.
20
+ * @param send Function responsible for sending responses.
21
21
  */
22
- constructor(proxy) {
23
- this.#proxy = proxy;
22
+ constructor(send) {
23
+ this.#send = send;
24
24
  }
25
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.
26
+ * Adds a method handler to this instance, allowing it to receive requests and notifications.
27
+ * @param name The name of the method.
28
+ * @param handler The function to be invoked when the method is called.
29
+ * @returns A disposable object that can be used to remove the method handler.
30
30
  */
31
- add(name, handler) {
31
+ addMethod(name, handler) {
32
32
  return this.#methods.disposableOn(name, handler);
33
33
  }
34
34
  /**
@@ -41,7 +41,7 @@ export class RpcServer {
41
41
  if (!success) {
42
42
  return false;
43
43
  }
44
- const responder = new RpcRequestResponder(this.#proxy, request.id);
44
+ const responder = new RpcRequestResponder(this.#send, request.id);
45
45
  const methods = this.#methods.listeners(request.method);
46
46
  if (methods.length === 0) {
47
47
  await responder.error({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elgato/utils",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "description": "Utilities used throughout the Elgato ecosystem.",
5
5
  "type": "module",
6
6
  "files": [
@@ -13,7 +13,8 @@
13
13
  "test": "vitest run --typecheck",
14
14
  "test:coverage": "vitest run --coverage",
15
15
  "test:watch": "vitest --typecheck",
16
- "type:check": "tsc"
16
+ "type:check": "tsc",
17
+ "watch": "tsc --project tsconfig.build.json --watch"
17
18
  },
18
19
  "exports": {
19
20
  ".": {
@@ -1,28 +0,0 @@
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
- }
@@ -1,38 +0,0 @@
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
- }
@@ -1,7 +0,0 @@
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;
File without changes