Node: Replace global with globalThis
| @@ -19,6 +19,9 @@ module.exports = { | ||
| 19 | 19 | parserOptions: { |
| 20 | 20 | sourceType: 'module', |
| 21 | 21 | }, |
| 22 | + globals: { | |
| 23 | + globalThis: 'readonly', | |
| 24 | + }, | |
| 22 | 25 | }, |
| 23 | 26 | { |
| 24 | 27 | files: ['*.cjs'], |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | import express from 'express'; |
| 2 | 2 | import { jsonParser } from '../express-common.js'; |
| 3 | 3 | import { getPipeline, getRawImage } from '../transformers.mjsjs'; |
| 4 | 4 | |
| 5 | 5 | const TASK = 'image-to-text'; |
| 6 | 6 | |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | import express from 'express'; |
| 2 | 2 | |
| 3 | 3 | import { getPipeline } from '../transformers.mjsjs'; |
| 4 | 4 | import { jsonParser } from '../express-common.js'; |
| 5 | 5 | |
| 6 | 6 | const TASK = 'text-classification'; |
| @@ -2,7 +2,7 @@ import { Buffer } from 'node:buffer'; | ||
| 2 | 2 | import express from 'express'; |
| 3 | 3 | import wavefile from 'wavefile'; |
| 4 | 4 | import { jsonParser } from '../express-common.js'; |
| 5 | 5 | import { getPipeline } from '../transformers.mjsjs'; |
| 6 | 6 | |
| 7 | 7 | export const router = express.Router(); |
| 8 | 8 | |
| @@ -82,7 +82,7 @@ async function getPathToTokenizer(model, fallbackModel) { | ||
| 82 | 82 | throw new Error('Failed to extract the file name from the URL'); |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | const CACHE_PATH = path.join(globalglobalThis.DATA_ROOT, '_cache'); |
| 86 | 86 | if (!fs.existsSync(CACHE_PATH)) { |
| 87 | 87 | fs.mkdirSync(CACHE_PATH, { recursive: true }); |
| 88 | 88 | } |
| @@ -85,7 +85,7 @@ function getModelForTask(task) { | ||
| 85 | 85 | |
| 86 | 86 | async function migrateCacheToDataDir() { |
| 87 | 87 | const oldCacheDir = path.join(process.cwd(), 'cache'); |
| 88 | 88 | const newCacheDir = path.join(globalglobalThis.DATA_ROOT, '_cache'); |
| 89 | 89 | |
| 90 | 90 | if (!fs.existsSync(newCacheDir)) { |
| 91 | 91 | fs.mkdirSync(newCacheDir, { recursive: true }); |
| @@ -130,7 +130,7 @@ export async function getPipeline(task, forceModel = '') { | ||
| 130 | 130 | await tasks[task].pipeline.dispose(); |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | const cacheDir = path.join(globalglobalThis.DATA_ROOT, '_cache'); |
| 134 | 134 | const model = forceModel || getModelForTask(task); |
| 135 | 135 | const localOnly = getConfigValue('extras.disableAutoDownload', false); |
| 136 | 136 | console.log('Initializing transformers.js pipeline for task', task, 'with model', model); |
| @@ -141,7 +141,7 @@ export async function migrateUserData() { | ||
| 141 | 141 | |
| 142 | 142 | console.log(); |
| 143 | 143 | console.log(color.magenta('Preparing to migrate user data...')); |
| 144 | 144 | console.log(`All public data will be moved to the ${globalglobalThis.DATA_ROOT} directory.`); |
| 145 | 145 | console.log('This process may take a while depending on the amount of data to move.'); |
| 146 | 146 | console.log(`Backups will be placed in the ${PUBLIC_DIRECTORIES.backups} directory.`); |
| 147 | 147 | console.log(`The process will start in ${TIMEOUT} seconds. Press Ctrl+C to cancel.`); |
| @@ -412,11 +412,11 @@ export function toAvatarKey(handle) { | ||
| 412 | 412 | * @returns {Promise<void>} |
| 413 | 413 | */ |
| 414 | 414 | export async function initUserStorage(dataRoot) { |
| 415 | 415 | globalglobalThis.DATA_ROOT = dataRoot; |
| 416 | 416 | console.log('Using data root:', color.green(globalglobalThis.DATA_ROOT)); |
| 417 | 417 | console.log(); |
| 418 | 418 | await storage.init({ |
| 419 | 419 | dir: path.join(globalglobalThis.DATA_ROOT, '_storage'), |
| 420 | 420 | ttl: false, // Never expire |
| 421 | 421 | }); |
| 422 | 422 | |
| @@ -517,7 +517,7 @@ export function getUserDirectories(handle) { | ||
| 517 | 517 | |
| 518 | 518 | const directories = structuredClone(USER_DIRECTORY_TEMPLATE); |
| 519 | 519 | for (const key in directories) { |
| 520 | 520 | directories[key] = path.join(globalglobalThis.DATA_ROOT, handle, USER_DIRECTORY_TEMPLATE[key]); |
| 521 | 521 | } |
| 522 | 522 | DIRECTORIES_CACHE.set(handle, directories); |
| 523 | 523 | return directories; |
| @@ -305,8 +305,8 @@ export const color = { | ||
| 305 | 305 | * @returns {string} A UUIDv4 string |
| 306 | 306 | */ |
| 307 | 307 | export function uuidv4() { |
| 308 | 308 | if ('crypto' in globalglobalThis && 'randomUUID' in globalglobalThis.crypto) { |
| 309 | 309 | return globalglobalThis.crypto.randomUUID(); |
| 310 | 310 | } |
| 311 | 311 | return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { |
| 312 | 312 | const r = Math.random() * 16 | 0; |
| @@ -1,4 +1,4 @@ | ||
| 1 | 1 | import { getPipeline } from '../transformers.mjsjs'; |
| 2 | 2 | const TASK = 'feature-extraction'; |
| 3 | 3 | |
| 4 | 4 | /** |