Skip to content

Commit

Permalink
hash loader hardening: 404 handler, set sceneId only if success
Browse files Browse the repository at this point in the history
  • Loading branch information
kfarr committed Sep 12, 2023
1 parent 7c5b2b8 commit bf7b4b3
Showing 1 changed file with 38 additions and 31 deletions.
69 changes: 38 additions & 31 deletions src/json-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Takes one or more elements (from a DOM queryselector call)
and returns a Javascript object
*/
function convertDOMElToObject (entity) {
function convertDOMElToObject(entity) {
const data = [];
const environmentElement = document.querySelector('#environment');
const referenceEntities = document.querySelector('#reference-layers');
Expand All @@ -23,7 +23,7 @@ function convertDOMElToObject (entity) {
};
}

function getElementData (entity) {
function getElementData(entity) {
if (!entity.isEntity) {
return;
}
Expand All @@ -42,7 +42,7 @@ function getElementData (entity) {
return elementTree;
}

function getAttributes (entity) {
function getAttributes(entity) {
const elemObj = {};

elemObj['element'] = entity.tagName.toLowerCase();
Expand Down Expand Up @@ -83,7 +83,7 @@ function getAttributes (entity) {
return elemObj;
}

function toPropString (propData) {
function toPropString(propData) {
if (
typeof propData === 'string' ||
typeof propData === 'number' ||
Expand Down Expand Up @@ -117,11 +117,11 @@ function toPropString (propData) {
}
}

function isSingleProperty (schema) {
function isSingleProperty(schema) {
return AFRAME.schema.isSingleProperty(schema);
}

function isEmpty (object) {
function isEmpty(object) {
return Object.keys(object).length === 0;
}

Expand All @@ -142,15 +142,15 @@ const renameProps = {
intersection: 'not-intersection'
};

function filterJSONstreet (removeProps, renameProps, streetJSON) {
function removeValueCheck (removeVal, value) {
function filterJSONstreet(removeProps, renameProps, streetJSON) {
function removeValueCheck(removeVal, value) {
if (AFRAME.utils.deepEqual(removeVal, value) || removeVal === '*') {
return true;
}
return undefined;
}

let stringJSON = JSON.stringify(streetJSON, function replacer (key, value) {
let stringJSON = JSON.stringify(streetJSON, function replacer(key, value) {
for (var removeKey in removeProps) {
// check for removing components
if (key === removeKey) {
Expand Down Expand Up @@ -203,7 +203,7 @@ function filterJSONstreet (removeProps, renameProps, streetJSON) {
* @return The value of the component or components'
* property coming from mixins of the source.
*/
function getMixedValue (component, propertyName, source) {
function getMixedValue(component, propertyName, source) {
var value;
var reversedMixins = source.mixinEls.reverse();
for (var i = 0; value === undefined && i < reversedMixins.length; i++) {
Expand All @@ -220,7 +220,7 @@ function getMixedValue (component, propertyName, source) {
return [component.name, value];
}

function shallowEqual (object1, object2) {
function shallowEqual(object1, object2) {
if (
(typeof object1 === 'string' && typeof object2 === 'string') ||
(typeof object1 === 'number' && typeof object2 === 'number')
Expand All @@ -243,7 +243,7 @@ function shallowEqual (object1, object2) {
return true;
}

function getModifiedProperty (entity, componentName) {
function getModifiedProperty(entity, componentName) {
const data = AFRAME.utils.entity.getComponentProperty(entity, componentName);

// if it is element's attribute
Expand Down Expand Up @@ -305,7 +305,7 @@ function getModifiedProperty (entity, componentName) {
return diff;
}

function createEntities (entitiesData, parentEl) {
function createEntities(entitiesData, parentEl) {
const sceneElement = document.querySelector('a-scene');
const removeEntities = ['environment', 'reference-layers'];
for (const entityData of entitiesData) {
Expand Down Expand Up @@ -347,7 +347,7 @@ Add a new entity with a list of components and children (if exists)
* @param {Element} parentEl the parent element to which the Entity will be added
* @return {Element} Entity created
*/
function createEntityFromObj (entityData, parentEl) {
function createEntityFromObj(entityData, parentEl) {
const entity =
entityData.entityElement || document.createElement(entityData.element);

Expand Down Expand Up @@ -449,11 +449,6 @@ AFRAME.registerComponent('set-loader-from-hash', {
}
},
fetchJSON: function (requestURL) {
const sceneId = getUUIDFromPath(requestURL);
if (sceneId) {
console.log('sceneId from fetchJSON from url hash loader', sceneId);
AFRAME.scenes[0].setAttribute('metadata', 'sceneId', sceneId);
}
const request = new XMLHttpRequest();
request.open('GET', requestURL, true);
request.onload = function () {
Expand All @@ -469,24 +464,36 @@ AFRAME.registerComponent('set-loader-from-hash', {
'200 response received and JSON parsed, now createElementsFromJSON'
);
createElementsFromJSON(jsonData);
const sceneId = getUUIDFromPath(requestURL);
let sceneId = getUUIDFromPath(requestURL);
if (sceneId) {
console.log('sceneId from fetchJSON from url hash loader', sceneId);
AFRAME.scenes[0].setAttribute('metadata', 'sceneId', sceneId);
}
} else if (this.status === 404) {
console.error(
'[set-loader-from-hash] Error trying to load scene: Resource not found.'
);
AFRAME.scenes[0].components['notify'].message(
'Error trying to load scene: Resource not found.',
'error'
);
}
};
request.onerror = function () {
// There was a connection error of some sort
console.log(
console.error(
'Loading Error: There was a connection error during JSON loading'
);
AFRAME.scenes[0].components['notify'].message(
'Could not fetch scene.',
'error'
);
};
request.send();
}
});

function getUUIDFromPath (path) {
function getUUIDFromPath(path) {
// UUID regex pattern: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}
const uuidPattern =
/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/;
Expand All @@ -500,7 +507,7 @@ function getUUIDFromPath (path) {
}

// this use os text input prompt, delete current scene, then load streetmix file
function inputStreetmix () {
function inputStreetmix() {
streetmixURL = prompt(
'Please enter a Streetmix URL',
'https://streetmix.net/kfarr/3/example-street'
Expand All @@ -521,7 +528,7 @@ function inputStreetmix () {
}

// JSON loading starts here
function getValidJSON (stringJSON) {
function getValidJSON(stringJSON) {
// Preserve newlines, etc. - use valid JSON
// Remove non-printable and other non-valid JSON characters
return stringJSON
Expand All @@ -530,16 +537,16 @@ function getValidJSON (stringJSON) {
.replace(/[\u0000-\u0019]+/g, '');
}

function createElementsFromJSON (streetJSON) {
function createElementsFromJSON(streetJSON) {
let streetObject = {};
if (typeof streetJSON === 'string') {
if (typeof streetJSON == 'string') {
const validJSONString = getValidJSON(streetJSON);
streetObject = JSON.parse(validJSONString);
} else if (typeof streetJSON === 'object') {
} else if (typeof streetJSON == 'object') {
streetObject = streetJSON;
}

const sceneTitle = streetObject.title;
let sceneTitle = streetObject.title;
if (sceneTitle) {
console.log('sceneTitle from createElementsFromJSON', sceneTitle);
AFRAME.scenes[0].setAttribute('metadata', 'sceneTitle', sceneTitle);
Expand All @@ -558,16 +565,16 @@ function createElementsFromJSON (streetJSON) {
}

// viewer widget click to paste json string of 3dstreet scene
function inputJSON () {
function inputJSON() {
const stringJSON = prompt('Please paste 3DStreet JSON string');
if (stringJSON) {
createElementsFromJSON(stringJSON);
}
}

// handle viewer widget click to open 3dstreet json scene
function fileJSON () {
const reader = new FileReader();
function fileJSON() {
let reader = new FileReader();
reader.onload = function () {
AFRAME.scenes[0].setAttribute('metadata', 'sceneId', '');
AFRAME.scenes[0].setAttribute('metadata', 'sceneTitle', '');
Expand Down

0 comments on commit bf7b4b3

Please sign in to comment.