From e69484d80de9263da171cfb4334ef897e6b3c59e Mon Sep 17 00:00:00 2001 From: Petr Pchelko Date: Mon, 29 Nov 2021 12:25:41 -0800 Subject: [PATCH] Add content purge filter * This filter can be added in case ALL content in a certain time range needs to be purged. * Add configuration * Fix CI problems --- config.fullstack.test.yaml | 4 +++ lib/purge_content.js | 33 +++++++++++++++++++ package.json | 1 + .../features/pagecontent/language_variants.js | 2 +- 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 lib/purge_content.js diff --git a/config.fullstack.test.yaml b/config.fullstack.test.yaml index 1206e69a..5b8a2076 100644 --- a/config.fullstack.test.yaml +++ b/config.fullstack.test.yaml @@ -229,6 +229,10 @@ spec_root: &spec_root # Some more general RESTBase info x-request-filters: - path: lib/security_response_header_filter.js + - path: lib/purge_content.js + options: + start_time: "Wed Jan 24 2024 14:55:23 GMT+0000" # start of purge (ISO 8601) + end_time: "Wed Jan 25 2024 14:55:23 GMT+0000" # end of purge (ISO 8601) x-sub-request-filters: - type: default diff --git a/lib/purge_content.js b/lib/purge_content.js new file mode 100644 index 00000000..4bcf6fee --- /dev/null +++ b/lib/purge_content.js @@ -0,0 +1,33 @@ +'use strict'; + +const mwUtil = require('./mwUtil'); + +module.exports = (hyper, req, next, options) => { + const startTime = Date.parse(options.start_time); + const endTime = Date.parse(options.end_time); + + return next(hyper, req) + .then((res) => { + if (!startTime || !endTime) { + return res; + } + + let contentTimestamp; + try { + contentTimestamp = mwUtil.extractDateFromEtag(res.headers.etag); + } catch (error) { + return res; + } + + if (!contentTimestamp || contentTimestamp < startTime || contentTimestamp > endTime) { + return res; + } + + if (mwUtil.isNoCacheRequest(req)) { + return res; + } + + req.headers['cache-control'] = 'no-cache'; + return next(hyper, req); + }); +}; diff --git a/package.json b/package.json index 364bd4a6..101988d3 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "eslint-plugin-jsdoc": "^20.4.0", "eslint-plugin-json": "^1.4.0", "js-yaml": "^3.13.1", + "jsonc-parser": "3.2.0", "mocha": "^6.2.3", "mocha-lcov-reporter": "^1.3.0", "mocha.parallel": "^0.15.6", diff --git a/test/features/pagecontent/language_variants.js b/test/features/pagecontent/language_variants.js index e27e74d9..180630c6 100644 --- a/test/features/pagecontent/language_variants.js +++ b/test/features/pagecontent/language_variants.js @@ -158,7 +158,7 @@ describe('Language variants', function() { assert.deepEqual(res.headers['content-language'], 'de'); assert.deepEqual(res.headers['x-restbase-sunset'] || null, 'true'); assert.checkString(res.headers.etag, /^"\d+\/[a-f0-9-]+"$/); - assert.deepEqual(res.body.extract, 'Das ist eine testseite'); + assert.deepEqual(res.body.extract, 'Das ist eine testseite!'); }) });