From 06aad80165d09a8863ab8103149a8ff518b10641 Mon Sep 17 00:00:00 2001 From: lutangar Date: Tue, 2 Nov 2021 19:11:20 +0100 Subject: chore(refactor): remove shared folder dependencies to the server Many files from the `shared` folder were importing files from the `server` folder. When attempting to use Typescript project references to describe dependencies, it highlighted a circular dependency beetween `shared` <-> `server`. The Typescript project forbid such usages. Using project references greatly improve performance by rebuilding only the updated project and not all source files. > see https://www.typescriptlang.org/docs/handbook/project-references.html --- shared/core-utils/crypto.ts | 14 ++++++++++++++ shared/core-utils/index.ts | 2 ++ shared/core-utils/path.ts | 34 ++++++++++++++++++++++++++++++++++ shared/core-utils/uuid.ts | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 shared/core-utils/crypto.ts create mode 100644 shared/core-utils/path.ts create mode 100644 shared/core-utils/uuid.ts (limited to 'shared/core-utils') diff --git a/shared/core-utils/crypto.ts b/shared/core-utils/crypto.ts new file mode 100644 index 000000000..d6d1150d0 --- /dev/null +++ b/shared/core-utils/crypto.ts @@ -0,0 +1,14 @@ +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) +} + +export { + sha256, + sha1 +} diff --git a/shared/core-utils/index.ts b/shared/core-utils/index.ts index e0a6a8087..ee5cd4412 100644 --- a/shared/core-utils/index.ts +++ b/shared/core-utils/index.ts @@ -1,8 +1,10 @@ export * from './abuse' export * from './common' export * from './i18n' +export * from './path' export * from './plugins' export * from './renderer' export * from './users' export * from './utils' export * from './videos' +export * from './uuid' diff --git a/shared/core-utils/path.ts b/shared/core-utils/path.ts new file mode 100644 index 000000000..b1a45d69b --- /dev/null +++ b/shared/core-utils/path.ts @@ -0,0 +1,34 @@ +import { basename, extname, isAbsolute, join, resolve } from 'path' + +let rootPath: string + +function root () { + if (rootPath) return rootPath + + rootPath = __dirname + + if (basename(rootPath) === 'core-utils') rootPath = resolve(rootPath, '..') + if (basename(rootPath) === 'shared') rootPath = resolve(rootPath, '..') + if (basename(rootPath) === 'server') rootPath = resolve(rootPath, '..') + if (basename(rootPath) === 'dist') rootPath = resolve(rootPath, '..') + + return rootPath +} + +function buildPath (path: string) { + if (isAbsolute(path)) return path + + return join(root(), path) +} + +function getLowercaseExtension (filename: string) { + const ext = extname(filename) || '' + + return ext.toLowerCase() +} + +export { + root, + buildPath, + getLowercaseExtension +} diff --git a/shared/core-utils/uuid.ts b/shared/core-utils/uuid.ts new file mode 100644 index 000000000..f3c80e046 --- /dev/null +++ b/shared/core-utils/uuid.ts @@ -0,0 +1,32 @@ +import short, { uuid } from 'short-uuid' + +const translator = short() + +function buildUUID () { + return 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 +} -- cgit v1.2.3