Skip to content

Commit b967557

Browse files
committed
guard all window references
1 parent fb1ded3 commit b967557

File tree

4 files changed

+29
-30
lines changed

4 files changed

+29
-30
lines changed

src/BodyPix/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,13 @@ class BodyPix {
9090
bodyPartsSpec(colorOptions) {
9191
const result = colorOptions !== undefined || Object.keys(colorOptions).length >= 24 ? colorOptions : this.config.palette;
9292

93-
// Check if we're getting p5 colors, make sure they are rgb
94-
if (p5Utils.checkP5() && result !== undefined && Object.keys(result).length >= 24) {
93+
// Check if we're getting p5 colors, make sure they are rgb
94+
const p5 = p5Utils.p5Instance;
95+
if (p5 && result !== undefined && Object.keys(result).length >= 24) {
9596
// Ensure the p5Color object is an RGB array
9697
Object.keys(result).forEach(part => {
97-
if (result[part].color instanceof window.p5.Color) {
98+
if (result[part].color instanceof p5.Color) {
9899
result[part].color = this.p5Color2RGB(result[part].color);
99-
} else {
100-
result[part].color = result[part].color;
101100
}
102101
});
103102
}
@@ -452,4 +451,4 @@ const bodyPix = (videoOrOptionsOrCallback, optionsOrCallback, cb) => {
452451
return callback ? instance : instance.ready;
453452
}
454453

455-
export default bodyPix;
454+
export default bodyPix;

src/FaceApi/index.js

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
/* eslint prefer-destructuring: ["error", {AssignmentExpression: {array: false}}] */
77
/* eslint no-await-in-loop: "off" */
8+
/* eslint class-methods-use-this: "off" */
89

910
/*
1011
* FaceApi: real-time face recognition, and landmark detection
@@ -14,6 +15,7 @@
1415
import * as tf from "@tensorflow/tfjs";
1516
import * as faceapi from "face-api.js";
1617
import callCallback from "../utils/callcallback";
18+
import modelLoader from "../utils/modelLoader";
1719

1820
const DEFAULTS = {
1921
withLandmarks: true,
@@ -93,7 +95,7 @@ class FaceApiBase {
9395

9496
Object.keys(this.config.MODEL_URLS).forEach(item => {
9597
if (modelOptions.includes(item)) {
96-
this.config.MODEL_URLS[item] = this.getModelPath(this.config.MODEL_URLS[item]);
98+
this.config.MODEL_URLS[item] = modelLoader.getModelPath(this.config.MODEL_URLS[item]);
9799
}
98100
});
99101

@@ -354,18 +356,6 @@ class FaceApiBase {
354356
return _param !== undefined ? _param : _default;
355357
}
356358

357-
/**
358-
* Checks if the given string is an absolute or relative path and returns
359-
* the path to the modelJson
360-
* @param {String} absoluteOrRelativeUrl
361-
*/
362-
getModelPath(absoluteOrRelativeUrl) {
363-
const modelJsonPath = this.isAbsoluteURL(absoluteOrRelativeUrl)
364-
? absoluteOrRelativeUrl
365-
: window.location.pathname + absoluteOrRelativeUrl;
366-
return modelJsonPath;
367-
}
368-
369359
/**
370360
* Sets the return options for .detect() or .detectSingle() in case any are given
371361
* @param {Object} faceApiOptions
@@ -399,12 +389,6 @@ class FaceApiBase {
399389
});
400390
}
401391

402-
/* eslint class-methods-use-this: "off" */
403-
isAbsoluteURL(str) {
404-
const pattern = new RegExp("^(?:[a-z]+:)?//", "i");
405-
return !!pattern.test(str);
406-
}
407-
408392
/**
409393
* get parts from landmarks
410394
* @param {*} result

src/utils/modelLoader.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1+
/**
2+
* Check if the provided URL string starts with a hostname,
3+
* such as http://, https://, etc.
4+
* @param {string} str
5+
* @returns {boolean}
6+
*/
17
function isAbsoluteURL(str) {
28
const pattern = new RegExp('^(?:[a-z]+:)?//', 'i');
3-
return !!pattern.test(str);
9+
return pattern.test(str);
410
}
511

12+
/**
13+
* Accepts a URL that may be a complete URL, or a relative location.
14+
* Returns an absolute URL based on the current window location.
15+
* @param {string} absoluteOrRelativeUrl
16+
* @returns {string}
17+
*/
618
function getModelPath(absoluteOrRelativeUrl) {
7-
const modelJsonPath = isAbsoluteURL(absoluteOrRelativeUrl) ? absoluteOrRelativeUrl : window.location.pathname + absoluteOrRelativeUrl
8-
return modelJsonPath;
19+
if (!isAbsoluteURL(absoluteOrRelativeUrl) && typeof window !== 'undefined') {
20+
return window.location.pathname + absoluteOrRelativeUrl;
21+
}
22+
return absoluteOrRelativeUrl;
923
}
1024

1125
export default {
1226
isAbsoluteURL,
1327
getModelPath
14-
}
28+
}

src/utils/p5Utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
class P5Util {
77
constructor() {
8+
if (typeof window !== "undefined") {
89
this.m_p5Instance = window;
10+
}
911
}
1012

1113
/**
@@ -19,7 +21,7 @@ class P5Util {
1921
/**
2022
* This getter will return p5, checking first if it is in
2123
* the window and next if it is in the p5 property of this.m_p5Instance
22-
* @returns {boolean} if it is in p5
24+
* @returns {boolean} if it is in p5
2325
*/
2426
get p5Instance() {
2527
if (typeof this.m_p5Instance !== "undefined" &&

0 commit comments

Comments
 (0)