@aztec/foundation 0.0.1-commit.9ef841308 → 0.0.1-commit.a89ec08

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 (58) hide show
  1. package/dest/collection/index.d.ts +1 -2
  2. package/dest/collection/index.d.ts.map +1 -1
  3. package/dest/collection/index.js +0 -1
  4. package/dest/config/env_var.d.ts +2 -2
  5. package/dest/config/env_var.d.ts.map +1 -1
  6. package/dest/config/network_config.d.ts +6 -6
  7. package/dest/config/network_config.js +2 -2
  8. package/dest/config/network_name.d.ts +2 -2
  9. package/dest/config/network_name.d.ts.map +1 -1
  10. package/dest/config/network_name.js +0 -2
  11. package/dest/crypto/serialize.d.ts +51 -0
  12. package/dest/crypto/serialize.d.ts.map +1 -0
  13. package/dest/crypto/serialize.js +68 -0
  14. package/dest/curves/grumpkin/point.d.ts +4 -4
  15. package/dest/curves/grumpkin/point.d.ts.map +1 -1
  16. package/dest/curves/grumpkin/point.js +7 -4
  17. package/dest/jest/setup.js +0 -24
  18. package/dest/serialize/buffer_reader.d.ts +20 -7
  19. package/dest/serialize/buffer_reader.d.ts.map +1 -1
  20. package/dest/serialize/buffer_reader.js +18 -5
  21. package/dest/serialize/field_reader.d.ts +13 -5
  22. package/dest/serialize/field_reader.d.ts.map +1 -1
  23. package/dest/serialize/field_reader.js +13 -0
  24. package/dest/sleep/index.d.ts +1 -2
  25. package/dest/sleep/index.d.ts.map +1 -1
  26. package/dest/sleep/index.js +1 -10
  27. package/dest/timer/date.d.ts +1 -3
  28. package/dest/timer/date.d.ts.map +1 -1
  29. package/dest/timer/date.js +0 -4
  30. package/dest/trees/indexed_merkle_tree_calculator.d.ts +1 -1
  31. package/dest/trees/indexed_merkle_tree_calculator.d.ts.map +1 -1
  32. package/dest/trees/indexed_merkle_tree_calculator.js +1 -5
  33. package/package.json +2 -3
  34. package/src/collection/index.ts +0 -1
  35. package/src/config/env_var.ts +4 -11
  36. package/src/config/network_config.ts +1 -1
  37. package/src/config/network_name.ts +1 -4
  38. package/src/crypto/serialize.ts +85 -0
  39. package/src/curves/grumpkin/point.ts +6 -3
  40. package/src/jest/setup.mjs +0 -27
  41. package/src/serialize/buffer_reader.ts +29 -6
  42. package/src/serialize/field_reader.ts +22 -4
  43. package/src/sleep/index.ts +1 -10
  44. package/src/timer/date.ts +0 -6
  45. package/src/trees/indexed_merkle_tree_calculator.ts +1 -5
  46. package/src/trees/membership_witness.ts +2 -2
  47. package/dest/collection/lru_set.d.ts +0 -35
  48. package/dest/collection/lru_set.d.ts.map +0 -1
  49. package/dest/collection/lru_set.js +0 -95
  50. package/dest/fifo/fifo_frame_reader.d.ts +0 -41
  51. package/dest/fifo/fifo_frame_reader.d.ts.map +0 -1
  52. package/dest/fifo/fifo_frame_reader.js +0 -74
  53. package/dest/fifo/index.d.ts +0 -2
  54. package/dest/fifo/index.d.ts.map +0 -1
  55. package/dest/fifo/index.js +0 -1
  56. package/src/collection/lru_set.ts +0 -115
  57. package/src/fifo/fifo_frame_reader.ts +0 -98
  58. package/src/fifo/index.ts +0 -1
