Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/breezy-llamas-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tus/utils": minor
---

Add IoRedisKvStore & use redis.scan instead of discouraged redis.keys
5 changes: 5 additions & 0 deletions .changeset/smooth-gifts-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tus/server": minor
---

Add ioredis as optional dependency
90 changes: 85 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion packages/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ import S3Store, {type MetadataValue} from '@tus/s3-store'
import {createClient} from '@redis/client'

const client = await createClient().connect()
const path = './uploads'
const prefix = 'foo' // prefix for the key (foo${id})

new S3Store({
Expand All @@ -351,6 +350,22 @@ new S3Store({
})
```

#### `IoRedisKvStore`

```ts
import { IoRedisKvStore } from '@tus/server';
import S3Store, { type MetadataValue } from '@tus/s3-store';
import Redis from 'ioredis';

const client = new Redis();
const prefix = 'foo'; // prefix for the key (foo${id})

new S3Store({
// ...
cache: new IoRedisKvStore<MetadataValue>(client, prefix),
});
```

## Examples

### Example: integrate tus into Express
Expand Down
3 changes: 2 additions & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"ts-node": "^10.9.2"
},
"optionalDependencies": {
"@redis/client": "^1.5.13"
"@redis/client": "^1.5.13",
"ioredis": "^5.4.1"
},
"engines": {
"node": ">=16"
Expand Down
1 change: 1 addition & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@types/debug": "^4.1.12",
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.5",
"ioredis": "^5.4.1",
"mocha": "^10.4.0",
"should": "^13.2.3",
"ts-node": "^10.9.2"
Expand Down
54 changes: 54 additions & 0 deletions packages/utils/src/kvstores/IoRedisKvStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type {Redis as IoRedis} from 'ioredis'
import type {KvStore} from './Types'
import type {Upload} from '../models'

export class IoRedisKvStore<T = Upload> implements KvStore<T> {
constructor(
private redis: IoRedis,
private prefix = ''
) {
this.redis = redis
this.prefix = prefix
}

private prefixed(key: string): string {
return `${this.prefix}${key}`
}

async get(key: string): Promise<T | undefined> {
return this.deserializeValue(await this.redis.get(this.prefixed(key)))
}

async set(key: string, value: T): Promise<void> {
await this.redis.set(this.prefixed(key), this.serializeValue(value))
}

async delete(key: string): Promise<void> {
await this.redis.del(this.prefixed(key))
}

async list(): Promise<Array<string>> {
const keys = new Set<string>()
let cursor = '0'
do {
const [next, batch] = await this.redis.scan(
cursor,
'MATCH',
this.prefixed('*'),
'COUNT',
'20'
)
cursor = next
for (const key of batch) keys.add(key)
} while (cursor !== '0')
return Array.from(keys)
}

private serializeValue(value: T): string {
return JSON.stringify(value)
}

private deserializeValue(buffer: string | null): T | undefined {
return buffer ? JSON.parse(buffer) : undefined
}
}
9 changes: 8 additions & 1 deletion packages/utils/src/kvstores/RedisKvStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ export class RedisKvStore<T = Upload> implements KvStore<T> {
}

async list(): Promise<Array<string>> {
return this.redis.keys(`${this.prefix}*`)
const keys = new Set<string>()
let cursor = 0
do {
const result = await this.redis.scan(cursor, {MATCH: `${this.prefix}*`, COUNT: 20})
cursor = result.cursor
for (const key of result.keys) keys.add(key)
} while (cursor !== 0)
return Array.from(keys)
}

private serializeValue(value: T): string {
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/kvstores/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export {FileKvStore} from './FileKvStore'
export {MemoryKvStore} from './MemoryKvStore'
export {RedisKvStore} from './RedisKvStore'
export {IoRedisKvStore} from './IoRedisKvStore'
export {KvStore} from './Types'
Loading