Node: Replace global with globalThis

a32dd436d744819aee51f19dc6f8f269962d79a6

Cohee <18619528+Cohee1207@users.noreply.github.com>

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