Skip to content

Commit

Permalink
fixed easy global requires
Browse files Browse the repository at this point in the history
  • Loading branch information
gracepark committed Jun 18, 2021
1 parent ba95c62 commit 0762734
Show file tree
Hide file tree
Showing 17 changed files with 85 additions and 43 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ module.exports = {
ecmaVersion: 11
},
rules: {
'import/no-extraneous-dependencies': ['error']
'import/no-extraneous-dependencies': ['error'],
'node/global-require': ['error']
},
overrides: [
{
Expand Down
3 changes: 2 additions & 1 deletion lib/frontmatter.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const parse = require('./read-frontmatter')
const semverValidRange = require('semver').validRange
const layoutNames = Object.keys(require('./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'))
Expand Down
3 changes: 2 additions & 1 deletion lib/redirects/permalinks.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
4 changes: 3 additions & 1 deletion middleware/csrf.js
Original file line number Diff line number Diff line change
@@ -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']
})
63 changes: 42 additions & 21 deletions middleware/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
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 webpack = require('./webpack')
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'
Expand All @@ -15,57 +36,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 }))
if (isDevelopment) app.use(webpack)

// *** 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'))

// *** 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
Expand Down Expand Up @@ -131,7 +152,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) {
Expand All @@ -145,5 +166,5 @@ module.exports = function (app) {
app.get('/*', asyncMiddleware(instrument('./render-page')))

// *** Error handling, must go last ***
app.use(require('./handle-errors'))
app.use(handleErrors)
}
6 changes: 4 additions & 2 deletions middleware/search.js
Original file line number Diff line number Diff line change
@@ -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')

Expand Down
4 changes: 3 additions & 1 deletion script/helpers/github.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 4 additions & 2 deletions script/reconcile-filenames-with-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
3 changes: 2 additions & 1 deletion script/rest/utils/operation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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')
Expand Down
4 changes: 2 additions & 2 deletions script/sync-search-indices.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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') {
Expand All @@ -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())
Expand Down
3 changes: 2 additions & 1 deletion tests/browser/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const sleep = require('await-sleep')
const { latest } = require('../../lib/enterprise-server-releases')
const languages = require('../../lib/languages')
const featureFlags = require('../../feature-flags.json')

describe('homepage', () => {
jest.setTimeout(60 * 1000)
Expand Down Expand Up @@ -325,7 +326,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')
Expand Down
3 changes: 2 additions & 1 deletion tests/content/site-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')}`
Expand Down
5 changes: 3 additions & 2 deletions tests/rendering/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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)))
Expand Down Expand Up @@ -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)
})
})
3 changes: 2 additions & 1 deletion tests/rendering/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion tests/routing/top-developer-site-path-redirects.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { head } = require('../helpers/supertest')
const topOldDeveloperSitePaths = require('../fixtures/top-old-developer-site-paths.json')

describe('developer.github.com redirects', () => {
jest.setTimeout(30 * 60 * 1000)
Expand All @@ -16,7 +17,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)

Expand Down
3 changes: 2 additions & 1 deletion tests/unit/liquid-helpers.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down

0 comments on commit 0762734

Please sign in to comment.