diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-31 14:34:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-11 15:02:33 +0200 |
commit | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch) | |
tree | e4510b39bdac9c318fdb4b47018d08f15368b8f0 /packages/node-utils | |
parent | 04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff) | |
download | PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip |
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)
Diffstat (limited to 'packages/node-utils')
-rw-r--r-- | packages/node-utils/package.json | 19 | ||||
-rw-r--r-- | packages/node-utils/src/crypto.ts | 20 | ||||
-rw-r--r-- | packages/node-utils/src/env.ts | 58 | ||||
-rw-r--r-- | packages/node-utils/src/file.ts | 11 | ||||
-rw-r--r-- | packages/node-utils/src/index.ts | 5 | ||||
-rw-r--r-- | packages/node-utils/src/path.ts | 50 | ||||
-rw-r--r-- | packages/node-utils/src/uuid.ts | 32 | ||||
-rw-r--r-- | packages/node-utils/tsconfig.json | 8 |
8 files changed, 203 insertions, 0 deletions
diff --git a/packages/node-utils/package.json b/packages/node-utils/package.json new file mode 100644 index 000000000..cd7d8ac80 --- /dev/null +++ b/packages/node-utils/package.json | |||
@@ -0,0 +1,19 @@ | |||
1 | { | ||
2 | "name": "@peertube/peertube-node-utils", | ||
3 | "private": true, | ||
4 | "version": "0.0.0", | ||
5 | "main": "dist/index.js", | ||
6 | "files": [ "dist" ], | ||
7 | "exports": { | ||
8 | "types": "./dist/index.d.ts", | ||
9 | "peertube:tsx": "./src/index.ts", | ||
10 | "default": "./dist/index.js" | ||
11 | }, | ||
12 | "type": "module", | ||
13 | "devDependencies": {}, | ||
14 | "scripts": { | ||
15 | "build": "tsc", | ||
16 | "watch": "tsc -w" | ||
17 | }, | ||
18 | "dependencies": {} | ||
19 | } | ||
diff --git a/packages/node-utils/src/crypto.ts b/packages/node-utils/src/crypto.ts new file mode 100644 index 000000000..1a583f1a0 --- /dev/null +++ b/packages/node-utils/src/crypto.ts | |||
@@ -0,0 +1,20 @@ | |||
1 | import { BinaryToTextEncoding, createHash } from 'crypto' | ||
2 | |||
3 | function sha256 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') { | ||
4 | return createHash('sha256').update(str).digest(encoding) | ||
5 | } | ||
6 | |||
7 | function sha1 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') { | ||
8 | return createHash('sha1').update(str).digest(encoding) | ||
9 | } | ||
10 | |||
11 | // high excluded | ||
12 | function randomInt (low: number, high: number) { | ||
13 | return Math.floor(Math.random() * (high - low) + low) | ||
14 | } | ||
15 | |||
16 | export { | ||
17 | randomInt, | ||
18 | sha256, | ||
19 | sha1 | ||
20 | } | ||
diff --git a/packages/node-utils/src/env.ts b/packages/node-utils/src/env.ts new file mode 100644 index 000000000..1a28f509e --- /dev/null +++ b/packages/node-utils/src/env.ts | |||
@@ -0,0 +1,58 @@ | |||
1 | export function parallelTests () { | ||
2 | return process.env.MOCHA_PARALLEL === 'true' | ||
3 | } | ||
4 | |||
5 | export function isGithubCI () { | ||
6 | return !!process.env.GITHUB_WORKSPACE | ||
7 | } | ||
8 | |||
9 | export function areHttpImportTestsDisabled () { | ||
10 | const disabled = process.env.DISABLE_HTTP_IMPORT_TESTS === 'true' | ||
11 | |||
12 | if (disabled) console.log('DISABLE_HTTP_IMPORT_TESTS env set to "true" so import tests are disabled') | ||
13 | |||
14 | return disabled | ||
15 | } | ||
16 | |||
17 | export function areMockObjectStorageTestsDisabled () { | ||
18 | const disabled = process.env.ENABLE_OBJECT_STORAGE_TESTS !== 'true' | ||
19 | |||
20 | if (disabled) console.log('ENABLE_OBJECT_STORAGE_TESTS env is not set to "true" so object storage tests are disabled') | ||
21 | |||
22 | return disabled | ||
23 | } | ||
24 | |||
25 | export function areScalewayObjectStorageTestsDisabled () { | ||
26 | if (areMockObjectStorageTestsDisabled()) return true | ||
27 | |||
28 | const enabled = process.env.OBJECT_STORAGE_SCALEWAY_KEY_ID && process.env.OBJECT_STORAGE_SCALEWAY_ACCESS_KEY | ||
29 | if (!enabled) { | ||
30 | console.log( | ||
31 | 'OBJECT_STORAGE_SCALEWAY_KEY_ID and/or OBJECT_STORAGE_SCALEWAY_ACCESS_KEY are not set, so scaleway object storage tests are disabled' | ||
32 | ) | ||
33 | |||
34 | return true | ||
35 | } | ||
36 | |||
37 | return false | ||
38 | } | ||
39 | |||
40 | export function isTestInstance () { | ||
41 | return process.env.NODE_ENV === 'test' | ||
42 | } | ||
43 | |||
44 | export function isDevInstance () { | ||
45 | return process.env.NODE_ENV === 'dev' | ||
46 | } | ||
47 | |||
48 | export function isTestOrDevInstance () { | ||
49 | return isTestInstance() || isDevInstance() | ||
50 | } | ||
51 | |||
52 | export function isProdInstance () { | ||
53 | return process.env.NODE_ENV === 'production' | ||
54 | } | ||
55 | |||
56 | export function getAppNumber () { | ||
57 | return process.env.NODE_APP_INSTANCE || '' | ||
58 | } | ||
diff --git a/packages/node-utils/src/file.ts b/packages/node-utils/src/file.ts new file mode 100644 index 000000000..89cf5fe0f --- /dev/null +++ b/packages/node-utils/src/file.ts | |||
@@ -0,0 +1,11 @@ | |||
1 | import { stat } from 'fs/promises' | ||
2 | |||
3 | async function getFileSize (path: string) { | ||
4 | const stats = await stat(path) | ||
5 | |||
6 | return stats.size | ||
7 | } | ||
8 | |||
9 | export { | ||
10 | getFileSize | ||
11 | } | ||
diff --git a/packages/node-utils/src/index.ts b/packages/node-utils/src/index.ts new file mode 100644 index 000000000..89f22e7d3 --- /dev/null +++ b/packages/node-utils/src/index.ts | |||
@@ -0,0 +1,5 @@ | |||
1 | export * from './crypto.js' | ||
2 | export * from './env.js' | ||
3 | export * from './file.js' | ||
4 | export * from './path.js' | ||
5 | export * from './uuid.js' | ||
diff --git a/packages/node-utils/src/path.ts b/packages/node-utils/src/path.ts new file mode 100644 index 000000000..1d569833e --- /dev/null +++ b/packages/node-utils/src/path.ts | |||
@@ -0,0 +1,50 @@ | |||
1 | import { basename, extname, isAbsolute, join, resolve } from 'path' | ||
2 | import { fileURLToPath } from 'url' | ||
3 | |||
4 | let rootPath: string | ||
5 | |||
6 | export function currentDir (metaUrl: string) { | ||
7 | return resolve(fileURLToPath(metaUrl), '..') | ||
8 | } | ||
9 | |||
10 | export function root (metaUrl?: string) { | ||
11 | if (rootPath) return rootPath | ||
12 | |||
13 | if (!metaUrl) { | ||
14 | metaUrl = import.meta.url | ||
15 | |||
16 | const filename = basename(metaUrl) === 'path.js' || basename(metaUrl) === 'path.ts' | ||
17 | if (!filename) throw new Error('meta url must be specified as this file has been bundled in another one') | ||
18 | } | ||
19 | |||
20 | rootPath = currentDir(metaUrl) | ||
21 | |||
22 | if (basename(rootPath) === 'src' || basename(rootPath) === 'dist') rootPath = resolve(rootPath, '..') | ||
23 | if ([ 'node-utils', 'peertube-cli', 'peertube-runner' ].includes(basename(rootPath))) rootPath = resolve(rootPath, '..') | ||
24 | if ([ 'packages', 'apps' ].includes(basename(rootPath))) rootPath = resolve(rootPath, '..') | ||
25 | if (basename(rootPath) === 'dist') rootPath = resolve(rootPath, '..') | ||
26 | |||
27 | return rootPath | ||
28 | } | ||
29 | |||
30 | export function buildPath (path: string) { | ||
31 | if (isAbsolute(path)) return path | ||
32 | |||
33 | return join(root(), path) | ||
34 | } | ||
35 | |||
36 | export function getLowercaseExtension (filename: string) { | ||
37 | const ext = extname(filename) || '' | ||
38 | |||
39 | return ext.toLowerCase() | ||
40 | } | ||
41 | |||
42 | export function buildAbsoluteFixturePath (path: string, customCIPath = false) { | ||
43 | if (isAbsolute(path)) return path | ||
44 | |||
45 | if (customCIPath && process.env.GITHUB_WORKSPACE) { | ||
46 | return join(process.env.GITHUB_WORKSPACE, 'fixtures', path) | ||
47 | } | ||
48 | |||
49 | return join(root(), 'packages', 'tests', 'fixtures', path) | ||
50 | } | ||
diff --git a/packages/node-utils/src/uuid.ts b/packages/node-utils/src/uuid.ts new file mode 100644 index 000000000..f158ec487 --- /dev/null +++ b/packages/node-utils/src/uuid.ts | |||
@@ -0,0 +1,32 @@ | |||
1 | import short from 'short-uuid' | ||
2 | |||
3 | const translator = short() | ||
4 | |||
5 | function buildUUID () { | ||
6 | return short.uuid() | ||
7 | } | ||
8 | |||
9 | function uuidToShort (uuid: string) { | ||
10 | if (!uuid) return uuid | ||
11 | |||
12 | return translator.fromUUID(uuid) | ||
13 | } | ||
14 | |||
15 | function shortToUUID (shortUUID: string) { | ||
16 | if (!shortUUID) return shortUUID | ||
17 | |||
18 | return translator.toUUID(shortUUID) | ||
19 | } | ||
20 | |||
21 | function isShortUUID (value: string) { | ||
22 | if (!value) return false | ||
23 | |||
24 | return value.length === translator.maxLength | ||
25 | } | ||
26 | |||
27 | export { | ||
28 | buildUUID, | ||
29 | uuidToShort, | ||
30 | shortToUUID, | ||
31 | isShortUUID | ||
32 | } | ||
diff --git a/packages/node-utils/tsconfig.json b/packages/node-utils/tsconfig.json new file mode 100644 index 000000000..58fa2330b --- /dev/null +++ b/packages/node-utils/tsconfig.json | |||
@@ -0,0 +1,8 @@ | |||
1 | { | ||
2 | "extends": "../../tsconfig.base.json", | ||
3 | "compilerOptions": { | ||
4 | "outDir": "./dist", | ||
5 | "rootDir": "src", | ||
6 | "tsBuildInfoFile": "./dist/.tsbuildinfo" | ||
7 | } | ||
8 | } | ||