Skip to content

Commit d62bf9d

Browse files
committed
refactor: Apply review changes
Signed-off-by: Dylan Kilkenny <dylankilkenny95@gmail.com>
1 parent 9be5dc1 commit d62bf9d

File tree

4 files changed

+9
-48
lines changed

4 files changed

+9
-48
lines changed

plugins/examples/kv-storage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export async function handler({ kv, params }: PluginContext) {
5454

5555
case 'scan': {
5656
const { pattern, batch } = params ?? {};
57-
const keys = await kv.scan(pattern ?? '*', toInt(batch, 500));
57+
const keys = await kv.listKeys(pattern ?? '*', toInt(batch, 500));
5858
return { keys };
5959
}
6060

@@ -95,7 +95,7 @@ export async function handler({ kv, params }: PluginContext) {
9595
const deleted = await kv.del('example:to-delete');
9696

9797
// 4) Scan keys under example:*
98-
const list = await kv.scan('example:*');
98+
const list = await kv.listKeys('example:*');
9999

100100
// 5) Use a lock to protect an update
101101
const lockResult = await kv.withLock(

plugins/lib/kv.ts

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,6 @@ import { randomUUID } from 'crypto';
2626
* Minimal async key-value store used by plugins.
2727
*/
2828
export interface PluginKVStore {
29-
/**
30-
* Establish the connection. Call once before using the store.
31-
* @returns Promise that resolves when the connection is established.
32-
*/
33-
connect(): Promise<void>;
34-
/**
35-
* Close the connection gracefully. Safe to call multiple times.
36-
* @returns Promise that resolves when the connection is closed.
37-
*/
38-
disconnect(): Promise<void>;
39-
4029
/**
4130
* Get and JSON-parse a value by key.
4231
* @typeParam T - Expected value type after JSON parse.
@@ -68,12 +57,12 @@ export interface PluginKVStore {
6857
exists(key: string): Promise<boolean>;
6958

7059
/**
71-
* Scan this namespace for keys matching `pattern`.
60+
* List keys in this namespace matching `pattern`.
7261
* @param pattern - Glob-like match pattern (default '*').
7362
* @param batch - SCAN COUNT per iteration (default 500).
7463
* @returns Array of bare keys (without the namespace prefix).
7564
*/
76-
scan(pattern?: string, batch?: number): Promise<string[]>;
65+
listKeys(pattern?: string, batch?: number): Promise<string[]>;
7766
/**
7867
* Remove all keys in this namespace.
7968
* @returns The number of keys deleted.
@@ -133,27 +122,6 @@ export class DefaultPluginKVStore implements PluginKVStore {
133122
this.ns = `plugin_kv:{${pid}}`;
134123
}
135124

136-
/**
137-
* Connect the underlying Redis client. Call before any operation.
138-
* @returns Promise that resolves when the Redis connection is established.
139-
*/
140-
async connect(): Promise<void> {
141-
await this.client.connect();
142-
}
143-
144-
/**
145-
* Close the Redis connection. Falls back to hard disconnect if graceful
146-
* quit fails (e.g., when the connection is not fully established).
147-
* @returns Promise that resolves when the connection is closed.
148-
*/
149-
async disconnect(): Promise<void> {
150-
try {
151-
await this.client.quit();
152-
} catch {
153-
this.client.disconnect();
154-
}
155-
}
156-
157125
/**
158126
* Build a namespaced Redis key for the given segment and user key.
159127
* Validates the user-provided key and throws on invalid input.
@@ -222,13 +190,13 @@ export class DefaultPluginKVStore implements PluginKVStore {
222190
}
223191

224192
/**
225-
* Iterate over keys in this store, matching `pattern` (glob-like, default '*').
193+
* List keys in this store matching `pattern` (glob-like, default '*').
226194
* Returns bare keys (without namespace). Uses SCAN with COUNT=`batch`.
227195
* @param pattern - Glob-like match pattern (default '*').
228196
* @param batch - SCAN COUNT per iteration (default 500).
229197
* @returns Array of bare keys (without the namespace prefix).
230198
*/
231-
async scan(pattern = '*', batch = 500): Promise<string[]> {
199+
async listKeys(pattern = '*', batch = 500): Promise<string[]> {
232200
const out: string[] = [];
233201
let cursor = '0';
234202
const prefix = `${this.ns}:data:`;

plugins/lib/plugin.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -496,21 +496,14 @@ export async function runUserPlugin<T = any, R = any>(
496496
pluginParams: T,
497497
userScriptPath: string
498498
): Promise<R> {
499-
// Create both resources at the same level
500499
const plugin = new DefaultPluginAPI(socketPath);
501500
const kv = new DefaultPluginKVStore(pluginId);
502501

503502
try {
504-
// Connect KV before execution
505-
await kv.connect();
506-
507-
// Pass both API and KV to loader
508503
const result: R = await loadAndExecutePlugin<T, R>(userScriptPath, plugin, kv, pluginParams);
509504

510505
return result;
511506
} finally {
512-
// Clean up both resources at the same level
513-
await kv.disconnect();
514507
plugin.close();
515508
}
516509
}

plugins/tests/lib/kv.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ describe('PluginKV', () => {
7575
await kv.set('user:2', { name: 'Bob' });
7676
// Hold a lock during scan to ensure lock keys are not included
7777
await kv.withLock('some-resource', async () => {
78-
const userKeys = await kv.scan('user:*');
78+
const userKeys = await kv.listKeys('user:*');
7979
expect(userKeys).toHaveLength(2);
8080
expect(userKeys).toContain('user:1');
8181
expect(userKeys).toContain('user:2');
8282
});
8383

84-
const allKeys = await kv.scan();
84+
const allKeys = await kv.listKeys();
8585
expect(allKeys).toHaveLength(2);
8686
});
8787

@@ -92,7 +92,7 @@ describe('PluginKV', () => {
9292
}
9393
const deleted = await kv.clear();
9494
expect(deleted).toBeGreaterThanOrEqual(n);
95-
const keys = await kv.scan();
95+
const keys = await kv.listKeys();
9696
expect(keys).toHaveLength(0);
9797
});
9898
});

0 commit comments

Comments
 (0)