Skip to content

Commit

Permalink
Improve browser vs. webworker detection
Browse files Browse the repository at this point in the history
  • Loading branch information
xenova committed Dec 2, 2024
1 parent 11db949 commit 31a6463
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
10 changes: 5 additions & 5 deletions src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 */
Expand Down Expand Up @@ -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,

Expand Down
30 changes: 14 additions & 16 deletions src/utils/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.')
}

Expand Down Expand Up @@ -161,7 +159,7 @@ export class RawImage {
* @returns {Promise<RawImage>} 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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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.')
}

Expand All @@ -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.')
}

Expand Down Expand Up @@ -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.')
}

Expand Down Expand Up @@ -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.')
}

Expand Down

0 comments on commit 31a6463

Please sign in to comment.