From 71285aba317ce8b91db34cc088a19d398051254e Mon Sep 17 00:00:00 2001 From: Evan Lovely Date: Mon, 24 Feb 2020 09:57:09 -0800 Subject: [PATCH] fix: remove need to save ports used list to file system Before the previously used ports were saved to a config file written to the file system by the `conf` package. This replaces that with the built-in `Set` functionality that keeps it in memory and removes a dependency. --- .eslintrc | 10 +-- package-lock.json | 180 ++++--------------------------------------- package.json | 1 - src/twig-renderer.js | 85 +++++++++++++------- 4 files changed, 78 insertions(+), 198 deletions(-) diff --git a/.eslintrc b/.eslintrc index 7b37779..88f990a 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,16 +1,12 @@ { - "extends": [ - "airbnb", - "plugin:jest/recommended" - ], - "plugins": [ - "jest" - ], + "extends": ["airbnb", "plugin:jest/recommended"], + "plugins": ["jest"], "env": { "jest/globals": true }, "rules": { "no-console": "off", + "no-await-in-loop": "off", "no-plusplus": [ "error", { diff --git a/package-lock.json b/package-lock.json index 8a30d08..dcc9bf9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2075,70 +2075,6 @@ "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", "dev": true }, - "conf": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/conf/-/conf-5.0.0.tgz", - "integrity": "sha512-lRNyt+iRD4plYaOSVTxu1zPWpaH0EOxgFIR1l3mpC/DGZ7XzhoGFMKmbl54LAgXcSu6knqWgOwdINkqm58N85A==", - "requires": { - "ajv": "^6.10.0", - "dot-prop": "^5.0.0", - "env-paths": "^2.2.0", - "json-schema-typed": "^7.0.0", - "make-dir": "^3.0.0", - "pkg-up": "^3.0.1", - "write-file-atomic": "^3.0.0" - }, - "dependencies": { - "ajv": { - "version": "6.10.2", - "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.10.2.tgz", - "integrity": "sha512-TXtUUEYHuaTEbLZWIKUr5pmBuhDLy+8KYtPYdcV8qC+pOZL+NKqYwvWSRrVXHn+ZmRRAu8vJTAznH7Oag6RVRw==", - "requires": { - "fast-deep-equal": "^2.0.1", - "fast-json-stable-stringify": "^2.0.0", - "json-schema-traverse": "^0.4.1", - "uri-js": "^4.2.2" - } - }, - "dot-prop": { - "version": "5.1.0", - "resolved": "https://registry.npmjs.org/dot-prop/-/dot-prop-5.1.0.tgz", - "integrity": "sha512-n1oC6NBF+KM9oVXtjmen4Yo7HyAVWV2UUl50dCYJdw2924K6dX9bf9TTTWaKtYlRn0FEtxG27KS80ayVLixxJA==", - "requires": { - "is-obj": "^2.0.0" - } - }, - "is-obj": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/is-obj/-/is-obj-2.0.0.tgz", - "integrity": "sha512-drqDG3cbczxxEJRoOXcOjtdp1J/lyp1mNn0xaznRs8+muBhgQcrnbspox5X5fOw0HnMnbfDzvnEMEtqDEJEo8w==" - }, - "make-dir": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-3.0.0.tgz", - "integrity": "sha512-grNJDhb8b1Jm1qeqW5R/O63wUo4UXo2v2HMic6YT9i/HBlF93S8jkMgH7yugvY9ABDShH4VZMn8I+U8+fCNegw==", - "requires": { - "semver": "^6.0.0" - } - }, - "semver": { - "version": "6.3.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", - "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==" - }, - "write-file-atomic": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/write-file-atomic/-/write-file-atomic-3.0.1.tgz", - "integrity": "sha512-JPStrIyyVJ6oCSz/691fAjFtefZ6q+fP6tm+OS4Qw6o+TGQxNp1ziY2PgS+X/m0V8OWhZiO/m4xSj+Pr4RrZvw==", - "requires": { - "imurmurhash": "^0.1.4", - "is-typedarray": "^1.0.0", - "signal-exit": "^3.0.2", - "typedarray-to-buffer": "^3.1.5" - } - } - } - }, "configstore": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/configstore/-/configstore-3.1.2.tgz", @@ -2612,11 +2548,6 @@ "once": "^1.4.0" } }, - "env-paths": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/env-paths/-/env-paths-2.2.0.tgz", - "integrity": "sha512-6u0VYSCo/OW6IoD5WCLLy9JUGARbamfSavcNXry/eu8aHVFei6CD3Sw+VGX5alea1i9pgPHW0mbu6Xj0uBh7gA==" - }, "error-ex": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz", @@ -3521,8 +3452,7 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "aproba": { "version": "1.2.0", @@ -3543,14 +3473,12 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -3565,20 +3493,17 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -3695,8 +3620,7 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "ini": { "version": "1.3.5", @@ -3708,7 +3632,6 @@ "version": "1.0.0", "bundled": true, "dev": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -3723,7 +3646,6 @@ "version": "3.0.4", "bundled": true, "dev": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -3731,14 +3653,12 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "minipass": { "version": "2.2.4", "bundled": true, "dev": true, - "optional": true, "requires": { "safe-buffer": "^5.1.1", "yallist": "^3.0.0" @@ -3757,7 +3677,6 @@ "version": "0.5.1", "bundled": true, "dev": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -3838,8 +3757,7 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "object-assign": { "version": "4.1.1", @@ -3851,7 +3769,6 @@ "version": "1.4.0", "bundled": true, "dev": true, - "optional": true, "requires": { "wrappy": "1" } @@ -3937,8 +3854,7 @@ "safe-buffer": { "version": "5.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "safer-buffer": { "version": "2.1.2", @@ -3974,7 +3890,6 @@ "version": "1.0.2", "bundled": true, "dev": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -3994,7 +3909,6 @@ "version": "3.0.1", "bundled": true, "dev": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -4038,14 +3952,12 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "yallist": { "version": "3.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true } } }, @@ -4597,7 +4509,8 @@ "imurmurhash": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/imurmurhash/-/imurmurhash-0.1.4.tgz", - "integrity": "sha1-khi5srkoojixPcT7a21XbyMUU+o=" + "integrity": "sha1-khi5srkoojixPcT7a21XbyMUU+o=", + "dev": true }, "indent-string": { "version": "3.2.0", @@ -5005,7 +4918,8 @@ "is-typedarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz", - "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=" + "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=", + "dev": true }, "is-utf8": { "version": "0.2.1", @@ -6251,11 +6165,6 @@ "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==" }, - "json-schema-typed": { - "version": "7.0.1", - "resolved": "https://registry.npmjs.org/json-schema-typed/-/json-schema-typed-7.0.1.tgz", - "integrity": "sha512-IqUK+Cqc8/MqHsCvv1TMccbKdBzoATOLHXZAF5UDu70/CCxo648cHUig24hc+XTK53TyeNk1UeVTlc2Haovtsw==" - }, "json-stable-stringify-without-jsonify": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz", @@ -10380,7 +10289,8 @@ "path-exists": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", - "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=" + "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=", + "dev": true }, "path-is-absolute": { "version": "1.0.1", @@ -10470,54 +10380,6 @@ } } }, - "pkg-up": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/pkg-up/-/pkg-up-3.1.0.tgz", - "integrity": "sha512-nDywThFk1i4BQK4twPQ6TA4RT8bDY96yeuCVBWL3ePARCiEKDRSrNGbFIgUJpLp+XeIR65v8ra7WuJOFUBtkMA==", - "requires": { - "find-up": "^3.0.0" - }, - "dependencies": { - "find-up": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz", - "integrity": "sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==", - "requires": { - "locate-path": "^3.0.0" - } - }, - "locate-path": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz", - "integrity": "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==", - "requires": { - "p-locate": "^3.0.0", - "path-exists": "^3.0.0" - } - }, - "p-limit": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.1.tgz", - "integrity": "sha512-85Tk+90UCVWvbDavCLKPOLC9vvY8OwEX/RtKF+/1OADJMVlFfEHOiMTPVyxg7mk/dKa+ipdHm0OUkTvCpMTuwg==", - "requires": { - "p-try": "^2.0.0" - } - }, - "p-locate": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-3.0.0.tgz", - "integrity": "sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==", - "requires": { - "p-limit": "^2.0.0" - } - }, - "p-try": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz", - "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==" - } - } - }, "pluralize": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/pluralize/-/pluralize-7.0.0.tgz", @@ -12321,14 +12183,6 @@ "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.3.1.tgz", "integrity": "sha512-cUGJnCdr4STbePCgqNFbpVNCepa+kAVohJs1sLhxzdH+gnEoOd8VhbYa7pD3zZYGiURWM2xzEII3fQcRizDkYQ==" }, - "typedarray-to-buffer": { - "version": "3.1.5", - "resolved": "https://registry.npmjs.org/typedarray-to-buffer/-/typedarray-to-buffer-3.1.5.tgz", - "integrity": "sha512-zdu8XMNEDepKKR+XYOXAVPtWui0ly0NtohUscw+UmaHiAWT8hrV1rr//H6V+0DvJ3OQ19S979M0laLfX8rm82Q==", - "requires": { - "is-typedarray": "^1.0.0" - } - }, "uglify-js": { "version": "3.4.9", "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.4.9.tgz", diff --git a/package.json b/package.json index 9612974..f2c36ec 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,6 @@ "fs-extra": "^7.0.1", "node-fetch": "^2.1.2", "sleep-promise": "^8.0.1", - "conf": "^5.0.0", "get-port": "^5.0.0" }, "devDependencies": { diff --git a/src/twig-renderer.js b/src/twig-renderer.js index 15862e4..2801d7e 100644 --- a/src/twig-renderer.js +++ b/src/twig-renderer.js @@ -5,7 +5,6 @@ import sleep from 'sleep-promise'; import fs from 'fs-extra'; import execa from 'execa'; import getPort from 'get-port'; -import Conf from 'conf'; import Ajv from 'ajv'; import { formatSchemaErrors, getAllFolders } from './utils'; import configSchema from '../config.schema'; @@ -35,7 +34,8 @@ class TwigRenderer { process.exit(1); } - this.configStore = new Conf(); + /** @type {Set} */ + this.portsUsed = new Set(); this.serverState = serverStates.STOPPED; this.inProgressRequests = 0; this.totalRequests = 0; @@ -44,7 +44,10 @@ class TwigRenderer { const isValid = validateSchemaAndAssignDefaults(this.config); if (!isValid) { const { errors } = validateSchemaAndAssignDefaults; - const msgs = ['Error: Please check config passed into TwigRenderer.', formatSchemaErrors(errors)].join('\n'); + const msgs = [ + 'Error: Please check config passed into TwigRenderer.', + formatSchemaErrors(errors), + ].join('\n'); console.error(msgs); if (process.env.NODE_ENV === 'testing') { process.exitCode = 1; @@ -61,7 +64,10 @@ class TwigRenderer { process.exitCode = 1; throw new Error(msg); } - this.config.relativeFrom = path.resolve(process.cwd(), this.config.relativeFrom); + this.config.relativeFrom = path.resolve( + process.cwd(), + this.config.relativeFrom, + ); } else { this.config.relativeFrom = process.cwd(); } @@ -70,7 +76,9 @@ class TwigRenderer { this.config.alterTwigEnv = this.config.alterTwigEnv.map((item) => { const isAbsolute = path.isAbsolute(item.file); return { - file: isAbsolute ? item.file : path.resolve(this.config.relativeFrom, item.file), + file: isAbsolute + ? item.file + : path.resolve(this.config.relativeFrom, item.file), functions: item.functions, }; }); @@ -78,7 +86,10 @@ class TwigRenderer { this.config = TwigRenderer.processPaths(this.config); // Writing this so `server--sync.php` can use - fs.writeFileSync(path.join(__dirname, 'shared-config.json'), JSON.stringify(this.config, null, ' ')); + fs.writeFileSync( + path.join(__dirname, 'shared-config.json'), + JSON.stringify(this.config, null, ' '), + ); } /** @@ -110,7 +121,10 @@ class TwigRenderer { if (namespaces) { namespaces = namespaces.map(namespace => ({ id: namespace.id, - paths: checkPaths(namespace.paths, { relativeFrom, recursive: namespace.recursive }), + paths: checkPaths(namespace.paths, { + relativeFrom, + recursive: namespace.recursive, + }), })); } @@ -143,16 +157,17 @@ class TwigRenderer { host: '127.0.0.1', // helps ensure the host being checked matches the PHP server being spun up }); + /* eslint-disable no-await-in-loop */ // pick another port if the one selected has already been taken - while (this.configStore.has(`ports.${portSelected}`)) { - // eslint-disable-next-line no-await-in-loop + while (this.portsUsed.has(portSelected)) { portSelected = await getPort({ host: '127.0.0.1', // helps ensure the host being checked matches the PHP server being spun up }); } + /* eslint-enable no-await-in-loop */ // remember which ports have been assigned to avoid giving out the same port twice - this.configStore.set(`ports.${portSelected}`, true); + this.portsUsed.add(portSelected); return portSelected; } @@ -179,8 +194,14 @@ class TwigRenderer { this.phpServerUrl = `http://127.0.0.1:${this.phpServerPort}`; // @todo Pass config to PHP server a better way than writing JSON file, then reading in PHP - const sharedConfigPath = path.join(__dirname, `shared-config--${this.phpServerPort}.json`); - await fs.writeFile(sharedConfigPath, JSON.stringify(this.config, null, ' ')); + const sharedConfigPath = path.join( + __dirname, + `shared-config--${this.phpServerPort}.json`, + ); + await fs.writeFile( + sharedConfigPath, + JSON.stringify(this.config, null, ' '), + ); const phpMemoryLimit = '4048M'; // @todo make user configurable const params = [ @@ -238,17 +259,19 @@ class TwigRenderer { async closeServer() { // console.log('checking if we can stop the server...'); if (this.config.keepAlive === false) { - if (this.completedRequests === this.totalRequests + if ( + this.completedRequests === this.totalRequests && this.inProgressRequests === 0 - && ( - this.serverState !== serverStates.STOPPING - || this.serverState !== serverStates.STOPPED - ) + && (this.serverState !== serverStates.STOPPING + || this.serverState !== serverStates.STOPPED) ) { this.stop(); } else { setTimeout(() => { - if (this.completedRequests === this.totalRequests && this.inProgressRequests === 0) { + if ( + this.completedRequests === this.totalRequests + && this.inProgressRequests === 0 + ) { this.stop(); } }, 300); @@ -282,8 +305,8 @@ class TwigRenderer { async checkServerWhileStarting() { while (this.serverState === serverStates.STARTING) { // console.log(`checkServerWhileStarting: ${this.serverState}`); - await this.checkIfServerIsReady(); // eslint-disable-line no-await-in-loop - await sleep(100); // eslint-disable-line no-await-in-loop + await this.checkIfServerIsReady(); + await sleep(100); } return this.serverState; } @@ -333,15 +356,17 @@ class TwigRenderer { } while (this.serverState !== serverStates.READY) { - await sleep(250); // eslint-disable-line no-await-in-loop + await sleep(250); } while (this.inProgressRequests > this.config.maxConcurrency) { - await sleep(250); // eslint-disable-line no-await-in-loop + await sleep(250); } if (this.config.verbose) { - console.log(`About to render & server on port ${this.phpServerPort} is ${this.serverState}`); + console.log( + `About to render & server on port ${this.phpServerPort} is ${this.serverState}`, + ); } const attempts = 3; @@ -356,7 +381,7 @@ class TwigRenderer { })}`; // @todo Fail if no response after X seconds - const res = await fetch(requestUrl, { // eslint-disable-line no-await-in-loop + const res = await fetch(requestUrl, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -369,12 +394,12 @@ class TwigRenderer { const warning = headers.get('Warning'); if (contentType === 'application/json') { - results = await res.json(); // eslint-disable-line no-await-in-loop + results = await res.json(); } else { results = { ok, message: warning, - html: await res.text(), // eslint-disable-line no-await-in-loop + html: await res.text(), }; } this.inProgressRequests -= 1; @@ -382,7 +407,13 @@ class TwigRenderer { if (this.config.verbose) { // console.log('vvvvvvvvvvvvvvv'); - console.log(`Render request received: Ok: ${ok ? 'true' : 'false'}, Status Code: ${status}, type: ${type}. ${body.template ? `template: ${body.template}` : ''}`); + console.log( + `Render request received: Ok: ${ + ok ? 'true' : 'false' + }, Status Code: ${status}, type: ${type}. ${ + body.template ? `template: ${body.template}` : '' + }`, + ); if (warning) { console.warn('Warning: ', warning); }