Files
OpenShell/crates/openshell-sdk-node/lib.mjs
T
Max Dubrinsky d7b306349c feat(sdk-node): add napi-rs TypeScript binding (@openshell/sdk)
Per RFC 0005, ship openshell-sdk-node as a napi-rs wrapper over the
openshell-sdk crate, exposing the same surface to TypeScript/Node
consumers as @openshell/sdk.

- New crate openshell-sdk-node binds OpenShellClient, OidcRefresher,
  the edge-tunnel dialer, and the sandbox/exec API through napi-rs.
- Generated index.d.ts and index.js are committed; the per-platform
  .node binary is gitignored and rebuilt with `napi build` per host.
- lib.mjs provides a small ESM facade with named exports plus the
  errorCode() helper for typed error discrimination.

Tests: 5-case smoke suite that exercises exports, typed connect errors,
and OidcRefresher single-flight semantics.
2026-06-25 13:41:49 -04:00

33 lines
1.2 KiB
JavaScript

// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// ESM facade over the auto-generated CommonJS index.
//
// Re-exports the napi-generated classes and adds the `errorCode` helper,
// which parses the `[code] message` prefix the binding uses to surface the
// SDK's discriminable error kind. The auto-generated `index.js`/`index.d.ts`
// pair from napi-rs is left untouched so the build matrix stays cookie-cutter.
import nativeBinding from './index.js'
const { OpenShellClient, OidcRefresher } = nativeBinding
export { OpenShellClient, OidcRefresher }
/**
* Extract the SDK error code from a thrown error.
*
* The native binding prefixes every error message with `[code] ` where
* `code` is one of: `invalid_config`, `tls`, `connect`, `auth`, `io`,
* `not_found`, `already_exists`, `rpc`. Returns `null` when the prefix is
* missing (the error wasn't thrown by this binding).
*
* @param {unknown} err
* @returns {string | null}
*/
export function errorCode(err) {
if (!err || typeof err.message !== 'string') return null
const match = err.message.match(/^\[([^\]]+)\]/)
return match ? match[1] : null
}