2 * @file Strapp main driver
3 * @author Jordan Lavatai, Ken Grimes
6 * @copyright August 2017 - Ken Grimes, Jordan Lavatai
7 * @summmary Bootstrapper for the strapp.io mechanism
9 import LocalForage
from "localforage"
11 const StrappFile
= (() => {
12 const localforage
= LocalForage
.createInstance({
13 driver
: [LocalForage
.LOCALSTORAGE
,
14 LocalForage
.INDEXEDDB
,
20 const authorize
= (pubKey
, mode
, stat
) => {
22 if (pubKey
=== stat
.owner
)
23 allowed
= (stat
.perms
>>> 16) & 0xF
26 let uGroups
= StrappFile
.get(`acct/${pubKey}/groups`).split(' ')
27 for (let i
= 0; i
< uGroups
.length
; i
++) {
28 if (uGroups
[i
] === stat
.group
) {
34 allowed
= (stat
.perms
>>> 8) & 0xF
36 allowed
= stat
.perms
& 0xF
42 return (allowed
& 0x6) === 0x6
44 return (allowed
& 0x2) === 0x2
46 return (allowed
& 0x4) === 0x4
48 return (allowed
& 0x1) === 0x1
50 console
.log(`Unknown access mode: ${mode}`)
54 class StrappFile
extends Object
{
55 constructor(...props
) {
57 return Object
.assign(this, new.target
.defaults
, ...props
)
59 static PermissionDenied() {
60 return new Promise((resolve
, reject
) => reject('Permission denied'))
63 return localforage
.getItem(path
)
65 static set(path
, data
) {
66 return localforage
.setItem(path
, data
)
69 return localforage
.removeItem(path
)
72 if (authorize(opt
.pubKey
, 'r', this.stat
))
73 return new Promise((resolve
, reject
) => resolve(''))
75 return StrappFile
.PermissionDenied()
78 if (authorize(opt
.pubKey
, 'r', this.stat
))
79 return StrappFile
.get(this.path
)
80 else return StrappFile
.PermissionDenied()
83 if (authorize(opt
.pubKey
, 'w', this.stat
))
84 return StrappFile
.set(this.path
, opt
.data
)
85 else return StrappFile
.PermissionDenied()
88 return this.PUT(Object
.assign(opt
, { data
: this.GET(opt
) + opt
.data
}))
91 if (authorize(opt
.pubKey
, 'w', this.stat
))
92 return StrappFile
.delete(this.path
)
93 else return StrappFile
.PermissionDenied()
106 StrappFile
.defaults
= {
110 owner
: 'thisOwnerPubKey',
114 accessed
: 'time - not saved'
120 const StrappPeerConnection
= (() => {
121 class StrappPeerConnection
extends StrappFile
{
123 //get metadata (held in filesystem), with owner, usage info, etc
124 //if unauthed, send message down socket
127 //create w/ sdp, register callback (or pipe), set owner
133 //send routing message down socket
134 //POST(opts.routemessage)
137 return StrappPeerConnection
140 const StrappDirectory
= (() => {
141 class StrappDirectory
extends StrappFile
{
143 return StrappDirectory
148 export default StrappFile
149 export { StrappPeerConnection
, StrappDirectory
}