assert.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use strict'
  2. const check = require('check-types')
  3. const { Blob } = require('overleaf-editor-core')
  4. const assert = check.assert
  5. const MONGO_ID_REGEXP = /^[0-9a-f]{24}$/
  6. const POSTGRES_ID_REGEXP = /^[1-9][0-9]{0,9}$/
  7. const PROJECT_ID_REGEXP = /^([0-9a-f]{24}|[1-9][0-9]{0,9})$/
  8. function transaction(transaction, message) {
  9. assert.function(transaction, message)
  10. }
  11. function blobHash(arg, message) {
  12. assert.match(arg, Blob.HEX_HASH_RX, message)
  13. }
  14. /**
  15. * A chunk id is a string that contains either an integer (for projects stored in Postgres) or 24
  16. * hex digits (for projects stored in Mongo)
  17. */
  18. function projectId(arg, message) {
  19. assert.match(arg, PROJECT_ID_REGEXP, message)
  20. }
  21. /**
  22. * A chunk id is either a number (for projects stored in Postgres) or a 24
  23. * character string (for projects stored in Mongo)
  24. */
  25. function chunkId(arg, message) {
  26. const valid = check.integer(arg) || check.match(arg, MONGO_ID_REGEXP)
  27. if (!valid) {
  28. throw new TypeError(message)
  29. }
  30. }
  31. function mongoId(arg, message) {
  32. assert.match(arg, MONGO_ID_REGEXP)
  33. }
  34. module.exports = {
  35. ...assert,
  36. transaction,
  37. blobHash,
  38. projectId,
  39. chunkId,
  40. mongoId,
  41. MONGO_ID_REGEXP,
  42. POSTGRES_ID_REGEXP,
  43. }