Client.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. stopCompile(projectId, callback) {
  42. if (callback == null) {
  43. callback = function () {}
  44. }
  45. return request.post(
  46. { url: `${this.host}/project/${projectId}/compile/stop` },
  47. callback
  48. )
  49. },
  50. clearCache(projectId, callback) {
  51. if (callback == null) {
  52. callback = function () {}
  53. }
  54. return request.del(`${this.host}/project/${projectId}`, callback)
  55. },
  56. getOutputFile(response, type) {
  57. for (const file of Array.from(response.compile.outputFiles)) {
  58. if (file.type === type && file.url.match(`output.${type}`)) {
  59. return file
  60. }
  61. }
  62. return null
  63. },
  64. runFakeFilestoreService(directory) {
  65. const app = express()
  66. app.use(express.static(directory))
  67. this.startFakeFilestoreApp(app)
  68. },
  69. startFakeFilestoreApp(app) {
  70. let server
  71. before(function (done) {
  72. server = app.listen(error => {
  73. if (error) {
  74. done(new Error('error starting server: ' + error.message))
  75. } else {
  76. const addr = server.address()
  77. Settings.filestoreDomainOveride = `http://127.0.0.1:${addr.port}`
  78. done()
  79. }
  80. })
  81. })
  82. after(function (done) {
  83. server.close(done)
  84. })
  85. },
  86. syncFromCode(projectId, file, line, column, callback) {
  87. Client.syncFromCodeWithImage(projectId, file, line, column, '', callback)
  88. },
  89. syncFromCodeWithImage(projectId, file, line, column, imageName, callback) {
  90. if (callback == null) {
  91. callback = function () {}
  92. }
  93. return request.get(
  94. {
  95. url: `${this.host}/project/${projectId}/sync/code`,
  96. qs: {
  97. imageName,
  98. file,
  99. line,
  100. column,
  101. },
  102. json: true,
  103. },
  104. (error, response, body) => {
  105. if (error != null) {
  106. return callback(error)
  107. }
  108. if (response.statusCode !== 200) {
  109. return callback(new Error(`statusCode=${response.statusCode}`), body)
  110. }
  111. return callback(null, body)
  112. }
  113. )
  114. },
  115. syncFromPdf(projectId, page, h, v, callback) {
  116. Client.syncFromPdfWithImage(projectId, page, h, v, '', callback)
  117. },
  118. syncFromPdfWithImage(projectId, page, h, v, imageName, callback) {
  119. if (callback == null) {
  120. callback = function () {}
  121. }
  122. return request.get(
  123. {
  124. url: `${this.host}/project/${projectId}/sync/pdf`,
  125. qs: {
  126. imageName,
  127. page,
  128. h,
  129. v,
  130. },
  131. json: true,
  132. },
  133. (error, response, body) => {
  134. if (error != null) {
  135. return callback(error)
  136. }
  137. if (response.statusCode !== 200) {
  138. return callback(new Error(`statusCode=${response.statusCode}`), body)
  139. }
  140. return callback(null, body)
  141. }
  142. )
  143. },
  144. compileDirectory(projectId, baseDirectory, directory, callback) {
  145. if (callback == null) {
  146. callback = function () {}
  147. }
  148. const resources = []
  149. let entities = fs.readdirSync(`${baseDirectory}/${directory}`)
  150. let rootResourcePath = 'main.tex'
  151. while (entities.length > 0) {
  152. const entity = entities.pop()
  153. const stat = fs.statSync(`${baseDirectory}/${directory}/${entity}`)
  154. if (stat.isDirectory()) {
  155. entities = entities.concat(
  156. fs
  157. .readdirSync(`${baseDirectory}/${directory}/${entity}`)
  158. .map(subEntity => {
  159. if (subEntity === 'main.tex') {
  160. rootResourcePath = `${entity}/${subEntity}`
  161. }
  162. return `${entity}/${subEntity}`
  163. })
  164. )
  165. } else if (stat.isFile() && entity !== 'output.pdf') {
  166. const extension = entity.split('.').pop()
  167. if (
  168. [
  169. 'tex',
  170. 'bib',
  171. 'cls',
  172. 'sty',
  173. 'pdf_tex',
  174. 'Rtex',
  175. 'ist',
  176. 'md',
  177. 'Rmd',
  178. 'Rnw',
  179. ].indexOf(extension) > -1
  180. ) {
  181. resources.push({
  182. path: entity,
  183. content: fs
  184. .readFileSync(`${baseDirectory}/${directory}/${entity}`)
  185. .toString(),
  186. })
  187. } else if (
  188. ['eps', 'ttf', 'png', 'jpg', 'pdf', 'jpeg'].indexOf(extension) > -1
  189. ) {
  190. resources.push({
  191. path: entity,
  192. url: `http://filestore/${directory}/${entity}`,
  193. modified: stat.mtime,
  194. })
  195. }
  196. }
  197. }
  198. return fs.readFile(
  199. `${baseDirectory}/${directory}/options.json`,
  200. (error, body) => {
  201. const req = {
  202. resources,
  203. rootResourcePath,
  204. }
  205. if (error == null) {
  206. body = JSON.parse(body)
  207. req.options = body
  208. }
  209. return this.compile(projectId, req, callback)
  210. }
  211. )
  212. },
  213. wordcount(projectId, file, callback) {
  214. const image = undefined
  215. Client.wordcountWithImage(projectId, file, image, callback)
  216. },
  217. wordcountWithImage(projectId, file, image, callback) {
  218. if (callback == null) {
  219. callback = function () {}
  220. }
  221. return request.get(
  222. {
  223. url: `${this.host}/project/${projectId}/wordcount`,
  224. qs: {
  225. image,
  226. file,
  227. },
  228. },
  229. (error, response, body) => {
  230. if (error != null) {
  231. return callback(error)
  232. }
  233. if (response.statusCode !== 200) {
  234. return callback(new Error(`statusCode=${response.statusCode}`), body)
  235. }
  236. return callback(null, JSON.parse(body))
  237. }
  238. )
  239. },
  240. }