X-Git-Url: https://git.kengrimes.com/?p=henge%2Fkiak.git;a=blobdiff_plain;f=src%2Fstrapp.js;fp=src%2Fstrapp.js;h=c37ba162fe6891fee900c81d6aa45192dc267440;hp=aefbc1b9f9a5429b47c9d51a8c2b02a5cf7b56b6;hb=2b03fdac9b4b79770ab8cd5ded7b0353e179ab4d;hpb=b98d95ba22c7a5b1bcf89fb8ac0a8b159f8c8184 diff --git a/src/strapp.js b/src/strapp.js index aefbc1b..c37ba16 100644 --- a/src/strapp.js +++ b/src/strapp.js @@ -1,149 +1,42 @@ /** -* @file Strapp main driver +* @file Strapp API and initialization driver * @author Jordan Lavatai, Ken Grimes * @version 0.0.4 * @license AGPL-3.0 * @copyright August 2017 - Ken Grimes, Jordan Lavatai -* @summmary Bootstrapper for the strapp.io mechanism +* @summmary */ -import LocalForage from "localforage" +import fs from "./strappFileSystem.js" -const StrappFile = (() => { - const localforage = LocalForage.createInstance({ - driver: [LocalForage.LOCALSTORAGE, - LocalForage.INDEXEDDB, - LocalForage.WEBSQL], - name: 'strapp', - version: 0.1, - storeName: 'strapp' - }) - const authorize = (pubKey, mode, stat) => { - let allowed - if (pubKey === stat.owner) - allowed = (stat.perms >>> 16) & 0xF - else { - let gAccess = false - let uGroups = StrappFile.get(`acct/${pubKey}/groups`).split(' ') - for (let i = 0; i < uGroups.length; i++) { - if (uGroups[i] === stat.group) { - gAccess = true - break - } - } - if (gAccess) - allowed = (stat.perms >>> 8) & 0xF - else - allowed = stat.perms & 0xF - } - switch(mode){ - case 'r+': - case 'rw': - case 'wr': - return (allowed & 0x6) === 0x6 - case 'w': - return (allowed & 0x2) === 0x2 - case 'r': - return (allowed & 0x4) === 0x4 - case 'x': - return (allowed & 0x1) === 0x1 - default: - console.log(`Unknown access mode: ${mode}`) - return false - } +const strapp = (() => { + const events = {} + return { + request: (location, method, data) => new Promise((resolve, reject) => { + fs.request(location, method, data) + .then((response) => resolve(response)) + .catch((err) => reject(err)) + }), + on: (eventName, fn) => { + if (!events[eventName]) + events[eventName] = [] + events[eventName].push(fn) + }, + emit: (eventName, data) => + events[eventName] && events[eventName].forEach((fn) => fn(data)) + , + GET: (location) => fs.request(location, 'GET'), + POST: (location, data) => fs.request(location, 'POST', data) } - class StrappFile extends Object { - constructor(...props) { - super() - return Object.assign(this, new.target.defaults, ...props) - } - static PermissionDenied() { - return new Promise((resolve, reject) => reject('Permission denied')) - } - static get(path) { - return localforage.getItem(path) - } - static set(path, data) { - return localforage.setItem(path, data) - } - static delete(path) { - return localforage.removeItem(path) - } - HEAD(opt) { - if (authorize(opt.pubKey, 'r', this.stat)) - return new Promise((resolve, reject) => resolve('')) - else - return StrappFile.PermissionDenied() - } - GET(opt) { - if (authorize(opt.pubKey, 'r', this.stat)) - return StrappFile.get(this.path) - else return StrappFile.PermissionDenied() - } - PUT(opt) { - if (authorize(opt.pubKey, 'w', this.stat)) - return StrappFile.set(this.path, opt.data) - else return StrappFile.PermissionDenied() - } - POST(opt) { - return this.PUT(Object.assign(opt, { data: this.GET(opt) + opt.data })) - } - DELETE(opt) { - if (authorize(opt.pubKey, 'w', this.stat)) - return StrappFile.delete(this.path) - else return StrappFile.PermissionDenied() - } - OPTIONS(opt) { - return this.stat - } - CONNECT(opt) { - return this.GET(opt) - } - TRACE(opt) { - } - PATCH(opt) { - } - } - StrappFile.defaults = { - stat: { - type: 'mime/type', - perm: 0, - owner: 'thisOwnerPubKey', - group: 'groupname', - changed: 'time', - created: 'time', - accessed: 'time - not saved' - } - } - return StrappFile -})() - -const StrappPeerConnection = (() => { - class StrappPeerConnection extends StrappFile { - GET(opts) { - //get metadata (held in filesystem), with owner, usage info, etc - //if unauthed, send message down socket - } - PUT(opts) { - //create w/ sdp, register callback (or pipe), set owner - } - POST(opts) { - //send msg - } - CONNECT(opts) { - //send routing message down socket - //POST(opts.routemessage) - } - } - return StrappPeerConnection + //!set up root account (/acct/local) + //.make device pseudo-files (/dev/keyboard, /dev/touch, /dev/camera, /dev/audio, ...) + //-multiplexed devices: /run//keyboard, /run//touch, /run/ + // (directory whose post/gets are sockets to app) + //devices are just web api event forwarders. e.g.: + //document.addEventListener('keydown') etcetera -> /dev/keyboard.emit('keydown') + //CONNECT adds remote listener? CONNECT gives event emitter (listener) for file + // that emits GET/PUT/POST/DEL/etc events. each file has a "connections" list, + // which are objects with .send methods (sockets, or an event emitter object + // evt emitter object has a fn emit(evt), and .send(evt) -> emit(evt) })() -const StrappDirectory = (() => { - class StrappDirectory extends StrappFile { - } - return StrappDirectory -})() - - - -export default StrappFile -export { StrappPeerConnection, StrappDirectory } +export default strapp