Skip to content

Commit

Permalink
Code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Apr 28, 2024
1 parent 1a173c4 commit e843313
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
14 changes: 7 additions & 7 deletions src/fast_transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ export class FastTransformer {
*/
async listen() {
if (this.#process) return this.#port;

this.#port = await getPort();
return new Promise((fulfill, reject) => {
const args = ["-S", `${FastTransformer.#address}:${this.#port}`, "-t", import.meta.dirname];
this.#process = spawn(this.#executable, args, {stdio: ["ignore", "pipe", "ignore"]});
this.#process.on("error", reject);
this.#process.on("spawn", () => setTimeout(() => fulfill(this.#port), 1_000));
});

const {promise, reject, resolve: fulfill} = Promise.withResolvers();
const args = ["-S", `${FastTransformer.#address}:${this.#port}`, "-t", import.meta.dirname];
this.#process = spawn(this.#executable, args, {stdio: ["ignore", "pipe", "ignore"]});
this.#process.on("error", reject);
this.#process.on("spawn", () => setTimeout(() => fulfill(this.#port), 1_000));
return promise;
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/safe_transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ export class SafeTransformer {
* @returns {Promise<string>} The transformed script.
*/
transform(file) {
const maxBuffer = 20 * 1_024 * 1_024;
// eslint-disable-next-line no-promise-executor-return
return new Promise((fulfill, reject) => execFile(this.#executable, ["-w", resolve(file)], {maxBuffer}, (error, stdout) => {
const {promise, reject, resolve: fulfill} = Promise.withResolvers();
execFile(this.#executable, ["-w", resolve(file)], {maxBuffer: 20 * 1_024 * 1_024}, (error, stdout) => {
if (error) reject(error);
else fulfill(stdout);
}));
});

return promise;
}
}

0 comments on commit e843313

Please sign in to comment.