File size: 8,046 Bytes
76289e7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | // Public facade for plugin-scoped SQLite blob storage.
import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js";
import {
MAX_PLUGIN_BLOB_BYTES_PER_ENTRY,
MAX_PLUGIN_BLOB_BYTES_PER_PLUGIN,
MAX_PLUGIN_BLOB_ENTRIES_PER_PLUGIN,
pluginBlobClear,
pluginBlobDelete,
pluginBlobDeleteExpiredKey,
pluginBlobDeleteExpired,
pluginBlobEntries,
pluginBlobLookup,
pluginBlobRegister,
pluginBlobRegisterIfAbsent,
} from "./plugin-blob-store.sqlite.js";
import type {
OpenBlobStoreOptions,
PluginBlobOverflowPolicy,
PluginBlobStore,
PluginBlobStoreOperation,
} from "./plugin-blob-store.types.js";
import { PluginBlobStoreError } from "./plugin-blob-store.types.js";
import {
createPluginStoreOptionPolicy,
serializePluginStoreJson,
validateOptionalPluginStoreTtlMs,
validatePluginStoreKey,
validatePluginStoreNamespace,
validatePluginStorePositiveInteger,
} from "./plugin-store-validation.js";
export type {
OpenBlobStoreOptions,
PluginBlobEntry,
PluginBlobEntryInfo,
PluginBlobStore,
} from "./plugin-blob-store.types.js";
type BlobStoreOptionSignature = {
maxEntries: number;
maxBytesPerEntry: number;
maxBytesPerNamespace: number;
overflowPolicy: PluginBlobOverflowPolicy;
defaultTtlMs?: number;
};
type PreparedBlob = {
key: string;
bytes: Uint8Array;
metadataJson: string;
ttlMs?: number;
};
function invalidInput(
message: string,
operation: PluginBlobStoreOperation = "register",
): PluginBlobStoreError {
return new PluginBlobStoreError(message, {
code: "PLUGIN_BLOB_INVALID_INPUT",
operation,
});
}
function limitError(message: string): PluginBlobStoreError {
return new PluginBlobStoreError(message, {
code: "PLUGIN_BLOB_LIMIT_EXCEEDED",
operation: "register",
});
}
const validationErrors = (operation: PluginBlobStoreOperation) => ({
invalid: (message: string) => invalidInput(message, operation),
limit: (message: string) => limitError(message),
});
function validateNamespace(value: string): string {
return validatePluginStoreNamespace({
value,
label: "plugin blob",
errors: validationErrors("open"),
});
}
function validateKey(value: string, operation: PluginBlobStoreOperation): string {
return validatePluginStoreKey({
value,
label: "plugin blob",
errors: validationErrors(operation),
});
}
function validatePositiveLimit(value: number, label: string, maximum: number): number {
const normalized = validatePluginStorePositiveInteger({
value,
label,
errors: validationErrors("open"),
});
if (normalized > maximum) {
throw invalidInput(`${label} must be <= ${maximum}`, "open");
}
return normalized;
}
const optionPolicy = createPluginStoreOptionPolicy<BlobStoreOptionSignature>({
label: "plugin blob",
invalid: (message) => invalidInput(message, "open"),
});
function validateTtl(
value: number | undefined,
operation: PluginBlobStoreOperation,
): number | undefined {
return validateOptionalPluginStoreTtlMs({
value,
label: "plugin blob ttlMs",
errors: validationErrors(operation),
});
}
function prepareBlob(params: {
key: string;
bytes: Uint8Array;
metadata: unknown;
maxBytesPerEntry: number;
defaultTtlMs?: number;
opts?: { ttlMs?: number };
}): PreparedBlob {
const key = validateKey(params.key, "register");
if (!(params.bytes instanceof Uint8Array)) {
throw invalidInput("plugin blob bytes must be a Uint8Array");
}
if (params.bytes.byteLength > params.maxBytesPerEntry) {
throw limitError(
`plugin blob entry exceeds the configured ${params.maxBytesPerEntry} byte limit`,
);
}
const metadataJson = serializePluginStoreJson({
value: params.metadata,
label: "plugin blob metadata",
errors: validationErrors("register"),
});
const ttlMs = validateTtl(params.opts?.ttlMs, "register") ?? params.defaultTtlMs;
return {
key,
bytes: Uint8Array.from(params.bytes),
metadataJson,
...(ttlMs !== undefined ? { ttlMs } : {}),
};
}
function createPluginBlobStoreInternal<TMetadata>(
pluginId: string,
options: OpenBlobStoreOptions,
env?: NodeJS.ProcessEnv,
): PluginBlobStore<TMetadata> {
if (pluginId.startsWith("core:")) {
throw invalidInput("Plugin ids starting with 'core:' are reserved for core consumers.", "open");
}
const namespace = validateNamespace(options.namespace);
const maxEntries = validatePositiveLimit(
options.maxEntries,
"plugin blob maxEntries",
MAX_PLUGIN_BLOB_ENTRIES_PER_PLUGIN,
);
const maxBytesPerEntry = validatePositiveLimit(
options.maxBytesPerEntry,
"plugin blob maxBytesPerEntry",
MAX_PLUGIN_BLOB_BYTES_PER_ENTRY,
);
const maxBytesPerNamespace = validatePositiveLimit(
options.maxBytesPerNamespace,
"plugin blob maxBytesPerNamespace",
MAX_PLUGIN_BLOB_BYTES_PER_PLUGIN,
);
if (maxBytesPerEntry > maxBytesPerNamespace) {
throw invalidInput("plugin blob maxBytesPerEntry must not exceed maxBytesPerNamespace", "open");
}
const overflowPolicy = optionPolicy.resolveOverflowPolicy(options.overflowPolicy);
const defaultTtlMs = validateTtl(options.defaultTtlMs, "open");
optionPolicy.assertConsistent(pluginId, namespace, {
maxEntries,
maxBytesPerEntry,
maxBytesPerNamespace,
overflowPolicy,
defaultTtlMs,
});
const writeParams = (blob: PreparedBlob) => ({
pluginId,
namespace,
key: blob.key,
bytes: blob.bytes,
metadataJson: blob.metadataJson,
maxEntries,
maxBytesPerNamespace,
overflowPolicy,
...(blob.ttlMs !== undefined ? { ttlMs: blob.ttlMs } : {}),
...(env ? { env } : {}),
});
return {
async register(key, bytes, metadata, opts) {
const blob = prepareBlob({
key,
bytes,
metadata,
maxBytesPerEntry,
defaultTtlMs,
opts,
});
pluginBlobRegister(writeParams(blob));
},
async registerIfAbsent(key, bytes, metadata, opts) {
const blob = prepareBlob({
key,
bytes,
metadata,
maxBytesPerEntry,
defaultTtlMs,
opts,
});
return pluginBlobRegisterIfAbsent(writeParams(blob));
},
async lookup(key) {
return pluginBlobLookup<TMetadata>({
pluginId,
namespace,
key: validateKey(key, "lookup"),
...(env ? { env } : {}),
});
},
async entries() {
return pluginBlobEntries<TMetadata>({ pluginId, namespace, ...(env ? { env } : {}) });
},
async delete(key) {
return pluginBlobDelete({
pluginId,
namespace,
key: validateKey(key, "delete"),
...(env ? { env } : {}),
});
},
async deleteExpiredKey(key) {
return pluginBlobDeleteExpiredKey<TMetadata>({
pluginId,
namespace,
key: validateKey(key, "sweep"),
...(env ? { env } : {}),
});
},
async deleteExpired() {
return pluginBlobDeleteExpired<TMetadata>({
pluginId,
namespace,
...(env ? { env } : {}),
});
},
async clear() {
pluginBlobClear({ pluginId, namespace, ...(env ? { env } : {}) });
},
};
}
/** Opens an async blob namespace for a non-core plugin id. */
export function createPluginBlobStore<TMetadata>(
pluginId: string,
options: OpenBlobStoreOptions,
): PluginBlobStore<TMetadata> {
return createPluginBlobStoreInternal<TMetadata>(pluginId, options);
}
/** Test-only factory with an isolated state environment. */
export function createPluginBlobStoreForTests<TMetadata>(
pluginId: string,
options: OpenBlobStoreOptions,
env: NodeJS.ProcessEnv,
): PluginBlobStore<TMetadata> {
return createPluginBlobStoreInternal<TMetadata>(pluginId, options, env);
}
/** Resets facade signatures and the shared state database handle for tests. */
export function resetPluginBlobStoreForTests(options: { closeDatabase?: boolean } = {}): void {
optionPolicy.clear();
if (options.closeDatabase !== false) {
closeOpenClawStateDatabaseForTest();
}
}
|