96bed71022
* Improves error logging
- use pino error logger correctly by passing in error as first argument
* monitor deployment: Filter logging at INFO level and above
* Use structured logging to create parameters for monitoring results
* Add structured logging to obj fileimport service
* Fileimport service, fix and improve logging
- use child logger with additional context where possible
- select appropriate logging level
- fix duplicated context in log statement
* REST endpoints, add context to structured logging and remove same context from message
* Webhook service provides context to bound logger to properly use structured logging
- Pass bound logger containing context to `makeNetworkRequest`
- do not log url, as it may contain a secret (like Discord's webhook urls), instead log the webhook Id
- log error message when network call fails
* upload: make better use of structured logging when recording data
* pino-pretty when in dev or test mode
- pino-pretty configured to send to stderr
* LOG_PRETTY env var
* Silence structured logging during testing
- can not rely on determining the port number by reading from stdout/stderr
- instead we determine which port is free, then create our server on that port
- we then poll that port until the server is ready before commencing tests
* Allow puppeteer to install chromium
* Do not need to install chromium separately
102 lines
2.8 KiB
JavaScript
102 lines
2.8 KiB
JavaScript
'use strict'
|
|
const zlib = require('zlib')
|
|
const cors = require('cors')
|
|
|
|
const { validatePermissionsReadStream } = require('./authUtils')
|
|
|
|
const { getObject, getObjectChildrenStream } = require('../services/objects')
|
|
const { SpeckleObjectsStream } = require('./speckleObjectsStream')
|
|
const { pipeline, PassThrough } = require('stream')
|
|
const { logger } = require('@/logging/logging')
|
|
module.exports = (app) => {
|
|
app.options('/objects/:streamId/:objectId', cors())
|
|
|
|
app.get('/objects/:streamId/:objectId', cors(), async (req, res) => {
|
|
const boundLogger = logger.child({
|
|
userId: req.context.userId || '-',
|
|
streamId: req.params.streamId,
|
|
objectId: req.params.objectId
|
|
})
|
|
const hasStreamAccess = await validatePermissionsReadStream(
|
|
req.params.streamId,
|
|
req
|
|
)
|
|
if (!hasStreamAccess.result) {
|
|
return res.status(hasStreamAccess.status).end()
|
|
}
|
|
|
|
// Populate first object (the "commit")
|
|
const obj = await getObject({
|
|
streamId: req.params.streamId,
|
|
objectId: req.params.objectId
|
|
})
|
|
|
|
if (!obj) {
|
|
return res.status(404).send('Failed to find object.')
|
|
}
|
|
|
|
const simpleText = req.headers.accept === 'text/plain'
|
|
|
|
res.writeHead(200, {
|
|
'Content-Encoding': 'gzip',
|
|
'Content-Type': simpleText ? 'text/plain; charset=UTF-8' : 'application/json'
|
|
})
|
|
|
|
const dbStream = await getObjectChildrenStream({
|
|
streamId: req.params.streamId,
|
|
objectId: req.params.objectId
|
|
})
|
|
const speckleObjStream = new SpeckleObjectsStream(simpleText)
|
|
const gzipStream = zlib.createGzip()
|
|
|
|
speckleObjStream.write(obj)
|
|
|
|
pipeline(
|
|
dbStream,
|
|
speckleObjStream,
|
|
gzipStream,
|
|
new PassThrough({ highWaterMark: 16384 * 31 }),
|
|
res,
|
|
(err) => {
|
|
if (err) {
|
|
boundLogger.error(err, 'Error downloading object.')
|
|
} else {
|
|
boundLogger.info(
|
|
`Downloaded object (size: ${gzipStream.bytesWritten / 1000000} MB)`
|
|
)
|
|
}
|
|
}
|
|
)
|
|
})
|
|
|
|
app.options('/objects/:streamId/:objectId/single', cors())
|
|
app.get('/objects/:streamId/:objectId/single', cors(), async (req, res) => {
|
|
const boundLogger = logger.child({
|
|
userId: req.context.userId || '-',
|
|
streamId: req.params.streamId,
|
|
objectId: req.params.objectId
|
|
})
|
|
const hasStreamAccess = await validatePermissionsReadStream(
|
|
req.params.streamId,
|
|
req
|
|
)
|
|
if (!hasStreamAccess.result) {
|
|
return res.status(hasStreamAccess.status).end()
|
|
}
|
|
|
|
const obj = await getObject({
|
|
streamId: req.params.streamId,
|
|
objectId: req.params.objectId
|
|
})
|
|
|
|
if (!obj) {
|
|
boundLogger.warn('Failed to find object.')
|
|
return res.status(404).send('Failed to find object.')
|
|
}
|
|
|
|
boundLogger.info('Downloaded single object.')
|
|
|
|
res.send(obj.data)
|
|
})
|
|
}
|