From 3a4992633ee62d5edfbb484d9c6bcb3cf158489d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 31 Jul 2023 14:34:36 +0200 Subject: Migrate server to ESM Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports) --- server/helpers/custom-validators/misc.ts | 190 ------------------------------- 1 file changed, 190 deletions(-) delete mode 100644 server/helpers/custom-validators/misc.ts (limited to 'server/helpers/custom-validators/misc.ts') diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts deleted file mode 100644 index 937ae0632..000000000 --- a/server/helpers/custom-validators/misc.ts +++ /dev/null @@ -1,190 +0,0 @@ -import 'multer' -import { UploadFilesForCheck } from 'express' -import { sep } from 'path' -import validator from 'validator' -import { isShortUUID, shortToUUID } from '@shared/extra-utils' - -function exists (value: any) { - return value !== undefined && value !== null -} - -function isSafePath (p: string) { - return exists(p) && - (p + '').split(sep).every(part => { - return [ '..' ].includes(part) === false - }) -} - -function isSafeFilename (filename: string, extension?: string) { - const regex = extension - ? new RegExp(`^[a-z0-9-]+\\.${extension}$`) - : new RegExp(`^[a-z0-9-]+\\.[a-z0-9]{1,8}$`) - - return typeof filename === 'string' && !!filename.match(regex) -} - -function isSafePeerTubeFilenameWithoutExtension (filename: string) { - return filename.match(/^[a-z0-9-]+$/) -} - -function isArray (value: any): value is any[] { - return Array.isArray(value) -} - -function isNotEmptyIntArray (value: any) { - return Array.isArray(value) && value.every(v => validator.isInt('' + v)) && value.length !== 0 -} - -function isNotEmptyStringArray (value: any) { - return Array.isArray(value) && value.every(v => typeof v === 'string' && v.length !== 0) && value.length !== 0 -} - -function isArrayOf (value: any, validator: (value: any) => boolean) { - return isArray(value) && value.every(v => validator(v)) -} - -function isDateValid (value: string) { - return exists(value) && validator.isISO8601(value) -} - -function isIdValid (value: string) { - return exists(value) && validator.isInt('' + value) -} - -function isUUIDValid (value: string) { - return exists(value) && validator.isUUID('' + value, 4) -} - -function areUUIDsValid (values: string[]) { - return isArray(values) && values.every(v => isUUIDValid(v)) -} - -function isIdOrUUIDValid (value: string) { - return isIdValid(value) || isUUIDValid(value) -} - -function isBooleanValid (value: any) { - return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value)) -} - -function isIntOrNull (value: any) { - return value === null || validator.isInt('' + value) -} - -// --------------------------------------------------------------------------- - -function isFileValid (options: { - files: UploadFilesForCheck - - maxSize: number | null - mimeTypeRegex: string | null - - field?: string - - optional?: boolean // Default false -}) { - const { files, mimeTypeRegex, field, maxSize, optional = false } = options - - // Should have files - if (!files) return optional - - const fileArray = isArray(files) - ? files - : files[field] - - if (!fileArray || !isArray(fileArray) || fileArray.length === 0) { - return optional - } - - // The file exists - const file = fileArray[0] - if (!file?.originalname) return false - - // Check size - if ((maxSize !== null) && file.size > maxSize) return false - - if (mimeTypeRegex === null) return true - - return checkMimetypeRegex(file.mimetype, mimeTypeRegex) -} - -function checkMimetypeRegex (fileMimeType: string, mimeTypeRegex: string) { - return new RegExp(`^${mimeTypeRegex}$`, 'i').test(fileMimeType) -} - -// --------------------------------------------------------------------------- - -function toCompleteUUID (value: string) { - if (isShortUUID(value)) { - try { - return shortToUUID(value) - } catch { - return '' - } - } - - return value -} - -function toCompleteUUIDs (values: string[]) { - return values.map(v => toCompleteUUID(v)) -} - -function toIntOrNull (value: string) { - const v = toValueOrNull(value) - - if (v === null || v === undefined) return v - if (typeof v === 'number') return v - - return validator.toInt('' + v) -} - -function toBooleanOrNull (value: any) { - const v = toValueOrNull(value) - - if (v === null || v === undefined) return v - if (typeof v === 'boolean') return v - - return validator.toBoolean('' + v) -} - -function toValueOrNull (value: string) { - if (value === 'null') return null - - return value -} - -function toIntArray (value: any) { - if (!value) return [] - if (isArray(value) === false) return [ validator.toInt(value) ] - - return value.map(v => validator.toInt(v)) -} - -// --------------------------------------------------------------------------- - -export { - exists, - isArrayOf, - isNotEmptyIntArray, - isArray, - isIntOrNull, - isIdValid, - isSafePath, - isNotEmptyStringArray, - isUUIDValid, - toCompleteUUIDs, - toCompleteUUID, - isIdOrUUIDValid, - isDateValid, - toValueOrNull, - toBooleanOrNull, - isBooleanValid, - toIntOrNull, - areUUIDsValid, - toIntArray, - isFileValid, - isSafePeerTubeFilenameWithoutExtension, - isSafeFilename, - checkMimetypeRegex -} -- cgit v1.2.3