-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
filestorage.js
161 lines (144 loc) · 4.02 KB
/
filestorage.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
"use strict";
/** @interface */
function FileStorageInterface() {}
/**
* Read a portion of a file.
* @param {string} sha256sum
* @param {number} offset
* @param {number} count
* @return {!Promise<Uint8Array>} null if file does not exist.
*/
FileStorageInterface.prototype.read = function(sha256sum, offset, count) {};
/**
* Add a read-only file to the filestorage.
* @param {string} sha256sum
* @param {!Uint8Array} data
* @return {!Promise}
*/
FileStorageInterface.prototype.cache = function(sha256sum, data) {};
/**
* Call this when the file won't be used soon, e.g. when a file closes or when this immutable
* version is already out of date. It is used to help prevent accumulation of unused files in
* memory in the long run for some FileStorage mediums.
*/
FileStorageInterface.prototype.uncache = function(sha256sum) {};
/**
* @constructor
* @implements {FileStorageInterface}
*/
function MemoryFileStorage()
{
/**
* From sha256sum to file data.
* @type {Map<string,Uint8Array>}
*/
this.filedata = new Map();
}
/**
* @param {string} sha256sum
* @param {number} offset
* @param {number} count
* @return {!Promise<Uint8Array>} null if file does not exist.
*/
MemoryFileStorage.prototype.read = async function(sha256sum, offset, count)
{
dbg_assert(sha256sum, "MemoryFileStorage read: sha256sum should be a non-empty string");
const data = this.filedata.get(sha256sum);
if(!data)
{
return null;
}
return data.subarray(offset, offset + count);
};
/**
* @param {string} sha256sum
* @param {!Uint8Array} data
*/
MemoryFileStorage.prototype.cache = async function(sha256sum, data)
{
dbg_assert(sha256sum, "MemoryFileStorage cache: sha256sum should be a non-empty string");
this.filedata.set(sha256sum, data);
};
/**
* @param {string} sha256sum
*/
MemoryFileStorage.prototype.uncache = function(sha256sum)
{
this.filedata.delete(sha256sum);
};
/**
* @constructor
* @implements {FileStorageInterface}
* @param {FileStorageInterface} file_storage
* @param {string} baseurl
*/
function ServerFileStorageWrapper(file_storage, baseurl)
{
dbg_assert(baseurl, "ServerMemoryFileStorage: baseurl should not be empty");
this.storage = file_storage;
this.baseurl = baseurl;
}
/**
* @param {string} sha256sum
* @return {!Promise<Uint8Array>}
*/
ServerFileStorageWrapper.prototype.load_from_server = function(sha256sum)
{
return new Promise((resolve, reject) =>
{
v86util.load_file(this.baseurl + sha256sum, { done: async buffer =>
{
const data = new Uint8Array(buffer);
await this.cache(sha256sum, data);
resolve(data);
}});
});
};
/**
* @param {string} sha256sum
* @param {number} offset
* @param {number} count
* @return {!Promise<Uint8Array>}
*/
ServerFileStorageWrapper.prototype.read = async function(sha256sum, offset, count)
{
const data = await this.storage.read(sha256sum, offset, count);
if(!data)
{
const full_file = await this.load_from_server(sha256sum);
return full_file.subarray(offset, offset + count);
}
return data;
};
/**
* @param {string} sha256sum
* @param {!Uint8Array} data
*/
ServerFileStorageWrapper.prototype.cache = async function(sha256sum, data)
{
return await this.storage.cache(sha256sum, data);
};
/**
* @param {string} sha256sum
*/
ServerFileStorageWrapper.prototype.uncache = function(sha256sum)
{
this.storage.uncache(sha256sum);
};
// Closure Compiler's way of exporting
if(typeof window !== "undefined")
{
window["MemoryFileStorage"] = MemoryFileStorage;
window["ServerFileStorageWrapper"] = ServerFileStorageWrapper;
}
else if(typeof module !== "undefined" && typeof module.exports !== "undefined")
{
module.exports["MemoryFileStorage"] = MemoryFileStorage;
module.exports["ServerFileStorageWrapper"] = ServerFileStorageWrapper;
}
else if(typeof importScripts === "function")
{
// web worker
self["MemoryFileStorage"] = MemoryFileStorage;
self["ServerFileStorageWrapper"] = ServerFileStorageWrapper;
}