mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-02-25 18:55:32 -06:00
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:
19
packages/node-utils/package.json
Normal file
19
packages/node-utils/package.json
Normal 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": {}
|
||||
}
|
||||
20
packages/node-utils/src/crypto.ts
Normal file
20
packages/node-utils/src/crypto.ts
Normal 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
|
||||
}
|
||||
58
packages/node-utils/src/env.ts
Normal file
58
packages/node-utils/src/env.ts
Normal 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 || ''
|
||||
}
|
||||
11
packages/node-utils/src/file.ts
Normal file
11
packages/node-utils/src/file.ts
Normal 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
|
||||
}
|
||||
5
packages/node-utils/src/index.ts
Normal file
5
packages/node-utils/src/index.ts
Normal 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'
|
||||
50
packages/node-utils/src/path.ts
Normal file
50
packages/node-utils/src/path.ts
Normal 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)
|
||||
}
|
||||
32
packages/node-utils/src/uuid.ts
Normal file
32
packages/node-utils/src/uuid.ts
Normal 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
|
||||
}
|
||||
8
packages/node-utils/tsconfig.json
Normal file
8
packages/node-utils/tsconfig.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"rootDir": "src",
|
||||
"tsBuildInfoFile": "./dist/.tsbuildinfo"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user