} - Returns true if the checksums match.
19 | */
20 | export default async function verify(shaUrl, shaOut, cacheDir, ffmpeg, logLevel, shaSum) {
21 | const shaOutExists = await util.fileExists(shaOut);
22 |
23 | if (shaOutExists === false) {
24 | /* Create directory if does not exist. */
25 | await fs.promises.mkdir(path.dirname(shaOut), { recursive: true });
26 |
27 | /* Download SHASUM text file. */
28 | await request(shaUrl, shaOut);
29 | }
30 |
31 | /* Read SHASUM text file */
32 | const shasum = await fs.promises.readFile(shaOut, { encoding: 'utf-8' });
33 | const shasums = shasum.trim().split('\n');
34 | for await (const line of shasums) {
35 | const [storedSha, filePath] = line.split(/\s+/);
36 | const relativeFilePath = path.resolve(cacheDir, filePath);
37 | const relativefilePathExists = await util.fileExists(relativeFilePath);
38 | if (relativefilePathExists) {
39 | const fileBuffer = await fs.promises.readFile(relativeFilePath);
40 | const hash = crypto.createHash('sha256');
41 | hash.update(fileBuffer);
42 | const generatedSha = hash.digest('hex');
43 | if (!crypto.timingSafeEqual(Buffer.from(generatedSha, 'hex'), Buffer.from(storedSha, 'hex'))) {
44 | if (filePath.includes('ffmpeg') && ffmpeg) {
45 | console.warn(`The generated shasum for the community ffmpeg at ${filePath} is ${generatedSha}. The integrity of this file should be manually verified.`);
46 | } else {
47 | const message = `SHA256 checksums do not match. The file ${filePath} expected shasum is ${storedSha} but the actual shasum is ${generatedSha}.`;
48 | if (shaSum) {
49 | throw new Error(message);
50 | } else {
51 | util.log('warn', logLevel, message);
52 | }
53 | }
54 | }
55 | }
56 | }
57 |
58 | return true;
59 | }
60 |
--------------------------------------------------------------------------------
/src/index.d.ts:
--------------------------------------------------------------------------------
1 |
2 | /** NW supported platform */
3 | export type SupportedPlatform = "linux" | "osx" | "win";
4 |
5 | /** Configuration options
6 | */
7 | export interface Options {
8 | /** String of space separated glob patterns which correspond to NW app code */
9 | srcDir?: "./" | string,
10 | /** Run or build application */
11 | mode?: "build" | "get" | "run",
12 | /** NW runtime version */
13 | version?: "latest" | "stable" | string,
14 | /** NW runtime flavor */
15 | flavor?: "normal" | "sdk",
16 | /** NW supported platforms */
17 | platform?: P,
18 | /** NW supported architectures */
19 | arch?: "ia32" | "x64" | "arm64",
20 | /** Directory to store build artifacts */
21 | outDir?: "./out" | string,
22 | /** Directry to store NW binaries */
23 | cacheDir?: "./cache" | string,
24 | /** URI to download NW binaries from */
25 | downloadUrl?: "https://dl.nwjs.io" | string,
26 | /** URI to download manifest */
27 | manifestUrl?: "https://nwjs.io/versions" | string,
28 | /** Refer to Linux/Windows/Osx specific options */
29 | app: AppOptions
,
30 | /** If true the existing cache is used. Otherwise it removes and redownloads it. */
31 | cache?: boolean,
32 | /** If true, "zip", "tar" or "tgz" the outDir directory is compressed. */
33 | zip?: boolean | "zip" | "tar" | "tgz",
34 | /** If true the CLI is used to glob srcDir and parse other options */
35 | cli?: boolean,
36 | /** If true the chromium ffmpeg is replaced by community version */
37 | ffmpeg?: boolean,
38 | /** If true globbing is enabled */
39 | glob?: boolean,
40 | /** Specified log level. */
41 | logLevel?: "error" | "warn" | "info" | "debug",
42 | /** Managed manifest */
43 | managedManifest?: boolean | string | object,
44 | }
45 |
46 | /** Platform-specific application options */
47 | export type AppOptions
=
48 | P extends 'win' ? WindowsAppOptions :
49 | P extends 'osx' ? OsxAppOptions :
50 | P extends 'linux' ? LinuxAppOptions
51 | : (WindowsAppOptions | OsxAppOptions | LinuxAppOptions);
52 |
53 | /** Windows configuration options
54 | *
55 | * References:
56 | * - https://learn.microsoft.com/en-us/windows/win32/msi/version
57 | * - https://learn.microsoft.com/en-gb/windows/win32/sbscs/application-manifests
58 | * - https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2015/deployment/trustinfo-element-clickonce-application?view=vs-2015#requestedexecutionlevel
59 | * - https://learn.microsoft.com/en-gb/windows/win32/menurc/versioninfo-resource
60 | */
61 | export interface WindowsAppOptions {
62 | /** The name of the application */
63 | name?: string,
64 | /** The version of the application */
65 | version?: string,
66 | /** Additional information that should be displayed for diagnostic purposes. */
67 | comments?: string,
68 | /** Company that produced the file—for example, Microsoft Corporation or Standard Microsystems Corporation, Inc. This string is required. */
69 | company: string,
70 | /** File description to be presented to users. This string may be displayed in a list box when the user is choosing files to install. For example, Keyboard Driver for AT-Style Keyboards. This string is required. */
71 | fileDescription: string,
72 | /** Version number of the file. For example, 3.10 or 5.00.RC2. This string is required. */
73 | fileVersion: string,
74 | /** The path to the icon file. It should be a .ico file. */
75 | icon?: string,
76 | /** Internal name of the file, if one exists—for example, a module name if the file is a dynamic-link library. If the file has no internal name, this string should be the original filename, without extension. This string is required. */
77 | internalName: string,
78 | /** Copyright notices that apply to the file. This should include the full text of all notices, legal symbols, copyright dates, and so on. This string is optional. */
79 | legalCopyright?: string,
80 | /** Trademarks and registered trademarks that apply to the file. This should include the full text of all notices, legal symbols, trademark numbers, and so on. This string is optional. */
81 | legalTrademark?: string,
82 | /** Original name of the file, not including a path. This information enables an application to determine whether a file has been renamed by a user. The format of the name depends on the file system for which the file was created. This string is required. */
83 | originalFilename: string,
84 | /** Information about a private version of the file—for example, Built by TESTER1 on \\TESTBED. This string should be present only if VS_FF_PRIVATEBUILD is specified in the fileflags parameter of the root block. */
85 | privateBuild?: string,
86 | /** Name of the product with which the file is distributed. This string is required. */
87 | productName: string,
88 | /** Version of the product with which the file is distributed—for example, 3.10 or 5.00.RC2. This string is required. */
89 | productVersion: string,
90 | /** Text that specifies how this version of the file differs from the standard version—for example, Private build for TESTER1 solving mouse problems on M250 and M250E computers. This string should be present only if VS_FF_SPECIALBUILD is specified in the fileflags parameter of the root block. */
91 | specialBuild?: string,
92 | /** Language of the file, defined by Microsoft, see: https://learn.microsoft.com/en-us/openspecs/office_standards/ms-oe376/6c085406-a698-4e12-9d4d-c3b0ee3dbc4a */
93 | languageCode?: number,
94 | }
95 |
96 | /** Linux configuration options
97 | *
98 | * References:
99 | * https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html
100 | */
101 | export interface LinuxAppOptions {
102 | /** Name of the application */
103 | name?: string,
104 | /** Generic name of the application */
105 | genericName?: string,
106 | /** If true the application is not displayed */
107 | noDisplay?: boolean,
108 | /** Tooltip for the entry, for example "View sites on the Internet". */
109 | comment?: string,
110 | /** Icon to display in file manager, menus, etc. */
111 | icon?: string,
112 | /** A list of strings identifying the desktop environments that should (/not) display a given desktop entry */
113 | onlyShowIn?: string[],
114 | /** A list of strings identifying the desktop environments that should (/not) display a given desktop entry */
115 | notShowIn?: string[],
116 | /** A boolean value specifying if D-Bus activation is supported for this application */
117 | dBusActivatable?: boolean,
118 | /** Path to an executable file on disk used to determine if the program is actually installed */
119 | tryExec?: string,
120 | /** Program to execute, possibly with arguments. */
121 | exec?: string,
122 | /** If entry is of type Application, the working directory to run the program in. */
123 | path?: string,
124 | /** Whether the program runs in a terminal window. */
125 | terminal?: boolean,
126 | /** Identifiers for application actions. */
127 | actions?: string[],
128 | /** The MIME type(s) supported by this application. */
129 | mimeType?: string[],
130 | /** Categories in which the entry should be shown in a menu */
131 | categories?: string[],
132 | /** A list of interfaces that this application implements. */
133 | implements?: string[],
134 | /** A list of strings which may be used in addition to other metadata to describe this entry. */
135 | keywords?: string[],
136 | /** If true, it is KNOWN that the application will send a "remove" message when started with the DESKTOP_STARTUP_ID environment variable set. If false, it is KNOWN that the application does not work with startup notification at all. */
137 | startupNotify?: boolean,
138 | /** If specified, it is known that the application will map at least one window with the given string as its WM class or WM name hin */
139 | startupWMClass?: string,
140 | /** If true, the application prefers to be run on a more powerful discrete GPU if available. */
141 | prefersNonDefaultGPU?: boolean,
142 | /** If true, the application has a single main window, and does not support having an additional one opened. */
143 | singleMainWindow?: string,
144 | }
145 |
146 | /** OSX resource configuration options
147 | *
148 | * References:
149 | * https://developer.apple.com/documentation/bundleresources/information_property_list
150 | */
151 | export interface OsxAppOptions {
152 | /** The name of the application */
153 | name?: string,
154 | /** The path to the icon file. It should be a .icns file. */
155 | icon?: string,
156 | /** The category that best describes your app for the App Store. */
157 | LSApplicationCategoryType?: string,
158 | /** A unique identifier for a bundle usually in reverse DNS format. */
159 | CFBundleIdentifier?: string,
160 | /** A user-visible short name for the bundle. */
161 | CFBundleName?: string,
162 | /** The user-visible name for the bundle. */
163 | CFBundleDisplayName?: string,
164 | /** A replacement for the app name in text-to-speech operations. */
165 | CFBundleSpokenName?: string,
166 | /** The version of the build that identifies an iteration of the bundle. */
167 | CFBundleVersion?: string,
168 | /** The release or version number of the bundle. */
169 | CFBundleShortVersionString?: string,
170 | /** A human-readable copyright notice for the bundle. */
171 | NSHumanReadableCopyright?: string,
172 | /** A human-readable description of why the application needs access to the local network. */
173 | NSLocalNetworkUsageDescription?: string,
174 | }
175 |
176 | /**
177 | * Automates building an NW.js application.
178 | */
179 | declare function nwbuild
(options: Options
): Promise;
180 |
181 | export default nwbuild;
182 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import child_process from 'node:child_process';
2 | import console from 'node:console';
3 | import fs from 'node:fs';
4 | import path from 'node:path';
5 |
6 | import bld from './bld.js';
7 | import get from './get/index.js';
8 | import run from './run.js';
9 | import util from './util.js';
10 |
11 | /**
12 | * @typedef {object} Options Configuration options
13 | * @property {"get" | "run" | "build"} [mode="build"] Choose between get, run or build mode
14 | * @property {"latest" | "stable" | string} [version="latest"] Runtime version
15 | * @property {"normal" | "sdk"} [flavor="normal"] Runtime flavor
16 | * @property {"linux" | "osx" | "win"} platform Host platform
17 | * @property {"ia32" | "x64" | "arm64"} arch Host architecture
18 | * @property {"https://dl.nwjs.io" | string} [downloadUrl="https://dl.nwjs.io"] Download server
19 | * @property {"https://nwjs.io/versions" | string} [manifestUrl="https://nwjs.io/versions"] Versions manifest
20 | * @property {"./cache" | string} [cacheDir="./cache"] Directory to cache NW binaries
21 | * @property {"./" | string} [srcDir="./"] File paths to application code
22 | * @property {"./out" | string} [outDir="./out"] Directory to store build artifacts
23 | * @property {object} app Refer to Linux/Windows Specific Options under Getting Started in the docs
24 | * @property {boolean} [cache=true] If true the existing cache is used. Otherwise it removes and redownloads it.
25 | * @property {boolean} [ffmpeg=false] If true the chromium ffmpeg is replaced by community version
26 | * @property {boolean} [glob=true] If true file globbing is enabled when parsing srcDir.
27 | * @property {"error" | "warn" | "info" | "debug"} [logLevel="info"] Specify level of logging.
28 | * @property {boolean} [shaSum = true] If true, shasum is enabled. Otherwise, disabled.
29 | * @property {boolean | "zip" | "tar" | "tgz"} [zip=false] If true, "zip", "tar" or "tgz" the outDir directory is compressed.
30 | * @property {boolean | string | object} [managedManifest = false] Managed manifest mode
31 | * @property {false | "gyp"} [nodeAddon = false] Rebuild Node native addons
32 | * @property {boolean} [cli=false] If true the CLI is used to parse options. This option is used internally.
33 | */
34 |
35 | /**
36 | * Main module exported.
37 | * @async
38 | * @function
39 | * @param {Options} options Options
40 | * @returns {Promise} - Returns NW.js process if run mode, otherwise returns `undefined`.
41 | */
42 | async function nwbuild(options) {
43 | let built;
44 | let releaseInfo = {};
45 | let manifest = {
46 | path: '',
47 | json: undefined,
48 | };
49 |
50 | try {
51 | /* Parse options */
52 | options = await util.parse(options, manifest);
53 | util.log('debug', 'info', 'Parse initial options');
54 |
55 | util.log('debug', 'info', 'Get node manifest...');
56 | manifest = await util.getNodeManifest({ srcDir: options.srcDir, glob: options.glob });
57 | if (typeof manifest.json?.nwbuild === 'object') {
58 | options = manifest.json.nwbuild;
59 | }
60 |
61 | util.log('info', options.logLevel, 'Parse final options using node manifest');
62 | options = await util.parse(options, manifest.json);
63 | util.log('debug', options.logLevel, 'Manifest: ', `${manifest.path}\n${manifest.json}\n`);
64 |
65 | built = fs.existsSync(options.cacheDir);
66 | if (built === false) {
67 | await fs.promises.mkdir(options.cacheDir, { recursive: true });
68 | }
69 |
70 | if (options.mode === 'build') {
71 | built = fs.existsSync(options.outDir);
72 | if (built === false) {
73 | await fs.promises.mkdir(options.outDir, { recursive: true });
74 | }
75 | }
76 |
77 | /* Validate options.version to get the version specific release info */
78 | util.log('info', options.logLevel, 'Get version specific release info...');
79 | releaseInfo = await util.getReleaseInfo(
80 | options.version,
81 | options.platform,
82 | options.arch,
83 | options.cacheDir,
84 | options.manifestUrl,
85 | );
86 | util.log('debug', options.logLevel, `Release info:\n${JSON.stringify(releaseInfo, null, 2)}\n`);
87 |
88 | util.log('info', options.logLevel, 'Validate options.* ...');
89 | await util.validate(options, releaseInfo);
90 | util.log('debug', options.logLevel, `Options:\n${JSON.stringify(options, null, 2)}`);
91 |
92 | /* Remove leading "v" from version string */
93 | options.version = releaseInfo.version.slice(1);
94 |
95 | util.log('info', options.logLevel, 'Getting NW.js and related binaries...');
96 | await get({
97 | version: options.version,
98 | flavor: options.flavor,
99 | platform: options.platform,
100 | arch: options.arch,
101 | downloadUrl: options.downloadUrl,
102 | cacheDir: options.cacheDir,
103 | cache: options.cache,
104 | ffmpeg: options.ffmpeg,
105 | nativeAddon: options.nativeAddon,
106 | shaSum: options.shaSum,
107 | logLevel: options.logLevel,
108 | });
109 |
110 | if (options.mode === 'get') {
111 | // Do nothing else since we have already downloaded the binaries.
112 | return undefined;
113 | }
114 |
115 | if (options.mode === 'run') {
116 | util.log('info', options.logLevel, 'Running NW.js in run mode...');
117 | const nwProcess = await run({
118 | version: options.version,
119 | flavor: options.flavor,
120 | platform: options.platform,
121 | arch: options.arch,
122 | srcDir: options.srcDir,
123 | cacheDir: options.cacheDir,
124 | glob: options.glob,
125 | argv: options.argv,
126 | });
127 | return nwProcess;
128 | } else if (options.mode === 'build') {
129 | util.log('info', options.logLevel, `Build a NW.js application for ${options.platform} ${options.arch}...`);
130 | await bld({
131 | version: options.version,
132 | flavor: options.flavor,
133 | platform: options.platform,
134 | arch: options.arch,
135 | manifestUrl: options.manifestUrl,
136 | srcDir: options.srcDir,
137 | cacheDir: options.cacheDir,
138 | outDir: options.outDir,
139 | app: options.app,
140 | glob: options.glob,
141 | managedManifest: options.managedManifest,
142 | nativeAddon: options.nativeAddon,
143 | zip: options.zip,
144 | releaseInfo: releaseInfo,
145 | });
146 | util.log('info', options.logLevel, `Appliction is available at ${path.resolve(options.outDir)}`);
147 | }
148 | } catch (error) {
149 | console.error(error);
150 | throw error;
151 | }
152 |
153 | return undefined;
154 | }
155 |
156 | export default nwbuild;
157 |
--------------------------------------------------------------------------------
/src/postinstall.js:
--------------------------------------------------------------------------------
1 | import path from 'node:path';
2 | import url from 'node:url';
3 |
4 | const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
5 |
6 | const baseVoltaPath = path.resolve(path.join(__dirname, '..', 'node_modules', 'base-volta-off-of-nwjs', 'index.js'));
7 |
8 | /* Execute the script in development mode only since it is used during testing */
9 | import(baseVoltaPath)
10 | .then(() => console.log('Node version is updated'))
11 | .catch((error) => console.log(error));
12 |
--------------------------------------------------------------------------------
/src/run.js:
--------------------------------------------------------------------------------
1 | import child_process from 'node:child_process';
2 | import console from 'node:console';
3 | import path from 'node:path';
4 | import process from 'node:process';
5 |
6 | import util from './util.js';
7 |
8 | /**
9 | * @typedef {object} RunOptions
10 | * @property {string | "latest" | "stable" | "lts"} [version = "latest"] Runtime version
11 | * @property {"normal" | "sdk"} [flavor = "normal"] Build flavor
12 | * @property {"linux" | "osx" | "win"} [platform] Target platform
13 | * @property {"ia32" | "x64" | "arm64"} [arch] Target arch
14 | * @property {string} [srcDir = "./src"] Source directory
15 | * @property {string} [cacheDir = "./cache"] Cache directory
16 | * @property {boolean} [glob = false] If true, throw error
17 | * @property {string[]} [argv = []] CLI arguments
18 | */
19 |
20 | /**
21 | * Run NW.js application.
22 | * @async
23 | * @function
24 | * @param {RunOptions} options Run mode options
25 | * @returns {Promise} - A Node.js process object
26 | */
27 | async function run({
28 | version = 'latest',
29 | flavor = 'normal',
30 | platform = util.PLATFORM_KV[process.platform],
31 | arch = util.ARCH_KV[process.arch],
32 | srcDir = './src',
33 | cacheDir = './cache',
34 | glob = false,
35 | argv = [],
36 | }) {
37 | /**
38 | * @type {child_process.ChildProcess | null}
39 | */
40 | let nwProcess = null;
41 |
42 | try {
43 | if (util.EXE_NAME[platform] === undefined) {
44 | throw new Error('Unsupported platform.');
45 | }
46 | if (glob === true) {
47 | throw new Error('Globbing is not supported with run mode.');
48 | }
49 |
50 | const nwDir = path.resolve(
51 | cacheDir,
52 | `nwjs${flavor === 'sdk' ? '-sdk' : ''}-v${version}-${platform}-${arch}`,
53 | );
54 |
55 | return new Promise((res, rej) => {
56 | /* It is assumed that the package.json is located at `${options.srcDir}/package.json` */
57 | nwProcess = child_process.spawn(
58 | path.resolve(nwDir, util.EXE_NAME[platform]),
59 | [...[srcDir], ...argv],
60 | { stdio: 'inherit' },
61 | );
62 |
63 | nwProcess.on('close', () => {
64 | res();
65 | });
66 |
67 | nwProcess.on('error', (error) => {
68 | console.error(error);
69 | rej(error);
70 | });
71 | });
72 | } catch (error) {
73 | console.error(error);
74 | }
75 | return nwProcess;
76 | }
77 |
78 | export default run;
79 |
--------------------------------------------------------------------------------
/src/util.js:
--------------------------------------------------------------------------------
1 | import console from 'node:console';
2 | import fs from 'node:fs';
3 | import https from 'node:https';
4 | import path from 'node:path';
5 | import process from 'node:process';
6 |
7 | import * as GlobModule from 'glob';
8 | import semver from 'semver';
9 |
10 | /**
11 | * Get manifest (array of NW release metadata) from URL.
12 | * @param {string} manifestUrl Url to manifest
13 | * @returns {Promise