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