1 |
|
2 |
|
3 |
|
4 |
|
5 | class SolidLocalStorage {
|
6 | constructor() {
|
7 | this.prefix = "ls"
|
8 | this.name = "solid-rest-localStorage-1.0.0"
|
9 | localStorage.setItem( "/", " " );
|
10 | }
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | async getObjectType(pathname,options){
|
20 | let type = (pathname.match(/\/$/)) ? "Container" : "Resource";
|
21 | pathname = pathname.replace(/\/$/,'')
|
22 | let exists = false
|
23 | let keys = Object.keys(localStorage)
|
24 | for(var k in keys) {
|
25 | let item = keys[k]
|
26 | if(type==="Container" && item.startsWith(pathname)){ exists=true; break }
|
27 | if(item === pathname){ exists=true; break }
|
28 | }
|
29 | return [type,exists]
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | async getResource(pathname,options){
|
39 | try {
|
40 | let body = localStorage.getItem( pathname );
|
41 | return Promise.resolve( [ 200, body ] )
|
42 | }
|
43 | catch(e){ Promise.resolve( [500] ) }
|
44 | }
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | async getContainer(pathname,options) {
|
52 | const files = Object.keys(localStorage)
|
53 | .filter(path => path.startsWith(pathname) && path != pathname)
|
54 | .map(path => path.substr(pathname.length))
|
55 | .filter(path => !path.slice(0, -1).includes("/"))
|
56 |
|
57 | return files
|
58 | }
|
59 |
|
60 | dump(pathname,options) {
|
61 | let keys = Object.keys(localStorage).filter(k=>{
|
62 | if(!k.match(/(setItem|getItem|removeItem)/)) return k
|
63 | }).map(m=>{
|
64 | console.log( m, localStorage.getItem(m) )
|
65 | })
|
66 | }
|
67 | clear() {
|
68 | let keys = Object.keys(localStorage).filter(k=>{
|
69 | if(!k.match(/(setItem|getItem|removeItem)/)) return k
|
70 | }).map(item=>{
|
71 | this.deleteResource(item)
|
72 | })
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | async putResource(pathname,options){
|
83 | options = options || {};
|
84 | options.body = options.body || "";
|
85 | try {
|
86 | localStorage.setItem( pathname, options.body );
|
87 | return Promise.resolve( [201] )
|
88 | }
|
89 | catch(e){ console.log(e); return Promise.resolve( [500] ) }
|
90 | }
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | async postContainer(pathname,options){
|
100 | pathname = pathname + '/'
|
101 | return this.putResource(pathname,options)
|
102 | }
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | async deleteResource(pathname,options){
|
111 | try {
|
112 | localStorage.removeItem(pathname)
|
113 | return Promise.resolve( [200] )
|
114 | }
|
115 | catch(e){ return Promise.resolve( [500] ) }
|
116 | }
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | async deleteContainer(pathname,options){
|
126 | let files = await this.getContainer(pathname,options)
|
127 | if( files.length ){ return Promise.resolve( [409] ) }
|
128 | return await this.deleteResource(pathname,options)
|
129 | }
|
130 |
|
131 | async makeContainers(pathname,options){
|
132 | const inexistentParents = []
|
133 |
|
134 |
|
135 | let curParent = getParent(pathname)
|
136 | while (curParent && !(await this.getObjectType(curParent))[1]) {
|
137 | inexistentParents.push(curParent)
|
138 | curParent = getParent(curParent)
|
139 | }
|
140 | if (!curParent)
|
141 | return [500]
|
142 |
|
143 |
|
144 | while (inexistentParents.length) {
|
145 |
|
146 | await this.postContainer(inexistentParents.pop().slice(0, -1))
|
147 | }
|
148 | return [201]
|
149 | }
|
150 | }
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | function getParent(url) {
|
159 | while (url.endsWith('/'))
|
160 | url = url.slice(0, -1)
|
161 | if (!url.includes('/'))
|
162 | return null
|
163 | return url.substring(0, url.lastIndexOf('/')) + '/'
|
164 | }
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | if(typeof window==="undefined") {
|
189 | alert = (msg) => console.log(msg)
|
190 | localStorage = {
|
191 | getItem : (key) => { return localStorage[key] },
|
192 | removeItem : (key) => { delete localStorage[key] },
|
193 | setItem : (key,val) => { localStorage[key]=val },
|
194 | }
|
195 | module.exports = SolidLocalStorage
|
196 | }
|