Node: Replace global with globalThis

This commit is contained in:
Cohee
2024-10-18 09:13:25 +00:00
parent 6a5828f2c7
commit a32dd436d7
9 changed files with 17 additions and 14 deletions

View File

@ -19,6 +19,9 @@ module.exports = {
parserOptions: { parserOptions: {
sourceType: 'module', sourceType: 'module',
}, },
globals: {
globalThis: 'readonly',
},
}, },
{ {
files: ['*.cjs'], files: ['*.cjs'],

View File

@ -1,6 +1,6 @@
import express from 'express'; import express from 'express';
import { jsonParser } from '../express-common.js'; import { jsonParser } from '../express-common.js';
import { getPipeline, getRawImage } from '../transformers.mjs'; import { getPipeline, getRawImage } from '../transformers.js';
const TASK = 'image-to-text'; const TASK = 'image-to-text';

View File

@ -1,6 +1,6 @@
import express from 'express'; import express from 'express';
import { getPipeline } from '../transformers.mjs'; import { getPipeline } from '../transformers.js';
import { jsonParser } from '../express-common.js'; import { jsonParser } from '../express-common.js';
const TASK = 'text-classification'; const TASK = 'text-classification';

View File

@ -2,7 +2,7 @@ import { Buffer } from 'node:buffer';
import express from 'express'; import express from 'express';
import wavefile from 'wavefile'; import wavefile from 'wavefile';
import { jsonParser } from '../express-common.js'; import { jsonParser } from '../express-common.js';
import { getPipeline } from '../transformers.mjs'; import { getPipeline } from '../transformers.js';
export const router = express.Router(); export const router = express.Router();

View File

@ -82,7 +82,7 @@ async function getPathToTokenizer(model, fallbackModel) {
throw new Error('Failed to extract the file name from the URL'); 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)) { if (!fs.existsSync(CACHE_PATH)) {
fs.mkdirSync(CACHE_PATH, { recursive: true }); fs.mkdirSync(CACHE_PATH, { recursive: true });
} }

View File

@ -85,7 +85,7 @@ function getModelForTask(task) {
async function migrateCacheToDataDir() { async function migrateCacheToDataDir() {
const oldCacheDir = path.join(process.cwd(), 'cache'); 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)) { if (!fs.existsSync(newCacheDir)) {
fs.mkdirSync(newCacheDir, { recursive: true }); fs.mkdirSync(newCacheDir, { recursive: true });
@ -130,7 +130,7 @@ export async function getPipeline(task, forceModel = '') {
await tasks[task].pipeline.dispose(); 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 model = forceModel || getModelForTask(task);
const localOnly = getConfigValue('extras.disableAutoDownload', false); const localOnly = getConfigValue('extras.disableAutoDownload', false);
console.log('Initializing transformers.js pipeline for task', task, 'with model', model); console.log('Initializing transformers.js pipeline for task', task, 'with model', model);

View File

@ -141,7 +141,7 @@ export async function migrateUserData() {
console.log(); console.log();
console.log(color.magenta('Preparing to migrate user data...')); 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('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(`Backups will be placed in the ${PUBLIC_DIRECTORIES.backups} directory.`);
console.log(`The process will start in ${TIMEOUT} seconds. Press Ctrl+C to cancel.`); console.log(`The process will start in ${TIMEOUT} seconds. Press Ctrl+C to cancel.`);
@ -412,11 +412,11 @@ export function toAvatarKey(handle) {
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
export async function initUserStorage(dataRoot) { export async function initUserStorage(dataRoot) {
global.DATA_ROOT = dataRoot; globalThis.DATA_ROOT = dataRoot;
console.log('Using data root:', color.green(global.DATA_ROOT)); console.log('Using data root:', color.green(globalThis.DATA_ROOT));
console.log(); console.log();
await storage.init({ await storage.init({
dir: path.join(global.DATA_ROOT, '_storage'), dir: path.join(globalThis.DATA_ROOT, '_storage'),
ttl: false, // Never expire ttl: false, // Never expire
}); });
@ -517,7 +517,7 @@ export function getUserDirectories(handle) {
const directories = structuredClone(USER_DIRECTORY_TEMPLATE); const directories = structuredClone(USER_DIRECTORY_TEMPLATE);
for (const key in directories) { 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); DIRECTORIES_CACHE.set(handle, directories);
return directories; return directories;

View File

@ -305,8 +305,8 @@ export const color = {
* @returns {string} A UUIDv4 string * @returns {string} A UUIDv4 string
*/ */
export function uuidv4() { export function uuidv4() {
if ('crypto' in global && 'randomUUID' in global.crypto) { if ('crypto' in globalThis && 'randomUUID' in globalThis.crypto) {
return global.crypto.randomUUID(); return globalThis.crypto.randomUUID();
} }
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = Math.random() * 16 | 0; const r = Math.random() * 16 | 0;

View File

@ -1,4 +1,4 @@
import { getPipeline } from '../transformers.mjs'; import { getPipeline } from '../transformers.js';
const TASK = 'feature-extraction'; const TASK = 'feature-extraction';
/** /**