diff --git a/libraries/logger/log-level-checker.js b/libraries/logger/log-level-checker.js index 99b3c7dc0c..05e9484f44 100644 --- a/libraries/logger/log-level-checker.js +++ b/libraries/logger/log-level-checker.js @@ -1,4 +1,4 @@ -const fetch = require('node-fetch') +const { fetchString } = require('@overleaf/fetch-utils') const fs = require('fs') class LogLevelChecker { @@ -51,11 +51,7 @@ class GCEMetadataLogLevelChecker extends LogLevelChecker { }, } const uri = `http://metadata.google.internal/computeMetadata/v1/project/attributes/${this.logger.fields.name}-setLogLevelEndTime` - const res = await fetch(uri, options) - if (!res.ok) { - throw new Error('Metadata not okay') - } - const strEndTime = await res.text() + const strEndTime = await fetchString(uri, options) return parseInt(strEndTime, 10) } } diff --git a/libraries/logger/package.json b/libraries/logger/package.json index e05c2b2e11..92981d0c36 100644 --- a/libraries/logger/package.json +++ b/libraries/logger/package.json @@ -18,10 +18,10 @@ }, "dependencies": { "@google-cloud/logging-bunyan": "^5.0.0", + "@overleaf/fetch-utils": "*", "@overleaf/o-error": "*", "@sentry/node": "^6.13.2", - "bunyan": "^1.8.14", - "node-fetch": "^2.6.7" + "bunyan": "^1.8.14" }, "devDependencies": { "chai": "^4.3.6", diff --git a/libraries/logger/test/unit/log-level-checker-tests.js b/libraries/logger/test/unit/log-level-checker-tests.js index db9f9243ed..b26be378eb 100644 --- a/libraries/logger/test/unit/log-level-checker-tests.js +++ b/libraries/logger/test/unit/log-level-checker-tests.js @@ -20,18 +20,14 @@ describe('LogLevelChecker', function () { level: sinon.stub(), fields: { name: 'myapp' }, } - this.fetchResponse = { - text: sinon.stub().resolves(''), - status: 200, - ok: true, + this.FetchUtils = { + fetchString: sinon.stub(), } - this.fetch = sinon - .stub() - .withArgs( - 'http://metadata.google.internal/computeMetadata/v1/project/attributes/myapp-setLogLevelEndTime', - { headers: { 'Metadata-Flavor': 'Google' } } - ) - .resolves(this.fetchResponse) + this.fetchLogLevelEndTimeStub = this.FetchUtils.fetchString.withArgs( + 'http://metadata.google.internal/computeMetadata/v1/project/attributes/myapp-setLogLevelEndTime', + { headers: { 'Metadata-Flavor': 'Google' } } + ) + this.fetchLogLevelEndTimeStub.resolves('') this.fs = { promises: { @@ -43,7 +39,7 @@ describe('LogLevelChecker', function () { this.module = SandboxedModule.require(MODULE_PATH, { requires: { - 'node-fetch': this.fetch, + '@overleaf/fetch-utils': this.FetchUtils, fs: this.fs, }, }) @@ -142,16 +138,7 @@ describe('LogLevelChecker', function () { describe('when the request errors', function () { beforeEach(async function () { - this.fetchResponse.text.rejects(new Error('Read error!')) - }) - checkLogLevel() - expectLevelSetTo(DEFAULT_LEVEL) - }) - - describe('when the request returns a failure status code', function () { - beforeEach(async function () { - this.fetchResponse.status = 500 - this.fetchResponse.ok = false + this.FetchUtils.fetchString.rejects(new Error('Read error!')) }) checkLogLevel() expectLevelSetTo(DEFAULT_LEVEL) @@ -181,7 +168,7 @@ function setupTracingEndTimeGCE(contents) { beforeEach( `set tracing end time in GCE metadata to ${contents}`, function () { - this.fetchResponse.text.resolves(contents) + this.fetchLogLevelEndTimeStub.resolves(contents) } ) } diff --git a/libraries/object-persistor/test/Init.js b/libraries/object-persistor/test/Init.js index 83ad4d7d29..0ed2aeb0d9 100644 --- a/libraries/object-persistor/test/Init.js +++ b/libraries/object-persistor/test/Init.js @@ -5,5 +5,5 @@ chai.use(require('sinon-chai')) chai.use(require('chai-as-promised')) SandboxedModule.configure({ - globals: { Buffer, console, process, URL }, + globals: { Buffer, Math, console, process, URL }, }) diff --git a/package-lock.json b/package-lock.json index 24ddd621e0..c405d6b122 100644 --- a/package-lock.json +++ b/package-lock.json @@ -264,10 +264,10 @@ "license": "AGPL-3.0-only", "dependencies": { "@google-cloud/logging-bunyan": "^5.0.0", + "@overleaf/fetch-utils": "*", "@overleaf/o-error": "*", "@sentry/node": "^6.13.2", - "bunyan": "^1.8.14", - "node-fetch": "^2.6.7" + "bunyan": "^1.8.14" }, "devDependencies": { "chai": "^4.3.6", @@ -53718,12 +53718,12 @@ "version": "file:libraries/logger", "requires": { "@google-cloud/logging-bunyan": "^5.0.0", + "@overleaf/fetch-utils": "*", "@overleaf/o-error": "*", "@sentry/node": "^6.13.2", "bunyan": "^1.8.14", "chai": "^4.3.6", "mocha": "^10.2.0", - "node-fetch": "^2.6.7", "sandboxed-module": "^2.0.4", "sinon": "^9.2.4", "sinon-chai": "^3.7.0"