From 46f4552be2bfa62fef4a8246d1d75e7ecd740558 Mon Sep 17 00:00:00 2001 From: morgan557 Date: Sun, 1 Aug 2021 15:39:49 +0200 Subject: [PATCH 1/3] initial testing --- .eslintrc.yml | 1 + jest.setup.js | 10 +- package-lock.json | 228 ++++++++++++++++-- package.json | 5 +- src/index.js | 46 +++- .../file1.md | 7 +- .../remote-success-case-1-link/file1.txt | 1 + tests/index.test.js | 48 +++- 8 files changed, 307 insertions(+), 39 deletions(-) rename tests/fixtures/{remote-success-case => remote-success-case-1-link}/file1.md (64%) create mode 100644 tests/fixtures/remote-success-case-1-link/file1.txt diff --git a/.eslintrc.yml b/.eslintrc.yml index 78ad84a..d96b3c6 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -2,6 +2,7 @@ env: browser: true commonjs: true es2021: true + node: true parserOptions: ecmaVersion: 12 rules: {} diff --git a/jest.setup.js b/jest.setup.js index 0e068b9..40b4e4c 100644 --- a/jest.setup.js +++ b/jest.setup.js @@ -1,11 +1,13 @@ const { setupServer } = require('msw/node') const { rest } = require('msw') -const status = { - 'https://github.com/nodejs/undici': 204 +const status = { + 'https://github.com': 200, + 'https://gitlab.com': 201, + 'https://bitbucket.com': 204 } const endpoints = Object.entries(status) - .map(([url, statusCode]) => rest.get(url, (req, res, ctx) => res(ctx.status(statusCode)))) + .map(([url, statusCode]) => rest.get(url, (req, res, ctx) => res(ctx.status(statusCode)))) -global.mockRemoteServer = setupServer(...endpoints) +global.mockRemoteServer = setupServer(...endpoints) \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 8121ba0..5d0279b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -768,6 +768,11 @@ "integrity": "sha512-Aq58f5HiWdyDlFffbbSjAlv596h/cOnt2DO1w3DOC7OJ5EHs0hd/nycJfiu9RJbT6Yk6F1knnRRXNSpxoIVZ9Q==", "dev": true }, + "@sindresorhus/is": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-4.0.1.tgz", + "integrity": "sha512-Qm9hBEBu18wt1PO2flE7LPb30BHMQt1eQgbV76YntdNk73XZGpn3izvGTYxbGgzXKgbCjiia0uxTd3aTNQrY/g==" + }, "@sinonjs/commons": { "version": "1.8.3", "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-1.8.3.tgz", @@ -786,6 +791,14 @@ "@sinonjs/commons": "^1.7.0" } }, + "@szmarczak/http-timer": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/@szmarczak/http-timer/-/http-timer-4.0.6.tgz", + "integrity": "sha512-4BAffykYOgO+5nzBWYwE3W90sBgLJoUPRWWcL8wlyiM8IB8ipJz3UMJ9KXQd1RKQXpKp8Tutn80HZtWsu2u76w==", + "requires": { + "defer-to-connect": "^2.0.0" + } + }, "@tootallnate/once": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-1.1.2.tgz", @@ -833,6 +846,17 @@ "@babel/types": "^7.3.0" } }, + "@types/cacheable-request": { + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/@types/cacheable-request/-/cacheable-request-6.0.2.tgz", + "integrity": "sha512-B3xVo+dlKM6nnKTcmm5ZtY/OL8bOAOd2Olee9M1zft65ox50OzjEHW91sDiU9j6cvW8Ejg1/Qkf4xd2kugApUA==", + "requires": { + "@types/http-cache-semantics": "*", + "@types/keyv": "*", + "@types/node": "*", + "@types/responselike": "*" + } + }, "@types/cookie": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/@types/cookie/-/cookie-0.4.1.tgz", @@ -848,6 +872,11 @@ "@types/node": "*" } }, + "@types/http-cache-semantics": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz", + "integrity": "sha512-SZs7ekbP8CN0txVG2xVRH6EgKmEm31BOxA07vkFaETzZz1xh+cbt8BcI0slpymvwhx5dlFnQG2rTlPVQn+iRPQ==" + }, "@types/inquirer": { "version": "7.3.3", "resolved": "https://registry.npmjs.org/@types/inquirer/-/inquirer-7.3.3.tgz", @@ -894,11 +923,18 @@ "integrity": "sha512-YSBPTLTVm2e2OoQIDYx8HaeWJ5tTToLH67kXR7zYNGupXMEHa2++G8k+DczX2cFVgalypqtyZIcU19AFcmOpmg==", "dev": true }, + "@types/keyv": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/@types/keyv/-/keyv-3.1.2.tgz", + "integrity": "sha512-/FvAK2p4jQOaJ6CGDHJTqZcUtbZe820qIeTg7o0Shg7drB4JHeL+V/dhSaly7NXx6u8eSee+r7coT+yuJEvDLg==", + "requires": { + "@types/node": "*" + } + }, "@types/node": { "version": "16.4.8", "resolved": "https://registry.npmjs.org/@types/node/-/node-16.4.8.tgz", - "integrity": "sha512-VL7RZyCpfYEmbyd3/Eq5RNYhZt7yoL1JThZQ3KzimzhLya2Qa86U1ZZmioNWAAjiz99z1ED1xF9NUV2srvfVrA==", - "dev": true + "integrity": "sha512-VL7RZyCpfYEmbyd3/Eq5RNYhZt7yoL1JThZQ3KzimzhLya2Qa86U1ZZmioNWAAjiz99z1ED1xF9NUV2srvfVrA==" }, "@types/parse-json": { "version": "4.0.0", @@ -912,6 +948,14 @@ "integrity": "sha512-eI5Yrz3Qv4KPUa/nSIAi0h+qX0XyewOliug5F2QAtuRg6Kjg6jfmxe1GIwoIRhZspD1A0RP8ANrPwvEXXtRFog==", "dev": true }, + "@types/responselike": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@types/responselike/-/responselike-1.0.0.tgz", + "integrity": "sha512-85Y2BjiufFzaMIlvJDvTTB8Fxl2xfLo4HgmHzVBz08w4wDePCTjYw66PdrolO0kzli3yam/YCgRufyo1DdQVTA==", + "requires": { + "@types/node": "*" + } + }, "@types/set-cookie-parser": { "version": "2.4.1", "resolved": "https://registry.npmjs.org/@types/set-cookie-parser/-/set-cookie-parser-2.4.1.tgz", @@ -1221,8 +1265,7 @@ "balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", - "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", - "dev": true + "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" }, "base64-js": { "version": "1.5.1", @@ -1251,7 +1294,6 @@ "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", - "dev": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -1310,6 +1352,35 @@ "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", "dev": true }, + "cacheable-lookup": { + "version": "5.0.4", + "resolved": "https://registry.npmjs.org/cacheable-lookup/-/cacheable-lookup-5.0.4.tgz", + "integrity": "sha512-2/kNscPhpcxrOigMZzbiWF7dz8ilhb/nIHU3EyZiXWXpeq/au8qJ8VhdftMkty3n7Gj6HIGalQG8oiBNB3AJgA==" + }, + "cacheable-request": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-7.0.2.tgz", + "integrity": "sha512-pouW8/FmiPQbuGpkXQ9BAPv/Mo5xDGANgSNXzTzJ8DrKGuXOssM4wIQRjfanNRh3Yu5cfYPvcorqbhg2KIJtew==", + "requires": { + "clone-response": "^1.0.2", + "get-stream": "^5.1.0", + "http-cache-semantics": "^4.0.0", + "keyv": "^4.0.0", + "lowercase-keys": "^2.0.0", + "normalize-url": "^6.0.1", + "responselike": "^2.0.0" + }, + "dependencies": { + "get-stream": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-5.2.0.tgz", + "integrity": "sha512-nBF+F1rAZVCu/p7rjzgA+Yb4lfYXrpl7a6VmJrU8wF9I1CKvP/QwPNZHnOlwbTkY6dvtFIzFMSyQXbLoTQPRpA==", + "requires": { + "pump": "^3.0.0" + } + } + } + }, "callsites": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/callsites/-/callsites-3.1.0.tgz", @@ -1457,6 +1528,14 @@ "integrity": "sha1-2jCcwmPfFZlMaIypAheco8fNfH4=", "dev": true }, + "clone-response": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/clone-response/-/clone-response-1.0.2.tgz", + "integrity": "sha1-0dyXOSAxTfZ/vrlCI7TuNQI56Ws=", + "requires": { + "mimic-response": "^1.0.0" + } + }, "co": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", @@ -1502,8 +1581,7 @@ "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", - "dev": true + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" }, "convert-source-map": { "version": "1.8.0", @@ -1671,6 +1749,21 @@ "integrity": "sha512-V0pfhfr8suzyPGOx3nmq4aHqabehUZn6Ch9kyFpV79TGDTWFmHqUqXdabR7QHqxzrYolF4+tVmJhUG4OURg5dQ==", "dev": true }, + "decompress-response": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/decompress-response/-/decompress-response-6.0.0.tgz", + "integrity": "sha512-aW35yZM6Bb/4oJlZncMH2LCoZtJXTRxES17vE3hoRiowU2kWHaJKFkSBDnDR+cm9J+9QhXmREyIfv0pji9ejCQ==", + "requires": { + "mimic-response": "^3.1.0" + }, + "dependencies": { + "mimic-response": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-3.1.0.tgz", + "integrity": "sha512-z0yWI+4FDrrweS8Zmt4Ej5HdJmky15+L2e6Wgn3+iK5fWzb6T3fhNFq2+MeTRb064c6Wr4N/wv0DzQTjNzHNGQ==" + } + } + }, "dedent": { "version": "0.7.0", "resolved": "https://registry.npmjs.org/dedent/-/dedent-0.7.0.tgz", @@ -1698,6 +1791,11 @@ "clone": "^1.0.2" } }, + "defer-to-connect": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/defer-to-connect/-/defer-to-connect-2.0.1.tgz", + "integrity": "sha512-4tvttepXG1VaYGrRibk5EwJd1t4udunSOVMdLSAL6mId1ix438oPwPZMALY41FCijukO1L0twNcGsdzS7dHgDg==" + }, "delayed-stream": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", @@ -1769,6 +1867,14 @@ "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", "dev": true }, + "end-of-stream": { + "version": "1.4.4", + "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.4.tgz", + "integrity": "sha512-+uw1inIHVPQoaVuHzRyXd21icM+cnt4CzD5rW+NC1wjOUSTOs+Te7FOv7AhN7vS9x/oIyhLP5PR1H+phQAHu5Q==", + "requires": { + "once": "^1.4.0" + } + }, "enquirer": { "version": "2.3.6", "resolved": "https://registry.npmjs.org/enquirer/-/enquirer-2.3.6.tgz", @@ -2244,8 +2350,7 @@ "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", - "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", - "dev": true + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" }, "fsevents": { "version": "2.3.2", @@ -2294,7 +2399,6 @@ "version": "7.1.7", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.7.tgz", "integrity": "sha512-OvD9ENzPLbegENnYP5UUfJIirTg4+XwMWGaQfQTY0JenxNvvIKP3U3/tAQSPIu/lHxXYSZmpXlUHeqAIdKzBLQ==", - "dev": true, "requires": { "fs.realpath": "^1.0.0", "inflight": "^1.0.4", @@ -2344,6 +2448,24 @@ } } }, + "got": { + "version": "11.8.2", + "resolved": "https://registry.npmjs.org/got/-/got-11.8.2.tgz", + "integrity": "sha512-D0QywKgIe30ODs+fm8wMZiAcZjypcCodPNuMz5H9Mny7RJ+IjJ10BdmGW7OM7fHXP+O7r6ZwapQ/YQmMSvB0UQ==", + "requires": { + "@sindresorhus/is": "^4.0.0", + "@szmarczak/http-timer": "^4.0.5", + "@types/cacheable-request": "^6.0.1", + "@types/responselike": "^1.0.0", + "cacheable-lookup": "^5.0.3", + "cacheable-request": "^7.0.1", + "decompress-response": "^6.0.0", + "http2-wrapper": "^1.0.0-beta.5.2", + "lowercase-keys": "^2.0.0", + "p-cancelable": "^2.0.0", + "responselike": "^2.0.0" + } + }, "graceful-fs": { "version": "4.2.6", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.6.tgz", @@ -2392,6 +2514,11 @@ "integrity": "sha512-H2iMtd0I4Mt5eYiapRdIDjp+XzelXQ0tFE4JS7YFwFevXXMmOp9myNrUvCg0D6ws8iqkRPBfKHgbwig1SmlLfg==", "dev": true }, + "http-cache-semantics": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/http-cache-semantics/-/http-cache-semantics-4.1.0.tgz", + "integrity": "sha512-carPklcUh7ROWRK7Cv27RPtdhYhUsela/ue5/jKzjegVvXDqM2ILE9Q2BGn9JZJh1g87cp56su/FgQSzcWS8cQ==" + }, "http-proxy-agent": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/http-proxy-agent/-/http-proxy-agent-4.0.1.tgz", @@ -2403,6 +2530,15 @@ "debug": "4" } }, + "http2-wrapper": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/http2-wrapper/-/http2-wrapper-1.0.3.tgz", + "integrity": "sha512-V+23sDMr12Wnz7iTcDeJr3O6AIxlnvT/bmaAAAP/Xda35C90p9599p0F1eHR/N1KILWSoWVAiOMFjBBXaXSMxg==", + "requires": { + "quick-lru": "^5.1.1", + "resolve-alpn": "^1.0.0" + } + }, "https-proxy-agent": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-5.0.0.tgz", @@ -2470,7 +2606,6 @@ "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", - "dev": true, "requires": { "once": "^1.3.0", "wrappy": "1" @@ -2479,8 +2614,7 @@ "inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", - "dev": true + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, "inquirer": { "version": "8.1.2", @@ -3338,6 +3472,11 @@ "integrity": "sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA==", "dev": true }, + "json-buffer": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz", + "integrity": "sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==" + }, "json-parse-even-better-errors": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/json-parse-even-better-errors/-/json-parse-even-better-errors-2.3.1.tgz", @@ -3365,6 +3504,14 @@ "minimist": "^1.2.5" } }, + "keyv": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.0.3.tgz", + "integrity": "sha512-zdGa2TOpSZPq5mU6iowDARnMBZgtCqJ11dJROFi6tg6kTn4nuUdU09lFyLFSaHrWqpIJ+EBq4E8/Dc0Vx5vLdA==", + "requires": { + "json-buffer": "3.0.1" + } + }, "kleur": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/kleur/-/kleur-3.0.3.tgz", @@ -3436,6 +3583,11 @@ "is-unicode-supported": "^0.1.0" } }, + "lowercase-keys": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-2.0.0.tgz", + "integrity": "sha512-tqNXrS78oMOE73NMxK4EMLQsQowWf8jKooH9g7xPavRT706R6bkQJ6DY2Te7QukaZsulxa30wQ7bk0pm4XiHmA==" + }, "lru-cache": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", @@ -3514,11 +3666,15 @@ "integrity": "sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==", "dev": true }, + "mimic-response": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-1.0.1.tgz", + "integrity": "sha512-j5EctnkH7amfV/q5Hgmoal1g2QHFJRraOtmx0JpIqkxhBhI/lJSl1nMpQ45hVarwNETOoWEimndZ4QK0RHxuxQ==" + }, "minimatch": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", - "dev": true, "requires": { "brace-expansion": "^1.1.7" } @@ -3634,6 +3790,11 @@ "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", "dev": true }, + "normalize-url": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/normalize-url/-/normalize-url-6.1.0.tgz", + "integrity": "sha512-DlL+XwOy3NxAQ8xuC0okPgK46iuVNAK01YN7RueYBqqFeGsBjV9XmCAzAdgt+667bCl5kPh9EqKKDwnaPG1I7A==" + }, "npm-run-path": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-4.0.1.tgz", @@ -3653,7 +3814,6 @@ "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", - "dev": true, "requires": { "wrappy": "1" } @@ -3710,6 +3870,11 @@ "integrity": "sha512-2+ax7EEnAxi+g+HN4VrFE5qfLnCb7OQJiJ2LS4K+PQ6lTeHbLpF/AvVTilt7FnTCQuXksQI7lPV1C91oOKeKXg==", "dev": true }, + "p-cancelable": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/p-cancelable/-/p-cancelable-2.1.1.tgz", + "integrity": "sha512-BZOr3nRQHOntUjTrH8+Lh54smKHoHyur8We1V8DSMVrl5A2malOOwuJRnKRDjSnkoeBh4at6BwEnb5I7Jl31wg==" + }, "p-each-series": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/p-each-series/-/p-each-series-2.2.0.tgz", @@ -3776,8 +3941,7 @@ "path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", - "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", - "dev": true + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" }, "path-key": { "version": "3.1.1", @@ -3869,6 +4033,15 @@ "integrity": "sha512-RIdOzyoavK+hA18OGGWDqUTsCLhtA7IcZ/6NCs4fFJaHBDab+pDDmDIByWFRQJq2Cd7r1OoQxBGKOaztq+hjIQ==", "dev": true }, + "pump": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz", + "integrity": "sha512-LwZy+p3SFs1Pytd/jYct4wpv49HiYCqd9Rlc5ZVdk0V+8Yzv6jR5Blk3TRmPL1ft69TxP0IMZGJ+WPFU2BFhww==", + "requires": { + "end-of-stream": "^1.1.0", + "once": "^1.3.1" + } + }, "punycode": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", @@ -3881,6 +4054,11 @@ "integrity": "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==", "dev": true }, + "quick-lru": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/quick-lru/-/quick-lru-5.1.1.tgz", + "integrity": "sha512-WuyALRjWPDGtt/wzJiadO5AXY+8hZ80hVpe6MyivgraREW751X3SbhRvG3eLKOYN+8VEvqLcf3wdnt44Z4S4SA==" + }, "react-is": { "version": "17.0.2", "resolved": "https://registry.npmjs.org/react-is/-/react-is-17.0.2.tgz", @@ -3935,6 +4113,11 @@ "path-parse": "^1.0.6" } }, + "resolve-alpn": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/resolve-alpn/-/resolve-alpn-1.2.0.tgz", + "integrity": "sha512-e4FNQs+9cINYMO5NMFc6kOUCdohjqFPSgMuwuZAOUWqrfWsen+Yjy5qZFkV5K7VO7tFSLKcUL97olkED7sCBHA==" + }, "resolve-cwd": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/resolve-cwd/-/resolve-cwd-3.0.0.tgz", @@ -3958,6 +4141,14 @@ "integrity": "sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==", "dev": true }, + "responselike": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/responselike/-/responselike-2.0.0.tgz", + "integrity": "sha512-xH48u3FTB9VsZw7R+vvgaKeLKzT6jOogbQhEe/jewwnZgzPcnyWui2Av6JpoYZF/91uueC+lqhWqeURw5/qhCw==", + "requires": { + "lowercase-keys": "^2.0.0" + } + }, "restore-cursor": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/restore-cursor/-/restore-cursor-3.1.0.tgz", @@ -4611,8 +4802,7 @@ "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", - "dev": true + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" }, "write-file-atomic": { "version": "3.0.3", diff --git a/package.json b/package.json index 50c3cf8..abc9ef9 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,10 @@ "jest-runner-eslint": "^0.10.1", "msw": "^0.33.2" }, - "dependencies": {}, + "dependencies": { + "glob": "^7.1.7", + "got": "^11.8.2" + }, "repository": { "type": "git", "url": "git+https://github.com/restqa/404-links.git" diff --git a/src/index.js b/src/index.js index 5ee35d9..842689e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,6 @@ +const fs = require('fs'); +const glob = require("glob"); +const got = require('got'); /** * Instanciate 404-links * @@ -19,9 +22,42 @@ * ] * } * - * const notFound = new NotFoundLinks(options) - * console.log(result) + * const notFound = NotFoundLinks(options) */ -module.exports = function (options) { - throw new Error(`The folder "${options.folder}" doesn't exist, please share an existing folder.`) -} +module.exports = function(options) { + if (options && fs.existsSync(options.folder) == false) { + throw new Error(`The folder "${options.folder}" doesn't exist, please share an existing folder.`) + } + if (!options['stream']) { + throw new Error('The options stream is not defined, please pass a valid Writable stream.') + } + + const arr = glob.sync(`${options.folder}/**/*.md`) + arr.forEach(item => { + const maString = fs.readFileSync(item, 'utf-8') + const resultat = maString.match(/\(((https|http){1}.*?)\)/gm) + resultat.forEach(item => { + item = item.replace('(', '') + item = item.replace(')', '') + got(item).then((value) => { + const obj = { + url: null, + status: null, + passed: null + } + obj.status = value.statusCode; + obj.url = item; + if (value.statusCode === 200) { + obj.passed = true + } else { + obj.passed = false + } + options.stream.write(Buffer.from(JSON.stringify(obj))) + options.stream.end() + }) + }) + }) + + //options.stream.write(Buffer.from(JSON.stringify({ foo: 'bar' }))) + //options.stream.end() +} \ No newline at end of file diff --git a/tests/fixtures/remote-success-case/file1.md b/tests/fixtures/remote-success-case-1-link/file1.md similarity index 64% rename from tests/fixtures/remote-success-case/file1.md rename to tests/fixtures/remote-success-case-1-link/file1.md index 2567081..f888f07 100644 --- a/tests/fixtures/remote-success-case/file1.md +++ b/tests/fixtures/remote-success-case-1-link/file1.md @@ -2,10 +2,7 @@ We are now using a feature file that si containing a list of valid links: -## 3 remote links +## 1 remote links -- [] +- [Github Link](https://github.com) -## 2 locals links - -- [] diff --git a/tests/fixtures/remote-success-case-1-link/file1.txt b/tests/fixtures/remote-success-case-1-link/file1.txt new file mode 100644 index 0000000..5640a69 --- /dev/null +++ b/tests/fixtures/remote-success-case-1-link/file1.txt @@ -0,0 +1 @@ +- [Not used Link](https://github.com/not-used) \ No newline at end of file diff --git a/tests/index.test.js b/tests/index.test.js index 3a0575a..a5656c6 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -1,15 +1,53 @@ const NotFoundLinks = require('../src') +const { Writable } = require('stream') +const path = require('path') beforeAll(() => mockRemoteServer.listen()) afterEach(() => mockRemoteServer.resetHandlers()) afterAll(() => mockRemoteServer.close()) -test('throw an error if the options pass doesn\'t contains a valid folder', () => { - expect(() => { +test('throw an error if the options folder doesn\'t contains a valid folder', () => { + expect(() => { + const options = { + folder: '/fooooooo' + } + NotFoundLinks(options) + }).toThrow('The folder "/fooooooo" doesn\'t exist, please share an existing folder.') +}) + +test('throw an error if the options stream doesn\'t contains a valid Stream', () => { + expect(() => { + const options = { + folder: process.cwd() + } + NotFoundLinks(options) + }).toThrow('The options stream is not defined, please pass a valid Writable stream.') + expect(() => { + const options = { + folder: process.cwd(), + stream: undefined + } + NotFoundLinks(options) + }).toThrow('The options stream is not defined, please pass a valid Writable stream.') +}) + +test('Get the sucessful result of the remote calls', (done) => { + const opt = { + write: (chunk, _, done) => { + expect(JSON.parse(chunk.toString())).toEqual({ + url: 'https://github.com', + status: 200, + passed: true + }) + done() + } + } + const testStream = new Writable(opt) + testStream.on('finish', done) const options = { - folder: '/fooooooo' + folder: path.resolve(__dirname, 'fixtures/remote-success-case-1-link'), + stream: testStream } NotFoundLinks(options) - }).toThrow('The folder "/fooooooo" doesn\'t exist, please share an existing folder.') -}) +}) \ No newline at end of file From cbd4cf680ba4d26386087ba95eebded39f7bcc5a Mon Sep 17 00:00:00 2001 From: morgan557 Date: Sun, 29 Aug 2021 16:31:11 +0200 Subject: [PATCH 2/3] add 2 tests --- .eslintrc.yml | 3 +- jest.setup.js | 6 +- package.json | 136 +++++----- src/index.js | 65 +++-- .../fixtures/remote-duplicate-links/file3.md | 16 ++ .../remote-invalid-case-3-links/file3.md | 9 + .../remote-invalid-case-4-links/file4.md | 11 + .../remote-multiple-files/file-error.md | 8 + .../remote-multiple-files/file-success.md | 15 ++ .../remote-success-case-2-links/file2.md | 9 + tests/index.test.js | 252 +++++++++++++++++- 11 files changed, 435 insertions(+), 95 deletions(-) create mode 100644 tests/fixtures/remote-duplicate-links/file3.md create mode 100644 tests/fixtures/remote-invalid-case-3-links/file3.md create mode 100644 tests/fixtures/remote-invalid-case-4-links/file4.md create mode 100644 tests/fixtures/remote-multiple-files/file-error.md create mode 100644 tests/fixtures/remote-multiple-files/file-success.md create mode 100644 tests/fixtures/remote-success-case-2-links/file2.md diff --git a/.eslintrc.yml b/.eslintrc.yml index d96b3c6..85f7d26 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -5,7 +5,8 @@ env: node: true parserOptions: ecmaVersion: 12 -rules: {} +rules: + no-done-callback: off extends: - eslint:recommended - plugin:jest/style diff --git a/jest.setup.js b/jest.setup.js index 40b4e4c..1a80ccb 100644 --- a/jest.setup.js +++ b/jest.setup.js @@ -4,7 +4,11 @@ const { rest } = require('msw') const status = { 'https://github.com': 200, 'https://gitlab.com': 201, - 'https://bitbucket.com': 204 + 'https://bitbucket.com': 204, + 'https://broken.com/test': 404, + 'https://broken.com': 403, + 'https://gittlab.com': 500, + 'https://ggithub.com': 401 } const endpoints = Object.entries(status) diff --git a/package.json b/package.json index abc9ef9..a7b11bb 100644 --- a/package.json +++ b/package.json @@ -1,74 +1,72 @@ { - "name": "@restqa/404-links", - "version": "2.0.0", - "description": "A simple link checker, no more broken link in your project documentation", - "main": "index.js", - "scripts": { - "serve:dev": "nodemon server.js", - "test": "jest", - "test:watch": "jest --watch", - "test:coverage": "jest --coverage" - }, - "author": "olivier rodomond", - "license": "MIT", - "devDependencies": { - "eslint": "^7.32.0", - "eslint-plugin-jest": "^24.4.0", - "eslint-plugin-node": "^11.1.0", - "jest": "^27.0.6", - "jest-runner-eslint": "^0.10.1", - "msw": "^0.33.2" - }, - "dependencies": { - "glob": "^7.1.7", - "got": "^11.8.2" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/restqa/404-links.git" - }, - "keywords": [ - "qa", - "spellcheck", - "automation", - "opensource" - ], - "bugs": { - "url": "https://github.com/restqa/404-links/issues" - }, - "homepage": "https://github.com/restqa/404-links#readme", - "jest": { - "verbose": true, - "watchPlugins": [ - "jest-runner-eslint/watch-fix" + "name": "@restqa/404-links", + "version": "2.0.0", + "description": "A simple link checker, no more broken link in your project documentation", + "main": "index.js", + "scripts": { + "serve:dev": "nodemon server.js", + "test": "jest", + "test:watch": "jest --watch", + "test:coverage": "jest --coverage" + }, + "author": "olivier rodomond", + "license": "MIT", + "devDependencies": { + "eslint": "^7.32.0", + "eslint-plugin-jest": "^24.4.0", + "eslint-plugin-node": "^11.1.0", + "jest": "^27.0.6", + "jest-runner-eslint": "^0.10.1", + "msw": "^0.33.2" + }, + "dependencies": { + "glob": "^7.1.7", + "got": "^11.8.2" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/restqa/404-links.git" + }, + "keywords": [ + "qa", + "spellcheck", + "automation", + "opensource" ], - "moduleFileExtensions": [ - "js" - ], - "projects": [ - { - "displayName": "test", - "setupFilesAfterEnv": [ - "/jest.setup.js" + "bugs": { + "url": "https://github.com/restqa/404-links/issues" + }, + "homepage": "https://github.com/restqa/404-links#readme", + "jest": { + "verbose": true, + "watchPlugins": [ + "jest-runner-eslint/watch-fix" ], - "testMatch": [ - "/tests/**/*.test.js" - ] - }, - { - "runner": "jest-runner-eslint", - "displayName": "lint", - "testMatch": [ - "/bin/404-links", - "/src/**/*.js", - "/tests/**/*.js" + "moduleFileExtensions": [ + "js" + ], + "projects": [{ + "displayName": "test", + "setupFilesAfterEnv": [ + "/jest.setup.js" + ], + "testMatch": [ + "/tests/**/*.test.js" + ] + }, + { + "runner": "jest-runner-eslint", + "displayName": "lint", + "testMatch": [ + "/bin/404-links", + "/src/**/*.js" + ] + } ] - } - ] - }, - "jest-runner-eslint": { - "cliOptions": { - "fix": true + }, + "jest-runner-eslint": { + "cliOptions": { + "fix": true + } } - } -} +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 842689e..c3437f9 100644 --- a/src/index.js +++ b/src/index.js @@ -33,31 +33,50 @@ module.exports = function(options) { } const arr = glob.sync(`${options.folder}/**/*.md`) + let arr1 = []; arr.forEach(item => { const maString = fs.readFileSync(item, 'utf-8') - const resultat = maString.match(/\(((https|http){1}.*?)\)/gm) - resultat.forEach(item => { - item = item.replace('(', '') - item = item.replace(')', '') - got(item).then((value) => { - const obj = { - url: null, - status: null, - passed: null - } - obj.status = value.statusCode; - obj.url = item; - if (value.statusCode === 200) { - obj.passed = true - } else { - obj.passed = false - } - options.stream.write(Buffer.from(JSON.stringify(obj))) - options.stream.end() - }) - }) + let resultat = maString.match(/\(((https|http){1}.*?)\)/gm) + resultat = [...new Set(resultat)]; + arr1 = arr1.concat(resultat); + }) + var promiseList = arr1.map((item) => { + item = item.replace('(', '') + item = item.replace(')', '') + return got(item); + }) - //options.stream.write(Buffer.from(JSON.stringify({ foo: 'bar' }))) - //options.stream.end() + Promise.allSettled(promiseList).then((results) => { + try { + results.forEach(item => { + const obj = { + url: null, + status: null, + passed: null + } + + if (item.status === 'fulfilled') { + obj.status = item.value.statusCode; + obj.url = item.value.url; + obj.passed = true + arr.push(item.value.statusCode) + } else if (item.status === 'rejected') { + obj.status = item.reason.response.statusCode; + obj.passed = false + obj.url = item.reason.response.url; + arr.push(item.reason.response.statusCode) + } + options.stream.write(Buffer.from(JSON.stringify(obj))) + }) + } catch (e) { + console.log(e); + } + options.stream.end() + }) + .catch((err) => { + console.log(err); + }) + + } \ No newline at end of file diff --git a/tests/fixtures/remote-duplicate-links/file3.md b/tests/fixtures/remote-duplicate-links/file3.md new file mode 100644 index 0000000..8e4eaf7 --- /dev/null +++ b/tests/fixtures/remote-duplicate-links/file3.md @@ -0,0 +1,16 @@ +# Hello world project + +We are now using a feature file that si containing a list of valid links: + +## 2 remote links + +- [Github Link](https://github.com) +- [Github Link](https://github.com) +- [Github Link](https://github.com) +- [Github Link](https://github.com) +- [Github Link](https://github.com) +- [Github Link](https://github.com) +- [Github Link](https://github.com) +- [Github Link](https://github.com) +- [Gitlab Link](https://gitlab.com) +- [broken Link](https://broken.com/test) \ No newline at end of file diff --git a/tests/fixtures/remote-invalid-case-3-links/file3.md b/tests/fixtures/remote-invalid-case-3-links/file3.md new file mode 100644 index 0000000..10de239 --- /dev/null +++ b/tests/fixtures/remote-invalid-case-3-links/file3.md @@ -0,0 +1,9 @@ +# Hello world project + +We are now using a feature file that si containing a list of valid links: + +## 2 remote links + +- [Github Link](https://github.com) +- [Gitlab Link](https://gitlab.com) +- [broken Link](https://broken.com/test) \ No newline at end of file diff --git a/tests/fixtures/remote-invalid-case-4-links/file4.md b/tests/fixtures/remote-invalid-case-4-links/file4.md new file mode 100644 index 0000000..eef5169 --- /dev/null +++ b/tests/fixtures/remote-invalid-case-4-links/file4.md @@ -0,0 +1,11 @@ +# Hello world project + +We are now using a feature file that si containing a list of valid links: + +## 2 remote links + +- [Ggithub Link](https://ggithub.com) +- [Gittlab Link](https://gittlab.com) +- [broken Link test path](https://broken.com/test) +- [broken Link](https://broken.com) + diff --git a/tests/fixtures/remote-multiple-files/file-error.md b/tests/fixtures/remote-multiple-files/file-error.md new file mode 100644 index 0000000..7645a8d --- /dev/null +++ b/tests/fixtures/remote-multiple-files/file-error.md @@ -0,0 +1,8 @@ +# Hello world project + +We are now using a feature file that si containing a list of valid links: + +## 2 remote links + +- [broken Link test path](https://broken.com/test) +- [broken Link](https://broken.com) \ No newline at end of file diff --git a/tests/fixtures/remote-multiple-files/file-success.md b/tests/fixtures/remote-multiple-files/file-success.md new file mode 100644 index 0000000..438b1fb --- /dev/null +++ b/tests/fixtures/remote-multiple-files/file-success.md @@ -0,0 +1,15 @@ +# Hello world project + +We are now using a feature file that si containing a list of valid links: + +## 2 remote links + +- [Github Link](https://github.com) +- [Github Link](https://github.com) +- [Github Link](https://github.com) +- [Github Link](https://github.com) +- [Github Link](https://github.com) +- [Github Link](https://github.com) +- [Github Link](https://github.com) +- [Github Link](https://github.com) +- [Gitlab Link](https://gitlab.com) \ No newline at end of file diff --git a/tests/fixtures/remote-success-case-2-links/file2.md b/tests/fixtures/remote-success-case-2-links/file2.md new file mode 100644 index 0000000..f845086 --- /dev/null +++ b/tests/fixtures/remote-success-case-2-links/file2.md @@ -0,0 +1,9 @@ +# Hello world project + +We are now using a feature file that si containing a list of valid links: + +## 2 remote links + +- [Github Link](https://github.com) +- [Gitlab Link](https://gitlab.com) + diff --git a/tests/index.test.js b/tests/index.test.js index a5656c6..7926018 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -36,7 +36,7 @@ test('Get the sucessful result of the remote calls', (done) => { const opt = { write: (chunk, _, done) => { expect(JSON.parse(chunk.toString())).toEqual({ - url: 'https://github.com', + url: 'https://github.com/', status: 200, passed: true }) @@ -50,4 +50,254 @@ test('Get the sucessful result of the remote calls', (done) => { stream: testStream } NotFoundLinks(options) +}) + +test('Get 2 sucessful result of the remote calls (status code 200/201)', (done) => { + const results = [] + const opt = { + write: (chunk, _, cb) => { + results.push(JSON.parse(chunk.toString())) + cb() + } + } + const testStream = new Writable(opt) + testStream.on('finish', () => { + try { + expect(results[0]).toEqual({ + url: 'https://github.com/', + status: 200, + passed: true + }) + expect(results[1]).toEqual({ + url: 'https://gitlab.com/', + status: 201, + passed: true + }) + done() + } catch (err) { + done(err) + } + }) + testStream.on('error', (err) => { + done(err) + }) + const options = { + folder: path.resolve(__dirname, 'fixtures/remote-success-case-2-links'), + stream: testStream + } + NotFoundLinks(options) +}) + +test('Get 2 sucessful result of the remote calls (status code 200/201)', (done) => { + const results = [] + const opt = { + write: (chunk, _, cb) => { + results.push(JSON.parse(chunk.toString())) + cb() + } + } + const testStream = new Writable(opt) + testStream.on('finish', () => { + try { + expect(results[0]).toEqual({ + url: 'https://github.com/', + status: 200, + passed: true + }) + expect(results[1]).toEqual({ + url: 'https://gitlab.com/', + status: 201, + passed: true + }) + done() + } catch (err) { + done(err) + } + }) + testStream.on('error', (err) => { + done(err) + }) + const options = { + folder: path.resolve(__dirname, 'fixtures/remote-success-case-2-links'), + stream: testStream + } + NotFoundLinks(options) +}) + +test('Get 2 sucessful result and 1 broken of the remote calls (status code 200/201/404)', (done) => { + const results = [] + const opt = { + write: (chunk, _, cb) => { + results.push(JSON.parse(chunk.toString())) + cb() + } + } + const testStream = new Writable(opt) + testStream.on('finish', () => { + try { + expect(results[0]).toEqual({ + url: 'https://github.com/', + status: 200, + passed: true + }) + expect(results[1]).toEqual({ + url: 'https://gitlab.com/', + status: 201, + passed: true + }) + expect(results[2]).toEqual({ + url: 'https://broken.com/test', + status: 404, + passed: false + }) + done() + } catch (err) { + done(err) + } + }) + testStream.on('error', (err) => { + done(err) + }) + const options = { + folder: path.resolve(__dirname, 'fixtures/remote-invalid-case-3-links'), + stream: testStream + } + NotFoundLinks(options) +}) + +test('Get only 4 broken of the remote calls (status code 404/ 500/ 403 /401)', (done) => { + const results = [] + const opt = { + write: (chunk, _, cb) => { + results.push(JSON.parse(chunk.toString())) + cb() + } + } + const testStream = new Writable(opt) + testStream.on('finish', () => { + try { + expect(results[0]).toEqual({ + url: 'https://ggithub.com/', + status: 401, + passed: false + }) + expect(results[1]).toEqual({ + url: 'https://gittlab.com/', + status: 500, + passed: false + }) + expect(results[2]).toEqual({ + url: 'https://broken.com/test', + status: 404, + passed: false + }) + expect(results[3]).toEqual({ + url: 'https://broken.com/', + status: 403, + passed: false + }) + done() + } catch (err) { + done(err) + } + }) + testStream.on('error', (err) => { + done(err) + }) + const options = { + folder: path.resolve(__dirname, 'fixtures/remote-invalid-case-4-links'), + stream: testStream + } + NotFoundLinks(options) +}) + +test('Duplicate - Get 2 sucessful result and 1 broken of the remote calls', (done) => { + const results = [] + const opt = { + write: (chunk, _, cb) => { + results.push(JSON.parse(chunk.toString())) + cb() + } + } + const testStream = new Writable(opt) + testStream.on('finish', () => { + try { + expect(results.length).toEqual(3) + expect(results[0]).toEqual({ + url: 'https://github.com/', + status: 200, + passed: true + }) + expect(results[1]).toEqual({ + url: 'https://gitlab.com/', + status: 201, + passed: true + }) + expect(results[2]).toEqual({ + url: 'https://broken.com/test', + status: 404, + passed: false + }) + done() + } catch (err) { + done(err) + } + }) + testStream.on('error', (err) => { + done(err) + }) + const options = { + folder: path.resolve(__dirname, 'fixtures/remote-duplicate-links'), + stream: testStream + } + NotFoundLinks(options) +}) + +test('Duplicate and multiple files - Get 2 sucessful result and 2 broken of the remote calls', (done) => { + const results = [] + const opt = { + write: (chunk, _, cb) => { + results.push(JSON.parse(chunk.toString())) + cb() + } + } + const testStream = new Writable(opt) + testStream.on('finish', () => { + try { + expect(results.length).toEqual(4) + expect(results[0]).toEqual({ + url: 'https://broken.com/test', + status: 404, + passed: false + }) + expect(results[1]).toEqual({ + url: 'https://broken.com/', + status: 403, + passed: false + }) + expect(results[2]).toEqual({ + url: 'https://github.com/', + status: 200, + passed: true + }) + expect(results[3]).toEqual({ + url: 'https://gitlab.com/', + status: 201, + passed: true + }) + + done() + } catch (err) { + done(err) + } + }) + testStream.on('error', (err) => { + done(err) + }) + const options = { + folder: path.resolve(__dirname, 'fixtures/remote-multiple-files'), + stream: testStream + } + NotFoundLinks(options) + }) \ No newline at end of file From b1deed80504288c7f6cd93214877d31e3916f0ad Mon Sep 17 00:00:00 2001 From: morgan557 Date: Wed, 1 Sep 2021 07:48:06 +0200 Subject: [PATCH 3/3] clean code --- src/index.js | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/index.js b/src/index.js index c3437f9..db00f50 100644 --- a/src/index.js +++ b/src/index.js @@ -31,22 +31,25 @@ module.exports = function(options) { if (!options['stream']) { throw new Error('The options stream is not defined, please pass a valid Writable stream.') } - - const arr = glob.sync(`${options.folder}/**/*.md`) - let arr1 = []; - arr.forEach(item => { - const maString = fs.readFileSync(item, 'utf-8') - let resultat = maString.match(/\(((https|http){1}.*?)\)/gm) - resultat = [...new Set(resultat)]; - arr1 = arr1.concat(resultat); - }) - var promiseList = arr1.map((item) => { - item = item.replace('(', '') - item = item.replace(')', '') - return got(item); - + //find the content of the options folder + const filesContent = glob.sync(`${options.folder}/**/*.md`) + let results = []; + filesContent.forEach(item => { + //find the list of url write in files + const str = fs.readFileSync(item, 'utf-8') + //verify if the content of the file start by http or https + let result = str.match(/\(((https|http){1}.*?)\)/gm) + //delete the duplicates url + result = [...new Set(result)]; + results = results.concat(result); }) + var promiseList = results.map((item) => { + // delete the brackets of each elements in the array + item = item.replace('(', '').replace(')', '') + return got(item); + }) + //verify the status of the promises and add value in the object Promise.allSettled(promiseList).then((results) => { try { results.forEach(item => { @@ -60,12 +63,12 @@ module.exports = function(options) { obj.status = item.value.statusCode; obj.url = item.value.url; obj.passed = true - arr.push(item.value.statusCode) + filesContent.push(item.value.statusCode) } else if (item.status === 'rejected') { obj.status = item.reason.response.statusCode; obj.passed = false obj.url = item.reason.response.url; - arr.push(item.reason.response.statusCode) + filesContent.push(item.reason.response.statusCode) } options.stream.write(Buffer.from(JSON.stringify(obj))) })