Files
overleaf-cep/services/history-v1/storage/lib/assert.js
T
Brian Gough 835e14b8b2 Merge pull request #24768 from overleaf/bg-history-redis-buffer
test redis caching when loading latest chunk in history-v1

GitOrigin-RevId: f0ee09e5e9e1d7605e228913cb8539be4134e1f7
2025-04-15 08:05:03 +00:00

77 lines
1.7 KiB
JavaScript

'use strict'
const OError = require('@overleaf/o-error')
const check = require('check-types')
const { Blob } = require('overleaf-editor-core')
const assert = check.assert
const MONGO_ID_REGEXP = /^[0-9a-f]{24}$/
const POSTGRES_ID_REGEXP = /^[1-9][0-9]{0,9}$/
const PROJECT_ID_REGEXP = /^([0-9a-f]{24}|[1-9][0-9]{0,9})$/
function transaction(transaction, message) {
assert.function(transaction, message)
}
function blobHash(arg, message) {
try {
assert.match(arg, Blob.HEX_HASH_RX, message)
} catch (error) {
throw OError.tag(error, message, { arg })
}
}
/**
* A project id is a string that contains either an integer (for projects stored in Postgres) or 24
* hex digits (for projects stored in Mongo)
*/
function projectId(arg, message) {
try {
assert.match(arg, PROJECT_ID_REGEXP, message)
} catch (error) {
throw OError.tag(error, message, { arg })
}
}
/**
* A chunk id is either a number (for projects stored in Postgres) or a 24
* character string (for projects stored in Mongo)
*/
function chunkId(arg, message) {
const valid = check.integer(arg) || check.match(arg, MONGO_ID_REGEXP)
if (!valid) {
const error = new TypeError(message)
throw OError.tag(error, message, { arg })
}
}
function mongoId(arg, message) {
try {
assert.match(arg, MONGO_ID_REGEXP, message)
} catch (error) {
throw OError.tag(error, message, { arg })
}
}
function postgresId(arg, message) {
try {
assert.match(arg, POSTGRES_ID_REGEXP, message)
} catch (error) {
throw OError.tag(error, message, { arg })
}
}
module.exports = {
...assert,
transaction,
blobHash,
projectId,
chunkId,
mongoId,
postgresId,
MONGO_ID_REGEXP,
POSTGRES_ID_REGEXP,
}