Skip to content

Commit

Permalink
fix(/api/diff): chunk large lists of objects (#1737)
Browse files Browse the repository at this point in the history
* Revert "fix(/api/diff): return 400 if greater than max objects (#1736)"
This reverts commit 783e785.

* fix(/api/diff): chunk long object lists to remain within maximum length
  • Loading branch information
iainsproat authored Jul 29, 2023
1 parent 783e785 commit a7ca2f4
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions packages/server/modules/core/rest/diffUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { validatePermissionsWriteStream } = require('./authUtils')

const { hasObjects } = require('../services/objects')

const MAXIMUM_OBJECTS = 65536
const { chunk } = require('lodash')

module.exports = (app) => {
app.options('/api/diff/:streamId', corsMiddleware())
Expand All @@ -25,19 +25,22 @@ module.exports = (app) => {
}

const objectList = JSON.parse(req.body.objects)
if (objectList.length > MAXIMUM_OBJECTS) {
req.log.warn(
`User ${req.context.userId} tried to diff ${objectList.length} objects, which is greater than the maximum of ${MAXIMUM_OBJECTS}.`
)
return res.status(400).end(`Too many objects. Maximum ${MAXIMUM_OBJECTS}.`)
}

req.log.info(`Diffing ${objectList.length} objects.`)

const response = await hasObjects({
streamId: req.params.streamId,
objectIds: objectList
})
const chunkSize = 1000
const objectListChunks = chunk(objectList, chunkSize)
const mappedObjects = await Promise.all(
objectListChunks.map((objectListChunk) =>
hasObjects({
streamId: req.params.streamId,
objectIds: objectListChunk
})
)
)
const response = {}
Object.assign(response, ...mappedObjects)

req.log.debug(response)
res.writeHead(200, {
'Content-Encoding': 'gzip',
Expand Down

0 comments on commit a7ca2f4

Please sign in to comment.