00f10507611057a94f6f1149295bca150355a8fa
2 * @file Strapp File System
3 * @author Jordan Lavatai, Ken Grimes
6 * @copyright August 2017 - Ken Grimes, Jordan Lavatai
7 * @summmary File system implementation for a strapp node
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
)
71 static routeMessage(lmkid
) {
72 //split lmkid by spaces
73 //regex sanitize. if '/', MSG. else if ' ', resolve method
76 if (authorize(opt
.pubKey
, 'r', this.stat
))
77 return new Promise((resolve
, reject
) => resolve(''))
79 return StrappFile
.PermissionDenied()
82 if (authorize(opt
.pubKey
, 'r', this.stat
))
83 return StrappFile
.get(this.path
)
84 else return StrappFile
.PermissionDenied()
87 if (authorize(opt
.pubKey
, 'w', this.stat
))
88 return StrappFile
.set(this.path
, opt
.data
)
89 else return StrappFile
.PermissionDenied()
92 return this.PUT(Object
.assign(opt
, { data
: this.GET(opt
) + opt
.data
}))
95 if (authorize(opt
.pubKey
, 'w', this.stat
))
96 return StrappFile
.delete(this.path
)
97 else return StrappFile
.PermissionDenied()
102 CONNECT(opt
) { //make channel
110 StrappFile
.defaults
= {
114 owner
: 'thisOwnerPubKey',
118 accessed
: 'time - not saved'
124 const StrappPeerConnection
= (() => {
125 class StrappPeerConnection
extends StrappFile
{
127 //get metadata (held in filesystem), with owner, usage info, etc
128 //if unauthed, send message down socket
131 //create w/ sdp, register callback (or pipe), set owner
137 //send routing message down socket
138 //POST(opts.routemessage)
141 return StrappPeerConnection
144 const StrappDirectory
= (() => {
145 class StrappDirectory
extends StrappFile
{
147 //send routing message to the directory (handle the next part here)
150 return StrappDirectory
155 export default StrappFile
156 export { StrappPeerConnection
, StrappDirectory
}