diff --git a/src/env.js b/src/env.js index 3fd7cdbdd..287c98dfc 100644 --- a/src/env.js +++ b/src/env.js @@ -29,9 +29,9 @@ import url from 'url'; const VERSION = '3.1.0'; // Check if various APIs are available (depends on environment) -const IS_BROWSER_ENV = typeof self !== 'undefined'; -const IS_WEBWORKER_ENV = IS_BROWSER_ENV && self.constructor.name === 'DedicatedWorkerGlobalScope'; -const IS_WEB_CACHE_AVAILABLE = IS_BROWSER_ENV && 'caches' in self; +const IS_BROWSER_ENV = typeof window !== "undefined" && typeof window.document !== "undefined"; +const IS_WEBWORKER_ENV = typeof self !== "undefined" && self.constructor?.name === 'DedicatedWorkerGlobalScope'; +const IS_WEB_CACHE_AVAILABLE = typeof self !== "undefined" && 'caches' in self; const IS_WEBGPU_AVAILABLE = typeof navigator !== 'undefined' && 'gpu' in navigator; const IS_WEBNN_AVAILABLE = typeof navigator !== 'undefined' && 'ml' in navigator; @@ -44,7 +44,7 @@ const IS_PATH_AVAILABLE = !isEmpty(path); * A read-only object containing information about the APIs available in the current environment. */ export const apis = Object.freeze({ - /** Whether we are running in a browser environment */ + /** Whether we are running in a browser environment (and not a web worker) */ IS_BROWSER_ENV, /** Whether we are running in a web worker environment */ @@ -137,7 +137,7 @@ export const env = { remoteHost: 'https://huggingface.co/', remotePathTemplate: '{model}/resolve/{revision}/', - allowLocalModels: !IS_BROWSER_ENV, + allowLocalModels: !(IS_BROWSER_ENV || IS_WEBWORKER_ENV), localModelPath: localModelPath, useFS: IS_FS_AVAILABLE, diff --git a/src/utils/image.js b/src/utils/image.js index 37c812561..d372cd0ac 100644 --- a/src/utils/image.js +++ b/src/utils/image.js @@ -10,19 +10,17 @@ import { isNullishDimension } from './core.js'; import { getFile } from './hub.js'; -import { env } from '../env.js'; +import { env, apis } from '../env.js'; import { Tensor } from './tensor.js'; // Will be empty (or not used) if running in browser or web-worker import sharp from 'sharp'; -const BROWSER_ENV = typeof self !== 'undefined'; -const WEBWORKER_ENV = BROWSER_ENV && self.constructor.name === 'DedicatedWorkerGlobalScope'; - let createCanvasFunction; let ImageDataClass; let loadImageFunction; -if (BROWSER_ENV) { +const IS_BROWSER_OR_WEBWORKER = apis.IS_BROWSER_ENV || apis.IS_WEBWORKER_ENV; +if (IS_BROWSER_OR_WEBWORKER) { // Running in browser or web-worker createCanvasFunction = (/** @type {number} */ width, /** @type {number} */ height) => { if (!self.OffscreenCanvas) { @@ -132,7 +130,7 @@ export class RawImage { * @returns {RawImage} The image object. */ static fromCanvas(canvas) { - if (!BROWSER_ENV) { + if (!IS_BROWSER_OR_WEBWORKER) { throw new Error('fromCanvas() is only supported in browser environments.') } @@ -161,7 +159,7 @@ export class RawImage { * @returns {Promise} The image object. */ static async fromBlob(blob) { - if (BROWSER_ENV) { + if (IS_BROWSER_OR_WEBWORKER) { // Running in environment with canvas const img = await loadImageFunction(blob); @@ -339,7 +337,7 @@ export class RawImage { height = (width / this.width) * this.height; } - if (BROWSER_ENV) { + if (IS_BROWSER_OR_WEBWORKER) { // TODO use `resample` in browser environment // Store number of channels before resizing @@ -412,7 +410,7 @@ export class RawImage { return this; } - if (BROWSER_ENV) { + if (IS_BROWSER_OR_WEBWORKER) { // Store number of channels before padding const numChannels = this.channels; @@ -461,7 +459,7 @@ export class RawImage { const crop_width = x_max - x_min + 1; const crop_height = y_max - y_min + 1; - if (BROWSER_ENV) { + if (IS_BROWSER_OR_WEBWORKER) { // Store number of channels before resizing const numChannels = this.channels; @@ -509,7 +507,7 @@ export class RawImage { const height_offset = (this.height - crop_height) / 2; - if (BROWSER_ENV) { + if (IS_BROWSER_OR_WEBWORKER) { // Store number of channels before resizing const numChannels = this.channels; @@ -614,7 +612,7 @@ export class RawImage { } async toBlob(type = 'image/png', quality = 1) { - if (!BROWSER_ENV) { + if (!IS_BROWSER_OR_WEBWORKER) { throw new Error('toBlob() is only supported in browser environments.') } @@ -640,7 +638,7 @@ export class RawImage { } toCanvas() { - if (!BROWSER_ENV) { + if (!IS_BROWSER_OR_WEBWORKER) { throw new Error('toCanvas() is only supported in browser environments.') } @@ -744,8 +742,8 @@ export class RawImage { */ async save(path) { - if (BROWSER_ENV) { - if (WEBWORKER_ENV) { + if (IS_BROWSER_OR_WEBWORKER) { + if (apis.IS_WEBWORKER_ENV) { throw new Error('Unable to save an image from a Web Worker.') } @@ -781,7 +779,7 @@ export class RawImage { } toSharp() { - if (BROWSER_ENV) { + if (IS_BROWSER_OR_WEBWORKER) { throw new Error('toSharp() is only supported in server-side environments.') }