@dbx-tools/lakebox

中文文档
0.9.206发布于 2026/11/11Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 5704

Databricks Lakebox REST, pooled SSH transport, and deterministic user routing

原始使用文档
文档目录5 节
  1. Provision a Lakebox
  2. Connect and run a command
  3. Stop or delete a Lakebox
  4. Configuration
  5. Understand lifecycle and transport limits

@dbx-tools/lakebox

Connect to, provision, and run commands on Databricks Lakeboxes.

Provision a Lakebox

import { client as authClient } from "@dbx-tools/auth";
import { LakeboxClient } from "@dbx-tools/lakebox/rest";
import { ensureLakeboxRunning } from "@dbx-tools/lakebox/lifecycle";

const auth = await authClient.createAuthClient({ profile: "MY-PROFILE" });
const api = new LakeboxClient({
  host: auth.host,
  ...(auth.workspaceId ? { workspaceId: auth.workspaceId } : {}),
  authenticate: async (headers) => {
    for (const [name, value] of Object.entries(await auth.headers())) {
      headers.set(name, value);
    }
  },
});
const entry = await api.create();
await api.setIdleTimeout(entry.sandboxId, 3_600);
const running = await ensureLakeboxRunning(api, entry);

LakeboxClient accepts any credential object with host and authenticate(headers). AppKit and the Databricks SDK WorkspaceClient.config satisfy this contract directly. The example adapts @dbx-tools/auth for an explicit local profile. Use the app service principal for lifecycle calls in a Databricks App.

Connect and run a command

Create or load an SSH key, register it with the workspace, verify the gateway host key, and close the session when work finishes:

import { SshKeyStore } from "@dbx-tools/lakebox/key-store";
import type { LakeboxClient, LakeboxEntry } from "@dbx-tools/lakebox/rest";
import { SshSession } from "@dbx-tools/lakebox/session";

export async function runCommand(
  api: LakeboxClient,
  running: LakeboxEntry,
  user: { name: string; home: string },
): Promise<void> {
  const owner = "my-service";
  const keys = new SshKeyStore();
  const key = await keys.ensureRegistered(api, owner);
  const gatewayHost = running.gatewayHost ?? key.gatewayHost;
  if (!gatewayHost) {
    throw new Error("The Lakebox API did not return an SSH gateway host");
  }

  const gatewayPort = 2_222;
  const scope = await api.workspaceScope();
  const knownHost = await keys.knownHost(scope, owner, gatewayHost, gatewayPort);
  const session = new SshSession({
    host: gatewayHost,
    port: gatewayPort,
    username: running.sandboxId,
    privateKey: key.privateKey,
    privateKeyPath: key.privateKeyPath,
    knownHostsPath: keys.knownHostsPath(scope, owner, gatewayHost, gatewayPort),
    connectTimeoutMs: 30_000,
    sshTransport: "auto",
    ssh2Patch: "auto",
    ...(knownHost ? { knownHost } : {}),
    rememberHost: (fingerprint, hostKey) =>
      keys.rememberHost(scope, owner, gatewayHost, gatewayPort, fingerprint, hostKey),
  });

  try {
    await session.connect();
    const command = session.execute("sh", ["-lc", "uname -a"], {
      user,
      timeoutMs: 30_000,
    });
    const result = await command.result;
    if (!result.success) {
      throw new Error(result.stderr || `Command exited with ${result.exitCode}`);
    }
  } finally {
    await session.close();
  }
}

Every command clears inherited Databricks credentials and SSH agent variables before it runs. execute() returns a live SshCommand. Its stdout and stderr fields are async iterables, awaiting the command returns its numeric exit code, and result returns captured output and timing metadata.

The low-level package doesn't create Linux users. Pass an account that already exists on the Lakebox. @dbx-tools/appkit-lakebox creates and isolates application user accounts before it runs their commands.

forwardOut(host, port) opens a forwarded TCP stream through a separate pool. Long-lived HTTP and WebSocket streams don't consume the warm command connection.

Stop or delete a Lakebox

Use lifecycle convergence when you need to wait for a terminal state:

import { ensureLakeboxStopped } from "@dbx-tools/lakebox/lifecycle";
import type { LakeboxClient, LakeboxEntry } from "@dbx-tools/lakebox/rest";

export async function stopAndDelete(api: LakeboxClient, running: LakeboxEntry): Promise<void> {
  const stopped = await ensureLakeboxStopped(api, running);
  await api.delete(stopped.sandboxId);
}

stop() preserves the persistent Lakebox filesystem. delete() permanently removes the Lakebox and its data.

Configuration

FieldValuesDefault
sshTransportauto, openssh, ssh2auto prefers native OpenSSH with SOCKS5
ssh2Patchauto, true, falseauto patches only in development

SshSessionOptions also requires the gateway host and port, Lakebox ID as the SSH username, private key material, key and known-host paths, and a connection timeout. SshKeyStore stores keys and gateway fingerprints under ~/.dbx-tools/lakebox unless you pass another root.

Understand lifecycle and transport limits

  • ensureLakeboxRunning() and ensureLakeboxStopped() poll for up to five minutes.
  • The REST client doesn't map users, persist app resource identity, or manage services. Use @dbx-tools/appkit-lakebox for those application-level capabilities.
  • The command pool keeps one tested connection warm and grows to 10 connections. Forwarding uses a separate pool with no minimum.
  • Each borrowed SSH resource handles one operation and is destroyed after that command or forwarded stream. A failed forwarding open retries once with a new resource. Established streams and commands aren't replayed.
  • auto selects native OpenSSH when it is available and selects ssh2 otherwise. It doesn't switch transports after a runtime failure.
  • close() cancels active commands, closes transport pools, and waits for an in-progress connection attempt to settle.

版本与发布记录

@dbx-tools/lakebox 共发布 54 个版本,首次发布于 2026/10/15;过去 12 个月发布了 54 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.9.206latest2026/11/11今天
0.9.2052026/11/111 天前
0.9.2042026/11/101 天前
0.9.2032026/11/074 天前
0.9.2022026/11/065 天前
0.9.2012026/11/056 天前

关于 @dbx-tools/lakebox 的常见问题

@dbx-tools/lakebox 是什么?

Databricks Lakebox REST, pooled SSH transport, and deterministic user routing

如何安装 @dbx-tools/lakebox?

在项目目录运行 npm install @dbx-tools/lakebox,或使用 pnpm add / yarn add / bun add。

@dbx-tools/lakebox 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dbx-tools/lakebox 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@dbx-tools/lakebox 可以商用吗?

@dbx-tools/lakebox 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@dbx-tools/lakebox 的下载量有多少?

根据 npm 官方下载统计,@dbx-tools/lakebox 在 2026/10/12 – 2026/11/10 这 30 天内被下载 5,704 次,日均约 190 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dbx-tools/lakebox 还在维护吗?

最新版本 0.9.206 发布于 2026/11/11(今天);至今共发布 54 个版本,其中过去 12 个月发布了 54 个;首次发布于 2026/10/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。