Skip to content

Commit afea52d

Browse files
committed
emulator.ts reject/resolve fix. formatting.
1 parent 84f0770 commit afea52d

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

scripts/emulator-testing/emulators/emulator.ts

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import * as os from 'os';
2323
import * as path from 'path';
2424
// @ts-ignore
2525
import * as tmp from 'tmp';
26-
import { Readable } from "stream";
2726

2827
export abstract class Emulator {
2928
binaryPath: string | null = null;
@@ -58,33 +57,47 @@ export abstract class Emulator {
5857
const filepath: string = path.resolve(dir, this.binaryName);
5958
const writer = fs.createWriteStream(filepath);
6059
console.log(`Downloading emulator from [${this.binaryUrl}] ...`);
61-
const downloadPromise = new Promise((downloadComplete, downloadFailed) => {
62-
try {
63-
fetch(this.binaryUrl).then(resp => {
64-
const reader = resp.body?.getReader();
65-
reader?.read().then(function readStuff({ done, value }): any {
66-
if (done) {
67-
downloadComplete;
68-
} else {
69-
writer.write(value);
70-
return reader.read().then(readStuff);
60+
// Map the DOM's fetch Reader to node's streaming file system
61+
// operations.
62+
const downloadPromise = new Promise<void>(
63+
(downloadComplete, downloadFailed) => {
64+
fetch(this.binaryUrl)
65+
.then(resp => {
66+
if (resp.status !== 200) {
67+
console.log('Download of emulator failed: ', resp.statusText);
68+
downloadFailed();
7169
}
70+
const reader = resp.body?.getReader();
71+
reader?.read().then(function readChunk({ done, value }): any {
72+
if (done) {
73+
downloadComplete();
74+
} else {
75+
writer.write(value);
76+
return reader.read().then(readChunk);
77+
}
78+
});
79+
})
80+
.catch(e => {
81+
console.log(`Download of emulator failed: ${e}`);
82+
downloadFailed();
7283
});
73-
});
74-
} catch (e) {
75-
console.log(`Download of emulator failed: ${e}`);
76-
downloadFailed();
7784
}
78-
});
79-
downloadPromise.then(() => {
80-
this.binaryPath = filepath;
81-
if (this.copyToCache()) {
82-
console.log(`Cached emulator at ${this.cacheBinaryPath}`);
85+
);
86+
// Copy to cache when the download completes, and resolve
87+
// the promise returned by this method.
88+
downloadPromise.then(
89+
() => {
90+
console.log('Download complete');
91+
this.binaryPath = filepath;
92+
if (this.copyToCache()) {
93+
console.log(`Cached emulator at ${this.cacheBinaryPath}`);
94+
}
95+
resolve();
96+
},
97+
() => {
98+
reject();
8399
}
84-
resolve;
85-
}, () => {
86-
reject;
87-
});
100+
);
88101
});
89102
});
90103
}

0 commit comments

Comments
 (0)