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 Tus v1 #401

Merged
merged 5 commits into from
Dec 29, 2023
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
2,832 changes: 1,686 additions & 1,146 deletions package-lock.json

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"test:dummy-data": "tsx -r dotenv/config ./src/test/db/import-dummy-data.ts",
"test": "npm run infra:restart && npm run test:dummy-data && jest --runInBand --forceExit",
"test:coverage": "npm run infra:restart && npm run test:dummy-data && jest --runInBand --coverage --forceExit",
"prettier:check": "prettier -c src/**",
"prettier:check": "prettier -v && prettier -c src/**",
"format": "prettier -c --write src/**",
"eslint:check": "eslint 'src/**'",
"infra:stop": "docker-compose --project-directory . -f src/test/db/docker-compose.yml down --remove-orphans",
Expand All @@ -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": "1.1.0",
"@tus/s3-store": "1.2.0",
"@tus/server": "1.2.0",
"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 Expand Up @@ -78,15 +77,15 @@
"@typescript-eslint/parser": "^5.12.1",
"babel-jest": "^29.2.2",
"eslint": "^8.9.0",
"eslint-config-prettier": "^8.4.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-config-prettier": "^8.10.0",
"eslint-plugin-prettier": "^4.2.1",
"form-data": "^4.0.0",
"jest": "^29.2.2",
"js-yaml": "^4.1.0",
"json-schema-to-ts": "^2.5.4",
"mustache": "^4.2.0",
"pino-pretty": "^8.1.0",
"prettier": "^2.5.1",
"prettier": "^2.8.8",
"ts-jest": "^29.0.3",
"ts-node-dev": "^1.1.8",
"tsx": "^3.13.0",
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 @@
}
}

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

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

Expand All @@ -46,7 +46,7 @@
try {
await runMigrationsOnTenant(databaseUrl)
console.log(`${tenantId} migrations ran successfully`)
} catch (error: any) {

Check warning on line 49 in src/database/tenant.ts

View workflow job for this annotation

GitHub Actions / Test / OS ubuntu-20.04 / Node 18

Unexpected any. Specify a different type
if (logOnError) {
console.error(`${tenantId} migration error:`, error.message)
return
Expand Down Expand Up @@ -197,19 +197,10 @@
/**
* 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'

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
Loading