SandboXP

Форк
0
/
filestorage.js 
160 строк · 4.0 Кб
1
"use strict";
2

3
/** @interface */
4
function FileStorageInterface() {}
5

6
/**
7
 * Read a portion of a file.
8
 * @param {string} sha256sum
9
 * @param {number} offset
10
 * @param {number} count
11
 * @return {!Promise<Uint8Array>} null if file does not exist.
12
 */
13
FileStorageInterface.prototype.read = function(sha256sum, offset, count) {};
14

15
/**
16
 * Add a read-only file to the filestorage.
17
 * @param {string} sha256sum
18
 * @param {!Uint8Array} data
19
 * @return {!Promise}
20
 */
21
FileStorageInterface.prototype.cache = function(sha256sum, data) {};
22

23
/**
24
 * Call this when the file won't be used soon, e.g. when a file closes or when this immutable
25
 * version is already out of date. It is used to help prevent accumulation of unused files in
26
 * memory in the long run for some FileStorage mediums.
27
 */
28
FileStorageInterface.prototype.uncache = function(sha256sum) {};
29

30
/**
31
 * @constructor
32
 * @implements {FileStorageInterface}
33
 */
34
function MemoryFileStorage()
35
{
36
    /**
37
     * From sha256sum to file data.
38
     * @type {Map<string,Uint8Array>}
39
     */
40
    this.filedata = new Map();
41
}
42

43
/**
44
 * @param {string} sha256sum
45
 * @param {number} offset
46
 * @param {number} count
47
 * @return {!Promise<Uint8Array>} null if file does not exist.
48
 */
49
MemoryFileStorage.prototype.read = async function(sha256sum, offset, count)
50
{
51
    dbg_assert(sha256sum, "MemoryFileStorage read: sha256sum should be a non-empty string");
52
    const data = this.filedata.get(sha256sum);
53

54
    if(!data)
55
    {
56
        return null;
57
    }
58

59
    return data.subarray(offset, offset + count);
60
};
61

62
/**
63
 * @param {string} sha256sum
64
 * @param {!Uint8Array} data
65
 */
66
MemoryFileStorage.prototype.cache = async function(sha256sum, data)
67
{
68
    dbg_assert(sha256sum, "MemoryFileStorage cache: sha256sum should be a non-empty string");
69
    this.filedata.set(sha256sum, data);
70
};
71

72
/**
73
 * @param {string} sha256sum
74
 */
75
MemoryFileStorage.prototype.uncache = function(sha256sum)
76
{
77
    this.filedata.delete(sha256sum);
78
};
79

80
/**
81
 * @constructor
82
 * @implements {FileStorageInterface}
83
 * @param {FileStorageInterface} file_storage
84
 * @param {string} baseurl
85
 */
86
function ServerFileStorageWrapper(file_storage, baseurl)
87
{
88
    dbg_assert(baseurl, "ServerMemoryFileStorage: baseurl should not be empty");
89

90
    this.storage = file_storage;
91
    this.baseurl = baseurl;
92
}
93

94
/**
95
 * @param {string} sha256sum
96
 * @return {!Promise<Uint8Array>}
97
 */
98
ServerFileStorageWrapper.prototype.load_from_server = function(sha256sum)
99
{
100
    return new Promise((resolve, reject) =>
101
    {
102
        v86util.load_file(this.baseurl + sha256sum, { done: buffer =>
103
        {
104
            const data = new Uint8Array(buffer);
105
            this.cache(sha256sum, data).then(() => resolve(data));
106
        }});
107
    });
108
};
109

110
/**
111
 * @param {string} sha256sum
112
 * @param {number} offset
113
 * @param {number} count
114
 * @return {!Promise<Uint8Array>}
115
 */
116
ServerFileStorageWrapper.prototype.read = async function(sha256sum, offset, count)
117
{
118
    const data = await this.storage.read(sha256sum, offset, count);
119
    if(!data)
120
    {
121
        const full_file = await this.load_from_server(sha256sum);
122
        return full_file.subarray(offset, offset + count);
123
    }
124
    return data;
125
};
126

127
/**
128
 * @param {string} sha256sum
129
 * @param {!Uint8Array} data
130
 */
131
ServerFileStorageWrapper.prototype.cache = async function(sha256sum, data)
132
{
133
    return await this.storage.cache(sha256sum, data);
134
};
135

136
/**
137
 * @param {string} sha256sum
138
 */
139
ServerFileStorageWrapper.prototype.uncache = function(sha256sum)
140
{
141
    this.storage.uncache(sha256sum);
142
};
143

144
// Closure Compiler's way of exporting
145
if(typeof window !== "undefined")
146
{
147
    window["MemoryFileStorage"] = MemoryFileStorage;
148
    window["ServerFileStorageWrapper"] = ServerFileStorageWrapper;
149
}
150
else if(typeof module !== "undefined" && typeof module.exports !== "undefined")
151
{
152
    module.exports["MemoryFileStorage"] = MemoryFileStorage;
153
    module.exports["ServerFileStorageWrapper"] = ServerFileStorageWrapper;
154
}
155
else if(typeof importScripts === "function")
156
{
157
    // web worker
158
    self["MemoryFileStorage"] = MemoryFileStorage;
159
    self["ServerFileStorageWrapper"] = ServerFileStorageWrapper;
160
}
161

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.