@defuse-protocol/outlayer-sdk

中文文档
0.6.0发布于 2026/09/29MITTypeScriptES Module近 30 天下载 4164

TypeScript gRPC SDK for the Outlayer service

原始使用文档
文档目录4 节
  1. Install
  2. Usage
  3. Deadlines, cancellation, and fuel
  4. Authentication

@defuse-protocol/outlayer-sdk

TypeScript gRPC SDK for the Outlayer service, built with Connect-ES. Node.js only — speaks gRPC over HTTP/2 to the worker, and ships the generated message types plus an execute helper.

Install

The SDK is published publicly to npm:

npm install @defuse-protocol/outlayer-sdk

Usage

import { createOutlayerClient } from "@defuse-protocol/outlayer-sdk";

const client = createOutlayerClient("http://localhost:50051");

const res = await client.execute({
  app: { appId: "app.near" },
  input: new TextEncoder().encode("{}"),
});

if (res.error) {
  console.error(res.error, res.fuelConsumed); // WASM run failed (returned as data)
} else {
  console.log(new TextDecoder().decode(res.output), res.fuelConsumed);
}

Apps can also be executed directly from a URL with either a hexadecimal or raw-byte SHA-256 hash:

const res = await client.execute({
  app: {
    codeUrl: "https://example.com/app.wasm",
    codeHash: "8f2466da...",
  },
  input: new TextEncoder().encode("{}"),
});

execute resolves to { output, logs, fuelConsumed, error? }; output/logs are raw Uint8Arrays. A populated error means the run reached the worker but the WASM failed — returned as data, not thrown. Transport failures reject with a ConnectError.

Deadlines, cancellation, and fuel

execute takes three more options. All of them are optional:

const controller = new AbortController();

const res = await client.execute(
  { app, input },
  {
    timeoutMs: 5_000,
    signal: controller.signal,
    fuelLimit: 1_000_000_000n,
  },
);

timeoutMs is one budget for the whole call. Getting the token, the request, and the retry with a refreshed token all share it, so a call never takes two full timeouts. To give every call a deadline, set a default on the client. A single call can still override it:

const client = createOutlayerClient({
  endpoint: "https://worker.example.com:443",
  defaultTimeoutMs: 30_000,
});

timeoutMs and signal only end the call on your side. The worker finishes a run that has already started, so neither of them frees its capacity. fuelLimit is the only limit that can stop a run while it runs. Leave it out and the worker uses its own default.

Authentication

A worker behind Google IAM wants a Google ID token. Create a Google token manager and inject it into the client. The manager mints, caches, and refreshes tokens. If the worker rejects a request as unauthenticated, the Google manager invalidates the cached token, generates a fresh one, and retries once:

import {
  createGoogleJwtTokenManager,
  createOutlayerClient,
  type JwtTokenManager,
} from "@defuse-protocol/outlayer-sdk";

const jwtTokenManager = createGoogleJwtTokenManager({
  impersonate: "[email protected]",
});

const client = createOutlayerClient({
  endpoint: "https://worker.example.com:443",
  auth: jwtTokenManager,
});

Omit auth for a worker with authentication disabled. With Google, omit impersonate to use Application Default Credentials directly:

createOutlayerClient("http://localhost:50051");
createGoogleJwtTokenManager();
createGoogleJwtTokenManager({ impersonate });

Google auth needs the optional peer dependency google-auth-library. Install it only when using createGoogleJwtTokenManager.

The audience defaults to outlayer, which is what the worker expects unless its deployment sets another one. Override it in the manager options:

createGoogleJwtTokenManager({ audience: "another-audience" });

Other identity providers can implement the provider-neutral interface:

const jwtTokenManager: JwtTokenManager = {
  generateToken: () => myTokenSource(),
  invalidateToken: async () => clearMyTokenCache(),
  handleAuthError: async (error) => {
    throw error;
  },
};

Call invalidateToken() at any time to force the next request to generate a fresh token:

jwtTokenManager.invalidateToken();

An authToken passed to execute overrides the configured manager for that request. The override can be a token or a synchronous or asynchronous function:

await client.execute({ app, input }, { authToken: idToken });
await client.execute({ app, input }, { authToken: () => getFreshToken() });

Override tokens are sent as given and bypass the manager's retry behavior.

The JWT-protected worker integration test is disabled unless its endpoint or token is configured. Provide both variables to run it:

OUTLAYER_ENDPOINT=https://worker.example.com:443 \
OUTLAYER_JWT_TOKEN="$(get-token)" \
pnpm test:jwt-live

For inline WASM or a custom transport, use the underlying Connect client at client.raw, or build your own from the re-exported OutlayerService.

版本与发布记录

@defuse-protocol/outlayer-sdk 共发布 3 个版本,首次发布于 2026/08/27;过去 12 个月发布了 3 个版本。

版本发布日期距今
0.6.0latest2026/09/291 个月前
0.5.12026/08/282 个月前
0.0.02026/08/272 个月前

关于 @defuse-protocol/outlayer-sdk 的常见问题

@defuse-protocol/outlayer-sdk 是什么?

TypeScript gRPC SDK for the Outlayer service

如何安装 @defuse-protocol/outlayer-sdk?

在项目目录运行 npm install @defuse-protocol/outlayer-sdk,或使用 pnpm add / yarn add / bun add。

@defuse-protocol/outlayer-sdk 支持 TypeScript 吗?

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

@defuse-protocol/outlayer-sdk 支持 ESM 还是 CommonJS?

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

@defuse-protocol/outlayer-sdk 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@defuse-protocol/outlayer-sdk 可以商用吗?

@defuse-protocol/outlayer-sdk 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@defuse-protocol/outlayer-sdk 的下载量有多少?

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

@defuse-protocol/outlayer-sdk 还在维护吗?

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