Skip to content

Commit

Permalink
fix(get): FFmpeg decompression (#962)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushmanchhabra authored Oct 16, 2023
1 parent 55a35ea commit f05b386
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 91 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

## [4.4.2] - 2023-10-16

### Changed

- Fix FFmpeg decompression.
- Auto generate docs from JSDoc comments.
- Improve TypeScript type definitions.
- Fix get mode.
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nw-builder",
"version": "4.4.1",
"version": "4.4.2",
"description": "Build NW.js desktop applications for MacOS, Windows and Linux.",
"keywords": [
"NW.js",
Expand Down Expand Up @@ -48,8 +48,7 @@
"doc:bld": "node .github/jsdoc.config.cjs && vitepress build doc",
"test:unit": "node --test test/unit/index.js",
"test:e2e": "node --test test/e2e/index.js",
"test:mod": "npm link nw-builder && cd test/fixture && node demo.js",
"test:cli": "npm link nw-builder && cd test/fixture && nwbuild --platform win --arch x64 --outDir out --no-glob app"
"demo": "cd test/fixture && node demo.js"
},
"devDependencies": {
"concurrently": "^8.2.1",
Expand All @@ -73,6 +72,6 @@
},
"packageManager": "[email protected]",
"engines": {
"node": ">= v16.20.1 || >= v18.18.0 || >= v20.7.0"
"node": "^16.20.1 || ^18.18.2 || >= 20.8.1"
}
}
49 changes: 18 additions & 31 deletions src/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import progress from "cli-progress";
import compressing from "compressing";

import { log } from "./log.js";
import { PLATFORM_KV, ARCH_KV } from "./util.js";
import { replaceFfmpeg } from "./util/ffmpeg.js";
import { ARCH_KV, PLATFORM_KV, replaceFfmpeg } from "./util.js";

/**
* _Note: This an internal function which is not called directly. Please see example usage below._
Expand Down Expand Up @@ -155,7 +154,7 @@ async function get_nwjs({
// Check if cache exists.
try {
await readdir(nwDir);
log.debug(`Found existing binaries`);
log.debug(`Found existing NW.js binaries`);
} catch (error) {
log.debug(`No existing binaries`);
nwCached = false;
Expand Down Expand Up @@ -283,37 +282,13 @@ async function get_ffmpeg({
recursive: true,
force: true,
});
log.debug(`FFMPEG zip cache removed`);
}

const unzipFFMPeg = async () => {
if (platform === "linux") {
await compressing.tgz.uncompress(out, nwDir);
} else {
await compressing.zip.uncompress(out, nwDir);
}
let ffmpegFile;
if (platform === "linux") {
ffmpegFile = "libffmpeg.so";
} else if (platform === "win") {
ffmpegFile = "ffmpeg.dll";
} else if (platform === "osx") {
ffmpegFile = "libffmpeg.dylib";
}
await replaceFfmpeg(platform, nwDir, ffmpegFile);

if (cache === false) {
log.debug(`Removing FFMPEG zip cache`);
await rm(out, {
recursive: true,
force: true,
});
log.debug(`FFMPEG zip cache removed`);
}
};
// Check if cache exists.
if (existsSync(out)) {
if (existsSync(out) === true) {
log.debug(`Found existing FFMPEG cache`);
await unzipFFMPeg();
await compressing.zip.uncompress(out, nwDir);
return;
}

Expand Down Expand Up @@ -355,5 +330,17 @@ async function get_ffmpeg({
});

// Remove compressed file after download and decompress.
return request.then(unzipFFMPeg);
return request
.then(async () => await compressing.zip.uncompress(out, nwDir))
.then(async () => await replaceFfmpeg(platform, nwDir))
.then(async () => {
if (cache === false) {
log.debug(`Removing FFMPEG zip cache`);
await rm(out, {
recursive: true,
force: true,
});
log.debug(`FFMPEG zip cache removed`);
}
});
}
55 changes: 54 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFile, writeFile } from "node:fs/promises";
import { copyFile, readFile, writeFile } from "node:fs/promises";
import { get } from "node:https";
import { resolve } from "node:path";

Expand Down Expand Up @@ -101,3 +101,56 @@ export const EXE_NAME = {
osx: "nwjs.app/Contents/MacOS/nwjs",
linux: "nw",
};

/**
* Replaces the ffmpeg file in the nwjs directory with the one provided
*
* @param {string} platform The platform to replace the ffmpeg file for
* @param {string} nwDir The directory of the nwjs installation
*/
export const replaceFfmpeg = async (platform, nwDir) => {
let ffmpegFile;
if (platform === "linux") {
ffmpegFile = "libffmpeg.so";
} else if (platform === "win") {
ffmpegFile = "ffmpeg.dll";
} else if (platform === "osx") {
ffmpegFile = "libffmpeg.dylib";
}
const src = resolve(nwDir, ffmpegFile);
if (platform === "linux") {
const dest = resolve(nwDir, "lib", ffmpegFile);
await copyFile(src, dest);
} else if (platform === "win") {
// don't do anything for windows because the extracted file is already in the correct path
// await copyFile(src, resolve(nwDir, ffmpegFile));
} else if (platform === "osx") {
let dest = resolve(
nwDir,
"nwjs.app",
"Contents",
"Frameworks",
"nwjs Framework.framework",
"Versions",
"Current",
ffmpegFile,
);

try {
await copyFile(src, dest);
} catch (e) {
//some versions of node/macOS complain about destination being a file, and others complain when it is only a directory.
//the only thing I can think to do is to try both
dest = resolve(
nwDir,
"nwjs.app",
"Contents",
"Frameworks",
"nwjs Framework.framework",
"Versions",
"Current",
);
await copyFile(src, dest);
}
}
};
48 changes: 0 additions & 48 deletions src/util/ffmpeg.js

This file was deleted.

19 changes: 15 additions & 4 deletions test/fixture/demo.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import nwbuild from "nw-builder";
import nwbuild from "../../src/index.js";

await nwbuild({
mode: "build",
version: "0.80.0",
srcDir: "app",
outDir: "out",
mode: "get",
version: "stable",
flavor: "normal",
platform: "linux",
arch: "x64",
cache: true,
ffmpeg: true,
glob: false,
app: {
company: "Some Company",
fileDescription: "Process Name",
productName: "Some Product",
legalCopyright: "Copyright (c) 2023",
},
logLevel: "debug",
});

0 comments on commit f05b386

Please sign in to comment.