mirror of
https://github.com/osnr/TabFS.git
synced 2024-05-21 23:16:39 +02:00
more refactoring: standardize fromStringMaker to use arrays
resources and screenshot.png seem to still work
This commit is contained in:
parent
dc71428fbd
commit
583611af58
|
@ -40,8 +40,7 @@ function pathComponent(path, i) {
|
||||||
return components[i >= 0 ? i : components.length + i];
|
return components[i >= 0 ? i : components.length + i];
|
||||||
}
|
}
|
||||||
function sanitize(s) { return s.replace(/[^A-Za-z0-9_\-\.]/gm, '_'); }
|
function sanitize(s) { return s.replace(/[^A-Za-z0-9_\-\.]/gm, '_'); }
|
||||||
function utf8(str, offset, size) {
|
function utf8(str) {
|
||||||
// converts to UTF8, then takes slice
|
|
||||||
var utf8 = [];
|
var utf8 = [];
|
||||||
for (var i=0; i < str.length; i++) {
|
for (var i=0; i < str.length; i++) {
|
||||||
var charcode = str.charCodeAt(i);
|
var charcode = str.charCodeAt(i);
|
||||||
|
@ -65,18 +64,7 @@ function utf8(str, offset, size) {
|
||||||
0x80 | (charcode & 0x3f));
|
0x80 | (charcode & 0x3f));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return String.fromCharCode(...utf8.slice(offset, offset + size))
|
return utf8;
|
||||||
}
|
|
||||||
function stringSize(str) {
|
|
||||||
// returns the byte length of an utf8 string
|
|
||||||
var s = str.length;
|
|
||||||
for (var i=str.length-1; i>=0; i--) {
|
|
||||||
var code = str.charCodeAt(i);
|
|
||||||
if (code > 0x7f && code <= 0x7ff) s++;
|
|
||||||
else if (code > 0x7ff && code <= 0xffff) s+=2;
|
|
||||||
if (code >= 0xDC00 && code <= 0xDFFF) i--; //trail surrogate
|
|
||||||
}
|
|
||||||
return s + 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const TabManager = {
|
const TabManager = {
|
||||||
|
@ -127,10 +115,6 @@ const BrowserState = { lastFocusedWindowId: null };
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
|
|
||||||
/* if I could specify a custom editor interface for all the routing
|
|
||||||
below ... I would highlight the route names in blocks of some color
|
|
||||||
that sticks out, and let you collapse them. then you could get a
|
|
||||||
view of what the whole filesystem looks like at a glance. */
|
|
||||||
const router = {};
|
const router = {};
|
||||||
|
|
||||||
const Cache = {
|
const Cache = {
|
||||||
|
@ -145,6 +129,24 @@ const Cache = {
|
||||||
getObjectForHandle(handle) { return this.store[handle]; },
|
getObjectForHandle(handle) { return this.store[handle]; },
|
||||||
removeObjectForHandle(handle) { delete this.store[handle]; }
|
removeObjectForHandle(handle) { delete this.store[handle]; }
|
||||||
};
|
};
|
||||||
|
function toArray(stringOrArray) {
|
||||||
|
if (typeof stringOrArray == 'string') { return utf8(stringOrArray); }
|
||||||
|
else { return stringOrArray; }
|
||||||
|
}
|
||||||
|
const fromStringMaker = stringMaker => ({
|
||||||
|
async getattr({path}) {
|
||||||
|
return {
|
||||||
|
st_mode: unix.S_IFREG | 0444,
|
||||||
|
st_nlink: 1,
|
||||||
|
st_size: toArray(await stringMaker(path)).length
|
||||||
|
};
|
||||||
|
},
|
||||||
|
async open({path}) { return { fh: Cache.storeObject(toArray(await stringMaker(path))) }; },
|
||||||
|
async read({path, fh, size, offset}) {
|
||||||
|
return { buf: String.fromCharCode(...Cache.getObjectForHandle(fh).slice(offset, offset + size)) }
|
||||||
|
},
|
||||||
|
async release({fh}) { Cache.removeObjectForHandle(fh); return {}; }
|
||||||
|
});
|
||||||
|
|
||||||
router["/tabs/by-id"] = {
|
router["/tabs/by-id"] = {
|
||||||
async readdir() {
|
async readdir() {
|
||||||
|
@ -153,20 +155,6 @@ router["/tabs/by-id"] = {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
(function() {
|
(function() {
|
||||||
const fromStringMaker = stringMaker => ({
|
|
||||||
async getattr({path}) {
|
|
||||||
return {
|
|
||||||
st_mode: unix.S_IFREG | 0444,
|
|
||||||
st_nlink: 1,
|
|
||||||
st_size: stringSize(stringMaker(path))
|
|
||||||
};
|
|
||||||
},
|
|
||||||
async open({path}) { return { fh: Cache.storeObject(await stringMaker(path)) }; },
|
|
||||||
async read({path, fh, size, offset}) {
|
|
||||||
return { buf: utf8(Cache.getObjectForHandle(fh), offset, size) }
|
|
||||||
},
|
|
||||||
async release({fh}) { Cache.removeObjectForHandle(fh); return {}; }
|
|
||||||
});
|
|
||||||
const withTab = handler => fromStringMaker(async path => {
|
const withTab = handler => fromStringMaker(async path => {
|
||||||
const tabId = parseInt(pathComponent(path, -2));
|
const tabId = parseInt(pathComponent(path, -2));
|
||||||
const tab = await browser.tabs.get(tabId);
|
const tab = await browser.tabs.get(tabId);
|
||||||
|
@ -193,81 +181,35 @@ router["/tabs/by-id/*/screenshot.png"] = {
|
||||||
const slice = Cache.getObjectForHandle(fh).slice(offset, offset + size);
|
const slice = Cache.getObjectForHandle(fh).slice(offset, offset + size);
|
||||||
return { buf: String.fromCharCode(...slice) };
|
return { buf: String.fromCharCode(...slice) };
|
||||||
},
|
},
|
||||||
async release({fh}) { Cache.removeObjectForHandle(fh); }
|
async release({fh}) { Cache.removeObjectForHandle(fh); return {}; }
|
||||||
};
|
};
|
||||||
router["/tabs/by-id/*/resources"] = {
|
router["/tabs/by-id/*/resources"] = {
|
||||||
async opendir({path}) {
|
|
||||||
const tabId = parseInt(pathComponent(path, -2));
|
|
||||||
await TabManager.debugTab(tabId);
|
|
||||||
return { fh: 0 };
|
|
||||||
},
|
|
||||||
async readdir({path}) {
|
async readdir({path}) {
|
||||||
const tabId = parseInt(pathComponent(path, -2));
|
const tabId = parseInt(pathComponent(path, -2));
|
||||||
|
await TabManager.debugTab(tabId);
|
||||||
const {frameTree} = await sendDebuggerCommand(tabId, "Page.getResourceTree", {});
|
const {frameTree} = await sendDebuggerCommand(tabId, "Page.getResourceTree", {});
|
||||||
return { entries: frameTree.resources.map(r => sanitize(String(r.url).slice(0, 200))) };
|
return { entries: frameTree.resources.map(r => sanitize(String(r.url).slice(0, 200))) };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
router["/tabs/by-id/*/resources/*"] = {
|
router["/tabs/by-id/*/resources/*"] = fromStringMaker(async path => {
|
||||||
async getattr({path}) {
|
const [tabId, suffix] = [parseInt(pathComponent(path, -3)), pathComponent(path, -1)];
|
||||||
// FIXME: cache the file
|
|
||||||
const tabId = parseInt(pathComponent(path, -3));
|
|
||||||
const suffix = pathComponent(path, -1);
|
|
||||||
|
|
||||||
await TabManager.debugTab(tabId); await TabManager.enableDomainForTab(tabId, "Page");
|
await TabManager.debugTab(tabId); await TabManager.enableDomainForTab(tabId, "Page");
|
||||||
|
|
||||||
const {frameTree} = await sendDebuggerCommand(tabId, "Page.getResourceTree", {});
|
const {frameTree} = await sendDebuggerCommand(tabId, "Page.getResourceTree", {});
|
||||||
for (let resource of frameTree.resources) {
|
for (let resource of frameTree.resources) {
|
||||||
const resourceSuffix = sanitize(String(resource.url).slice(0, 200));
|
const resourceSuffix = sanitize(String(resource.url).slice(0, 200));
|
||||||
if (resourceSuffix === suffix) {
|
if (resourceSuffix === suffix) {
|
||||||
let {base64Encoded, content} = await sendDebuggerCommand(tabId, "Page.getResourceContent", {
|
let {base64Encoded, content} = await sendDebuggerCommand(tabId, "Page.getResourceContent", {
|
||||||
frameId: frameTree.frame.id,
|
frameId: frameTree.frame.id,
|
||||||
url: resource.url
|
url: resource.url
|
||||||
});
|
});
|
||||||
if (base64Encoded) {
|
if (base64Encoded) { return Uint8Array.from(atob(content), c => c.charCodeAt(0)); }
|
||||||
content = atob(content);
|
return content;
|
||||||
}
|
|
||||||
return {
|
|
||||||
st_mode: unix.S_IFREG | 0444,
|
|
||||||
st_nlink: 1,
|
|
||||||
st_size: stringSize(content) // FIXME
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
async open({path}) {
|
|
||||||
// FIXME: cache the file.
|
|
||||||
const tabId = parseInt(pathComponent(path, -3));
|
|
||||||
await TabManager.debugTab(tabId); await TabManager.enableDomainForTab(tabId, "Page");
|
|
||||||
return {fh: 3};
|
|
||||||
},
|
|
||||||
async read({path, fh, size, offset}) {
|
|
||||||
const tabId = parseInt(pathComponent(path, -3));
|
|
||||||
const suffix = pathComponent(path, -1);
|
|
||||||
|
|
||||||
const {frameTree} = await sendDebuggerCommand(tabId, "Page.getResourceTree", {}); // FIXME: cache this
|
|
||||||
for (let resource of frameTree.resources) {
|
|
||||||
const resourceSuffix = sanitize(String(resource.url).slice(0, 200));
|
|
||||||
if (resourceSuffix === suffix) {
|
|
||||||
let {base64Encoded, content} = await sendDebuggerCommand(tabId, "Page.getResourceContent", {
|
|
||||||
frameId: frameTree.frame.id,
|
|
||||||
url: resource.url
|
|
||||||
});
|
|
||||||
if (base64Encoded) {
|
|
||||||
const arr = Uint8Array.from(atob(content), c => c.charCodeAt(0));
|
|
||||||
const slice = arr.slice(offset, offset + size);
|
|
||||||
return { buf: String.fromCharCode(...slice) };
|
|
||||||
} else {
|
|
||||||
return { buf: utf8(content, offset, size) };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new UnixError(unix.ENOENT);
|
|
||||||
},
|
|
||||||
async release({path, fh}) {
|
|
||||||
// FIXME: free the debug?
|
|
||||||
return {};
|
|
||||||
}
|
}
|
||||||
};
|
throw new UnixError(unix.ENOENT);
|
||||||
|
});
|
||||||
|
|
||||||
router["/tabs/by-id/*/control"] = {
|
router["/tabs/by-id/*/control"] = {
|
||||||
// echo remove >> mnt/tabs/by-id/1644/control
|
// echo remove >> mnt/tabs/by-id/1644/control
|
||||||
|
|
Loading…
Reference in a new issue