@@ -1,35 +0,0 @@
1
- /**
2
- * A bounded set with Least Recently Used (LRU) eviction.
3
- * Both {@link has} and {@link add} count as an access and refresh the entry's
4
- * recency, so items that are actively checked stay in the set longest.
5
- *
6
- * Uses a doubly-linked list for O(1) ordering and a Map for O(1) lookup.
7
- * Head = least recent, tail = most recent.
8
- */
9
- export declare class LruSet<T> {
10
- private readonly maxSize;
11
- /** Map from value to its linked-list node for O(1) lookup. */
12
- private readonly map;
13
- private head;
14
- private tail;
15
- constructor(maxSize: number);
16
- /** Number of entries in the set. */
17
- get size(): number;
18
- /**
19
- * Returns true if the item is in the set.
20
- * Refreshes the item's recency so it becomes the most recently used.
21
- */
22
- has(item: T): boolean;
23
- /**
24
- * Adds an item to the set. If the item already exists, refreshes its recency.
25
- * If the set is at capacity, evicts the least recently used item.
26
- */
27
- add(item: T): void;
28
- /** Removes all entries from the set. */
29
- clear(): void;
30
- /** Unlinks a node from its current position and relinks it at the tail. */
31
- private moveToTail;
32
- /** Evicts the head (least recently used) node. */
33
- private evictHead;
34
- }
35
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibHJ1X3NldC5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2NvbGxlY3Rpb24vbHJ1X3NldC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFPQTs7Ozs7OztHQU9HO0FBQ0gscUJBQWEsTUFBTSxDQUFDLENBQUM7SUFNUCxPQUFPLENBQUMsUUFBUSxDQUFDLE9BQU87SUFMcEMsOERBQThEO0lBQzlELE9BQU8sQ0FBQyxRQUFRLENBQUMsR0FBRyxDQUE0QjtJQUNoRCxPQUFPLENBQUMsSUFBSSxDQUF5QjtJQUNyQyxPQUFPLENBQUMsSUFBSSxDQUF5QjtJQUVyQyxZQUE2QixPQUFPLEVBQUUsTUFBTSxFQUkzQztJQUVELG9DQUFvQztJQUNwQyxJQUFJLElBQUksSUFBSSxNQUFNLENBRWpCO0lBRUQ7OztPQUdHO0lBQ0gsR0FBRyxDQUFDLElBQUksRUFBRSxDQUFDLEdBQUcsT0FBTyxDQU9wQjtJQUVEOzs7T0FHRztJQUNILEdBQUcsQ0FBQyxJQUFJLEVBQUUsQ0FBQyxHQUFHLElBQUksQ0FtQmpCO0lBRUQsd0NBQXdDO0lBQ3hDLEtBQUssSUFBSSxJQUFJLENBSVo7SUFFRCwyRUFBMkU7SUFDM0UsT0FBTyxDQUFDLFVBQVU7SUF3QmxCLGtEQUFrRDtJQUNsRCxPQUFPLENBQUMsU0FBUztDQVdsQiJ9
@@ -1 +0,0 @@
1
- {"version":3,"file":"lru_set.d.ts","sourceRoot":"","sources":["../../src/collection/lru_set.ts"],"names":[],"mappings":"AAOA;;;;;;;GAOG;AACH,qBAAa,MAAM,CAAC,CAAC;IAMP,OAAO,CAAC,QAAQ,CAAC,OAAO;IALpC,8DAA8D;IAC9D,OAAO,CAAC,QAAQ,CAAC,GAAG,CAA4B;IAChD,OAAO,CAAC,IAAI,CAAyB;IACrC,OAAO,CAAC,IAAI,CAAyB;IAErC,YAA6B,OAAO,EAAE,MAAM,EAI3C;IAED,oCAAoC;IACpC,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;;OAGG;IACH,GAAG,CAAC,IAAI,EAAE,CAAC,GAAG,OAAO,CAOpB;IAED;;;OAGG;IACH,GAAG,CAAC,IAAI,EAAE,CAAC,GAAG,IAAI,CAmBjB;IAED,wCAAwC;IACxC,KAAK,IAAI,IAAI,CAIZ;IAED,2EAA2E;IAC3E,OAAO,CAAC,UAAU;IAwBlB,kDAAkD;IAClD,OAAO,CAAC,SAAS;CAWlB"}
@@ -1,95 +0,0 @@
1
- /** Node in a doubly-linked list used by {@link LruSet}. */ /**
2
- * A bounded set with Least Recently Used (LRU) eviction.
3
- * Both {@link has} and {@link add} count as an access and refresh the entry's
4
- * recency, so items that are actively checked stay in the set longest.
5
- *
6
- * Uses a doubly-linked list for O(1) ordering and a Map for O(1) lookup.
7
- * Head = least recent, tail = most recent.
8
- */ export class LruSet {
9
- maxSize;
10
- /** Map from value to its linked-list node for O(1) lookup. */ map;
11
- head;
12
- tail;
13
- constructor(maxSize){
14
- this.maxSize = maxSize;
15
- this.map = new Map();
16
- if (maxSize < 1) {
17
- throw new Error('LruSet maxSize must be at least 1');
18
- }
19
- }
20
- /** Number of entries in the set. */ get size() {
21
- return this.map.size;
22
- }
23
- /**
24
- * Returns true if the item is in the set.
25
- * Refreshes the item's recency so it becomes the most recently used.
26
- */ has(item) {
27
- const node = this.map.get(item);
28
- if (!node) {
29
- return false;
30
- }
31
- this.moveToTail(node);
32
- return true;
33
- }
34
- /**
35
- * Adds an item to the set. If the item already exists, refreshes its recency.
36
- * If the set is at capacity, evicts the least recently used item.
37
- */ add(item) {
38
- const existing = this.map.get(item);
39
- if (existing) {
40
- this.moveToTail(existing);
41
- return;
42
- }
43
- if (this.map.size >= this.maxSize) {
44
- this.evictHead();
45
- }
46
- const node = {
47
- value: item,
48
- prev: this.tail,
49
- next: undefined
50
- };
51
- if (this.tail) {
52
- this.tail.next = node;
53
- } else {
54
- this.head = node;
55
- }
56
- this.tail = node;
57
- this.map.set(item, node);
58
- }
59
- /** Removes all entries from the set. */ clear() {
60
- this.map.clear();
61
- this.head = undefined;
62
- this.tail = undefined;
63
- }
64
- /** Unlinks a node from its current position and relinks it at the tail. */ moveToTail(node) {
65
- if (node === this.tail) {
66
- return;
67
- }
68
- // Unlink
69
- if (node.prev) {
70
- node.prev.next = node.next;
71
- } else {
72
- this.head = node.next;
73
- }
74
- if (node.next) {
75
- node.next.prev = node.prev;
76
- }
77
- // Relink at tail
78
- node.prev = this.tail;
79
- node.next = undefined;
80
- if (this.tail) {
81
- this.tail.next = node;
82
- }
83
- this.tail = node;
84
- }
85
- /** Evicts the head (least recently used) node. */ evictHead() {
86
- const oldHead = this.head;
87
- this.map.delete(oldHead.value);
88
- this.head = oldHead.next;
89
- if (this.head) {
90
- this.head.prev = undefined;
91
- } else {
92
- this.tail = undefined;
93
- }
94
- }
95
- }
@@ -1,41 +0,0 @@
1
- import EventEmitter from 'node:events';
2
- import type { Readable } from 'node:stream';
3
- /**
4
- * Events emitted by FifoFrameReader.
5
- *
6
- * - `frame`: A complete frame payload (without the 4-byte length header).
7
- * - `error`: An unrecoverable error (invalid frame length, stream error).
8
- * - `end`: The underlying stream has ended.
9
- */
10
- export interface FifoFrameReaderEvents {
11
- frame: [payload: Buffer];
12
- error: [error: Error];
13
- end: [];
14
- }
15
- /**
16
- * Reads length-delimited frames from a readable stream (typically a named FIFO pipe).
17
- *
18
- * Wire format: `[4-byte big-endian payload length][payload bytes]`
19
- *
20
- * Emits a `frame` event for each complete frame with the raw payload buffer.
21
- * Callers are responsible for deserializing the payload (e.g., via msgpack).
22
- *
23
- * On encountering an invalid payload length (0 or >maxPayloadSize), emits `error`
24
- * and destroys the stream.
25
- */
26
- export declare class FifoFrameReader extends EventEmitter<FifoFrameReaderEvents> {
27
- private readonly maxPayloadSize;
28
- private stream;
29
- private pendingBuf;
30
- private running;
31
- constructor(maxPayloadSize?: number);
32
- /** Open a FIFO at the given path and start reading frames. */
33
- start(fifoPath: string, highWaterMark?: number): void;
34
- /** Start reading frames from an existing readable stream. */
35
- startFromStream(stream: Readable): void;
36
- /** Stop reading and destroy the underlying stream. */
37
- stop(): void;
38
- /** Parse complete frames out of the pending buffer. */
39
- private drainFrames;
40
- }
41
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZmlmb19mcmFtZV9yZWFkZXIuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9maWZvL2ZpZm9fZnJhbWVfcmVhZGVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sWUFBWSxNQUFNLGFBQWEsQ0FBQztBQUV2QyxPQUFPLEtBQUssRUFBRSxRQUFRLEVBQUUsTUFBTSxhQUFhLENBQUM7QUFFNUM7Ozs7OztHQU1HO0FBQ0gsTUFBTSxXQUFXLHFCQUFxQjtJQUNwQyxLQUFLLEVBQUUsQ0FBQyxPQUFPLEVBQUUsTUFBTSxDQUFDLENBQUM7SUFDekIsS0FBSyxFQUFFLENBQUMsS0FBSyxFQUFFLEtBQUssQ0FBQyxDQUFDO0lBQ3RCLEdBQUcsRUFBRSxFQUFFLENBQUM7Q0FDVDtBQUVEOzs7Ozs7Ozs7O0dBVUc7QUFDSCxxQkFBYSxlQUFnQixTQUFRLFlBQVksQ0FBQyxxQkFBcUIsQ0FBQztJQUsxRCxPQUFPLENBQUMsUUFBUSxDQUFDLGNBQWM7SUFKM0MsT0FBTyxDQUFDLE1BQU0sQ0FBeUI7SUFDdkMsT0FBTyxDQUFDLFVBQVUsQ0FBMkI7SUFDN0MsT0FBTyxDQUFDLE9BQU8sQ0FBUztJQUV4QixZQUE2QixjQUFjLFNBQW1CLEVBRTdEO0lBRUQsOERBQThEO0lBQzlELEtBQUssQ0FBQyxRQUFRLEVBQUUsTUFBTSxFQUFFLGFBQWEsU0FBWSxHQUFHLElBQUksQ0FFdkQ7SUFFRCw2REFBNkQ7SUFDN0QsZUFBZSxDQUFDLE1BQU0sRUFBRSxRQUFRLEdBQUcsSUFBSSxDQXVCdEM7SUFFRCxzREFBc0Q7SUFDdEQsSUFBSSxJQUFJLElBQUksQ0FNWDtJQUVELHVEQUF1RDtJQUN2RCxPQUFPLENBQUMsV0FBVztDQW1CcEIifQ==
@@ -1 +0,0 @@
1
- {"version":3,"file":"fifo_frame_reader.d.ts","sourceRoot":"","sources":["../../src/fifo/fifo_frame_reader.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,aAAa,CAAC;AAEvC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE5C;;;;;;GAMG;AACH,MAAM,WAAW,qBAAqB;IACpC,KAAK,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IACzB,KAAK,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IACtB,GAAG,EAAE,EAAE,CAAC;CACT;AAED;;;;;;;;;;GAUG;AACH,qBAAa,eAAgB,SAAQ,YAAY,CAAC,qBAAqB,CAAC;IAK1D,OAAO,CAAC,QAAQ,CAAC,cAAc;IAJ3C,OAAO,CAAC,MAAM,CAAyB;IACvC,OAAO,CAAC,UAAU,CAA2B;IAC7C,OAAO,CAAC,OAAO,CAAS;IAExB,YAA6B,cAAc,SAAmB,EAE7D;IAED,8DAA8D;IAC9D,KAAK,CAAC,QAAQ,EAAE,MAAM,EAAE,aAAa,SAAY,GAAG,IAAI,CAEvD;IAED,6DAA6D;IAC7D,eAAe,CAAC,MAAM,EAAE,QAAQ,GAAG,IAAI,CAuBtC;IAED,sDAAsD;IACtD,IAAI,IAAI,IAAI,CAMX;IAED,uDAAuD;IACvD,OAAO,CAAC,WAAW;CAmBpB"}
@@ -1,74 +0,0 @@
1
- import EventEmitter from 'node:events';
2
- import * as fs from 'node:fs';
3
- /**
4
- * Reads length-delimited frames from a readable stream (typically a named FIFO pipe).
5
- *
6
- * Wire format: `[4-byte big-endian payload length][payload bytes]`
7
- *
8
- * Emits a `frame` event for each complete frame with the raw payload buffer.
9
- * Callers are responsible for deserializing the payload (e.g., via msgpack).
10
- *
11
- * On encountering an invalid payload length (0 or >maxPayloadSize), emits `error`
12
- * and destroys the stream.
13
- */ export class FifoFrameReader extends EventEmitter {
14
- maxPayloadSize;
15
- stream;
16
- pendingBuf;
17
- running;
18
- constructor(maxPayloadSize = 10 * 1024 * 1024){
19
- super(), this.maxPayloadSize = maxPayloadSize, this.stream = null, this.pendingBuf = Buffer.alloc(0), this.running = false;
20
- }
21
- /** Open a FIFO at the given path and start reading frames. */ start(fifoPath, highWaterMark = 64 * 1024) {
22
- this.startFromStream(fs.createReadStream(fifoPath, {
23
- highWaterMark
24
- }));
25
- }
26
- /** Start reading frames from an existing readable stream. */ startFromStream(stream) {
27
- if (this.running) {
28
- throw new Error('FifoFrameReader is already running');
29
- }
30
- this.running = true;
31
- this.pendingBuf = Buffer.alloc(0);
32
- this.stream = stream;
33
- stream.on('data', (chunk)=>{
34
- const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
35
- this.pendingBuf = this.pendingBuf.length > 0 ? Buffer.concat([
36
- this.pendingBuf,
37
- buf
38
- ]) : buf;
39
- this.drainFrames();
40
- });
41
- stream.on('error', (err)=>{
42
- if (this.running) {
43
- this.emit('error', err);
44
- }
45
- });
46
- stream.on('end', ()=>{
47
- this.emit('end');
48
- });
49
- }
50
- /** Stop reading and destroy the underlying stream. */ stop() {
51
- this.running = false;
52
- if (this.stream) {
53
- this.stream.destroy();
54
- this.stream = null;
55
- }
56
- }
57
- /** Parse complete frames out of the pending buffer. */ drainFrames() {
58
- while(this.pendingBuf.length >= 4){
59
- const payloadLen = this.pendingBuf.readUInt32BE(0);
60
- if (payloadLen === 0 || payloadLen > this.maxPayloadSize) {
61
- this.emit('error', new Error(`Invalid payload length: ${payloadLen}`));
62
- this.stop();
63
- return;
64
- }
65
- const frameLen = 4 + payloadLen;
66
- if (this.pendingBuf.length < frameLen) {
67
- break; // Wait for more data
68
- }
69
- const payload = this.pendingBuf.subarray(4, frameLen);
70
- this.pendingBuf = this.pendingBuf.subarray(frameLen);
71
- this.emit('frame', Buffer.from(payload));
72
- }
73
- }
74
- }
@@ -1,2 +0,0 @@
1
- export { FifoFrameReader } from './fifo_frame_reader.js';
2
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9maWZvL2luZGV4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxlQUFlLEVBQUUsTUFBTSx3QkFBd0IsQ0FBQyJ9
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/fifo/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC"}
@@ -1 +0,0 @@
1
- export { FifoFrameReader } from './fifo_frame_reader.js';
@@ -1,115 +0,0 @@
1
- /** Node in a doubly-linked list used by {@link LruSet}. */
2
- type LruNode<T> = {
3
- value: T;
4
- prev: LruNode<T> | undefined;
5
- next: LruNode<T> | undefined;
6
- };
7
-
8
- /**
9
- * A bounded set with Least Recently Used (LRU) eviction.
10
- * Both {@link has} and {@link add} count as an access and refresh the entry's
11
- * recency, so items that are actively checked stay in the set longest.
12
- *
13
- * Uses a doubly-linked list for O(1) ordering and a Map for O(1) lookup.
14
- * Head = least recent, tail = most recent.
15
- */
16
- export class LruSet<T> {
17
- /** Map from value to its linked-list node for O(1) lookup. */
18
- private readonly map = new Map<T, LruNode<T>>();
19
- private head: LruNode<T> | undefined;
20
- private tail: LruNode<T> | undefined;
21
-
22
- constructor(private readonly maxSize: number) {
23
- if (maxSize < 1) {
24
- throw new Error('LruSet maxSize must be at least 1');
25
- }
26
- }
27
-
28
- /** Number of entries in the set. */
29
- get size(): number {
30
- return this.map.size;
31
- }
32
-
33
- /**
34
- * Returns true if the item is in the set.
35
- * Refreshes the item's recency so it becomes the most recently used.
36
- */
37
- has(item: T): boolean {
38
- const node = this.map.get(item);
39
- if (!node) {
40
- return false;
41
- }
42
- this.moveToTail(node);
43
- return true;
44
- }
45
-
46
- /**
47
- * Adds an item to the set. If the item already exists, refreshes its recency.
48
- * If the set is at capacity, evicts the least recently used item.
49
- */
50
- add(item: T): void {
51
- const existing = this.map.get(item);
52
- if (existing) {
53
- this.moveToTail(existing);
54
- return;
55
- }
56
-
57
- if (this.map.size >= this.maxSize) {
58
- this.evictHead();
59
- }
60
-
61
- const node: LruNode<T> = { value: item, prev: this.tail, next: undefined };
62
- if (this.tail) {
63
- this.tail.next = node;
64
- } else {
65
- this.head = node;
66
- }
67
- this.tail = node;
68
- this.map.set(item, node);
69
- }
70
-
71
- /** Removes all entries from the set. */
72
- clear(): void {
73
- this.map.clear();
74
- this.head = undefined;
75
- this.tail = undefined;
76
- }
77
-
78
- /** Unlinks a node from its current position and relinks it at the tail. */
79
- private moveToTail(node: LruNode<T>): void {
80
- if (node === this.tail) {
81
- return;
82
- }
83
-
84
- // Unlink
85
- if (node.prev) {
86
- node.prev.next = node.next;
87
- } else {
88
- this.head = node.next;
89
- }
90
- if (node.next) {
91
- node.next.prev = node.prev;
92
- }
93
-
94
- // Relink at tail
95
- node.prev = this.tail;
96
- node.next = undefined;
97
- if (this.tail) {
98
- this.tail.next = node;
99
- }
100
- this.tail = node;
101
- }
102
-
103
- /** Evicts the head (least recently used) node. */
104
- private evictHead(): void {
105
- const oldHead = this.head!;
106
- this.map.delete(oldHead.value);
107
-
108
- this.head = oldHead.next;
109
- if (this.head) {
110
- this.head.prev = undefined;
111
- } else {
112
- this.tail = undefined;
113
- }
114
- }
115
- }
@@ -1,98 +0,0 @@
1
- import EventEmitter from 'node:events';
2
- import * as fs from 'node:fs';
3
- import type { Readable } from 'node:stream';
4
-
5
- /**
6
- * Events emitted by FifoFrameReader.
7
- *
8
- * - `frame`: A complete frame payload (without the 4-byte length header).
9
- * - `error`: An unrecoverable error (invalid frame length, stream error).
10
- * - `end`: The underlying stream has ended.
11
- */
12
- export interface FifoFrameReaderEvents {
13
- frame: [payload: Buffer];
14
- error: [error: Error];
15
- end: [];
16
- }
17
-
18
- /**
19
- * Reads length-delimited frames from a readable stream (typically a named FIFO pipe).
20
- *
21
- * Wire format: `[4-byte big-endian payload length][payload bytes]`
22
- *
23
- * Emits a `frame` event for each complete frame with the raw payload buffer.
24
- * Callers are responsible for deserializing the payload (e.g., via msgpack).
25
- *
26
- * On encountering an invalid payload length (0 or >maxPayloadSize), emits `error`
27
- * and destroys the stream.
28
- */
29
- export class FifoFrameReader extends EventEmitter<FifoFrameReaderEvents> {
30
- private stream: Readable | null = null;
31
- private pendingBuf: Buffer = Buffer.alloc(0);
32
- private running = false;
33
-
34
- constructor(private readonly maxPayloadSize = 10 * 1024 * 1024) {
35
- super();
36
- }
37
-
38
- /** Open a FIFO at the given path and start reading frames. */
39
- start(fifoPath: string, highWaterMark = 64 * 1024): void {
40
- this.startFromStream(fs.createReadStream(fifoPath, { highWaterMark }));
41
- }
42
-
43
- /** Start reading frames from an existing readable stream. */
44
- startFromStream(stream: Readable): void {
45
- if (this.running) {
46
- throw new Error('FifoFrameReader is already running');
47
- }
48
- this.running = true;
49
- this.pendingBuf = Buffer.alloc(0);
50
- this.stream = stream;
51
-
52
- stream.on('data', (chunk: Buffer | string) => {
53
- const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
54
- this.pendingBuf = this.pendingBuf.length > 0 ? Buffer.concat([this.pendingBuf, buf]) : buf;
55
- this.drainFrames();
56
- });
57
-
58
- stream.on('error', (err: Error) => {
59
- if (this.running) {
60
- this.emit('error', err);
61
- }
62
- });
63
-
64
- stream.on('end', () => {
65
- this.emit('end');
66
- });
67
- }
68
-
69
- /** Stop reading and destroy the underlying stream. */
70
- stop(): void {
71
- this.running = false;
72
- if (this.stream) {
73
- this.stream.destroy();
74
- this.stream = null;
75
- }
76
- }
77
-
78
- /** Parse complete frames out of the pending buffer. */
79
- private drainFrames(): void {
80
- while (this.pendingBuf.length >= 4) {
81
- const payloadLen = this.pendingBuf.readUInt32BE(0);
82
- if (payloadLen === 0 || payloadLen > this.maxPayloadSize) {
83
- this.emit('error', new Error(`Invalid payload length: ${payloadLen}`));
84
- this.stop();
85
- return;
86
- }
87
-
88
- const frameLen = 4 + payloadLen;
89
- if (this.pendingBuf.length < frameLen) {
90
- break; // Wait for more data
91
- }
92
-
93
- const payload = this.pendingBuf.subarray(4, frameLen);
94
- this.pendingBuf = this.pendingBuf.subarray(frameLen);
95
- this.emit('frame', Buffer.from(payload));
96
- }
97
- }
98
- }
package/src/fifo/index.ts DELETED
@@ -1 +0,0 @@
1
- export { FifoFrameReader } from './fifo_frame_reader.js';