2024-08-06 16:48:29 +02:00
|
|
|
AFRAME.registerComponent('selfcontainer', {
|
|
|
|
schema: {
|
|
|
|
foo: { type:"string"}
|
|
|
|
},
|
|
|
|
|
|
|
|
init: async function () {
|
|
|
|
this.installProxyServer()
|
|
|
|
},
|
|
|
|
|
|
|
|
events:{ },
|
|
|
|
|
|
|
|
convert:{
|
|
|
|
|
|
|
|
arrayBufferToBase64: function(buffer){
|
|
|
|
let binary = '';
|
|
|
|
const bytes = new Uint8Array(buffer);
|
|
|
|
const len = bytes.byteLength;
|
|
|
|
for (let i = 0; i < len; i++) binary += String.fromCharCode(bytes[i]);
|
|
|
|
return window.btoa(binary);
|
|
|
|
},
|
|
|
|
|
|
|
|
base64ToArrayBuffer: function(base64) {
|
|
|
|
const binaryString = window.atob(base64);
|
|
|
|
const len = binaryString.length;
|
|
|
|
const bytes = new Uint8Array(len);
|
|
|
|
|
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
|
bytes[i] = binaryString.charCodeAt(i);
|
|
|
|
}
|
|
|
|
return bytes.buffer;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
installProxyServer: function(){
|
|
|
|
if( !window.store ) window.store = {}
|
2024-11-19 17:00:49 +01:00
|
|
|
|
2024-08-06 16:48:29 +02:00
|
|
|
// selfcontain every webrequest to store (and serve if stored)
|
|
|
|
let curry = function(me){
|
|
|
|
return function(request, response, cb){
|
2024-11-18 21:18:33 +01:00
|
|
|
|
2024-08-06 16:48:29 +02:00
|
|
|
let data = request ? window.store[ request.url ] || false : false
|
|
|
|
if( data ){ // return inline version
|
2024-11-18 21:18:33 +01:00
|
|
|
console.log('selfcontainer.js: serving '+request.url+' from cache')
|
2024-08-06 16:48:29 +02:00
|
|
|
let res = new Response()
|
|
|
|
res[ data.binary ? 'data' : 'text' ] = data.binary ? () => me.convert.base64ToArrayBuffer(data.text) : data.text
|
|
|
|
cb(res)
|
|
|
|
}else{
|
2024-11-18 21:18:33 +01:00
|
|
|
|
|
|
|
if( request.url.match(/(^file:\/\/xrsh)/) ) return cb(response)
|
|
|
|
|
|
|
|
console.log("selfcontainer.js: caching "+request.url)
|
2024-08-06 16:48:29 +02:00
|
|
|
if( response.text ){
|
|
|
|
data = {text: response.text}
|
|
|
|
}else{
|
|
|
|
data = {binary: true, text: me.convert.arrayBufferToBase64(response.data)}
|
|
|
|
}
|
|
|
|
window.store[ request.url ] = data
|
2024-08-13 18:08:03 +02:00
|
|
|
let $store = document.querySelector('template#store')
|
|
|
|
if( $store ) $store.remove()
|
2024-08-06 16:48:29 +02:00
|
|
|
document.head.innerHTML += `\n<`+`template id="store">\nwindow.store = ${JSON.stringify(window.store,null,2)}\n`+`<`+`/template>`
|
|
|
|
cb(response);
|
|
|
|
}
|
2024-08-13 18:08:03 +02:00
|
|
|
}
|
2024-08-06 16:48:29 +02:00
|
|
|
}
|
2024-08-13 18:08:03 +02:00
|
|
|
xhook.after( curry(this) )
|
2024-08-06 16:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|