Commit 62bd3489 authored by Michael Henretty's avatar Michael Henretty
Browse files

disallow implicit any on server code

parent f498c04d
...@@ -26,7 +26,7 @@ export default class API { ...@@ -26,7 +26,7 @@ export default class API {
} }
private getRandomSentences(count: number): Promise<string[]> { private getRandomSentences(count: number): Promise<string[]> {
return this.getSentences().then(sentences => { return this.getSentences().then((sentences: string[]) => {
let randoms = []; let randoms = [];
for (var i = 0; i < count; i++) { for (var i = 0; i < count; i++) {
let distribution = Random.integer(0, sentences.length - 1); let distribution = Random.integer(0, sentences.length - 1);
...@@ -37,9 +37,10 @@ export default class API { ...@@ -37,9 +37,10 @@ export default class API {
}); });
} }
private getFilesInFolder(folderpath) { private getFilesInFolder(folderpath: string) {
return new Promise((res, rej) => { return new Promise((res: (files: string[]) => void,
fs.readdir(folderpath, (err, files) => { rej: (error: any) => void) => {
fs.readdir(folderpath, (err: any, files: string[]) => {
if (err) { if (err) {
rej(err); rej(err);
return; return;
...@@ -50,11 +51,12 @@ export default class API { ...@@ -50,11 +51,12 @@ export default class API {
}); });
} }
private getFileContents(filepath) { private getFileContents(filepath: string) {
return new Promise((res, rej) => { return new Promise((res: (contents: string) => void,
rej: (error: any) => void) => {
fs.readFile(filepath, { fs.readFile(filepath, {
contents: 'utf8' contents: 'utf8'
}, (err, data) => { }, (err: any, data: Buffer) => {
if (err) { if (err) {
rej(err); rej(err);
return; return;
...@@ -100,7 +102,7 @@ export default class API { ...@@ -100,7 +102,7 @@ export default class API {
} }
return this.getFilesInFolder(this.getSentenceFolder()) return this.getFilesInFolder(this.getSentenceFolder())
.then(files => { .then((files: string[]) => {
return Promise.all(files.map(filename => { return Promise.all(files.map(filename => {
// Only parse the top-level text files, not any sub folders. // Only parse the top-level text files, not any sub folders.
...@@ -115,8 +117,8 @@ export default class API { ...@@ -115,8 +117,8 @@ export default class API {
// Chop the array of content strings into an array of sentences. // Chop the array of content strings into an array of sentences.
.then((values) => { .then((values: string[]) => {
let sentences = []; let sentences: string[] = [];
let sentenceArrays = values.map(fileContents => { let sentenceArrays = values.map(fileContents => {
if (!fileContents) { if (!fileContents) {
return []; return [];
...@@ -131,7 +133,7 @@ export default class API { ...@@ -131,7 +133,7 @@ export default class API {
console.log('sentences found', sentences.length); console.log('sentences found', sentences.length);
this.sentencesCache = sentences; this.sentencesCache = sentences;
}) })
.catch(err => { .catch((err: any) => {
console.error('could not retrieve sentences', err); console.error('could not retrieve sentences', err);
}); });
} }
...@@ -144,7 +146,7 @@ export default class API { ...@@ -144,7 +146,7 @@ export default class API {
this.getSentences().then((sentences: String[]) => { this.getSentences().then((sentences: String[]) => {
return this.getRandomSentences(count); return this.getRandomSentences(count);
}).then(randoms => { }).then((randoms: string[]) => {
respond(response, randoms.join('\n')); respond(response, randoms.join('\n'));
}).catch((err: any) => { }).catch((err: any) => {
console.error('Could not load sentences', err); console.error('Could not load sentences', err);
......
...@@ -57,13 +57,13 @@ export default class Clip { ...@@ -57,13 +57,13 @@ export default class Clip {
let retrieveParam = {Bucket: BUCKET_NAME, Key: key}; let retrieveParam = {Bucket: BUCKET_NAME, Key: key};
let awsResult = this.s3.getObject(retrieveParam); let awsResult = this.s3.getObject(retrieveParam);
f.pass(awsResult); f.pass(awsResult);
}, (awsResult) => { }, (awsResult: any) => {
let tmpFile = fs.createWriteStream(tmpFilePath); let tmpFile = fs.createWriteStream(tmpFilePath);
tmpFile = awsResult.createReadStream().pipe(tmpFile); tmpFile = awsResult.createReadStream().pipe(tmpFile);
tmpFile.on('finish', f.wait()); tmpFile.on('finish', f.wait());
}, () => { }, () => {
ms.pipe(request, response, tmpFilePath); ms.pipe(request, response, tmpFilePath);
}).onError(err => { }).onError((err: any) => {
console.error('streaming audio error', err, err.stack); console.error('streaming audio error', err, err.stack);
respond(response, 'Server error, could not fetch audio data.', 500); respond(response, 'Server error, could not fetch audio data.', 500);
}); });
...@@ -273,14 +273,14 @@ export default class Clip { ...@@ -273,14 +273,14 @@ export default class Clip {
// If we were given base64, we'll need to concat it all first // If we were given base64, we'll need to concat it all first
// So we can decode it in the next step. // So we can decode it in the next step.
if (contentType.includes('base64')) { if (contentType.includes('base64')) {
let chunks = []; let chunks: Buffer[] = [];
f.pass(chunks); f.pass(chunks);
request.on('data', (chunk: Buffer) => { request.on('data', (chunk: Buffer) => {
chunks.push(chunk); chunks.push(chunk);
}); });
request.on('end', f.wait()); request.on('end', f.wait());
} }
}, (chunks) => { }, (chunks: Buffer[]) => {
// If upload was base64, make sure we decode it first. // If upload was base64, make sure we decode it first.
if (contentType.includes('base64')) { if (contentType.includes('base64')) {
......
...@@ -9,7 +9,7 @@ export default class BaseDB { ...@@ -9,7 +9,7 @@ export default class BaseDB {
constructor( constructor(
public mysql: Mysql, public mysql: Mysql,
public name: string, public name: string,
public columns: Object, public columns: any,
public index: string public index: string
) {} ) {}
...@@ -17,9 +17,9 @@ export default class BaseDB { ...@@ -17,9 +17,9 @@ export default class BaseDB {
* Query database, but using promises. * Query database, but using promises.
*/ */
q(text: string, values?: any[]) { q(text: string, values?: any[]) {
return new Promise((resolve: (rows: any[]) => void,
return new Promise((resolve, reject) => { reject: (error: any) => void) => {
this.mysql.query(text,null,(error, results) => { this.mysql.query(text, null, (error: any, results: any) => {
error ? reject(error) : resolve(results); error ? reject(error) : resolve(results);
}); });
}) })
......
...@@ -67,7 +67,7 @@ export default class Mysql { ...@@ -67,7 +67,7 @@ export default class Mysql {
} }
query(text: string, values: any[], callback: Function) { query(text: string, values: any[], callback: Function) {
this.pool.query(text, function (error, results, fields) { this.pool.query(text, function (error: any, results: any, fields: any) {
error ? callback(error.message, null) : callback(null, results); error ? callback(error.message, null) : callback(null, results);
}); });
} }
......
...@@ -18,13 +18,21 @@ const CONFIG_PATH = path.resolve(__dirname, '../../..', ...@@ -18,13 +18,21 @@ const CONFIG_PATH = path.resolve(__dirname, '../../..',
const config = require(CONFIG_PATH); const config = require(CONFIG_PATH);
const BUCKET_NAME = config.BUCKET_NAME || 'common-voice-corpus'; const BUCKET_NAME = config.BUCKET_NAME || 'common-voice-corpus';
interface FileData {
sentence: string;
text: string;
sound: string;
votes: number;
pushed: boolean;
}
interface FileHolder {
[key: string]: FileData;
}
export default class Files { export default class Files {
private s3: any; private s3: any;
private files: { private files: FileHolder;
// fileGlob: [
// sentence: 'the text of the sentenct'
// ]
};
private paths: string[]; private paths: string[];
private votes: number; private votes: number;
private validated: number; private validated: number;
...@@ -53,7 +61,8 @@ export default class Files { ...@@ -53,7 +61,8 @@ export default class Files {
*/ */
private fetchSentenceFromS3(glob: string): Promise<string> { private fetchSentenceFromS3(glob: string): Promise<string> {
let key = glob + TEXT_EXT; let key = glob + TEXT_EXT;
return new Promise((res, rej) => { return new Promise((res: (stentence: string) => void,
rej: (error: any) => void) => {
let glob = this.getGlob(key); let glob = this.getGlob(key);
let params = {Bucket: BUCKET_NAME, Key: key}; let params = {Bucket: BUCKET_NAME, Key: key};
this.s3.getObject(params, (err: any, s3Data: any) => { this.s3.getObject(params, (err: any, s3Data: any) => {
...@@ -117,7 +126,7 @@ export default class Files { ...@@ -117,7 +126,7 @@ export default class Files {
}); });
let startRequest = Date.now(); let startRequest = Date.now();
awsRequest.on('success', (response) => { awsRequest.on('success', (response: any) => {
let next = response['data']['NextContinuationToken']; let next = response['data']['NextContinuationToken'];
let contents = response['data']['Contents']; let contents = response['data']['Contents'];
...@@ -140,7 +149,10 @@ export default class Files { ...@@ -140,7 +149,10 @@ export default class Files {
if (!this.files[glob]) { if (!this.files[glob]) {
this.files[glob] = { this.files[glob] = {
pushed: false, pushed: false,
votes: 0 sentence: null,
text: null,
sound: null,
votes: 0,
}; };
} }
...@@ -181,7 +193,7 @@ export default class Files { ...@@ -181,7 +193,7 @@ export default class Files {
}, LOAD_DELAY); }, LOAD_DELAY);
}); });
awsRequest.on('error', (response) => { awsRequest.on('error', (response: any) => {
console.error('Error while fetching clip list', response); console.error('Error while fetching clip list', response);
// Retry loading current batch. // Retry loading current batch.
...@@ -197,7 +209,7 @@ export default class Files { ...@@ -197,7 +209,7 @@ export default class Files {
* Load sound file metadata into memory. * Load sound file metadata into memory.
*/ */
private loadCache(): Promise<void> { private loadCache(): Promise<void> {
return new Promise((res, rej) => { return new Promise((res: Function, rej: Function) => {
this.loadNext(res, rej); this.loadNext(res, rej);
}); });
} }
......
...@@ -57,11 +57,11 @@ export default class Logger { ...@@ -57,11 +57,11 @@ export default class Logger {
} }
} }
log(...args) { log(...args: any[]) {
this.printFields(this.getMessageFields(LEVEL_LOG, args.join(', '))); this.printFields(this.getMessageFields(LEVEL_LOG, args.join(', ')));
} }
error(...args) { error(...args: any[]) {
this.printFields(this.getMessageFields(LEVEL_ERROR, args.join(', '))); this.printFields(this.getMessageFields(LEVEL_ERROR, args.join(', ')));
} }
...@@ -73,13 +73,13 @@ export default class Logger { ...@@ -73,13 +73,13 @@ export default class Logger {
// Override console.log to user our json logger. // Override console.log to user our json logger.
this.boundLog = console.log.bind(console); this.boundLog = console.log.bind(console);
console.log = (...args) => { console.log = (...args: any[]) => {
this.log(...args); this.log(...args);
} }
// Override console.error to user our json logger. // Override console.error to user our json logger.
this.boundError = console.error.bind(console); this.boundError = console.error.bind(console);
console.error = (...args) => { console.error = (...args: any[]) => {
this.error(...args); this.error(...args);
} }
} }
......
...@@ -22,7 +22,7 @@ export default function respond(response: ServerResponse, ...@@ -22,7 +22,7 @@ export default function respond(response: ServerResponse,
content: string = '', content: string = '',
statusCode: number = 200, statusCode: number = 200,
contentType: string = CONTENT_TYPES.TXT, contentType: string = CONTENT_TYPES.TXT,
headers: object = {}, headers: any = {},
characterEncoding: string = 'utf-8') { characterEncoding: string = 'utf-8') {
headers['Content-Type'] = contentType; headers['Content-Type'] = contentType;
......
...@@ -13,7 +13,7 @@ export function getFileExt(path: string): string { ...@@ -13,7 +13,7 @@ export function getFileExt(path: string): string {
* Returns the first defined argument. Returns null if there are no defined * Returns the first defined argument. Returns null if there are no defined
* arguments. * arguments.
*/ */
export function getFirstDefined(...options) { export function getFirstDefined(...options: any[]) {
for (var i = 0; i < options.length; i++) { for (var i = 0; i < options.length; i++) {
if (options[i] !== undefined) { if (options[i] !== undefined) {
return options[i]; return options[i];
......
...@@ -92,7 +92,7 @@ export default class Server { ...@@ -92,7 +92,7 @@ export default class Server {
} }
} }
process.on('uncaughtException', function(err) { process.on('uncaughtException', function(err: any) {
console.error('uncaught exception', err); console.error('uncaught exception', err);
}); });
......
{ {
"compilerOptions": { "compilerOptions": {
"target": "es5", "target": "es5",
"noImplicitAny": false, "noImplicitAny": true,
"strictNullChecks": false, "strictNullChecks": false,
"noEmitOnError": true, "noEmitOnError": true,
"typeRoots": [ "../node_modules/@types" ], "typeRoots": [ "../node_modules/@types" ],
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment