Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@harnessa-fe/node-runtime

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@harnessa-fe/node-runtime

Node.js server-side SDK for Harnessa-FE. Captures server errors, console output, and Route Handler traces — links them to the browser session via a shared sessionId.

latest
Source
npmnpm
Version
3.0.0
Version published
Maintainers
1
Created
Source

@harnessa-fe/node-runtime

Node.js server-side SDK for Harnessa-FE. Captures server errors, console output, and Route Handler / Server Action traces — and links them to the browser session via a shared sessionId.

pnpm add @harnessa-fe/node-runtime

Most users don't import this directly — @harnessa-fe/next's <HarnessaScript> auto-boots it. Reach for it when:

  • You're on Next but want to call register() from instrumentation.ts yourself
  • You're on Express / Fastify / Hono / Koa / any non-Next Node server
  • You're writing a framework adapter and want to plug a sessionId resolver in via setSessionIdProvider

What it captures

EventWhen
server-errprocess.on('uncaughtException'), unhandledRejection, or explicit reportError()
server-logAuto-captured console.log/info/warn/error/debug calls (toggle via captureConsole)
app-logExplicit reportAppLog() calls — @harnessa-fe/log emits these
server-actionHandlers wrapped with withHarnessaTracing() — duration + status

Every event is tagged with the current request's sessionId (when one exists).

Quickstart — non-Next Node app

// app.ts (Express example)
import express from 'express';
import { register, withHarnessaTracing } from '@harnessa-fe/node-runtime';

register({
    projectId: 'my-api',
    buildId: process.env.GIT_SHA,
    mcpUrl: 'ws://127.0.0.1:47729',
});

const app = express();

app.get('/hello', withHarnessaTracing(async (req, res) => {
    console.log('handling /hello'); // → server-log with this request's sessionId
    res.json({ ok: true });
}));

withHarnessaTracing() reads the x-hfe-session-id header (set by the browser runtime client when it ships events) and binds it into AsyncLocalStorage for the handler scope. Every console.* call and any reportError() inside the handler inherits that id.

Quickstart — Next.js (advanced)

If you already use <HarnessaScript>, you're done — it calls register() for you. The remaining hook you might care about is withHarnessaTracing for Route Handlers:

// app/api/foo/route.ts
import { withHarnessaTracing } from '@harnessa-fe/node-runtime';

export const POST = withHarnessaTracing(async (req: Request) => {
    console.log('foo received');
    return Response.json({ ok: true });
});

For App Router pages, <HarnessaScript> already sets up the cache()-backed sessionId; you don't need withHarnessaTracing there.

API

register(opts: RegisterOptions): void

Idempotent. First call wires up the transport (WS in Node runtime, HTTP-batch in Edge), installs process.on('uncaughtException' / 'unhandledRejection') handlers, and patches console.* (unless disabled).

OptionDefaultNotes
projectId (required)Stable id; matches your package.json name typically
displayNameprojectIdLabel shown in agent UIs
buildIdBuild artifact id (git SHA)
mcpUrlws://127.0.0.1:47729Daemon WebSocket URL
baseUrlderivedDaemon HTTP base — only matters when using HTTP transport (Edge)
captureConsoletrueSet false (or env HARNESSA_FE_NODE_CONSOLE=0) to skip patching console.*

withHarnessaTracing(handler)

HoC for Route Handlers / Server Actions. Reads x-hfe-session-id from the incoming request, binds it into ALS for the handler's async scope, and emits a server-action event on completion (duration + status).

setSessionIdProvider(fn: () => string | undefined): void

Dependency-injection hook for framework adapters. The Next adapter calls this on module load with its React cache()-backed getSessionId. node-runtime stays React-agnostic; the adapter pushes its environment-specific resolver in.

getRequestSessionId(): string | undefined

Returns the current request's sessionId. Resolution order:

  • AsyncLocalStorage (populated by withHarnessaTracing())
  • Adapter-supplied provider (from setSessionIdProvider)
  • undefined → orphan event

reportError(err, ctx?)

Explicit error report; auto-called by the global uncaughtException / unhandledRejection handlers.

reportLog(level, args, ctx?) / reportAppLog(level, args, ctx?)

Lower-level emit. reportAppLog is what @harnessa-fe/log calls; you can use it directly if you want the same t: 'app-log' event type without the wrapper.

Transport selection

ConditionTransport
process.env.NEXT_RUNTIME === 'edge'HTTP-batch (POST /events)
HARNESSA_FE_TRANSPORT=http envHTTP-batch
ws module installed (default in Node)WebSocket
FallbackHTTP-batch

The Edge entry point is @harnessa-fe/node-runtime/auto-edge (loaded automatically by <HarnessaScript> when on Edge). It doesn't import ws and doesn't call process.on, keeping the worker bundle slim.

Auto-boot via /auto entry

For Node-runtime users who want zero-touch:

// at the very top of your server entry
import '@harnessa-fe/node-runtime/auto';

The auto module reads HARNESSA_FE_* env vars (projectId, mcpUrl, buildId, …) and calls register() for you. Works in webpack-bundled Next server entries (withHarnessa() injects this for you).

Concurrency safety

getRequestSessionId() is read fresh on every call — never closed over. Two concurrent requests in the same Node process get separate AsyncLocalStorage scopes (via withHarnessaTracing) AND separate React cache() scopes (via the Next adapter provider). 28 unit tests verify zero cross-request contamination including a Promise.all([renderA, renderB]) interleaved-console.log case.

Orphans

If a console.log (or reportError, etc.) fires with no ALS and no adapter provider — e.g. cold-start init, post-response timer, top-level module side-effect — it emits with sessionId: undefined. The daemon files these under ~/.harnessa/data/sessions/server-orphans/... rather than guessing. Better orphaned than misattributed.

Production behavior

<HarnessaScript> and the /auto entry only call register() when NODE_ENV === 'development'. If you call register() directly outside of dev, it still works — useful for staging or self-hosted dev environments — but you must opt in explicitly.

License

MIT

Keywords

harnessa-fe

FAQs

Package last updated on 22 May 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts