From 93e0898c12bc227e11d47597178b4ac8d604641b Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Mon, 27 Feb 2023 18:34:03 +0100 Subject: [PATCH] Extracted `version-match` middleware to separate package - this middlware is standalone and has 100% test coverage, therefore we can move it out to a separate package and remap the requires accordingly --- .../server/web/api/endpoints/admin/app.js | 9 +++--- .../core/server/web/api/middleware/index.js | 3 +- ghost/core/package.json | 1 + ghost/mw-version-match/.eslintrc.js | 6 ++++ ghost/mw-version-match/README.md | 23 ++++++++++++++ ghost/mw-version-match/index.js | 1 + .../lib/mw-version-match.js} | 0 ghost/mw-version-match/package.json | 30 +++++++++++++++++++ ghost/mw-version-match/test/.eslintrc.js | 6 ++++ .../test}/version-match.test.js | 5 ++-- 10 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 ghost/mw-version-match/.eslintrc.js create mode 100644 ghost/mw-version-match/README.md create mode 100644 ghost/mw-version-match/index.js rename ghost/{core/core/server/web/api/middleware/version-match.js => mw-version-match/lib/mw-version-match.js} (100%) create mode 100644 ghost/mw-version-match/package.json create mode 100644 ghost/mw-version-match/test/.eslintrc.js rename ghost/{core/test/unit/server/web/api/middleware => mw-version-match/test}/version-match.test.js (96%) diff --git a/ghost/core/core/server/web/api/endpoints/admin/app.js b/ghost/core/core/server/web/api/endpoints/admin/app.js index 42b83e4d23..3ac02257f4 100644 --- a/ghost/core/core/server/web/api/endpoints/admin/app.js +++ b/ghost/core/core/server/web/api/endpoints/admin/app.js @@ -1,10 +1,11 @@ const debug = require('@tryghost/debug')('web:endpoints:admin:app'); const boolParser = require('express-query-boolean'); -const express = require('../../../../../shared/express'); const bodyParser = require('body-parser'); -const shared = require('../../../shared'); -const apiMw = require('../../middleware'); const errorHandler = require('@tryghost/mw-error-handler'); +const versionMatch = require('@tryghost/mw-version-match'); + +const shared = require('../../../shared'); +const express = require('../../../../../shared/express'); const sentry = require('../../../../../shared/sentry'); const routes = require('./routes'); const APIVersionCompatibilityService = require('../../../../services/api-version-compatibility'); @@ -24,7 +25,7 @@ module.exports = function setupApiApp() { // Check version matches for API requests, depends on res.locals.safeVersion being set // Therefore must come after themeHandler.ghostLocals, for now - apiApp.use(apiMw.versionMatch); + apiApp.use(versionMatch); // Admin API shouldn't be cached apiApp.use(shared.middleware.cacheControl('private')); diff --git a/ghost/core/core/server/web/api/middleware/index.js b/ghost/core/core/server/web/api/middleware/index.js index 91b438796c..a30445b446 100644 --- a/ghost/core/core/server/web/api/middleware/index.js +++ b/ghost/core/core/server/web/api/middleware/index.js @@ -1,6 +1,5 @@ module.exports = { cors: require('./cors'), updateUserLastSeen: require('./update-user-last-seen'), - upload: require('./upload'), - versionMatch: require('./version-match') + upload: require('./upload') }; diff --git a/ghost/core/package.json b/ghost/core/package.json index cad4b42683..5cf19c11ce 100644 --- a/ghost/core/package.json +++ b/ghost/core/package.json @@ -119,6 +119,7 @@ "@tryghost/mw-cache-control": "0.0.0", "@tryghost/mw-error-handler": "0.0.0", "@tryghost/mw-session-from-token": "0.0.0", + "@tryghost/mw-version-match": "0.0.0", "@tryghost/mw-vhost": "0.0.0", "@tryghost/nodemailer": "0.3.32", "@tryghost/nql": "0.11.0", diff --git a/ghost/mw-version-match/.eslintrc.js b/ghost/mw-version-match/.eslintrc.js new file mode 100644 index 0000000000..c9c1bcb522 --- /dev/null +++ b/ghost/mw-version-match/.eslintrc.js @@ -0,0 +1,6 @@ +module.exports = { + plugins: ['ghost'], + extends: [ + 'plugin:ghost/node' + ] +}; diff --git a/ghost/mw-version-match/README.md b/ghost/mw-version-match/README.md new file mode 100644 index 0000000000..20cfe72469 --- /dev/null +++ b/ghost/mw-version-match/README.md @@ -0,0 +1,23 @@ +# Mw Version Match + +Version compatibility middleware for Ghost + + +## Usage + + +## Develop + +This is a monorepo package. + +Follow the instructions for the top-level repo. +1. `git clone` this repo & `cd` into it as usual +2. Run `yarn` to install top-level dependencies. + + + +## Test + +- `yarn lint` run just eslint +- `yarn test` run lint and tests + diff --git a/ghost/mw-version-match/index.js b/ghost/mw-version-match/index.js new file mode 100644 index 0000000000..5ef8696c30 --- /dev/null +++ b/ghost/mw-version-match/index.js @@ -0,0 +1 @@ +module.exports = require('./lib/mw-version-match'); diff --git a/ghost/core/core/server/web/api/middleware/version-match.js b/ghost/mw-version-match/lib/mw-version-match.js similarity index 100% rename from ghost/core/core/server/web/api/middleware/version-match.js rename to ghost/mw-version-match/lib/mw-version-match.js diff --git a/ghost/mw-version-match/package.json b/ghost/mw-version-match/package.json new file mode 100644 index 0000000000..4a5f9f87bd --- /dev/null +++ b/ghost/mw-version-match/package.json @@ -0,0 +1,30 @@ +{ + "name": "@tryghost/mw-version-match", + "version": "0.0.0", + "repository": "https://github.com/TryGhost/Ghost/tree/main/packages/mw-version-match", + "author": "Ghost Foundation", + "private": true, + "main": "index.js", + "scripts": { + "dev": "echo \"Implement me!\"", + "test:unit": "NODE_ENV=testing c8 --all --check-coverage --100 --reporter text --reporter cobertura mocha './test/**/*.test.js'", + "test": "yarn test:unit", + "lint:code": "eslint *.js lib/ --ext .js --cache", + "lint": "yarn lint:code && yarn lint:test", + "lint:test": "eslint -c test/.eslintrc.js test/ --ext .js --cache" + }, + "files": [ + "index.js", + "lib" + ], + "devDependencies": { + "c8": "7.13.0", + "mocha": "10.2.0", + "sinon": "15.0.1" + }, + "dependencies": { + "@tryghost/errors": "1.2.21", + "@tryghost/tpl": "0.1.21", + "semver": "7.3.8" + } +} diff --git a/ghost/mw-version-match/test/.eslintrc.js b/ghost/mw-version-match/test/.eslintrc.js new file mode 100644 index 0000000000..829b601eb0 --- /dev/null +++ b/ghost/mw-version-match/test/.eslintrc.js @@ -0,0 +1,6 @@ +module.exports = { + plugins: ['ghost'], + extends: [ + 'plugin:ghost/test' + ] +}; diff --git a/ghost/core/test/unit/server/web/api/middleware/version-match.test.js b/ghost/mw-version-match/test/version-match.test.js similarity index 96% rename from ghost/core/test/unit/server/web/api/middleware/version-match.test.js rename to ghost/mw-version-match/test/version-match.test.js index e2fb5a04c5..fd76e00145 100644 --- a/ghost/core/test/unit/server/web/api/middleware/version-match.test.js +++ b/ghost/mw-version-match/test/version-match.test.js @@ -1,6 +1,7 @@ -const should = require('should'); +require('should'); const sinon = require('sinon'); -const versionMatch = require('../../../../../../core/server/web/api/middleware/version-match'); + +const versionMatch = require('../'); describe('Version Mismatch', function () { let req;