sabcom 0.0.6 → 0.0.8

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 (2) hide show
  1. package/README.md +93 -14
  2. package/package.json +13 -12
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
- ### Basic Example
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
- ### `write(data: unknown, buffer: SharedArrayBuffer, timeout?: number): void`
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
- Writes data to the shared buffer using chunked transfer.
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
- - `timeout` - Timeout in milliseconds (default: 5000)
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
- ### `read(buffer: SharedArrayBuffer, timeout?: number): unknown`
113
+ #### `readSync(buffer: SharedArrayBuffer, options?: Options): unknown`
66
114
 
67
- Reads data from the shared buffer.
115
+ Synchronously reads data from the shared buffer.
68
116
 
69
117
  - `buffer` - SharedArrayBuffer for communication
70
- - `timeout` - Timeout in milliseconds (default: 5000)
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
- Uses `Atomics.wait()`, `Atomics.notify()`, and `Atomics.store()` for synchronization. Requires SharedArrayBuffer support and proper threading context.
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sabcom",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "A TypeScript/Node.js library for inter-thread communication using SharedArrayBuffer with atomic operations and V8 serialization",
5
5
  "type": "module",
6
6
  "types": "build/index.d.ts",
@@ -14,6 +14,12 @@
14
14
  "build",
15
15
  "src/index.ts"
16
16
  ],
17
+ "scripts": {
18
+ "build": "rm -rf build && inop src build -i __tests__ -i *.tmp.ts && tsc --declaration --emitDeclarationOnly",
19
+ "lint": "eslint src",
20
+ "test": "vitest run",
21
+ "test:coverage": "vitest run --coverage"
22
+ },
17
23
  "repository": {
18
24
  "type": "git",
19
25
  "url": "git+https://github.com/3axap4eHko/sabcom.git"
@@ -44,9 +50,9 @@
44
50
  },
45
51
  "devDependencies": {
46
52
  "@eslint/js": "^9.30.0",
47
- "@types/node": "^24.0.7",
48
- "@typescript-eslint/eslint-plugin": "^8.35.0",
49
- "@typescript-eslint/parser": "^8.35.0",
53
+ "@types/node": "^24.0.8",
54
+ "@typescript-eslint/eslint-plugin": "^8.35.1",
55
+ "@typescript-eslint/parser": "^8.35.1",
50
56
  "@vitest/coverage-v8": "^3.2.4",
51
57
  "eslint": "^9.30.0",
52
58
  "eslint-config-prettier": "^10.1.5",
@@ -55,13 +61,8 @@
55
61
  "inop": "^0.7.8",
56
62
  "prettier": "^3.6.2",
57
63
  "typescript": "^5.8.3",
58
- "typescript-eslint": "^8.35.0",
64
+ "typescript-eslint": "^8.35.1",
59
65
  "vitest": "^3.2.4"
60
66
  },
61
- "scripts": {
62
- "build": "rm -rf build && inop src build -i __tests__ -i *.tmp.ts && tsc --declaration --emitDeclarationOnly",
63
- "lint": "eslint src",
64
- "test": "vitest run",
65
- "test:coverage": "vitest run --coverage"
66
- }
67
- }
67
+ "packageManager": "pnpm@10.12.4"
68
+ }