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
1 change: 1 addition & 0 deletions packages/backend/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface DbExports {
export function createBackend (options?: BackendOptions): any // Replace 'any' with Backend class type

// Exports instances and constructors for Redis and database connections
export function getRedis (options?: any): Redis
export const redis: RedisExports['redis']
export const redlock: RedisExports['redlock']
export const Redlock: RedisExports['Redlock']
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default function createBackend ({

const backend = new ShareDB({
db,
pubsub,
...(pubsub ? { pubsub } : {}),
extraDbs
})

Expand Down
3 changes: 2 additions & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
"ioredis": "^5.3.2",
"ioredis-mock": "^8.9.0",
"mongodb": "^6.0.0",
"redis": "^5.0.0",
"redlock": "^3.0.0",
"sharedb": "^5.0.0",
"sharedb": "5.2.2",
"sharedb-hooks": "~4.0.0",
"sharedb-mongo": "^4.1.2",
"sharedb-redis-pubsub": "^5.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import RedisMock from 'ioredis-mock'

export { Redis, RedisMock }

export function getRedis ({ enable = true, opts, url, keyPrefix, ...additionalOptions }) {
export function getIoRedis ({ enable = true, opts, url, keyPrefix, ...additionalOptions }) {
if (enable) {
if (typeof opts === 'string') {
opts = JSON.parse(opts)
Expand Down
59 changes: 59 additions & 0 deletions packages/backend/redis/getNodeRedis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { readFileSync } from 'fs'
import { createClient, createSentinel } from 'redis'

export function getNodeRedis ({ opts, url }) {
const parsedOpts = typeof opts === 'string' ? JSON.parse(opts) : opts

let client

if (parsedOpts?.sentinels) {
client = createSentinel(_getSentinelOptions(parsedOpts))
} else if (url) {
client = createClient({ url })
}

if (!client) {
throw new Error('[@teamplay/backend] REDIS_URL or REDIS_OPTS is required when Redis pubsub is enabled')
}

client.on('error', error => {
console.error('[@teamplay/backend] Redis pubsub client error:', error)
})

return client
}

function _getSentinelOptions (opts) {
const tls = _getTlsOptions(opts)
const socket = tls ? { tls: true, ...tls } : undefined
const sentinelRootNodes = opts.sentinels.map(sentinel => ({
host: sentinel.host || sentinel.hostname || sentinel.ip || sentinel.address,
port: Number(sentinel.port || opts.sentinel_port || 26379)
}))

return {
name: opts.name || 'mymaster',
sentinelRootNodes,
nodeClientOptions: {
socket,
username: opts.username,
password: opts.password,
database: opts.db || 0
},
sentinelClientOptions: {
socket,
username: opts.sentinelUsername,
password: opts.sentinelPassword
}
}
}

function _getTlsOptions (opts) {
if (!opts?.key) return

return {
key: readFileSync(opts.key),
cert: readFileSync(opts.cert),
ca: readFileSync(opts.ca)
}
}
29 changes: 20 additions & 9 deletions packages/backend/redis/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
import Redlock from 'redlock'
import redisPubSub from 'sharedb-redis-pubsub'
import { getRedis, Redis, RedisMock } from './getRedis.js'
import { getIoRedis, Redis, RedisMock } from './getIoRedis.js'
import { getNodeRedis } from './getNodeRedis.js'

const ENABLE_REDIS = !process.env.NO_REDIS
const ENABLE_REDIS_PUBSUB = ENABLE_REDIS && !!(process.env.REDIS_URL || process.env.REDIS_OPTS)

const RedisClient = ENABLE_REDIS ? Redis : RedisMock
export { RedisClient as Redis, getRedis, getRedisOptions, generatePrefix }
export {
RedisClient as Redis,
getRedisOptions,
generatePrefix
}
export const getRedis = getIoRedis
export const prefix = generatePrefix({
mongoUrl: process.env.MONGO_URL,
baseUrl: process.env.BASE_URL
})
export const redis = getRedis(getRedisOptions())
export const redisObserver = getRedis(getRedisOptions())
export const redis = getIoRedis(getRedisOptions())

export const pubsub = redisPubSub({
client: redis,
observer: redisObserver,
prefix
})
// Teamplay exposes ioredis for the rest of the backend ecosystem
// (BullMQ, Redlock, mocks), but sharedb-redis-pubsub@5 expects node-redis.
// We therefore create separate node-redis clients only for ShareDB pubsub.
export const pubsub = ENABLE_REDIS_PUBSUB
? redisPubSub({
client: getNodeRedis(getRedisOptions({ addPrefix: false })),
observer: getNodeRedis(getRedisOptions({ addPrefix: false })),
prefix
})
: undefined

export const redlock = getRedlock(redis)

Expand Down
4 changes: 1 addition & 3 deletions packages/sharedb-access/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
"lodash": "^4.17.20"
},
"devDependencies": {
"mocha": "^11.7.5",
"racer": "1.0.1",
"sharedb": "^5.0.0"
"mocha": "^11.7.5"
}
}
1 change: 1 addition & 0 deletions packages/sharedb-access/test/db.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO: Update tests to work with new teamplay api
import promisifyRacer from '@startupjs/orm/lib/promisifyRacer.js'
import racer from 'racer'
import sharedb from 'sharedb'
Expand Down
4 changes: 1 addition & 3 deletions packages/sharedb-schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
"z-schema": "^4.2.3"
},
"devDependencies": {
"mocha": "^11.7.5",
"racer": "1.0.1",
"sharedb": "^5.0.0"
"mocha": "^11.7.5"
}
}
1 change: 1 addition & 0 deletions packages/sharedb-schema/test/model.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO: Update tests to work with new teamplay api
import promisifyRacer from '@startupjs/orm/lib/promisifyRacer.js'
import racer from 'racer'
import sharedb from 'sharedb'
Expand Down
2 changes: 1 addition & 1 deletion packages/teamplay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"localforage": "^1.10.0",
"lodash": "^4.17.20",
"pluralize": "^8.0.0",
"sharedb": "^5.0.0",
"sharedb": "5.2.2",
"stream": "npm:readable-stream@^4.7.0"
},
"devDependencies": {
Expand Down
Loading
Loading