Files
speckle-server/packages/server/modules/core/rest/diffUpload.ts
T
Kristaps Fabians Geikins bde148f286 chore(server): migrating fully to ESM (#5042)
* wip

* some extra fixes

* stuff kinda works?

* need to figure out mocks

* need to figure out mocks

* fix db listener

* gqlgen fix

* minor gqlgen watch adjustment

* lint fixes

* delete old codegen file

* converting migrations to ESM

* getModuleDIrectory

* vitest sort of works

* added back ts-vitest

* resolve gql double load

* fixing test timeout configs

* TSC lint fix

* fix automate tests

* moar debugging

* debugging

* more debugging

* codegen update

* server works

* yargs migrated

* chore(server): getting rid of global mocks for Server ESM (#5046)

* got rid of email mock

* got rid of comment mocks

* got rid of multi region mocks

* got rid of stripe mock

* admin override mock updated

* removed final mock

* fixing import.meta.resolve calls

* another import.meta.resolve fix

* added requested test

* nyc ESM fix

* removed unneeded deps + linting

* yarn lock forgot to commit

* tryna fix flakyness

* email capture util fix

* sendEmail fix

* fix TSX check

* sender transporter fix + CR comments

* merge main fix

* test fixx

* circleci fix

* gqlgen bigint fix

* error formatter fix

* more error formatting improvements

* esmloader added to Dockerfile

* more dockerfile fixes

* bg jobs fix
2025-07-14 10:26:19 +03:00

71 lines
2.4 KiB
TypeScript

import zlib from 'zlib'
import { corsMiddlewareFactory } from '@/modules/core/configs/cors'
import { chunk } from 'lodash-es'
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/utils/dbSelector'
import { UserInputError } from '@/modules/core/errors/userinput'
import { ensureError } from '@speckle/shared'
export default (app: Application) => {
const validatePermissionsWriteStream = validatePermissionsWriteStreamFactory({
validateScopes,
authorizeResolver
})
app.options('/api/diff/:streamId', corsMiddlewareFactory())
app.post('/api/diff/:streamId', corsMiddlewareFactory(), 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 })
let objectList: string[]
try {
objectList = JSON.parse(req.body.objects)
} catch (err) {
throw new UserInputError(
'Invalid body. Please provide a JSON object containing the property "objects" of type string. The value must be a JSON string representation of an array of object IDs.',
ensureError(err, 'Unknown JSON parsing issue')
)
}
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)
})
}