From 735d228f1bdc196865aa9cb5d7ca822fc8936e8d Mon Sep 17 00:00:00 2001 From: Jason Suttles Date: Thu, 21 Dec 2023 04:06:43 +0000 Subject: [PATCH] fix(get): wrap `unzipper` in Promise (#999) --- package.json | 2 +- src/get.js | 55 +++++++++++++++++++++++++----------------------- test/get.pre.js | 17 --------------- test/get.test.js | 7 ++++-- 4 files changed, 35 insertions(+), 46 deletions(-) delete mode 100644 test/get.pre.js diff --git a/package.json b/package.json index 0ce5a9afd..f768cd993 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "scripts": { "lint": "eslint src/get.js", "docs": "jsdoc -d docs ./src/get.js ./src/run.js ./src/bld.js", - "test": "node test/get.pre.js && node --test test/get.test.js", + "test": "node --test test/get.test.js", "demo": "cd test/fixture && node demo.js" }, "devDependencies": { diff --git a/src/get.js b/src/get.js index 455d9f7c8..b6e616475 100644 --- a/src/get.js +++ b/src/get.js @@ -13,20 +13,20 @@ import util from "./util.js"; /** * @typedef {object} GetOptions - * @property {string | "latest" | "stable" | "lts"} [options.version = "latest"] Runtime version - * @property {"normal" | "sdk"} [options.flavor = "normal"] Build flavor - * @property {"linux" | "osx" | "win"} [options.platform] Target platform - * @property {"ia32" | "x64" | "arm64"} [options.arch] Target arch - * @property {string} [options.downloadUrl = "https://dl.nwjs.io"] Download server - * @property {string} [options.cacheDir = "./cache"] Cache directory - * @property {boolean} [options.cache = true] If false, remove cache and redownload. - * @property {boolean} [options.ffmpeg = false] If true, ffmpeg is not downloaded. - * @property {false | "gyp"} [options.nativeAddon = false] Rebuild native modules + * @property {string | "latest" | "stable" | "lts"} [version = "latest"] Runtime version + * @property {"normal" | "sdk"} [flavor = "normal"] Build flavor + * @property {"linux" | "osx" | "win"} [platform] Target platform + * @property {"ia32" | "x64" | "arm64"} [arch] Target arch + * @property {string} [downloadUrl = "https://dl.nwjs.io"] Download server + * @property {string} [cacheDir = "./cache"] Cache directory + * @property {boolean} [cache = true] If false, remove cache and redownload. + * @property {boolean} [ffmpeg = false] If true, ffmpeg is not downloaded. + * @property {false | "gyp"} [nativeAddon = false] Rebuild native modules */ /** * Get binaries. - * + * * @async * @function * @param {GetOptions} options Get mode options @@ -37,11 +37,11 @@ import util from "./util.js"; * nwbuild({ * mode: "get", * }); - * + * * // Use with nw module * nwbuild({ * mode: "get", - * cacheDir: "./node_modules/nw" + * cacheDir: "./node_modules/nw" * }); * * @example @@ -125,13 +125,14 @@ const getNwjs = async (options) => { C: options.cacheDir }); } else { - fs.createReadStream(out) - .pipe(unzipper.Extract({ path: options.cacheDir })) - .on("finish", async () => { - if (options.platform === "osx") { - await createSymlinks(options); - } - }); + await new Promise((res) => { + fs.createReadStream(out) + .pipe(unzipper.Extract({ path: options.cacheDir })) + .on("finish", res); + }); + if (options.platform === "osx") { + await createSymlinks(options); + } } return; } @@ -201,13 +202,15 @@ const getNwjs = async (options) => { C: options.cacheDir }); } else { - fs.createReadStream(out) - .pipe(unzipper.Extract({ path: options.cacheDir })) - .on("finish", async () => { - if (options.platform === "osx") { - await createSymlinks(options); - } - }); + await new Promise((res) => { + fs.createReadStream(out) + .pipe(unzipper.Extract({ path: options.cacheDir })) + .on("finish", res); + }); + if (options.platform === "osx") { + await createSymlinks(options); + } + } } diff --git a/test/get.pre.js b/test/get.pre.js deleted file mode 100644 index b1b544560..000000000 --- a/test/get.pre.js +++ /dev/null @@ -1,17 +0,0 @@ -import get from "../src/get.js"; - -// TODO: fix get function and move this into a before hook -// Running this inside a before hook makes the test suite fail. -// There is likely some asyncronous behaviour that is not being handled properly. -// This allows the test suite to pass. -await get({ - version: "0.82.0", - flavor: "sdk", - platform: "osx", - arch: "x64", - downloadUrl: "https://dl.nwjs.io", - cacheDir: "test/fixture/cache", - cache: true, - ffmpeg: false, - nativeAddon: false, -}); diff --git a/test/get.test.js b/test/get.test.js index 384360aaa..915773259 100644 --- a/test/get.test.js +++ b/test/get.test.js @@ -3,10 +3,10 @@ import fs from "node:fs"; import fsm from "node:fs/promises"; import path from "node:path"; import process from "node:process"; -import { describe, it } from "node:test"; +import { describe, it, before } from "node:test"; +import get from '../src/get.js'; describe("get", async () => { - const options = { version: "0.82.0", flavor: "sdk", @@ -18,6 +18,9 @@ describe("get", async () => { ffmpeg: false, nativeAddon: false, }; + before(async () => { + await get(options); + }); it("downloads macos binary", async function () { assert.strictEqual(fs.existsSync(path.resolve(process.cwd(), options.cacheDir, `nwjs${options.flavor === "sdk" ? "-sdk" : ""}-v${options.version}-${options.platform}-${options.arch}`, "nwjs.app")), true);