Client.js 6.1 KB

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