@push.rocks/smartrust 1.2.1 → 1.3.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # @push.rocks/smartrust
2
2
 
3
- A type-safe, production-ready bridge between TypeScript and Rust binaries via JSON-over-stdin/stdout IPC — with support for request/response, streaming, and event patterns.
3
+ A type-safe, production-ready bridge between TypeScript and Rust binaries — with support for **stdio** (child process) and **socket** (Unix socket / Windows named pipe) transports, request/response, streaming, and event patterns.
4
4
 
5
5
  ## Issue Reporting and Security
6
6
 
@@ -16,25 +16,34 @@ pnpm install @push.rocks/smartrust
16
16
 
17
17
  ## Overview 🔭
18
18
 
19
- `@push.rocks/smartrust` provides a complete bridge for TypeScript applications that need to communicate with Rust binaries. It handles the entire lifecycle — binary discovery, process spawning, request/response correlation, **streaming responses**, event pub/sub, and graceful shutdown — so you can focus on your command definitions instead of IPC plumbing.
19
+ `@push.rocks/smartrust` provides a complete bridge for TypeScript applications that need to communicate with Rust binaries. It handles the entire lifecycle — binary discovery, process spawning **or socket connection**, request/response correlation, **streaming responses**, event pub/sub, and graceful shutdown — so you can focus on your command definitions instead of IPC plumbing.
20
+
21
+ ### Two Transport Modes 🔌
22
+
23
+ | Mode | Method | Use Case |
24
+ |------|--------|----------|
25
+ | **Stdio** | `bridge.spawn()` | Spawn the Rust binary as a child process. Communicate via stdin/stdout. |
26
+ | **Socket** | `bridge.connect(path)` | Connect to an **already-running** Rust daemon via Unix socket or Windows named pipe. |
27
+
28
+ The JSON protocol is identical in both modes — only the transport layer changes. Socket mode enables use cases where the Rust binary runs as a **privileged system service** (e.g., a VPN daemon needing root for TUN devices, a network proxy binding to privileged ports) while the TypeScript app connects to it unprivileged.
20
29
 
21
30
  ### Why? 🤔
22
31
 
23
32
  If you're integrating Rust into a Node.js project, you'll inevitably need:
24
33
  - A way to **find** the compiled Rust binary across different environments (dev, CI, production, platform packages)
25
- - A way to **spawn** it and establish reliable two-way communication
34
+ - A way to **spawn it** or **connect to it** and establish reliable two-way communication
26
35
  - **Type-safe** request/response patterns with proper error handling
27
36
  - **Streaming responses** for progressive data processing, log tailing, or chunked transfers
28
37
  - **Event streaming** from Rust to TypeScript
29
- - **Graceful lifecycle management** (ready detection, clean shutdown, force kill)
38
+ - **Graceful lifecycle management** (ready detection, clean shutdown, auto-reconnection)
30
39
 
31
- `smartrust` wraps all of this into three classes: `RustBridge`, `RustBinaryLocator`, and `StreamingResponse`.
40
+ `smartrust` wraps all of this into a clean API: `RustBridge`, `RustBinaryLocator`, `StreamingResponse`, and pluggable transports.
32
41
 
33
42
  ## Usage 🚀
34
43
 
35
44
  ### The IPC Protocol
36
45
 
37
- `smartrust` uses a simple, newline-delimited JSON protocol over stdin/stdout:
46
+ `smartrust` uses a simple, newline-delimited JSON protocol:
38
47
 
39
48
  | Direction | Format | Description |
40
49
  |-----------|--------|-------------|
@@ -44,7 +53,7 @@ If you're integrating Rust into a Node.js project, you'll inevitably need:
44
53
  | **Rust → TS** (Stream Chunk) | `{"id": "req_1", "stream": true, "data": {...}}` | Intermediate chunk (zero or more) |
45
54
  | **Rust → TS** (Event) | `{"event": "ready", "data": {...}}` | Unsolicited event (no ID) |
46
55
 
47
- Your Rust binary reads JSON lines from stdin and writes JSON lines to stdout. That's it. Stderr is free for logging.
56
+ This protocol works identically over stdio and socket transports. Your Rust binary reads JSON lines from one end and writes JSON lines to the other. That's it.
48
57
 
49
58
  ### Defining Your Commands
50
59
 
@@ -62,7 +71,9 @@ type TMyCommands = {
62
71
  };
