@ultimat3/realtime 31.16.0 → 31.18.0

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": "@ultimat3/realtime",
3
- "version": "31.16.0",
3
+ "version": "31.18.0",
4
4
  "description": "Three-tier realtime: channels, live queries, local-first sync — one protocol, one mutator shape",
5
5
  "keywords": [
6
6
  "ultimate",
@@ -52,10 +52,10 @@
52
52
  "test": "bun test"
53
53
  },
54
54
  "dependencies": {
55
- "@ultimat3/core": "31.16.0",
56
- "@ultimat3/entity": "31.16.0",
57
- "@ultimat3/policy": "31.16.0",
58
- "@ultimat3/query": "31.16.0",
55
+ "@ultimat3/core": "31.18.0",
56
+ "@ultimat3/entity": "31.18.0",
57
+ "@ultimat3/policy": "31.18.0",
58
+ "@ultimat3/query": "31.18.0",
59
59
  "nats": "2.29.3"
60
60
  }
61
61
  }
package/src/pg-bytes.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  // Single responsibility: the byte primitives every Postgres frame is built from — big-endian
2
2
  // integers, NUL-terminated strings, length-prefixed blobs. Shared by the wire framing, the auth
3
3
  // handshake and the pgoutput decoder so none of them owns a second copy of "read an Int32".
4
- // A read past the end is `X_REPLICATION_PROTOCOL`, never a silent `NaN` or an empty string.
4
+ // A read past the end, or a negative length, is `X_REPLICATION_PROTOCOL` — never a silent misread.
5
5
 
6
6
  import { ReplicationProtocolError } from './errors';
7
7
 
@@ -32,6 +32,14 @@ export class ByteReader {
32
32
 
33
33
  #need(count: number): number {
34
34
  const at = this.#at;
35
+ // A length is off the wire: a negative one would move the cursor BACKWARDS and every later read
36
+ // would succeed on the wrong bytes — a silent misread, not a truncation (`property-pgoutput`).
37
+ if (count < 0 || !Number.isInteger(count)) {
38
+ throw new ReplicationProtocolError({
39
+ stage: this.#stage,
40
+ detail: `a length of ${count} at offset ${at} is not a byte count`,
41
+ });
42
+ }
35
43
  if (at + count > this.#bytes.length) {
36
44
  throw new ReplicationProtocolError({
37
45
  stage: this.#stage,
@@ -85,6 +93,18 @@ export class ByteReader {
85
93
  return decoder.decode(this.take(count));
86
94
  }
87
95
 
96
+ /**
97
+ * The message is over: a format that fixes its own length has no tail, and bytes after it mean
98
+ * the frame was cut or built at the wrong boundary — refused rather than ignored.
99
+ */
100
+ end(): void {
101
+ if (this.remaining === 0) return;
102
+ throw new ReplicationProtocolError({
103
+ stage: this.#stage,
104
+ detail: `${this.remaining} bytes follow a complete message at offset ${this.#at}`,
105
+ });
106
+ }
107
+
88
108
  /** NUL-terminated string. A frame that never terminates one is a truncated frame. */
89
109
  cstring(): string {
90
110
  const end = this.#bytes.indexOf(0, this.#at);
package/src/pgoutput.ts CHANGED
@@ -156,6 +156,13 @@ export class PgOutputDecoder {
156
156
  decode(payload: Uint8Array): PgOutputMessage {
157
157
  const reader = new ByteReader(payload, 'pgoutput');
158
158
  const tag = reader.tag();
159
+ const message = this.#decodeTagged(tag, reader);
160
+ // Protocol 1 fixes every decoded message's length; an 'other' tag is skipped unread.
161
+ if (message.kind !== 'other') reader.end();
162
+ return message;
163
+ }
164
+
165
+ #decodeTagged(tag: string, reader: ByteReader): PgOutputMessage {
159
166
  switch (tag) {
160
167
  case 'B':
161
168
  return this.#decodeBegin(reader);