@shukoi/core
v1.0.4
Published
Universal action engine, adaptive TypeBox validation core, and authentication layer for the Shukoi micro-framework.
Maintainers
Readme
@shukoi/core
The Universal Action Engine & Adaptive Validation Core of the Shukoi Ecosystem
@shukoi/core is a platform-agnostic, zero-dependency foundational TypeScript engine designed to implement the Write-Once Contract-First paradigm. It empowers software engineers to define business logic, input/output validation contracts, operational limits, and security policies once in a single declarative file (defineAction), which can then be exposed over REST and Model Context Protocol (MCP) without code modification.
Key Features & Invariants
- Zero-Guesswork Contracts: Strict TypeBox schema validation on all inputs and outputs.
- Adaptive Isolate-Safe Validator: Automatic boot-time capability detection: compiles via TypeBox JIT (
TypeCompiler) on Node.js/Bun, and falls back to AST evaluation (Value.Check) on V8 Isolates (Cloudflare Workers) where dynamic code generation is restricted. - Single-Flight JWKS Key Discovery: In-memory caching with TTL and an active promise mutex preventing JWKS cache stampedes under high concurrency.
- Constant-Time Crypto: Secret and token comparisons execute using Web Crypto SHA-256 bitwise XOR loops (
safeCompareSecrets) to prevent timing side-channel attacks. - Web-Standard Sterile Logger: Zero-dependency structured JSON logger writing exclusively to
process.stderr(orconsole.error), preserving File Descriptor 1 (stdout) for clean MCP STDIO protocol frames. - Atomic Idempotency Locking: Bounded in-memory LRU store with automatic TTL expiration ensuring exactly-once execution for mutating requests.
- Composite Cancellation: Combines transport abort signals, action timeouts, and container execution deadlines into a unified
AbortSignal.any. - Pure Web-Standard Engine: Contains zero
node:*imports or native C++ addons, guaranteeing 100% compatibility across edge sandboxes.
Installation
pnpm add @shukoi/core @sinclair/typebox jose
# or
npm install @shukoi/core @sinclair/typebox jose
# or
bun add @shukoi/core @sinclair/typebox josePeer Dependencies
@sinclair/typebox(^0.34.0)jose(^5.9.6)
Quickstart: The 1-File Action Pattern
import { Type } from '@sinclair/typebox';
import { defineAction, DomainError } from '@shukoi/core';
export const createUserAction = defineAction({
name: 'createUser',
summary: 'Registers a new user profile',
description: 'Validates input, checks authorization, and stores profile data',
authRequired: true,
requiredScopes: ['users:write'],
timeoutMs: 15000,
http: {
method: 'POST',
path: '/users',
successStatus: 201,
requireIdempotencyKey: true,
},
mcp: {
enabled: true,
entityType: 'tool',
annotations: { destructiveHint: true, idempotentHint: true },
},
input: Type.Object(
{
username: Type.String({ minLength: 3, maxLength: 32 }),
email: Type.String({ format: 'email' }),
},
{ additionalProperties: false },
),
output: Type.Object(
{
id: Type.String(),
username: Type.String(),
createdAt: Type.String(),
},
{ additionalProperties: false },
),
async handler(input, ctx) {
ctx.logger.info('Creating user', { username: input.username });
if (ctx.signal.aborted) {
throw new DomainError('GATEWAY_TIMEOUT', 'Operation aborted by caller', 504);
}
ctx.onFinally(async () => {
ctx.logger.debug('Cleaning up database handles');
});
return {
id: crypto.randomUUID(),
username: input.username,
createdAt: new Date().toISOString(),
// Internal fields are automatically stripped before output validation
internal_hash: 'SECRET_DO_NOT_LEAK',
} as unknown as { id: string; username: string; createdAt: string };
},
});API Reference
defineAction(definition)
Creates a compiled, deeply frozen CompiledAction with pre-compiled TypeBox input and output validators.
executeAction(action, rawInput, options)
Executes an action through the standardized 8-step pipeline:
- Correlation Tracking & Child Logger Setup
- Composite Cancellation Controller (
AbortSignal.any) - Authentication & Scope RBAC Verification
- Atomic Idempotency Locking
- Input Sanitization & TypeBox Validation
- Pure Handler Invocation
- Output Data Sanitization (
Value.Clean) & Contract Validation - Deterministic LIFO Resource Teardown (
ctx.onFinally)
compileSchemaValidator(schema, forceAst?)
Compiles a TypeBox schema into an adaptive validator object. Automatically enforces { additionalProperties: false } recursively across object nodes.
createDefaultConfig(overrides?) / validateShukoiConfig(raw)
Creates or validates strongly-typed, deeply frozen ShukoiConfig objects containing security, limits, and observability thresholds.
DomainError(code, detail, statusCode?, errors?)
Typed error exception class that maps operational failures to RFC 9457 Problem Details (application/problem+json).
Testing Harness: @shukoi/core/testing
Test actions without spinning up full HTTP or MCP network servers:
import { describe, it, expect } from 'vitest';
import { createActionTestHarness } from '@shukoi/core/testing';
import { createUserAction } from './createUser.js';
describe('createUserAction', () => {
it('creates user profile successfully', async () => {
const harness = createActionTestHarness();
const result = await harness.execute(
createUserAction,
{ username: 'alice', email: '[email protected]' },
{ headers: { 'x-api-key': 'valid-key' }, staticApiKeys: { 'valid-key': { id: 'usr-1' } } },
);
expect(result.id).toBeDefined();
expect(result.username).toBe('alice');
});
});License
MIT © Shukoi Contributors
