Skip to content

Commit

Permalink
Use hubs-proxy for fetching media in development
Browse files Browse the repository at this point in the history
  • Loading branch information
robertlong committed Apr 15, 2019
1 parent 2999d09 commit 23c43e9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .env.defaults
Expand Up @@ -13,4 +13,5 @@
HUBS_SERVER="dev.reticulum.io"
RETICULUM_SERVER="dev.reticulum.io"
FARSPARK_SERVER="farspark-dev.reticulum.io"
NON_CORS_PROXY_DOMAINS="hubs.local,localhost"
NON_CORS_PROXY_DOMAINS="hubs.local,localhost"
CORS_PROXY_SERVER="hubs-proxy.com"
23 changes: 23 additions & 0 deletions src/api/Api.js
Expand Up @@ -7,6 +7,7 @@ import PublishDialog from "./PublishDialog";
import ProgressDialog from "../ui/dialogs/ProgressDialog";
import Fuse from "fuse.js";
import jwtDecode from "jwt-decode";
import { buildAbsoluteURL } from "url-toolkit";

// Media related functions should be kept up to date with Hubs media-utils:
// https://github.com/mozilla/hubs/blob/master/src/utils/media-utils.js
Expand Down Expand Up @@ -278,6 +279,28 @@ export default class Project extends EventEmitter {
return { canonicalUrl, accessibleUrl, contentType };
}

unproxyUrl(baseUrl, url) {
if (process.env.CORS_PROXY_SERVER) {
const corsProxyPrefix = `https://${process.env.CORS_PROXY_SERVER}/`;

if (baseUrl.startsWith(corsProxyPrefix)) {
baseUrl = baseUrl.substring(corsProxyPrefix.length);
}

if (url.startsWith(corsProxyPrefix)) {
url = url.substring(corsProxyPrefix.length);
}
}

// HACK HLS.js resolves relative urls internally, but our CORS proxying screws it up. Resolve relative to the original unproxied url.
// TODO extend HLS.js to allow overriding of its internal resolving instead
if (!url.startsWith("http")) {
url = buildAbsoluteURL(baseUrl, url.startsWith("/") ? url : `/${url}`);
}

return proxiedUrlFor(url);
}

async searchMedia(source, params, cursor, signal) {
const url = new URL(`https://${RETICULUM_SERVER}/api/v1/media/search`);

Expand Down
17 changes: 2 additions & 15 deletions src/editor/nodes/VideoNode.js
@@ -1,6 +1,5 @@
import EditorNodeMixin from "./EditorNodeMixin";
import Video from "../objects/Video";
import { buildAbsoluteURL } from "url-toolkit";
import Hls from "hls.js/dist/hls.light";
import isHLS from "../utils/isHLS";

Expand Down Expand Up @@ -90,21 +89,9 @@ export default class VideoNode extends EditorNodeMixin(Video) {
const isHls = isHLS(src, contentType);

if (isHls) {
const corsProxyPrefix = `http://localhost:9090/api/cors-proxy/`;
const baseUrl = src.startsWith(corsProxyPrefix) ? src.substring(corsProxyPrefix.length) : src;
this.hls = new Hls({
xhrSetup: (xhr, u) => {
if (u.startsWith(corsProxyPrefix)) {
u = u.substring(corsProxyPrefix.length);
}

// HACK HLS.js resolves relative urls internally, but our CORS proxying screws it up. Resolve relative to the original unproxied url.
// TODO extend HLS.js to allow overriding of its internal resolving instead
if (!u.startsWith("http")) {
u = buildAbsoluteURL(baseUrl, u.startsWith("/") ? u : `/${u}`);
}

xhr.open("GET", new URL(`/api/cors-proxy/${u}`, window.location).href);
xhrSetup: (xhr, url) => {
xhr.open("GET", this.editor.api.unproxyUrl(src, url));
}
});
}
Expand Down

0 comments on commit 23c43e9

Please sign in to comment.