chore: import upstream snapshot with attribution
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
/**
|
||||
* Generic Pub/Sub Channel Factory
|
||||
*
|
||||
* Creates a single-channel pub/sub adapter backed by Redis (with EventEmitter fallback).
|
||||
* Each call creates its own Redis connections — use multiple instances for multiple channels.
|
||||
*/
|
||||
|
||||
import { EventEmitter } from 'events'
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { noop } from '@sim/utils/helpers'
|
||||
import Redis, { type RedisOptions } from 'ioredis'
|
||||
import { env } from '@/lib/core/config/env'
|
||||
import { getRedisConnectionDefaults } from '@/lib/core/config/redis'
|
||||
|
||||
const logger = createLogger('PubSub')
|
||||
|
||||
export interface PubSubChannel<T> {
|
||||
publish(event: T): void
|
||||
subscribe(handler: (event: T) => void): () => void
|
||||
dispose(): void
|
||||
}
|
||||
|
||||
interface PubSubChannelConfig {
|
||||
channel: string
|
||||
label: string
|
||||
}
|
||||
|
||||
class RedisPubSubChannel<T> implements PubSubChannel<T> {
|
||||
private pub: Redis
|
||||
private sub: Redis
|
||||
private handlers = new Set<(event: T) => void>()
|
||||
private disposed = false
|
||||
|
||||
constructor(
|
||||
redisUrl: string,
|
||||
connectionDefaults: ReturnType<typeof getRedisConnectionDefaults>,
|
||||
private config: PubSubChannelConfig
|
||||
) {
|
||||
const commonOpts = {
|
||||
...connectionDefaults,
|
||||
maxRetriesPerRequest: null,
|
||||
retryStrategy: (times: number) => {
|
||||
if (times > 10) return 30000
|
||||
return Math.min(times * 500, 5000)
|
||||
},
|
||||
} satisfies RedisOptions
|
||||
|
||||
this.pub = new Redis(redisUrl, { ...commonOpts, connectionName: `${config.label}-pub` })
|
||||
this.sub = new Redis(redisUrl, { ...commonOpts, connectionName: `${config.label}-sub` })
|
||||
|
||||
this.pub.on('error', (err) =>
|
||||
logger.error(`${config.label} publish client error:`, err.message)
|
||||
)
|
||||
this.sub.on('error', (err) =>
|
||||
logger.error(`${config.label} subscribe client error:`, err.message)
|
||||
)
|
||||
this.pub.on('connect', () => logger.info(`${config.label} publish client connected`))
|
||||
this.sub.on('connect', () => logger.info(`${config.label} subscribe client connected`))
|
||||
|
||||
this.sub.subscribe(config.channel, (err) => {
|
||||
if (err) {
|
||||
logger.error(`Failed to subscribe to ${config.label} channel:`, err)
|
||||
} else {
|
||||
logger.info(`Subscribed to ${config.label} channel`)
|
||||
}
|
||||
})
|
||||
|
||||
this.sub.on('message', (channel: string, message: string) => {
|
||||
if (channel !== config.channel) return
|
||||
try {
|
||||
const parsed = JSON.parse(message) as T
|
||||
for (const handler of this.handlers) {
|
||||
try {
|
||||
handler(parsed)
|
||||
} catch (err) {
|
||||
logger.error(`Error in ${config.label} handler:`, err)
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error(`Failed to parse ${config.label} message:`, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
publish(event: T): void {
|
||||
if (this.disposed) return
|
||||
this.pub.publish(this.config.channel, JSON.stringify(event)).catch((err) => {
|
||||
logger.error(`Failed to publish to ${this.config.label}:`, err)
|
||||
})
|
||||
}
|
||||
|
||||
subscribe(handler: (event: T) => void): () => void {
|
||||
this.handlers.add(handler)
|
||||
return () => {
|
||||
this.handlers.delete(handler)
|
||||
}
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this.disposed = true
|
||||
this.handlers.clear()
|
||||
|
||||
this.pub.removeAllListeners()
|
||||
this.sub.removeAllListeners()
|
||||
this.pub.on('error', noop)
|
||||
this.sub.on('error', noop)
|
||||
|
||||
this.sub.unsubscribe().catch(noop)
|
||||
this.pub.quit().catch(noop)
|
||||
this.sub.quit().catch(noop)
|
||||
logger.info(`${this.config.label} Redis pub/sub disposed`)
|
||||
}
|
||||
}
|
||||
|
||||
class LocalPubSubChannel<T> implements PubSubChannel<T> {
|
||||
private emitter = new EventEmitter()
|
||||
|
||||
constructor(private config: PubSubChannelConfig) {
|
||||
this.emitter.setMaxListeners(100)
|
||||
logger.info(`${config.label}: Using process-local EventEmitter (Redis not configured)`)
|
||||
}
|
||||
|
||||
publish(event: T): void {
|
||||
this.emitter.emit(this.config.channel, event)
|
||||
}
|
||||
|
||||
subscribe(handler: (event: T) => void): () => void {
|
||||
this.emitter.on(this.config.channel, handler)
|
||||
return () => {
|
||||
this.emitter.off(this.config.channel, handler)
|
||||
}
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this.emitter.removeAllListeners()
|
||||
logger.info(`${this.config.label} local pub/sub disposed`)
|
||||
}
|
||||
}
|
||||
|
||||
export function createPubSubChannel<T>(config: PubSubChannelConfig): PubSubChannel<T> {
|
||||
const redisUrl = env.REDIS_URL
|
||||
if (!redisUrl) return new LocalPubSubChannel<T>(config)
|
||||
|
||||
// Resolve config-derived defaults outside the try so a missing
|
||||
// REDIS_TLS_SERVERNAME (config error) surfaces instead of silently degrading
|
||||
// to the in-process EventEmitter — that would break cross-replica pub/sub.
|
||||
const connectionDefaults = getRedisConnectionDefaults(redisUrl)
|
||||
|
||||
try {
|
||||
logger.info(`${config.label}: Using Redis`)
|
||||
return new RedisPubSubChannel<T>(redisUrl, connectionDefaults, config)
|
||||
} catch (err) {
|
||||
logger.error(`Failed to create Redis ${config.label}, falling back to local:`, err)
|
||||
return new LocalPubSubChannel<T>(config)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* Generic Workspace SSE Endpoint Factory
|
||||
*
|
||||
* Creates a GET handler that authenticates the user, verifies workspace access,
|
||||
* and streams Server-Sent Events with heartbeats and cleanup.
|
||||
*/
|
||||
|
||||
import { createLogger } from '@sim/logger'
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { getSession } from '@/lib/auth'
|
||||
import { SSE_HEADERS } from '@/lib/core/utils/sse'
|
||||
import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils'
|
||||
|
||||
interface SSESubscription {
|
||||
subscribe(
|
||||
workspaceId: string,
|
||||
send: (eventName: string, data: Record<string, unknown>) => void
|
||||
): () => void
|
||||
}
|
||||
|
||||
interface WorkspaceSSEConfig {
|
||||
label: string
|
||||
subscriptions: SSESubscription[]
|
||||
}
|
||||
|
||||
const HEARTBEAT_INTERVAL_MS = 30_000
|
||||
|
||||
export function createWorkspaceSSE(config: WorkspaceSSEConfig) {
|
||||
const logger = createLogger(`${config.label}-SSE`)
|
||||
|
||||
return async function GET(request: NextRequest): Promise<Response> {
|
||||
const session = await getSession()
|
||||
if (!session?.user?.id) {
|
||||
return new Response('Unauthorized', { status: 401 })
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url)
|
||||
const workspaceId = searchParams.get('workspaceId')
|
||||
if (!workspaceId) {
|
||||
return new Response('Missing workspaceId query parameter', { status: 400 })
|
||||
}
|
||||
|
||||
const permissions = await getUserEntityPermissions(session.user.id, 'workspace', workspaceId)
|
||||
if (!permissions) {
|
||||
return new Response('Access denied to workspace', { status: 403 })
|
||||
}
|
||||
|
||||
const encoder = new TextEncoder()
|
||||
const unsubscribers: Array<() => void> = []
|
||||
let cleaned = false
|
||||
|
||||
const cleanup = () => {
|
||||
if (cleaned) return
|
||||
cleaned = true
|
||||
for (const unsub of unsubscribers) {
|
||||
unsub()
|
||||
}
|
||||
logger.info(`SSE connection closed for workspace ${workspaceId}`)
|
||||
}
|
||||
|
||||
const stream = new ReadableStream({
|
||||
start(controller) {
|
||||
const send = (eventName: string, data: Record<string, unknown>) => {
|
||||
if (cleaned) return
|
||||
try {
|
||||
controller.enqueue(
|
||||
encoder.encode(`event: ${eventName}\ndata: ${JSON.stringify(data)}\n\n`)
|
||||
)
|
||||
} catch {
|
||||
// Stream already closed
|
||||
}
|
||||
}
|
||||
|
||||
for (const subscription of config.subscriptions) {
|
||||
const unsub = subscription.subscribe(workspaceId, send)
|
||||
unsubscribers.push(unsub)
|
||||
}
|
||||
|
||||
const heartbeat = setInterval(() => {
|
||||
if (cleaned) {
|
||||
clearInterval(heartbeat)
|
||||
return
|
||||
}
|
||||
try {
|
||||
controller.enqueue(encoder.encode(': heartbeat\n\n'))
|
||||
} catch {
|
||||
clearInterval(heartbeat)
|
||||
}
|
||||
}, HEARTBEAT_INTERVAL_MS)
|
||||
unsubscribers.push(() => clearInterval(heartbeat))
|
||||
|
||||
request.signal.addEventListener(
|
||||
'abort',
|
||||
() => {
|
||||
cleanup()
|
||||
try {
|
||||
controller.close()
|
||||
} catch {
|
||||
// Already closed
|
||||
}
|
||||
},
|
||||
{ once: true }
|
||||
)
|
||||
|
||||
logger.info(`SSE connection opened for workspace ${workspaceId}`)
|
||||
},
|
||||
cancel() {
|
||||
cleanup()
|
||||
},
|
||||
})
|
||||
|
||||
return new Response(stream, { headers: SSE_HEADERS })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user