forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary_wasmfs_fetch.js
80 lines (73 loc) · 2.6 KB
/
library_wasmfs_fetch.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
/**
* @license
* Copyright 2022 The Emscripten Authors
* SPDX-License-Identifier: MIT
*/
mergeInto(LibraryManager.library, {
// Fetch backend: On first access of the file (either a read or a getSize), it
// will fetch() the data from the network asynchronously. Otherwise, after
// that fetch it behaves just like JSFile (and it reuses the code from there).
_wasmfs_create_fetch_backend_js__deps: [
'$wasmFS$backends',
'$wasmFS$JSMemoryFiles',
'_wasmfs_create_js_file_backend_js',
],
_wasmfs_create_fetch_backend_js: async function(backend) {
// Get a promise that fetches the data and stores it in JS memory (if it has
// not already been fetched).
async function getFile(file) {
if (wasmFS$JSMemoryFiles[file]) {
// The data is already here, so nothing to do before we continue on to
// the actual read below.
return Promise.resolve();
}
// This is the first time we want the file's data.
var url = '';
var fileUrl_p = __wasmfs_fetch_get_file_path(file);
var fileUrl = UTF8ToString(fileUrl_p);
var isAbs = fileUrl.indexOf('://') !== -1;
if (isAbs) {
url = fileUrl;
} else {
try {
var u = new URL(fileUrl, self.location.origin);
url = u.toString();
} catch (e) {
}
}
var response = await fetch(url);
var buffer = await response['arrayBuffer']();
wasmFS$JSMemoryFiles[file] = new Uint8Array(buffer);
}
// Start with the normal JSFile operations. This sets
// wasmFS$backends[backend]
// which we will then augment.
__wasmfs_create_js_file_backend_js(backend);
// Add the async operations on top.
var jsFileOps = wasmFS$backends[backend];
wasmFS$backends[backend] = {
// alloc/free operations are not actually async. Just forward to the
// parent class, but we must return a Promise as the caller expects.
allocFile: async (file) => {
jsFileOps.allocFile(file);
return Promise.resolve();
},
freeFile: async (file) => {
jsFileOps.freeFile(file);
return Promise.resolve();
},
write: async (file, buffer, length, offset) => {
abort("TODO: file writing in fetch backend? read-only for now");
},
// read/getSize fetch the data, then forward to the parent class.
read: async (file, buffer, length, offset) => {
await getFile(file);
return jsFileOps.read(file, buffer, length, offset);
},
getSize: async(file) => {
await getFile(file);
return jsFileOps.getSize(file);
},
};
},
});