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)
This commit is contained in:
Chocobozzz
2023-07-31 14:34:36 +02:00
parent 04d1da5621
commit 3a4992633e
2196 changed files with 12690 additions and 11574 deletions

View File

@@ -0,0 +1,19 @@
{
"name": "@peertube/peertube-node-utils",
"private": true,
"version": "0.0.0",
"main": "dist/index.js",
"files": [ "dist" ],
"exports": {
"types": "./dist/index.d.ts",
"peertube:tsx": "./src/index.ts",
"default": "./dist/index.js"
},
"type": "module",
"devDependencies": {},
"scripts": {
"build": "tsc",
"watch": "tsc -w"
},
"dependencies": {}
}

View File

@@ -0,0 +1,20 @@
import { BinaryToTextEncoding, createHash } from 'crypto'
function sha256 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') {
return createHash('sha256').update(str).digest(encoding)
}
function sha1 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') {
return createHash('sha1').update(str).digest(encoding)
}
// high excluded
function randomInt (low: number, high: number) {
return Math.floor(Math.random() * (high - low) + low)
}
export {
randomInt,
sha256,
sha1
}

View File

@@ -0,0 +1,58 @@
export function parallelTests () {
return process.env.MOCHA_PARALLEL === 'true'
}
export function isGithubCI () {
return !!process.env.GITHUB_WORKSPACE
}
export function areHttpImportTestsDisabled () {
const disabled = process.env.DISABLE_HTTP_IMPORT_TESTS === 'true'
if (disabled) console.log('DISABLE_HTTP_IMPORT_TESTS env set to "true" so import tests are disabled')
return disabled
}
export function areMockObjectStorageTestsDisabled () {
const disabled = process.env.ENABLE_OBJECT_STORAGE_TESTS !== 'true'
if (disabled) console.log('ENABLE_OBJECT_STORAGE_TESTS env is not set to "true" so object storage tests are disabled')
return disabled
}
export function areScalewayObjectStorageTestsDisabled () {
if (areMockObjectStorageTestsDisabled()) return true
const enabled = process.env.OBJECT_STORAGE_SCALEWAY_KEY_ID && process.env.OBJECT_STORAGE_SCALEWAY_ACCESS_KEY
if (!enabled) {
console.log(
'OBJECT_STORAGE_SCALEWAY_KEY_ID and/or OBJECT_STORAGE_SCALEWAY_ACCESS_KEY are not set, so scaleway object storage tests are disabled'
)
return true
}
return false
}
export function isTestInstance () {
return process.env.NODE_ENV === 'test'
}
export function isDevInstance () {
return process.env.NODE_ENV === 'dev'
}
export function isTestOrDevInstance () {
return isTestInstance() || isDevInstance()
}
export function isProdInstance () {
return process.env.NODE_ENV === 'production'
}
export function getAppNumber () {
return process.env.NODE_APP_INSTANCE || ''
}

View File

@@ -0,0 +1,11 @@
import { stat } from 'fs/promises'
async function getFileSize (path: string) {
const stats = await stat(path)
return stats.size
}
export {
getFileSize
}

View File

@@ -0,0 +1,5 @@
export * from './crypto.js'
export * from './env.js'
export * from './file.js'
export * from './path.js'
export * from './uuid.js'

View File

@@ -0,0 +1,50 @@
import { basename, extname, isAbsolute, join, resolve } from 'path'
import { fileURLToPath } from 'url'
let rootPath: string
export function currentDir (metaUrl: string) {
return resolve(fileURLToPath(metaUrl), '..')
}
export function root (metaUrl?: string) {
if (rootPath) return rootPath
if (!metaUrl) {
metaUrl = import.meta.url
const filename = basename(metaUrl) === 'path.js' || basename(metaUrl) === 'path.ts'
if (!filename) throw new Error('meta url must be specified as this file has been bundled in another one')
}
rootPath = currentDir(metaUrl)
if (basename(rootPath) === 'src' || basename(rootPath) === 'dist') rootPath = resolve(rootPath, '..')
if ([ 'node-utils', 'peertube-cli', 'peertube-runner' ].includes(basename(rootPath))) rootPath = resolve(rootPath, '..')
if ([ 'packages', 'apps' ].includes(basename(rootPath))) rootPath = resolve(rootPath, '..')
if (basename(rootPath) === 'dist') rootPath = resolve(rootPath, '..')
return rootPath
}
export function buildPath (path: string) {
if (isAbsolute(path)) return path
return join(root(), path)
}
export function getLowercaseExtension (filename: string) {
const ext = extname(filename) || ''
return ext.toLowerCase()
}
export function buildAbsoluteFixturePath (path: string, customCIPath = false) {
if (isAbsolute(path)) return path
if (customCIPath && process.env.GITHUB_WORKSPACE) {
return join(process.env.GITHUB_WORKSPACE, 'fixtures', path)
}
return join(root(), 'packages', 'tests', 'fixtures', path)
}

View File

@@ -0,0 +1,32 @@
import short from 'short-uuid'
const translator = short()
function buildUUID () {
return short.uuid()
}
function uuidToShort (uuid: string) {
if (!uuid) return uuid
return translator.fromUUID(uuid)
}
function shortToUUID (shortUUID: string) {
if (!shortUUID) return shortUUID
return translator.toUUID(shortUUID)
}
function isShortUUID (value: string) {
if (!value) return false
return value.length === translator.maxLength
}
export {
buildUUID,
uuidToShort,
shortToUUID,
isShortUUID
}

View File

@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "src",
"tsBuildInfoFile": "./dist/.tsbuildinfo"
}
}