Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add redis adapter #304

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions packages/automerge-repo-storage-redis/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2024-Present, Ryan Kois
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like in this file you missed the first line. Like here.


Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
25 changes: 25 additions & 0 deletions packages/automerge-repo-storage-redis/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@automerge/automerge-repo-storage-redis",
"version": "1.0.0",
"description": "Redis storage adapter for Automerge Repo",
"repository": "https://github.com/automerge/automerge-repo/tree/master/packages/automerge-repo-storage-redis",
"author": "Ryan Kois <[email protected]>",
"license": "MIT",
"type": "module",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"watch": "tsc -w"
},
"dependencies": {
"@automerge/automerge-repo": "workspace:*",
"debug": "^4.3.4",
"ioredis": "^5.3.2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like the ioredis must be peer dependency. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that makes sense, I agree

},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"typescript": "^5.3.3"
}
}
89 changes: 89 additions & 0 deletions packages/automerge-repo-storage-redis/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* This module provides a storage adapter for Redis.
*
* @packageDocumentation
*/

import {
Chunk,
StorageAdapterInterface,
type StorageKey,
} from "@automerge/automerge-repo"

import { Redis, RedisOptions } from "ioredis"
import debug from "debug"

const log = debug("automerge-repo:storage-redis")

export class RedisStorageAdapter implements StorageAdapterInterface {
private redis: Redis

/**
* Create a new {@link RedisStorageAdapter}.
* @param opts - Options to pass to the Redis client.
*/
constructor(
opts?: RedisOptions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe Redis instance must be passed as a constructor parameter?

) {
this.redis = new Redis(opts)
// TODO: handle errors
this.redis.on("error", (err) => {
log("redis error %O", err)
})
}

async load(keyArray: StorageKey): Promise<Uint8Array | undefined> {
const data = await this.redis.getBuffer(keyArray.join(":"))
return data
}

async save(keyArray: string[], binary: Uint8Array): Promise<void> {
console.log('save', binary.length, keyArray.join(":"))
await this.redis.set(keyArray.join(":"), Buffer.from(binary))
}

async remove(keyArray: string[]): Promise<void> {
await this.redis.del(keyArray.join(":"))
}

async loadRange(keyPrefix: string[]): Promise<Chunk[]> {
const lowerBound = [...keyPrefix, '*'].join(":")
const result: Chunk[] = []
const stream = this.redis.scanStream({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

match: lowerBound,
})
stream.on("data", async (keys: string[]) => {
for (const key of keys) {
stream.pause()
const data = await this.redis.getBuffer(key)
result.push({
key: key.split(":"),
data,
})
stream.resume()
}
})
return await new Promise((resolve, reject) => {
stream.on("end", () => resolve(result))
stream.on("error", (err) => reject(err))
})
}

async removeRange(keyPrefix: string[]): Promise<void> {
const lowerBound = [...keyPrefix, '*'].join(":")
const stream = this.redis.scanStream({
match: lowerBound,
})
stream.on("data", async (keys: string[]) => {
for (const key of keys) {
stream.pause()
await this.redis.del(key)
stream.resume()
}
})
return await new Promise((resolve, reject) => {
stream.on("end", resolve)
stream.on("error", reject)
})
}
}
15 changes: 15 additions & 0 deletions packages/automerge-repo-storage-redis/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "Node16",
"declaration": true,
"declarationMap": true,
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": false,
"skipLibCheck": true
},
"include": ["src/**/*.ts"]
}
5 changes: 5 additions & 0 deletions packages/automerge-repo-storage-redis/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": ["../../typedoc.base.json"],
"entryPoints": ["src/index.ts"],
"readme": "none"
}
Loading
Loading