sabcom 0.0.6 → 0.0.7
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/README.md +93 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,10 +11,12 @@ A TypeScript/Node.js library for inter-thread communication using SharedArrayBuf
|
|
|
11
11
|
## Features
|
|
12
12
|
|
|
13
13
|
- **Thread-safe communication** using atomic operations
|
|
14
|
+
- **Async and sync APIs** for different use cases
|
|
14
15
|
- **Chunked data transfer** for large payloads
|
|
15
16
|
- **V8 serialization** for complex data types
|
|
16
17
|
- **Timeout handling** with configurable timeouts
|
|
17
18
|
- **Zero-copy operations** where possible
|
|
19
|
+
- **Generator-based** low-level API for custom implementations
|
|
18
20
|
|
|
19
21
|
## Installation
|
|
20
22
|
|
|
@@ -24,7 +26,7 @@ npm install sabcom
|
|
|
24
26
|
|
|
25
27
|
## Usage
|
|
26
28
|
|
|
27
|
-
###
|
|
29
|
+
### Async Example
|
|
28
30
|
|
|
29
31
|
```typescript
|
|
30
32
|
import { write, read } from 'sabcom';
|
|
@@ -32,48 +34,122 @@ import { write, read } from 'sabcom';
|
|
|
32
34
|
// Create a shared buffer (1MB)
|
|
33
35
|
const buffer = new SharedArrayBuffer(1024 * 1024);
|
|
34
36
|
|
|
35
|
-
// Writer thread
|
|
37
|
+
// Writer thread (async)
|
|
36
38
|
const data = { message: 'Hello World', numbers: [1, 2, 3, 4, 5] };
|
|
37
|
-
write(data, buffer);
|
|
39
|
+
await write(data, buffer);
|
|
38
40
|
|
|
39
|
-
// Reader thread
|
|
40
|
-
const received = read(buffer);
|
|
41
|
+
// Reader thread (async)
|
|
42
|
+
const received = await read(buffer);
|
|
41
43
|
console.log(received); // { message: 'Hello World', numbers: [1, 2, 3, 4, 5] }
|
|
42
44
|
```
|
|
43
45
|
|
|
46
|
+
### Sync Example
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { writeSync, readSync } from 'sabcom';
|
|
50
|
+
|
|
51
|
+
// Writer thread (sync)
|
|
52
|
+
writeSync(data, buffer);
|
|
53
|
+
|
|
54
|
+
// Reader thread (sync)
|
|
55
|
+
const received = readSync(buffer);
|
|
56
|
+
```
|
|
57
|
+
|
|
44
58
|
### With Custom Timeout
|
|
45
59
|
|
|
46
60
|
```typescript
|
|
47
61
|
// 10 second timeout
|
|
48
|
-
write(data, buffer, 10000);
|
|
49
|
-
const received = read(buffer, 10000);
|
|
62
|
+
await write(data, buffer, { timeout: 10000 });
|
|
63
|
+
const received = await read(buffer, { timeout: 10000 });
|
|
64
|
+
|
|
65
|
+
// Sync with timeout
|
|
66
|
+
writeSync(data, buffer, { timeout: 10000 });
|
|
67
|
+
const received = readSync(buffer, { timeout: 10000 });
|
|
50
68
|
```
|
|
51
69
|
|
|
52
70
|
## API Reference
|
|
53
71
|
|
|
54
|
-
###
|
|
72
|
+
### Async Functions
|
|
73
|
+
|
|
74
|
+
#### `write(data: unknown, buffer: SharedArrayBuffer, options?: Options): Promise<void>`
|
|
75
|
+
|
|
76
|
+
Asynchronously writes data to the shared buffer using chunked transfer.
|
|
77
|
+
|
|
78
|
+
- `data` - Any serializable data
|
|
79
|
+
- `buffer` - SharedArrayBuffer for communication
|
|
80
|
+
- `options` - Optional configuration object
|
|
81
|
+
- `timeout` - Timeout in milliseconds (default: 5000)
|
|
82
|
+
|
|
83
|
+
**Throws:**
|
|
84
|
+
- `Error` - On handshake or chunk timeout
|
|
85
|
+
|
|
86
|
+
#### `read(buffer: SharedArrayBuffer, options?: Options): Promise<unknown>`
|
|
87
|
+
|
|
88
|
+
Asynchronously reads data from the shared buffer.
|
|
55
89
|
|
|
56
|
-
|
|
90
|
+
- `buffer` - SharedArrayBuffer for communication
|
|
91
|
+
- `options` - Optional configuration object
|
|
92
|
+
- `timeout` - Timeout in milliseconds (default: 5000)
|
|
93
|
+
|
|
94
|
+
**Returns:** Promise resolving to deserialized data
|
|
95
|
+
|
|
96
|
+
**Throws:**
|
|
97
|
+
- `Error` - On timeout or integrity failure
|
|
98
|
+
|
|
99
|
+
### Sync Functions
|
|
100
|
+
|
|
101
|
+
#### `writeSync(data: unknown, buffer: SharedArrayBuffer, options?: Options): void`
|
|
102
|
+
|
|
103
|
+
Synchronously writes data to the shared buffer using chunked transfer.
|
|
57
104
|
|
|
58
105
|
- `data` - Any serializable data
|
|
59
106
|
- `buffer` - SharedArrayBuffer for communication
|
|
60
|
-
- `
|
|
107
|
+
- `options` - Optional configuration object
|
|
108
|
+
- `timeout` - Timeout in milliseconds (default: 5000)
|
|
61
109
|
|
|
62
110
|
**Throws:**
|
|
63
111
|
- `Error` - On handshake or chunk timeout
|
|
64
112
|
|
|
65
|
-
|
|
113
|
+
#### `readSync(buffer: SharedArrayBuffer, options?: Options): unknown`
|
|
66
114
|
|
|
67
|
-
|
|
115
|
+
Synchronously reads data from the shared buffer.
|
|
68
116
|
|
|
69
117
|
- `buffer` - SharedArrayBuffer for communication
|
|
70
|
-
- `
|
|
118
|
+
- `options` - Optional configuration object
|
|
119
|
+
- `timeout` - Timeout in milliseconds (default: 5000)
|
|
71
120
|
|
|
72
121
|
**Returns:** Deserialized data
|
|
73
122
|
|
|
74
123
|
**Throws:**
|
|
75
124
|
- `Error` - On timeout or integrity failure
|
|
76
125
|
|
|
126
|
+
### Low-level Generator Functions
|
|
127
|
+
|
|
128
|
+
#### `writeGenerator(data: unknown, buffer: SharedArrayBuffer, options?: Options): Generator<WaitRequest, void, WaitResponse>`
|
|
129
|
+
|
|
130
|
+
Generator function for custom write implementations.
|
|
131
|
+
|
|
132
|
+
#### `readGenerator(buffer: SharedArrayBuffer, options?: Options): Generator<WaitRequest, unknown, WaitResponse>`
|
|
133
|
+
|
|
134
|
+
Generator function for custom read implementations.
|
|
135
|
+
|
|
136
|
+
### Types
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
interface Options {
|
|
140
|
+
timeout?: number;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
interface WaitRequest {
|
|
144
|
+
target: Int32Array;
|
|
145
|
+
index: number;
|
|
146
|
+
value: number;
|
|
147
|
+
timeout?: number;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
type WaitResponse = ReturnType<typeof Atomics.wait>;
|
|
151
|
+
```
|
|
152
|
+
|
|
77
153
|
## Protocol
|
|
78
154
|
|
|
79
155
|
The library uses a header-based protocol with atomic operations:
|
|
@@ -97,7 +173,10 @@ The library uses a header-based protocol with atomic operations:
|
|
|
97
173
|
|
|
98
174
|
## Thread Safety
|
|
99
175
|
|
|
100
|
-
|
|
176
|
+
- **Async functions** (`write`, `read`) use `Atomics.waitAsync()` for non-blocking operations
|
|
177
|
+
- **Sync functions** (`writeSync`, `readSync`) use `Atomics.wait()` for blocking operations
|
|
178
|
+
- All functions use `Atomics.store()` and `Atomics.notify()` for synchronization
|
|
179
|
+
- Requires SharedArrayBuffer support and proper threading context
|
|
101
180
|
|
|
102
181
|
## Requirements
|
|
103
182
|
|
package/package.json
CHANGED