@dorokuma/herdsman-pi 0.6.4 → 0.6.5

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": "@dorokuma/herdsman-pi",
3
- "version": "0.6.4",
3
+ "version": "0.6.5",
4
4
  "description": "Pi extension bridge for Herdsman agent history. Forked from @ryonakae/shepherd.",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -1,5 +1,5 @@
1
1
  import { createConnection, type Socket } from "node:net";
2
- import { JsonLineDecoder } from "@/shared/json-lines.js";
2
+ import { JsonLineDecoder } from "./shared/json-lines.js";
3
3
 
4
4
  export type AgentEventWireRecord = {
5
5
  agentId?: string | null;
@@ -0,0 +1,46 @@
1
+ export function encodeJsonLine(value: unknown): string {
2
+ const encoded = JSON.stringify(value);
3
+
4
+ if (encoded === undefined) {
5
+ throw new TypeError("JSON Lines values must be JSON-serializable");
6
+ }
7
+
8
+ return `${encoded}\n`;
9
+ }
10
+
11
+ export class JsonLineFrameTooLargeError extends Error {
12
+ constructor(readonly maxBytes: number) {
13
+ super(`JSON Lines frame exceeds maximum size of ${maxBytes} bytes`);
14
+ this.name = "JsonLineFrameTooLargeError";
15
+ }
16
+ }
17
+
18
+ export class JsonLineDecoder {
19
+ #buffer = "";
20
+ readonly #maxBytes: number;
21
+
22
+ constructor(maxBytes = 1024 * 1024) {
23
+ if (!Number.isInteger(maxBytes) || maxBytes <= 0) {
24
+ throw new RangeError("JSON Lines maximum frame size must be a positive integer");
25
+ }
26
+ this.#maxBytes = maxBytes;
27
+ }
28
+
29
+ push(chunk: string): unknown[] {
30
+ this.#buffer += chunk;
31
+ if (Buffer.byteLength(this.#buffer, "utf8") > this.#maxBytes) {
32
+ throw new JsonLineFrameTooLargeError(this.#maxBytes);
33
+ }
34
+
35
+ const lines = this.#buffer.split("\n");
36
+ this.#buffer = lines.pop() ?? "";
37
+ return lines.filter((line) => line.length > 0).map((line) => JSON.parse(line));
38
+ }
39
+
40
+ flush(): unknown[] {
41
+ if (this.#buffer.length === 0) return [];
42
+ const line = this.#buffer;
43
+ this.#buffer = "";
44
+ return [JSON.parse(line)];
45
+ }
46
+ }