@textjs/opencode

中文文档
1.1.0发布于 2026/11/03MITTypeScriptES Module零依赖

A loopback bridge that connects textjs.org/opencode to the OpenCode server on your machine.

原始使用文档
文档目录4 节
  1. Options
  2. Security
  3. Text.js projects
  4. Errors

▼ @textjs/opencode

A local bridge that lets textjs.org/opencode talk to the OpenCode server on your machine. It has no dependencies and runs on Node 22+ or Bun.

bunx @textjs/opencode@latest proxy    # or: npx @textjs/opencode@latest proxy

Leave it running, open textjs.org/opencode, enter http://127.0.0.1:4096 and press Connect. Allow local network access if Chrome asks, then enter the pairing code the bridge printed. Stop the bridge with Ctrl+C.

Browsers block a public page from reading http://127.0.0.1 unless the server answers CORS and Private Network Access preflights, and the OpenCode service also requires a password. The bridge adds the headers and the password, and forwards each request to OpenCode.

Options

FlagDescription
-p, --port <port>Port to listen on (default 4096)
--target <url>OpenCode server URL. Skips discovery.
--allow-origin <origin>Allow another browser origin, such as https://example.com or http://localhost:*. Repeatable.
-v, --versionPrint the version
-h, --helpShow help

Without --target, the bridge asks opencode service status for the managed service and reads its password with opencode service get password. It caches the result for 30 seconds and looks again when OpenCode answers 401 or can't be reached, so it follows a service that restarts with a new port or password.

For a foreground server, pass its URL. If the server already uses port 4096, run the bridge on another port and connect the page to that port:

bunx @textjs/opencode@latest proxy --port 4097 --target http://127.0.0.1:4096

For a password-protected foreground server, export OPENCODE_PASSWORD in the bridge's shell. OPENCODE_SERVER_URL works in place of --target.

Security

  • Loopback only. The bridge listens on 127.0.0.1 and answers only requests whose Host is localhost, 127.0.0.1 or [::1], which blocks DNS rebinding.
  • Origin allowlist. Browser requests must come from https://textjs.org, https://www.textjs.org or http://localhost, 127.0.0.1 and [::1] on any port. Other origins get a 403. Add your own with --allow-origin, for example --allow-origin 'https://*.textjs-avg.pages.dev' for textjs.org preview deployments.
  • Pairing code. The bridge prints a code when it starts. A page sends it once to POST /__bridge/pair and gets a token for this bridge run, tied to that page's origin. Apart from CORS preflights and GET /__bridge, every request needs the token in x-textjs-bridge-token, or it gets a 428 and never reaches OpenCode. GET /__bridge says whether the page is paired, and a wrong code gets a 200 with ok: false and the attempts left, so the page never logs a failed request. Ten wrong codes lock pairing until the bridge restarts. Every response carries x-textjs-bridge-pairing: 1, so the page refuses 1.0.x bridges, which forward without a code.
  • The password stays on your machine. The bridge keeps it in memory and adds it to requests it forwards to OpenCode. It isn't logged, written to disk or sent to the browser.
  • Writes need a custom header. Requests other than GET, HEAD and OPTIONS must send x-opencode-client: 1, a header HTML forms can't set. Request bodies are capped at 4 MB.
  • Only a few headers cross. The bridge forwards Accept, Content-Type, Last-Event-ID and x-opencode-directory to OpenCode, and returns Content-Type, Location and x-request-id. Cookies, Authorization and the pairing token from the browser are dropped.
  • Passwords apply to loopback targets only. OPENCODE_PASSWORD is never sent to a non-loopback --target, and --target rejects URLs that contain credentials, a path, a query or a hash (TX021).

Text.js projects

@textjs/app runs the same bridge as text proxy, with the same flags.

Errors

CodeMeaning
TX021--target isn't a bare HTTP(S) origin
TX022--port is the OpenCode server's own port
TX023A bridge is already running on that port
TX024Something else is using that port

版本与发布记录

@textjs/opencode 共发布 5 个版本,首次发布于 2026/11/02;过去 12 个月发布了 5 个版本。

版本发布日期距今
1.1.0latest2026/11/03今天
1.0.42026/11/021 天前
1.0.32026/11/021 天前
1.0.22026/11/021 天前
0.0.0-stage2026/11/021 天前

关于 @textjs/opencode 的常见问题

@textjs/opencode 是什么?

连接textjs.org/opencode到机器上的OpenCode服务器的环回桥接器。

如何安装 @textjs/opencode?

在项目目录运行 npm install @textjs/opencode,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 textjs-opencode,可以用 npx textjs-opencode 直接运行。

@textjs/opencode 支持 TypeScript 吗?

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

@textjs/opencode 支持 ESM 还是 CommonJS?

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

@textjs/opencode 需要什么版本的 Node.js?

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

@textjs/opencode 可以商用吗?

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

@textjs/opencode 还在维护吗?

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