diff --git a/lib/frontmatter.js b/lib/frontmatter.js index bdc0e45cb5..3fb6fafe85 100644 --- a/lib/frontmatter.js +++ b/lib/frontmatter.js @@ -1,8 +1,12 @@ const parse = require('./read-frontmatter') -const layoutNames = Object.keys(require('./layouts')).concat([false]) +const semver = require('semver') +const layouts = require('./layouts') + +const semverValidRange = semver.validRange +const layoutNames = Object.keys(layouts).concat([false]) const semverRange = { type: 'string', - conform: require('semver').validRange, + conform: semverValidRange, message: 'Must be a valid SemVer range' } const versionIds = Object.keys(require('./all-versions')) diff --git a/lib/redirects/permalinks.js b/lib/redirects/permalinks.js index d6c91dd307..dd04e8aec4 100644 --- a/lib/redirects/permalinks.js +++ b/lib/redirects/permalinks.js @@ -1,6 +1,7 @@ const path = require('path') const patterns = require('../patterns') -const supportedVersions = new Set(Object.keys(require('../all-versions'))) +const allVersions = require('../all-versions') +const supportedVersions = new Set(Object.keys(allVersions)) const getOldPathsFromPermalink = require('./get-old-paths-from-permalink') const { getVersionStringFromPath } = require('../path-utils') const { getNewVersionedPath } = require('../old-versions-utils') diff --git a/lib/rest/index.js b/lib/rest/index.js index c44094da71..2784942808 100644 --- a/lib/rest/index.js +++ b/lib/rest/index.js @@ -6,7 +6,7 @@ const operations = {} fs.readdirSync(schemasPath) .forEach(filename => { const key = filename.replace('.json', '') - const value = require(path.join(schemasPath, filename)) + const value = JSON.parse(fs.readFileSync(path.join(schemasPath, filename))) operations[key] = value }) const allVersions = require('../all-versions') diff --git a/lib/webhooks/index.js b/lib/webhooks/index.js index 366f0d4443..69dde77015 100644 --- a/lib/webhooks/index.js +++ b/lib/webhooks/index.js @@ -19,7 +19,7 @@ versions.forEach(version => { // payload file: /path/to/check_run.completed.payload.json // payload path: check_run.completed const payloadPath = path.basename(payloadFile).replace('.payload.json', '') - set(payloadsPerVersion, payloadPath, formatAsJsonCodeBlock(require(payloadFile))) + set(payloadsPerVersion, payloadPath, formatAsJsonCodeBlock(JSON.parse(fs.readFileSync(payloadFile)))) }) payloads[version] = payloadsPerVersion diff --git a/middleware/contextualizers/graphql.js b/middleware/contextualizers/graphql.js index 257ed248bc..f07a1506ed 100644 --- a/middleware/contextualizers/graphql.js +++ b/middleware/contextualizers/graphql.js @@ -1,3 +1,5 @@ +const fs = require('fs') +const path = require('path') const previews = require('../../lib/graphql/static/previews') const upcomingChanges = require('../../lib/graphql/static/upcoming-changes') const changelog = require('../../lib/graphql/static/changelog') @@ -23,7 +25,7 @@ module.exports = function graphqlContext (req, res, next) { const graphqlVersion = currentVersionObj.miscVersionName req.context.graphql = { - schemaForCurrentVersion: require(`../../lib/graphql/static/schema-${graphqlVersion}`), + schemaForCurrentVersion: JSON.parse(fs.readFileSync(path.join(process.cwd(), `lib/graphql/static/schema-${graphqlVersion}.json`))), previewsForCurrentVersion: previews[graphqlVersion], upcomingChangesForCurrentVersion: upcomingChanges[graphqlVersion], prerenderedObjectsForCurrentVersion: prerenderedObjects[graphqlVersion], diff --git a/middleware/csrf.js b/middleware/csrf.js index 914546178b..7e7f4d3cb5 100644 --- a/middleware/csrf.js +++ b/middleware/csrf.js @@ -1,4 +1,6 @@ +const cookieSettings = require('../lib/cookie-settings') + module.exports = require('csurf')({ - cookie: require('../lib/cookie-settings'), + cookie: cookieSettings, ignoreMethods: ['GET', 'HEAD', 'OPTIONS'] }) diff --git a/middleware/index.js b/middleware/index.js index 3741e6ddc6..32b176592b 100644 --- a/middleware/index.js +++ b/middleware/index.js @@ -1,6 +1,26 @@ const express = require('express') const instrument = require('../lib/instrument-middleware') const haltOnDroppedConnection = require('./halt-on-dropped-connection') +const abort = require('./abort') +const timeout = require('./timeout') +const morgan = require('morgan') +const datadog = require('./connect-datadog') +const rateLimit = require('./rate-limit') +const cors = require('./cors') +const helmet = require('helmet') +const csp = require('./csp') +const cookieParser = require('./cookie-parser') +const csrf = require('./csrf') +const handleCsrfErrors = require('./handle-csrf-errors') +const compression = require('compression') +const disableCachingOnSafari = require('./disable-caching-on-safari') +const setFastlySurrogateKey = require('./set-fastly-surrogate-key') +const setFastlyCacheHeaders = require('./set-fastly-cache-headers') +const catchBadAcceptLanguage = require('./catch-bad-accept-language') +const reqUtils = require('./req-utils') +const recordRedirect = require('./record-redirect') +const connectSlashes = require('connect-slashes') +const handleErrors = require('./handle-errors') const { NODE_ENV } = process.env const isDevelopment = NODE_ENV === 'development' @@ -15,58 +35,57 @@ const asyncMiddleware = fn => module.exports = function (app) { // *** Request connection management *** - if (!isTest) app.use(require('./timeout')) - app.use(require('./abort')) + if (!isTest) app.use(timeout) + app.use(abort) // *** Development tools *** - app.use(require('morgan')('dev', { skip: (req, res) => !isDevelopment })) - if (isDevelopment) app.use(require('./webpack')) + app.use(morgan('dev', { skip: (req, res) => !isDevelopment })) // *** Observability *** if (process.env.DD_API_KEY) { - app.use(require('./connect-datadog')) + app.use(datadog) } // *** Early exits *** // Don't use the proxy's IP, use the requester's for rate limiting // See https://expressjs.com/en/guide/behind-proxies.html app.set('trust proxy', 1) - app.use(require('./rate-limit')) + app.use(rateLimit) app.use(instrument('./handle-invalid-paths')) app.use(instrument('./handle-next-data-path')) // *** Security *** - app.use(require('./cors')) - app.use(require('helmet')({ + app.use(cors) + app.use(helmet({ // Override referrerPolicy to match the browser's default: "strict-origin-when-cross-origin". // Helmet now defaults to "no-referrer", which is a problem for our archived assets proxying. referrerPolicy: { policy: 'strict-origin-when-cross-origin' } })) - app.use(require('./csp')) // Must come after helmet - app.use(require('./cookie-parser')) // Must come before csrf + app.use(csp) // Must come after helmet + app.use(cookieParser) // Must come before csrf app.use(express.json()) // Must come before csrf - app.use(require('./csrf')) - app.use(require('./handle-csrf-errors')) // Must come before regular handle-errors + app.use(csrf) + app.use(handleCsrfErrors) // Must come before regular handle-errors // *** Headers *** app.set('etag', false) // We will manage our own ETags if desired - app.use(require('compression')()) - app.use(require('./disable-caching-on-safari')) - app.use(require('./set-fastly-surrogate-key')) - app.use(require('./catch-bad-accept-language')) + app.use(compression()) + app.use(disableCachingOnSafari) + app.use(setFastlySurrogateKey) + app.use(catchBadAcceptLanguage) // *** Config and context for redirects *** - app.use(require('./req-utils')) // Must come before record-redirect and events - app.use(require('./record-redirect')) + app.use(reqUtils) // Must come before record-redirect and events + app.use(recordRedirect) app.use(instrument('./detect-language')) // Must come before context, breadcrumbs, find-page, handle-errors, homepages app.use(asyncMiddleware(instrument('./context'))) // Must come before early-access-*, handle-redirects app.use(asyncMiddleware(instrument('./contextualizers/short-versions'))) // Support version shorthands // *** Redirects, 3xx responses *** // I ordered these by use frequency - app.use(require('connect-slashes')(false)) + app.use(connectSlashes(false)) app.use(instrument('./redirects/external')) app.use(instrument('./redirects/help-to-docs')) app.use(instrument('./redirects/language-code-redirects')) // Must come before contextualizers @@ -132,7 +151,7 @@ module.exports = function (app) { app.use(asyncMiddleware(instrument('./is-next-request'))) // *** Headers for pages only *** - app.use(require('./set-fastly-cache-headers')) + app.use(setFastlyCacheHeaders) // handle serving NextJS bundled code (/_next/*) if (process.env.FEATURE_NEXTJS) { @@ -146,5 +165,5 @@ module.exports = function (app) { app.get('/*', asyncMiddleware(instrument('./render-page'))) // *** Error handling, must go last *** - app.use(require('./handle-errors')) + app.use(handleErrors) } diff --git a/middleware/is-next-request.js b/middleware/is-next-request.js index 93d8c3c69c..21764b7ccf 100644 --- a/middleware/is-next-request.js +++ b/middleware/is-next-request.js @@ -1,38 +1,10 @@ -const pathToRegexp = require('path-to-regexp') - -const { productIds } = require('../lib/all-products') -const versionIds = Object.keys(require('../lib/all-versions')) - const { FEATURE_NEXTJS } = process.env; -const homePageExp = pathToRegexp('/:locale/:versionId?') -const productPageExp = pathToRegexp('/:locale/:versionId?/:productId') -const subSectionExp = pathToRegexp('/:locale/:versionId?/:productId/:subSection*') - module.exports = function isNextRequest(req, res, next) { req.renderWithNextjs = false; - if (FEATURE_NEXTJS && !req.path.startsWith('/_next/')) { - if ('nextjs' in req.query) { - req.renderWithNextjs = true; - } else { - // Custom path matching to determine if we should render with nextjs - - // Remove any query string (?...) and/or fragment identifier (#...) - const { pathname } = new URL(req.originalUrl, "https://docs.github.com"); - - // Should the current path be rendered by NextJS? - const homePageMatch = homePageExp.exec(pathname) - const productPageMatch = productPageExp.exec(pathname) - const subSectionMatch = subSectionExp.exec(pathname) - if (homePageMatch && (!homePageMatch[2] || versionIds.includes(homePageMatch[2]))) { - req.renderWithNextjs = true - } else if (productPageMatch && productIds.includes(productPageMatch[3])) { - req.renderWithNextjs = true - } else if (subSectionMatch) { - req.renderWithNextjs = true - } - } + if (FEATURE_NEXTJS) { + req.renderWithNextjs = true; } next(); diff --git a/middleware/search.js b/middleware/search.js index fed1e1cad6..c79f3ee2a3 100644 --- a/middleware/search.js +++ b/middleware/search.js @@ -1,6 +1,8 @@ const express = require('express') -const languages = new Set(Object.keys(require('../lib/languages'))) -const versions = new Set(Object.values(require('../lib/search/versions'))) +const libLanguages = require('../lib/languages') +const searchVersions = require('../lib/search/versions') +const languages = new Set(Object.keys(libLanguages)) +const versions = new Set(Object.values(searchVersions)) const loadLunrResults = require('../lib/search/lunr-search') const loadAlgoliaResults = require('../lib/search/algolia-search') diff --git a/middleware/webpack.js b/middleware/webpack.js deleted file mode 100644 index f8156a8fc4..0000000000 --- a/middleware/webpack.js +++ /dev/null @@ -1,14 +0,0 @@ -const webpack = require('webpack') -const middleware = require('webpack-dev-middleware') -const config = require('../webpack.config') - -const webpackCompiler = webpack({ - ...config, - plugins: [ - ...config.plugins - ] -}) - -module.exports = middleware(webpackCompiler, { - publicPath: config.output.publicPath -}) diff --git a/package-lock.json b/package-lock.json index 97318e578f..7037b447fd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -51,12 +51,12 @@ "lodash": "^4.17.21", "lunr": "^2.3.9", "lunr-languages": "^1.4.0", + "mdast-util-from-markdown": "^0.8.5", "mdast-util-to-string": "^2.0.0", "morgan": "^1.10.0", "next": "^11.0.0", "node-fetch": "^2.6.1", "parse5": "^6.0.1", - "path-to-regexp": "^0.1.7", "port-used": "^2.0.8", "rate-limit-redis": "^2.1.0", "react": "^17.0.2", @@ -143,7 +143,6 @@ "jest-slow-test-reporter": "^1.0.0", "linkinator": "^2.13.6", "make-promises-safe": "^5.1.0", - "mdast-util-from-markdown": "^0.8.5", "mini-css-extract-plugin": "^1.6.0", "mkdirp": "^1.0.4", "mock-express-response": "^0.3.0", @@ -169,7 +168,6 @@ "url-template": "^2.0.8", "webpack": "^5.37.0", "webpack-cli": "^4.7.0", - "webpack-dev-middleware": "^4.2.0", "website-scraper": "^4.2.3" }, "engines": { @@ -4179,7 +4177,6 @@ "version": "3.0.3", "resolved": "https://registry.npmjs.org/@types/mdast/-/mdast-3.0.3.tgz", "integrity": "sha512-SXPBMnFVQg1s00dlMCc/jCdvPqdE4mXaMMCeRlxLDmTAEoegHT53xKtkDnzDTOcmMHUfcjyf36/YYZ6SxRdnsw==", - "dev": true, "dependencies": { "@types/unist": "*" } @@ -16037,7 +16034,6 @@ "version": "0.8.5", "resolved": "https://registry.npmjs.org/mdast-util-from-markdown/-/mdast-util-from-markdown-0.8.5.tgz", "integrity": "sha512-2hkTXtYYnr+NubD/g6KGBS/0mFmBcifAsI0yIWRiRo0PjVs6SSOSOdtzbp6kSGnShDN6G5aWZpKQ2lWRy27mWQ==", - "dev": true, "dependencies": { "@types/mdast": "^3.0.0", "mdast-util-to-string": "^2.0.0", @@ -16050,7 +16046,6 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/parse-entities/-/parse-entities-2.0.0.tgz", "integrity": "sha512-kkywGpCcRYhqQIchaWqZ875wzpS/bMKhz5HnN3p7wveJTkTtyAB/AlnS0f8DFSqYW1T82t6yEAkEcB+A1I3MbQ==", - "dev": true, "dependencies": { "character-entities": "^1.0.0", "character-entities-legacy": "^1.0.0", @@ -16261,7 +16256,6 @@ "version": "2.11.3", "resolved": "https://registry.npmjs.org/micromark/-/micromark-2.11.3.tgz", "integrity": "sha512-oph5YYkVqR2U9OtWBcXYysZMtrdIvi8dfSeyEdr1wFr3Bk6YwI6THosX2AzKnhdps7mVUbXiqhmosu9DcA+xlQ==", - "dev": true, "dependencies": { "debug": "^4.0.0", "parse-entities": "^2.0.0" @@ -16271,7 +16265,6 @@ "version": "4.3.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz", "integrity": "sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==", - "dev": true, "dependencies": { "ms": "2.1.2" }, @@ -16282,14 +16275,12 @@ "node_modules/micromark/node_modules/ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "node_modules/micromark/node_modules/parse-entities": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/parse-entities/-/parse-entities-2.0.0.tgz", "integrity": "sha512-kkywGpCcRYhqQIchaWqZ875wzpS/bMKhz5HnN3p7wveJTkTtyAB/AlnS0f8DFSqYW1T82t6yEAkEcB+A1I3MbQ==", - "dev": true, "dependencies": { "character-entities": "^1.0.0", "character-entities-legacy": "^1.0.0", @@ -28366,7 +28357,6 @@ "version": "3.0.3", "resolved": "https://registry.npmjs.org/@types/mdast/-/mdast-3.0.3.tgz", "integrity": "sha512-SXPBMnFVQg1s00dlMCc/jCdvPqdE4mXaMMCeRlxLDmTAEoegHT53xKtkDnzDTOcmMHUfcjyf36/YYZ6SxRdnsw==", - "dev": true, "requires": { "@types/unist": "*" } @@ -38331,7 +38321,6 @@ "version": "0.8.5", "resolved": "https://registry.npmjs.org/mdast-util-from-markdown/-/mdast-util-from-markdown-0.8.5.tgz", "integrity": "sha512-2hkTXtYYnr+NubD/g6KGBS/0mFmBcifAsI0yIWRiRo0PjVs6SSOSOdtzbp6kSGnShDN6G5aWZpKQ2lWRy27mWQ==", - "dev": true, "requires": { "@types/mdast": "^3.0.0", "mdast-util-to-string": "^2.0.0", @@ -38344,7 +38333,6 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/parse-entities/-/parse-entities-2.0.0.tgz", "integrity": "sha512-kkywGpCcRYhqQIchaWqZ875wzpS/bMKhz5HnN3p7wveJTkTtyAB/AlnS0f8DFSqYW1T82t6yEAkEcB+A1I3MbQ==", - "dev": true, "requires": { "character-entities": "^1.0.0", "character-entities-legacy": "^1.0.0", @@ -38535,7 +38523,6 @@ "version": "2.11.3", "resolved": "https://registry.npmjs.org/micromark/-/micromark-2.11.3.tgz", "integrity": "sha512-oph5YYkVqR2U9OtWBcXYysZMtrdIvi8dfSeyEdr1wFr3Bk6YwI6THosX2AzKnhdps7mVUbXiqhmosu9DcA+xlQ==", - "dev": true, "requires": { "debug": "^4.0.0", "parse-entities": "^2.0.0" @@ -38545,7 +38532,6 @@ "version": "4.3.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz", "integrity": "sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==", - "dev": true, "requires": { "ms": "2.1.2" } @@ -38553,14 +38539,12 @@ "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "parse-entities": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/parse-entities/-/parse-entities-2.0.0.tgz", "integrity": "sha512-kkywGpCcRYhqQIchaWqZ875wzpS/bMKhz5HnN3p7wveJTkTtyAB/AlnS0f8DFSqYW1T82t6yEAkEcB+A1I3MbQ==", - "dev": true, "requires": { "character-entities": "^1.0.0", "character-entities-legacy": "^1.0.0", diff --git a/package.json b/package.json index ab65103260..0d1015c119 100644 --- a/package.json +++ b/package.json @@ -57,13 +57,12 @@ "lodash": "^4.17.21", "lunr": "^2.3.9", "lunr-languages": "^1.4.0", - "mdast-util-to-string": "^2.0.0", "mdast-util-from-markdown": "^0.8.5", + "mdast-util-to-string": "^2.0.0", "morgan": "^1.10.0", "next": "^11.0.0", "node-fetch": "^2.6.1", "parse5": "^6.0.1", - "path-to-regexp": "^0.1.7", "port-used": "^2.0.8", "rate-limit-redis": "^2.1.0", "react": "^17.0.2", @@ -175,7 +174,6 @@ "url-template": "^2.0.8", "webpack": "^5.37.0", "webpack-cli": "^4.7.0", - "webpack-dev-middleware": "^4.2.0", "website-scraper": "^4.2.3" }, "optionalDependencies": { diff --git a/script/enterprise-server-releases/create-rest-files.js b/script/enterprise-server-releases/create-rest-files.js index fd037af66a..35a99b90bb 100755 --- a/script/enterprise-server-releases/create-rest-files.js +++ b/script/enterprise-server-releases/create-rest-files.js @@ -63,7 +63,7 @@ async function main () { fs.writeFileSync(newDereferencedFile, newDereferenceContent) console.log(`Created ${newDereferencedFile}.`) - const dereferencedSchema = require(path.join(process.cwd(), newDereferencedFile)) + const dereferencedSchema = JSON.parse(fs.readFileSync(path.join(process.cwd(), newDereferencedFile))) // Store all operations in an array of operation objects const operations = await getOperations(dereferencedSchema) diff --git a/script/helpers/github.js b/script/helpers/github.js index df98cd26e5..ba9f42dfff 100644 --- a/script/helpers/github.js +++ b/script/helpers/github.js @@ -1,5 +1,7 @@ +const dotenv = require('dotenv') + if (!process.env.GITHUB_TOKEN) { - require('dotenv').config() + dotenv.config() } // this module needs to work in development, production, and GitHub Actions diff --git a/script/reconcile-filenames-with-ids.js b/script/reconcile-filenames-with-ids.js index cd64487683..e34e9bbbd5 100755 --- a/script/reconcile-filenames-with-ids.js +++ b/script/reconcile-filenames-with-ids.js @@ -3,8 +3,10 @@ const fs = require('fs') const path = require('path') const walk = require('walk-sync') -const slugger = new (require('github-slugger'))() -const entities = new (require('html-entities').XmlEntities)() +const GithubSlugger = require('github-slugger') +const htmlEntities = require('html-entities') +const slugger = new GithubSlugger() +const entities = new htmlEntities.XmlEntities() const frontmatter = require('../lib/read-frontmatter') const { execSync } = require('child_process') const addRedirectToFrontmatter = require('./helpers/add-redirect-to-frontmatter') diff --git a/script/rest/openapi-check.js b/script/rest/openapi-check.js index 151c2cf1b9..b4ae718cea 100755 --- a/script/rest/openapi-check.js +++ b/script/rest/openapi-check.js @@ -1,5 +1,6 @@ #!/usr/bin/env node - +const fs = require('fs') +const path = require('path') const glob = require('glob') const program = require('commander') const getOperations = require('./utils/get-operations') @@ -28,8 +29,7 @@ if (filesToCheck.length) { async function check (files) { console.log('Verifying OpenAPI files are valid with decorator') - - const documents = files.map(filename => [filename, require(filename)]) + const documents = files.map(filename => [filename, JSON.parse(fs.readFileSync(path.join(process.cwd(), filename)))]) for (const [filename, schema] of documents) { try { diff --git a/script/rest/update-files.js b/script/rest/update-files.js index 5dbcbde3c9..bc2f0f6675 100755 --- a/script/rest/update-files.js +++ b/script/rest/update-files.js @@ -75,7 +75,7 @@ async function getDereferencedFiles () { // name of the `github/github` checkout. A CI test // checks the version and fails if it's not a semantic version. schemas.forEach(filename => { - const schema = require(path.join(dereferencedPath, filename)) + const schema = JSON.parse(fs.readFileSync(path.join(dereferencedPath, filename))) schema.info.version = `${githubBranch} !!DEVELOPMENT MODE - DO NOT MERGE!!` fs.writeFileSync(path.join(dereferencedPath, filename), JSON.stringify(schema, null, 2)) }) @@ -85,7 +85,7 @@ async function decorate () { console.log('\nšŸŽ„ Decorating the OpenAPI schema files in lib/rest/static/dereferenced.\n') const dereferencedSchemas = schemas.reduce((acc, filename) => { - const schema = require(path.join(dereferencedPath, filename)) + const schema = JSON.parse(fs.readFileSync(path.join(dereferencedPath, filename))) const key = filename.replace('.deref.json', '') return { ...acc, [key]: schema } }, {}) diff --git a/script/rest/utils/operation.js b/script/rest/utils/operation.js index f2a6d6be07..77e5a96d39 100644 --- a/script/rest/utils/operation.js +++ b/script/rest/utils/operation.js @@ -1,10 +1,12 @@ const { get, flatten, isPlainObject } = require('lodash') const { sentenceCase } = require('change-case') -const slugger = new (require('github-slugger'))() +const GitHubSlugger = require('github-slugger') +const slugger = new GitHubSlugger() const httpStatusCodes = require('http-status-code') const renderContent = require('../../../lib/render-content') const createCodeSamples = require('./create-code-samples') const Ajv = require('ajv') +const operationSchema = require('./operation-schema') // titles that can't be derived by sentence-casing the ID const categoryTitles = { scim: 'SCIM' } @@ -39,7 +41,7 @@ module.exports = class Operation { } get schema () { - return require('./operation-schema') + return operationSchema } async process () { diff --git a/script/sync-search-indices.js b/script/sync-search-indices.js index 18a3dab834..e9ef3cf41c 100755 --- a/script/sync-search-indices.js +++ b/script/sync-search-indices.js @@ -6,13 +6,13 @@ // It can also be run manually. For more info see [contributing/search.md](contributing/search.md) // // [end-readme] - +const searchSync = require('./search/sync') require('make-promises-safe') main() async function main () { - const sync = require('./search/sync') + const sync = searchSync const opts = { dryRun: 'DRY_RUN' in process.env, language: process.env.LANGUAGE, diff --git a/server.js b/server.js index d50a46d5e9..b6842357fe 100644 --- a/server.js +++ b/server.js @@ -4,6 +4,9 @@ const throng = require('throng') const os = require('os') const portUsed = require('port-used') const prefixStreamWrite = require('./lib/prefix-stream-write') +const libApp = require('./lib/app') +const libWarmServer = require('./lib/warm-server') +const http = require('http') // Intentionally require these for both cluster primary and workers require('./lib/check-node-version') @@ -46,8 +49,8 @@ async function checkPortAvailability () { } async function startServer () { - const app = require('./lib/app') - const warmServer = require('./lib/warm-server') + const app = libApp + const warmServer = libWarmServer // If in a deployed environment... if (NODE_ENV === 'production') { @@ -58,7 +61,7 @@ async function startServer () { } // Workaround for https://github.com/expressjs/express/issues/1101 - const server = require('http').createServer(app) + const server = http.createServer(app) server .listen(port, () => console.log(`app running on http://localhost:${port}`)) .on('error', () => server.close()) diff --git a/tests/browser/browser.js b/tests/browser/browser.js index 6f7a46c01d..88138c7ed5 100644 --- a/tests/browser/browser.js +++ b/tests/browser/browser.js @@ -1,7 +1,10 @@ /* global page, browser */ +const fs = require('fs') +const path = require('path') const sleep = require('await-sleep') const { latest } = require('../../lib/enterprise-server-releases') const languages = require('../../lib/languages') +const featureFlags = JSON.parse(fs.readFileSync(path.join(process.cwd(), '/feature-flags.json'))) describe('homepage', () => { jest.setTimeout(60 * 1000) @@ -329,7 +332,7 @@ describe('nextjs query param', () => { jest.setTimeout(60 * 1000) it('landing page renders through nextjs pipeline depending on FEATURE_NEXTJS value', async () => { - const flagVal = require('../../feature-flags.json').FEATURE_NEXTJS + const flagVal = featureFlags.FEATURE_NEXTJS await page.goto('http://localhost:4001/en/actions?nextjs=') const IS_NEXTJS_PAGE = await page.evaluate(() => window.IS_NEXTJS_PAGE) const nextWrapper = await page.$('#__next') diff --git a/tests/content/graphql.js b/tests/content/graphql.js index 7bcaac0eec..67d2735f6b 100644 --- a/tests/content/graphql.js +++ b/tests/content/graphql.js @@ -1,3 +1,5 @@ +const fs = require('fs') +const path = require('path') const previewsJson = require('../../lib/graphql/static/previews') const upcomingChangesJson = require('../../lib/graphql/static/upcoming-changes') const prerenderedObjectsJson = require('../../lib/graphql/static/prerendered-objects') @@ -20,7 +22,7 @@ describe('graphql json files', () => { test('schemas object validation', () => { graphqlVersions.forEach(version => { - const schemaJsonPerVersion = require(`../../lib/graphql/static/schema-${version}`) + const schemaJsonPerVersion = JSON.parse(fs.readFileSync(path.join(process.cwd(), `lib/graphql/static/schema-${version}.json`))) // all graphql types are arrays except for queries graphqlTypes .filter(type => type !== 'queries') diff --git a/tests/content/site-data.js b/tests/content/site-data.js index 5f144ff7e5..046602970f 100644 --- a/tests/content/site-data.js +++ b/tests/content/site-data.js @@ -5,6 +5,7 @@ const flat = require('flat') const loadSiteData = require('../../lib/site-data') const patterns = require('../../lib/patterns') const { liquid } = require('../../lib/render-content') +const walkSync = require('walk-sync') describe('siteData module (English)', () => { let data @@ -82,7 +83,7 @@ describe('siteData module (English)', () => { }) test('warn if any YAML reusables are found', async () => { - const reusables = require('walk-sync')(path.join(__dirname, '../../data/reusables')) + const reusables = walkSync(path.join(__dirname, '../../data/reusables')) expect(reusables.length).toBeGreaterThan(100) const yamlReusables = reusables.filter(filename => filename.endsWith('.yml') || filename.endsWith('.yaml')) const message = `reusables are now written as individual Markdown files. Please migrate the following YAML files to Markdown:\n${yamlReusables.join('\n')}` diff --git a/tests/rendering/rest.js b/tests/rendering/rest.js index 34a9428a04..3b1a51cfa4 100644 --- a/tests/rendering/rest.js +++ b/tests/rendering/rest.js @@ -3,6 +3,7 @@ const path = require('path') const { difference, isPlainObject } = require('lodash') const { getJSON } = require('../helpers/supertest') const enterpriseServerReleases = require('../../lib/enterprise-server-releases') +const rest = require('../../lib/rest') // list of REST markdown files that do not correspond to REST API resources // TODO could we get this list dynamically, say via page frontmatter? const excludeFromResourceNameCheck = [ @@ -15,7 +16,7 @@ describe('REST references docs', () => { jest.setTimeout(3 * 60 * 1000) test('markdown file exists for every operationId prefix in the api.github.com schema', async () => { - const { categories } = require('../../lib/rest') + const { categories } = rest const referenceDir = path.join(__dirname, '../../content/rest/reference') const filenames = (await fs.readdir(referenceDir)) .filter(filename => !excludeFromResourceNameCheck.find(excludedFile => filename.endsWith(excludedFile))) @@ -47,7 +48,7 @@ describe('REST references docs', () => { }) test('no wrongly detected AppleScript syntax highlighting in schema data', async () => { - const { operations } = require('../../lib/rest') + const { operations } = rest expect(JSON.stringify(operations).includes('hljs language-applescript')).toBe(false) }) }) diff --git a/tests/rendering/server.js b/tests/rendering/server.js index db47b2075c..b0788fdd5f 100644 --- a/tests/rendering/server.js +++ b/tests/rendering/server.js @@ -6,6 +6,7 @@ const path = require('path') const { loadPages } = require('../../lib/page-data') const builtAssets = require('../../lib/built-asset-urls') const AZURE_STORAGE_URL = 'githubdocs.azureedge.net' +const CspParse = require('csp-parse') describe('server', () => { jest.setTimeout(60 * 1000) @@ -39,7 +40,7 @@ describe('server', () => { const res = await get('/en') expect('content-security-policy' in res.headers).toBe(true) - const csp = new (require('csp-parse'))(res.headers['content-security-policy']) + const csp = new CspParse(res.headers['content-security-policy']) expect(csp.get('default-src')).toBe("'none'") expect(csp.get('font-src').includes("'self'")).toBe(true) diff --git a/tests/routing/top-developer-site-path-redirects.js b/tests/routing/top-developer-site-path-redirects.js index fdd34806ae..5df215832c 100644 --- a/tests/routing/top-developer-site-path-redirects.js +++ b/tests/routing/top-developer-site-path-redirects.js @@ -1,4 +1,7 @@ +const fs = require('fs') +const path = require('path') const { head } = require('../helpers/supertest') +const topOldDeveloperSitePaths = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'tests/fixtures/top-old-developer-site-paths.json'))) describe('developer.github.com redirects', () => { jest.setTimeout(30 * 60 * 1000) @@ -16,7 +19,7 @@ describe('developer.github.com redirects', () => { // test a subset of the top paths const pathsToCheck = 50 - const paths = require('../fixtures/top-old-developer-site-paths.json') + const paths = topOldDeveloperSitePaths .filter(path => !ignoredPatterns.some(pattern => path.match(pattern))) .slice(0, pathsToCheck) diff --git a/tests/unit/liquid-helpers.js b/tests/unit/liquid-helpers.js index 9aad2ba8f0..c72a7e36a1 100644 --- a/tests/unit/liquid-helpers.js +++ b/tests/unit/liquid-helpers.js @@ -1,6 +1,7 @@ const { liquid } = require('../../lib/render-content') const { loadPageMap } = require('../../lib/page-data') -const entities = new (require('html-entities').XmlEntities)() +const htmlEntities = require('html-entities') +const entities = new htmlEntities.XmlEntities() const nonEnterpriseDefaultVersion = require('../../lib/non-enterprise-default-version') describe('liquid helper tags', () => {