Skip to content

Commit da6a2b6

Browse files
authored
Improve browser vs. webworker detection (#1067)
1 parent 0dc1d8b commit da6a2b6

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

src/env.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ import url from 'url';
2929
const VERSION = '3.1.0';
3030

3131
// Check if various APIs are available (depends on environment)
32-
const IS_BROWSER_ENV = typeof self !== 'undefined';
33-
const IS_WEBWORKER_ENV = IS_BROWSER_ENV && self.constructor.name === 'DedicatedWorkerGlobalScope';
34-
const IS_WEB_CACHE_AVAILABLE = IS_BROWSER_ENV && 'caches' in self;
32+
const IS_BROWSER_ENV = typeof window !== "undefined" && typeof window.document !== "undefined";
33+
const IS_WEBWORKER_ENV = typeof self !== "undefined" && self.constructor?.name === 'DedicatedWorkerGlobalScope';
34+
const IS_WEB_CACHE_AVAILABLE = typeof self !== "undefined" && 'caches' in self;
3535
const IS_WEBGPU_AVAILABLE = typeof navigator !== 'undefined' && 'gpu' in navigator;
3636
const IS_WEBNN_AVAILABLE = typeof navigator !== 'undefined' && 'ml' in navigator;
3737

@@ -44,7 +44,7 @@ const IS_PATH_AVAILABLE = !isEmpty(path);
4444
* A read-only object containing information about the APIs available in the current environment.
4545
*/
4646
export const apis = Object.freeze({
47-
/** Whether we are running in a browser environment */
47+
/** Whether we are running in a browser environment (and not a web worker) */
4848
IS_BROWSER_ENV,
4949

5050
/** Whether we are running in a web worker environment */
@@ -137,7 +137,7 @@ export const env = {
137137
remoteHost: 'https://huggingface.co/',
138138
remotePathTemplate: '{model}/resolve/{revision}/',
139139

140-
allowLocalModels: !IS_BROWSER_ENV,
140+
allowLocalModels: !(IS_BROWSER_ENV || IS_WEBWORKER_ENV),
141141
localModelPath: localModelPath,
142142
useFS: IS_FS_AVAILABLE,
143143

src/utils/image.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,17 @@
1010

1111
import { isNullishDimension } from './core.js';
1212
import { getFile } from './hub.js';
13-
import { env } from '../env.js';
13+
import { env, apis } from '../env.js';
1414
import { Tensor } from './tensor.js';
1515

1616
// Will be empty (or not used) if running in browser or web-worker
1717
import sharp from 'sharp';
1818

19-
const BROWSER_ENV = typeof self !== 'undefined';
20-
const WEBWORKER_ENV = BROWSER_ENV && self.constructor.name === 'DedicatedWorkerGlobalScope';
21-
2219
let createCanvasFunction;
2320
let ImageDataClass;
2421
let loadImageFunction;
25-
if (BROWSER_ENV) {
22+
const IS_BROWSER_OR_WEBWORKER = apis.IS_BROWSER_ENV || apis.IS_WEBWORKER_ENV;
23+
if (IS_BROWSER_OR_WEBWORKER) {
2624
// Running in browser or web-worker
2725
createCanvasFunction = (/** @type {number} */ width, /** @type {number} */ height) => {
2826
if (!self.OffscreenCanvas) {
@@ -132,7 +130,7 @@ export class RawImage {
132130
* @returns {RawImage} The image object.
133131
*/
134132
static fromCanvas(canvas) {
135-
if (!BROWSER_ENV) {
133+
if (!IS_BROWSER_OR_WEBWORKER) {
136134
throw new Error('fromCanvas() is only supported in browser environments.')
137135
}
138136

@@ -161,7 +159,7 @@ export class RawImage {
161159
* @returns {Promise<RawImage>} The image object.
162160
*/
163161
static async fromBlob(blob) {
164-
if (BROWSER_ENV) {
162+
if (IS_BROWSER_OR_WEBWORKER) {
165163
// Running in environment with canvas
166164
const img = await loadImageFunction(blob);
167165

@@ -339,7 +337,7 @@ export class RawImage {
339337
height = (width / this.width) * this.height;
340338
}
341339

342-
if (BROWSER_ENV) {
340+
if (IS_BROWSER_OR_WEBWORKER) {
343341
// TODO use `resample` in browser environment
344342

345343
// Store number of channels before resizing
@@ -412,7 +410,7 @@ export class RawImage {
412410
return this;
413411
}
414412

415-
if (BROWSER_ENV) {
413+
if (IS_BROWSER_OR_WEBWORKER) {
416414
// Store number of channels before padding
417415
const numChannels = this.channels;
418416

@@ -461,7 +459,7 @@ export class RawImage {
461459
const crop_width = x_max - x_min + 1;
462460
const crop_height = y_max - y_min + 1;
463461

464-
if (BROWSER_ENV) {
462+
if (IS_BROWSER_OR_WEBWORKER) {
465463
// Store number of channels before resizing
466464
const numChannels = this.channels;
467465

@@ -509,7 +507,7 @@ export class RawImage {
509507
const height_offset = (this.height - crop_height) / 2;
510508

511509

512-
if (BROWSER_ENV) {
510+
if (IS_BROWSER_OR_WEBWORKER) {
513511
// Store number of channels before resizing
514512
const numChannels = this.channels;
515513

@@ -614,7 +612,7 @@ export class RawImage {
614612
}
615613

616614
async toBlob(type = 'image/png', quality = 1) {
617-
if (!BROWSER_ENV) {
615+
if (!IS_BROWSER_OR_WEBWORKER) {
618616
throw new Error('toBlob() is only supported in browser environments.')
619617
}
620618

@@ -640,7 +638,7 @@ export class RawImage {
640638
}
641639

642640
toCanvas() {
643-
if (!BROWSER_ENV) {
641+
if (!IS_BROWSER_OR_WEBWORKER) {
644642
throw new Error('toCanvas() is only supported in browser environments.')
645643
}
646644

@@ -744,8 +742,8 @@ export class RawImage {
744742
*/
745743
async save(path) {
746744

747-
if (BROWSER_ENV) {
748-
if (WEBWORKER_ENV) {
745+
if (IS_BROWSER_OR_WEBWORKER) {
746+
if (apis.IS_WEBWORKER_ENV) {
749747
throw new Error('Unable to save an image from a Web Worker.')
750748
}
751749

@@ -781,7 +779,7 @@ export class RawImage {
781779
}
782780

783781
toSharp() {
784-
if (BROWSER_ENV) {
782+
if (IS_BROWSER_OR_WEBWORKER) {
785783
throw new Error('toSharp() is only supported in server-side environments.')
786784
}
787785

0 commit comments

Comments
 (0)