@nwutils/getter 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +49 -0
- package/package.json +51 -0
- package/src/decompress.js +98 -0
- package/src/ffmpeg.js +42 -0
- package/src/main.js +217 -0
- package/src/node.js +52 -0
- package/src/nw.js +63 -0
- package/src/request.js +36 -0
- package/src/verify.js +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Ayushman Chhabra
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# @nwutils/getter
|
|
2
|
+
|
|
3
|
+
Download NW.js and related binaries for Linux, MacOS and Windows.
|
|
4
|
+
|
|
5
|
+
## Getting Started
|
|
6
|
+
|
|
7
|
+
1. Install [Volta](https://volta.sh)
|
|
8
|
+
1. `npm i` to install third party dependencies
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
```js
|
|
13
|
+
import get from "@nwutils/getter";
|
|
14
|
+
|
|
15
|
+
await get({
|
|
16
|
+
...
|
|
17
|
+
});
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## API Reference
|
|
21
|
+
|
|
22
|
+
Options
|
|
23
|
+
|
|
24
|
+
| Name | Type | Default | Description |
|
|
25
|
+
| ---- | ------- | --------- | ----------- |
|
|
26
|
+
| mode | `"get" \| "run" \| "build"` | `"build"` | Choose between get, run or build mode |
|
|
27
|
+
| version | `string \| "latest" \| "stable"` | `"latest"` | Runtime version |
|
|
28
|
+
| flavor | `"normal" \| "sdk"` | `"normal"` | Runtime flavor |
|
|
29
|
+
| platform | `"linux" \| "osx" \| "win"` | | Host platform |
|
|
30
|
+
| arch | `"ia32" \| "x64" \| "arm64"` | | Host architecture |
|
|
31
|
+
| downloadUrl | `"https://dl.nwjs.io" \| "https://npm.taobao.org/mirrors/nwjs" \| https://npmmirror.com/mirrors/nwjs \| "https://github.com/corwin-of-amber/nw.js/releases/"` | `"https://dl.nwjs.io"` | Download server. Supports file systems too (for example `file:///home/localghost/nwjs_mirror`) |
|
|
32
|
+
| manifestUrl | `"https://nwjs.io/versions.json" \| "https://raw.githubusercontent.com/nwutils/nw-builder/main/src/util/osx.arm.versions.json"` | `"https://nwjs.io/versions.json"` | Versions manifest |
|
|
33
|
+
| cacheDir | `string` | `"./cache"` | Directory to cache NW binaries |
|
|
34
|
+
| cache | `boolean` | `true`| If true the existing cache is used. Otherwise it removes and redownloads it. |
|
|
35
|
+
| ffmpeg | `boolean` | `false`| If true the chromium ffmpeg is replaced by community version with proprietary codecs. |
|
|
36
|
+
| logLevel | `"error" \| "warn" \| "info" \| "debug"` | `"info"`| Specify level of logging. |
|
|
37
|
+
| shaSum | `boolean` | `true` | Flag to enable/disable shasum checks. |
|
|
38
|
+
|
|
39
|
+
## Contributing
|
|
40
|
+
|
|
41
|
+
### External contributor
|
|
42
|
+
|
|
43
|
+
- Use Node.js standard libraries whenever possible.
|
|
44
|
+
- Prefer to use syncronous APIs over modern APIs which have been introduced in later versions.
|
|
45
|
+
|
|
46
|
+
### Maintainer
|
|
47
|
+
|
|
48
|
+
- npm trusted publishing is used for releases
|
|
49
|
+
- a package is released when a maintainer creates a release note for a specific version
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nwutils/getter",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Get NW.js desktop applications for MacOS, Windows and Linux.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"NW.js",
|
|
7
|
+
"node-webkit",
|
|
8
|
+
"Desktop",
|
|
9
|
+
"Application"
|
|
10
|
+
],
|
|
11
|
+
"author": {
|
|
12
|
+
"name": "Ayushman Chhabra",
|
|
13
|
+
"url": "https://ayushmanchhabra.com"
|
|
14
|
+
},
|
|
15
|
+
"maintainers": [
|
|
16
|
+
{
|
|
17
|
+
"name": "Ayushman Chhabra",
|
|
18
|
+
"url": "https://ayushmanchhabra.com/"
|
|
19
|
+
}
|
|
20
|
+
],
|
|
21
|
+
"contributors": [
|
|
22
|
+
{
|
|
23
|
+
"name": "@nwutils/getter Contributors",
|
|
24
|
+
"url": "https://github.com/nwutils/getter/graphs/contributors"
|
|
25
|
+
}
|
|
26
|
+
],
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"main": "./src/main.js",
|
|
29
|
+
"type": "module",
|
|
30
|
+
"files": [
|
|
31
|
+
"LICENSE",
|
|
32
|
+
"src"
|
|
33
|
+
],
|
|
34
|
+
"homepage": "https://github.com/nwutils/getter",
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "https://github.com/nwutils/getter.git"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"test": "node --test --experimental-test-coverage --test-coverage-exclude=\"tests/**/*\" \"tests/specs/*.test.js\""
|
|
41
|
+
},
|
|
42
|
+
"volta": {
|
|
43
|
+
"node": "24.9.0",
|
|
44
|
+
"npm": "11.6.2"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"axios": "^1.13.2",
|
|
48
|
+
"tar": "^7.5.2",
|
|
49
|
+
"yauzl-promise": "^4.0.0"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import stream from 'node:stream';
|
|
5
|
+
|
|
6
|
+
import * as tar from 'tar';
|
|
7
|
+
import yauzl from 'yauzl-promise';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Decompresses a file at `filePath` to `cacheDir` directory.
|
|
11
|
+
* @param {string} filePath - file path to compressed binary
|
|
12
|
+
* @param {string} cacheDir - directory to decompress into
|
|
13
|
+
*/
|
|
14
|
+
export default async function decompress(filePath, cacheDir) {
|
|
15
|
+
if (filePath.endsWith('.zip')) {
|
|
16
|
+
await unzip(filePath, cacheDir);
|
|
17
|
+
} else {
|
|
18
|
+
tar.extract({
|
|
19
|
+
file: filePath,
|
|
20
|
+
C: cacheDir,
|
|
21
|
+
sync: true,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Get file mode from entry. Reference implementation is [here](https://github.com/fpsqdb/zip-lib/blob/ac447d269218d396e05cd7072d0e9cd82b5ec52c/src/unzip.ts#L380).
|
|
28
|
+
* @param {yauzl.Entry} entry - Yauzl entry
|
|
29
|
+
* @returns {number} - entry's file mode
|
|
30
|
+
*/
|
|
31
|
+
function modeFromEntry(entry) {
|
|
32
|
+
const attr = entry.externalFileAttributes >> 16 || 33188;
|
|
33
|
+
|
|
34
|
+
return [448 /* S_IRWXU */, 56 /* S_IRWXG */, 7 /* S_IRWXO */]
|
|
35
|
+
.map(mask => attr & mask)
|
|
36
|
+
.reduce((a, b) => a + b, attr & 61440 /* S_IFMT */);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Unzip `zippedFile` to `cacheDir`.
|
|
41
|
+
* @async
|
|
42
|
+
* @function
|
|
43
|
+
* @param {string} zippedFile - file path to .zip file
|
|
44
|
+
* @param {string} cacheDir - directory to unzip in
|
|
45
|
+
* @returns {Promise<void>}
|
|
46
|
+
*/
|
|
47
|
+
async function unzip(zippedFile, cacheDir) {
|
|
48
|
+
const zip = await yauzl.open(zippedFile);
|
|
49
|
+
let entry = await zip.readEntry();
|
|
50
|
+
/* Array to hold symbolic link entries */
|
|
51
|
+
const symlinks = [];
|
|
52
|
+
|
|
53
|
+
while (entry !== null) {
|
|
54
|
+
let entryPathAbs = path.join(cacheDir, entry.filename);
|
|
55
|
+
/* Check if entry is a symbolic link */
|
|
56
|
+
const isSymlink = ((modeFromEntry(entry) & 0o170000) === 0o120000);
|
|
57
|
+
|
|
58
|
+
if (isSymlink) {
|
|
59
|
+
/* Store symlink entries to process later */
|
|
60
|
+
symlinks.push(entry);
|
|
61
|
+
} else {
|
|
62
|
+
/* Handle regular files and directories */
|
|
63
|
+
await fs.promises.mkdir(path.dirname(entryPathAbs), {recursive: true});
|
|
64
|
+
/* Skip directories */
|
|
65
|
+
if (!entry.filename.endsWith('/')) {
|
|
66
|
+
const readStream = await entry.openReadStream();
|
|
67
|
+
const writeStream = fs.createWriteStream(entryPathAbs);
|
|
68
|
+
await stream.promises.pipeline(readStream, writeStream);
|
|
69
|
+
|
|
70
|
+
/* Set file permissions after the file has been written */
|
|
71
|
+
const mode = modeFromEntry(entry);
|
|
72
|
+
await fs.promises.chmod(entryPathAbs, mode);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/* Read next entry */
|
|
77
|
+
entry = await zip.readEntry();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/* Process symbolic links after all other files have been extracted */
|
|
81
|
+
for (const symlinkEntry of symlinks) {
|
|
82
|
+
let entryPathAbs = path.join(cacheDir, symlinkEntry.filename);
|
|
83
|
+
const readStream = await symlinkEntry.openReadStream();
|
|
84
|
+
const chunks = [];
|
|
85
|
+
readStream.on('data', (chunk) => chunks.push(chunk));
|
|
86
|
+
await new Promise(resolve => readStream.on('end', resolve));
|
|
87
|
+
const linkTarget = Buffer.concat(chunks).toString('utf8').trim();
|
|
88
|
+
|
|
89
|
+
/* Check if the symlink or a file/directory already exists at the destination */
|
|
90
|
+
if (fs.existsSync(entryPathAbs)) {
|
|
91
|
+
/* skip */
|
|
92
|
+
} else {
|
|
93
|
+
/* Create symbolic link */
|
|
94
|
+
await fs.promises.symlink(linkTarget, entryPathAbs);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
await zip.close();
|
|
98
|
+
}
|
package/src/ffmpeg.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import request from './request.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Download community FFmpeg binary from `https://github.com/nwjs-ffmpeg-prebuilt/nwjs-ffmpeg-prebuilt`.
|
|
7
|
+
* @param {string} downloadUrl - Download server
|
|
8
|
+
* @param {string} version - Runtime version
|
|
9
|
+
* @param {string} platform - NW supported platform
|
|
10
|
+
* @param {string} arch - NW supported architecture
|
|
11
|
+
* @param {string} cacheDir - Directory to store FFmpeg binary
|
|
12
|
+
* @throws {Error} - When download fails
|
|
13
|
+
* @returns {string} - Path of compressed file which containscommunity FFmpeg binary.
|
|
14
|
+
*/
|
|
15
|
+
export default function ffmpeg(downloadUrl, version, platform, arch, cacheDir) {
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* URL to download specific FFmpeg binary from.
|
|
19
|
+
* @type {string}
|
|
20
|
+
*/
|
|
21
|
+
const url = [
|
|
22
|
+
downloadUrl,
|
|
23
|
+
version,
|
|
24
|
+
`${version}-${platform}-${arch}.zip`,
|
|
25
|
+
].join('/');
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Absolute path of compressed file which contains FFmpeg binary.
|
|
29
|
+
*/
|
|
30
|
+
const ffmpegFileAbs = path.resolve(
|
|
31
|
+
cacheDir,
|
|
32
|
+
`ffmpeg-${version}-${platform}-${arch}.zip`,
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
request(url, ffmpegFileAbs, function (error) {
|
|
36
|
+
if (error) {
|
|
37
|
+
throw error;
|
|
38
|
+
} else {
|
|
39
|
+
return ffmpegFileAbs;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
package/src/main.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import url from 'node:url';
|
|
4
|
+
|
|
5
|
+
import decompress from './decompress.js';
|
|
6
|
+
import ffmpeg from './ffmpeg.js';
|
|
7
|
+
import node from './node.js';
|
|
8
|
+
import nw from './nw.js';
|
|
9
|
+
import verify from './verify.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {object} GetOptions
|
|
13
|
+
* @property {string | "latest" | "stable" | "lts"} [version = "latest"] Runtime version
|
|
14
|
+
* @property {"normal" | "sdk"} [flavor = "normal"] Build flavor
|
|
15
|
+
* @property {"linux" | "osx" | "win"} [platform] Target platform
|
|
16
|
+
* @property {"ia32" | "x64" | "arm64"} [arch] Target arch
|
|
17
|
+
* @property {string} [downloadUrl = "https://dl.nwjs.io"] Download server
|
|
18
|
+
* @property {string} [cacheDir = "./cache"] Cache directory
|
|
19
|
+
* @property {boolean} [cache = true] If false, remove cache and redownload.
|
|
20
|
+
* @property {boolean} [ffmpeg = false] If true, ffmpeg is not downloaded.
|
|
21
|
+
* @property {false | "gyp"} [nativeAddon = false] Rebuild native modules
|
|
22
|
+
* @property {string} [logLevel = 'info'] User defined log level.
|
|
23
|
+
* @property {boolean} [shaSum = true] If true shasum is enabled, otherwise disabled.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Get binaries.
|
|
28
|
+
* @async
|
|
29
|
+
* @function
|
|
30
|
+
* @param {GetOptions} options Get mode options
|
|
31
|
+
* @returns {Promise<void>}
|
|
32
|
+
*/
|
|
33
|
+
async function get(options) {
|
|
34
|
+
|
|
35
|
+
const uri = new url.URL(options.downloadUrl);
|
|
36
|
+
|
|
37
|
+
/* Download server is the cached directory. */
|
|
38
|
+
if (uri.protocol === 'file:') {
|
|
39
|
+
options.cacheDir = path.resolve(decodeURIComponent(options.downloadUrl.slice('file://'.length)));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* If `options.cacheDir` exists, then `true`. Otherwise, it is `false`.
|
|
44
|
+
* @type {boolean}
|
|
45
|
+
*/
|
|
46
|
+
const cacheDirExists = fs.existsSync(options.cacheDir);
|
|
47
|
+
if (cacheDirExists === false) {
|
|
48
|
+
await fs.promises.mkdir(options.cacheDir, { recursive: true });
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* File path to compressed binary.
|
|
53
|
+
* @type {string}
|
|
54
|
+
*/
|
|
55
|
+
let nwFilePath = path.resolve(
|
|
56
|
+
options.cacheDir,
|
|
57
|
+
`nwjs${options.flavor === 'sdk' ? '-sdk' : ''}-v${options.version}-${options.platform}-${options.arch}.${options.platform === 'linux' ? 'tar.gz' : 'zip'
|
|
58
|
+
}`,
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* File path to directory which contain NW.js and related binaries.
|
|
63
|
+
* @type {string}
|
|
64
|
+
*/
|
|
65
|
+
let nwDirPath = path.resolve(
|
|
66
|
+
options.cacheDir,
|
|
67
|
+
`nwjs${options.flavor === 'sdk' ? '-sdk' : ''}-v${options.version}-${options.platform}-${options.arch}`,
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
// If `options.cache` is false, then remove the compressed binary.
|
|
71
|
+
if (options.cache === false) {
|
|
72
|
+
await fs.promises.rm(nwFilePath, {
|
|
73
|
+
recursive: true,
|
|
74
|
+
force: true,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// We remove the nwDir to prevent the edge case where you download with ffmpeg flag enabled
|
|
79
|
+
// but want a subsequent build with ffmpeg flag disabled. By removing the directory and
|
|
80
|
+
// decompressing it again, we prevent the community ffmpeg files from being left over.
|
|
81
|
+
// This is important since the community ffmpeg builds have specific licensing constraints.
|
|
82
|
+
await fs.promises.rm(nwDirPath, { recursive: true, force: true });
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* If the compressed binary exists, then `true`. Otherwise, it is `false`.
|
|
86
|
+
* @type {boolean}
|
|
87
|
+
*/
|
|
88
|
+
const nwFilePathExists = fs.existsSync(nwFilePath);
|
|
89
|
+
if (nwFilePathExists === false) {
|
|
90
|
+
nwFilePath = await nw(options.downloadUrl, options.version, options.flavor, options.platform, options.arch, options.cacheDir);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
await decompress(nwFilePath, options.cacheDir);
|
|
94
|
+
|
|
95
|
+
await verify(
|
|
96
|
+
`${options.downloadUrl}/v${options.version}/SHASUMS256.txt`,
|
|
97
|
+
`${options.cacheDir}/shasum/${options.version}.txt`,
|
|
98
|
+
options.cacheDir,
|
|
99
|
+
options.ffmpeg,
|
|
100
|
+
options.logLevel,
|
|
101
|
+
options.shaSum,
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
if (options.ffmpeg === true) {
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* File path to compressed binary which contains community FFmpeg binary.
|
|
108
|
+
* @type {string}
|
|
109
|
+
*/
|
|
110
|
+
let ffmpegFilePath = path.resolve(
|
|
111
|
+
options.cacheDir,
|
|
112
|
+
`ffmpeg-${options.version}-${options.platform}-${options.arch}.zip`,
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
// If `options.cache` is false, then remove the compressed binary.
|
|
116
|
+
if (options.cache === false) {
|
|
117
|
+
await fs.promises.rm(ffmpegFilePath, {
|
|
118
|
+
recursive: true,
|
|
119
|
+
force: true,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* If the compressed binary exists, then `true`. Otherwise, it is `false`.
|
|
125
|
+
* @type {boolean}
|
|
126
|
+
*/
|
|
127
|
+
const ffmpegFilePathExists = fs.existsSync(ffmpegFilePath);
|
|
128
|
+
if (ffmpegFilePathExists === false) {
|
|
129
|
+
// Do not update the options.downloadUrl with the ffmpeg URL here. Doing so would lead to error when options.ffmpeg and options.nativeAddon are both enabled.
|
|
130
|
+
const downloadUrl =
|
|
131
|
+
'https://github.com/nwjs-ffmpeg-prebuilt/nwjs-ffmpeg-prebuilt/releases/download';
|
|
132
|
+
ffmpegFilePath = await ffmpeg(downloadUrl, options.version, options.platform, options.arch, options.cacheDir);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
await decompress(ffmpegFilePath, options.cacheDir);
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Platform dependant file name of FFmpeg binary.
|
|
139
|
+
* @type {string}
|
|
140
|
+
*/
|
|
141
|
+
let ffmpegFileName = '';
|
|
142
|
+
|
|
143
|
+
if (options.platform === 'linux') {
|
|
144
|
+
ffmpegFileName = 'libffmpeg.so';
|
|
145
|
+
} else if (options.platform === 'win') {
|
|
146
|
+
ffmpegFileName = 'ffmpeg.dll';
|
|
147
|
+
} else if (options.platform === 'osx') {
|
|
148
|
+
ffmpegFileName = 'libffmpeg.dylib';
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* File path to platform specific FFmpeg file.
|
|
153
|
+
* @type {string}
|
|
154
|
+
*/
|
|
155
|
+
let ffmpegBinaryPath = path.resolve(options.cacheDir, ffmpegFileName);
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* File path of where FFmpeg will be copied to.
|
|
159
|
+
* @type {string}
|
|
160
|
+
*/
|
|
161
|
+
let ffmpegBinaryDest = '';
|
|
162
|
+
|
|
163
|
+
if (options.platform === 'linux') {
|
|
164
|
+
ffmpegBinaryDest = path.resolve(nwDirPath, 'lib', ffmpegFileName);
|
|
165
|
+
} else if (options.platform === 'win') {
|
|
166
|
+
ffmpegBinaryDest = path.resolve(nwDirPath, ffmpegFileName);
|
|
167
|
+
} else if (options.platform === 'osx') {
|
|
168
|
+
ffmpegBinaryDest = path.resolve(
|
|
169
|
+
nwDirPath,
|
|
170
|
+
'nwjs.app',
|
|
171
|
+
'Contents',
|
|
172
|
+
'Frameworks',
|
|
173
|
+
'nwjs Framework.framework',
|
|
174
|
+
'Versions',
|
|
175
|
+
'Current',
|
|
176
|
+
ffmpegFileName,
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
await fs.promises.copyFile(ffmpegBinaryPath, ffmpegBinaryDest);
|
|
181
|
+
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (options.nativeAddon === 'gyp') {
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* File path to NW'js Node headers tarball.
|
|
188
|
+
* @type {string}
|
|
189
|
+
*/
|
|
190
|
+
let nodeFilePath = path.resolve(
|
|
191
|
+
options.cacheDir,
|
|
192
|
+
`headers-v${options.version}.tar.gz`,
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
// If `options.cache` is false, then remove the compressed binary.
|
|
196
|
+
if (options.cache === false) {
|
|
197
|
+
await fs.promises.rm(nodeFilePath, {
|
|
198
|
+
recursive: true,
|
|
199
|
+
force: true,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* If the compressed binary exists, then `true`. Otherwise, it is `false`.
|
|
205
|
+
* @type {boolean}
|
|
206
|
+
*/
|
|
207
|
+
const nodeFilePathExists = fs.existsSync(nodeFilePath);
|
|
208
|
+
if (nodeFilePathExists === false) {
|
|
209
|
+
nodeFilePath = await node(options.downloadUrl, options.version, options.cacheDir);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
await decompress(nodeFilePath, options.cacheDir);
|
|
213
|
+
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export default get;
|
package/src/node.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import request from './request.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Download NW.js's Node.js headers.
|
|
7
|
+
* @param {string} downloadUrl - Download server
|
|
8
|
+
* @param {string} version - Runtime version
|
|
9
|
+
* @param {string} cacheDir - Directory to store NW binaries
|
|
10
|
+
* @throws {Error} - When download fails
|
|
11
|
+
* @returns {string} - path of compressed file which contains the Node headers.
|
|
12
|
+
*/
|
|
13
|
+
export default function nw(downloadUrl, version, cacheDir) {
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Name of directory which contains Node headers.
|
|
17
|
+
* @type {string}
|
|
18
|
+
*/
|
|
19
|
+
const nodeDir = `node-v${version}`;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Name of compressed file which contains Node headers.
|
|
23
|
+
* @type {string}
|
|
24
|
+
*/
|
|
25
|
+
const nwFile = `${nodeDir}.tar.gz`;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* URL to download specific Node headers from.
|
|
29
|
+
* @type {string}
|
|
30
|
+
*/
|
|
31
|
+
const url = [
|
|
32
|
+
downloadUrl,
|
|
33
|
+
`v${version}`,
|
|
34
|
+
`nw-headers-v${version}.tar.gz`
|
|
35
|
+
].join('/');
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Absolute path of compressed file which contains Node headers.
|
|
39
|
+
*/
|
|
40
|
+
const nwFileAbs = path.resolve(
|
|
41
|
+
cacheDir,
|
|
42
|
+
nwFile
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
request(url, nwFileAbs, function (error) {
|
|
46
|
+
if (error) {
|
|
47
|
+
throw error;
|
|
48
|
+
} else {
|
|
49
|
+
return nwFileAbs;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
package/src/nw.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
import request from './request.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Download NW.js binary.
|
|
8
|
+
* @param {string} downloadUrl - Download server
|
|
9
|
+
* @param {string} version - Runtime version
|
|
10
|
+
* @param {string} flavor - Runtime build flavor
|
|
11
|
+
* @param {string} platform - NW supported platform
|
|
12
|
+
* @param {string} arch - NW supported architecture
|
|
13
|
+
* @param {string} cacheDir - Directory to store NW binaries
|
|
14
|
+
* @throws {Error} - When download fails
|
|
15
|
+
* @returns {string} - Path of compressed file which contains NW.js binaries.
|
|
16
|
+
*/
|
|
17
|
+
export default function nw(downloadUrl, version, flavor, platform, arch, cacheDir) {
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Name of directory which contains NW.js binaries.
|
|
21
|
+
* @type {string}
|
|
22
|
+
*/
|
|
23
|
+
const nwDir = [
|
|
24
|
+
'nwjs',
|
|
25
|
+
flavor === 'sdk' ? '-sdk' : '',
|
|
26
|
+
`-v${version}-${platform}-${arch}`,
|
|
27
|
+
].join('');
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Name of compressed file which contains NW.js binaries.
|
|
31
|
+
* @type {string}
|
|
32
|
+
*/
|
|
33
|
+
const nwFile = [
|
|
34
|
+
nwDir,
|
|
35
|
+
platform === 'linux' ? 'tar.gz' : 'zip'
|
|
36
|
+
].join('.');
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* URL to download specific NW.js binary from.
|
|
40
|
+
* @type {string}
|
|
41
|
+
*/
|
|
42
|
+
const url = [
|
|
43
|
+
downloadUrl,
|
|
44
|
+
`v${version}`,
|
|
45
|
+
nwFile,
|
|
46
|
+
].join('/');
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Absolute path of compressed file which contains NW.js binaries.
|
|
50
|
+
*/
|
|
51
|
+
const nwFileAbs = path.resolve(
|
|
52
|
+
cacheDir,
|
|
53
|
+
nwFile
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
request(url, nwFileAbs, function (error) {
|
|
57
|
+
if (error) {
|
|
58
|
+
throw error;
|
|
59
|
+
} else {
|
|
60
|
+
return nwFileAbs;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
package/src/request.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import process from 'node:process';
|
|
3
|
+
import stream from 'node:stream';
|
|
4
|
+
|
|
5
|
+
import axios from 'axios';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Download from `url`.
|
|
9
|
+
* @async
|
|
10
|
+
* @function
|
|
11
|
+
* @param {string} url - Download server
|
|
12
|
+
* @param {string} filePath - file path of downloaded content
|
|
13
|
+
* @returns {Promise<void>}
|
|
14
|
+
*/
|
|
15
|
+
export default async function request(url, filePath) {
|
|
16
|
+
|
|
17
|
+
const writeStream = fs.createWriteStream(filePath);
|
|
18
|
+
|
|
19
|
+
/* Listen for SIGINT (Ctrl+C) */
|
|
20
|
+
process.on('SIGINT', function () {
|
|
21
|
+
/* Delete file if it exists. This prevents unnecessary `Central Directory not found` errors. */
|
|
22
|
+
if (fs.existsSync(filePath)) {
|
|
23
|
+
fs.unlinkSync(filePath);
|
|
24
|
+
}
|
|
25
|
+
process.exit();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const response = await axios({
|
|
29
|
+
method: 'get',
|
|
30
|
+
url: url,
|
|
31
|
+
responseType: 'stream'
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
await stream.promises.pipeline(response.data, writeStream);
|
|
35
|
+
|
|
36
|
+
}
|
package/src/verify.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
import request from './request.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Verify the SHA256 checksum of downloaded artifacts.
|
|
9
|
+
* @param {string} shaUrl - URL to get the shasum text file from.
|
|
10
|
+
* @param {string} shaOut - File path to shasum text file.
|
|
11
|
+
* @param {string} cacheDir - File path to cache directory.
|
|
12
|
+
* @param {boolean} ffmpeg - Toggle between community (true) and official (false) ffmpeg binary
|
|
13
|
+
* @param {string} logLevel - User defined log level.
|
|
14
|
+
* @param {boolean} shaSum - Throws error if true, otherwise logs a warning.
|
|
15
|
+
* @throws {Error}
|
|
16
|
+
* @returns {Promise<boolean>} - Returns true if the checksums match.
|
|
17
|
+
*/
|
|
18
|
+
export default async function verify(shaUrl, shaOut, cacheDir, ffmpeg, logLevel, shaSum) {
|
|
19
|
+
const shaOutExists = fs.existsSync(shaOut);
|
|
20
|
+
|
|
21
|
+
if (shaOutExists === false) {
|
|
22
|
+
/* Create directory if does not exist. */
|
|
23
|
+
await fs.promises.mkdir(path.dirname(shaOut), { recursive: true });
|
|
24
|
+
|
|
25
|
+
/* Download SHASUM text file. */
|
|
26
|
+
await request(shaUrl, shaOut);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/* Read SHASUM text file */
|
|
30
|
+
const shasum = await fs.promises.readFile(shaOut, { encoding: 'utf-8' });
|
|
31
|
+
const shasums = shasum.trim().split('\n');
|
|
32
|
+
for await (const line of shasums) {
|
|
33
|
+
const [storedSha, filePath] = line.split(/\s+/);
|
|
34
|
+
const relativeFilePath = path.resolve(cacheDir, filePath);
|
|
35
|
+
const relativefilePathExists = fs.existsSync(relativeFilePath);
|
|
36
|
+
if (relativefilePathExists) {
|
|
37
|
+
const fileBuffer = await fs.promises.readFile(relativeFilePath);
|
|
38
|
+
const hash = crypto.createHash('sha256');
|
|
39
|
+
hash.update(fileBuffer);
|
|
40
|
+
const generatedSha = hash.digest('hex');
|
|
41
|
+
if (!crypto.timingSafeEqual(Buffer.from(generatedSha, 'hex'), Buffer.from(storedSha, 'hex'))) {
|
|
42
|
+
if (filePath.includes('ffmpeg') && ffmpeg) {
|
|
43
|
+
console.warn(`The generated shasum for the community ffmpeg at ${filePath} is ${generatedSha}. The integrity of this file should be manually verified.`);
|
|
44
|
+
} else {
|
|
45
|
+
const message = `SHA256 checksums do not match. The file ${filePath} expected shasum is ${storedSha} but the actual shasum is ${generatedSha}.`;
|
|
46
|
+
if (shaSum) {
|
|
47
|
+
throw new Error(message);
|
|
48
|
+
} else {
|
|
49
|
+
console.log(message);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return true;
|
|
57
|
+
}
|