@barry-rocks/sdk/services/http
Scaffolding for a bag’s own HTTP service: serve, applyBaseMiddleware,
applyAuth for Barry’s local-secret check, request and error loggers, and
camelResponse. It gives a service the same authentication and logging every
built-in service uses.
import { … } from "@barry-rocks/sdk/services/http";Interfaces
Section titled “Interfaces”BaseMiddlewareOptions
Section titled “BaseMiddlewareOptions”Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
rateLimitPerSecond? |
number |
Requests per second per IP. The SPA fires a burst on page load. |
CreateLoggerOptions
Section titled “CreateLoggerOptions”Properties
Section titled “Properties”Logger
Section titled “Logger”Methods
Section titled “Methods”child()
Section titled “child()”child(
context):Logger
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
context |
LogContext |
Returns
Section titled “Returns”debug()
Section titled “debug()”debug(
msg,context?):void
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
msg |
string |
context? |
LogContext |
Returns
Section titled “Returns”void
error()
Section titled “error()”error(
msg,context?):void
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
msg |
string |
context? |
LogContext |
Returns
Section titled “Returns”void
flush()
Section titled “flush()”flush():
Promise<void>
Returns
Section titled “Returns”Promise<void>
info()
Section titled “info()”info(
msg,context?):void
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
msg |
string |
context? |
LogContext |
Returns
Section titled “Returns”void
warn()
Section titled “warn()”warn(
msg,context?):void
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
msg |
string |
context? |
LogContext |
Returns
Section titled “Returns”void
RequestAuthorizationOptions
Section titled “RequestAuthorizationOptions”Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
policy |
(request) => RequestPolicy | null |
- |
timeoutMs? |
number |
- |
verifyCredential |
(credential, signal) => unknown |
The principal it vouches for, or a promise of it. |
RequestLoggerOptions
Section titled “RequestLoggerOptions”The HTTP scaffolding every Barry service puts in front of its routes.
Extracted when bags/sessions/api split into session and identity services. It is shared rather than copied because these are SECURITY defaults — auth, CORS, the CSRF content-type guard, rate limiting — and a copy that drifts is a service that is quietly less protected than its sibling while looking identical from outside.
Order is load-bearing and matches what bags/sessions/api had: security headers,
body parsing, rate limit, CORS, then auth, then the CSRF guard. Anything a
service wants to expose publicly (health, webhooks) must be registered on the
app BEFORE calling applyAuth.
bags/vault is the deliberate exception and must NOT be converted onto this
path. It runs under its own _barryvault UID specifically to defend against
same-UID processes — the other Barry services included — and this path
admits anyone holding the instance’s BARRY_SECRET, which every same-UID
process on this machine can read. A vault that accepted the
platform’s ambient BARRY_SECRET, or trusted loopback, would be readable by
exactly the processes the UID boundary was created to exclude. Its own JWT
issuance and per-account bearer tokens are the feature. If a sweep flags it as
the last hand-rolled server, that sweep is wrong and this paragraph is why.
Properties
Section titled “Properties”RequestPolicy
Section titled “RequestPolicy”Properties
Section titled “Properties”Properties
Section titled “Properties”Type Aliases
Section titled “Type Aliases”CreateLogger
Section titled “CreateLogger”CreateLogger = (
service,options?) =>Logger
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
service |
string |
options? |
CreateLoggerOptions |
Returns
Section titled “Returns”LogContext
Section titled “LogContext”LogContext =
Record<string,unknown>
Arbitrary structured context attached to a log line.
Functions
Section titled “Functions”applyAuth()
Section titled “applyAuth()”applyAuth(
app,authorization?):void
Authentication and the CSRF guard.
Call AFTER registering any public routes (health, webhooks): this rejects everything that has not already been handled.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
app |
Express |
authorization? |
RequestAuthorizationOptions |
Returns
Section titled “Returns”void
applyBaseMiddleware()
Section titled “applyBaseMiddleware()”applyBaseMiddleware(
app,options?):void
Everything that runs BEFORE authentication.
Call this first, then register public routes, then applyAuth.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
app |
Express |
options |
BaseMiddlewareOptions |
Returns
Section titled “Returns”void
camelResponse()
Section titled “camelResponse()”camelResponse(
body):unknown
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
body |
unknown |
Returns
Section titled “Returns”unknown
createErrorLogger()
Section titled “createErrorLogger()”createErrorLogger(
service):ErrorRequestHandler
Error logging middleware Place after your routes to log errors
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
service |
string |
Returns
Section titled “Returns”ErrorRequestHandler
Example
Section titled “Example”app.use(routes);app.use(createErrorLogger("web"));createRequestLogger()
Section titled “createRequestLogger()”createRequestLogger(
service,options?):RequestHandler
Express middleware for request logging
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
service |
string |
The service name |
options |
RequestLoggerOptions |
Optional configuration |
Returns
Section titled “Returns”RequestHandler
Example
Section titled “Example”import express from "express";import { createRequestLogger } from "./index.js";
const app = express();app.use(createRequestLogger("web", { skip: (req) => req.path === "/health",}));
app.get("/api/users", (req, res) => { req.log.info("Fetching users", { userId: req.params.id }); // ...});serve()
Section titled “serve()”serve(
router,base,options?):object
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
router |
Router |
base |
string |
options |
{ casing?: "camel" | "verbatim"; } |
options.casing? |
"camel" | "verbatim" |
Returns
Section titled “Returns”object
| Name | Type |
|---|---|
delete() |
(declaration, …handlers) => void |
get() |
(declaration, …handlers) => void |
patch() |
(declaration, …handlers) => void |
post() |
(declaration, …handlers) => void |
put() |
(declaration, …handlers) => void |
route() |
(declaration, …handlers) => void |
servedRoutes()
Section titled “servedRoutes()”servedRoutes(
router): readonlyRoute[]
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
router |
Router |
Returns
Section titled “Returns”readonly Route[]
setLoggerFactory()
Section titled “setLoggerFactory()”setLoggerFactory(
factory):void
Replace the logger factory, as a test does to record what is logged.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
factory |
CreateLogger |
Returns
Section titled “Returns”void
templatePath()
Section titled “templatePath()”templatePath(
path):string
Replace identifier-looking path segments with :id.
Without this every session id is its own route: the live logs show three ids
producing three “endpoints” for the same handler, which makes a top-routes
panel meaningless and would blow up any summarize by path aggregation.
The rules are deliberately narrow. Over-templating is its own failure — if a
real route segment collapses to :id the panel silently groups unrelated
endpoints and the numbers stop meaning anything. So a segment is only
templated when it is shaped like an identifier and NOT like a word:
- all digits (
/identities/42) - a UUID
- nanoid-shaped: 16+ chars from the nanoid alphabet, AND containing a digit,
-or_, or mixed case.messagesandavailableare long lowercase words and stay;F_1aL9OLQsKGNCZNxxyC9goes.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
path |
string |
Returns
Section titled “Returns”string
References
Section titled “References”corsOrigins
Section titled “corsOrigins”Re-exports corsOrigins
ENV_RULES
Section titled “ENV_RULES”Re-exports ENV_RULES
EnvRule
Section titled “EnvRule”Re-exports EnvRule
EnvValidationResult
Section titled “EnvValidationResult”Re-exports EnvValidationResult
getHostingConfig
Section titled “getHostingConfig”Re-exports getHostingConfig
getServicePort
Section titled “getServicePort”Re-exports getServicePort
getServiceUrl
Section titled “getServiceUrl”Re-exports getServiceUrl
HostingConfig
Section titled “HostingConfig”Re-exports HostingConfig
originAllowed
Section titled “originAllowed”Re-exports originAllowed
ServiceName
Section titled “ServiceName”Re-exports ServiceName
validateEnv
Section titled “validateEnv”Re-exports validateEnv