* Ring buffer reader (used by worker thread)
| 289 | * Ring buffer reader (used by worker thread) |
| 290 | */ |
| 291 | class RingBufferReader { |
| 292 | constructor(sharedBuffer) { |
| 293 | this.buffer = sharedBuffer; |
| 294 | this.int32 = new Int32Array(sharedBuffer); |
| 295 | this.uint8 = new Uint8Array(sharedBuffer); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Check if there's data available in the network ring buffer |
| 300 | */ |
| 301 | hasNetworkData() { |
| 302 | const head = Atomics.load(this.int32, NET_HEAD_INDEX); |
| 303 | const tail = Atomics.load(this.int32, NET_TAIL_INDEX); |
| 304 | return head !== tail; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Check if there's stdin data available |
| 309 | */ |
| 310 | hasStdinData() { |
| 311 | return Atomics.load(this.int32, STDIN_FLAG_INDEX) !== 0; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Read stdin data (returns null if none available) |
| 316 | */ |
| 317 | readStdin() { |
| 318 | if (Atomics.load(this.int32, STDIN_FLAG_INDEX) === 0) { |
| 319 | return null; |
| 320 | } |
| 321 | |
| 322 | const size = Atomics.load(this.int32, STDIN_SIZE_INDEX); |
| 323 | const data = this.uint8.slice(STDIN_OFFSET, STDIN_OFFSET + size); |
| 324 | |
| 325 | // Clear flag and notify writer |
| 326 | Atomics.store(this.int32, STDIN_SIZE_INDEX, 0); |
| 327 | Atomics.store(this.int32, STDIN_FLAG_INDEX, 0); |
| 328 | Atomics.notify(this.int32, STDIN_FLAG_INDEX); |
| 329 | |
| 330 | return data; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Read next network message (returns null if none available) |
| 335 | * @returns {Object|null} - { type, payload } or null |
| 336 | */ |
| 337 | readNetworkMessage() { |
| 338 | const head = Atomics.load(this.int32, NET_HEAD_INDEX); |
| 339 | let tail = Atomics.load(this.int32, NET_TAIL_INDEX); |
| 340 | |
| 341 | if (head === tail) { |
| 342 | return null; // Empty |
| 343 | } |
| 344 | |
| 345 | const ringStart = NET_RING_OFFSET; |
| 346 | |
| 347 | // Read length (2 bytes, little-endian) |
| 348 | const lenLow = this.uint8[ringStart + tail]; |
nothing calls this directly
no outgoing calls
no test coverage detected