Skip to content

Commit

Permalink
feat: tus v1 stable
Browse files Browse the repository at this point in the history
  • Loading branch information
fenos committed Dec 7, 2023
1 parent b6385fa commit 8a4fda7
Show file tree
Hide file tree
Showing 20 changed files with 2,139 additions and 1,643 deletions.
2,841 changes: 1,723 additions & 1,118 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
"@fastify/swagger": "^8.3.1",
"@fastify/swagger-ui": "^1.7.0",
"@isaacs/ttlcache": "^1.4.1",
"@tus/file-store": "^1.0.0-beta.1",
"@tus/s3-store": "https://gitpkg.now.sh/supabase/tus-node-server/packages/s3-store/dist?build",
"@tus/server": "https://gitpkg.now.sh/supabase/tus-node-server/packages/server/dist?build",
"@tus/file-store": "https://gitpkg.now.sh/supabase/tus-node-server/packages/file-store/dist?supabase/build-v2",
"@tus/s3-store": "https://gitpkg.now.sh/supabase/tus-node-server/packages/s3-store/dist?supabase/build-v2",
"@tus/server": "https://gitpkg.now.sh/supabase/tus-node-server/packages/server/dist?supabase/build-v2",
"agentkeepalive": "^4.2.1",
"async-retry": "^1.3.3",
"axios": "^0.27.2",
Expand All @@ -46,7 +46,6 @@
"crypto-js": "^4.2.0",
"dotenv": "^16.0.0",
"fastify": "^4.8.1",
"fastify-cors": "^6.1.0",
"fastify-metrics": "^10.2.0",
"fastify-plugin": "^4.0.0",
"fs-extra": "^10.0.1",
Expand Down
9 changes: 5 additions & 4 deletions src/database/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import { Client, ClientConfig } from 'pg'
import { migrate } from 'postgres-migrations'
import { getConfig } from '../config'
import { logger } from '../monitoring'

const { multitenantDatabaseUrl, databaseSSLRootCert } = getConfig()

/**
* Runs tenant migrations
*/
export async function runMigrations(): Promise<void> {
console.log('running migrations')
logger.info('running migrations')
let ssl: ClientConfig['ssl'] | undefined = undefined

if (databaseSSLRootCert) {
ssl = { ca: databaseSSLRootCert }
}
await connectAndMigrate(process.env.DATABASE_URL, './migrations/tenant', ssl)
console.log('finished migrations')
logger.info('finished migrations')
}

/**
* Runs multi-tenant migrations
*/
export async function runMultitenantMigrations(): Promise<void> {
console.log('running multitenant migrations')
logger.info('running multitenant migrations')
await connectAndMigrate(multitenantDatabaseUrl, './migrations/multitenant')
console.log('finished multitenant migrations')
logger.info('finished multitenant migrations')
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/database/pubsub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { PostgresPubSub } from '../pubsub'
import { getConfig } from '../config'

const { isMultitenant, databaseURL, multitenantDatabaseUrl } = getConfig()

const connectionString = isMultitenant ? (multitenantDatabaseUrl as string) : databaseURL
export const PubSub = new PostgresPubSub(connectionString)
17 changes: 4 additions & 13 deletions src/database/tenant.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import createSubscriber from 'pg-listen'
import { getConfig } from '../config'
import { decrypt, verifyJWT } from '../auth'
import { runMigrationsOnTenant } from './migrate'
import { knex } from './multitenant-db'
import { StorageBackendError } from '../storage'
import { JwtPayload } from 'jsonwebtoken'
import { PubSubAdapter } from '../pubsub'

interface TenantConfig {
anonKey: string
Expand All @@ -26,7 +26,7 @@ export interface Features {
}
}

const { multitenantDatabaseUrl, isMultitenant, serviceKey, jwtSecret } = getConfig()
const { isMultitenant, serviceKey, jwtSecret } = getConfig()

const tenantConfigCache = new Map<string, TenantConfig>()

Expand Down Expand Up @@ -197,19 +197,10 @@ const TENANTS_UPDATE_CHANNEL = 'tenants_update'
/**
* Keeps the in memory config cache up to date
*/
export async function listenForTenantUpdate(): Promise<void> {
const subscriber = createSubscriber({ connectionString: multitenantDatabaseUrl })

subscriber.notifications.on(TENANTS_UPDATE_CHANNEL, (tenantId) => {
export async function listenForTenantUpdate(pubSub: PubSubAdapter): Promise<void> {
await pubSub.subscribe(TENANTS_UPDATE_CHANNEL, (tenantId) => {
tenantConfigCache.delete(tenantId)
})

subscriber.events.on('error', (error) => {
console.error('Postgres notification subscription error:', error)
})

await subscriber.connect()
await subscriber.listenTo(TENANTS_UPDATE_CHANNEL)
}

async function cacheTenantConfigAndRunMigrations(
Expand Down
12 changes: 3 additions & 9 deletions src/http/routes/tus/file-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@ import { Upload } from '@tus/server'
import fsExtra from 'fs-extra'
import path from 'path'
import { FileBackend } from '../../../storage/backend'

type Store = {
get(key: string): Upload | undefined
set(key: string, value: Upload): void
delete(key: string): void
all: Record<string, Upload>
}
import { Configstore } from '@tus/file-store/configstores'

type FileStoreOptions = {
directory: string
configstore?: Store
configstore?: Configstore
expirationPeriodInMilliseconds?: number
}

Expand All @@ -33,7 +27,7 @@ export class FileStore extends TusFileStore {
cacheControl: file.metadata?.cacheControl || '',
contentType: file.metadata?.contentType || '',
})
this.configstore.set(file.id, file)
await this.configstore.set(file.id, file)
return file
}
}
Loading

0 comments on commit 8a4fda7

Please sign in to comment.