From aff34570124de6573b743bee49ee9c38a99444b0 Mon Sep 17 00:00:00 2001 From: Daniel Schischkin Date: Sat, 17 Apr 2021 00:24:09 +0200 Subject: [PATCH 1/5] Add new function to dynamically replace the a "substring" of the fragment url To avoid a complicated setup, there is now a function to dynamically replace the fragment url by putting the area you want to replace in curly brackets. This way you have a simple setup for different environments. So you get the possibility to switch the environment via the environment variable if necessary. e.g. production: ZALANDO_HEADER_HOST: header.zalando.de staging: ZALANDO_HEADER_HOST: header.staging.zalando.de --- lib/parse-environment.js | 9 +++++++ tests/parse-environment.js | 54 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 lib/parse-environment.js create mode 100644 tests/parse-environment.js diff --git a/lib/parse-environment.js b/lib/parse-environment.js new file mode 100644 index 0000000..1ef5e5d --- /dev/null +++ b/lib/parse-environment.js @@ -0,0 +1,9 @@ +'use strict'; + +module.exports = url => { + const percentRegEx = /\{([^}]+)\}/; + const matches = percentRegEx.exec(url); + return matches != null && Object.keys(process.env).includes(matches[1]) + ? url.replace(matches[0], process.env[matches[1]]) + : url; +}; diff --git a/tests/parse-environment.js b/tests/parse-environment.js new file mode 100644 index 0000000..4691be7 --- /dev/null +++ b/tests/parse-environment.js @@ -0,0 +1,54 @@ +'use strict'; +const assert = require('assert'); +const parseEnvironmentVariables = require('../lib/parse-environment'); + +describe('Parse Environment', () => { + it('should return the same url when no brackets found', () => { + const fragmentUrl = 'https://test.zalando.de/#/blub/12'; + assert.strictEqual( + parseEnvironmentVariables(fragmentUrl), + fragmentUrl, + 'The url is not the same anymore' + ); + }); + + it('should return the same url when only one bracket is found', () => { + let fragmentUrl = 'https://{test.zalando.de/#/blub/12'; + assert.strictEqual( + parseEnvironmentVariables(fragmentUrl), + fragmentUrl, + 'The url is not the same anymore' + ); + + fragmentUrl = 'https://test.zalando.de}/#/blub/12'; + assert.strictEqual( + parseEnvironmentVariables(fragmentUrl), + fragmentUrl, + 'The url is not the same anymore' + ); + }); + + it('should return the same url when no environment variable is found', () => { + const envVarName = `TEST__ROOT_HOST_${+new Date()}`; + const fragmentUrl = `https://{${envVarName}}/#/blub/12`; + delete process.env[envVarName]; + assert.strictEqual( + parseEnvironmentVariables(fragmentUrl), + fragmentUrl, + 'The url is not the same anymore' + ); + }); + + it('should return the modified url when everything is given', () => { + const envVarName = `TEST__ROOT_HOST_${+new Date()}`; + const envVarValue = 'test.zalando.de'; + const fragmentUrl = `https://{${envVarName}}/#/blub/12`; + process.env[envVarName] = envVarValue; + assert.strictEqual( + parseEnvironmentVariables(fragmentUrl), + fragmentUrl.replace(`{${envVarName}}`, envVarValue), + 'The url is the same' + ); + delete process.env[envVarName]; + }); +}); From 19b0f3074c35dee79180cfbbbbe6e18739099690 Mon Sep 17 00:00:00 2001 From: Daniel Schischkin Date: Sat, 17 Apr 2021 00:28:33 +0200 Subject: [PATCH 2/5] Implement the new "ParseEnvironmentVariable" Feature in request-fragment.js --- lib/request-fragment.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/request-fragment.js b/lib/request-fragment.js index 63909fa..3aa0566 100644 --- a/lib/request-fragment.js +++ b/lib/request-fragment.js @@ -5,6 +5,7 @@ const https = require('https'); const url = require('url'); const { globalTracer, FORMAT_HTTP_HEADERS } = require('opentracing'); const tracer = globalTracer(); +const parseEnvironmentVariables = require('./parse-environment'); // By default tailor supports gzipped response from fragments const requiredHeaders = { @@ -30,7 +31,7 @@ module.exports = filterHeaders => ( span = null ) => new Promise((resolve, reject) => { - const parsedUrl = url.parse(fragmentUrl); + const parsedUrl = url.parse(parseEnvironmentVariables(fragmentUrl)); const options = Object.assign( { headers: Object.assign( From 2bdb0533312aac80179b9a87d756329ac70fdc78 Mon Sep 17 00:00:00 2001 From: Daniel Schischkin Date: Mon, 19 Apr 2021 09:31:45 +0200 Subject: [PATCH 3/5] Fix eslint config to avoid spread operator error (unexpected token ..) I added the parser options and set them to ECMAScript2018, so that the spread operator is recognized by ESLint and green builds can be produced again. --- .eslintrc | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/.eslintrc b/.eslintrc index 96db0ca..dd919cd 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,4 +1,7 @@ { + "parserOptions": { + "ecmaVersion": 2018 + }, "env": { "browser": false, "es6": true, @@ -19,12 +22,21 @@ "no-eval": 2, "no-unused-vars": 2, "no-lonely-if": 2, - "quotes": [2, "single"], - "strict": [2, "global"], - "prettier/prettier": [2, { - "singleQuote": true, - "tabWidth": 4 - }] + "quotes": [ + 2, + "single" + ], + "strict": [ + 2, + "global" + ], + "prettier/prettier": [ + 2, + { + "singleQuote": true, + "tabWidth": 4 + } + ] }, "globals": { "require": false, From 466ea48b5604656e1a736d7a32dcdbde12a08dea Mon Sep 17 00:00:00 2001 From: Daniel Schischkin Date: Mon, 19 Apr 2021 10:32:54 +0200 Subject: [PATCH 4/5] Fix deprecated assert code in tests/tailor.js --- tests/tailor.js | 131 ++++++++++++++++++++++++------------------------ 1 file changed, 65 insertions(+), 66 deletions(-) diff --git a/tests/tailor.js b/tests/tailor.js index ecde416..97fe9ae 100644 --- a/tests/tailor.js +++ b/tests/tailor.js @@ -124,8 +124,8 @@ describe('Tailor', () => { mockTemplate.returns(false); getResponse('http://localhost:8080/missing-template') .then(response => { - assert.equal(response.statusCode, 500); - assert.equal(response.body, 'error template'); + assert.strictEqual(response.statusCode, 500); + assert.strictEqual(response.body, 'error template'); }) .then(done, done); }); @@ -134,8 +134,8 @@ describe('Tailor', () => { mockTemplate.returns('404'); getResponse('http://localhost:8080/404-template') .then(response => { - assert.equal(response.statusCode, 404); - assert.equal(response.body, 'template not found'); + assert.strictEqual(response.statusCode, 404); + assert.strictEqual(response.body, 'template not found'); }) .then(done, done); }); @@ -156,8 +156,8 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal(response.statusCode, 200); - assert.equal( + assert.strictEqual(response.statusCode, 200); + assert.strictEqual( response.body, '' + '' + @@ -182,7 +182,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -209,7 +209,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -243,11 +243,11 @@ describe('Tailor', () => { it('should stream pipe definition with loader in the head', done => { getResponse('http://localhost:8083/test') .then(response => { - assert.equal( + assert.strictEqual( response.headers.link, '; rel="preload"; as="script"; nopush; crossorigin' ); - assert.equal( + assert.strictEqual( response.body, '' + '\n' + @@ -277,8 +277,8 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal(response.statusCode, 300); - assert.equal(response.headers.location, 'https://redirect'); + assert.strictEqual(response.statusCode, 300); + assert.strictEqual(response.headers.location, 'https://redirect'); }) .then(done, done); }); @@ -304,7 +304,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal(response.statusCode, 200); + assert.strictEqual(response.statusCode, 200); assert.deepEqual(response.headers['set-cookie'], [cookie]); }) .then(done, done); @@ -364,11 +364,11 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { const headers = response.headers; - assert.equal( + assert.strictEqual( 'no-cache, no-store, must-revalidate', headers['cache-control'] ); - assert.equal('no-cache', headers['pragma']); + assert.strictEqual('no-cache', headers['pragma']); }) .then(done, done); }); @@ -402,7 +402,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.headers.link, '; rel="preload"; as="script"; nopush; crossorigin' ); @@ -424,7 +424,7 @@ describe('Tailor', () => { getResponse('http://localhost:8082/test') .then(response => { - assert.equal(response.headers.link, undefined); + assert.strictEqual(response.headers.link, undefined); }) .then(done, done); }); @@ -450,7 +450,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.headers.link, '; rel="preload"; as="script"; nopush; crossorigin,; rel="preload"; as="style"; nopush;,; rel="preload"; as="script"; nopush; crossorigin' ); @@ -473,7 +473,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.headers.link, '; rel="preload"; as="script"; nopush; crossorigin,; rel="preload"; as="style"; nopush;,; rel="preload"; as="script"; nopush;' ); @@ -500,7 +500,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' ); @@ -520,7 +520,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal(response.statusCode, 500); + assert.strictEqual(response.statusCode, 500); }) .then(done, done); }); @@ -538,7 +538,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal(response.statusCode, 500); + assert.strictEqual(response.statusCode, 500); }) .then(done, done); }); @@ -558,7 +558,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal(response.statusCode, 200); + assert.strictEqual(response.statusCode, 200); }) .then(done, done); }); @@ -578,7 +578,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal(response.statusCode, 500); + assert.strictEqual(response.statusCode, 500); }) .then(done, done); }); @@ -599,7 +599,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -629,7 +629,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -657,7 +657,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -688,7 +688,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -721,8 +721,8 @@ describe('Tailor', () => { mockContext.returns(Promise.resolve(contextObj)); getResponse('http://localhost:8080/test').then(response => { - assert.equal(response.statusCode, 200); - assert.equal( + assert.strictEqual(response.statusCode, 200); + assert.strictEqual( response.body, '' + '' + @@ -741,8 +741,8 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal(response.statusCode, 200); - assert.equal( + assert.strictEqual(response.statusCode, 200); + assert.strictEqual( response.body, '' + '' + @@ -771,7 +771,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, 'hello' ); @@ -792,7 +792,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -823,7 +823,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -853,7 +853,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -881,7 +881,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -900,7 +900,7 @@ describe('Tailor', () => { mockTemplate.returns(''); http.get('http://localhost:8080/test', () => { - assert.equal(console.warn.callCount, 1); + assert.strictEqual(console.warn.callCount, 1); console.warn.restore(); done(); }); @@ -915,7 +915,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -938,7 +938,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -961,7 +961,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -992,7 +992,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -1026,7 +1026,7 @@ describe('Tailor', () => { ); http.get('http://localhost:8080/test', response => { - assert.equal(response.statusCode, 500); + assert.strictEqual(response.statusCode, 500); done(); }); }); @@ -1052,7 +1052,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -1086,7 +1086,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -1116,7 +1116,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -1141,7 +1141,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -1189,7 +1189,7 @@ describe('Tailor', () => { getResponse('http://localhost:8081/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -1237,7 +1237,7 @@ describe('Tailor', () => { getResponse('http://localhost:8081/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -1285,7 +1285,7 @@ describe('Tailor', () => { getResponse('http://localhost:8081/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -1317,7 +1317,7 @@ describe('Tailor', () => { getResponse('http://localhost:8081/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -1351,8 +1351,8 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(() => { const { tags } = traceResults(); - assert.equal(tags.length, 1); - assert.deepEqual(tags[0], { + assert.strictEqual(tags.length, 1); + assert.deepStrictEqual(tags[0], { 'http.url': '/test', 'span.kind': 'server' }); @@ -1365,13 +1365,13 @@ describe('Tailor', () => { getResponse('http://localhost:8080/error') .then(() => { const { tags, logs } = traceResults(); - assert.deepEqual(tags[0], { + assert.deepStrictEqual(tags[0], { 'http.url': '/error', 'span.kind': 'server', error: true, 'http.status_code': 500 }); - assert.equal(logs.length, 1); + assert.strictEqual(logs.length, 1); }) .then(done, done); }); @@ -1389,19 +1389,18 @@ describe('Tailor', () => { .then(() => { const { tags } = traceResults(); // Tailor should return error - assert.equal(tags[0].error, true); + assert.strictEqual(tags[0].error, true); // Primary fragment error - assert.deepEqual(tags[1], { - error: true, - primary: true, - foo: 'bar', + assert.deepStrictEqual(tags[1], { + 'error': true, + 'primary': true, 'span.kind': 'client', 'http.url': 'https://fragment/1', - fallback: false, - public: false, - async: false, - id: 'unnamed', - timeout: 3000 + 'fallback': false, + 'public': false, + 'async': false, + 'id': 'unnamed', + 'timeout': 3000 }); }) .then(done, done); @@ -1423,7 +1422,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(() => { const { tags } = traceResults(); - assert.deepEqual(tags[1], { + assert.deepStrictEqual(tags[1], { 'span.kind': 'client', [Tags.HTTP_URL]: 'https://fragment/1', id: 'test', From 311a8affa7ef2caf477c285cc743f3b102128458 Mon Sep 17 00:00:00 2001 From: Daniel Schischkin Date: Mon, 19 Apr 2021 10:35:41 +0200 Subject: [PATCH 5/5] Satisfy eslint for tests/tailor.js --- tests/tailor.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/tests/tailor.js b/tests/tailor.js index 97fe9ae..a560455 100644 --- a/tests/tailor.js +++ b/tests/tailor.js @@ -278,7 +278,10 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { assert.strictEqual(response.statusCode, 300); - assert.strictEqual(response.headers.location, 'https://redirect'); + assert.strictEqual( + response.headers.location, + 'https://redirect' + ); }) .then(done, done); }); @@ -1392,15 +1395,15 @@ describe('Tailor', () => { assert.strictEqual(tags[0].error, true); // Primary fragment error assert.deepStrictEqual(tags[1], { - 'error': true, - 'primary': true, + error: true, + primary: true, 'span.kind': 'client', 'http.url': 'https://fragment/1', - 'fallback': false, - 'public': false, - 'async': false, - 'id': 'unnamed', - 'timeout': 3000 + fallback: false, + public: false, + async: false, + id: 'unnamed', + timeout: 3000 }); }) .then(done, done);