zydecodb 0.9.0-beta.2

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.
@@ -0,0 +1,173 @@
1
+ import net from "node:net";
2
+ import { once } from "node:events";
3
+ import { ConnectionError, fromStatus } from "./errors.js";
4
+ import { encodeHeader, HEADER_LEN, PROTO_VERSION, Status, Cmd } from "./protocol.js";
5
+ /**
6
+ * A single TCP connection to a ZydecoDB server. Safe for concurrent use:
7
+ * requests are pipelined. The writer queue respects OS backpressure (drain),
8
+ * and the reader loop continuously consumes frames to resolve promises in FIFO order.
9
+ */
10
+ export class Connection {
11
+ socket = null;
12
+ inFlight = [];
13
+ writeQueue = [];
14
+ writing = false;
15
+ dead = false;
16
+ lastUsed = 0;
17
+ host;
18
+ port;
19
+ timeoutMs;
20
+ apiKey;
21
+ constructor(host, port, timeoutMs, apiKey) {
22
+ this.host = host;
23
+ this.port = port;
24
+ this.timeoutMs = timeoutMs;
25
+ this.apiKey = apiKey;
26
+ }
27
+ get connected() {
28
+ return this.socket !== null && !this.dead;
29
+ }
30
+ connect() {
31
+ return new Promise((resolve, reject) => {
32
+ const socket = net.connect({ host: this.host, port: this.port });
33
+ socket.setNoDelay(true); // requests are small and latency-sensitive
34
+ const onError = (err) => {
35
+ this.fail(new ConnectionError(`connect to ${this.host}:${this.port} failed`, { cause: err }));
36
+ reject(this.lastError(err));
37
+ };
38
+ socket.once("error", onError);
39
+ socket.once("connect", () => {
40
+ socket.removeListener("error", onError);
41
+ this.socket = socket;
42
+ this.lastUsed = Date.now();
43
+ // Start the async reader loop
44
+ this.readLoop(socket).catch(err => {
45
+ this.fail(new ConnectionError("read loop failed", { cause: err }));
46
+ });
47
+ socket.on("error", (err) => this.fail(new ConnectionError("socket error", { cause: err })));
48
+ socket.on("close", () => this.fail(new ConnectionError("connection closed")));
49
+ if (this.apiKey) {
50
+ this.sessionInit(this.apiKey).then(resolve, reject);
51
+ }
52
+ else {
53
+ resolve();
54
+ }
55
+ });
56
+ });
57
+ }
58
+ lastError(cause) {
59
+ return new ConnectionError("connection error", { cause });
60
+ }
61
+ async sessionInit(apiKey) {
62
+ const res = await this.request(Cmd.SessionInit, Buffer.from(apiKey, "utf8"));
63
+ if (res.status !== Status.Ok) {
64
+ throw fromStatus(res.status, "SessionInit", res.body);
65
+ }
66
+ }
67
+ /** Send one framed request and resolve with the framed response. */
68
+ request(command, payload = Buffer.alloc(0)) {
69
+ return new Promise((resolve, reject) => {
70
+ if (!this.socket || this.dead) {
71
+ reject(new ConnectionError("not connected"));
72
+ return;
73
+ }
74
+ const timer = setTimeout(() => {
75
+ this.fail(new ConnectionError("request timed out"));
76
+ }, this.timeoutMs);
77
+ this.inFlight.push({ resolve, reject, timer });
78
+ const frame = Buffer.concat([encodeHeader(command, payload.length), payload]);
79
+ this.writeQueue.push({ frame, reject });
80
+ this.pumpWrites();
81
+ });
82
+ }
83
+ async pumpWrites() {
84
+ if (this.writing || !this.socket || this.dead)
85
+ return;
86
+ this.writing = true;
87
+ try {
88
+ this.socket.cork();
89
+ while (this.writeQueue.length > 0) {
90
+ if (this.dead || !this.socket)
91
+ break;
92
+ const task = this.writeQueue.shift();
93
+ const canContinue = this.socket.write(task.frame, (err) => {
94
+ if (err) {
95
+ task.reject(new ConnectionError("write failed", { cause: err }));
96
+ this.fail(new ConnectionError("write failed", { cause: err }));
97
+ }
98
+ });
99
+ // Respect backpressure
100
+ if (!canContinue && !this.dead && this.socket) {
101
+ this.socket.uncork();
102
+ await once(this.socket, "drain");
103
+ if (!this.dead && this.socket)
104
+ this.socket.cork();
105
+ }
106
+ }
107
+ if (!this.dead && this.socket)
108
+ this.socket.uncork();
109
+ }
110
+ catch (err) {
111
+ this.fail(new ConnectionError("write loop error", { cause: err }));
112
+ }
113
+ finally {
114
+ this.writing = false;
115
+ }
116
+ }
117
+ async readLoop(socket) {
118
+ let inbox = Buffer.alloc(0);
119
+ for await (const chunk of socket) {
120
+ inbox = inbox.length === 0 ? chunk : Buffer.concat([inbox, chunk]);
121
+ while (inbox.length >= HEADER_LEN) {
122
+ const version = inbox.readUInt8(0);
123
+ if (version !== PROTO_VERSION) {
124
+ throw new ConnectionError(`unexpected protocol version 0x${version.toString(16)}`);
125
+ }
126
+ const status = inbox.readUInt8(1);
127
+ const length = inbox.readUInt32BE(2);
128
+ if (inbox.length < HEADER_LEN + length)
129
+ break; // wait for more chunks
130
+ const body = inbox.subarray(HEADER_LEN, HEADER_LEN + length);
131
+ inbox = inbox.subarray(HEADER_LEN + length);
132
+ const pending = this.inFlight.shift();
133
+ if (!pending) {
134
+ throw new ConnectionError("received response with no pending request");
135
+ }
136
+ clearTimeout(pending.timer);
137
+ this.lastUsed = Date.now();
138
+ pending.resolve({ status, body: Buffer.from(body) });
139
+ }
140
+ }
141
+ }
142
+ /** Tear down the connection and reject any in-flight requests. */
143
+ fail(err) {
144
+ if (this.dead)
145
+ return;
146
+ this.dead = true;
147
+ const queue = this.inFlight;
148
+ this.inFlight = [];
149
+ for (const pending of queue) {
150
+ clearTimeout(pending.timer);
151
+ pending.reject(err);
152
+ }
153
+ this.close();
154
+ }
155
+ close() {
156
+ if (this.socket) {
157
+ this.socket.removeAllListeners();
158
+ this.socket.destroy();
159
+ this.socket = null;
160
+ }
161
+ }
162
+ /** Send a keepalive; resolves true if the server answered OK. */
163
+ async ping() {
164
+ try {
165
+ const res = await this.request(Cmd.Ping);
166
+ return res.status === Status.Ok;
167
+ }
168
+ catch {
169
+ return false;
170
+ }
171
+ }
172
+ }
173
+ //# sourceMappingURL=connection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connection.js","sourceRoot":"","sources":["../src/connection.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,UAAU,CAAC;AAC3B,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAEnC,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,UAAU,EAA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AG,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YACzC,OAAO,GAAG,CAAC,MAAM,KAAK,MAAM,CAAC,EAAE,CAAC;QAClC,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,36 @@
1
+ /** Base class for every error thrown by this driver. */
2
+ export declare class ZydecoError extends Error {
3
+ }
4
+ /**
5
+ * A transport-level failure (connect/write/read). Safe to retry for idempotent
6
+ * operations; the client does this automatically.
7
+ */
8
+ export declare class ConnectionError extends ZydecoError {
9
+ constructor(message: string, options?: {
10
+ cause?: unknown;
11
+ });
12
+ }
13
+ /**
14
+ * A non-OK response from the server. `status` is the wire status byte so callers
15
+ * can branch on the failure class without string-matching messages.
16
+ */
17
+ export declare class ServerError extends ZydecoError {
18
+ readonly status: number;
19
+ readonly op: string;
20
+ constructor(op: string, status: number, detail: string);
21
+ }
22
+ /** A constraint conflict, e.g. a unique-index violation (status 0x03). */
23
+ export declare class ConflictError extends ServerError {
24
+ }
25
+ /** Unauthorized or forbidden (status 0x0B / 0x0C). */
26
+ export declare class AuthError extends ServerError {
27
+ }
28
+ /** The server is shedding load (status 0x07). Retried automatically. */
29
+ export declare class ServerBusyError extends ServerError {
30
+ }
31
+ /** The server rejected the request as malformed or invalid. */
32
+ export declare class InvalidRequestError extends ServerError {
33
+ }
34
+ /** Build the most specific ServerError for a non-OK response. */
35
+ export declare function fromStatus(status: number, op: string, payload: Buffer): ServerError;
36
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAEA,wDAAwD;AACxD,qBAAa,WAAY,SAAQ,KAAK;CAAG;AAEzC;;;GAGG;AACH,qBAAa,eAAgB,SAAQ,WAAW;gBAClC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE;CAI3D;AAED;;;GAGG;AACH,qBAAa,WAAY,SAAQ,WAAW;IAC1C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;gBAER,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;CAOvD;AAED,0EAA0E;AAC1E,qBAAa,aAAc,SAAQ,WAAW;CAAG;AAEjD,sDAAsD;AACtD,qBAAa,SAAU,SAAQ,WAAW;CAAG;AAE7C,wEAAwE;AACxE,qBAAa,eAAgB,SAAQ,WAAW;CAAG;AAEnD,+DAA+D;AAC/D,qBAAa,mBAAoB,SAAQ,WAAW;CAAG;AAEvD,iEAAiE;AACjE,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,WAAW,CAiBnF"}
package/dist/errors.js ADDED
@@ -0,0 +1,61 @@
1
+ import { Status, statusName } from "./protocol.js";
2
+ /** Base class for every error thrown by this driver. */
3
+ export class ZydecoError extends Error {
4
+ }
5
+ /**
6
+ * A transport-level failure (connect/write/read). Safe to retry for idempotent
7
+ * operations; the client does this automatically.
8
+ */
9
+ export class ConnectionError extends ZydecoError {
10
+ constructor(message, options) {
11
+ super(message, options);
12
+ this.name = "ConnectionError";
13
+ }
14
+ }
15
+ /**
16
+ * A non-OK response from the server. `status` is the wire status byte so callers
17
+ * can branch on the failure class without string-matching messages.
18
+ */
19
+ export class ServerError extends ZydecoError {
20
+ status;
21
+ op;
22
+ constructor(op, status, detail) {
23
+ const base = `${op} failed: ${statusName(status)}`;
24
+ super(detail ? `${base} (${detail})` : base);
25
+ this.name = "ServerError";
26
+ this.op = op;
27
+ this.status = status;
28
+ }
29
+ }
30
+ /** A constraint conflict, e.g. a unique-index violation (status 0x03). */
31
+ export class ConflictError extends ServerError {
32
+ }
33
+ /** Unauthorized or forbidden (status 0x0B / 0x0C). */
34
+ export class AuthError extends ServerError {
35
+ }
36
+ /** The server is shedding load (status 0x07). Retried automatically. */
37
+ export class ServerBusyError extends ServerError {
38
+ }
39
+ /** The server rejected the request as malformed or invalid. */
40
+ export class InvalidRequestError extends ServerError {
41
+ }
42
+ /** Build the most specific ServerError for a non-OK response. */
43
+ export function fromStatus(status, op, payload) {
44
+ const detail = payload.length ? payload.toString("utf8") : "";
45
+ switch (status) {
46
+ case Status.Conflict:
47
+ return new ConflictError(op, status, detail);
48
+ case Status.Unauthorized:
49
+ case Status.Forbidden:
50
+ return new AuthError(op, status, detail);
51
+ case Status.EngineBusy:
52
+ return new ServerBusyError(op, status, detail);
53
+ case Status.ProtocolError:
54
+ case Status.InvalidKey:
55
+ case Status.InvalidValue:
56
+ return new InvalidRequestError(op, status, detail);
57
+ default:
58
+ return new ServerError(op, status, detail);
59
+ }
60
+ }
61
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAEnD,wDAAwD;AACxD,MAAM,OAAO,WAAY,SAAQ,KAAK;CAAG;AAEzC;;;GAGG;AACH,MAAM,OAAO,eAAgB,SAAQ,WAAW;IAC9C,YAAY,OAAe,EAAE,OAA6B;QACxD,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAChC,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,OAAO,WAAY,SAAQ,WAAW;IACjC,MAAM,CAAS;IACf,EAAE,CAAS;IAEpB,YAAY,EAAU,EAAE,MAAc,EAAE,MAAc;QACpD,MAAM,IAAI,GAAG,GAAG,EAAE,YAAY,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;QACnD,KAAK,CAAC,MAAM,CAAC,CAAC,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IAAI,WAAW,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IAC/C,CAAC;AACH,CAAC"}
@@ -0,0 +1,7 @@
1
+ export { Client, generateId } from "./client.ts";
2
+ export type { ClientOptions, FindOptions, UpdateResult } from "./client.ts";
3
+ export { Collection } from "./collection.ts";
4
+ export type { Document, QueryOptions } from "./collection.ts";
5
+ export { AuthError, ConflictError, ConnectionError, InvalidRequestError, ServerBusyError, ServerError, ZydecoError, } from "./errors.ts";
6
+ export type { Projection, Row, SortKey } from "./protocol.ts";
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACjD,YAAY,EAAE,aAAa,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC5E,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,YAAY,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC9D,OAAO,EACL,SAAS,EACT,aAAa,EACb,eAAe,EACf,mBAAmB,EACnB,eAAe,EACf,WAAW,EACX,WAAW,GACZ,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,UAAU,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { Client, generateId } from "./client.js";
2
+ export { Collection } from "./collection.js";
3
+ export { AuthError, ConflictError, ConnectionError, InvalidRequestError, ServerBusyError, ServerError, ZydecoError, } from "./errors.js";
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEjD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C,OAAO,EACL,SAAS,EACT,aAAa,EACb,eAAe,EACf,mBAAmB,EACnB,eAAe,EACf,WAAW,EACX,WAAW,GACZ,MAAM,aAAa,CAAC"}
package/dist/pool.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ import { Connection } from "./connection.ts";
2
+ export interface PoolOptions {
3
+ host: string;
4
+ port: number;
5
+ apiKey: string | null;
6
+ timeoutMs: number;
7
+ maxSize: number;
8
+ acquireTimeoutMs?: number;
9
+ keepaliveIdleMs?: number;
10
+ }
11
+ /**
12
+ * A multiplexing connection pool. Because the server executes requests serially
13
+ * per connection but processes pipelined frames without requiring a round-trip
14
+ * between them, we can safely and efficiently multiplex many concurrent requests
15
+ * across a small pool of open connections. This avoids artificial queueing
16
+ * timeouts and allows high-throughput `Promise.all` patterns.
17
+ */
18
+ export declare class ConnectionPool {
19
+ private readonly connections;
20
+ private total;
21
+ private nextIdx;
22
+ private closed;
23
+ private connecting;
24
+ private readonly keepaliveIdleMs;
25
+ private readonly opts;
26
+ constructor(opts: PoolOptions);
27
+ private newConnection;
28
+ acquire(): Promise<Connection>;
29
+ /** Release is a no-op in a multiplexing pool, but we validate it is alive. */
30
+ release(conn: Connection): void;
31
+ /** Permanently drop a (presumed-broken) connection, freeing its slot. */
32
+ discard(conn: Connection): void;
33
+ close(): void;
34
+ }
35
+ //# sourceMappingURL=pool.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pool.d.ts","sourceRoot":"","sources":["../src/pool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAG7C,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED;;;;;;GAMG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAoB;IAChD,OAAO,CAAC,KAAK,CAAK;IAClB,OAAO,CAAC,OAAO,CAAK;IACpB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,UAAU,CAAoC;IAEtD,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAS;IACzC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAc;gBAEvB,IAAI,EAAE,WAAW;IAK7B,OAAO,CAAC,aAAa;IAIf,OAAO,IAAI,OAAO,CAAC,UAAU,CAAC;IAoDpC,8EAA8E;IAC9E,OAAO,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI;IAM/B,yEAAyE;IACzE,OAAO,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI;IAS/B,KAAK,IAAI,IAAI;CAMd"}
package/dist/pool.js ADDED
@@ -0,0 +1,94 @@
1
+ import { Connection } from "./connection.js";
2
+ import { ConnectionError } from "./errors.js";
3
+ /**
4
+ * A multiplexing connection pool. Because the server executes requests serially
5
+ * per connection but processes pipelined frames without requiring a round-trip
6
+ * between them, we can safely and efficiently multiplex many concurrent requests
7
+ * across a small pool of open connections. This avoids artificial queueing
8
+ * timeouts and allows high-throughput `Promise.all` patterns.
9
+ */
10
+ export class ConnectionPool {
11
+ connections = [];
12
+ total = 0;
13
+ nextIdx = 0;
14
+ closed = false;
15
+ connecting = null;
16
+ keepaliveIdleMs;
17
+ opts;
18
+ constructor(opts) {
19
+ this.opts = opts;
20
+ this.keepaliveIdleMs = opts.keepaliveIdleMs ?? 30_000;
21
+ }
22
+ newConnection() {
23
+ return new Connection(this.opts.host, this.opts.port, this.opts.timeoutMs, this.opts.apiKey);
24
+ }
25
+ async acquire() {
26
+ if (this.closed)
27
+ throw new ConnectionError("pool is closed");
28
+ // Clean out dead connections
29
+ for (let i = this.connections.length - 1; i >= 0; i--) {
30
+ const c = this.connections[i];
31
+ if (c && !c.connected) {
32
+ this.connections.splice(i, 1);
33
+ this.total -= 1;
34
+ }
35
+ }
36
+ // Lazily grow the pool up to maxSize if we're under capacity.
37
+ if (this.total < this.opts.maxSize) {
38
+ if (!this.connecting) {
39
+ this.connecting = (async () => {
40
+ try {
41
+ const fresh = this.newConnection();
42
+ await fresh.connect();
43
+ this.connections.push(fresh);
44
+ this.total += 1;
45
+ return fresh;
46
+ }
47
+ finally {
48
+ this.connecting = null;
49
+ }
50
+ })();
51
+ }
52
+ // If we literally have 0 active connections, we MUST wait for this first one.
53
+ if (this.connections.length === 0) {
54
+ return this.connecting;
55
+ }
56
+ // Otherwise, fall through and multiplex on the existing ones
57
+ // while the new connection builds in the background!
58
+ }
59
+ // Multiplex round-robin
60
+ const conn = this.connections[this.nextIdx % this.connections.length];
61
+ this.nextIdx++;
62
+ // Idle validation
63
+ if (Date.now() - conn.lastUsed > this.keepaliveIdleMs) {
64
+ if (!(await conn.ping())) {
65
+ this.discard(conn);
66
+ return this.acquire(); // Try again
67
+ }
68
+ }
69
+ return conn;
70
+ }
71
+ /** Release is a no-op in a multiplexing pool, but we validate it is alive. */
72
+ release(conn) {
73
+ if (this.closed || !conn.connected) {
74
+ this.discard(conn);
75
+ }
76
+ }
77
+ /** Permanently drop a (presumed-broken) connection, freeing its slot. */
78
+ discard(conn) {
79
+ conn.close();
80
+ const idx = this.connections.indexOf(conn);
81
+ if (idx >= 0) {
82
+ this.connections.splice(idx, 1);
83
+ this.total -= 1;
84
+ }
85
+ }
86
+ close() {
87
+ this.closed = true;
88
+ for (const conn of this.connections)
89
+ conn.close();
90
+ this.total = 0;
91
+ this.connections.length = 0;
92
+ }
93
+ }
94
+ //# sourceMappingURL=pool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pool.js","sourceRoot":"","sources":["../src/pool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,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AAM,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACnC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACrB,CAAC;IACH,CAAC;IAED,yEAAyE;IACzE,OAAO,CAAC,IAAgB;QACtB,IAAI,CAAC,KAAK,EAAE,CAAC;QACb,MAAM,GAAG,GAAG,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAC3C,IAAI,GAAG,IAAI,CAAC,EAAE,CAAC;YACb,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;YAChC,IAAI,CAAC,KAAK,IAAI,CAAC,CAAC;QAClB,CAAC;IACH,CAAC;IAED,KAAK;QACH,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QACnB,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,WAAW;YAAE,IAAI,CAAC,KAAK,EAAE,CAAC;QAClD,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC;QACf,IAAI,CAAC,WAAW,CAAC,MAAM,GAAG,CAAC,CAAC;IAC9B,CAAC;CACF"}
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Binary wire protocol: command/status codes and the encode/decode of payload
3
+ * bodies, with no I/O. Mirrors the Rust definitions in
4
+ * crates/zydecodb-engine/src/frame.rs and crates/zydecodb-document/src/wire.rs
5
+ * and is verified byte-for-byte against clients/conformance/vectors.json.
6
+ */
7
+ export declare const PROTO_VERSION = 1;
8
+ export declare const HEADER_LEN = 6;
9
+ export declare const Cmd: {
10
+ readonly Put: 1;
11
+ readonly Get: 2;
12
+ readonly Del: 3;
13
+ readonly Query: 32;
14
+ readonly DocPut: 33;
15
+ readonly DocDel: 34;
16
+ readonly Find: 35;
17
+ readonly Update: 36;
18
+ readonly Delete: 37;
19
+ readonly Count: 38;
20
+ readonly IndexDef: 48;
21
+ readonly SessionInit: 64;
22
+ readonly Ping: 240;
23
+ readonly Stats: 241;
24
+ };
25
+ export declare const Proj: {
26
+ readonly None: 0;
27
+ readonly Include: 1;
28
+ readonly Exclude: 2;
29
+ };
30
+ export declare const Status: {
31
+ readonly Ok: 0;
32
+ readonly NotFound: 1;
33
+ readonly Error: 2;
34
+ readonly Conflict: 3;
35
+ readonly IoError: 4;
36
+ readonly InvalidKey: 5;
37
+ readonly InvalidValue: 6;
38
+ readonly EngineBusy: 7;
39
+ readonly ProtocolError: 8;
40
+ readonly Unauthorized: 11;
41
+ readonly Forbidden: 12;
42
+ };
43
+ export declare function statusName(status: number): string;
44
+ /** Build the 6-byte request envelope header. */
45
+ export declare function encodeHeader(command: number, payloadLen: number): Buffer;
46
+ /** Build a Put payload. */
47
+ export declare function encodePut(key: Buffer, value: Buffer, expiresAt?: number | bigint): Buffer;
48
+ /** Build a Key payload for Get/Del. */
49
+ export declare function encodeKey(key: Buffer): Buffer;
50
+ /**
51
+ * DocPut payload: [collection][doc_id][body][flags]. `body` is already-
52
+ * serialized JSON (the codec treats it as opaque bytes).
53
+ */
54
+ export declare function encodeDocPut(collection: string, docId: Buffer, body: Buffer, relaxed: boolean): Buffer;
55
+ /** DocDel payload: [collection][doc_id]. */
56
+ export declare function encodeDocDel(collection: string, docId: Buffer): Buffer;
57
+ /** IndexDef payload: [collection][index][unique u8][field_count u32]{[field]}. */
58
+ export declare function encodeIndexDef(collection: string, index: string, fields: string[], unique: boolean): Buffer;
59
+ /** By-id Query payload: [mode][collection][doc_id]. */
60
+ export declare function encodeQueryById(collection: string, docId: Buffer): Buffer;
61
+ /**
62
+ * Index-range Query payload: [mode][collection][index][limit u32][lo][hi][cursor].
63
+ * `lo`/`hi` are JSON-array bound bytes (empty = unbounded); `cursor` is an
64
+ * opaque page token (empty = first page).
65
+ */
66
+ export declare function encodeQueryIndexRange(collection: string, index: string, lo: Buffer, hi: Buffer, cursor: Buffer, limit: number): Buffer;
67
+ /** One ordering term: a dotted field path and its direction. */
68
+ export interface SortKey {
69
+ field: string;
70
+ ascending: boolean;
71
+ }
72
+ /** Field selection. `mode` is Proj.None/Include/Exclude. */
73
+ export interface Projection {
74
+ mode: number;
75
+ fields: string[];
76
+ }
77
+ /** Find payload. `filter` is opaque JSON bytes (empty = match all). */
78
+ export declare function encodeFind(collection: string, filter: Buffer, sort: SortKey[], projection: Projection, skip: number, limit: number, cursor: Buffer): Buffer;
79
+ /** Update payload: [collection][filter][update][multi u8][flags]. */
80
+ export declare function encodeUpdate(collection: string, filter: Buffer, update: Buffer, multi: boolean, relaxed: boolean): Buffer;
81
+ /** Filter-based Delete payload: [collection][filter][multi u8][flags]. */
82
+ export declare function encodeDelete(collection: string, filter: Buffer, multi: boolean, relaxed: boolean): Buffer;
83
+ /** Count payload: [mode][collection][filter]. */
84
+ export declare function encodeCount(collection: string, filter: Buffer): Buffer;
85
+ /** Distinct payload: [mode][collection][filter][field]. */
86
+ export declare function encodeDistinct(collection: string, filter: Buffer, field: string): Buffer;
87
+ /** One decoded row from a query/find response page. */
88
+ export interface Row {
89
+ docId: Buffer;
90
+ body: Buffer;
91
+ }
92
+ /** A decoded response page. `cursor` is null when there are no more pages. */
93
+ export interface Page {
94
+ rows: Row[];
95
+ cursor: Buffer | null;
96
+ }
97
+ /** Decode a response page: [row_count u32]{[doc_id][body]}[cursor]. */
98
+ export declare function decodePage(buf: Buffer): Page;
99
+ //# sourceMappingURL=protocol.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../src/protocol.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,eAAO,MAAM,aAAa,IAAO,CAAC;AAClC,eAAO,MAAM,UAAU,IAAI,CAAC;AAG5B,eAAO,MAAM,GAAG;;;;;;;;;;;;;;;CAeN,CAAC;AASX,eAAO,MAAM,IAAI;;;;CAIP,CAAC;AAMX,eAAO,MAAM,MAAM;;;;;;;;;;;;CAYT,CAAC;AAgBX,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAEjD;AAED,gDAAgD;A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