@aztec/foundation 0.23.0 → 0.24.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.
Files changed (118) hide show
  1. package/dest/fields/fields.js +3 -3
  2. package/package.json +2 -2
  3. package/src/abi/abi.ts +337 -0
  4. package/src/abi/buffer.ts +36 -0
  5. package/src/abi/decoder.ts +176 -0
  6. package/src/abi/encoder.ts +143 -0
  7. package/src/abi/index.ts +6 -0
  8. package/src/abi/selector.ts +243 -0
  9. package/src/abi/utils.ts +50 -0
  10. package/src/array/array.ts +86 -0
  11. package/src/array/index.ts +1 -0
  12. package/src/async-map/index.ts +18 -0
  13. package/src/aztec-address/index.ts +36 -0
  14. package/src/bigint-buffer/index.ts +87 -0
  15. package/src/collection/array.ts +64 -0
  16. package/src/collection/index.ts +1 -0
  17. package/src/committable/committable.ts +46 -0
  18. package/src/committable/index.ts +1 -0
  19. package/src/crypto/index.ts +16 -0
  20. package/src/crypto/keccak/index.ts +33 -0
  21. package/src/crypto/pedersen/index.ts +1 -0
  22. package/src/crypto/pedersen/pedersen.elliptic.ts +584 -0
  23. package/src/crypto/pedersen/pedersen.noble.ts +573 -0
  24. package/src/crypto/pedersen/pedersen.wasm.ts +42 -0
  25. package/src/crypto/random/index.ts +42 -0
  26. package/src/crypto/sha256/index.ts +3 -0
  27. package/src/errors/index.ts +6 -0
  28. package/src/eth-address/index.ts +234 -0
  29. package/src/fields/coordinate.ts +104 -0
  30. package/src/fields/fields.ts +328 -0
  31. package/src/fields/index.ts +3 -0
  32. package/src/fields/point.ts +145 -0
  33. package/src/fifo/bounded_serial_queue.ts +100 -0
  34. package/src/fifo/index.ts +4 -0
  35. package/src/fifo/memory_fifo.ts +118 -0
  36. package/src/fifo/semaphore.ts +33 -0
  37. package/src/fifo/serial_queue.ts +81 -0
  38. package/src/index.ts +29 -0
  39. package/src/json-rpc/README.md +55 -0
  40. package/src/json-rpc/class_converter.ts +213 -0
  41. package/src/json-rpc/client/index.ts +1 -0
  42. package/src/json-rpc/client/json_rpc_client.ts +147 -0
  43. package/src/json-rpc/convert.ts +163 -0
  44. package/src/json-rpc/fixtures/class_a.ts +15 -0
  45. package/src/json-rpc/fixtures/class_b.ts +15 -0
  46. package/src/json-rpc/fixtures/test_state.ts +59 -0
  47. package/src/json-rpc/index.ts +8 -0
  48. package/src/json-rpc/js_utils.ts +20 -0
  49. package/src/json-rpc/server/index.ts +2 -0
  50. package/src/json-rpc/server/json_proxy.ts +60 -0
  51. package/src/json-rpc/server/json_rpc_server.ts +269 -0
  52. package/src/log/console.ts +39 -0
  53. package/src/log/debug.ts +83 -0
  54. package/src/log/index.ts +5 -0
  55. package/src/log/log_fn.ts +5 -0
  56. package/src/log/log_history.ts +44 -0
  57. package/src/log/logger.ts +137 -0
  58. package/src/mutex/index.ts +83 -0
  59. package/src/mutex/mutex_database.ts +12 -0
  60. package/src/noir/index.ts +1 -0
  61. package/src/noir/noir_package_config.ts +54 -0
  62. package/src/retry/index.ts +99 -0
  63. package/src/running-promise/index.ts +60 -0
  64. package/src/serialize/buffer_reader.ts +286 -0
  65. package/src/serialize/field_reader.ts +143 -0
  66. package/src/serialize/free_funcs.ts +147 -0
  67. package/src/serialize/index.ts +5 -0
  68. package/src/serialize/serialize.ts +303 -0
  69. package/src/serialize/types.ts +40 -0
  70. package/src/sleep/index.ts +71 -0
  71. package/src/testing/index.ts +1 -0
  72. package/src/testing/test_data.ts +36 -0
  73. package/src/timer/elapsed.ts +23 -0
  74. package/src/timer/index.ts +3 -0
  75. package/src/timer/timeout.ts +64 -0
  76. package/src/timer/timer.ts +48 -0
  77. package/src/transport/browser/index.ts +4 -0
  78. package/src/transport/browser/message_port_socket.ts +48 -0
  79. package/src/transport/browser/shared_worker_connector.ts +21 -0
  80. package/src/transport/browser/shared_worker_listener.ts +53 -0
  81. package/src/transport/browser/worker_connector.ts +30 -0
  82. package/src/transport/browser/worker_listener.ts +54 -0
  83. package/src/transport/dispatch/create_dispatch_fn.ts +35 -0
  84. package/src/transport/dispatch/create_dispatch_proxy.ts +141 -0
  85. package/src/transport/dispatch/messages.ts +58 -0
  86. package/src/transport/index.ts +11 -0
  87. package/src/transport/interface/connector.ts +9 -0
  88. package/src/transport/interface/listener.ts +16 -0
  89. package/src/transport/interface/socket.ts +15 -0
  90. package/src/transport/interface/transferable.ts +125 -0
  91. package/src/transport/node/index.ts +2 -0
  92. package/src/transport/node/node_connector.ts +30 -0
  93. package/src/transport/node/node_connector_socket.ts +52 -0
  94. package/src/transport/node/node_listener.ts +34 -0
  95. package/src/transport/node/node_listener_socket.ts +48 -0
  96. package/src/transport/transport_client.ts +131 -0
  97. package/src/transport/transport_server.ts +108 -0
  98. package/src/trees/index.ts +54 -0
  99. package/src/types/index.ts +8 -0
  100. package/src/url/index.ts +73 -0
  101. package/src/wasm/README.md +6 -0
  102. package/src/wasm/empty_wasi_sdk.ts +166 -0
  103. package/src/wasm/fixtures/gcd.wasm +0 -0
  104. package/src/wasm/fixtures/gcd.wat +27 -0
  105. package/src/wasm/index.ts +1 -0
  106. package/src/wasm/wasm_module.ts +260 -0
  107. package/src/worker/browser/index.ts +2 -0
  108. package/src/worker/browser/start_web_module.ts +23 -0
  109. package/src/worker/browser/web_data_store.ts +38 -0
  110. package/src/worker/browser/web_worker.ts +24 -0
  111. package/src/worker/data_store.ts +19 -0
  112. package/src/worker/index.ts +2 -0
  113. package/src/worker/node/index.ts +2 -0
  114. package/src/worker/node/node_data_store.ts +27 -0
  115. package/src/worker/node/node_worker.ts +22 -0
  116. package/src/worker/node/start_node_module.ts +29 -0
  117. package/src/worker/wasm_worker.ts +7 -0
  118. package/src/worker/worker_pool.ts +73 -0
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Represents a request message.
3
+ * Contains a unique identifier (msgId) and a payload object.
4
+ */
5
+ export interface RequestMessage<Payload> {
6
+ /**
7
+ * A unique identifier for a message.
8
+ */
9
+ msgId: number;
10
+ /**
11
+ * The data content carried within a message.
12
+ */
13
+ payload: Payload;
14
+ }
15
+
16
+ /**
17
+ * Represents a structured response message.
18
+ * Contains an identifier to match with the corresponding request.
19
+ */
20
+ export interface ResponseMessage<Payload> {
21
+ /**
22
+ * A unique identifier for the message.
23
+ */
24
+ msgId: number;
25
+ /**
26
+ * The data content carried within the message.
27
+ */
28
+ payload?: Payload;
29
+ /**
30
+ * An optional error description in case the response contains an error instead of a payload.
31
+ */
32
+ error?: string;
33
+ }
34
+
35
+ /**
36
+ * Represents an event-based message in a communication system.
37
+ * Contains a payload with the relevant data associated with a specific event occurrence.
38
+ */
39
+ export interface EventMessage<Payload> {
40
+ /**
41
+ * The data content associated with a message.
42
+ */
43
+ payload: Payload;
44
+ }
45
+
46
+ /**
47
+ * Determines if the given 'msg' is an EventMessage by checking if its 'msgId' property is undefined.
48
+ * Returns true if the input message is of type EventMessage, otherwise false. This utility function can be used
49
+ * to differentiate between instances of ResponseMessage and EventMessage that share a common Payload type.
50
+ *
51
+ * @param msg - The message object that can be either a ResponseMessage or EventMessage with a specific payload.
52
+ * @returns A boolean value indicating whether the input message is an EventMessage (true) or not (false).
53
+ */
54
+ export function isEventMessage<Payload>(
55
+ msg: ResponseMessage<Payload> | EventMessage<Payload>,
56
+ ): msg is EventMessage<Payload> {
57
+ return (msg as ResponseMessage<Payload>).msgId === undefined;
58
+ }
@@ -0,0 +1,11 @@
1
+ export * from './dispatch/create_dispatch_fn.js';
2
+ export * from './dispatch/create_dispatch_proxy.js';
3
+ export * from './dispatch/messages.js';
4
+ export * from './interface/connector.js';
5
+ export * from './interface/listener.js';
6
+ export * from './interface/socket.js';
7
+ export * from './interface/transferable.js';
8
+ export * from './transport_client.js';
9
+ export * from './transport_server.js';
10
+ export * from './browser/index.js';
11
+ export * from './node/index.js';
@@ -0,0 +1,9 @@
1
+ import { Socket } from './socket.js';
2
+
3
+ /**
4
+ * Opens a socket with corresponding TransportListener.
5
+ */
6
+ export interface Connector {
7
+ // eslint-disable-next-line jsdoc/require-jsdoc
8
+ createSocket(): Promise<Socket>;
9
+ }
@@ -0,0 +1,16 @@
1
+ import EventEmitter from 'events';
2
+
3
+ import { Socket } from './socket.js';
4
+
5
+ /**
6
+ * Once opened, an implementation of a TransportListener will emit `new_socket` events as new clients connect.
7
+ * Possible implementations could include MessageChannels or WebSockets.
8
+ */
9
+ export interface Listener extends EventEmitter {
10
+ // eslint-disable-next-line jsdoc/require-jsdoc
11
+ open(): void;
12
+ // eslint-disable-next-line jsdoc/require-jsdoc
13
+ close(): void;
14
+ // eslint-disable-next-line jsdoc/require-jsdoc
15
+ on(name: 'new_socket', cb: (client: Socket) => void): this;
16
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Represents one end of a socket connection.
3
+ * A message sent via `send` will be handled by the corresponding Socket's handler function at the other end.
4
+ * Implementations could use e.g. MessagePorts for communication between browser workers,
5
+ * or WebSockets for communication between processes.
6
+ * If `registerHandler` callback receives `undefined` that signals the other end closed.
7
+ */
8
+ export interface Socket {
9
+ // eslint-disable-next-line jsdoc/require-jsdoc
10
+ send(msg: any, transfer?: Transferable[]): Promise<void>;
11
+ // eslint-disable-next-line jsdoc/require-jsdoc
12
+ registerHandler(cb: (msg: any) => any): void;
13
+ // eslint-disable-next-line jsdoc/require-jsdoc
14
+ close(): void;
15
+ }
@@ -0,0 +1,125 @@
1
+ const $transferable = Symbol('thread.transferable');
2
+
3
+ /**
4
+ * Represents a descriptor for transferable objects in multi-threaded environments.
5
+ * Provides a structure for marking certain objects as transferable and managing the ownership transfer
6
+ * between threads, particularly useful when working with Web Workers.
7
+ */
8
+ export interface TransferDescriptor<T = any> {
9
+ /**
10
+ * A unique symbol indicating that an object is a TransferDescriptor.
11
+ */
12
+ [$transferable]: true;
13
+ /**
14
+ * The transferable data to be sent between threads.
15
+ */
16
+ send: T;
17
+ /**
18
+ * An array of objects that can be transferred between threads without serialization and deserialization.
19
+ */
20
+ transferables: Transferable[];
21
+ }
22
+
23
+ /**
24
+ * Determines if the provided object is transferable.
25
+ * Transferable objects are instances of a certain set of classes,
26
+ * such as ArrayBuffer or MessagePort, which can be transferred between
27
+ * different execution contexts (e.g., workers) without incurring the
28
+ * overhead of serialization and deserialization.
29
+ *
30
+ * This function checks for the basic transferable criteria, but does not
31
+ * perform an exhaustive check for all possible transferable types. As new
32
+ * transferable types are added to JavaScript, they may be supported without
33
+ * needing to modify this function.
34
+ *
35
+ * @param thing - The object to check for transferability.
36
+ * @returns A boolean indicating whether the object is transferable.
37
+ */
38
+ function isTransferable(thing: any): thing is Transferable {
39
+ if (!thing || typeof thing !== 'object') {
40
+ return false;
41
+ }
42
+ // Don't check too thoroughly, since the list of transferable things in JS might grow over time
43
+ return true;
44
+ }
45
+
46
+ /**
47
+ * Determines whether a given object is a TransferDescriptor.
48
+ * A TransferDescriptor is an object with a [$transferable] property set to true and used for
49
+ * transferring ownership of transferable objects between threads.
50
+ * This function checks if the input object has the required properties to be considered
51
+ * a valid TransferDescriptor.
52
+ *
53
+ * @param thing - The object to be checked for being a TransferDescriptor.
54
+ * @returns True if the object is a TransferDescriptor, false otherwise.
55
+ */
56
+ export function isTransferDescriptor(thing: any): thing is TransferDescriptor {
57
+ return thing && typeof thing === 'object' && thing[$transferable];
58
+ }
59
+
60
+ /**
61
+ * Mark a transferable object as such, so it will no be serialized and
62
+ * deserialized on messaging with the main thread, but to transfer
63
+ * ownership of it to the receiving thread.
64
+ *
65
+ * Only works with array buffers, message ports and few more special
66
+ * types of objects, but it's much faster than serializing and
67
+ * deserializing them.
68
+ *
69
+ * Note:
70
+ * The transferable object cannot be accessed by this thread again
71
+ * unless the receiving thread transfers it back again!
72
+ *
73
+ * @param transferable - Array buffer, message port or similar.
74
+ * @see https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast
75
+ */
76
+ export function Transfer<T>(transferable: Transferable): TransferDescriptor<T>;
77
+
78
+ /**
79
+ * Mark transferable objects within an arbitrary object or array as
80
+ * being a transferable object. They will then not be serialized
81
+ * and deserialized on messaging with the main thread, but ownership
82
+ * of them will be transferred to the receiving thread.
83
+ *
84
+ * Only array buffers, message ports and few more special types of
85
+ * objects can be transferred, but it's much faster than serializing and
86
+ * deserializing them.
87
+ *
88
+ * Note:
89
+ * The transferable object cannot be accessed by this thread again
90
+ * unless the receiving thread transfers it back again!
91
+ *
92
+ * @param transferable - Array buffer, message port or similar.
93
+ * @see https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast
94
+ */
95
+ export function Transfer<T>(payload: T, transferables: Transferable[]): TransferDescriptor<T>;
96
+
97
+ /**
98
+ * Create a TransferDescriptor for transferable objects within an arbitrary object or array, allowing
99
+ * them to be transferred between threads instead of being serialized and deserialized.
100
+ * This method is particularly useful when working with Web Workers and other multi-threaded environments.
101
+ * Transferable objects include ArrayBuffers, MessagePorts, and a few other special types.
102
+ * Note that after transferring, the original thread will lose access to the transferred object unless
103
+ * it's transferred back again.
104
+ *
105
+ * @param payload - The transferable object or an object containing transferable properties.
106
+ * @param transferables - Optional array of Transferable objects found in the payload. If not provided,
107
+ * the payload itself should be a Transferable object.
108
+ * @returns A TransferDescriptor<T> containing the payload and transferables, marked as transferable.
109
+ * @throws Error if payload is not transferable and transferables array is not provided.
110
+ * @see https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast
111
+ */
112
+ export function Transfer<T>(payload: T, transferables?: Transferable[]): TransferDescriptor<T> {
113
+ if (!transferables) {
114
+ if (!isTransferable(payload)) {
115
+ throw Error();
116
+ }
117
+ transferables = [payload];
118
+ }
119
+
120
+ return {
121
+ [$transferable]: true,
122
+ send: payload,
123
+ transferables,
124
+ };
125
+ }
@@ -0,0 +1,2 @@
1
+ export * from './node_connector.js';
2
+ export * from './node_listener.js';
@@ -0,0 +1,30 @@
1
+ import { Worker } from 'worker_threads';
2
+
3
+ import { Connector } from '../interface/connector.js';
4
+ import { NodeConnectorSocket } from './node_connector_socket.js';
5
+
6
+ /**
7
+ * The NodeConnector class is a concrete implementation of the Connector interface, utilizing worker_threads for
8
+ * efficient parallel execution. This class provides an easy way to establish a connection with a Worker instance,
9
+ * allowing seamless communication via sockets.
10
+ *
11
+ * @example
12
+ * const worker = new Worker('./path/to/worker.js');
13
+ * const nodeConnector = new NodeConnector(worker);
14
+ * const socket = await nodeConnector.createSocket();
15
+ * socket.send('Hello from main thread!');
16
+ */
17
+ export class NodeConnector implements Connector {
18
+ constructor(private worker: Worker) {}
19
+
20
+ /**
21
+ * Creates a new instance of NodeConnectorSocket using the worker provided in the constructor.
22
+ * The createSocket method is used to establish connections using the worker_threads module,
23
+ * allowing for efficient and fast communication between different parts of the application.
24
+ *
25
+ * @returns A Promise that resolves to a newly created NodeConnectorSocket instance.
26
+ */
27
+ createSocket() {
28
+ return Promise.resolve(new NodeConnectorSocket(this.worker));
29
+ }
30
+ }
@@ -0,0 +1,52 @@
1
+ import { TransferListItem, Worker } from 'worker_threads';
2
+
3
+ import { Socket } from '../interface/socket.js';
4
+
5
+ /**
6
+ * NodeConnectorSocket is a wrapper class that implements the Socket interface for messaging between
7
+ * the main thread and worker threads in a Node.js environment. It uses the Worker API for
8
+ * communication by sending and receiving messages through postMessage and handling messages using
9
+ * event listeners.
10
+ *
11
+ * The send method sends messages to the worker thread, and the registerHandler method registers a
12
+ * callback function to handle incoming messages from the worker. The close method cleans up
13
+ * resources when the socket is no longer needed.
14
+ */
15
+ export class NodeConnectorSocket implements Socket {
16
+ constructor(private worker: Worker) {}
17
+
18
+ /**
19
+ * Sends a message from the NodeConnectorSocket instance to the associated worker thread.
20
+ * The 'msg' can be any data type and 'transfer' is an optional array of transferable objects
21
+ * that can be transferred with zero-copy semantics. The function returns a resolved Promise
22
+ * once the message has been posted.
23
+ *
24
+ * @param msg - The message to send to the worker thread.
25
+ * @param transfer - Optional array of Transferable objects to transfer ownership alongside the message.
26
+ * @returns A Promise that resolves when the message has been posted.
27
+ */
28
+ send(msg: any, transfer: Transferable[] = []): Promise<void> {
29
+ this.worker.postMessage(msg, transfer as TransferListItem[]);
30
+ return Promise.resolve();
31
+ }
32
+
33
+ /**
34
+ * Registers a callback function to handle incoming messages from the worker.
35
+ * The provided callback will be executed whenever a message is received from
36
+ * the worker, passing the message as its single argument.
37
+ *
38
+ * @param cb - The callback function to be called when a message is received.
39
+ */
40
+ registerHandler(cb: (msg: any) => any): void {
41
+ this.worker.on('message', cb);
42
+ }
43
+
44
+ /**
45
+ * Closes the worker connection and removes all event listeners.
46
+ * Sends an undefined message to the worker for graceful termination.
47
+ */
48
+ close() {
49
+ void this.send(undefined);
50
+ this.worker.removeAllListeners();
51
+ }
52
+ }
@@ -0,0 +1,34 @@
1
+ import EventEmitter from 'events';
2
+ import { parentPort } from 'worker_threads';
3
+
4
+ import { Listener } from '../interface/listener.js';
5
+ import { NodeListenerSocket } from './node_listener_socket.js';
6
+
7
+ /**
8
+ * NodeListener is an event-driven class that extends EventEmitter and implements the Listener interface.
9
+ * It provides methods to open and close communication with a worker thread using the NodeListenerSocket.
10
+ * The 'new_socket' event is emitted when a new NodeListenerSocket instance is created, allowing for
11
+ * efficient processing of incoming messages from the parent thread.
12
+ */
13
+ export class NodeListener extends EventEmitter implements Listener {
14
+ constructor() {
15
+ super();
16
+ }
17
+
18
+ /**
19
+ * Opens a new connection to a parent worker thread and emits an event with the created NodeListenerSocket instance.
20
+ * The 'new_socket' event can be listened for, providing access to the newly created NodeListenerSocket.
21
+ *
22
+ * Fires NodeListener#new_socket.
23
+ */
24
+ open() {
25
+ this.emit('new_socket', new NodeListenerSocket(parentPort as any));
26
+ }
27
+
28
+ /**
29
+ * Closes the NodeListener instance.
30
+ * This method currently has no implementation, as there is no need to perform any actions
31
+ * when closing a NodeListener. It exists for compatibility with the Listener interface.
32
+ */
33
+ close() {}
34
+ }
@@ -0,0 +1,48 @@
1
+ import { MessagePort, TransferListItem } from 'worker_threads';
2
+
3
+ import { Socket } from '../interface/socket.js';
4
+
5
+ /**
6
+ * An implementation of a TransportSocket using MessagePorts.
7
+ */
8
+ export class NodeListenerSocket implements Socket {
9
+ constructor(private port: MessagePort) {}
10
+
11
+ /**
12
+ * Sends a message through the MessagePort along with any provided Transferables.
13
+ * The transfer list allows for efficient sending of certain types of data,
14
+ * such as ArrayBuffer, ImageBitmap, and MessagePort.
15
+ * The Promise resolves once the message has been successfully sent.
16
+ *
17
+ * @param msg - The message to be sent through the MessagePort.
18
+ * @param transfer - An optional array of Transferable objects to be transferred.
19
+ * @returns A Promise that resolves once the message has been sent.
20
+ */
21
+ send(msg: any, transfer: Transferable[] = []): Promise<void> {
22
+ this.port.postMessage(msg, transfer as TransferListItem[]);
23
+ return Promise.resolve();
24
+ }
25
+
26
+ /**
27
+ * Registers a callback function to handle incoming messages from the MessagePort.
28
+ * When a message is received, the provided callback function will be invoked with
29
+ * the received message as its argument. This method allows for efficient and
30
+ * dynamic handling of incoming data in a NodeListenerSocket instance.
31
+ *
32
+ * @param cb - The callback function to process incoming messages.
33
+ */
34
+ registerHandler(cb: (msg: any) => any): void {
35
+ this.port.on('message', cb);
36
+ }
37
+
38
+ /**
39
+ * Closes the NodeListenerSocket instance, removing all listeners and closing the underlying MessagePort.
40
+ * Sends an undefined message to notify any connected ports about the closure before removing event listeners
41
+ * and cleaning up resources. This method should be called when the socket is no longer needed to avoid memory leaks.
42
+ */
43
+ close() {
44
+ void this.send(undefined);
45
+ this.port.removeAllListeners();
46
+ this.port.close();
47
+ }
48
+ }
@@ -0,0 +1,131 @@
1
+ import EventEmitter from 'events';
2
+ import { format } from 'util';
3
+
4
+ import { createDebugLogger } from '../log/index.js';
5
+ import { EventMessage, ResponseMessage, isEventMessage } from './dispatch/messages.js';
6
+ import { Connector } from './interface/connector.js';
7
+ import { Socket } from './interface/socket.js';
8
+
9
+ const debug = createDebugLogger('aztec:transport_client');
10
+
11
+ /**
12
+ * Represents a pending request in the TransportClient.
13
+ * Contains information about the message ID, and resolve/reject functions for handling responses.
14
+ * Used to track and manage asynchronous request/response communication with the TransportServer.
15
+ */
16
+ interface PendingRequest {
17
+ /**
18
+ * The unique message identifier used for tracking and matching request/response pairs.
19
+ */
20
+ msgId: number;
21
+ // eslint-disable-next-line jsdoc/require-jsdoc
22
+ resolve(data: any): void;
23
+ // eslint-disable-next-line jsdoc/require-jsdoc
24
+ reject(error: Error): void;
25
+ }
26
+
27
+ /**
28
+ * Represents a transport client for communication between TransportServer and clients.
29
+ * Provides request/response functionality, event handling, and multiplexing support
30
+ * for efficient and concurrent communication with a corresponding TransportServer.
31
+ */
32
+ export interface ITransportClient<Payload> extends EventEmitter {
33
+ // eslint-disable-next-line jsdoc/require-jsdoc
34
+ on(name: 'event_msg', handler: (payload: Payload) => void): this;
35
+ // eslint-disable-next-line jsdoc/require-jsdoc
36
+ emit(name: 'event_msg', payload: Payload): boolean;
37
+ }
38
+
39
+ /**
40
+ * A TransportClient provides a request/response and event api to a corresponding TransportServer.
41
+ * If `broadcast` is called on TransportServer, TransportClients will emit an `event_msg`.
42
+ * The `request` method will block until a response is returned from the TransportServer's dispatch function.
43
+ * Request multiplexing is supported.
44
+ */
45
+ export class TransportClient<Payload> extends EventEmitter {
46
+ private msgId = 0;
47
+ private pendingRequests: PendingRequest[] = [];
48
+ private socket?: Socket;
49
+
50
+ constructor(private transportConnect: Connector) {
51
+ super();
52
+ }
53
+
54
+ /**
55
+ * Initializes and opens the socket connection for the TransportClient.
56
+ * This method creates a new Socket instance using the provided Connector,
57
+ * registers a handler for incoming messages, and establishes the connection.
58
+ * It should be called before making any requests or handling events.
59
+ *
60
+ * @throws An error if the socket is already open or there's an issue opening the connection.
61
+ * @returns A Promise that resolves when the socket connection is successfully opened.
62
+ */
63
+ async open() {
64
+ this.socket = await this.transportConnect.createSocket();
65
+ this.socket.registerHandler(msg => this.handleSocketMessage(msg));
66
+ }
67
+
68
+ /**
69
+ * Close the transport client's socket connection and remove all event listeners.
70
+ * This method should be called when the client is no longer needed to ensure proper cleanup
71
+ * and prevent potential memory leaks. Once closed, the client cannot be reused and a new
72
+ * instance must be created if another connection is needed.
73
+ */
74
+ close() {
75
+ this.socket?.close();
76
+ this.socket = undefined;
77
+ this.removeAllListeners();
78
+ }
79
+
80
+ /**
81
+ * Sends a request to the TransportServer with the given payload and transferable objects.
82
+ * The method will block until a response from the TransportServer's dispatch function is returned.
83
+ * Request multiplexing is supported, allowing multiple requests to be sent concurrently.
84
+ *
85
+ * @param payload - The message payload to send to the server.
86
+ * @param transfer - An optional array of ArrayBuffer, MessagePort, or ImageBitmap objects to transfer ownership.
87
+ * @returns A Promise that resolves with the server's response data or rejects with an error message.
88
+ */
89
+ request(payload: Payload, transfer?: Transferable[]) {
90
+ if (!this.socket) {
91
+ throw new Error('Socket not open.');
92
+ }
93
+ const msgId = this.msgId++;
94
+ const msg = { msgId, payload };
95
+ debug(format(`->`, msg));
96
+ return new Promise<any>((resolve, reject) => {
97
+ this.pendingRequests.push({ resolve, reject, msgId });
98
+ this.socket!.send(msg, transfer).catch(reject);
99
+ });
100
+ }
101
+
102
+ /**
103
+ * Handles incoming socket messages from the TransportServer, such as ResponseMessage and EventMessage.
104
+ * If it's an EventMessage, emits an 'event_msg' event with the payload.
105
+ * If it's a ResponseMessage, resolves or rejects the corresponding pending request based on the message content.
106
+ *
107
+ * @param msg - The ResponseMessage or EventMessage received from the TransportServer, or undefined if the remote socket closed.
108
+ */
109
+ private handleSocketMessage(msg: ResponseMessage<Payload> | EventMessage<Payload> | undefined) {
110
+ if (msg === undefined) {
111
+ // The remote socket closed.
112
+ this.close();
113
+ return;
114
+ }
115
+ debug(format(`<-`, msg));
116
+ if (isEventMessage(msg)) {
117
+ this.emit('event_msg', msg.payload);
118
+ return;
119
+ }
120
+ const reqIndex = this.pendingRequests.findIndex(r => r.msgId === msg.msgId);
121
+ if (reqIndex === -1) {
122
+ return;
123
+ }
124
+ const [pending] = this.pendingRequests.splice(reqIndex, 1);
125
+ if (msg.error) {
126
+ pending.reject(new Error(msg.error));
127
+ } else {
128
+ pending.resolve(msg.payload);
129
+ }
130
+ }
131
+ }
@@ -0,0 +1,108 @@
1
+ import { RequestMessage, ResponseMessage } from './dispatch/messages.js';
2
+ import { Listener } from './interface/listener.js';
3
+ import { Socket } from './interface/socket.js';
4
+ import { isTransferDescriptor } from './interface/transferable.js';
5
+
6
+ /**
7
+ * Keeps track of clients, providing a broadcast, and request/response api with multiplexing.
8
+ */
9
+ export class TransportServer<Payload> {
10
+ private sockets: Socket[] = [];
11
+
12
+ constructor(private listener: Listener, private msgHandlerFn: (msg: Payload) => Promise<any>) {}
13
+
14
+ /**
15
+ * Starts the TransportServer, allowing it to accept new connections and handle incoming messages.
16
+ * The server will listen for 'new_socket' events from the underlying listener and invoke the provided message handler function
17
+ * for each received message. The server remains active until the 'stop' method is called.
18
+ */
19
+ start() {
20
+ this.listener.on('new_socket', client => this.handleNewSocket(client));
21
+ this.listener.open();
22
+ }
23
+
24
+ /**
25
+ * Stops accepting new connections. It doesn't close existing sockets.
26
+ * It's expected the clients will gracefully complete by closing their end, sending an `undefined` message.
27
+ */
28
+ stop() {
29
+ this.listener.close();
30
+ }
31
+
32
+ /**
33
+ * Sends a broadcast message to all connected clients.
34
+ * The given payload will be sent to all the clients currently connected to the TransportServer.
35
+ * It waits for all the messages to be sent and resolves when they are all sent successfully.
36
+ *
37
+ * @param msg - The payload to broadcast to all connected clients.
38
+ * @returns A Promise that resolves when all messages have been sent successfully.
39
+ */
40
+ async broadcast(msg: Payload) {
41
+ await Promise.all(this.sockets.map(s => s.send({ payload: msg })));
42
+ }
43
+
44
+ /**
45
+ * Handles the addition of a new socket to the server by registering a message handler for the client
46
+ * and adding the socket to the list of active sockets. The message handler processes incoming messages
47
+ * from the client, including detecting client disconnection and removing the closed socket.
48
+ *
49
+ * @param socket - The new Socket instance that has connected to the server.
50
+ */
51
+ private handleNewSocket(socket: Socket) {
52
+ socket.registerHandler(async msg => {
53
+ if (msg === undefined) {
54
+ // Client socket has closed. Remove it from the list of sockets. Call close on it for any cleanup.
55
+ const socketIndex = this.sockets.findIndex(s => s === socket);
56
+ const [closingSocket] = this.sockets.splice(socketIndex, 1);
57
+ closingSocket.close();
58
+ return;
59
+ }
60
+ return await this.handleSocketMessage(socket, msg);
61
+ });
62
+ this.sockets.push(socket);
63
+ }
64
+
65
+ /**
66
+ * Detect the 'transferables' argument to our socket from our message
67
+ * handler return type.
68
+ * @param data - The compound payload data.
69
+ * @returns The split data and transferables.
70
+ */
71
+ private getPayloadAndTransfers(data: any): [any, Transferable[]] {
72
+ if (isTransferDescriptor(data)) {
73
+ // We treat PayloadWithTransfers specially so that we're able to
74
+ // attach transferables while keeping a simple return-type based usage
75
+ return [data.send, data.transferables];
76
+ }
77
+ if (data instanceof Uint8Array) {
78
+ // We may want to devise a better solution to this. We maybe given a view over a non cloneable/transferrable
79
+ // ArrayBuffer (such as a view over wasm memory). In this case we want to take a copy, and then transfer it.
80
+ const respPayload = data instanceof Uint8Array && ArrayBuffer.isView(data) ? new Uint8Array(data) : data;
81
+ const transferables = data instanceof Uint8Array ? [respPayload.buffer] : [];
82
+ return [respPayload, transferables];
83
+ }
84
+ return [data, []];
85
+ }
86
+ /**
87
+ * Handles incoming socket messages, processing the request and sending back a response.
88
+ * This function is responsible for invoking the registered message handler function with the received
89
+ * payload, extracting the result and transferables, and sending a response message back to the client.
90
+ * In case of an error during message handling, it sends an error response with the stack trace.
91
+ *
92
+ * @param socket - The Socket instance from which the message was received.
93
+ * @param msg - The RequestMessage object containing the message ID and payload.
94
+ */
95
+ private async handleSocketMessage(socket: Socket, { msgId, payload }: RequestMessage<Payload>) {
96
+ try {
97
+ const data = await this.msgHandlerFn(payload);
98
+
99
+ const [respPayload, transferables] = this.getPayloadAndTransfers(data);
100
+ const rep: ResponseMessage<Payload> = { msgId, payload: respPayload };
101
+
102
+ await socket.send(rep, transferables);
103
+ } catch (err: any) {
104
+ const rep: ResponseMessage<Payload> = { msgId, error: err.stack };
105
+ await socket.send(rep);
106
+ }
107
+ }
108
+ }