@nwutils/getter 0.2.8 → 0.3.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nwutils/getter",
3
- "version": "0.2.8",
3
+ "version": "0.3.2",
4
4
  "description": "Get NW.js and related binaries for Linux, MacOS and Windows.",
5
5
  "keywords": [
6
6
  "NW.js",
@@ -47,11 +47,19 @@
47
47
  },
48
48
  "dependencies": {
49
49
  "semver": "^7.8.5",
50
- "tar": "^7.5.22",
50
+ "tar": "^7.5.23",
51
51
  "yauzl-promise": "^4.0.0"
52
52
  },
53
- "devDependencies": {
54
- "eslint-plugin-node-security": "^5.2.3",
55
- "eslint-plugin-secure-coding": "^5.1.4"
53
+ "devEngines": {
54
+ "runtime": {
55
+ "name": "node",
56
+ "version": "26.7.0",
57
+ "onFail": "warn"
58
+ },
59
+ "packageManager": {
60
+ "name": "npm",
61
+ "version": "12.0.2",
62
+ "onFail": "warn"
63
+ }
56
64
  }
57
65
  }
package/src/decompress.js CHANGED
@@ -46,13 +46,6 @@ export default async function decompress(filePath, cacheDir, limits = {}) {
46
46
  limits.maxTotalExtractedSize ?? MAX_TOTAL_EXTRACTED_SIZE;
47
47
 
48
48
  if (filePath.endsWith(".zip")) {
49
- /*
50
- * Every entry unzip() writes is resolved through resolveWithin() first -
51
- * file entries, symlink names and symlink targets alike. The rule reports
52
- * the call site rather than the write site, so it cannot see that guard
53
- * from here.
54
- */
55
- // eslint-disable-next-line node-security/no-zip-slip
56
49
  await unzip(filePath, cacheDir, maxEntries, maxTotalExtractedSize);
57
50
  } else {
58
51
  /*
@@ -67,7 +60,6 @@ export default async function decompress(filePath, cacheDir, limits = {}) {
67
60
  * `maxTotalExtractedSize` and skips any entry that would exceed it, so
68
61
  * this is bounded even though the linter can't see through the closure.
69
62
  */
70
- // eslint-disable-next-line node-security/no-zip-slip, secure-coding/no-unlimited-resource-allocation
71
63
  await tar.extract({
72
64
  file: filePath,
73
65
  C: cacheDir,
package/src/main.js CHANGED
@@ -250,13 +250,20 @@ async function get(options) {
250
250
  const checksumHost =
251
251
  uri.protocol === "file:" ? options.downloadUrl : "https://dl.nwjs.io";
252
252
 
253
- /* Verify the archive's checksum before extracting its contents. */
253
+ /*
254
+ * The main NW.js binary's SHASUMS256.txt from `dl.nwjs.io` is always
255
+ * plain-text, regardless of `options.ffmpeg` - that flag only switches
256
+ * the JSON format used for the community ffmpeg release below.
257
+ */
254
258
  await verify(
255
259
  `${checksumHost}/v${options.version}/SHASUMS256.txt`,
256
260
  `${options.cacheDir}/shasum/${options.version}.txt`,
257
261
  options.cacheDir,
258
- options.ffmpeg,
262
+ false,
259
263
  options.shaSum,
264
+ options.version,
265
+ options.platform,
266
+ options.arch,
260
267
  path.basename(nwFilePath),
261
268
  );
262
269
 
@@ -294,6 +301,19 @@ async function get(options) {
294
301
  );
295
302
  }
296
303
 
304
+ const ffmpegChecksumHost = `https://api.github.com/repos/nwjs-ffmpeg-prebuilt/nwjs-ffmpeg-prebuilt/releases/tags/${options.version}`;
305
+ await verify(
306
+ `${ffmpegChecksumHost}/SHASUMS256.txt`,
307
+ `${options.cacheDir}/shasum/${options.version}.txt`,
308
+ options.cacheDir,
309
+ options.ffmpeg,
310
+ options.shaSum,
311
+ options.version,
312
+ options.platform,
313
+ options.arch,
314
+ path.basename(ffmpegFilePath),
315
+ );
316
+
297
317
  await decompress(ffmpegFilePath, options.cacheDir);
298
318
 
299
319
  /**
@@ -378,8 +398,11 @@ async function get(options) {
378
398
  `${checksumHost}/v${options.version}/SHASUMS256.txt`,
379
399
  `${options.cacheDir}/shasum/${options.version}.txt`,
380
400
  options.cacheDir,
381
- options.ffmpeg,
401
+ false,
382
402
  options.shaSum,
403
+ options.version,
404
+ options.platform,
405
+ options.arch,
383
406
  headersFileName(options.version),
384
407
  );
385
408
 
package/src/verify.js CHANGED
@@ -13,7 +13,10 @@ import request from "./request.js";
13
13
  * @param {string} cacheDir - File path to cache directory.
14
14
  * @param {boolean} ffmpeg - Toggle between community (true) and official (false) ffmpeg binary
15
15
  * @param {boolean} shaSum - Throws error if true, otherwise logs a warning. Applies to both a checksum mismatch and `expectedFile` never being checked at all.
16
- * @param {string} [expectedFile] - Relative path, as listed in the SHASUMS file, that this call is actually relying on being verified. Other listed files that don't exist locally (eg. other platforms) are still skipped silently - only `expectedFile` going unchecked is treated as a failure, since that means the caller's "integrity verified" belief was never actually true.
16
+ * @param {string} version - Version of NW.js.
17
+ * @param {string} platform - Platform of NW.js.
18
+ * @param {string} arch - Architecture of NW.js.
19
+ * @param {string} expectedFile - Relative path, as listed in the SHASUMS file, that this call is actually relying on being verified. Other listed files that don't exist locally (eg. other platforms) are still skipped silently - only `expectedFile` going unchecked is treated as a failure, since that means the caller's "integrity verified" belief was never actually true.
17
20
  * @throws {Error}
18
21
  * @returns {Promise<boolean>} - Returns true if the checksums match.
19
22
  */
@@ -23,6 +26,9 @@ export default async function verify(
23
26
  cacheDir,
24
27
  ffmpeg,
25
28
  shaSum,
29
+ version,
30
+ platform,
31
+ arch,
26
32
  expectedFile,
27
33
  ) {
28
34
  const shaOutExists = fs.existsSync(shaOut);
@@ -31,39 +37,74 @@ export default async function verify(
31
37
  /* Create directory if does not exist. */
32
38
  await fs.promises.mkdir(path.dirname(shaOut), { recursive: true });
33
39
 
34
- /* Download SHASUM text file. */
35
- await request(shaUrl, shaOut);
40
+ if (ffmpeg) {
41
+ /* Download SHASUM text file for community ffmpeg binary. */
42
+ await request(shaUrl, shaOut);
43
+ } else {
44
+ /* Download SHASUM text file. */
45
+ await request(shaUrl, shaOut);
46
+ }
36
47
  }
37
48
 
38
- /* Read SHASUM text file */
39
- const shasum = await fs.promises.readFile(shaOut, { encoding: "utf-8" });
40
- const shasums = shasum.trim().split("\n");
49
+ /* TODO: refactor and make it cleaner */
41
50
  let expectedFileWasChecked = false;
51
+ if (ffmpeg) {
52
+ const shasum = JSON.parse(
53
+ await fs.promises.readFile(shaOut, { encoding: "utf-8" }),
54
+ );
55
+ const assetName = `${version}-${platform}-${arch}.zip`;
56
+ for await (const asset of shasum.assets) {
57
+ if (asset.name === assetName) {
58
+ const storedSha = asset.digest.slice(7).toLowerCase();
59
+ const relativeFilePath = path.resolve(cacheDir, expectedFile);
60
+ const relativefilePathExists = fs.existsSync(relativeFilePath);
61
+ if (relativefilePathExists) {
62
+ expectedFileWasChecked = true;
42
63
 
43
- for await (const line of shasums) {
44
- const [storedSha, filePath] = line.split(/\s+/);
45
- const relativeFilePath = path.resolve(cacheDir, filePath);
46
- const relativefilePathExists = fs.existsSync(relativeFilePath);
47
- if (relativefilePathExists) {
48
- if (filePath === expectedFile) {
49
- expectedFileWasChecked = true;
64
+ const fileBuffer = await fs.promises.readFile(relativeFilePath);
65
+ const hash = crypto.createHash("sha256");
66
+ hash.update(fileBuffer);
67
+ const generatedSha = hash.digest("hex");
68
+ if (
69
+ !crypto.timingSafeEqual(
70
+ Buffer.from(generatedSha, "hex"),
71
+ Buffer.from(storedSha, "hex"),
72
+ )
73
+ ) {
74
+ const message = `SHA256 checksums do not match. The file ${expectedFile} expected shasum is ${storedSha} but the actual shasum is ${generatedSha}.`;
75
+ if (shaSum) {
76
+ throw new Error(message);
77
+ } else {
78
+ console.log(message);
79
+ }
80
+ }
81
+ }
50
82
  }
83
+ }
84
+ } else {
85
+ /* Read SHASUM text file */
86
+ const shasum = await fs.promises.readFile(shaOut, { encoding: "utf-8" });
87
+ const shasums = shasum.trim().split("\n");
88
+
89
+ for await (const line of shasums) {
90
+ const [storedSha, filePath] = line.split(/\s+/);
91
+ const relativeFilePath = path.resolve(cacheDir, filePath);
92
+ const relativefilePathExists = fs.existsSync(relativeFilePath);
93
+ if (relativefilePathExists) {
94
+ if (filePath === expectedFile) {
95
+ expectedFileWasChecked = true;
96
+ }
51
97
 
52
- const fileBuffer = await fs.promises.readFile(relativeFilePath);
53
- const hash = crypto.createHash("sha256");
54
- hash.update(fileBuffer);
55
- const generatedSha = hash.digest("hex");
56
- if (
57
- !crypto.timingSafeEqual(
58
- Buffer.from(generatedSha, "hex"),
59
- Buffer.from(storedSha, "hex"),
60
- )
61
- ) {
62
- if (filePath.includes("ffmpeg") && ffmpeg) {
63
- console.warn(
64
- `The generated shasum for the community ffmpeg at ${filePath} is ${generatedSha}. The integrity of this file should be manually verified.`,
65
- );
66
- } else {
98
+ const fileBuffer = await fs.promises.readFile(relativeFilePath);
99
+ const hash = crypto.createHash("sha256");
100
+ hash.update(fileBuffer);
101
+ const generatedSha = hash.digest("hex");
102
+ if (
103
+ !crypto.timingSafeEqual(
104
+ Buffer.from(generatedSha, "hex"),
105
+ Buffer.from(storedSha, "hex"),
106
+ )
107
+ ) {
67
108
  const message = `SHA256 checksums do not match. The file ${filePath} expected shasum is ${storedSha} but the actual shasum is ${generatedSha}.`;
68
109
  if (shaSum) {
69
110
  throw new Error(message);
@@ -11,6 +11,6 @@
11
11
  * @returns {Promise<void>}
12
12
  */
13
13
  export default function decompress(filePath: string, cacheDir: string, limits?: {
14
- maxEntries?: number;
15
- maxTotalExtractedSize?: number;
14
+ maxEntries?: number | undefined;
15
+ maxTotalExtractedSize?: number | undefined;
16
16
  }): Promise<void>;
package/types/main.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export default get;
1
2
  export type Options = {
2
3
  /**
3
4
  * Runtime version
@@ -66,4 +67,3 @@ export type Options = {
66
67
  * @returns {Promise<void>}
67
68
  */
68
69
  declare function get(options: Options): Promise<void>;
69
- export default get;
package/types/node.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * @param {string} version - Runtime version
6
6
  * @returns {string} - eg. "node-v0.111.3.tar.gz" or "nw-headers-v0.107.0.tar.gz"
7
7
  */
8
- export declare function headersFileName(version: string): string;
8
+ export function headersFileName(version: string): string;
9
9
  /**
10
10
  * Download NW.js's Node.js headers.
11
11
  * @async
package/types/verify.d.ts CHANGED
@@ -7,8 +7,11 @@
7
7
  * @param {string} cacheDir - File path to cache directory.
8
8
  * @param {boolean} ffmpeg - Toggle between community (true) and official (false) ffmpeg binary
9
9
  * @param {boolean} shaSum - Throws error if true, otherwise logs a warning. Applies to both a checksum mismatch and `expectedFile` never being checked at all.
10
- * @param {string} [expectedFile] - Relative path, as listed in the SHASUMS file, that this call is actually relying on being verified. Other listed files that don't exist locally (eg. other platforms) are still skipped silently - only `expectedFile` going unchecked is treated as a failure, since that means the caller's "integrity verified" belief was never actually true.
10
+ * @param {string} version - Version of NW.js.
11
+ * @param {string} platform - Platform of NW.js.
12
+ * @param {string} arch - Architecture of NW.js.
13
+ * @param {string} expectedFile - Relative path, as listed in the SHASUMS file, that this call is actually relying on being verified. Other listed files that don't exist locally (eg. other platforms) are still skipped silently - only `expectedFile` going unchecked is treated as a failure, since that means the caller's "integrity verified" belief was never actually true.
11
14
  * @throws {Error}
12
15
  * @returns {Promise<boolean>} - Returns true if the checksums match.
13
16
  */
14
- export default function verify(shaUrl: string, shaOut: string, cacheDir: string, ffmpeg: boolean, shaSum: boolean, expectedFile?: string): Promise<boolean>;
17
+ export default function verify(shaUrl: string, shaOut: string, cacheDir: string, ffmpeg: boolean, shaSum: boolean, version: string, platform: string, arch: string, expectedFile: string): Promise<boolean>;