aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/core-utils
diff options
context:
space:
mode:
Diffstat (limited to 'shared/core-utils')
-rw-r--r--shared/core-utils/common/array.ts (renamed from shared/core-utils/utils/array.ts)0
-rw-r--r--shared/core-utils/common/env.ts30
-rw-r--r--shared/core-utils/common/index.ts9
-rw-r--r--shared/core-utils/common/object.ts35
-rw-r--r--shared/core-utils/common/path.ts48
-rw-r--r--shared/core-utils/common/random.ts8
-rw-r--r--shared/core-utils/common/time.ts7
-rw-r--r--shared/core-utils/common/types.ts45
-rw-r--r--shared/core-utils/common/url.ts2
-rw-r--r--shared/core-utils/common/version.ts (renamed from shared/core-utils/common/miscs.ts)23
-rw-r--r--shared/core-utils/i18n/i18n.ts3
-rw-r--r--shared/core-utils/index.ts1
-rw-r--r--shared/core-utils/users/user-role.ts2
-rw-r--r--shared/core-utils/utils/index.ts2
-rw-r--r--shared/core-utils/utils/object.ts15
-rw-r--r--shared/core-utils/videos/bitrate.ts2
16 files changed, 143 insertions, 89 deletions
diff --git a/shared/core-utils/utils/array.ts b/shared/core-utils/common/array.ts
index 9e326a5aa..9e326a5aa 100644
--- a/shared/core-utils/utils/array.ts
+++ b/shared/core-utils/common/array.ts
diff --git a/shared/core-utils/common/env.ts b/shared/core-utils/common/env.ts
new file mode 100644
index 000000000..38c96b152
--- /dev/null
+++ b/shared/core-utils/common/env.ts
@@ -0,0 +1,30 @@
1function parallelTests () {
2 return process.env.MOCHA_PARALLEL === 'true'
3}
4
5function isGithubCI () {
6 return !!process.env.GITHUB_WORKSPACE
7}
8
9function 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
17function areObjectStorageTestsDisabled () {
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 {
26 parallelTests,
27 isGithubCI,
28 areHttpImportTestsDisabled,
29 areObjectStorageTestsDisabled
30}
diff --git a/shared/core-utils/common/index.ts b/shared/core-utils/common/index.ts
index 0908ff981..720977ead 100644
--- a/shared/core-utils/common/index.ts
+++ b/shared/core-utils/common/index.ts
@@ -1,6 +1,11 @@
1export * from './array'
2export * from './random'
1export * from './date' 3export * from './date'
2export * from './miscs' 4export * from './env'
5export * from './object'
6export * from './path'
3export * from './regexp' 7export * from './regexp'
8export * from './time'
4export * from './promises' 9export * from './promises'
5export * from './types'
6export * from './url' 10export * from './url'
11export * from './version'
diff --git a/shared/core-utils/common/object.ts b/shared/core-utils/common/object.ts
new file mode 100644
index 000000000..49d209819
--- /dev/null
+++ b/shared/core-utils/common/object.ts
@@ -0,0 +1,35 @@
1function pick <O extends object, K extends keyof O> (object: O, keys: K[]): Pick<O, K> {
2 const result: any = {}
3
4 for (const key of keys) {
5 if (Object.prototype.hasOwnProperty.call(object, key)) {
6 result[key] = object[key]
7 }
8 }
9
10 return result
11}
12
13function getKeys <O extends object, K extends keyof O> (object: O, keys: K[]): K[] {
14 return (Object.keys(object) as K[]).filter(k => keys.includes(k))
15}
16
17function sortObjectComparator (key: string, order: 'asc' | 'desc') {
18 return (a: any, b: any) => {
19 if (a[key] < b[key]) {
20 return order === 'asc' ? -1 : 1
21 }
22
23 if (a[key] > b[key]) {
24 return order === 'asc' ? 1 : -1
25 }
26
27 return 0
28 }
29}
30
31export {
32 pick,
33 getKeys,
34 sortObjectComparator
35}
diff --git a/shared/core-utils/common/path.ts b/shared/core-utils/common/path.ts
new file mode 100644
index 000000000..006505316
--- /dev/null
+++ b/shared/core-utils/common/path.ts
@@ -0,0 +1,48 @@
1import { basename, extname, isAbsolute, join, resolve } from 'path'
2
3let rootPath: string
4
5function root () {
6 if (rootPath) return rootPath
7
8 rootPath = __dirname
9
10 if (basename(rootPath) === 'tools') rootPath = resolve(rootPath, '..')
11 if (basename(rootPath) === 'scripts') rootPath = resolve(rootPath, '..')
12 if (basename(rootPath) === 'common') rootPath = resolve(rootPath, '..')
13 if (basename(rootPath) === 'core-utils') rootPath = resolve(rootPath, '..')
14 if (basename(rootPath) === 'shared') rootPath = resolve(rootPath, '..')
15 if (basename(rootPath) === 'server') rootPath = resolve(rootPath, '..')
16 if (basename(rootPath) === 'dist') rootPath = resolve(rootPath, '..')
17
18 return rootPath
19}
20
21function buildPath (path: string) {
22 if (isAbsolute(path)) return path
23
24 return join(root(), path)
25}
26
27function getLowercaseExtension (filename: string) {
28 const ext = extname(filename) || ''
29
30 return ext.toLowerCase()
31}
32
33function buildAbsoluteFixturePath (path: string, customCIPath = false) {
34 if (isAbsolute(path)) return path
35
36 if (customCIPath && process.env.GITHUB_WORKSPACE) {
37 return join(process.env.GITHUB_WORKSPACE, 'fixtures', path)
38 }
39
40 return join(root(), 'server', 'tests', 'fixtures', path)
41}
42
43export {
44 root,
45 buildPath,
46 buildAbsoluteFixturePath,
47 getLowercaseExtension
48}
diff --git a/shared/core-utils/common/random.ts b/shared/core-utils/common/random.ts
new file mode 100644
index 000000000..705735d09
--- /dev/null
+++ b/shared/core-utils/common/random.ts
@@ -0,0 +1,8 @@
1// high excluded
2function randomInt (low: number, high: number) {
3 return Math.floor(Math.random() * (high - low) + low)
4}
5
6export {
7 randomInt
8}
diff --git a/shared/core-utils/common/time.ts b/shared/core-utils/common/time.ts
new file mode 100644
index 000000000..2992609ca
--- /dev/null
+++ b/shared/core-utils/common/time.ts
@@ -0,0 +1,7 @@
1function wait (milliseconds: number) {
2 return new Promise(resolve => setTimeout(resolve, milliseconds))
3}
4
5export {
6 wait
7}
diff --git a/shared/core-utils/common/types.ts b/shared/core-utils/common/types.ts
deleted file mode 100644
index bd2a97b98..000000000
--- a/shared/core-utils/common/types.ts
+++ /dev/null
@@ -1,45 +0,0 @@
1/* eslint-disable @typescript-eslint/array-type */
2
3export type FunctionPropertyNames<T> = {
4 [K in keyof T]: T[K] extends Function ? K : never
5}[keyof T]
6
7export type FunctionProperties<T> = Pick<T, FunctionPropertyNames<T>>
8
9export type AttributesOnly<T> = {
10 [K in keyof T]: T[K] extends Function ? never : T[K]
11}
12
13export type PickWith<T, KT extends keyof T, V> = {
14 [P in KT]: T[P] extends V ? V : never
15}
16
17export type PickWithOpt<T, KT extends keyof T, V> = {
18 [P in KT]?: T[P] extends V ? V : never
19}
20
21// https://github.com/krzkaczor/ts-essentials Rocks!
22export type DeepPartial<T> = {
23 [P in keyof T]?: T[P] extends Array<infer U>
24 ? Array<DeepPartial<U>>
25 : T[P] extends ReadonlyArray<infer U>
26 ? ReadonlyArray<DeepPartial<U>>
27 : DeepPartial<T[P]>
28}
29
30type Primitive = string | Function | number | boolean | Symbol | undefined | null
31export type DeepOmitHelper<T, K extends keyof T> = {
32 [P in K]: // extra level of indirection needed to trigger homomorhic behavior
33 T[P] extends infer TP // distribute over unions
34 ? TP extends Primitive
35 ? TP // leave primitives and functions alone
36 : TP extends any[]
37 ? DeepOmitArray<TP, K> // Array special handling
38 : DeepOmit<TP, K>
39 : never
40}
41export type DeepOmit<T, K> = T extends Primitive ? T : DeepOmitHelper<T, Exclude<keyof T, K>>
42
43export type DeepOmitArray<T extends any[], K> = {
44 [P in keyof T]: DeepOmit<T[P], K>
45}
diff --git a/shared/core-utils/common/url.ts b/shared/core-utils/common/url.ts
index 9c111cbcc..8020d9b28 100644
--- a/shared/core-utils/common/url.ts
+++ b/shared/core-utils/common/url.ts
@@ -50,6 +50,7 @@ function decorateVideoLink (options: {
50 warningTitle?: boolean 50 warningTitle?: boolean
51 controls?: boolean 51 controls?: boolean
52 peertubeLink?: boolean 52 peertubeLink?: boolean
53 p2p?: boolean
53}) { 54}) {
54 const { url } = options 55 const { url } = options
55 56
@@ -74,6 +75,7 @@ function decorateVideoLink (options: {
74 if (options.warningTitle === false) params.set('warningTitle', '0') 75 if (options.warningTitle === false) params.set('warningTitle', '0')
75 if (options.controls === false) params.set('controls', '0') 76 if (options.controls === false) params.set('controls', '0')
76 if (options.peertubeLink === false) params.set('peertubeLink', '0') 77 if (options.peertubeLink === false) params.set('peertubeLink', '0')
78 if (options.p2p !== undefined) params.set('p2p', options.p2p ? '1' : '0')
77 79
78 return buildUrl(url, params) 80 return buildUrl(url, params)
79} 81}
diff --git a/shared/core-utils/common/miscs.ts b/shared/core-utils/common/version.ts
index bc65dc338..8a64f8c4d 100644
--- a/shared/core-utils/common/miscs.ts
+++ b/shared/core-utils/common/version.ts
@@ -1,8 +1,3 @@
1// high excluded
2function randomInt (low: number, high: number) {
3 return Math.floor(Math.random() * (high - low) + low)
4}
5
6// Thanks https://stackoverflow.com/a/16187766 1// Thanks https://stackoverflow.com/a/16187766
7function compareSemVer (a: string, b: string) { 2function compareSemVer (a: string, b: string) {
8 const regExStrip0 = /(\.0+)+$/ 3 const regExStrip0 = /(\.0+)+$/
@@ -20,22 +15,6 @@ function compareSemVer (a: string, b: string) {
20 return segmentsA.length - segmentsB.length 15 return segmentsA.length - segmentsB.length
21} 16}
22 17
23function sortObjectComparator (key: string, order: 'asc' | 'desc') {
24 return (a: any, b: any) => {
25 if (a[key] < b[key]) {
26 return order === 'asc' ? -1 : 1
27 }
28
29 if (a[key] > b[key]) {
30 return order === 'asc' ? 1 : -1
31 }
32
33 return 0
34 }
35}
36
37export { 18export {
38 randomInt, 19 compareSemVer
39 compareSemVer,
40 sortObjectComparator
41} 20}
diff --git a/shared/core-utils/i18n/i18n.ts b/shared/core-utils/i18n/i18n.ts
index f27de20f1..ae6af8192 100644
--- a/shared/core-utils/i18n/i18n.ts
+++ b/shared/core-utils/i18n/i18n.ts
@@ -28,6 +28,8 @@ export const I18N_LOCALES = {
28 'ru-RU': 'русский', 28 'ru-RU': 'русский',
29 'sq': 'Shqip', 29 'sq': 'Shqip',
30 'sv-SE': 'Svenska', 30 'sv-SE': 'Svenska',
31 'nn': 'norsk nynorsk',
32 'nb-NO': 'norsk bokmål',
31 'th-TH': 'ไทย', 33 'th-TH': 'ไทย',
32 'vi-VN': 'Tiếng Việt', 34 'vi-VN': 'Tiếng Việt',
33 'zh-Hans-CN': '简体中文(中国)', 35 'zh-Hans-CN': '简体中文(中国)',
@@ -52,6 +54,7 @@ const I18N_LOCALE_ALIAS = {
52 'nl': 'nl-NL', 54 'nl': 'nl-NL',
53 'pl': 'pl-PL', 55 'pl': 'pl-PL',
54 'pt': 'pt-BR', 56 'pt': 'pt-BR',
57 'nb': 'nb-NO',
55 'ru': 'ru-RU', 58 'ru': 'ru-RU',
56 'sv': 'sv-SE', 59 'sv': 'sv-SE',
57 'th': 'th-TH', 60 'th': 'th-TH',
diff --git a/shared/core-utils/index.ts b/shared/core-utils/index.ts
index e0a6a8087..8daaa2d04 100644
--- a/shared/core-utils/index.ts
+++ b/shared/core-utils/index.ts
@@ -4,5 +4,4 @@ export * from './i18n'
4export * from './plugins' 4export * from './plugins'
5export * from './renderer' 5export * from './renderer'
6export * from './users' 6export * from './users'
7export * from './utils'
8export * from './videos' 7export * from './videos'
diff --git a/shared/core-utils/users/user-role.ts b/shared/core-utils/users/user-role.ts
index 81cba1dad..cc757d779 100644
--- a/shared/core-utils/users/user-role.ts
+++ b/shared/core-utils/users/user-role.ts
@@ -14,8 +14,8 @@ const userRoleRights: { [ id in UserRole ]: UserRight[] } = {
14 [UserRole.MODERATOR]: [ 14 [UserRole.MODERATOR]: [
15 UserRight.MANAGE_VIDEO_BLACKLIST, 15 UserRight.MANAGE_VIDEO_BLACKLIST,
16 UserRight.MANAGE_ABUSES, 16 UserRight.MANAGE_ABUSES,
17 UserRight.MANAGE_ANY_VIDEO_CHANNEL,
17 UserRight.REMOVE_ANY_VIDEO, 18 UserRight.REMOVE_ANY_VIDEO,
18 UserRight.REMOVE_ANY_VIDEO_CHANNEL,
19 UserRight.REMOVE_ANY_VIDEO_PLAYLIST, 19 UserRight.REMOVE_ANY_VIDEO_PLAYLIST,
20 UserRight.REMOVE_ANY_VIDEO_COMMENT, 20 UserRight.REMOVE_ANY_VIDEO_COMMENT,
21 UserRight.UPDATE_ANY_VIDEO, 21 UserRight.UPDATE_ANY_VIDEO,
diff --git a/shared/core-utils/utils/index.ts b/shared/core-utils/utils/index.ts
deleted file mode 100644
index 8d16365a8..000000000
--- a/shared/core-utils/utils/index.ts
+++ /dev/null
@@ -1,2 +0,0 @@
1export * from './array'
2export * from './object'
diff --git a/shared/core-utils/utils/object.ts b/shared/core-utils/utils/object.ts
deleted file mode 100644
index 9a8a98f9b..000000000
--- a/shared/core-utils/utils/object.ts
+++ /dev/null
@@ -1,15 +0,0 @@
1function pick <O extends object, K extends keyof O> (object: O, keys: K[]): Pick<O, K> {
2 const result: any = {}
3
4 for (const key of keys) {
5 if (Object.prototype.hasOwnProperty.call(object, key)) {
6 result[key] = object[key]
7 }
8 }
9
10 return result
11}
12
13export {
14 pick
15}
diff --git a/shared/core-utils/videos/bitrate.ts b/shared/core-utils/videos/bitrate.ts
index c1891188f..30d22df09 100644
--- a/shared/core-utils/videos/bitrate.ts
+++ b/shared/core-utils/videos/bitrate.ts
@@ -1,4 +1,4 @@
1import { VideoResolution } from "@shared/models" 1import { VideoResolution } from '@shared/models'
2 2
3type BitPerPixel = { [ id in VideoResolution ]: number } 3type BitPerPixel = { [ id in VideoResolution ]: number }
4 4