63
72
  ```
64
73
 
65
- ### Creating and Using the Bridge
74
+ ### Stdio Mode — Spawn a Child Process
75
+
76
+ This is the classic mode. The bridge spawns the Rust binary and communicates via stdin/stdout:
66
77
 
67
78
  ```typescript
68
79
  const bridge = new RustBridge<TMyCommands>({
@@ -90,10 +101,62 @@ bridge.on('management:configChanged', (data) => {
90
101
  console.log('Config was changed:', data);
91
102
  });
92
103
 
93
- // Clean shutdown
104
+ // Clean shutdown (SIGTERM → SIGKILL after 5s)
105
+ bridge.kill();
106
+ ```
107
+
108
+ ### Socket Mode — Connect to a Running Daemon 🔗
109
+
110
+ When the Rust binary runs as a system service (e.g., via `systemd`, `launchd`, or a Windows Service), use `connect()` to talk to it over a Unix socket or named pipe:
111
+
112
+ ```typescript
113
+ const bridge = new RustBridge<TMyCommands>({
114
+ binaryName: 'my-daemon', // used for logging / error messages
115
+ });
116
+
117
+ // Connect to the daemon's management socket
118
+ const ok = await bridge.connect('/var/run/my-daemon.sock');
119
+ if (!ok) {
120
+ console.error('Failed to connect to daemon');
121
+ process.exit(1);
122
+ }
123
+
124
+ // Same API as stdio mode — completely transparent!
125
+ const { pid } = await bridge.sendCommand('start', { port: 8080, host: '0.0.0.0' });
126
+ const metrics = await bridge.sendCommand('getMetrics', {});
127
+
128
+ // kill() closes the socket — it does NOT kill the daemon
94
129
  bridge.kill();
95
130
  ```
96
131
 
132
+ #### Auto-Reconnect
133
+
134
+ For long-running applications, enable automatic reconnection with exponential backoff:
135
+
136
+ ```typescript
137
+ const ok = await bridge.connect('/var/run/my-daemon.sock', {
138
+ autoReconnect: true, // reconnect on unexpected disconnect
139
+ reconnectBaseDelayMs: 100, // initial retry delay (doubles each attempt)
140
+ reconnectMaxDelayMs: 30000, // max retry delay cap
141
+ maxReconnectAttempts: 10, // give up after 10 attempts
142
+ });
143
+
144
+ // Listen for reconnection events
145
+ bridge.on('reconnected', () => {
146
+ console.log('Reconnected to daemon!');
147
+ });
148
+ ```
149
+
150
+ #### Platform Notes
151
+
152
+ | Platform | Socket Path Format | Example |
153
+ |----------|-------------------|---------|
154
+ | **Linux** | `/var/run/<name>.sock` or `$XDG_RUNTIME_DIR/<name>.sock` | `/var/run/my-daemon.sock` |
155
+ | **macOS** | `/var/run/<name>.sock` | `/var/run/my-daemon.sock` |
156
+ | **Windows** | `\\.\pipe\<name>` | `\\.\pipe\my-daemon` |
157
+
158
+ Node.js `net.connect()` handles all formats transparently — no platform-specific code needed.
159
+
97
160
  ### Streaming Commands 🌊
98
161
 
99
162
  For commands where the Rust binary sends a series of chunks before a final result, use `sendCommandStreaming`. This is perfect for progressive data processing, log tailing, search results, or any scenario where you want incremental output.
@@ -231,6 +294,17 @@ const bridge = new RustBridge<TMyCommands>({
231
294
  });
232
295
  ```
233
296
 
297
+ Socket connection options (passed to `bridge.connect()`):
298
+
299
+ ```typescript
300
+ interface ISocketConnectOptions {
301
+ autoReconnect?: boolean; // default: false
302
+ reconnectBaseDelayMs?: number; // default: 100
303
+ reconnectMaxDelayMs?: number; // default: 30000
304
+ maxReconnectAttempts?: number; // default: 10
305
+ }
306
+ ```
307
+
234
308
  ### Events 📡
235
309
 
236
310
  `RustBridge` extends `EventEmitter` and emits the following events:
@@ -238,8 +312,9 @@ const bridge = new RustBridge<TMyCommands>({
238
312
  | Event | Payload | Description |
239
313
  |-------|---------|-------------|
240
314
  | `ready` | — | Bridge connected and binary reported ready |
241
- | `exit` | `(code, signal)` | Rust process exited |
242
- | `stderr` | `string` | A line from the binary's stderr |
315
+ | `exit` | `(code, signal)` | Transport closed (process exited or socket disconnected) |
316
+ | `stderr` | `string` | A line from the binary's stderr (stdio mode only) |
317
+ | `reconnected` | — | Socket transport reconnected after unexpected disconnect |
243
318
  | `management:<name>` | `any` | Custom event from Rust (e.g. `management:configChanged`) |
244
319
 
245
320
  ### Custom Logger 📝
@@ -263,7 +338,9 @@ const bridge = new RustBridge<TMyCommands>({
263
338
 
264
339
  ### Writing the Rust Side 🦀
265
340
 
266
- Your Rust binary needs to implement a simple protocol:
341
+ Your Rust binary needs to implement a simple protocol. The transport (stdio or socket) doesn't change the message format — only how connections are established.
342
+
343
+ #### Stdio Mode (Child Process)
267
344
 
268
345
  1. **On startup**, write a ready event to stdout:
269
346
  ```
@@ -280,7 +357,14 @@ Your Rust binary needs to implement a simple protocol:
280
357
 
281
358
  6. **Use stderr** for logging — it won't interfere with the IPC protocol
282
359
 
283
- Here's a minimal Rust skeleton:
360
+ #### Socket Mode (Daemon)
361
+
362
+ 1. **Listen** on a Unix socket (e.g., `/var/run/my-daemon.sock`) or Windows named pipe
363
+ 2. **On each new client connection**, send the `{"event":"ready","data":{...}}\n` event
364
+ 3. Read/write JSON lines on the socket (same protocol as stdio)
365
+ 4. Support multiple concurrent clients — each connection is independent
366
+
367
+ Here's a minimal Rust skeleton (stdio mode):
284
368
 
285
369
  ```rust
286
370
  use serde::{Deserialize, Serialize};
@@ -368,6 +452,33 @@ fn main() {
368
452
  }
369
453
  ```
370
454
 
455
+ ## Architecture 🏗️
456
+
457
+ ```
458
+ ┌──────────────────────────────────────────────────────┐
459
+ │ RustBridge<T> │
460
+ │ (protocol layer: handleLine, sendCommand, events) │
461
+ ├──────────────┬───────────────────────────────────────┤
462
+ │ StdioTransport │ SocketTransport │
463
+ │ spawn() + │ net.connect() + auto-reconnect │
464
+ │ stdin/stdout │ Unix socket / named pipe │
465
+ ├──────────────┴───────────────────────────────────────┤
466
+ │ IRustTransport interface │
467
+ │ connect() / write() / disconnect() │
468
+ ├──────────────────────────────────────────────────────┤
469
+ │ LineScanner (shared newline scanner) │
470
+ ├──────────────────────────────────────────────────────┤
471
+ │ RustBinaryLocator │
472
+ │ (binary search — stdio mode only) │
473
+ └──────────────────────────────────────────────────────┘
474
+ ```
475
+
476
+ - **`RustBridge`** — The main class. Protocol-level logic (JSON parsing, request correlation, streaming, events) is transport-agnostic.
477
+ - **`StdioTransport`** — Spawns a child process, manages stdin/stdout/stderr, handles SIGTERM/SIGKILL.
478
+ - **`SocketTransport`** — Connects to an existing Unix socket or named pipe, with optional auto-reconnect and exponential backoff.
479
+ - **`LineScanner`** — Shared buffer-based newline scanner used by both transports for efficient message framing.
480
+ - **`RustBinaryLocator`** — Priority-ordered binary search (used by stdio mode only).
481
+
371
482
  ## API Reference 📖
372
483
 
373
484
  ### `RustBridge<TCommands>`
@@ -375,11 +486,12 @@ fn main() {
375
486
  | Method / Property | Signature | Description |
376
487
  |---|---|---|
377
488
  | `constructor` | `new RustBridge<T>(options: IRustBridgeOptions)` | Create a new bridge instance |
378
- | `spawn()` | `Promise<boolean>` | Spawn the binary and wait for ready; returns `false` on failure |
489
+ | `spawn()` | `Promise<boolean>` | **Stdio mode**: Spawn the binary and wait for ready; returns `false` on failure |
490
+ | `connect(socketPath, options?)` | `Promise<boolean>` | **Socket mode**: Connect to a running daemon; returns `false` on failure |
379
491
  | `sendCommand(method, params)` | `Promise<TCommands[K]['result']>` | Send a typed command and await the response |
380
492
  | `sendCommandStreaming(method, params)` | `StreamingResponse<TChunk, TResult>` | Send a streaming command; returns immediately |
381
- | `kill()` | `void` | SIGTERM the process, reject pending requests, force SIGKILL after 5s |
382
- | `running` | `boolean` | Whether the bridge is currently connected |
493
+ | `kill()` | `void` | Stdio: SIGTERM the process, SIGKILL after 5s. Socket: close the connection (daemon stays alive) |
494
+ | `running` | `boolean` | Whether the bridge is currently connected and ready |
383
495
 
384
496
  ### `StreamingResponse<TChunk, TResult>`
385
497
 
@@ -396,13 +508,43 @@ fn main() {
396
508
  | `findBinary()` | `Promise<string \| null>` | Find the binary using the priority search; result is cached |
397
509
  | `clearCache()` | `void` | Clear the cached path to force a fresh search |
398
510
 
511
+ ### `StdioTransport`
512
+
513
+ | Method / Property | Signature | Description |
514
+ |---|---|---|
515
+ | `constructor` | `new StdioTransport(options: IStdioTransportOptions)` | Create a stdio transport |
516
+ | `connect()` | `Promise<void>` | Spawn the child process |
517
+ | `write(data)` | `Promise<void>` | Write to stdin with backpressure handling |
518
+ | `disconnect()` | `void` | Kill the process (SIGTERM → SIGKILL after 5s) |
519
+ | `connected` | `boolean` | Whether the process is running |
520
+
521
+ ### `SocketTransport`
522
+
523
+ | Method / Property | Signature | Description |
524
+ |---|---|---|
525
+ | `constructor` | `new SocketTransport(options: ISocketTransportOptions)` | Create a socket transport |
526
+ | `connect()` | `Promise<void>` | Connect to the Unix socket / named pipe |
527
+ | `write(data)` | `Promise<void>` | Write to socket with backpressure handling |
528
+ | `disconnect()` | `void` | Close the socket (does not kill the daemon) |
529
+ | `connected` | `boolean` | Whether the socket is connected |
530
+
531
+ ### `LineScanner`
532
+
533
+ | Method / Property | Signature | Description |
534
+ |---|---|---|
535
+ | `constructor` | `new LineScanner(maxPayloadSize, logger)` | Create a line scanner |
536
+ | `push(chunk, onLine)` | `void` | Feed a `Buffer` chunk; calls `onLine` for each complete line |
537
+ | `clear()` | `void` | Reset the internal buffer |
538
+
399
539
  ### Exported Interfaces & Types
400
540
 
401
541
  | Interface / Type | Description |
402
542
  |---|---|
403
543
  | `IRustBridgeOptions` | Full configuration for `RustBridge` |
404
544
  | `IBinaryLocatorOptions` | Configuration for `RustBinaryLocator` |
545
+ | `ISocketConnectOptions` | Socket connection options (reconnect settings) |
405
546
  | `IRustBridgeLogger` | Logger interface: `{ log(level, message, data?) }` |
547
+ | `IRustTransport` | Transport interface (extends `EventEmitter`) |
406
548
  | `IManagementRequest` | IPC request shape: `{ id, method, params }` |
407
549
  | `IManagementResponse` | IPC response shape: `{ id, success, result?, error? }` |
408
550
  | `IManagementEvent` | IPC event shape: `{ event, data }` |
@@ -0,0 +1,124 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Mock "Rust daemon" for testing the SocketTransport and RustBridge socket mode.
5
+ * Creates a Unix socket server, accepts connections, and speaks the same
6
+ * JSON-over-newline IPC protocol as mock-rust-binary.mjs.
7
+ *
8
+ * Usage: node mock-socket-server.mjs <socket-path>
9
+ * Signals readiness by writing a JSON line to stdout: {"socketPath":"...","ready":true}
10
+ */
11
+
12
+ import * as net from 'net';
13
+ import * as fs from 'fs';
14
+
15
+ const socketPath = process.argv[2];
16
+ if (!socketPath) {
17
+ process.stderr.write('Usage: mock-socket-server.mjs <socket-path>\n');
18
+ process.exit(1);
19
+ }
20
+
21
+ // Remove stale socket file
22
+ try { fs.unlinkSync(socketPath); } catch { /* ignore */ }
23
+
24
+ /**
25
+ * Backpressure-aware write to a socket.
26
+ */
27
+ function writeResponse(conn, data) {
28
+ const json = JSON.stringify(data) + '\n';
29
+ if (!conn.write(json)) {
30
+ conn.once('drain', () => {});
31
+ }
32
+ }
33
+
34
+ function handleLine(line, conn) {
35
+ let request;
36
+ try {
37
+ request = JSON.parse(line);
38
+ } catch {
39
+ return;
40
+ }
41
+
42
+ const { id, method, params } = request;
43
+
44
+ if (method === 'echo') {
45
+ writeResponse(conn, { id, success: true, result: params });
46
+ } else if (method === 'largeEcho') {
47
+ writeResponse(conn, { id, success: true, result: params });
48
+ } else if (method === 'error') {
49
+ writeResponse(conn, { id, success: false, error: 'Test error message' });
50
+ } else if (method === 'emitEvent') {
51
+ writeResponse(conn, { event: params.eventName, data: params.eventData });
52
+ writeResponse(conn, { id, success: true, result: null });
53
+ } else if (method === 'slow') {
54
+ setTimeout(() => {
55
+ writeResponse(conn, { id, success: true, result: { delayed: true } });
56
+ }, 100);
57
+ } else if (method === 'streamEcho') {
58
+ const count = params.count || 0;
59
+ let sent = 0;
60
+ const interval = setInterval(() => {
61
+ if (sent < count) {
62
+ writeResponse(conn, { id, stream: true, data: { index: sent, value: `chunk_${sent}` } });
63
+ sent++;
64
+ } else {
65
+ clearInterval(interval);
66
+ writeResponse(conn, { id, success: true, result: { totalChunks: count } });
67
+ }
68
+ }, 10);
69
+ } else if (method === 'streamError') {
70
+ writeResponse(conn, { id, stream: true, data: { index: 0, value: 'before_error' } });
71
+ setTimeout(() => {
72
+ writeResponse(conn, { id, success: false, error: 'Stream error after chunk' });
73
+ }, 20);
74
+ } else if (method === 'streamEmpty') {
75
+ writeResponse(conn, { id, success: true, result: { totalChunks: 0 } });
76
+ } else if (method === 'exit') {
77
+ writeResponse(conn, { id, success: true, result: null });
78
+ // In socket mode, 'exit' just closes this connection, not the server
79
+ setTimeout(() => conn.end(), 50);
80
+ } else {
81
+ writeResponse(conn, { id, success: false, error: `Unknown method: ${method}` });
82
+ }
83
+ }
84
+
85
+ const server = net.createServer((conn) => {
86
+ // Send ready event on each new connection
87
+ writeResponse(conn, { event: 'ready', data: { version: '1.0.0' } });
88
+
89
+ // Buffer-based newline scanner for incoming data
90
+ let buffer = Buffer.alloc(0);
91
+ conn.on('data', (chunk) => {
92
+ buffer = Buffer.concat([buffer, chunk]);
93
+ let idx;
94
+ while ((idx = buffer.indexOf(0x0A)) !== -1) {
95
+ const lineBuffer = buffer.subarray(0, idx);
96
+ buffer = buffer.subarray(idx + 1);
97
+ const line = lineBuffer.toString('utf8').trim();
98
+ if (line) {
99
+ handleLine(line, conn);
100
+ }
101
+ }
102
+ });
103
+
104
+ conn.on('error', () => { /* ignore client errors */ });
105
+ });
106
+
107
+ server.listen(socketPath, () => {
108
+ // Signal to parent that the server is ready
109
+ process.stdout.write(JSON.stringify({ socketPath, ready: true }) + '\n');
110
+ });
111
+
112
+ // Handle SIGTERM gracefully
113
+ process.on('SIGTERM', () => {
114
+ server.close();
115
+ try { fs.unlinkSync(socketPath); } catch { /* ignore */ }
116
+ process.exit(0);
117
+ });
118
+
119
+ // Handle SIGINT
120
+ process.on('SIGINT', () => {
121
+ server.close();
122
+ try { fs.unlinkSync(socketPath); } catch { /* ignore */ }
123
+ process.exit(0);
124
+ });
@@ -0,0 +1,89 @@
1
+ import { expect, tap } from '@git.zone/tstest/tapbundle';
2
+ import { LineScanner } from '../ts/classes.linescanner.js';
3
+
4
+ const noopLogger = { log() {} };
5
+
6
+ tap.test('should parse a single complete line', async () => {
7
+ const scanner = new LineScanner(1024 * 1024, noopLogger);
8
+ const lines: string[] = [];
9
+ scanner.push(Buffer.from('{"hello":"world"}\n'), (line) => lines.push(line));
10
+ expect(lines.length).toEqual(1);
11
+ expect(lines[0]).toEqual('{"hello":"world"}');
12
+ });
13
+
14
+ tap.test('should parse multiple lines in one chunk', async () => {
15
+ const scanner = new LineScanner(1024 * 1024, noopLogger);
16
+ const lines: string[] = [];
17
+ scanner.push(Buffer.from('{"a":1}\n{"b":2}\n{"c":3}\n'), (line) => lines.push(line));
18
+ expect(lines.length).toEqual(3);
19
+ expect(lines[0]).toEqual('{"a":1}');
20
+ expect(lines[1]).toEqual('{"b":2}');
21
+ expect(lines[2]).toEqual('{"c":3}');
22
+ });
23
+
24
+ tap.test('should handle a line split across chunks', async () => {
25
+ const scanner = new LineScanner(1024 * 1024, noopLogger);
26
+ const lines: string[] = [];
27
+ scanner.push(Buffer.from('{"hel'), (line) => lines.push(line));
28
+ expect(lines.length).toEqual(0);
29
+ scanner.push(Buffer.from('lo":"world"}\n'), (line) => lines.push(line));
30
+ expect(lines.length).toEqual(1);
31
+ expect(lines[0]).toEqual('{"hello":"world"}');
32
+ });
33
+
34
+ tap.test('should drop oversized lines', async () => {
35
+ const scanner = new LineScanner(100, noopLogger);
36
+ const lines: string[] = [];
37
+ // Line is 200 chars + newline, exceeds maxPayloadSize of 100
38
+ const oversized = 'x'.repeat(200) + '\n';
39
+ scanner.push(Buffer.from(oversized), (line) => lines.push(line));
40
+ expect(lines.length).toEqual(0);
41
+ });
42
+
43
+ tap.test('should clear buffer on OOM (no newline, exceeds max)', async () => {
44
+ const scanner = new LineScanner(100, noopLogger);
45
+ const lines: string[] = [];
46
+ // Push 200 bytes without any newline — exceeds maxPayloadSize
47
+ scanner.push(Buffer.from('x'.repeat(200)), (line) => lines.push(line));
48
+ expect(lines.length).toEqual(0);
49
+ // After clearing, should work normally again
50
+ scanner.push(Buffer.from('{"ok":true}\n'), (line) => lines.push(line));
51
+ expect(lines.length).toEqual(1);
52
+ expect(lines[0]).toEqual('{"ok":true}');
53
+ });
54
+
55
+ tap.test('should skip empty lines', async () => {
56
+ const scanner = new LineScanner(1024 * 1024, noopLogger);
57
+ const lines: string[] = [];
58
+ scanner.push(Buffer.from('\n\n{"a":1}\n\n'), (line) => lines.push(line));
59
+ expect(lines.length).toEqual(1);
60
+ expect(lines[0]).toEqual('{"a":1}');
61
+ });
62
+
63
+ tap.test('should handle mixed complete and partial lines', async () => {
64
+ const scanner = new LineScanner(1024 * 1024, noopLogger);
65
+ const lines: string[] = [];
66
+ // First chunk: one complete line + start of second line
67
+ scanner.push(Buffer.from('{"first":1}\n{"sec'), (line) => lines.push(line));
68
+ expect(lines.length).toEqual(1);
69
+ // Second chunk: end of second line + complete third line
70
+ scanner.push(Buffer.from('ond":2}\n{"third":3}\n'), (line) => lines.push(line));
71
+ expect(lines.length).toEqual(3);
72
+ expect(lines[1]).toEqual('{"second":2}');
73
+ expect(lines[2]).toEqual('{"third":3}');
74
+ });
75
+
76
+ tap.test('clear should reset the buffer', async () => {
77
+ const scanner = new LineScanner(1024 * 1024, noopLogger);
78
+ const lines: string[] = [];
79
+ // Push partial data
80
+ scanner.push(Buffer.from('{"partial":'), (line) => lines.push(line));
81
+ // Clear
82
+ scanner.clear();
83
+ // Now push a complete new line — old partial should not affect it
84
+ scanner.push(Buffer.from('{"fresh":true}\n'), (line) => lines.push(line));
85
+ expect(lines.length).toEqual(1);
86
+ expect(lines[0]).toEqual('{"fresh":true}');
87
+ });
88
+
89
+ export default tap.start();