Skip to content

Commit 02d63cb

Browse files
authored
fix: optional queue + error code (#225)
1 parent 9d5c09d commit 02d63cb

File tree

10 files changed

+42
-10
lines changed

10 files changed

+42
-10
lines changed

.env.sample

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ IMGPROXY_URL=http://localhost:50020
2727

2828
# specify the extra headers will be persisted and passed into Postgrest client, for instance, "x-foo,x-bar"
2929
POSTGREST_FORWARD_HEADERS=
30-
WEBHOOK_URL=
30+
WEBHOOK_URL=
31+
ENABLE_QUEUE_EVENTS=false

src/config.ts

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ type StorageConfigType = {
2828
logflareEnabled?: boolean
2929
logflareApiKey?: string
3030
logflareSourceToken?: string
31+
enableQueueEvents: boolean
32+
pgQueueConnectionURL?: string
3133
webhookURL?: string
3234
webhookApiKey?: string
3335
disableImageTransformation: boolean
@@ -96,6 +98,8 @@ export function getConfig(): StorageConfigType {
9698
logflareEnabled: getOptionalConfigFromEnv('LOGFLARE_ENABLED') === 'true',
9799
logflareApiKey: getOptionalConfigFromEnv('LOGFLARE_API_KEY'),
98100
logflareSourceToken: getOptionalConfigFromEnv('LOGFLARE_SOURCE_TOKEN'),
101+
enableQueueEvents: getOptionalConfigFromEnv('ENABLE_QUEUE_EVENTS') === 'true',
102+
pgQueueConnectionURL: getOptionalConfigFromEnv('PG_QUEUE_CONNECTION_URL'),
99103
webhookURL: getOptionalConfigFromEnv('WEBHOOK_URL'),
100104
webhookApiKey: getOptionalConfigFromEnv('WEBHOOK_API_KEY'),
101105
disableImageTransformation: getOptionalConfigFromEnv('DISABLE_IMAGE_TRANSFORMATION') === 'true',

src/http/error-handler.ts

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export const setErrorHandler = (app: FastifyInstance) => {
1414
// it will be logged in the request log plugin
1515
reply.executionError = error
1616

17+
request.log.error({ error }, `request error | ${request.id}`)
18+
1719
if (process.env.NODE_ENV !== 'production') {
1820
console.error(error)
1921
}

src/queue/events/base-event.ts

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getPostgrestClient } from '../../database'
55
import { Storage } from '../../storage'
66
import { Database } from '../../storage/database'
77
import { createStorageBackend } from '../../storage/backend'
8+
import { getConfig } from '../../config'
89

910
export interface BasePayload {
1011
$version: string
@@ -16,6 +17,8 @@ export interface BasePayload {
1617

1718
export type StaticThis<T> = { new (...args: any): T }
1819

20+
const { enableQueueEvents } = getConfig()
21+
1922
export abstract class BaseEvent<T extends Omit<BasePayload, '$version'>> {
2023
public static readonly version: string = 'v1'
2124

@@ -71,6 +74,10 @@ export abstract class BaseEvent<T extends Omit<BasePayload, '$version'>> {
7174
}
7275

7376
send() {
77+
if (!enableQueueEvents) {
78+
return
79+
}
80+
7481
const constructor = this.constructor as typeof BaseEvent
7582

7683
return Queue.getInstance().send({

src/queue/queue.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ export abstract class Queue {
1717
return Queue.pgBoss
1818
}
1919

20-
const { isMultitenant, multitenantDatabaseUrl } = getConfig()
20+
const { isMultitenant, multitenantDatabaseUrl, pgQueueConnectionURL } = getConfig()
2121

22-
let url = process.env.DATABASE_URL
22+
let url = pgQueueConnectionURL ?? process.env.DATABASE_URL
2323

2424
if (isMultitenant) {
25-
url = multitenantDatabaseUrl
25+
url = pgQueueConnectionURL ?? multitenantDatabaseUrl
2626
}
2727
Queue.pgBoss = new PgBoss({
2828
connectionString: url,

src/server.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@ import { Queue } from './queue'
1212
const exposeDocs = true
1313

1414
;(async () => {
15-
const { isMultitenant, requestIdHeader, adminRequestIdHeader, adminPort, port, host } =
16-
getConfig()
15+
const {
16+
isMultitenant,
17+
requestIdHeader,
18+
adminRequestIdHeader,
19+
adminPort,
20+
port,
21+
host,
22+
enableQueueEvents,
23+
} = getConfig()
24+
1725
if (isMultitenant) {
1826
await runMultitenantMigrations()
1927
await listenForTenantUpdate()
@@ -34,7 +42,9 @@ const exposeDocs = true
3442
await runMigrations()
3543
}
3644

37-
await Queue.init()
45+
if (enableQueueEvents) {
46+
await Queue.init()
47+
}
3848

3949
const app: FastifyInstance<Server, IncomingMessage, ServerResponse> = build({
4050
logger,

src/storage/errors.ts

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ export class DatabaseError extends Error implements RenderableError {
4949
message = relationNotPresent
5050
? 'The parent resource is not found'
5151
: 'The resource already exists'
52+
} else if (responseStatus === 403) {
53+
code = '403'
54+
} else {
55+
code = '500'
56+
type = 'Database Error'
5257
}
5358

5459
return {

src/test/object.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ describe('testing POST object via multipart upload', () => {
220220
expect(S3Backend.prototype.uploadObject).not.toHaveBeenCalled()
221221
expect(response.body).toBe(
222222
JSON.stringify({
223-
statusCode: '42501',
223+
statusCode: '403',
224224
error: '',
225225
message: 'new row violates row-level security policy for table "objects"',
226226
})
@@ -431,7 +431,7 @@ describe('testing POST object via binary upload', () => {
431431
expect(S3Backend.prototype.uploadObject).not.toHaveBeenCalled()
432432
expect(response.body).toBe(
433433
JSON.stringify({
434-
statusCode: '42501',
434+
statusCode: '403',
435435
error: '',
436436
message: 'new row violates row-level security policy for table "objects"',
437437
})

src/test/webhooks.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
process.env.ENABLE_QUEUE_EVENTS = 'true'
2+
13
import { mockQueue, useMockObject } from './common'
24
import FormData from 'form-data'
5+
36
import fs from 'fs'
47
import app from '../app'
58
import { getConfig } from '../config'

src/test/x-forwarded-host.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('with X-Forwarded-Host header', () => {
5959
'x-forwarded-host': 'abcdefghijklmnopqrst.supabase.co',
6060
},
6161
})
62-
expect(response.statusCode).toBe(400)
62+
expect(response.statusCode).toBe(500)
6363
const responseJSON = JSON.parse(response.body)
6464
expect(responseJSON.message).toContain('http://abcdefghijklmnopqrst.supabase.co/rest/v1')
6565
await adminApp.inject({

0 commit comments

Comments
 (0)