diff options
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/crypto.ts (renamed from shared/core-utils/crypto.ts) | 6 | ||||
-rw-r--r-- | shared/core-utils/common/env.ts | 30 | ||||
-rw-r--r-- | shared/core-utils/common/index.ts | 8 | ||||
-rw-r--r-- | shared/core-utils/common/object.ts | 30 | ||||
-rw-r--r-- | shared/core-utils/common/path.ts (renamed from shared/core-utils/path.ts) | 12 | ||||
-rw-r--r-- | shared/core-utils/common/time.ts | 7 | ||||
-rw-r--r-- | shared/core-utils/common/version.ts (renamed from shared/core-utils/common/miscs.ts) | 23 | ||||
-rw-r--r-- | shared/core-utils/index.ts | 3 | ||||
-rw-r--r-- | shared/core-utils/utils/index.ts | 2 | ||||
-rw-r--r-- | shared/core-utils/utils/object.ts | 15 | ||||
-rw-r--r-- | shared/core-utils/videos/index.ts | 1 | ||||
-rw-r--r-- | shared/core-utils/videos/uuid.ts (renamed from shared/core-utils/uuid.ts) | 0 |
13 files changed, 94 insertions, 43 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/crypto.ts b/shared/core-utils/common/crypto.ts index d6d1150d0..1a583f1a0 100644 --- a/shared/core-utils/crypto.ts +++ b/shared/core-utils/common/crypto.ts | |||
@@ -8,7 +8,13 @@ function sha1 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') { | |||
8 | return createHash('sha1').update(str).digest(encoding) | 8 | return createHash('sha1').update(str).digest(encoding) |
9 | } | 9 | } |
10 | 10 | ||
11 | // high excluded | ||
12 | function randomInt (low: number, high: number) { | ||
13 | return Math.floor(Math.random() * (high - low) + low) | ||
14 | } | ||
15 | |||
11 | export { | 16 | export { |
17 | randomInt, | ||
12 | sha256, | 18 | sha256, |
13 | sha1 | 19 | sha1 |
14 | } | 20 | } |
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 @@ | |||
1 | function parallelTests () { | ||
2 | return process.env.MOCHA_PARALLEL === 'true' | ||
3 | } | ||
4 | |||
5 | function isGithubCI () { | ||
6 | return !!process.env.GITHUB_WORKSPACE | ||
7 | } | ||
8 | |||
9 | 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 | function 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 | |||
25 | export { | ||
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 5d3512148..f9ba10d5e 100644 --- a/shared/core-utils/common/index.ts +++ b/shared/core-utils/common/index.ts | |||
@@ -1,5 +1,11 @@ | |||
1 | export * from './array' | ||
2 | export * from './crypto' | ||
1 | export * from './date' | 3 | export * from './date' |
2 | export * from './miscs' | 4 | export * from './env' |
5 | export * from './object' | ||
6 | export * from './path' | ||
3 | export * from './regexp' | 7 | export * from './regexp' |
8 | export * from './time' | ||
4 | export * from './promises' | 9 | export * from './promises' |
5 | export * from './url' | 10 | export * from './url' |
11 | export * from './version' | ||
diff --git a/shared/core-utils/common/object.ts b/shared/core-utils/common/object.ts new file mode 100644 index 000000000..88d6b7514 --- /dev/null +++ b/shared/core-utils/common/object.ts | |||
@@ -0,0 +1,30 @@ | |||
1 | function 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 | |||
13 | function sortObjectComparator (key: string, order: 'asc' | 'desc') { | ||
14 | return (a: any, b: any) => { | ||
15 | if (a[key] < b[key]) { | ||
16 | return order === 'asc' ? -1 : 1 | ||
17 | } | ||
18 | |||
19 | if (a[key] > b[key]) { | ||
20 | return order === 'asc' ? 1 : -1 | ||
21 | } | ||
22 | |||
23 | return 0 | ||
24 | } | ||
25 | } | ||
26 | |||
27 | export { | ||
28 | pick, | ||
29 | sortObjectComparator | ||
30 | } | ||
diff --git a/shared/core-utils/path.ts b/shared/core-utils/common/path.ts index b1a45d69b..ec507538b 100644 --- a/shared/core-utils/path.ts +++ b/shared/core-utils/common/path.ts | |||
@@ -7,6 +7,7 @@ function root () { | |||
7 | 7 | ||
8 | rootPath = __dirname | 8 | rootPath = __dirname |
9 | 9 | ||
10 | if (basename(rootPath) === 'common') rootPath = resolve(rootPath, '..') | ||
10 | if (basename(rootPath) === 'core-utils') rootPath = resolve(rootPath, '..') | 11 | if (basename(rootPath) === 'core-utils') rootPath = resolve(rootPath, '..') |
11 | if (basename(rootPath) === 'shared') rootPath = resolve(rootPath, '..') | 12 | if (basename(rootPath) === 'shared') rootPath = resolve(rootPath, '..') |
12 | if (basename(rootPath) === 'server') rootPath = resolve(rootPath, '..') | 13 | if (basename(rootPath) === 'server') rootPath = resolve(rootPath, '..') |
@@ -27,8 +28,19 @@ function getLowercaseExtension (filename: string) { | |||
27 | return ext.toLowerCase() | 28 | return ext.toLowerCase() |
28 | } | 29 | } |
29 | 30 | ||
31 | function buildAbsoluteFixturePath (path: string, customCIPath = false) { | ||
32 | if (isAbsolute(path)) return path | ||
33 | |||
34 | if (customCIPath && process.env.GITHUB_WORKSPACE) { | ||
35 | return join(process.env.GITHUB_WORKSPACE, 'fixtures', path) | ||
36 | } | ||
37 | |||
38 | return join(root(), 'server', 'tests', 'fixtures', path) | ||
39 | } | ||
40 | |||
30 | export { | 41 | export { |
31 | root, | 42 | root, |
32 | buildPath, | 43 | buildPath, |
44 | buildAbsoluteFixturePath, | ||
33 | getLowercaseExtension | 45 | getLowercaseExtension |
34 | } | 46 | } |
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 @@ | |||
1 | function wait (milliseconds: number) { | ||
2 | return new Promise(resolve => setTimeout(resolve, milliseconds)) | ||
3 | } | ||
4 | |||
5 | export { | ||
6 | wait | ||
7 | } | ||
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 | ||
2 | function 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 |
7 | function compareSemVer (a: string, b: string) { | 2 | function 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 | ||
23 | function 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 | |||
37 | export { | 18 | export { |
38 | randomInt, | 19 | compareSemVer |
39 | compareSemVer, | ||
40 | sortObjectComparator | ||
41 | } | 20 | } |
diff --git a/shared/core-utils/index.ts b/shared/core-utils/index.ts index ee5cd4412..8daaa2d04 100644 --- a/shared/core-utils/index.ts +++ b/shared/core-utils/index.ts | |||
@@ -1,10 +1,7 @@ | |||
1 | export * from './abuse' | 1 | export * from './abuse' |
2 | export * from './common' | 2 | export * from './common' |
3 | export * from './i18n' | 3 | export * from './i18n' |
4 | export * from './path' | ||
5 | export * from './plugins' | 4 | export * from './plugins' |
6 | export * from './renderer' | 5 | export * from './renderer' |
7 | export * from './users' | 6 | export * from './users' |
8 | export * from './utils' | ||
9 | export * from './videos' | 7 | export * from './videos' |
10 | export * from './uuid' | ||
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 @@ | |||
1 | export * from './array' | ||
2 | export * 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 @@ | |||
1 | function 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 | |||
13 | export { | ||
14 | pick | ||
15 | } | ||
diff --git a/shared/core-utils/videos/index.ts b/shared/core-utils/videos/index.ts index 620e3a716..8f6736d39 100644 --- a/shared/core-utils/videos/index.ts +++ b/shared/core-utils/videos/index.ts | |||
@@ -1,2 +1,3 @@ | |||
1 | export * from './bitrate' | 1 | export * from './bitrate' |
2 | export * from './privacy' | 2 | export * from './privacy' |
3 | export * from './uuid' | ||
diff --git a/shared/core-utils/uuid.ts b/shared/core-utils/videos/uuid.ts index f3c80e046..f3c80e046 100644 --- a/shared/core-utils/uuid.ts +++ b/shared/core-utils/videos/uuid.ts | |||