@cueloop/daemon 0.1.0-alpha.111 → 0.1.0-alpha.112

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cueloop/daemon",
3
- "version": "0.1.0-alpha.111",
3
+ "version": "0.1.0-alpha.112",
4
4
  "description": "The cueloop session daemon and its client: socket protocol, atomic store, resumable waits",
5
5
  "homepage": "https://github.com/mmurakaru/cueloop#readme",
6
6
  "bugs": {
@@ -44,8 +44,8 @@
44
44
  "access": "public"
45
45
  },
46
46
  "dependencies": {
47
- "@cueloop/extension-api": "0.1.0-alpha.111",
48
- "@cueloop/schema": "0.1.0-alpha.111",
47
+ "@cueloop/extension-api": "0.1.0-alpha.112",
48
+ "@cueloop/schema": "0.1.0-alpha.112",
49
49
  "@pierre/diffs": "^1.6.1",
50
50
  "effect": "^4.0.6",
51
51
  "smol-toml": "^1.9.1",
@@ -0,0 +1,37 @@
1
+ import { expect, test } from "bun:test";
2
+ import { LineBuffer } from "./protocol";
3
+
4
+ test("partial and coalesced frames keep order and leave the last frame pending", () => {
5
+ const buffer = new LineBuffer();
6
+ const lines: string[] = [];
7
+
8
+ for (const chunk of [' {"a":', '1}\n\n {"b":2}\n{"c":', "3}", "\n"])
9
+ buffer.push(chunk, (line) => lines.push(line));
10
+
11
+ expect(lines).toEqual(['{"a":1}', '{"b":2}', '{"c":3}']);
12
+ });
13
+
14
+ test("large fragmented frames preserve text and allow a following frame", () => {
15
+ const buffer = new LineBuffer();
16
+ const text = JSON.stringify({ text: "漢字 🌍 ".repeat(150_000) });
17
+ const lines: string[] = [];
18
+
19
+ for (let offset = 0; offset < text.length; offset += 4096)
20
+ buffer.push(text.slice(offset, offset + 4096), (line) => lines.push(line));
21
+
22
+ expect(lines).toEqual([]);
23
+ buffer.push('\n{"done":true}\n', (line) => lines.push(line));
24
+ expect(lines).toEqual([text, '{"done":true}']);
25
+ });
26
+
27
+ test("a callback can supply more frames without changing pending frame order", () => {
28
+ const buffer = new LineBuffer();
29
+ const lines: string[] = [];
30
+
31
+ buffer.push("first\nsecond\n", (line) => {
32
+ lines.push(line);
33
+ buffer.push("third\n", (next) => lines.push(next));
34
+ });
35
+
36
+ expect(lines).toEqual(["first", "second", "third"]);
37
+ });
package/src/protocol.ts CHANGED
@@ -53,16 +53,27 @@ export function parseInboundFrame(line: string): Frame {
53
53
  /** Incremental NDJSON splitter; tolerates partial writes. */
54
54
  export class LineBuffer {
55
55
  private buffered = "";
56
+ private fragments: string[] = [];
56
57
 
57
58
  push(chunk: string, onLine: (line: string) => void): void {
58
59
  this.buffered += chunk;
59
60
  for (;;) {
60
61
  const newlineIndex = this.buffered.indexOf("\n");
61
62
 
62
- if (newlineIndex === -1) return;
63
+ if (newlineIndex === -1) {
64
+ if (this.buffered) this.fragments.push(this.buffered);
63
65
 
64
- const line = this.buffered.slice(0, newlineIndex).trim();
66
+ this.buffered = "";
65
67
 
68
+ return;
69
+ }
70
+
71
+ const fragment = this.buffered.slice(0, newlineIndex);
72
+ const line = this.fragments.length
73
+ ? [...this.fragments, fragment].join("").trim()
74
+ : fragment.trim();
75
+
76
+ this.fragments = [];
66
77
  this.buffered = this.buffered.slice(newlineIndex + 1);
67
78
 
68
79
  if (line) onLine(line);