|
| 1 | +// Import Node.js Dependencies |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | + |
1 | 6 | // Import Third-party Dependencies
|
2 |
| -import kleur from "kleur"; |
3 |
| -import { diffChars } from "diff"; |
4 |
| -import { packumentVersion } from "@nodesecure/npm-registry-sdk"; |
| 7 | +import * as npmRegistrySDK from "@nodesecure/npm-registry-sdk"; |
| 8 | +import { diff } from "json-diff-ts"; |
5 | 9 | import { tarball } from "@nodesecure/scanner";
|
| 10 | +import { |
| 11 | + parseNpmSpec, |
| 12 | + packageJSONIntegrityHash |
| 13 | +} from "@nodesecure/mama"; |
6 | 14 |
|
7 |
| -export async function main(spec, options) { |
8 |
| - const [pkgName, pkgVersion] = spec.split("@"); |
9 |
| - const { dist: { tarball: location, shasum: manifestIntegrity } } = await packumentVersion(pkgName, pkgVersion, { |
10 |
| - token: options.token |
11 |
| - }); |
12 |
| - const manifestManager = await tarball.extractAndResolve(location, { |
13 |
| - spec |
14 |
| - }); |
15 |
| - const tarballIntegrity = manifestManager.integrity; |
16 |
| - if (manifestIntegrity === tarballIntegrity) { |
17 |
| - console.log(`integrity: ${manifestIntegrity}`); |
18 |
| - |
19 |
| - return; |
| 15 | +export async function main( |
| 16 | + npmPackageSpec |
| 17 | +) { |
| 18 | + const parsedPackageSpec = parseNpmSpec(npmPackageSpec); |
| 19 | + if (!parsedPackageSpec) { |
| 20 | + throw new Error(`Invalid npm spec: ${npmPackageSpec}`); |
20 | 21 | }
|
21 | 22 |
|
22 |
| - console.log(`manifest integrity: ${manifestIntegrity}`); |
23 |
| - console.log(`tarball integrity: ${tarballIntegrity}`); |
24 |
| - process.stdout.write("integrity diff: "); |
25 |
| - for (const { added, removed, value } of diffChars(manifestIntegrity, tarballIntegrity)) { |
26 |
| - if (added) { |
27 |
| - process.stdout.write(kleur.green().bold(`+${value}`)); |
28 |
| - } |
29 |
| - else if (removed) { |
30 |
| - process.stdout.write(kleur.red().bold(`-${value}`)); |
| 23 | + const packumentVersion = await npmRegistrySDK.packumentVersion( |
| 24 | + parsedPackageSpec.name, |
| 25 | + parsedPackageSpec.semver, |
| 26 | + { |
| 27 | + token: process.env.NODE_SECURE_TOKEN |
31 | 28 | }
|
32 |
| - else { |
33 |
| - process.stdout.write(value); |
| 29 | + ); |
| 30 | + const remote = packageJSONIntegrityHash( |
| 31 | + packumentVersion, |
| 32 | + { isFromRemoteRegistry: true } |
| 33 | + ); |
| 34 | + |
| 35 | + const extractionDirectory = await fs.mkdtemp( |
| 36 | + path.join(os.tmpdir(), "nodesecure-tarball-integrity-") |
| 37 | + ); |
| 38 | + |
| 39 | + try { |
| 40 | + const mama = await tarball.extractAndResolve(extractionDirectory, { |
| 41 | + spec: npmPackageSpec |
| 42 | + }); |
| 43 | + const local = packageJSONIntegrityHash(mama.document); |
| 44 | + |
| 45 | + if (local.integrity === remote.integrity) { |
| 46 | + console.log("no integrity diff found"); |
| 47 | + |
| 48 | + return; |
34 | 49 | }
|
| 50 | + |
| 51 | + const diffs = diff(local.object, remote.object); |
| 52 | + console.log("integrity diff found:"); |
| 53 | + console.log(JSON.stringify(diffs, null, 2)); |
| 54 | + } |
| 55 | + finally { |
| 56 | + await fs.rm(extractionDirectory, { recursive: true, force: true }); |
35 | 57 | }
|
36 |
| - console.log("\n"); |
37 | 58 | }
|
0 commit comments