@zvndev/powdb-client 0.6.1 → 0.7.1

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/dist/index.d.ts CHANGED
@@ -18,7 +18,7 @@ import * as tls from "node:tls";
18
18
  import { EventEmitter } from "node:events";
19
19
  import { type TypedRow, type TypedSchema } from "./typed.js";
20
20
  /** Client library version. Compared to the server's reported version. */
21
- export declare const CLIENT_VERSION = "0.6.1";
21
+ export declare const CLIENT_VERSION = "0.7.1";
22
22
  export type QueryResult = {
23
23
  kind: "rows";
24
24
  columns: string[];
@@ -43,8 +43,17 @@ export type QueryResult = {
43
43
  */
44
44
  export type QueryParam = string | number | bigint | boolean | null;
45
45
  export interface ClientOptions {
46
- host: string;
47
- port: number;
46
+ /** TCP host. Required unless `path` (a Unix domain socket) is given. */
47
+ host?: string;
48
+ /** TCP port. Required unless `path` (a Unix domain socket) is given. */
49
+ port?: number;
50
+ /**
51
+ * Path to a Unix domain socket. When set, the client connects over the
52
+ * socket instead of TCP (same-host, ~2× lower round-trip latency) and
53
+ * `host`/`port`/`tls` are ignored. Requires a server started with
54
+ * `--socket <path>`.
55
+ */
56
+ path?: string;
48
57
  dbName?: string;
49
58
  password?: string | null;
50
59
  /**
package/dist/index.js CHANGED
@@ -21,7 +21,7 @@ import { encode, tryDecode, } from "./protocol.js";
21
21
  import { PowDBError } from "./errors.js";
22
22
  import { coerceRows, } from "./typed.js";
23
23
  /** Client library version. Compared to the server's reported version. */
24
- export const CLIENT_VERSION = "0.6.1";
24
+ export const CLIENT_VERSION = "0.7.1";
25
25
  function socketChunkToBuffer(chunk) {
26
26
  return typeof chunk === "string" ? Buffer.from(chunk) : chunk;
27
27
  }
@@ -85,8 +85,11 @@ export class Client extends EventEmitter {
85
85
  }
86
86
  /** Open a connection, send Connect, wait for ConnectOk. */
87
87
  static async connect(opts) {
88
- const { host, port, dbName = "default", password = null, user, connectTimeoutMs = 5000, tls: tlsOpt = false, } = opts;
89
- const socket = await openSocket(host, port, connectTimeoutMs, tlsOpt);
88
+ const { host, port, path, dbName = "default", password = null, user, connectTimeoutMs = 5000, tls: tlsOpt = false, } = opts;
89
+ if (path === undefined && (host === undefined || port === undefined)) {
90
+ throw new PowDBError("connect requires either { path } (Unix socket) or { host, port } (TCP)", "connect_failed");
91
+ }
92
+ const socket = await openSocket({ host, port, path }, connectTimeoutMs, tlsOpt);
90
93
  // We need to read the initial ConnectOk before wiring up the normal
91
94
  // pending-queue machinery, so we do a one-shot handshake here.
92
95
  const handshake = new Promise((resolve, reject) => {
@@ -147,7 +150,7 @@ export class Client extends EventEmitter {
147
150
  const serverMajor = majorOf(reply.version);
148
151
  const clientMajor = majorOf(CLIENT_VERSION);
149
152
  if (serverMajor !== clientMajor) {
150
- const key = `${host}:${port}`;
153
+ const key = path ?? `${host}:${port}`;
151
154
  if (!versionWarnings.has(key)) {
152
155
  versionWarnings.add(key);
153
156
  console.warn(`[powdb] server version ${reply.version} major (${serverMajor}) ` +
@@ -555,7 +558,7 @@ export class Client extends EventEmitter {
555
558
  }
556
559
  }
557
560
  }
558
- function openSocket(host, port, timeoutMs, tlsOpt) {
561
+ function openSocket(target, timeoutMs, tlsOpt) {
559
562
  return new Promise((resolve, reject) => {
560
563
  let socket;
561
564
  const timer = setTimeout(() => {
@@ -565,8 +568,8 @@ function openSocket(host, port, timeoutMs, tlsOpt) {
565
568
  const onConnect = () => {
566
569
  clearTimeout(timer);
567
570
  socket.setNoDelay(true);
568
- // Enable TCP keepalive on the underlying OS socket so dead peers are
569
- // detected even when the app is otherwise idle.
571
+ // Enable keepalive on the underlying OS socket so dead peers are detected
572
+ // even when the app is otherwise idle. (No-op for Unix sockets.)
570
573
  socket.setKeepAlive(true, 30_000);
571
574
  resolve(socket);
572
575
  };
@@ -576,12 +579,19 @@ function openSocket(host, port, timeoutMs, tlsOpt) {
576
579
  // branch on `.code === "connect_failed"` rather than string-matching.
577
580
  reject(new PowDBError(`connect failed: ${err.message}`, "connect_failed", { cause: err }));
578
581
  };
579
- if (tlsOpt) {
582
+ if (target.path !== undefined) {
583
+ // Unix domain socket: no TLS (local, same-host), no host/port.
584
+ socket = new net.Socket();
585
+ socket.once("connect", onConnect);
586
+ socket.once("error", onError);
587
+ socket.connect(target.path);
588
+ }
589
+ else if (tlsOpt) {
580
590
  // TLS path: `tls.connect` wraps an underlying net.Socket. `secureConnect`
581
591
  // fires once the TLS handshake is complete — that is the right hook for
582
592
  // "ready to send application data".
583
593
  const tlsOptions = tlsOpt === true ? {} : tlsOpt;
584
- const tlsSock = tls.connect(port, host, tlsOptions);
594
+ const tlsSock = tls.connect(target.port, target.host, tlsOptions);
585
595
  socket = tlsSock;
586
596
  tlsSock.once("secureConnect", onConnect);
587
597
  tlsSock.once("error", onError);
@@ -590,7 +600,7 @@ function openSocket(host, port, timeoutMs, tlsOpt) {
590
600
  socket = new net.Socket();
591
601
  socket.once("connect", onConnect);
592
602
  socket.once("error", onError);
593
- socket.connect(port, host);
603
+ socket.connect(target.port, target.host);
594
604
  }
595
605
  });
596
606
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zvndev/powdb-client",
3
- "version": "0.6.1",
3
+ "version": "0.7.1",
4
4
  "description": "TypeScript client for PowDB (PowQL wire protocol)",
5
5
  "type": "module",
6
6
  "packageManager": "pnpm@10.29.3",