@fontoxml/fontoxml-development-tools
Version:
Development tools for Fonto.
86 lines (76 loc) • 2.32 kB
JavaScript
import asyncRouteWithLockCleanupHandler from '../asyncRouteWithLockCleanupHandler.js';
/**
* This endpoint is a copy of configureDocumentRevisionGetPostRouteHandler.js.
*
* Please keep both endpoints in sync.
*
* But note the differences:
* - For Output there is never a editSessionToken in the request body or query parameters.
* - Output can receive an optional Fonto-Correlation-Id.
*/
/** @typedef {import('../../src/getAppConfig.js').DevCmsConfig} DevCmsConfig */
/**
* @param {DevCmsConfig} _config
*/
export default function configureOutputDocumentRevisionGetPostRouteHandler(
_config,
) {
return asyncRouteWithLockCleanupHandler(async (acquireLock, req, res) => {
const documentsRevisions = req.body.documentsRevisions;
const correlationIdRepository = req.repositories.correlationId;
// Because these requests do not originate from the editor, but from another server, we
// have no edit session token. In some cases, however, we have an correlationId which we
// may resolve to the editSessionToken used by the corresponding call to the proxy.
const editSessionToken =
correlationIdRepository.getEditSessionTokenForRequest(req);
const results = await Promise.all(
documentsRevisions.map(async (documentRevisions) => {
const documentId = documentRevisions.documentId;
const documentExists = req.cms.existsSync(documentId);
if (!documentExists) {
return {
documentId,
status: 404,
};
}
const revisions = await Promise.all(
documentRevisions.revisionIds.map(async (revisionId) => {
// Using a file lock per revision, otherwise we might still cause a race condition.
const fileLock = await acquireLock(documentId);
const content = await req.cms.getFileByRevisionId(
documentId,
revisionId,
editSessionToken,
fileLock,
);
fileLock.release();
if (content === null) {
return {
revisionId,
status: 404,
};
}
return {
revisionId,
status: 200,
revision: {
content,
},
};
}),
);
return {
documentId,
status: 200,
revisions,
};
}),
);
res
.status(200)
.set('content-type', 'application/json; charset=utf-8')
.json({
results,
});
});
}