Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6288998

Browse files
authored
Revert "Revert "Speed up warmServer by loading pages (and files) asyn… (#16837)
* Revert "Revert "Speed up warmServer by loading pages (and files) asynchronously (#16752)" (#16835)" This reverts commit a145171. * Move async to regular dependencies * Update package-lock.json
1 parent 0ac79f6 commit 6288998

12 files changed

Lines changed: 145 additions & 107 deletions

File tree

lib/page.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const assert = require('assert')
2-
const fs = require('fs')
2+
const fs = require('fs').promises
33
const path = require('path')
44
const cheerio = require('cheerio')
55
const patterns = require('./patterns')
@@ -23,15 +23,30 @@ const slash = require('slash')
2323
const statsd = require('./statsd')
2424

2525
class Page {
26-
constructor (opts) {
26+
static async init (opts) {
2727
assert(opts.relativePath, 'relativePath is required')
2828
assert(opts.basePath, 'basePath is required')
29+
30+
const relativePath = slash(opts.relativePath)
31+
const fullPath = slash(path.join(opts.basePath, relativePath))
32+
const raw = await fs.readFile(fullPath, 'utf8')
33+
34+
return new Page({ ...opts, relativePath, fullPath, raw })
35+
}
36+
37+
static async exists (path) {
38+
try {
39+
return await fs.stat(path)
40+
} catch (err) {
41+
if (err.code === 'ENOENT') return false
42+
console.error(err)
43+
}
44+
}
45+
46+
constructor (opts) {
2947
assert(opts.languageCode, 'languageCode is required')
3048

3149
Object.assign(this, { ...opts })
32-
this.relativePath = slash(this.relativePath)
33-
this.fullPath = slash(path.join(this.basePath, this.relativePath))
34-
this.raw = fs.readFileSync(this.fullPath, 'utf8')
3550

3651
// TODO remove this when crowdin-support issue 66 has been resolved
3752
if (this.languageCode !== 'en' && this.raw.includes(': verdadero')) {

lib/pages.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,50 @@ const path = require('path')
22
const walk = require('walk-sync').entries
33
const Page = require('./page')
44
const languages = require('./languages')
5-
const fs = require('fs')
5+
const { mapLimit, filterLimit } = require('async')
6+
const FILE_READ_LIMIT = 500
67

78
async function loadPageList () {
89
const pageList = []
910

1011
// load english pages
1112
const englishPath = path.join(__dirname, '..', languages.en.dir, 'content')
12-
const englishPages = walk(englishPath)
13-
.filter(({ relativePath }) => {
14-
return relativePath.endsWith('.md') &&
15-
!relativePath.includes('README')
16-
})
17-
.map(fileData => new Page({ ...fileData, languageCode: languages.en.code }))
18-
13+
const englishPaths = walk(englishPath)
14+
.filter(({ relativePath }) =>
15+
relativePath.endsWith('.md') && !relativePath.includes('README')
16+
)
17+
const englishPages = await mapLimit(
18+
englishPaths,
19+
FILE_READ_LIMIT,
20+
async fileData => await Page.init({ ...fileData, languageCode: languages.en.code })
21+
)
1922
pageList.push(...englishPages)
2023

2124
// load matching pages in other languages
22-
for (const page of englishPages) {
23-
for (const language of Object.values(languages)) {
24-
if (language.code === 'en') continue
25-
25+
let localizedPaths = Object.values(languages)
26+
.filter(({ code }) => code !== 'en')
27+
.map(language => {
2628
const basePath = path.join(__dirname, '..', language.dir, 'content')
27-
const localizedPath = path.join(basePath, page.relativePath)
28-
try {
29-
fs.statSync(localizedPath)
30-
} catch (_) {
31-
continue
32-
}
33-
34-
pageList.push(new Page({
35-
relativePath: page.relativePath,
29+
return englishPages.map(page => ({
3630
basePath,
31+
relativePath: page.relativePath,
32+
localizedPath: path.join(basePath, page.relativePath),
3733
languageCode: language.code
3834
}))
39-
}
40-
}
35+
})
36+
.flat()
37+
localizedPaths = await filterLimit(
38+
localizedPaths,
39+
FILE_READ_LIMIT,
40+
async ({ localizedPath }) => Page.exists(localizedPath)
41+
)
42+
const localizedPages = await mapLimit(
43+
localizedPaths,
44+
FILE_READ_LIMIT,
45+
async ({ basePath, relativePath, languageCode }) =>
46+
await Page.init({ basePath, relativePath, languageCode })
47+
)
48+
pageList.push(...localizedPages)
4149

4250
return pageList
4351
}

package-lock.json

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@primer/css": "^15.1.0",
2828
"@primer/octicons": "^11.0.0",
2929
"algoliasearch": "^3.35.1",
30+
"async": "^3.2.0",
3031
"babel-loader": "^8.1.0",
3132
"babel-preset-env": "^1.7.0",
3233
"browser-date-formatter": "^3.0.3",
@@ -94,7 +95,6 @@
9495
"devDependencies": {
9596
"@actions/core": "^1.2.6",
9697
"ajv": "^6.11.0",
97-
"async": "^3.2.0",
9898
"await-sleep": "0.0.1",
9999
"aws-sdk": "^2.610.0",
100100
"babel-eslint": "^10.1.0",
@@ -181,4 +181,4 @@
181181
"pre-push": "npm run prevent-pushes-to-main"
182182
}
183183
}
184-
}
184+
}

tests/browser/browser.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ const sleep = require('await-sleep')
33
const querystring = require('querystring')
44

55
describe('homepage', () => {
6+
jest.setTimeout(60 * 1000)
7+
68
test('should be titled "GitHub Documentation"', async () => {
79
await page.goto('http://localhost:4001')
810
await expect(page.title()).resolves.toMatch('GitHub Documentation')
911
})
1012
})
1113

1214
describe('algolia browser search', () => {
15+
jest.setTimeout(60 * 1000)
16+
1317
it('works on the homepage', async () => {
1418
await page.goto('http://localhost:4001/en')
1519
await page.click('#search-input-container input[type="search"]')

tests/content/crowdin-config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const ignoredPagePaths = config.files[0].ignore
44
const ignoredDataPaths = config.files[2].ignore
55

66
describe('crowdin.yml config file', () => {
7+
jest.setTimeout(60 * 1000)
8+
79
let pages
810
beforeAll(async (done) => {
911
pages = await loadPages()

tests/content/site-data-references.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const fs = require('fs').promises
77
const path = require('path')
88

99
describe('data references', () => {
10+
jest.setTimeout(60 * 1000)
11+
1012
let data, pages
1113

1214
beforeAll(async (done) => {

tests/routing/redirects.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ describe('redirects', () => {
1717
done()
1818
})
1919

20-
test('page.redirects is an array', () => {
21-
const page = new Page({
20+
test('page.redirects is an array', async () => {
21+
const page = await Page.init({
2222
relativePath: 'github/collaborating-with-issues-and-pull-requests/about-branches.md',
2323
basePath: path.join(__dirname, '../../content'),
2424
languageCode: 'en'
2525
})
2626
expect(isPlainObject(page.redirects)).toBe(true)
2727
})
2828

29-
test('dotcom homepage page.redirects', () => {
30-
const page = new Page({
29+
test('dotcom homepage page.redirects', async () => {
30+
const page = await Page.init({
3131
relativePath: 'github/index.md',
3232
basePath: path.join(__dirname, '../../content'),
3333
languageCode: 'en'
@@ -41,7 +41,7 @@ describe('redirects', () => {
4141
})
4242

4343
test('converts single `redirect_from` strings values into arrays', async () => {
44-
const page = new Page({
44+
const page = await Page.init({
4545
relativePath: 'github/collaborating-with-issues-and-pull-requests/about-conversations-on-github.md',
4646
basePath: path.join(__dirname, '../../content'),
4747
languageCode: 'en'

tests/unit/find-page.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe('find page', () => {
88
jest.setTimeout(1000 * 1000)
99

1010
test('falls back to the English page if it can\'t find a localized page', async () => {
11-
const page = new Page({
11+
const page = await Page.init({
1212
relativePath: 'page-that-does-not-exist-in-translations-dir.md',
1313
basePath: path.join(__dirname, '../fixtures'),
1414
languageCode: 'en'
@@ -24,7 +24,7 @@ describe('find page', () => {
2424
})
2525

2626
test('follows redirects', async () => {
27-
const page = new Page({
27+
const page = await Page.init({
2828
relativePath: 'page-with-redirects.md',
2929
basePath: path.join(__dirname, '../fixtures'),
3030
languageCode: 'en'

tests/unit/liquid-helpers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const { set } = require('lodash')
55
const nonEnterpriseDefaultVersion = require('../../lib/non-enterprise-default-version')
66

77
describe('liquid helper tags', () => {
8+
jest.setTimeout(60 * 1000)
9+
810
const context = {}
911
let pageMap
1012
beforeAll(async (done) => {

0 commit comments

Comments
 (0)