Files
speckle-server/packages/server/modules/core/rest/diffUpload.ts
T
Gergő Jedlicska 2e3e8bed1d gergo/sendReceiveMultiRegion (#3461)
* feat(webhooks): multi region webhook resolver

* feat(webhooks): multi region webhook cleanup

* fix(webhooks): DI fixes

* feat(activitystream): region aware save activity

* feat(accessrequests): multi region

* feat(cli): allow multi region project and commit download

* feat(postgres): make docker postgres 0 day multi region ready

* feat(cli): allow multi region project and commit download properly

* fix(cross-server-sync): di fix

* feat(activitystream): non region aware activities, they are not project data

* fix(webhooks): triggers need to be included

* feat(stream/projectCreate): activity save is not needed any more, its all event based

* feat(multiRegion): get all registered db clients

* fix(regions): test equal in any order

* feat(objects): use project region for send and receive
2024-11-08 14:46:36 +01:00

61 lines
1.9 KiB
TypeScript

import zlib from 'zlib'
import { corsMiddleware } from '@/modules/core/configs/cors'
import { chunk } from 'lodash'
import type { Application } from 'express'
import { hasObjectsFactory } from '@/modules/core/repositories/objects'
import { validatePermissionsWriteStreamFactory } from '@/modules/core/services/streams/auth'
import { authorizeResolver, validateScopes } from '@/modules/shared'
import { getProjectDbClient } from '@/modules/multiregion/dbSelector'
export default (app: Application) => {
const validatePermissionsWriteStream = validatePermissionsWriteStreamFactory({
validateScopes,
authorizeResolver
})
app.options('/api/diff/:streamId', corsMiddleware())
app.post('/api/diff/:streamId', corsMiddleware(), async (req, res) => {
req.log = req.log.child({
userId: req.context.userId || '-',
streamId: req.params.streamId
})
const hasStreamAccess = await validatePermissionsWriteStream(
req.params.streamId,
req
)
if (!hasStreamAccess.result) {
return res.status(hasStreamAccess.status).end()
}
const projectDb = await getProjectDbClient({ projectId: req.params.streamId })
const hasObjects = hasObjectsFactory({ db: projectDb })
const objectList = JSON.parse(req.body.objects)
req.log.info({ objectCount: objectList.length }, 'Diffing {objectCount} objects.')
const chunkSize = 1000
const objectListChunks = chunk(objectList, chunkSize)
const mappedObjects = await Promise.all(
objectListChunks.map((objectListChunk) =>
hasObjects({
streamId: req.params.streamId,
objectIds: objectListChunk as string[]
})
)
)
const response = {}
Object.assign(response, ...mappedObjects)
res.writeHead(200, {
'Content-Encoding': 'gzip',
'Content-Type': 'application/json'
})
const gzip = zlib.createGzip()
gzip.write(JSON.stringify(response))
gzip.flush()
gzip.end()
gzip.pipe(res)
})
}