forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary_wget.js
244 lines (212 loc) · 7.55 KB
/
library_wget.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
242
243
244
/**
* @license
* Copyright 2011 The Emscripten Authors
* SPDX-License-Identifier: MIT
*/
var LibraryWget = {
$wget: {
wgetRequests: {},
nextWgetRequestHandle: 0,
getNextWgetRequestHandle: function() {
var handle = wget.nextWgetRequestHandle;
wget.nextWgetRequestHandle++;
return handle;
},
},
emscripten_async_wget__deps: ['$PATH_FS', '$wget', '$callUserCallback', '$Browser', '$withStackSave'],
emscripten_async_wget__proxy: 'sync',
emscripten_async_wget__sig: 'viiii',
emscripten_async_wget: function(url, file, onload, onerror) {
{{{ runtimeKeepalivePush() }}}
var _url = UTF8ToString(url);
var _file = UTF8ToString(file);
_file = PATH_FS.resolve(_file);
function doCallback(callback) {
if (callback) {
{{{ runtimeKeepalivePop() }}}
callUserCallback(function() {
withStackSave(function() {
{{{ makeDynCall('vi', 'callback') }}}(allocateUTF8OnStack(_file));
});
});
}
}
var destinationDirectory = PATH.dirname(_file);
FS.createPreloadedFile(
destinationDirectory,
PATH.basename(_file),
_url, true, true,
function() {
doCallback(onload);
},
function() {
doCallback(onerror);
},
false, // dontCreateFile
false, // canOwn
function() { // preFinish
// if a file exists there, we overwrite it
try {
FS.unlink(_file);
} catch (e) {}
// if the destination directory does not yet exist, create it
FS.mkdirTree(destinationDirectory);
}
);
},
emscripten_async_wget_data__deps: ['$asyncLoad', 'malloc', 'free', '$callUserCallback'],
emscripten_async_wget_data__proxy: 'sync',
emscripten_async_wget_data__sig: 'viiii',
emscripten_async_wget_data: function(url, arg, onload, onerror) {
{{{ runtimeKeepalivePush() }}}
asyncLoad(UTF8ToString(url), function(byteArray) {
{{{ runtimeKeepalivePop() }}}
callUserCallback(function() {
var buffer = _malloc(byteArray.length);
HEAPU8.set(byteArray, buffer);
{{{ makeDynCall('viii', 'onload') }}}(arg, buffer, byteArray.length);
_free(buffer);
});
}, function() {
if (onerror) {
{{{ runtimeKeepalivePop() }}}
callUserCallback(function() {
{{{ makeDynCall('vi', 'onerror') }}}(arg);
});
}
}, true /* no need for run dependency, this is async but will not do any prepare etc. step */ );
},
emscripten_async_wget2__deps: ['$PATH_FS', '$wget', '$withStackSave'],
emscripten_async_wget2__proxy: 'sync',
emscripten_async_wget2__sig: 'iiiiiiiii',
emscripten_async_wget2: function(url, file, request, param, arg, onload, onerror, onprogress) {
{{{ runtimeKeepalivePush() }}}
var _url = UTF8ToString(url);
var _file = UTF8ToString(file);
_file = PATH_FS.resolve(_file);
var _request = UTF8ToString(request);
var _param = UTF8ToString(param);
var index = _file.lastIndexOf('/');
var http = new XMLHttpRequest();
http.open(_request, _url, true);
http.responseType = 'arraybuffer';
var handle = wget.getNextWgetRequestHandle();
var destinationDirectory = PATH.dirname(_file);
// LOAD
http.onload = function http_onload(e) {
{{{ runtimeKeepalivePop() }}}
if (http.status >= 200 && http.status < 300) {
// if a file exists there, we overwrite it
try {
FS.unlink(_file);
} catch (e) {}
// if the destination directory does not yet exist, create it
FS.mkdirTree(destinationDirectory);
FS.createDataFile( _file.substr(0, index), _file.substr(index + 1), new Uint8Array(/** @type{ArrayBuffer}*/(http.response)), true, true, false);
if (onload) {
withStackSave(function() {
{{{ makeDynCall('viii', 'onload') }}}(handle, arg, allocateUTF8OnStack(_file));
});
}
} else {
if (onerror) {{{ makeDynCall('viii', 'onerror') }}}(handle, arg, http.status);
}
delete wget.wgetRequests[handle];
};
// ERROR
http.onerror = function http_onerror(e) {
{{{ runtimeKeepalivePop() }}}
if (onerror) {{{ makeDynCall('viii', 'onerror') }}}(handle, arg, http.status);
delete wget.wgetRequests[handle];
};
// PROGRESS
http.onprogress = function http_onprogress(e) {
if (e.lengthComputable || (e.lengthComputable === undefined && e.total != 0)) {
var percentComplete = (e.loaded / e.total)*100;
if (onprogress) {{{ makeDynCall('viii', 'onprogress') }}}(handle, arg, percentComplete);
}
};
// ABORT
http.onabort = function http_onabort(e) {
{{{ runtimeKeepalivePop() }}}
delete wget.wgetRequests[handle];
};
if (_request == "POST") {
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(_param);
} else {
http.send(null);
}
wget.wgetRequests[handle] = http;
return handle;
},
emscripten_async_wget2_data__deps: ['$wget', 'malloc', 'free'],
emscripten_async_wget2_data__proxy: 'sync',
emscripten_async_wget2_data__sig: 'iiiiiiiii',
emscripten_async_wget2_data: function(url, request, param, arg, free, onload, onerror, onprogress) {
var _url = UTF8ToString(url);
var _request = UTF8ToString(request);
var _param = UTF8ToString(param);
var http = new XMLHttpRequest();
http.open(_request, _url, true);
http.responseType = 'arraybuffer';
var handle = wget.getNextWgetRequestHandle();
function onerrorjs() {
if (onerror) {
var statusText = 0;
if (http.statusText) {
var len = lengthBytesUTF8(http.statusText) + 1;
statusText = stackAlloc(len);
stringToUTF8(http.statusText, statusText, len);
}
{{{ makeDynCall('viiii', 'onerror') }}}(handle, arg, http.status, statusText);
}
}
// LOAD
http.onload = function http_onload(e) {
if (http.status >= 200 && http.status < 300 || (http.status === 0 && _url.substr(0,4).toLowerCase() != "http")) {
var byteArray = new Uint8Array(/** @type{ArrayBuffer} */(http.response));
var buffer = _malloc(byteArray.length);
HEAPU8.set(byteArray, buffer);
if (onload) {{{ makeDynCall('viiii', 'onload') }}}(handle, arg, buffer, byteArray.length);
if (free) _free(buffer);
} else {
onerrorjs();
}
delete wget.wgetRequests[handle];
};
// ERROR
http.onerror = function http_onerror(e) {
onerrorjs();
delete wget.wgetRequests[handle];
};
// PROGRESS
http.onprogress = function http_onprogress(e) {
if (onprogress) {{{ makeDynCall('viiii', 'onprogress') }}}(handle, arg, e.loaded, e.lengthComputable || e.lengthComputable === undefined ? e.total : 0);
};
// ABORT
http.onabort = function http_onabort(e) {
delete wget.wgetRequests[handle];
};
if (_request == "POST") {
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(_param);
} else {
http.send(null);
}
wget.wgetRequests[handle] = http;
return handle;
},
emscripten_async_wget2_abort__deps: ['$wget'],
emscripten_async_wget2_abort__proxy: 'sync',
emscripten_async_wget2_abort__sig: 'vi',
emscripten_async_wget2_abort: function(handle) {
var http = wget.wgetRequests[handle];
if (http) {
http.abort();
}
},
};
mergeInto(LibraryManager.library, LibraryWget);