@unikvs/debug
Middleware that logs debug information about operations and data being read or written.
Overview
@unikvs/debug is a plugin that logs the kind and size of data being read or written.Middleware
It passes data through unchanged while recording the operation name, key, and direction. Use it to observe data flow through storage.
nameisDebug.isOpenis alwaystrue.open()is unnecessary.- Logging uses
@logtape/logtape. Recording is controlled bykeyFilterandactionFilter.
Install with:
npm install @unikvs/debug @logtape/logtapepnpm add @unikvs/debug @logtape/logtapeyarn add @unikvs/debug @logtape/logtapebun add @unikvs/debug @logtape/logtapenub add @unikvs/debug @logtape/logtapeaube add @unikvs/debug @logtape/logtapeUsage
Debug implements IMiddleware. Pass it to UniKvs with use().
import { Debug } from "@unikvs/debug";
DebugOptions
Constructor arguments:
keyFilter?IDebugKeyFilter | undefined
Decides which keys to record. Only keys returning `true` are recorded. When omitted, all keys are recorded.
IDebugKeyFilter | undefinedactionFilter?IDebugActionFilter | undefined
Decides which operations to record. Only operations returning `true` are recorded. When omitted, all operations are recorded.
IDebugActionFilter | undefinedgetDebugInfo?IDebugInfoCallback | undefined
Builds the recorded info from data. When omitted, the type name and size are recorded.
IDebugInfoCallback | undefinedDebugDirection
DebugDirection is the data direction: write or read. It is write for encode on writes and read for decode on reads.
The following records writes and reads of a specific key:
import { configureSync } from "@logtape/logtape";
import { Debug } from "@unikvs/debug";
import { Memory } from "@unikvs/memory";
import * as unikvs from "unikvs";
configureSync({
sinks: { console: (record) => console.log(record.message) },
loggers: [{ category: ["@unikvs/debug"], sinks: ["console"], lowestLevel: "debug" }],
});
const kvs = new unikvs.Builder<{ foo: unikvs.Value<string> }>()
.use(
new Debug({
keyFilter: (key) => key.startsWith("foo"),
actionFilter: (action) => action === "set" || action === "get",
}),
)
.persist(new Memory())
.build();
await kvs.open();
await kvs.set("foo", "hello");
const value = await kvs.get("foo");
console.log(value); // "hello"
await kvs.close();
Data types
| Method | Input | Output |
|---|---|---|
encode |
TData |
TData |
decode |
TData |
TData |
encodeStream |
TChunk |
TChunk |
decodeStream |
TChunk |
TChunk |
encodeoutputs input data unchanged and logs it as a write.decodeoutputs input data unchanged and logs it as a read.encodeStreamoutputs each chunk unchanged and logs each one as a write.decodeStreamoutputs each chunk unchanged and logs each one as a read.- Operations rejected by filters are not logged.
Errors
This package has no package-specific errors.
Errors thrown by getDebugInfo propagate as-is.