aboutsummaryrefslogtreecommitdiffhomepage
path: root/packages/node-utils
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /packages/node-utils
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-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.json19
-rw-r--r--packages/node-utils/src/crypto.ts20
-rw-r--r--packages/node-utils/src/env.ts58
-rw-r--r--packages/node-utils/src/file.ts11
-rw-r--r--packages/node-utils/src/index.ts5
-rw-r--r--packages/node-utils/src/path.ts50
-rw-r--r--packages/node-utils/src/uuid.ts32
-rw-r--r--packages/node-utils/tsconfig.json8
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 @@
1import { BinaryToTextEncoding, createHash } from 'crypto'
2
3function sha256 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') {
4 return createHash('sha256').update(str).digest(encoding)
5}
6
7function sha1 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') {
8 return createHash('sha1').update(str).digest(encoding)
9}
10
11// high excluded
12function randomInt (low: number, high: number) {
13 return Math.floor(Math.random() * (high - low) + low)
14}
15
16export {
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 @@
1export function parallelTests () {
2 return process.env.MOCHA_PARALLEL === 'true'
3}
4
5export function isGithubCI () {
6 return !!process.env.GITHUB_WORKSPACE
7}
8
9export 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
17export 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
25export 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
40export function isTestInstance () {
41 return process.env.NODE_ENV === 'test'
42}
43
44export function isDevInstance () {
45 return process.env.NODE_ENV === 'dev'
46}
47
48export function isTestOrDevInstance () {
49 return isTestInstance() || isDevInstance()
50}
51
52export function isProdInstance () {
53 return process.env.NODE_ENV === 'production'
54}
55
56export 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 @@
1import { stat } from 'fs/promises'
2
3async function getFileSize (path: string) {
4 const stats = await stat(path)
5
6 return stats.size
7}
8
9export {
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 @@
1export * from './crypto.js'
2export * from './env.js'
3export * from './file.js'
4export * from './path.js'
5export * 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 @@
1import { basename, extname, isAbsolute, join, resolve } from 'path'
2import { fileURLToPath } from 'url'
3
4let rootPath: string
5
6export function currentDir (metaUrl: string) {
7 return resolve(fileURLToPath(metaUrl), '..')
8}
9
10export 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
30export function buildPath (path: string) {
31 if (isAbsolute(path)) return path
32
33 return join(root(), path)
34}
35
36export function getLowercaseExtension (filename: string) {
37 const ext = extname(filename) || ''
38
39 return ext.toLowerCase()
40}
41
42export 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 @@
1import short from 'short-uuid'
2
3const translator = short()
4
5function buildUUID () {
6 return short.uuid()
7}
8
9function uuidToShort (uuid: string) {
10 if (!uuid) return uuid
11
12 return translator.fromUUID(uuid)
13}
14
15function shortToUUID (shortUUID: string) {
16 if (!shortUUID) return shortUUID
17
18 return translator.toUUID(shortUUID)
19}
20
21function isShortUUID (value: string) {
22 if (!value) return false
23
24 return value.length === translator.maxLength
25}
26
27export {
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}