@aztec/foundation 0.16.4 → 0.16.5

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 (121) hide show
  1. package/dest/abi/function_selector.js +2 -2
  2. package/dest/aztec-address/index.js +2 -2
  3. package/dest/json-rpc/server/index.d.ts +1 -1
  4. package/dest/json-rpc/server/index.d.ts.map +1 -1
  5. package/dest/json-rpc/server/index.js +2 -2
  6. package/dest/json-rpc/server/json_rpc_server.d.ts +7 -0
  7. package/dest/json-rpc/server/json_rpc_server.d.ts.map +1 -1
  8. package/dest/json-rpc/server/json_rpc_server.js +13 -1
  9. package/package.json +2 -2
  10. package/src/abi/abi.ts +0 -310
  11. package/src/abi/abi_coder.ts +0 -25
  12. package/src/abi/decoder.ts +0 -161
  13. package/src/abi/encoder.ts +0 -120
  14. package/src/abi/function_selector.ts +0 -135
  15. package/src/abi/index.ts +0 -6
  16. package/src/abi/utils.ts +0 -28
  17. package/src/async-map/index.ts +0 -18
  18. package/src/aztec-address/index.ts +0 -30
  19. package/src/bigint-buffer/index.ts +0 -74
  20. package/src/collection/array.ts +0 -54
  21. package/src/collection/index.ts +0 -1
  22. package/src/committable/committable.ts +0 -46
  23. package/src/committable/index.ts +0 -1
  24. package/src/crypto/index.ts +0 -16
  25. package/src/crypto/keccak/index.ts +0 -33
  26. package/src/crypto/pedersen/index.ts +0 -1
  27. package/src/crypto/pedersen/pedersen.elliptic.ts +0 -584
  28. package/src/crypto/pedersen/pedersen.noble.ts +0 -573
  29. package/src/crypto/pedersen/pedersen.wasm.ts +0 -42
  30. package/src/crypto/random/index.ts +0 -42
  31. package/src/crypto/sha256/index.ts +0 -3
  32. package/src/errors/index.ts +0 -6
  33. package/src/eth-address/index.ts +0 -244
  34. package/src/fields/coordinate.ts +0 -104
  35. package/src/fields/fields.ts +0 -261
  36. package/src/fields/index.ts +0 -3
  37. package/src/fields/point.ts +0 -140
  38. package/src/fifo/bounded_serial_queue.ts +0 -100
  39. package/src/fifo/index.ts +0 -4
  40. package/src/fifo/memory_fifo.ts +0 -114
  41. package/src/fifo/semaphore.ts +0 -33
  42. package/src/fifo/serial_queue.ts +0 -78
  43. package/src/index.ts +0 -29
  44. package/src/json-rpc/README.md +0 -55
  45. package/src/json-rpc/class_converter.ts +0 -209
  46. package/src/json-rpc/client/index.ts +0 -1
  47. package/src/json-rpc/client/json_rpc_client.ts +0 -136
  48. package/src/json-rpc/convert.ts +0 -163
  49. package/src/json-rpc/fixtures/class_a.ts +0 -15
  50. package/src/json-rpc/fixtures/class_b.ts +0 -15
  51. package/src/json-rpc/fixtures/test_state.ts +0 -59
  52. package/src/json-rpc/index.ts +0 -8
  53. package/src/json-rpc/js_utils.ts +0 -20
  54. package/src/json-rpc/server/index.ts +0 -2
  55. package/src/json-rpc/server/json_proxy.ts +0 -43
  56. package/src/json-rpc/server/json_rpc_server.ts +0 -190
  57. package/src/log/console.ts +0 -39
  58. package/src/log/debug.ts +0 -83
  59. package/src/log/index.ts +0 -5
  60. package/src/log/log_fn.ts +0 -5
  61. package/src/log/log_history.ts +0 -44
  62. package/src/log/logger.ts +0 -137
  63. package/src/mutex/index.ts +0 -83
  64. package/src/mutex/mutex_database.ts +0 -12
  65. package/src/noir/index.ts +0 -1
  66. package/src/noir/noir_package_config.ts +0 -54
  67. package/src/retry/index.ts +0 -99
  68. package/src/running-promise/index.ts +0 -60
  69. package/src/serialize/buffer_reader.ts +0 -250
  70. package/src/serialize/free_funcs.ts +0 -279
  71. package/src/serialize/index.ts +0 -3
  72. package/src/serialize/types.ts +0 -40
  73. package/src/sleep/index.ts +0 -71
  74. package/src/testing/index.ts +0 -1
  75. package/src/testing/test_data.ts +0 -36
  76. package/src/timer/elapsed.ts +0 -23
  77. package/src/timer/index.ts +0 -3
  78. package/src/timer/timeout.ts +0 -64
  79. package/src/timer/timer.ts +0 -48
  80. package/src/transport/browser/index.ts +0 -4
  81. package/src/transport/browser/message_port_socket.ts +0 -48
  82. package/src/transport/browser/shared_worker_connector.ts +0 -21
  83. package/src/transport/browser/shared_worker_listener.ts +0 -53
  84. package/src/transport/browser/worker_connector.ts +0 -30
  85. package/src/transport/browser/worker_listener.ts +0 -54
  86. package/src/transport/dispatch/create_dispatch_fn.ts +0 -35
  87. package/src/transport/dispatch/create_dispatch_proxy.ts +0 -141
  88. package/src/transport/dispatch/messages.ts +0 -58
  89. package/src/transport/index.ts +0 -11
  90. package/src/transport/interface/connector.ts +0 -9
  91. package/src/transport/interface/listener.ts +0 -16
  92. package/src/transport/interface/socket.ts +0 -15
  93. package/src/transport/interface/transferable.ts +0 -125
  94. package/src/transport/node/index.ts +0 -2
  95. package/src/transport/node/node_connector.ts +0 -30
  96. package/src/transport/node/node_connector_socket.ts +0 -52
  97. package/src/transport/node/node_listener.ts +0 -34
  98. package/src/transport/node/node_listener_socket.ts +0 -48
  99. package/src/transport/transport_client.ts +0 -131
  100. package/src/transport/transport_server.ts +0 -108
  101. package/src/trees/index.ts +0 -48
  102. package/src/types/index.ts +0 -7
  103. package/src/url/index.ts +0 -73
  104. package/src/wasm/README.md +0 -6
  105. package/src/wasm/empty_wasi_sdk.ts +0 -166
  106. package/src/wasm/fixtures/gcd.wasm +0 -0
  107. package/src/wasm/fixtures/gcd.wat +0 -27
  108. package/src/wasm/index.ts +0 -1
  109. package/src/wasm/wasm_module.ts +0 -260
  110. package/src/worker/browser/index.ts +0 -2
  111. package/src/worker/browser/start_web_module.ts +0 -23
  112. package/src/worker/browser/web_data_store.ts +0 -38
  113. package/src/worker/browser/web_worker.ts +0 -24
  114. package/src/worker/data_store.ts +0 -19
  115. package/src/worker/index.ts +0 -2
  116. package/src/worker/node/index.ts +0 -2
  117. package/src/worker/node/node_data_store.ts +0 -27
  118. package/src/worker/node/node_worker.ts +0 -22
  119. package/src/worker/node/start_node_module.ts +0 -29
  120. package/src/worker/wasm_worker.ts +0 -7
  121. package/src/worker/worker_pool.ts +0 -73
