Client.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* eslint-disable
  2. no-unused-vars,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. let Client
  14. const express = require('express')
  15. const request = require('request')
  16. const fs = require('node:fs')
  17. const Settings = require('@overleaf/settings')
  18. module.exports = Client = {
  19. host: Settings.apis.clsi.url,
  20. randomId() {
  21. return Math.random().toString(16).slice(2)
  22. },
  23. compile(projectId, data, callback) {
  24. if (callback == null) {
  25. callback = function () {}
  26. }
  27. if (data) {
  28. // Enable pdf caching unless disabled explicitly.
  29. data.options = Object.assign({}, { enablePdfCaching: true }, data.options)
  30. }
  31. return request.post(
  32. {
  33. url: `${this.host}/project/${projectId}/compile`,
  34. json: {
  35. compile: data,
  36. },
  37. },
  38. callback
  39. )
  40. },
  41. clearCache(projectId, callback) {
  42. if (callback == null) {
  43. callback = function () {}
  44. }
  45. return request.del(`${this.host}/project/${projectId}`, callback)
  46. },
  47. getOutputFile(response, type) {
  48. for (const file of Array.from(response.compile.outputFiles)) {
  49. if (file.type === type && file.url.match(`output.${type}`)) {
  50. return file
  51. }
  52. }
  53. return null
  54. },
  55. runFakeFilestoreService(directory) {
  56. const app = express()
  57. app.use(express.static(directory))
  58. this.startFakeFilestoreApp(app)
  59. },
  60. startFakeFilestoreApp(app) {
  61. let server
  62. before(function (done) {
  63. server = app.listen(error => {
  64. if (error) {
  65. done(new Error('error starting server: ' + error.message))
  66. } else {
  67. const addr = server.address()
  68. Settings.filestoreDomainOveride = `http://127.0.0.1:${addr.port}`
  69. done()
  70. }
  71. })
  72. })
  73. after(function (done) {
  74. server.close(done)
  75. })
  76. },
  77. syncFromCode(projectId, file, line, column, callback) {
  78. Client.syncFromCodeWithImage(projectId, file, line, column, '', callback)
  79. },
  80. syncFromCodeWithImage(projectId, file, line, column, imageName, callback) {
  81. if (callback == null) {
  82. callback = function () {}
  83. }
  84. return request.get(
  85. {
  86. url: `${this.host}/project/${projectId}/sync/code`,
  87. qs: {
  88. imageName,
  89. file,
  90. line,
  91. column,
  92. },
  93. json: true,
  94. },
  95. (error, response, body) => {
  96. if (error != null) {
  97. return callback(error)
  98. }
  99. if (response.statusCode !== 200) {
  100. return callback(new Error(`statusCode=${response.statusCode}`), body)
  101. }
  102. return callback(null, body)
  103. }
  104. )
  105. },
  106. syncFromPdf(projectId, page, h, v, callback) {
  107. Client.syncFromPdfWithImage(projectId, page, h, v, '', callback)
  108. },
  109. syncFromPdfWithImage(projectId, page, h, v, imageName, callback) {
  110. if (callback == null) {
  111. callback = function () {}
  112. }
  113. return request.get(
  114. {
  115. url: `${this.host}/project/${projectId}/sync/pdf`,
  116. qs: {
  117. imageName,
  118. page,
  119. h,
  120. v,
  121. },
  122. json: true,
  123. },
  124. (error, response, body) => {
  125. if (error != null) {
  126. return callback(error)
  127. }
  128. if (response.statusCode !== 200) {
  129. return callback(new Error(`statusCode=${response.statusCode}`), body)
  130. }
  131. return callback(null, body)
  132. }
  133. )
  134. },
  135. compileDirectory(projectId, baseDirectory, directory, callback) {
  136. if (callback == null) {
  137. callback = function () {}
  138. }
  139. const resources = []
  140. let entities = fs.readdirSync(`${baseDirectory}/${directory}`)
  141. let rootResourcePath = 'main.tex'
  142. while (entities.length > 0) {
  143. const entity = entities.pop()
  144. const stat = fs.statSync(`${baseDirectory}/${directory}/${entity}`)
  145. if (stat.isDirectory()) {
  146. entities = entities.concat(
  147. fs
  148. .readdirSync(`${baseDirectory}/${directory}/${entity}`)
  149. .map(subEntity => {
  150. if (subEntity === 'main.tex') {
  151. rootResourcePath = `${entity}/${subEntity}`
  152. }
  153. return `${entity}/${subEntity}`
  154. })
  155. )
  156. } else if (stat.isFile() && entity !== 'output.pdf') {
  157. const extension = entity.split('.').pop()
  158. if (
  159. [
  160. 'tex',
  161. 'bib',
  162. 'cls',
  163. 'sty',
  164. 'pdf_tex',
  165. 'Rtex',
  166. 'ist',
  167. 'md',
  168. 'Rmd',
  169. 'Rnw',
  170. ].indexOf(extension) > -1
  171. ) {
  172. resources.push({
  173. path: entity,
  174. content: fs
  175. .readFileSync(`${baseDirectory}/${directory}/${entity}`)
  176. .toString(),
  177. })
  178. } else if (
  179. ['eps', 'ttf', 'png', 'jpg', 'pdf', 'jpeg'].indexOf(extension) > -1
  180. ) {
  181. resources.push({
  182. path: entity,
  183. url: `http://filestore/${directory}/${entity}`,
  184. modified: stat.mtime,
  185. })
  186. }
  187. }
  188. }
  189. return fs.readFile(
  190. `${baseDirectory}/${directory}/options.json`,
  191. (error, body) => {
  192. const req = {
  193. resources,
  194. rootResourcePath,
  195. }
  196. if (error == null) {
  197. body = JSON.parse(body)
  198. req.options = body
  199. }
  200. return this.compile(projectId, req, callback)
  201. }
  202. )
  203. },
  204. wordcount(projectId, file, callback) {
  205. const image = undefined
  206. Client.wordcountWithImage(projectId, file, image, callback)
  207. },
  208. wordcountWithImage(projectId, file, image, callback) {
  209. if (callback == null) {
  210. callback = function () {}
  211. }
  212. return request.get(
  213. {
  214. url: `${this.host}/project/${projectId}/wordcount`,
  215. qs: {
  216. image,
  217. file,
  218. },
  219. },
  220. (error, response, body) => {
  221. if (error != null) {
  222. return callback(error)
  223. }
  224. if (response.statusCode !== 200) {
  225. return callback(new Error(`statusCode=${response.statusCode}`), body)
  226. }
  227. return callback(null, JSON.parse(body))
  228. }
  229. )
  230. },
  231. }