diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 4a23b91d1..c9d345bc6 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -19,6 +19,9 @@ module.exports = { parserOptions: { sourceType: 'module', }, + globals: { + globalThis: 'readonly', + }, }, { files: ['*.cjs'], diff --git a/src/endpoints/caption.js b/src/endpoints/caption.js index 01d931c61..8fc312ed7 100644 --- a/src/endpoints/caption.js +++ b/src/endpoints/caption.js @@ -1,6 +1,6 @@ import express from 'express'; import { jsonParser } from '../express-common.js'; -import { getPipeline, getRawImage } from '../transformers.mjs'; +import { getPipeline, getRawImage } from '../transformers.js'; const TASK = 'image-to-text'; diff --git a/src/endpoints/classify.js b/src/endpoints/classify.js index 7e0fd2393..02c35a161 100644 --- a/src/endpoints/classify.js +++ b/src/endpoints/classify.js @@ -1,6 +1,6 @@ import express from 'express'; -import { getPipeline } from '../transformers.mjs'; +import { getPipeline } from '../transformers.js'; import { jsonParser } from '../express-common.js'; const TASK = 'text-classification'; diff --git a/src/endpoints/speech.js b/src/endpoints/speech.js index eee6a7679..1f6514a59 100644 --- a/src/endpoints/speech.js +++ b/src/endpoints/speech.js @@ -2,7 +2,7 @@ import { Buffer } from 'node:buffer'; import express from 'express'; import wavefile from 'wavefile'; import { jsonParser } from '../express-common.js'; -import { getPipeline } from '../transformers.mjs'; +import { getPipeline } from '../transformers.js'; export const router = express.Router(); diff --git a/src/endpoints/tokenizers.js b/src/endpoints/tokenizers.js index dd959a03f..02d5fcaa6 100644 --- a/src/endpoints/tokenizers.js +++ b/src/endpoints/tokenizers.js @@ -82,7 +82,7 @@ async function getPathToTokenizer(model, fallbackModel) { throw new Error('Failed to extract the file name from the URL'); } - const CACHE_PATH = path.join(global.DATA_ROOT, '_cache'); + const CACHE_PATH = path.join(globalThis.DATA_ROOT, '_cache'); if (!fs.existsSync(CACHE_PATH)) { fs.mkdirSync(CACHE_PATH, { recursive: true }); } diff --git a/src/transformers.mjs b/src/transformers.js similarity index 97% rename from src/transformers.mjs rename to src/transformers.js index a4981a191..3413101a6 100644 --- a/src/transformers.mjs +++ b/src/transformers.js @@ -85,7 +85,7 @@ function getModelForTask(task) { async function migrateCacheToDataDir() { const oldCacheDir = path.join(process.cwd(), 'cache'); - const newCacheDir = path.join(global.DATA_ROOT, '_cache'); + const newCacheDir = path.join(globalThis.DATA_ROOT, '_cache'); if (!fs.existsSync(newCacheDir)) { fs.mkdirSync(newCacheDir, { recursive: true }); @@ -130,7 +130,7 @@ export async function getPipeline(task, forceModel = '') { await tasks[task].pipeline.dispose(); } - const cacheDir = path.join(global.DATA_ROOT, '_cache'); + const cacheDir = path.join(globalThis.DATA_ROOT, '_cache'); const model = forceModel || getModelForTask(task); const localOnly = getConfigValue('extras.disableAutoDownload', false); console.log('Initializing transformers.js pipeline for task', task, 'with model', model); diff --git a/src/users.js b/src/users.js index cf221c811..96e21c5ab 100644 --- a/src/users.js +++ b/src/users.js @@ -141,7 +141,7 @@ export async function migrateUserData() { console.log(); console.log(color.magenta('Preparing to migrate user data...')); - console.log(`All public data will be moved to the ${global.DATA_ROOT} directory.`); + console.log(`All public data will be moved to the ${globalThis.DATA_ROOT} directory.`); console.log('This process may take a while depending on the amount of data to move.'); console.log(`Backups will be placed in the ${PUBLIC_DIRECTORIES.backups} directory.`); console.log(`The process will start in ${TIMEOUT} seconds. Press Ctrl+C to cancel.`); @@ -412,11 +412,11 @@ export function toAvatarKey(handle) { * @returns {Promise} */ export async function initUserStorage(dataRoot) { - global.DATA_ROOT = dataRoot; - console.log('Using data root:', color.green(global.DATA_ROOT)); + globalThis.DATA_ROOT = dataRoot; + console.log('Using data root:', color.green(globalThis.DATA_ROOT)); console.log(); await storage.init({ - dir: path.join(global.DATA_ROOT, '_storage'), + dir: path.join(globalThis.DATA_ROOT, '_storage'), ttl: false, // Never expire }); @@ -517,7 +517,7 @@ export function getUserDirectories(handle) { const directories = structuredClone(USER_DIRECTORY_TEMPLATE); for (const key in directories) { - directories[key] = path.join(global.DATA_ROOT, handle, USER_DIRECTORY_TEMPLATE[key]); + directories[key] = path.join(globalThis.DATA_ROOT, handle, USER_DIRECTORY_TEMPLATE[key]); } DIRECTORIES_CACHE.set(handle, directories); return directories; diff --git a/src/util.js b/src/util.js index b3a6cca75..7cebdc031 100644 --- a/src/util.js +++ b/src/util.js @@ -305,8 +305,8 @@ export const color = { * @returns {string} A UUIDv4 string */ export function uuidv4() { - if ('crypto' in global && 'randomUUID' in global.crypto) { - return global.crypto.randomUUID(); + if ('crypto' in globalThis && 'randomUUID' in globalThis.crypto) { + return globalThis.crypto.randomUUID(); } return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { const r = Math.random() * 16 | 0; diff --git a/src/vectors/embedding.js b/src/vectors/embedding.js index a3d499014..b28fcc967 100644 --- a/src/vectors/embedding.js +++ b/src/vectors/embedding.js @@ -1,4 +1,4 @@ -import { getPipeline } from '../transformers.mjs'; +import { getPipeline } from '../transformers.js'; const TASK = 'feature-extraction'; /**