• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

supabase / storage / 12886525928

21 Jan 2025 12:03PM UTC coverage: 77.302% (-0.7%) from 78.032%
12886525928

push

github

web-flow
feat: reconcile orphan objects from admin endpoint (#606)

1278 of 1809 branches covered (70.65%)

Branch coverage included in aggregate %.

912 of 1340 new or added lines in 26 files covered. (68.06%)

1 existing line in 1 file now uncovered.

15175 of 19475 relevant lines covered (77.92%)

159.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

47.12
/src/http/routes/admin/objects.ts
1
import { FastifyInstance, RequestGenericInterface } from 'fastify'
1✔
2
import apiKey from '../../plugins/apikey'
1✔
3
import { dbSuperUser, storage } from '../../plugins'
1✔
4
import { ObjectScanner } from '@storage/scanner/scanner'
1✔
5
import { FastifyReply } from 'fastify/types/reply'
1✔
6

1✔
7
const listOrphanedObjects = {
1✔
8
  description: 'List Orphaned Objects',
1✔
9
  params: {
1✔
10
    type: 'object',
1✔
11
    properties: {
1✔
12
      tenantId: { type: 'string' },
1✔
13
      bucketId: { type: 'string' },
1✔
14
    },
1✔
15
    required: ['tenantId', 'bucketId'],
1✔
16
  },
1✔
17
  query: {
1✔
18
    type: 'object',
1✔
19
    properties: {
1✔
20
      before: { type: 'string' },
1✔
21
      keepTmpTable: { type: 'boolean' },
1✔
22
    },
1✔
23
  },
1✔
24
} as const
1✔
25

1✔
26
const syncOrphanedObjects = {
1✔
27
  description: 'Sync Orphaned Objects',
1✔
28
  params: {
1✔
29
    type: 'object',
1✔
30
    properties: {
1✔
31
      tenantId: { type: 'string' },
1✔
32
      bucketId: { type: 'string' },
1✔
33
    },
1✔
34
    required: ['tenantId', 'bucketId'],
1✔
35
  },
1✔
36
  body: {
1✔
37
    type: 'object',
1✔
38
    properties: {
1✔
39
      deleteDbKeys: { type: 'boolean' },
1✔
40
      deleteS3Keys: { type: 'boolean' },
1✔
41
      tmpTable: { type: 'string' },
1✔
42
    },
1✔
43
  },
1✔
44
  optional: ['deleteDbKeys', 'deleteS3Keys'],
1✔
45
} as const
1✔
46

1✔
47
interface ListOrphanObjectsRequest extends RequestGenericInterface {
1✔
48
  Params: {
1✔
49
    tenantId: string
1✔
50
    bucketId: string
1✔
51
  }
1✔
52
  Querystring: {
1✔
53
    before?: string
1✔
54
    keepTmpTable?: boolean
1✔
55
  }
1✔
56
}
1✔
57

1✔
58
interface SyncOrphanObjectsRequest extends RequestGenericInterface {
1✔
59
  Params: {
1✔
60
    tenantId: string
1✔
61
    bucketId: string
1✔
62
  }
1✔
63
  Body: {
1✔
64
    deleteDbKeys?: boolean
1✔
65
    deleteS3Keys?: boolean
1✔
66
    before?: string
1✔
67
    tmpTable?: string
1✔
68
    keepTmpTable?: boolean
1✔
69
  }
1✔
70
}
1✔
71

1✔
72
export default async function routes(fastify: FastifyInstance) {
1✔
73
  fastify.register(apiKey)
1✔
74
  fastify.register(dbSuperUser, {
1✔
75
    disableHostCheck: true,
1✔
76
    maxConnections: 5,
1✔
77
  })
1✔
78
  fastify.register(storage)
1✔
79

1✔
80
  fastify.get<ListOrphanObjectsRequest>(
1✔
81
    '/:tenantId/buckets/:bucketId/orphan-objects',
1✔
82
    {
1✔
83
      schema: listOrphanedObjects,
1✔
84
    },
1✔
85
    async (req, reply) => {
1✔
NEW
86
      const bucket = req.params.bucketId
×
NEW
87
      let before = req.query.before ? new Date(req.query.before as string) : undefined
×
NEW
88

×
NEW
89
      if (before && isNaN(before.getTime())) {
×
NEW
90
        return reply.status(400).send({
×
NEW
91
          error: 'Invalid date format',
×
NEW
92
        })
×
NEW
93
      }
×
NEW
94
      if (!before) {
×
NEW
95
        before = new Date()
×
NEW
96
        before.setHours(before.getHours() - 1)
×
NEW
97
      }
×
NEW
98

×
NEW
99
      const scanner = new ObjectScanner(req.storage)
×
NEW
100
      const orphanObjects = scanner.listOrphaned(bucket, {
×
NEW
101
        signal: req.signals.disconnect.signal,
×
NEW
102
        before: before,
×
NEW
103
        keepTmpTable: Boolean(req.query.keepTmpTable),
×
NEW
104
      })
×
NEW
105

×
NEW
106
      reply.header('Content-Type', 'application/json; charset=utf-8')
×
NEW
107

×
NEW
108
      // Do not let the connection time out, periodically send
×
NEW
109
      // a ping message to keep the connection alive
×
NEW
110
      const respPing = ping(reply)
×
NEW
111

×
NEW
112
      try {
×
NEW
113
        for await (const result of orphanObjects) {
×
NEW
114
          if (result.value.length > 0) {
×
NEW
115
            respPing.update()
×
NEW
116
            reply.raw.write(
×
NEW
117
              JSON.stringify({
×
NEW
118
                ...result,
×
NEW
119
                event: 'data',
×
NEW
120
              })
×
NEW
121
            )
×
NEW
122
          }
×
NEW
123
        }
×
NEW
124
      } catch (e) {
×
NEW
125
        throw e
×
NEW
126
      } finally {
×
NEW
127
        respPing.clear()
×
NEW
128
        reply.raw.end()
×
NEW
129
      }
×
NEW
130
    }
×
131
  )
1✔
132

1✔
133
  fastify.delete<SyncOrphanObjectsRequest>(
1✔
134
    '/:tenantId/buckets/:bucketId/orphan-objects',
1✔
135
    {
1✔
136
      schema: syncOrphanedObjects,
1✔
137
    },
1✔
138
    async (req, reply) => {
1✔
NEW
139
      if (!req.body.deleteDbKeys && !req.body.deleteS3Keys) {
×
NEW
140
        return reply.status(400).send({
×
NEW
141
          error: 'At least one of deleteDbKeys or deleteS3Keys must be set to true',
×
NEW
142
        })
×
NEW
143
      }
×
NEW
144

×
NEW
145
      const bucket = `${req.params.bucketId}`
×
NEW
146
      let before = req.body.before ? new Date(req.body.before as string) : undefined
×
NEW
147

×
NEW
148
      if (!before) {
×
NEW
149
        before = new Date()
×
NEW
150
        before.setHours(before.getHours() - 1)
×
NEW
151
      }
×
NEW
152

×
NEW
153
      const respPing = ping(reply)
×
NEW
154

×
NEW
155
      try {
×
NEW
156
        const scanner = new ObjectScanner(req.storage)
×
NEW
157
        const result = scanner.deleteOrphans(bucket, {
×
NEW
158
          deleteDbKeys: req.body.deleteDbKeys,
×
NEW
159
          deleteS3Keys: req.body.deleteS3Keys,
×
NEW
160
          signal: req.signals.disconnect.signal,
×
NEW
161
          before,
×
NEW
162
          tmpTable: req.body.tmpTable,
×
NEW
163
        })
×
NEW
164

×
NEW
165
        for await (const deleted of result) {
×
NEW
166
          respPing.update()
×
NEW
167
          reply.raw.write(
×
NEW
168
            JSON.stringify({
×
NEW
169
              ...deleted,
×
NEW
170
              event: 'data',
×
NEW
171
            })
×
NEW
172
          )
×
NEW
173
        }
×
NEW
174
      } catch (e) {
×
NEW
175
        throw e
×
NEW
176
      } finally {
×
NEW
177
        respPing.clear()
×
NEW
178
        reply.raw.end()
×
NEW
179
      }
×
NEW
180
    }
×
181
  )
1✔
182
}
1✔
183

1✔
184
// Occasionally write a ping message to the response stream
1✔
NEW
185
function ping(reply: FastifyReply) {
×
NEW
186
  let lastSend = undefined as Date | undefined
×
NEW
187
  const clearPing = setInterval(() => {
×
NEW
188
    const fiveSecondsEarly = new Date()
×
NEW
189
    fiveSecondsEarly.setSeconds(fiveSecondsEarly.getSeconds() - 5)
×
NEW
190

×
NEW
191
    if (!lastSend || (lastSend && lastSend < fiveSecondsEarly)) {
×
NEW
192
      lastSend = new Date()
×
NEW
193
      reply.raw.write(
×
NEW
194
        JSON.stringify({
×
NEW
195
          event: 'ping',
×
NEW
196
        })
×
NEW
197
      )
×
NEW
198
    }
×
NEW
199
  }, 1000 * 10)
×
NEW
200

×
NEW
201
  return {
×
NEW
202
    clear: () => clearInterval(clearPing),
×
NEW
203
    update: () => {
×
NEW
204
      lastSend = new Date()
×
NEW
205
    },
×
NEW
206
  }
×
NEW
207
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc