@hile/micro
v4.1.0
Published
<!-- Generated by scripts/build-ai-context.mjs from docs/ai. Do not edit by hand. -->
Readme
@hile/micro
Run registry-backed service discovery, RPC, streaming RPC, and pub/sub between Node services.
This README is intentionally short and example-first. The complete AI-facing guide ships in AI.md in this package.
When To Use
Use the message packages for request/response messaging over WebSocket, process IPC, worker threads, file-system message handlers, service discovery, streaming RPC, and registry-backed pub/sub.
Install
pnpm add @hile/microCopy-Paste Example
Message handler file:
// src/messages/ping.msg.ts
import { defineMicroMessage } from '@hile/micro'
export default defineMicroMessage(async ({ data, params, invocation }) => {
return {
type: 'pong',
data,
params,
requestId: invocation.context.values.requestId,
timestamp: Date.now(),
}
})Microservice boot file:
// src/services/app.boot.ts
import { defineService } from '@hile/core'
import { Application } from '@hile/micro'
export default defineService('micro.app', async (shutdown) => {
const app = new Application({
namespace: process.env.MICRO_NAMESPACE ?? 'example.service',
registry: {
host: process.env.REGISTRY_HOST ?? '127.0.0.1',
port: Number(process.env.REGISTRY_PORT ?? 9876),
},
advertiseHost: process.env.HILE_ADVERTISE_HOST ?? '127.0.0.1',
})
await app.load(new URL('../messages', import.meta.url).pathname)
const stop = await app.listen(Number(process.env.MICRO_PORT ?? 9877))
shutdown(stop)
return app
})Caller:
import { randomUUID } from 'node:crypto'
import { createExecutionContext } from '@hile/context'
const context = createExecutionContext({ requestId: randomUUID() })
const result = await app.call('example.service', '/ping', { hello: 'world' }, { context })Typed fixed-path operations
Use defineMicroContract() and createMicroClient() from @hile/micro-contract to share stable JSON schemas and typed remote calls. Implement each operation in its matching message file with defineMicroMessage(operation, handler), then call loadMicroContract(app, contract, messagesDirectory) from @hile/micro to obtain { local, activate, close }.
The binding validates filesystem paths and complete implementation coverage before activation. Explicit local calls are same-service unary calls through the provider executor, not a namespace-based optimization; normal calls still use RPC. Typed remote calls default to zero retries. Native dynamic and streaming APIs remain supported. See packages/micro-contract.md for the full example, schema constraints, and shutdown ordering.
Boundaries
Do not use
stream()for normal single-result calls.Do not enable retries for a streamed request input. Input streams are consumed once and cannot be replayed safely.
Do not rely on message IDs for business idempotency. They are transport IDs.
Use
defineMicroMessage()for Micro business handlers; reserve genericdefineMessage()for transport-neutral loaders.Do not pass zero, fractional, non-finite, or oversized message timeouts. Explicit timeout values must be safe integers from
1through2_147_483_647milliseconds.Appending a secondary response getter to
client.request('/x', data)Returning a plain object from a handler called through
stream().Retrying a consumed request stream or hiding it inside a replay-unsafe factory.
Branching business code on whether a Micro target namespace is local or remote.
Using pub/sub as a durable queue.
Forgetting to register
shutdown(await app.listen(...)).
Verify
- Micro message files default-export
defineMicroMessage(...)and receiveinvocation.context. - Native untyped Micro message files may use
[...name].msg.ts; typed Micro Contract implementations remain fixed-path only and reject all parameterized file routes. - RPC callers use
await app.call(..., { context }). - Same-namespace
call()andstream()use normal Registry discovery, whilestreamPeer()keeps its exact-address selection; all three use the normal WebSocket/modem transport path and callers do not branch on locality. - Streaming handlers are async generators.
- Request-stream handlers consume
inputand callers either passoptions.inputalongside metadata or pass a stream directly asdata. - Streamed request calls do not configure nonzero retries.
- Custom modem timeout values use the documented safe-integer range.
- File and raw owner conflicts, atomic batch failure, concurrent load rejection, and protocol misdelivery are tested before any handler side effect.
- Typed fixed-path services validate both actual source and compiled route trees; explicit local and real remote calls test the same provider executor, admission, cancellation, and bounded shutdown.
- Registry is started before application nodes need discovery.
- Micro apps use stable namespaces and advertise reachable hosts.
More Context
AI.mdin this package: full package-local AI guide.- Root
llms-full.txt: full monorepo AI context. - Root
references/: source files copied fromdocs/ai.
