Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up Flow static type checking #19

Open
wants to merge 1 commit into
base: f24
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 4 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["@babel/preset-flow"],
"plugins": ["babel-plugin-syntax-hermes-parser"]
}
14 changes: 14 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[ignore]

[include]

[libs]

[lints]
untyped-type-import=error
internal-type=error
deprecated-type-bool=error

[options]

[strict]
96 changes: 96 additions & 0 deletions lib/admin/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
'use strict';

const fs = require('fs');
const path = require('path');
const sanitizeHTML = require('sanitize-html');
const nconf = require('nconf');
const winston = require('winston');
const file = require('../file');
const {
Translator

Check failure on line 10 in lib/admin/search.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 2 spaces

Check failure on line 10 in lib/admin/search.js

View workflow job for this annotation

GitHub Actions / test

Missing trailing comma
} = require('../translator');

Check failure on line 11 in lib/admin/search.js

View workflow job for this annotation

GitHub Actions / test

Expected 1 empty line after require statement not followed by another require
function filterDirectories(directories) {
return directories.map(dir => dir.replace(/^.*(admin.*?).tpl$/, '$1').split(path.sep).join('/')).filter(dir => !dir.endsWith('.js') && !dir.includes('/partials/') && /\/.*\//.test(dir) && !/manage\/(category|group|category-analytics)$/.test(dir));

Check failure on line 13 in lib/admin/search.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 2 spaces
}
async function getAdminNamespaces() {
const directories = await file.walk(path.resolve(nconf.get('views_dir'), 'admin'));

Check failure on line 16 in lib/admin/search.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 2 spaces
return filterDirectories(directories);

Check failure on line 17 in lib/admin/search.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 2 spaces
}
function sanitize(html) {
return sanitizeHTML(html, {

Check failure on line 20 in lib/admin/search.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 2 spaces
allowedTags: [],

Check failure on line 21 in lib/admin/search.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 4 spaces
allowedAttributes: []

Check failure on line 22 in lib/admin/search.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 4 spaces

Check failure on line 22 in lib/admin/search.js

View workflow job for this annotation

GitHub Actions / test

Missing trailing comma
});
}
function simplify(translations) {
return translations.replace(/(?:\{{1,2}[^}]*?\}{1,2})/g, '').replace(/(?:[ \t]*[\n\r]+[ \t]*)+/g, '\n').replace(/[\t ]+/g, ' ');
}
function nsToTitle(namespace) {
return namespace.replace('admin/', '').split('/').map(str => str[0].toUpperCase() + str.slice(1)).join(' > ').replace(/[^a-zA-Z> ]/g, ' ');
}
const fallbackCache = {};
async function initFallback(namespace) {
const template = await fs.promises.readFile(path.resolve(nconf.get('views_dir'), `${namespace}.tpl`), 'utf8');
const title = nsToTitle(namespace);
let translations = sanitize(template);
translations = Translator.removePatterns(translations);
translations = simplify(translations);
translations += `\n${title}`;
return {
namespace: namespace,
translations: translations,
title: title
};
}
async function fallback(namespace) {
if (fallbackCache[namespace]) {
return fallbackCache[namespace];
}
const params = await initFallback(namespace);
fallbackCache[namespace] = params;
return params;
}
async function initDict(language) {
const namespaces = await getAdminNamespaces();
return await Promise.all(namespaces.map(ns => buildNamespace(language, ns)));
}
async function buildNamespace(language, namespace) {
const translator = Translator.create(language);
try {
const translations = await translator.getTranslation(namespace);
if (!translations || !Object.keys(translations).length) {
return await fallback(namespace);
}
let str = Object.keys(translations).map(key => translations[key]).join('\n');
str = sanitize(str);
let title = namespace;
title = title.match(/admin\/(.+?)\/(.+?)$/);
title = `[[admin/menu:section-${title[1] === 'development' ? 'advanced' : title[1]}]]${title[2] ? ` > [[admin/menu:${title[1]}/${title[2]}]]` : ''}`;
title = await translator.translate(title);
return {
namespace: namespace,
translations: `${str}\n${title}`,
title: title
};
} catch (err) {
winston.error(err.stack);
return {
namespace: namespace,
translations: ''
};
}
}
const cache = {};
async function getDictionary(language) {
if (cache[language]) {
return cache[language];
}
const params = await initDict(language);
cache[language] = params;
return params;
}
module.exports.getDictionary = getDictionary;
module.exports.filterDirectories = filterDirectories;
module.exports.simplify = simplify;
module.exports.sanitize = sanitize;
require('../promisify')(module.exports);
40 changes: 40 additions & 0 deletions lib/admin/versions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const request = require('../request');
const meta = require('../meta');
let versionCache = '';
let versionCacheLastModified = '';
const isPrerelease = /^v?\d+\.\d+\.\d+-.+$/;
const latestReleaseUrl = 'https://api.github.com/repos/NodeBB/NodeBB/releases/latest';
async function getLatestVersion() {
const headers = {
Accept: 'application/vnd.github.v3+json',
'User-Agent': encodeURIComponent(`NodeBB Admin Control Panel/${meta.config.title}`)
};
if (versionCacheLastModified) {
headers['If-Modified-Since'] = versionCacheLastModified;
}
const {
body: latestRelease,
response
} = await request.get(latestReleaseUrl, {
headers: headers,
timeout: 2000
});
if (response.statusCode === 304) {
return versionCache;
}
if (response.statusCode !== 200) {
throw new Error(response.statusText);
}
if (!latestRelease || !latestRelease.tag_name) {
throw new Error('[[error:cant-get-latest-release]]');
}
const tagName = latestRelease.tag_name.replace(/^v/, '');
versionCache = tagName;
versionCacheLastModified = response.headers['last-modified'];
return versionCache;
}
exports.getLatestVersion = getLatestVersion;
exports.isPrerelease = isPrerelease;
require('../promisify')(exports);
7 changes: 7 additions & 0 deletions lib/als.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const {
AsyncLocalStorage
} = require('async_hooks');
const asyncLocalStorage = new AsyncLocalStorage();
module.exports = asyncLocalStorage;
Loading