Client.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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('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://localhost:${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. ].indexOf(extension) > -1
  170. ) {
  171. resources.push({
  172. path: entity,
  173. content: fs
  174. .readFileSync(`${baseDirectory}/${directory}/${entity}`)
  175. .toString(),
  176. })
  177. } else if (
  178. ['eps', 'ttf', 'png', 'jpg', 'pdf', 'jpeg'].indexOf(extension) > -1
  179. ) {
  180. resources.push({
  181. path: entity,
  182. url: `http://filestore/${directory}/${entity}`,
  183. modified: stat.mtime,
  184. })
  185. }
  186. }
  187. }
  188. return fs.readFile(
  189. `${baseDirectory}/${directory}/options.json`,
  190. (error, body) => {
  191. const req = {
  192. resources,
  193. rootResourcePath,
  194. }
  195. if (error == null) {
  196. body = JSON.parse(body)
  197. req.options = body
  198. }
  199. return this.compile(projectId, req, callback)
  200. }
  201. )
  202. },
  203. wordcount(projectId, file, callback) {
  204. const image = undefined
  205. Client.wordcountWithImage(projectId, file, image, callback)
  206. },
  207. wordcountWithImage(projectId, file, image, callback) {
  208. if (callback == null) {
  209. callback = function () {}
  210. }
  211. return request.get(
  212. {
  213. url: `${this.host}/project/${projectId}/wordcount`,
  214. qs: {
  215. image,
  216. file,
  217. },
  218. },
  219. (error, response, body) => {
  220. if (error != null) {
  221. return callback(error)
  222. }
  223. if (response.statusCode !== 200) {
  224. return callback(new Error(`statusCode=${response.statusCode}`), body)
  225. }
  226. return callback(null, JSON.parse(body))
  227. }
  228. )
  229. },
  230. }