Newer
Older
// fs abstraction for sys
function GoGetter() {
this.findHunks = (debug) => {
return new Promise((resolve, reject) => {
let htmlTreeDiver = (response) => {
// do we try to pick links out of this plaintext ? like a monkey ?
// I suppose we do
// header, for names
let hfront = response.indexOf('<h1>Index of /hunks') + 14
let hback = response.indexOf('</h1>')
let header = response.slice(hfront, hback)
let igot = unreturned.indexOf(header)
if (igot != -1) {
//console.log('return success')
unreturned.splice(igot, 1)
}
// git those paths (these aren't names, are they?)
let paths = response.split('<a href="/hunks/')
// rm first two, and last ()
paths = paths.slice(2) //
paths.forEach((path, index) => {
let end = path.indexOf('"')
//console.log('index', index, 'end', end)
paths[index] = path.slice(0, end)
})
paths.forEach((path, index) => {
if (path[path.length - 1] == '/') {
//console.log('folder', path)
let recurse = 'hunks/' + path
//console.log('getting', recurse)
unreturned.push(recurse)
jQuery.get(recurse, htmlTreeDiver)
} else if (path.includes('.js')) {
//console.log('likely hunk', path)
// secrets are rming .js
path = path.slice(0, path.indexOf('.'))
if (path === 'hunks' || path === 'manager' || path === 'template' || path.includes('hidden/')) {
// don't add these special hunks
} else {
returned.push(path)
}
}
})
if (unreturned.length === 0) {
// TODO cull hunks and hidden paths ...
// now, writing menu options for each path
// sort alphabetically
//console.log('GG returns this', JSON.parse(JSON.stringify(returned)))
resolve(returned)
}
}
let unreturned = new Array()
let returned = new Array()
unreturned.push('hunks/')
jQuery.get('hunks/', htmlTreeDiver)
})
}
// https://github.com/tc39/proposal-dynamic-import
this.importSource = (url) => {
// escape characters that are used to delimit the module URL.
// this way the following module works: 'data:text/javascript,console.log("hello")'
url = url.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
return new Promise((resolve, reject) => {
const script = document.createElement("script")
const tempGlobal = "__tempModuleLoadingVariable" + Math.random().toString(32).substring(2)
function cleanup() {
delete window[tempGlobal]
script.remove()
}
window[tempGlobal] = function(module) {
cleanup()
//console.log('ADDHUNK (2) import resolves', url)
resolve(module[Object.keys(module)[0]])
}
script.type = "module"
script.textContent = `import * as m from "${url}"; window.${tempGlobal}(m);`
script.onerror = () => {
reject(new Error("Failed to load module script with URL " + url))
cleanup()
}
document.documentElement.appendChild(script)
})
}
this.getJson = (path) => {
return new Promise((resolve, reject) => {
$.ajax({
url: path,
success: (data) => {
resolve(data)
},
error: (err) => {
reject(new Error("failure at GG ajax get for json object: program probably doesn't exist"))
// I'm keeping this here because GoGetter is localized to the environment, mgr is not
this.interpretername = 'cuttlefish 0.1'
export default GoGetter