@@ -1,141 +0,0 @@
1
- import { EventEmitter } from 'events';
2
-
3
- import { TransferDescriptor, isTransferDescriptor } from '../interface/transferable.js';
4
- import { TransportClient } from '../transport_client.js';
5
- import { DispatchMsg } from './create_dispatch_fn.js';
6
-
7
- /**
8
- * FilterOutAttributes type filters out all non-method properties of an object, leaving only the attributes
9
- * that are functions. This is useful for creating proxies or wrappers around objects while focusing only
10
- * on their methods and ignoring other properties.
11
- */
12
- type FilterOutAttributes<Base> = {
13
- [Key in keyof Base]: Base[Key] extends (...any: any) => any ? Base[Key] : never;
14
- };
15
-
16
- /**
17
- * Takes a function type `F` and returns a new function type with the same input parameters as `F`,
18
- * but returning a Promise of the original return type of `F`. This is useful for converting sync
19
- * functions or functions that take callbacks into a version that returns a Promise.
20
- */
21
- type PromisifyFunction<F extends (...any: any) => any> = (...args: Parameters<F>) => Promise<ReturnType<F>>;
22
-
23
- /**
24
- * Transforms the provided object type by converting each of its function properties into their
25
- * promise-returning counterparts. If a function property already returns a promise, it remains unchanged.
26
- * This is useful when wrapping synchronous methods to return promises in order to standardize the API for
27
- * asynchronous operations.
28
- *
29
- * @typeParam Base - The type of the object whose function properties need to be converted into their
30
- * promise-returning versions.
31
- */
32
- type Promisify<Base extends { [key: string]: (...any: any) => any }> = {
33
- [Key in keyof Base]: ReturnType<Base[Key]> extends Promise<any> ? Base[Key] : PromisifyFunction<Base[Key]>;
34
- };
35
-
36
- /**
37
- * Type that transforms a tuple of types, replacing each type 'T' with either 'T' or a `TransferDescriptor<T>` if 'T' is `Transferable`.
38
- * This is useful for handling arguments of functions that may accept both original and transferable representations of objects.
39
- */
40
- type TransferTypes<Tuple extends [...args: any]> = {
41
- [Index in keyof Tuple]: Tuple[Index] | (Tuple[Index] extends Transferable ? TransferDescriptor<Tuple[Index]> : never);
42
- };
43
-
44
- /**
45
- * Annoying.
46
- * @see https://github.com/microsoft/TypeScript/issues/29919
47
- * There's a bug that means we can't map over the tuple or function parameter types to make them transferrable, if
48
- * we use the Parameters builtin, and then try to map.
49
- * So instead we inline the Parameters builtin and apply the TransferTypes to the parameters within the inline.
50
- * Once the above is fixed we could in theory just do:
51
- *
52
- * type MakeFunctionTransferrable\<TFunction extends (...args: any) =\> any\> = (
53
- * ...args: TransferTypes\<Parameters\<TFunction\>\>
54
- * ) =\> ReturnType<TFunction>;
55
- */
56
- type MakeFunctionTransferrable<TFunction extends (...args: any) => any> = (
57
- ...args: TFunction extends (...args: infer P) => any ? TransferTypes<P> : never
58
- ) => ReturnType<TFunction>;
59
-
60
- /**
61
- * Transferrable type represents a utility type that maps over the provided Base object's methods,
62
- * transforming their argument types to support transferable objects. This is useful when dealing
63
- * with operations across different environments or threads, such as Web Workers or Node.js processes,
64
- * where certain objects need to be transferred instead of being serialized and deserialized.
65
- */
66
- type Transferrable<Base extends { [key: string]: (...any: any) => any }> = {
67
- [Key in keyof Base]: MakeFunctionTransferrable<Base[Key]>;
68
- };
69
-
70
- /**
71
- * Proxify is a mapped type that takes an object with functions as its properties and returns
72
- * a new object with the same properties, but with each function transformed to return a Promise
73
- * and accept Transferable types in place of their original parameters. This type is useful for
74
- * creating proxies that communicate over different environments or threads while maintaining
75
- * the original class's method signatures, allowing for type-safe interaction with remote instances.
76
- */
77
- export type Proxify<T> = Promisify<Transferrable<FilterOutAttributes<T>>>;
78
-
79
- /**
80
- * Creates a proxy object for the provided class, wrapping each method in a request function.
81
- * The resulting proxy object allows invoking methods of the original class, but their execution
82
- * is delegated to the request function. This is useful when executing methods across different
83
- * environments or threads, such as Web Workers or Node.js processes.
84
- *
85
- * @typeParam T - The type of the class to create a proxy for.
86
- * @param class_ - The class constructor to create a proxy for.
87
- * @param requestFn - A higher-order function that takes a method name and returns a function
88
- * with the same signature as the original method, which wraps the actual
89
- * invocation in a custom logic (e.g., sending a message to another thread).
90
- * @returns An object whose methods match those of the original class, but whose execution is
91
- * delegated to the provided request function.
92
- */
93
- export function createDispatchProxyFromFn<T>(
94
- class_: { new (...args: any[]): T },
95
- requestFn: (fn: string) => (...args: any[]) => Promise<any>,
96
- ): Proxify<T> {
97
- const proxy: any = class_.prototype instanceof EventEmitter ? new EventEmitter() : {};
98
- for (const fn of Object.getOwnPropertyNames(class_.prototype)) {
99
- if (fn === 'constructor') {
100
- continue;
101
- }
102
- proxy[fn] = requestFn(fn);
103
- }
104
- return proxy;
105
- }
106
-
107
- /**
108
- * Creates a proxy for the given class that transparently dispatches method calls over a transport client.
109
- * The proxy allows calling methods on remote instances of the class through the provided transport client
110
- * while maintaining the correct return types and handling promises. If the class inherits from EventEmitter,
111
- * it also handles event emissions through the transport client.
112
- *
113
- * @param class_ - The constructor function of the class to create the proxy for.
114
- * @param transportClient - The TransportClient instance used to handle communication between proxies.
115
- * @returns A proxified version of the given class with methods dispatched over the transport client.
116
- */
117
- export function createDispatchProxy<T>(
118
- class_: { new (...args: any[]): T },
119
- transportClient: TransportClient<DispatchMsg>,
120
- ): Proxify<T> {
121
- // Create a proxy of class_ that passes along methods over our transportClient
122
- const proxy = createDispatchProxyFromFn(class_, (fn: string) => (...args: any[]) => {
123
- // Pass our proxied function name and arguments over our transport client
124
- const transfer: Transferable[] = args.reduce(
125
- (acc, a) => (isTransferDescriptor(a) ? [...acc, ...a.transferables] : acc),
126
- [] as Transferable[],
127
- );
128
- args = args.map(a => (isTransferDescriptor(a) ? a.send : a));
129
- return transportClient.request({ fn, args }, transfer);
130
- });
131
- if (proxy instanceof EventEmitter) {
132
- // Handle proxied 'emit' calls if our proxy object is an EventEmitter
133
- transportClient.on('event_msg', ({ fn, args }) => {
134
- if (fn === 'emit') {
135
- const [eventName, ...restArgs] = args;
136
- proxy.emit(eventName, ...restArgs);
137
- }
138
- });
139
- }
140
- return proxy;
141
- }
@@ -1,58 +0,0 @@
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
- }
@@ -1,11 +0,0 @@
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';
@@ -1,9 +0,0 @@
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
- }
@@ -1,16 +0,0 @@
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
- }
@@ -1,15 +0,0 @@
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
- }
@@ -1,125 +0,0 @@
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
- }
@@ -1,2 +0,0 @@
1
- export * from './node_connector.js';
2
- export * from './node_listener.js';
@@ -1,30 +0,0 @@
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
- }
@@ -1,52 +0,0 @@
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
- }
@@ -1,34 +0,0 @@
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
- }
@@ -1,48 +0,0 @@
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
- }