RequestParser.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. const settings = require('@overleaf/settings')
  2. const VALID_COMPILERS = ['pdflatex', 'latex', 'xelatex', 'lualatex']
  3. const MAX_TIMEOUT = 600
  4. function parse(body, callback) {
  5. const response = {}
  6. if (body.compile == null) {
  7. return callback(
  8. new Error('top level object should have a compile attribute')
  9. )
  10. }
  11. const { compile } = body
  12. if (!compile.options) {
  13. compile.options = {}
  14. }
  15. try {
  16. response.metricsOpts = {
  17. path: _parseAttribute('metricsPath', compile.options.metricsPath, {
  18. default: '',
  19. type: 'string',
  20. }),
  21. method: _parseAttribute('metricsMethod', compile.options.metricsMethod, {
  22. default: '',
  23. type: 'string',
  24. }),
  25. }
  26. response.compiler = _parseAttribute('compiler', compile.options.compiler, {
  27. validValues: VALID_COMPILERS,
  28. default: 'pdflatex',
  29. type: 'string',
  30. })
  31. response.enablePdfCaching = _parseAttribute(
  32. 'enablePdfCaching',
  33. compile.options.enablePdfCaching,
  34. {
  35. default: false,
  36. type: 'boolean',
  37. }
  38. )
  39. response.pdfCachingMinChunkSize = _parseAttribute(
  40. 'pdfCachingMinChunkSize',
  41. compile.options.pdfCachingMinChunkSize,
  42. {
  43. default: settings.pdfCachingMinChunkSize,
  44. type: 'number',
  45. }
  46. )
  47. response.timeout = _parseAttribute('timeout', compile.options.timeout, {
  48. default: MAX_TIMEOUT,
  49. type: 'number',
  50. })
  51. response.imageName = _parseAttribute(
  52. 'imageName',
  53. compile.options.imageName,
  54. {
  55. type: 'string',
  56. validValues:
  57. settings.clsi &&
  58. settings.clsi.docker &&
  59. settings.clsi.docker.allowedImages,
  60. }
  61. )
  62. response.draft = _parseAttribute('draft', compile.options.draft, {
  63. default: false,
  64. type: 'boolean',
  65. })
  66. response.stopOnFirstError = _parseAttribute(
  67. 'stopOnFirstError',
  68. compile.options.stopOnFirstError,
  69. {
  70. default: false,
  71. type: 'boolean',
  72. }
  73. )
  74. response.check = _parseAttribute('check', compile.options.check, {
  75. type: 'string',
  76. })
  77. response.flags = _parseAttribute('flags', compile.options.flags, {
  78. default: [],
  79. type: 'object',
  80. })
  81. if (settings.allowedCompileGroups) {
  82. response.compileGroup = _parseAttribute(
  83. 'compileGroup',
  84. compile.options.compileGroup,
  85. {
  86. validValues: settings.allowedCompileGroups,
  87. default: '',
  88. type: 'string',
  89. }
  90. )
  91. }
  92. // The syncType specifies whether the request contains all
  93. // resources (full) or only those resources to be updated
  94. // in-place (incremental).
  95. response.syncType = _parseAttribute('syncType', compile.options.syncType, {
  96. validValues: ['full', 'incremental'],
  97. type: 'string',
  98. })
  99. // The syncState is an identifier passed in with the request
  100. // which has the property that it changes when any resource is
  101. // added, deleted, moved or renamed.
  102. //
  103. // on syncType full the syncState identifier is passed in and
  104. // stored
  105. //
  106. // on syncType incremental the syncState identifier must match
  107. // the stored value
  108. response.syncState = _parseAttribute(
  109. 'syncState',
  110. compile.options.syncState,
  111. { type: 'string' }
  112. )
  113. if (response.timeout > MAX_TIMEOUT) {
  114. response.timeout = MAX_TIMEOUT
  115. }
  116. response.timeout = response.timeout * 1000 // milliseconds
  117. response.resources = (compile.resources || []).map(resource =>
  118. _parseResource(resource)
  119. )
  120. const rootResourcePath = _parseAttribute(
  121. 'rootResourcePath',
  122. compile.rootResourcePath,
  123. {
  124. default: 'main.tex',
  125. type: 'string',
  126. }
  127. )
  128. response.rootResourcePath = _checkPath(rootResourcePath)
  129. } catch (error1) {
  130. const error = error1
  131. return callback(error)
  132. }
  133. callback(null, response)
  134. }
  135. function _parseResource(resource) {
  136. let modified
  137. if (resource.path == null || typeof resource.path !== 'string') {
  138. throw new Error('all resources should have a path attribute')
  139. }
  140. if (resource.modified != null) {
  141. modified = new Date(resource.modified)
  142. if (isNaN(modified.getTime())) {
  143. throw new Error(
  144. `resource modified date could not be understood: ${resource.modified}`
  145. )
  146. }
  147. }
  148. if (resource.url == null && resource.content == null) {
  149. throw new Error(
  150. 'all resources should have either a url or content attribute'
  151. )
  152. }
  153. if (resource.content != null && typeof resource.content !== 'string') {
  154. throw new Error('content attribute should be a string')
  155. }
  156. if (resource.url != null && typeof resource.url !== 'string') {
  157. throw new Error('url attribute should be a string')
  158. }
  159. return {
  160. path: resource.path,
  161. modified,
  162. url: resource.url,
  163. content: resource.content,
  164. }
  165. }
  166. function _parseAttribute(name, attribute, options) {
  167. if (attribute != null) {
  168. if (options.validValues != null) {
  169. if (options.validValues.indexOf(attribute) === -1) {
  170. throw new Error(
  171. `${name} attribute should be one of: ${options.validValues.join(
  172. ', '
  173. )}`
  174. )
  175. }
  176. }
  177. if (options.type != null) {
  178. // eslint-disable-next-line valid-typeof
  179. if (typeof attribute !== options.type) {
  180. throw new Error(`${name} attribute should be a ${options.type}`)
  181. }
  182. }
  183. } else {
  184. if (options.default != null) {
  185. return options.default
  186. }
  187. }
  188. return attribute
  189. }
  190. function _checkPath(path) {
  191. // check that the request does not use a relative path
  192. for (const dir of Array.from(path.split('/'))) {
  193. if (dir === '..') {
  194. throw new Error('relative path in root resource')
  195. }
  196. }
  197. return path
  198. }
  199. module.exports = { parse, MAX_TIMEOUT }