Преглед изворни кода

Merge pull request #33079 from overleaf/ar-remove-request-from-clsi

[clsi] remove request library

GitOrigin-RevId: 4690c476157fc2829e516d91f688f9424f8c162f
Andrew Rumble пре 2 месеци
родитељ
комит
9daa56becc
4 измењених фајлова са 53 додато и 45 уклоњено
  1. 7 4
      services/clsi/app.js
  2. 0 1
      services/clsi/package.json
  3. 46 39
      services/clsi/test/smoke/js/SmokeTests.js
  4. 0 1
      yarn.lock

+ 7 - 4
services/clsi/app.js

@@ -196,9 +196,6 @@ function runSmokeTest() {
     setTimeout(runSmokeTest, INTERVAL)
   })
 }
-if (Settings.smokeTest) {
-  runSmokeTest()
-}
 
 app.get('/health_check', function (req, res) {
   if (Settings.processTooOld) {
@@ -210,7 +207,10 @@ app.get('/health_check', function (req, res) {
   smokeTest.sendLastResult(res)
 })
 
-app.get('/smoke_test_force', (req, res) => smokeTest.sendNewResult(res))
+app.get(
+  '/smoke_test_force',
+  async (req, res, next) => await smokeTest.sendNewResult(res).catch(next)
+)
 
 app.use(function (error, req, res, next) {
   if (error instanceof Errors.NotFoundError) {
@@ -325,6 +325,9 @@ if (import.meta.main) {
       logger.fatal({ error }, `Error starting CLSI on ${host}:${port}`)
     } else {
       logger.debug(`CLSI starting up, listening on ${host}:${port}`)
+      if (Settings.smokeTest) {
+        runSmokeTest()
+      }
     }
   })
 

+ 0 - 1
services/clsi/package.json

@@ -33,7 +33,6 @@
     "multer": "2.1.1",
     "overleaf-editor-core": "workspace:*",
     "p-limit": "^3.1.0",
-    "request": "2.88.2",
     "send": "^0.19.0",
     "tar-fs": "^3.1.1",
     "workerpool": "^6.1.5"

+ 46 - 39
services/clsi/test/smoke/js/SmokeTests.js

@@ -1,4 +1,4 @@
-import request from 'request'
+import { fetchJson } from '@overleaf/fetch-utils'
 import Settings from '@overleaf/settings'
 
 const buildUrl = path =>
@@ -7,17 +7,29 @@ const buildUrl = path =>
 const url = buildUrl(`project/smoketest-${process.pid}/compile`)
 
 export default {
-  sendNewResult(res) {
-    this._run(error => this._sendResponse(res, error))
+  async sendNewResult(res) {
+    let error
+    try {
+      await this._run()
+    } catch (err) {
+      error = err
+    }
+
+    this._sendResponse(res, error)
   },
   sendLastResult(res) {
     this._sendResponse(res, this._lastError)
   },
   triggerRun(cb) {
-    this._run(error => {
-      this._lastError = error
-      cb(error)
-    })
+    this._run()
+      .then(() => {
+        this._lastError = null
+        cb()
+      })
+      .catch(error => {
+        this._lastError = error
+        cb(error)
+      })
   },
   lastRunSuccessful() {
     return this._lastError == null
@@ -36,19 +48,18 @@ export default {
     res.contentType('text/plain')
     res.status(code).send(body)
   },
-  _run(done) {
-    request.post(
-      {
-        url,
-        json: {
-          compile: {
-            options: {
-              metricsPath: 'health-check',
-            },
-            resources: [
-              {
-                path: 'main.tex',
-                content: `\
+  async _run() {
+    const body = await fetchJson(url, {
+      method: 'POST',
+      json: {
+        compile: {
+          options: {
+            metricsPath: 'health-check',
+          },
+          resources: [
+            {
+              path: 'main.tex',
+              content: `\
 % Membrane-like surface
 % Author: Yotam Avital
 \\documentclass{article}
@@ -102,28 +113,24 @@ date
 
 \\end{document}\
 `,
-              },
-            ],
-          },
+            },
+          ],
         },
       },
-      (error, response, body) => {
-        if (error) return done(error)
-        if (!body || !body.compile || !body.compile.outputFiles) {
-          return done(new Error('response payload incomplete'))
-        }
+    })
 
-        let pdfFound = false
-        let logFound = false
-        for (const file of body.compile.outputFiles) {
-          if (file.type === 'pdf') pdfFound = true
-          if (file.type === 'log') logFound = true
-        }
+    if (!body || !body.compile || !body.compile.outputFiles) {
+      throw new Error('response payload incomplete')
+    }
+
+    let pdfFound = false
+    let logFound = false
+    for (const file of body.compile.outputFiles) {
+      if (file.type === 'pdf') pdfFound = true
+      if (file.type === 'log') logFound = true
+    }
 
-        if (!pdfFound) return done(new Error('no pdf returned'))
-        if (!logFound) return done(new Error('no log returned'))
-        done()
-      }
-    )
+    if (!pdfFound) throw new Error('no pdf returned')
+    if (!logFound) throw new Error('no log returned')
   },
 }

+ 0 - 1
yarn.lock

@@ -6453,7 +6453,6 @@ __metadata:
     nyc: "npm:^17.1.0"
     overleaf-editor-core: "workspace:*"
     p-limit: "npm:^3.1.0"
-    request: "npm:2.88.2"
     send: "npm:^0.19.0"
     sinon: "npm:~9.0.1"
     sinon-chai: "npm:^3.7.0"