Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

feat(api): add incr to the kv client and mock provider

Hampus0cb18d16d3b1
diff --git a/fluxer_api/pkgs/kv_client/src/IKVProvider.ts b/fluxer_api/pkgs/kv_client/src/IKVProvider.ts
index fd7824123..677960308 100644
--- a/fluxer_api/pkgs/kv_client/src/IKVProvider.ts
+++ b/fluxer_api/pkgs/kv_client/src/IKVProvider.ts
@@ -44,6 +44,7 @@ export interface IKVProvider {
exists(key: string): Promise<number>;
expire(key: string, ttlSeconds: number): Promise<number>;
ttl(key: string): Promise<number>;
+ incr(key: string): Promise<number>;
getdel(key: string): Promise<string | null>;
sadd(key: string, ...members: Array<string>): Promise<number>;
srem(key: string, ...members: Array<string>): Promise<number>;
diff --git a/fluxer_api/pkgs/kv_client/src/KVClient.ts b/fluxer_api/pkgs/kv_client/src/KVClient.ts
index 0431a344a..501906cde 100644
--- a/fluxer_api/pkgs/kv_client/src/KVClient.ts
+++ b/fluxer_api/pkgs/kv_client/src/KVClient.ts
@@ -366,6 +366,10 @@ export class KVClient implements IKVProvider {
return await this.execute('ttl', async () => this.client.ttl(key));
}
+ async incr(key: string): Promise<number> {
+ return await this.execute('incr', async () => this.client.incr(key));
+ }
+
async getdel(key: string): Promise<string | null> {
return await this.execute('getdel', async () => {
const result = await this.client.call('GETDEL', key);
diff --git a/fluxer_api/pkgs/kv_client/src/testing/MockKVProvider.ts b/fluxer_api/pkgs/kv_client/src/testing/MockKVProvider.ts
index b02539921..d9d096790 100644
--- a/fluxer_api/pkgs/kv_client/src/testing/MockKVProvider.ts
+++ b/fluxer_api/pkgs/kv_client/src/testing/MockKVProvider.ts
@@ -138,6 +138,14 @@ export class MockKVProvider implements IKVProvider {
return remainingSeconds;
}
+ async incr(key: string): Promise<number> {
+ this.evictIfExpired(key);
+ const value = Number.parseInt(this.stringStore.get(key) ?? '0', 10) + 1;
+ this.ensureType(key, 'string');
+ this.stringStore.set(key, String(value));
+ return value;
+ }
+
async getdel(key: string): Promise<string | null> {
this.evictIfExpired(key);
const value = this.stringStore.get(key) ?? null;
@@ -268,8 +276,8 @@ export class MockKVProvider implements IKVProvider {
}
const offset = options?.offset ?? 0;
const limit = options?.count ?? Number.POSITIVE_INFINITY;
- const minBound = parseScoreBound(min, true);
- const maxBound = parseScoreBound(max, false);
+ const minBound = parseScoreBound(min, 'minimum');
+ const maxBound = parseScoreBound(max, 'maximum');
const sortedMembers = Array.from(zset.entries()).sort((a, b) => {
if (a[1] === b[1]) {
return a[0].localeCompare(b[0]);
@@ -833,7 +841,7 @@ interface StoredLeakyBucketState {
updatedAtMs: number;
}
-function parseScoreBound(bound: string | number, isMin: boolean): ScoreBound {
+function parseScoreBound(bound: string | number, side: 'minimum' | 'maximum'): ScoreBound {
if (typeof bound === 'number') {
return {value: bound, exclusive: false};
}
@@ -843,17 +851,14 @@ function parseScoreBound(bound: string | number, isMin: boolean): ScoreBound {
if (bound === '+inf') {
return {value: Number.POSITIVE_INFINITY, exclusive: false};
}
- if (bound.startsWith('(')) {
- const value = Number(bound.slice(1));
- return {
- value: Number.isFinite(value) ? value : isMin ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY,
- exclusive: true,
- };
+ const exclusive = bound.startsWith('(');
+ const value = Number(exclusive ? bound.slice(1) : bound);
+ if (Number.isFinite(value)) {
+ return {value, exclusive};
}
- const value = Number(bound);
return {
- value: Number.isFinite(value) ? value : isMin ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY,
- exclusive: false,
+ value: side === 'minimum' ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY,
+ exclusive,
};
}