Client.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 request = require('request')
  15. const fs = require('fs')
  16. const Settings = require('@overleaf/settings')
  17. const host = 'localhost'
  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. runServer(port, directory) {
  56. const express = require('express')
  57. const app = express()
  58. app.use(express.static(directory))
  59. console.log('starting test server on', port, host)
  60. return app.listen(port, host).on('error', error => {
  61. console.error('error starting server:', error.message)
  62. return process.exit(1)
  63. })
  64. },
  65. syncFromCode(projectId, file, line, column, callback) {
  66. Client.syncFromCodeWithImage(projectId, file, line, column, '', callback)
  67. },
  68. syncFromCodeWithImage(projectId, file, line, column, imageName, callback) {
  69. if (callback == null) {
  70. callback = function () {}
  71. }
  72. return request.get(
  73. {
  74. url: `${this.host}/project/${projectId}/sync/code`,
  75. qs: {
  76. imageName,
  77. file,
  78. line,
  79. column,
  80. },
  81. json: true,
  82. },
  83. (error, response, body) => {
  84. if (error != null) {
  85. return callback(error)
  86. }
  87. if (response.statusCode !== 200) {
  88. return callback(new Error(`statusCode=${response.statusCode}`), body)
  89. }
  90. return callback(null, body)
  91. }
  92. )
  93. },
  94. syncFromPdf(projectId, page, h, v, callback) {
  95. Client.syncFromPdfWithImage(projectId, page, h, v, '', callback)
  96. },
  97. syncFromPdfWithImage(projectId, page, h, v, imageName, callback) {
  98. if (callback == null) {
  99. callback = function () {}
  100. }
  101. return request.get(
  102. {
  103. url: `${this.host}/project/${projectId}/sync/pdf`,
  104. qs: {
  105. imageName,
  106. page,
  107. h,
  108. v,
  109. },
  110. json: true,
  111. },
  112. (error, response, body) => {
  113. if (error != null) {
  114. return callback(error)
  115. }
  116. if (response.statusCode !== 200) {
  117. return callback(new Error(`statusCode=${response.statusCode}`), body)
  118. }
  119. return callback(null, body)
  120. }
  121. )
  122. },
  123. compileDirectory(projectId, baseDirectory, directory, serverPort, callback) {
  124. if (callback == null) {
  125. callback = function () {}
  126. }
  127. const resources = []
  128. let entities = fs.readdirSync(`${baseDirectory}/${directory}`)
  129. let rootResourcePath = 'main.tex'
  130. while (entities.length > 0) {
  131. const entity = entities.pop()
  132. const stat = fs.statSync(`${baseDirectory}/${directory}/${entity}`)
  133. if (stat.isDirectory()) {
  134. entities = entities.concat(
  135. fs
  136. .readdirSync(`${baseDirectory}/${directory}/${entity}`)
  137. .map(subEntity => {
  138. if (subEntity === 'main.tex') {
  139. rootResourcePath = `${entity}/${subEntity}`
  140. }
  141. return `${entity}/${subEntity}`
  142. })
  143. )
  144. } else if (stat.isFile() && entity !== 'output.pdf') {
  145. const extension = entity.split('.').pop()
  146. if (
  147. [
  148. 'tex',
  149. 'bib',
  150. 'cls',
  151. 'sty',
  152. 'pdf_tex',
  153. 'Rtex',
  154. 'ist',
  155. 'md',
  156. 'Rmd',
  157. ].indexOf(extension) > -1
  158. ) {
  159. resources.push({
  160. path: entity,
  161. content: fs
  162. .readFileSync(`${baseDirectory}/${directory}/${entity}`)
  163. .toString(),
  164. })
  165. } else if (
  166. ['eps', 'ttf', 'png', 'jpg', 'pdf', 'jpeg'].indexOf(extension) > -1
  167. ) {
  168. resources.push({
  169. path: entity,
  170. url: `http://${host}:${serverPort}/${directory}/${entity}`,
  171. modified: stat.mtime,
  172. })
  173. }
  174. }
  175. }
  176. return fs.readFile(
  177. `${baseDirectory}/${directory}/options.json`,
  178. (error, body) => {
  179. const req = {
  180. resources,
  181. rootResourcePath,
  182. }
  183. if (error == null) {
  184. body = JSON.parse(body)
  185. req.options = body
  186. }
  187. return this.compile(projectId, req, callback)
  188. }
  189. )
  190. },
  191. wordcount(projectId, file, callback) {
  192. const image = undefined
  193. Client.wordcountWithImage(projectId, file, image, callback)
  194. },
  195. wordcountWithImage(projectId, file, image, callback) {
  196. if (callback == null) {
  197. callback = function () {}
  198. }
  199. return request.get(
  200. {
  201. url: `${this.host}/project/${projectId}/wordcount`,
  202. qs: {
  203. image,
  204. file,
  205. },
  206. },
  207. (error, response, body) => {
  208. if (error != null) {
  209. return callback(error)
  210. }
  211. if (response.statusCode !== 200) {
  212. return callback(new Error(`statusCode=${response.statusCode}`), body)
  213. }
  214. return callback(null, JSON.parse(body))
  215. }
  216. )
  217. },
  218. }