From c1e791bad0b079af67398f6407221e6dcbb573dd Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Wed, 25 Jul 2018 22:01:25 +0200 Subject: expliciting type checks and predicates (server only) --- server/helpers/activitypub.ts | 4 ++-- server/helpers/core-utils.ts | 3 ++- server/helpers/custom-validators/misc.ts | 4 ++-- server/helpers/custom-validators/videos.ts | 8 ++++---- server/helpers/express-utils.ts | 2 +- server/helpers/logger.ts | 3 ++- server/helpers/utils.ts | 3 ++- 7 files changed, 15 insertions(+), 12 deletions(-) (limited to 'server/helpers') diff --git a/server/helpers/activitypub.ts b/server/helpers/activitypub.ts index c49142a04..d710f5c97 100644 --- a/server/helpers/activitypub.ts +++ b/server/helpers/activitypub.ts @@ -67,8 +67,8 @@ async function activityPubCollectionPagination (url: string, handler: ActivityPu const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) const result = await handler(start, count) - let next: string - let prev: string + let next: string | undefined + let prev: string | undefined // Assert page is a number page = parseInt(page, 10) diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts index c560222d3..2951aef1e 100644 --- a/server/helpers/core-utils.ts +++ b/server/helpers/core-utils.ts @@ -42,7 +42,7 @@ function root () { const paths = [ __dirname, '..', '..' ] // We are under /dist directory - if (process.mainModule.filename.endsWith('.ts') === false) { + if (process.mainModule && process.mainModule.filename.endsWith('.ts') === false) { paths.push('..') } @@ -143,6 +143,7 @@ const renamePromise = promisify2WithVoid(rename) const writeFilePromise = promisify2WithVoid(writeFile) const readdirPromise = promisify1(readdir) const mkdirpPromise = promisify1(mkdirp) +// we cannot modify the Promise types, so we should make the promisify instance check mkdirp const pseudoRandomBytesPromise = promisify1(pseudoRandomBytes) const createPrivateKey = promisify1(pem.createPrivateKey) const getPublicKey = promisify1(pem.getPublicKey) diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts index 151fc852b..6d10a65a8 100644 --- a/server/helpers/custom-validators/misc.ts +++ b/server/helpers/custom-validators/misc.ts @@ -51,7 +51,7 @@ function isFileValid ( files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], mimeTypeRegex: string, field: string, - maxSize: number, + maxSize: number | null, optional = false ) { // Should have files @@ -69,7 +69,7 @@ function isFileValid ( if (!file || !file.originalname) return false // Check size - if (maxSize && file.size > maxSize) return false + if ((maxSize !== null) && file.size > maxSize) return false return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype) } diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index b5cb126d9..70904af0c 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts @@ -150,7 +150,7 @@ function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: Use } async function isVideoExist (id: string, res: Response) { - let video: VideoModel + let video: VideoModel | null if (validator.isInt(id)) { video = await VideoModel.loadAndPopulateAccountAndServerAndTags(+id) @@ -158,7 +158,7 @@ async function isVideoExist (id: string, res: Response) { video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(id) } - if (!video) { + if (video && video !== null) { res.status(404) .json({ error: 'Video not found' }) .end() @@ -173,7 +173,7 @@ async function isVideoExist (id: string, res: Response) { async function isVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) { if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) { const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId) - if (!videoChannel) { + if (videoChannel && videoChannel !== null) { res.status(400) .json({ error: 'Unknown video video channel on this instance.' }) .end() @@ -186,7 +186,7 @@ async function isVideoChannelOfAccountExist (channelId: number, user: UserModel, } const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id) - if (!videoChannel) { + if (videoChannel && videoChannel !== null) { res.status(400) .json({ error: 'Unknown video video channel for this account.' }) .end() diff --git a/server/helpers/express-utils.ts b/server/helpers/express-utils.ts index 76440348f..f136a4329 100644 --- a/server/helpers/express-utils.ts +++ b/server/helpers/express-utils.ts @@ -64,7 +64,7 @@ function createReqFiles ( } }) - const fields = [] + let fields: { name: string, maxCount: number }[] = [] for (const fieldName of fieldNames) { fields.push({ name: fieldName, diff --git a/server/helpers/logger.ts b/server/helpers/logger.ts index 04ddf01a6..7fdfe2125 100644 --- a/server/helpers/logger.ts +++ b/server/helpers/logger.ts @@ -80,7 +80,8 @@ const logger = winston.createLogger({ function bunyanLogFactory (level: string) { return function () { let meta = null - let args = [].concat(arguments) + let args: any[] = [] + args.concat(arguments) if (arguments[ 0 ] instanceof Error) { meta = arguments[ 0 ].toString() diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts index 9efc89d92..7ff1556e3 100644 --- a/server/helpers/utils.ts +++ b/server/helpers/utils.ts @@ -52,7 +52,7 @@ async function isSignupAllowed () { function isSignupAllowedForCurrentIP (ip: string) { const addr = ipaddr.parse(ip) let excludeList = [ 'blacklist' ] - let matched: string + let matched = '' // if there is a valid, non-empty whitelist, we exclude all unknown adresses too if (CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr(cidr)).length > 0) { @@ -144,6 +144,7 @@ let serverActor: ActorModel async function getServerActor () { if (serverActor === undefined) { const application = await ApplicationModel.load() + if (!application) throw Error('Could not application.') serverActor = application.Account.Actor } -- cgit v1.2.3