Skip to content

Commit 0f68511

Browse files
heiskrChiedo John
andauthored
Revert file reads from async to sync (#16979)
* Revert file reads from async to sync * Async funness * Fix warm server timers Co-authored-by: Chiedo John <chiedo@chiedosbmacbook.lan>
1 parent 06883b4 commit 0f68511

6 files changed

Lines changed: 31 additions & 40 deletions

File tree

lib/data-directory.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
const assert = require('assert')
2-
const fs = require('fs').promises
2+
const fs = require('fs')
33
const path = require('path')
44
const walk = require('walk-sync')
5-
const { mapLimit } = require('async')
65
const yaml = require('js-yaml')
76
const { isRegExp, set } = require('lodash')
87
const filenameToKey = require('./filename-to-key')
98

10-
const FILE_READ_LIMIT = 100
11-
12-
module.exports = async function dataDirectory (dir, opts = {}) {
9+
module.exports = function dataDirectory (dir, opts = {}) {
1310
const defaultOpts = {
1411
preprocess: (content) => { return content },
1512
ignorePatterns: [/README\.md$/i],
@@ -42,10 +39,9 @@ module.exports = async function dataDirectory (dir, opts = {}) {
4239
// ignore files that don't have a whitelisted file extension
4340
return opts.extensions.includes(path.extname(filename).toLowerCase())
4441
})
45-
const files = await mapLimit(
46-
filenames,
47-
FILE_READ_LIMIT,
48-
async filename => [filename, await fs.readFile(filename, 'utf8')]
42+
43+
const files = filenames.map(
44+
filename => [filename, fs.readFileSync(filename, 'utf8')]
4945
)
5046
files.forEach(([filename, fileContent]) => {
5147
// derive `foo.bar.baz` object key from `foo/bar/baz.yml` filename

lib/page.js

Lines changed: 5 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').promises
2+
const fs = require('fs')
33
const path = require('path')
44
const cheerio = require('cheerio')
55
const patterns = require('./patterns')
@@ -23,13 +23,13 @@ const slash = require('slash')
2323
const statsd = require('./statsd')
2424

2525
class Page {
26-
static async init (opts) {
27-
opts = await Page.read(opts)
26+
static init (opts) {
27+
opts = Page.read(opts)
2828
if (!opts) return
2929
return new Page(opts)
3030
}
3131

32-
static async read (opts) {
32+
static read (opts) {
3333
assert(opts.relativePath, 'relativePath is required')
3434
assert(opts.basePath, 'basePath is required')
3535

@@ -39,7 +39,7 @@ class Page {
3939
// Per https://nodejs.org/api/fs.html#fs_fs_exists_path_callback
4040
// its better to read and handle errors than to check access/stats first
4141
try {
42-
const raw = await fs.readFile(fullPath, 'utf8')
42+
const raw = fs.readFileSync(fullPath, 'utf8')
4343
return { ...opts, relativePath, fullPath, raw }
4444
} catch (err) {
4545
if (err.code === 'ENOENT') return false

lib/pages.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,16 @@ const path = require('path')
22
const walk = require('walk-sync').entries
33
const Page = require('./page')
44
const languages = require('./languages')
5-
const { mapLimit } = require('async')
65

7-
const FILE_READ_LIMIT = 100
8-
9-
async function loadPageList () {
6+
function loadPageList () {
107
// load english pages
118
const englishPath = path.join(__dirname, '..', languages.en.dir, 'content')
129
const englishPaths = walk(englishPath, {
1310
globs: ['**/*.md'],
1411
ignore: ['**/README.md']
1512
})
16-
const englishPages = await mapLimit(
17-
englishPaths,
18-
FILE_READ_LIMIT,
19-
async opts => await Page.read({
13+
const englishPages = englishPaths.map(
14+
opts => Page.read({
2015
...opts,
2116
languageCode: languages.en.code
2217
})
@@ -34,11 +29,10 @@ async function loadPageList () {
3429
}))
3530
})
3631
.flat()
37-
const localizedPages = await mapLimit(
38-
localizedPaths,
39-
FILE_READ_LIMIT,
40-
async ({ basePath, relativePath, languageCode }) =>
41-
await Page.read({ basePath, relativePath, languageCode })
32+
33+
const localizedPages = localizedPaths.map(
34+
({ basePath, relativePath, languageCode }) =>
35+
Page.read({ basePath, relativePath, languageCode })
4236
)
4337

4438
// Build out the list of prepared pages
@@ -64,8 +58,8 @@ function createMapFromArray (pageList) {
6458
return pageMap
6559
}
6660

67-
async function loadPageMap (pageList) {
68-
const pages = pageList || await loadPageList()
61+
function loadPageMap (pageList) {
62+
const pages = pageList || loadPageList()
6963
return createMapFromArray(pages)
7064
}
7165

lib/warm-server.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ const loadSiteTree = require('./site-tree')
77
// Instrument these functions so that
88
// it's wrapped in a timer that reports to Datadog
99
const dog = {
10-
loadPages: statsd.asyncTimer(loadPages, 'load_pages'),
11-
loadPageMap: statsd.asyncTimer(loadPageMap, 'load_page_map'),
10+
loadPages: statsd.timer(loadPages, 'load_pages'),
11+
loadPageMap: statsd.timer(loadPageMap, 'load_page_map'),
1212
loadRedirects: statsd.asyncTimer(loadRedirects, 'load_redirects'),
13-
loadSiteData: statsd.asyncTimer(loadSiteData, 'load_site_data'),
13+
loadSiteData: statsd.timer(loadSiteData, 'load_site_data'),
1414
loadSiteTree: statsd.asyncTimer(loadSiteTree, 'load_site_tree')
1515
}
1616

package-lock.json

Lines changed: 8 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"@primer/css": "^15.1.0",
2727
"@primer/octicons": "^11.0.0",
2828
"algoliasearch": "^3.35.1",
29-
"async": "^3.2.0",
3029
"babel-loader": "^8.1.0",
3130
"babel-preset-env": "^1.7.0",
3231
"browser-date-formatter": "^3.0.3",
@@ -95,6 +94,7 @@
9594
"devDependencies": {
9695
"@actions/core": "^1.2.6",
9796
"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",

0 commit comments

Comments
 (0)