@unikvs/readonly
How to use @unikvs/readonly, which rejects writes to make storage read-only.
Overview
@unikvs/readonly Middleware rejects writes to make storage read-only.
It rejects write-direction operations (set / copy / move / encode) and passes reads through. When allowDelete is false, it also rejects deletion.
nameisReadOnly.isOpenis alwaystrue.allowDeletedefaults tofalse.
Install with:
npm install @unikvs/readonlypnpm add @unikvs/readonlyyarn add @unikvs/readonlybun add @unikvs/readonlynub add @unikvs/readonlyaube add @unikvs/readonlyUsage
ReadOnly implements IMiddleware. Pass it to UniKvs with use().
import { ReadOnly } from "@unikvs/readonly";
ReadOnly
Middleware that rejects writes to make storage read-only.
Constructor arguments:
allowDelete?boolean
Whether to allow delete operations. When false, delete and clear are rejected.
booleanfalseThe following reads back stored values:
import { Memory } from "@unikvs/memory";
import { ReadOnly } from "@unikvs/readonly";
import * as unikvs from "unikvs";
const storage = new Memory();
const writer = new unikvs.Builder<{
foo: unikvs.Value<Uint8Array>;
}>()
.persist(storage)
.build();
await writer.open();
await writer.set("foo", new Uint8Array([0, 1, 2]));
await writer.close();
const kvs = new unikvs.Builder<{
foo: unikvs.Value<Uint8Array>;
}>()
.use(new ReadOnly())
.persist(storage)
.build();
await kvs.open();
const data = await kvs.get("foo");
console.log(data); // Uint8Array(3) [0, 1, 2]
await kvs.close();
Data types
| Method | Input | Output |
|---|---|---|
encode |
write value | Always throws ReadOnlySetNotAllowedError |
decode |
read value | Read value (passed through) |
getEncodable |
write chunk | Always throws ReadOnlySetNotAllowedError |
getDecodable |
read chunk | Read chunk (passed through) |
- The write direction always fails; the read direction performs no conversion and passes through.
copyandmoveare rejected because they write to the destination key.- Deletion is controlled by
use(): whenallowDeleteisfalse,deleteandclearare rejected.
Errors
Errors thrown by this package:
ReadOnlySetNotAllowedError
Meaning
Thrown when a write-direction operation (set / copy / move / encode) is called.
How to fix
Do not use this middleware when writes are needed.
Example
Catching ReadOnlySetNotAllowedError:
import { ReadOnlySetNotAllowedError } from "@unikvs/readonly";
try {
await kvs.set("foo", new Uint8Array([0, 1, 2]));
} catch (ex) {
if (ex instanceof ReadOnlySetNotAllowedError) {
console.log("writes are not allowed");
} else {
throw ex;
}
}
ReadOnlyDeleteNotAllowedError
Meaning
Thrown when delete is called while allowDelete is false.
How to fix
Pass allowDelete: true when deletion is needed.
ReadOnlyClearNotAllowedError
Meaning
Thrown when clear is called while allowDelete is false.
How to fix
Pass allowDelete: true when clearing everything is needed.