Node: Replace global with globalThis

a32dd436d744819aee51f19dc6f8f269962d79a6

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

9 files changed, +17 -14Showing whitespace changes
.eslintrc.cjs+3 -0
@@ -19,6 +19,9 @@ module.exports = {
1919 parserOptions: {
2020 sourceType: 'module',
2121 },
22+ globals: {
23+ globalThis: 'readonly',
24+ },
2225 },
2326 {
2427 files: ['*.cjs'],
src/endpoints/caption.js+1 -1
@@ -1,6 +1,6 @@
11import express from 'express';
22import { jsonParser } from '../express-common.js';
33import { getPipeline, getRawImage } from '../transformers.mjsjs';
44
55const TASK = 'image-to-text';
66
src/endpoints/classify.js+1 -1
@@ -1,6 +1,6 @@
11import express from 'express';
22
33import { getPipeline } from '../transformers.mjsjs';
44import { jsonParser } from '../express-common.js';
55
66const TASK = 'text-classification';
src/endpoints/speech.js+1 -1
@@ -2,7 +2,7 @@ import { Buffer } from 'node:buffer';
22import express from 'express';
33import wavefile from 'wavefile';
44import { jsonParser } from '../express-common.js';
55import { getPipeline } from '../transformers.mjsjs';
66
77export const router = express.Router();
88
src/endpoints/tokenizers.js+1 -1
@@ -82,7 +82,7 @@ async function getPathToTokenizer(model, fallbackModel) {
8282 throw new Error('Failed to extract the file name from the URL');
8383 }
8484
8585 const CACHE_PATH = path.join(globalglobalThis.DATA_ROOT, '_cache');
8686 if (!fs.existsSync(CACHE_PATH)) {
8787 fs.mkdirSync(CACHE_PATH, { recursive: true });
8888 }
src/transformers.mjs → src/transformers.js+2 -2
@@ -85,7 +85,7 @@ function getModelForTask(task) {
8585
8686async function migrateCacheToDataDir() {
8787 const oldCacheDir = path.join(process.cwd(), 'cache');
8888 const newCacheDir = path.join(globalglobalThis.DATA_ROOT, '_cache');
8989
9090 if (!fs.existsSync(newCacheDir)) {
9191 fs.mkdirSync(newCacheDir, { recursive: true });
@@ -130,7 +130,7 @@ export async function getPipeline(task, forceModel = '') {
130130 await tasks[task].pipeline.dispose();
131131 }
132132
133133 const cacheDir = path.join(globalglobalThis.DATA_ROOT, '_cache');
134134 const model = forceModel || getModelForTask(task);
135135 const localOnly = getConfigValue('extras.disableAutoDownload', false);
136136 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
142142 console.log();
143143 console.log(color.magenta('Preparing to migrate user data...'));
144144 console.log(`All public data will be moved to the ${globalglobalThis.DATA_ROOT} directory.`);
145145 console.log('This process may take a while depending on the amount of data to move.');
146146 console.log(`Backups will be placed in the ${PUBLIC_DIRECTORIES.backups} directory.`);
147147 console.log(`The process will start in ${TIMEOUT} seconds. Press Ctrl+C to cancel.`);
@@ -412,11 +412,11 @@ export function toAvatarKey(handle) {
412412 * @returns {Promise<void>}
413413 */
414414export async function initUserStorage(dataRoot) {
415415 globalglobalThis.DATA_ROOT = dataRoot;
416416 console.log('Using data root:', color.green(globalglobalThis.DATA_ROOT));
417417 console.log();
418418 await storage.init({
419419 dir: path.join(globalglobalThis.DATA_ROOT, '_storage'),
420420 ttl: false, // Never expire
421421 });
422422
@@ -517,7 +517,7 @@ export function getUserDirectories(handle) {
517517
518518 const directories = structuredClone(USER_DIRECTORY_TEMPLATE);
519519 for (const key in directories) {
520520 directories[key] = path.join(globalglobalThis.DATA_ROOT, handle, USER_DIRECTORY_TEMPLATE[key]);
521521 }
522522 DIRECTORIES_CACHE.set(handle, directories);
523523 return directories;
src/util.js+2 -2
@@ -305,8 +305,8 @@ export const color = {
305305 * @returns {string} A UUIDv4 string
306306 */
307307export function uuidv4() {
308308 if ('crypto' in globalglobalThis && 'randomUUID' in globalglobalThis.crypto) {
309309 return globalglobalThis.crypto.randomUUID();
310310 }
311311 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
312312 const r = Math.random() * 16 | 0;
src/vectors/embedding.js+1 -1
@@ -1,4 +1,4 @@
11import { getPipeline } from '../transformers.mjsjs';
22const TASK = 'feature-extraction';
33
44/**