Skip to content

Commit

Permalink
fix: refactor internal packages
Browse files Browse the repository at this point in the history
  • Loading branch information
fenos committed Jun 25, 2024
1 parent 96e667f commit 87d26fa
Show file tree
Hide file tree
Showing 131 changed files with 1,355 additions and 616 deletions.
17 changes: 17 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { build } = require('esbuild')

build({
entryPoints: ['./src/**/*.ts'],
bundle: false,
outdir: 'dist',
platform: 'node',
format: 'cjs',
target: 'node20',
sourcemap: true,
tsconfig: 'tsconfig.json',
loader: { '.ts': 'ts' },
}).catch((e) => {
console.error(e)
process.exit(1)
})
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ module.exports = {
transform: {
'^.+\\.(t|j)sx?$': 'ts-jest',
},
moduleNameMapper: {
'^@storage/(.*)$': '<rootDir>/src/storage/$1',
'^@internal/(.*)$': '<rootDir>/src/internal/$1',
},
setupFilesAfterEnv: ['<rootDir>/jest-setup.ts'],
testEnvironment: 'node',
testPathIgnorePatterns: ['node_modules', 'dist'],
Expand Down
1,164 changes: 913 additions & 251 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"dev": "tsx watch ./src/server.ts | pino-pretty",
"build": "tsc -p tsconfig.json",
"build": "node ./build.js && resolve-tspaths",
"start": "NODE_ENV=production node dist/server.js",
"migration:run": "tsx ./src/scripts/migrate-call.ts",
"docs:export": "tsx ./src/scripts/export-docs.ts",
Expand Down Expand Up @@ -95,6 +95,7 @@
"@typescript-eslint/eslint-plugin": "^5.12.1",
"@typescript-eslint/parser": "^5.12.1",
"babel-jest": "^29.2.2",
"esbuild": "0.21.5",
"eslint": "^8.9.0",
"eslint-config-prettier": "^8.10.0",
"eslint-plugin-prettier": "^4.2.1",
Expand All @@ -105,6 +106,7 @@
"mustache": "^4.2.0",
"pino-pretty": "^8.1.0",
"prettier": "^2.8.8",
"resolve-tspaths": "^0.8.19",
"stream-buffers": "^3.0.2",
"ts-jest": "^29.0.3",
"ts-node-dev": "^1.1.8",
Expand Down
3 changes: 1 addition & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fastify, { FastifyInstance, FastifyServerOptions } from 'fastify'
import fastifyMultipart from '@fastify/multipart'
import fastifySwagger from '@fastify/swagger'
import fastifySwaggerUi from '@fastify/swagger-ui'
import { routes, schemas, plugins, setErrorHandler } from './http'
Expand Down Expand Up @@ -30,7 +29,7 @@ const build = (opts: buildOpts = {}): FastifyInstance => {
info: {
title: 'Supabase Storage API',
description: 'API documentation for Supabase Storage',
version: '0.0.1',
version: version,
},
tags: [
{ name: 'object', description: 'Object end-points' },
Expand Down
2 changes: 1 addition & 1 deletion src/http/error-handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FastifyInstance } from 'fastify'
import { ErrorCode, isRenderableError } from '../storage'
import { FastifyError } from '@fastify/error'
import { DatabaseError } from 'pg'
import { ErrorCode, isRenderableError } from '@internal/errors'

/**
* The global error handler for all the uncaught exceptions within a request.
Expand Down
10 changes: 5 additions & 5 deletions src/http/plugins/db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fastifyPlugin from 'fastify-plugin'
import { getConfig, MultitenantMigrationStrategy } from '../../config'
import {
areMigrationsUpToDate,
getServiceKeyUser,
Expand All @@ -9,11 +10,10 @@ import {
getPostgresConnection,
progressiveMigrations,
runMigrationsOnTenant,
} from '../../database'
import { verifyJWT } from '../../auth'
import { logSchema } from '../../monitoring'
import { getConfig, MultitenantMigrationStrategy } from '../../config'
import { createMutexByKey } from '../../concurrency'
} from '@internal/database'
import { verifyJWT } from '@internal/auth'
import { logSchema } from '@internal/monitoring'
import { createMutexByKey } from '@internal/concurrency'

declare module 'fastify' {
interface FastifyRequest {
Expand Down
7 changes: 4 additions & 3 deletions src/http/plugins/jwt.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import fastifyPlugin from 'fastify-plugin'
import { verifyJWT } from '../../auth'
import { getJwtSecret } from '../../database'
import { JwtPayload } from 'jsonwebtoken'
import { ERRORS } from '../../storage'

import { verifyJWT } from '@internal/auth'
import { getJwtSecret } from '@internal/database'
import { ERRORS } from '@internal/errors'

declare module 'fastify' {
interface FastifyRequest {
Expand Down
2 changes: 1 addition & 1 deletion src/http/plugins/log-request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fastifyPlugin from 'fastify-plugin'
import { logSchema, redactQueryParamFromRequest } from '../../monitoring'
import { logSchema, redactQueryParamFromRequest } from '@internal/monitoring'
import { trace } from '@opentelemetry/api'

interface RequestLoggerOptions {
Expand Down
2 changes: 1 addition & 1 deletion src/http/plugins/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fastifyPlugin from 'fastify-plugin'
import { MetricsRegistrar } from '../../monitoring/metrics'
import { MetricsRegistrar } from '@internal/monitoring/metrics'
import fastifyMetrics from 'fastify-metrics'
import { getConfig } from '../../config'

Expand Down
9 changes: 5 additions & 4 deletions src/http/plugins/signature-v4.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { FastifyInstance, FastifyRequest } from 'fastify'
import fastifyPlugin from 'fastify-plugin'
import { getS3CredentialsByAccessKey, getTenantConfig } from '../../database'
import { ClientSignature, SignatureV4 } from '../../storage/protocols/s3'
import { ERRORS } from '../../storage'
import { signJWT, verifyJWT } from '../../auth'
import { getS3CredentialsByAccessKey, getTenantConfig } from '@internal/database'
import { ClientSignature, SignatureV4 } from '@storage/protocols/s3'
import { signJWT, verifyJWT } from '@internal/auth'
import { ERRORS } from '@internal/errors'

import { getConfig } from '../../config'

const {
Expand Down
6 changes: 3 additions & 3 deletions src/http/plugins/storage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fastifyPlugin from 'fastify-plugin'
import { StorageBackendAdapter, createStorageBackend } from '../../storage/backend'
import { Storage } from '../../storage'
import { StorageKnexDB } from '../../storage/database'
import { StorageBackendAdapter, createStorageBackend } from '@storage/backend'
import { Storage } from '@storage/storage'
import { StorageKnexDB } from '@storage/database'
import { getConfig } from '../../config'

declare module 'fastify' {
Expand Down
3 changes: 2 additions & 1 deletion src/http/plugins/tenant-feature.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fastifyPlugin from 'fastify-plugin'
import { Features, getFeatures } from '@internal/database'

import { getConfig } from '../../config'
import { Features, getFeatures } from '../../database/tenant'

/**
* Requires a specific feature to be enabled for a given tenant.
Expand Down
3 changes: 2 additions & 1 deletion src/http/plugins/tracing-mode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fastifyPlugin from 'fastify-plugin'
import { getTenantConfig } from '../../database'
import { getTenantConfig } from '@internal/database'

import { getConfig } from '../../config'

declare module 'fastify' {
Expand Down
File renamed without changes.
5 changes: 3 additions & 2 deletions src/http/routes/admin/migrations.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { FastifyInstance } from 'fastify'
import { Queue } from '@internal/queue'
import { multitenantKnex, runMigrationsOnAllTenants } from '@internal/database'
import { RunMigrationsOnTenants } from '@storage/events'
import apiKey from '../../plugins/apikey'
import { Queue, RunMigrationsOnTenants } from '../../../queue'
import { getConfig } from '../../../config'
import { multitenantKnex, runMigrationsOnAllTenants } from '../../../database'

const { pgQueueEnable } = getConfig()

Expand Down
6 changes: 5 additions & 1 deletion src/http/routes/admin/s3.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { FastifyInstance, RequestGenericInterface } from 'fastify'
import apiKey from '../../plugins/apikey'
import { createS3Credentials, deleteS3Credential, listS3Credentials } from '../../../database'
import {
createS3Credentials,
deleteS3Credential,
listS3Credentials,
} from '../../../internal/database'
import { FromSchema } from 'json-schema-to-ts'

const createCredentialsSchema = {
Expand Down
4 changes: 2 additions & 2 deletions src/http/routes/admin/tenants.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { FastifyInstance, RequestGenericInterface } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import apiKey from '../../plugins/apikey'
import { decrypt, encrypt } from '../../../auth'
import { decrypt, encrypt } from '../../../internal/auth'
import {
deleteTenantConfig,
TenantMigrationStatus,
multitenantKnex,
lastMigrationName,
runMigrationsOnTenant,
progressiveMigrations,
} from '../../../database'
} from '../../../internal/database'
import { dbSuperUser, storage } from '../../plugins'

const patchSchema = {
Expand Down
4 changes: 2 additions & 2 deletions src/http/routes/bucket/createBucket.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FastifyInstance, FastifyRequest } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { createDefaultSchema } from '../../generic-routes'
import { AuthenticatedRequest } from '../../request'
import { createDefaultSchema } from '../../routes-helper'
import { AuthenticatedRequest } from '../../types'
import { ROUTE_OPERATIONS } from '../operations'

const createBucketBodySchema = {
Expand Down
4 changes: 2 additions & 2 deletions src/http/routes/bucket/deleteBucket.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FastifyInstance } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { createDefaultSchema, createResponse } from '../../generic-routes'
import { AuthenticatedRequest } from '../../request'
import { createDefaultSchema, createResponse } from '../../routes-helper'
import { AuthenticatedRequest } from '../../types'
import { ROUTE_OPERATIONS } from '../operations'

const deleteBucketParamsSchema = {
Expand Down
4 changes: 2 additions & 2 deletions src/http/routes/bucket/emptyBucket.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FastifyInstance } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { createDefaultSchema, createResponse } from '../../generic-routes'
import { AuthenticatedRequest } from '../../request'
import { createDefaultSchema, createResponse } from '../../routes-helper'
import { AuthenticatedRequest } from '../../types'
import { ROUTE_OPERATIONS } from '../operations'

const emptyBucketParamsSchema = {
Expand Down
6 changes: 3 additions & 3 deletions src/http/routes/bucket/getAllBuckets.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FastifyInstance } from 'fastify'
import { createDefaultSchema } from '../../generic-routes'
import { AuthenticatedRequest } from '../../request'
import { bucketSchema } from '../../../storage/schemas'
import { createDefaultSchema } from '../../routes-helper'
import { AuthenticatedRequest } from '../../types'
import { bucketSchema } from '@storage/schemas'
import { ROUTE_OPERATIONS } from '../operations'

const successResponseSchema = {
Expand Down
6 changes: 3 additions & 3 deletions src/http/routes/bucket/getBucket.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { FastifyInstance } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { createDefaultSchema } from '../../generic-routes'
import { bucketSchema } from '../../../storage/schemas'
import { AuthenticatedRequest } from '../../request'
import { createDefaultSchema } from '../../routes-helper'
import { bucketSchema } from '@storage/schemas'
import { AuthenticatedRequest } from '../../types'
import { ROUTE_OPERATIONS } from '../operations'

const getBucketParamsSchema = {
Expand Down
4 changes: 2 additions & 2 deletions src/http/routes/bucket/updateBucket.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FastifyInstance } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { createDefaultSchema, createResponse } from '../../generic-routes'
import { AuthenticatedRequest } from '../../request'
import { createDefaultSchema, createResponse } from '../../routes-helper'
import { AuthenticatedRequest } from '../../types'
import { ROUTE_OPERATIONS } from '../operations'

const updateBucketBodySchema = {
Expand Down
4 changes: 2 additions & 2 deletions src/http/routes/object/copyObject.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FastifyInstance, FastifyRequest } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { createDefaultSchema } from '../../generic-routes'
import { AuthenticatedRequest } from '../../request'
import { createDefaultSchema } from '../../routes-helper'
import { AuthenticatedRequest } from '../../types'
import { ROUTE_OPERATIONS } from '../operations'

const copyRequestBodySchema = {
Expand Down
2 changes: 1 addition & 1 deletion src/http/routes/object/createObject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FastifyInstance, RequestGenericInterface } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { createDefaultSchema } from '../../generic-routes'
import { createDefaultSchema } from '../../routes-helper'
import { ROUTE_OPERATIONS } from '../operations'
import fastifyMultipart from '@fastify/multipart'

Expand Down
4 changes: 2 additions & 2 deletions src/http/routes/object/deleteObject.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FastifyInstance } from 'fastify'
import { FromSchema, JSONSchema } from 'json-schema-to-ts'
import { createDefaultSchema, createResponse } from '../../generic-routes'
import { AuthenticatedRequest } from '../../request'
import { createDefaultSchema, createResponse } from '../../routes-helper'
import { AuthenticatedRequest } from '../../types'
import { ROUTE_OPERATIONS } from '../operations'

const deleteObjectParamsSchema = {
Expand Down
6 changes: 3 additions & 3 deletions src/http/routes/object/deleteObjects.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { FastifyInstance, FastifyRequest } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { createDefaultSchema } from '../../generic-routes'
import { AuthenticatedRequest } from '../../request'
import { objectSchema } from '../../../storage/schemas/object'
import { createDefaultSchema } from '../../routes-helper'
import { AuthenticatedRequest } from '../../types'
import { objectSchema } from '@storage/schemas/object'
import { ROUTE_OPERATIONS } from '../operations'
const deleteObjectsParamsSchema = {
type: 'object',
Expand Down
2 changes: 1 addition & 1 deletion src/http/routes/object/getObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { IncomingMessage, Server, ServerResponse } from 'http'
import { getConfig } from '../../../config'
import { AuthenticatedRangeRequest } from '../../request'
import { AuthenticatedRangeRequest } from '../../types'
import { ROUTE_OPERATIONS } from '../operations'

const { storageS3Bucket } = getConfig()
Expand Down
4 changes: 2 additions & 2 deletions src/http/routes/object/getObjectInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { IncomingMessage, Server, ServerResponse } from 'http'
import { getConfig } from '../../../config'
import { AuthenticatedRangeRequest } from '../../request'
import { Obj } from '../../../storage/schemas'
import { AuthenticatedRangeRequest } from '../../types'
import { Obj } from '@storage/schemas'
import { ROUTE_OPERATIONS } from '../operations'

const { storageS3Bucket } = getConfig()
Expand Down
6 changes: 3 additions & 3 deletions src/http/routes/object/getSignedObject.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { FastifyInstance } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { getConfig } from '../../../config'
import { SignedToken, verifyJWT } from '../../../auth'
import { ERRORS } from '../../../storage'
import { getJwtSecret } from '../../../database/tenant'
import { SignedToken, verifyJWT } from '../../../internal/auth'
import { getJwtSecret } from '../../../internal/database'
import { ROUTE_OPERATIONS } from '../operations'
import { ERRORS } from '../../../internal/errors'

const { storageS3Bucket } = getConfig()

Expand Down
8 changes: 4 additions & 4 deletions src/http/routes/object/getSignedURL.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { FastifyInstance } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { createDefaultSchema } from '../../generic-routes'
import { AuthenticatedRequest } from '../../request'
import { ImageRenderer } from '../../../storage/renderer'
import { createDefaultSchema } from '../../routes-helper'
import { AuthenticatedRequest } from '../../types'
import { ImageRenderer } from '@storage/renderer'
import { transformationOptionsSchema } from '../../schemas/transformations'
import { isImageTransformationEnabled } from '../../../storage/limits'
import { isImageTransformationEnabled } from '@storage/limits'
import { ROUTE_OPERATIONS } from '../operations'

const getSignedURLParamsSchema = {
Expand Down
4 changes: 2 additions & 2 deletions src/http/routes/object/getSignedURLs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FastifyInstance, FastifyRequest } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { createDefaultSchema } from '../../generic-routes'
import { AuthenticatedRequest } from '../../request'
import { createDefaultSchema } from '../../routes-helper'
import { AuthenticatedRequest } from '../../types'
import { ROUTE_OPERATIONS } from '../operations'

const getSignedURLsParamsSchema = {
Expand Down
4 changes: 2 additions & 2 deletions src/http/routes/object/getSignedUploadURL.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FastifyInstance } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { createDefaultSchema } from '../../generic-routes'
import { AuthenticatedRequest } from '../../request'
import { createDefaultSchema } from '../../routes-helper'
import { AuthenticatedRequest } from '../../types'
import { getConfig } from '../../../config'
import { ROUTE_OPERATIONS } from '../operations'

Expand Down
6 changes: 3 additions & 3 deletions src/http/routes/object/listObjects.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { FastifyInstance } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { createDefaultSchema } from '../../generic-routes'
import { AuthenticatedRequest } from '../../request'
import { objectSchema } from '../../../storage/schemas'
import { createDefaultSchema } from '../../routes-helper'
import { AuthenticatedRequest } from '../../types'
import { objectSchema } from '@storage/schemas'
import { ROUTE_OPERATIONS } from '../operations'

const searchRequestParamsSchema = {
Expand Down
Loading

0 comments on commit 87d26fa

Please sign in to comment.