-
Notifications
You must be signed in to change notification settings - Fork 59
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2024-Present, Ryan Kois | ||
|
||
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. |
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It feels like the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
}) | ||
} | ||
} |
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"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": ["../../typedoc.base.json"], | ||
"entryPoints": ["src/index.ts"], | ||
"readme": "none" | ||
} |
There was a problem hiding this comment.
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.