-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathcommon.js
241 lines (221 loc) · 6.67 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
(function() {
window.WSC = {store_id:"ofhbbkphhbklhfoeikjpcbhemlocgigb"}
WSC.DEBUG = false
WSC.VERBOSE = false
function getchromeversion() {
var version
var match = navigator.userAgent.match(/Chrome\/([\d]+)/)
if (match) {
var version = parseInt(match[1])
}
return version
}
WSC.getchromeversion = getchromeversion
WSC.maybePromise = function(maybePromiseObj, resolveFn, ctx) {
if(maybePromiseObj && maybePromiseObj.then) {
return maybePromiseObj.then(function(ret){ return resolveFn.call(ctx, ret); });
} else {
return resolveFn.call(ctx, maybePromiseObj);
}
}
WSC.strformat = function(s) {
var args = Array.prototype.slice.call(arguments,1,arguments.length);
return s.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
}
WSC.parse_header = function(line) {
debugger
}
WSC.encode_header = function(name, d) {
if (!d) {
return name
}
var out = [name]
for (var k in d) {
var v = d[k]
if (! v) {
out.push(k)
} else {
// quote?
outpush(k + '=' + v)
}
}
return out.join('; ')
}
if (! String.prototype.endsWith) {
String.prototype.endsWith = function(substr) {
for (var i=0; i<substr.length; i++) {
if (this[this.length - 1 - i] !== substr[substr.length - 1 - i]) {
return false
}
}
return true
}
}
if (! String.prototype.startsWith) {
String.prototype.startsWith = function(substr) {
for (var i=0; i<substr.length; i++) {
if (this[i] !== substr[i]) {
return false
}
}
return true
}
}
// common stuff
function EntryCache() {
this.cache = {}
}
var EntryCacheprototype = {
clearTorrent: function() {
// todo
},
clearKey: function(skey) {
var todelete = []
for (var key in this.cache) {
if (key.startsWith(skey)) {
todelete.push(key)
}
}
for (var i=0; i<todelete.length; i++) {
delete this.cache[todelete[i]]
}
},
clear: function() {
this.cache = {}
},
unset: function(k) {
delete this.cache[k]
},
set: function(k,v) {
this.cache[k] = {v: v};
// Copy the last-modified date for later verification.
if (v.lastModifiedDate) {
this.cache[k].lastModifiedDate = v.lastModifiedDate;
}
},
get: function(k) {
if (this.cache[k]) {
var v = this.cache[k].v;
// If the file was modified, then the file object's last-modified date
// will be different (greater than) the copied date. In this case the
// file object will have stale contents so we must invalidate the cache.
// This happens when reading files from Google Drive.
if (v.lastModifiedDate && this.cache[k].lastModifiedDate < v.lastModifiedDate) {
console.log("invalidate file by lastModifiedDate");
this.unset(k);
return null;
} else {
return v;
}
}
}
}
_.extend(EntryCache.prototype, EntryCacheprototype)
window.WSC.entryCache = new EntryCache
window.WSC.entryFileCache = new EntryCache
WSC.recursiveGetEntry = function(filesystem, path, callback, allowFolderCreation) {
var useCache = false
// XXX duplication with jstorrent
var cacheKey = filesystem.filesystem.name +
filesystem.fullPath +
'/' + path.join('/')
var inCache = WSC.entryCache.get(cacheKey)
if (useCache && inCache) {
//console.log('cache hit');
callback(inCache); return
}
var state = {e:filesystem}
function recurse(e) {
if (path.length == 0) {
if (e.name == 'TypeMismatchError') {
state.e.getDirectory(state.path, {create:false}, recurse, recurse)
} else if (e.isFile) {
if (useCache) WSC.entryCache.set(cacheKey,e)
callback(e)
} else if (e.isDirectory) {
//console.log(filesystem,path,cacheKey,state)
if (useCache) WSC.entryCache.set(cacheKey,e)
callback(e)
} else {
callback({error:'path not found'})
}
} else if (e.isDirectory) {
if (path.length > 1) {
// this is not calling error callback, simply timing out!!!
e.getDirectory(path.shift(), {create:!!allowFolderCreation}, recurse, recurse)
} else {
state.e = e
state.path = _.clone(path)
e.getFile(path.shift(), {create:false}, recurse, recurse)
}
} else if (e.name == 'NotFoundError') {
callback({error:e.name, message:e.message})
} else {
callback({error:'file exists'})
}
}
recurse(filesystem)
}
WSC.parseHeaders = function(lines) {
var headers = {}
var line
// TODO - multi line headers?
for (var i=0;i<lines.length;i++) {
line = lines[i]
var j = line.indexOf(':')
headers[ line.slice(0,j).toLowerCase() ] = line.slice(j+1,line.length).trim()
}
return headers
}
function ui82str(arr, startOffset) {
console.assert(arr)
if (! startOffset) { startOffset = 0 }
var length = arr.length - startOffset // XXX a few random exceptions here
var str = ""
for (var i=0; i<length; i++) {
str += String.fromCharCode(arr[i + startOffset])
}
return str
}
function ui82arr(arr, startOffset) {
if (! startOffset) { startOffset = 0 }
var length = arr.length - startOffset
var outarr = []
for (var i=0; i<length; i++) {
outarr.push(arr[i + startOffset])
}
return outarr
}
function str2ab(s) {
var buf = new ArrayBuffer(s.length);
var bufView = new Uint8Array(buf);
for (var i=0; i<s.length; i++) {
bufView[i] = s.charCodeAt(i);
}
return buf;
}
WSC.ui82str = ui82str
WSC.str2ab = str2ab
WSC.stringToUint8Array = function(string) {
var encoder = new TextEncoder()
return encoder.encode(string)
};
WSC.arrayBufferToString = function(buffer) {
var decoder = new TextDecoder()
return decoder.decode(buffer)
};
/*
var logToScreen = function(log) {
logger.textContent += log + "\n";
}
*/
function parseUri(str) {
return new URL(str) // can throw exception, watch out!
}
WSC.parseUri = parseUri
})();