Skip to content

Commit e31cbb5

Browse files
committed
Use async/await for internal readAsync function. NFC
Followup to emscripten-core#23104.
1 parent d04f14a commit e31cbb5

File tree

86 files changed

+102
-103
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+102
-103
lines changed

src/closure-externs/node-externs.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ Buffer.from = function(arrayBufferOrString, byteOffsetOrEncoding, length) {};
8686
*/
8787
Buffer.alloc = function(size, fill, encoding) {};
8888

89+
/**
90+
* @return {boolean}
91+
* @nosideeffects
92+
*/
93+
Buffer.isBuffer = function(obj) {};
94+
8995
/**
9096
* @param {number=} start
9197
* @param {number=} end

src/node_shell_read.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@ readBinary = (filename) => {
99
filename = isFileURI(filename) ? new URL(filename) : filename;
1010
var ret = fs.readFileSync(filename);
1111
#if ASSERTIONS
12-
assert(ret.buffer);
12+
assert(Buffer.isBuffer(ret));
1313
#endif
1414
return ret;
1515
};
1616

17-
readAsync = (filename, binary = true) => {
17+
readAsync = async (filename, binary = true) => {
1818
// See the comment in the `readBinary` function.
1919
filename = isFileURI(filename) ? new URL(filename) : filename;
20-
return new Promise((resolve, reject) => {
21-
fs.readFile(filename, binary ? undefined : 'utf8', (err, data) => {
22-
if (err) reject(err);
23-
else resolve(binary ? data.buffer : data);
24-
});
25-
});
20+
var ret = fs.readFileSync(filename, binary ? undefined : 'utf8');
21+
#if ASSERTIONS
22+
assert(binary ? Buffer.isBuffer(ret) : typeof ret == 'string');
23+
#endif
24+
return ret;
2625
};

src/shell.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,7 @@ if (ENVIRONMENT_IS_SHELL) {
300300
return data;
301301
};
302302

303-
readAsync = (f) => {
304-
return new Promise((resolve, reject) => {
305-
setTimeout(() => resolve(readBinary(f)));
306-
});
307-
};
303+
readAsync = async (f) => readBinary(f);
308304

309305
globalThis.clearTimeout ??= (id) => {};
310306

src/web_or_worker_shell_read.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
}
1717
#endif
1818

19-
readAsync = (url) => {
19+
readAsync = async (url) => {
2020
#if ENVIRONMENT_MAY_BE_WEBVIEW
2121
// Fetch has some additional restrictions over XHR, like it can't be used on a file:// url.
2222
// See https://github.com/github/fetch/pull/92#issuecomment-140665932
@@ -41,11 +41,9 @@
4141
#elif ASSERTIONS
4242
assert(!isFileURI(url), "readAsync does not work with file:// URLs");
4343
#endif
44-
return fetch(url, {{{ makeModuleReceiveExpr('fetchSettings', "{ credentials: 'same-origin' }") }}})
45-
.then((response) => {
46-
if (response.ok) {
47-
return response.arrayBuffer();
48-
}
49-
return Promise.reject(new Error(response.status + ' : ' + response.url));
50-
})
44+
var response = await fetch(url, {{{ makeModuleReceiveExpr('fetchSettings', "{ credentials: 'same-origin' }") }}});
45+
if (response.ok) {
46+
return response.arrayBuffer();
47+
}
48+
throw new Error(response.status + ' : ' + response.url);
5149
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8576
1+
8545
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20969
1+
20951
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8560
1+
8530
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20937
1+
20919
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9614
1+
9579
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24814
1+
24796

0 commit comments

Comments
 (0)