also looking at this
Gate releases on shipped artifact findings
#316
11 files
+454
-25
| @@ -1,9 +1,11 @@ | ||
| 1 | import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs' | |
| 1 | import { createHash } from 'node:crypto' | |
| 2 | import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, writeFileSync } from 'node:fs' | |
| 2 | 3 | import { tmpdir } from 'node:os' |
| 3 | import { dirname, join, resolve } from 'node:path' | |
| 4 | import { dirname, join, resolve, sep } from 'node:path' | |
| 4 | 5 | |
| 5 | 6 | type Manifest = { |
| 6 | 7 | name: string |
| 8 | version: string | |
| 7 | 9 | private?: boolean |
| 8 | 10 | scripts?: Record<string, string> |
| 9 | 11 | peerDependencies?: Record<string, string> |
| @@ -76,10 +78,18 @@ function importSpecifiers(pkg: Manifest): string[] { | ||
| 76 | 78 | return [pkg.name] |
| 77 | 79 | } |
| 78 | 80 | |
| 79 | export async function verifyNpmPackages(buildOnly = false): Promise<void> { | |
| 81 | export async function verifyNpmPackages(buildOnly = false, archiveDir?: string): Promise<void> { | |
| 80 | 82 | const packages = publicPackages() |
| 81 | 83 | if (packages.length === 0) |
| 82 | 84 | throw new Error('No public npm workspaces found') |
| 85 | if (buildOnly && archiveDir) throw new Error('Cannot export archives in build-only mode') | |
| 86 | const output = archiveDir ? resolve(archiveDir) : undefined | |
| 87 | if (output) { | |
| 88 | if (output === root || output.startsWith(`${root}${sep}`)) | |
| 89 | throw new Error('Archive output must be outside the repository') | |
| 90 | mkdirSync(output, { recursive: true }) | |
| 91 | if (readdirSync(output).length) throw new Error('Archive output must be empty') | |
| 92 | } | |
| 83 | 93 | const temp = mkdtempSync(join(tmpdir(), 'craft-npm-verify-')) |
| 84 | 94 | try { |
| 85 | 95 | for (const { dir, pkg } of packages) { |
| @@ -95,12 +105,17 @@ export async function verifyNpmPackages(buildOnly = false): Promise<void> { | ||
| 95 | 105 | return |
| 96 | 106 | |
| 97 | 107 | const specifiers: string[] = [] |
| 108 | const archives: { name: string, version: string, file: string, sha256: string }[] = [] | |
| 98 | 109 | for (const [index, { dir, pkg }] of packages.entries()) { |
| 99 | const archive = join(temp, `${index}.tgz`) | |
| 110 | const file = `${index}.tgz` | |
| 111 | const archive = join(output ?? temp, file) | |
| 100 | 112 | run([process.execPath, 'pm', 'pack', '--filename', archive], dir) |
| 101 | 113 | const entries = run(['tar', '-tzf', archive], temp).trim().split('\n') |
| 102 | 114 | const packed: Manifest = JSON.parse(run(['tar', '-xOzf', archive, 'package/package.json'], temp)) |
| 103 | 115 | verifyArchiveTargets(packed, entries) |
| 116 | if (packed.name !== pkg.name || packed.version !== pkg.version) | |
| 117 | throw new Error(`${pkg.name}: packed identity differs from the source manifest`) | |
| 118 | archives.push({ name: packed.name, version: packed.version, file, sha256: createHash('sha256').update(readFileSync(archive)).digest('hex') }) | |
| 104 | 119 | const installed = join(temp, 'node_modules', pkg.name) |
| 105 | 120 | mkdirSync(installed, { recursive: true }) |
| 106 | 121 | run(['tar', '-xzf', archive, '--strip-components=1', '-C', installed], temp) |
| @@ -133,6 +148,8 @@ export async function verifyNpmPackages(buildOnly = false): Promise<void> { | ||
| 133 | 148 | '--target', 'esnext', '--skipLibCheck', '--ignoreConfig', |
| 134 | 149 | '--typeRoots', join(root, 'node_modules/@types'), |
| 135 | 150 | ], temp) |
| 151 | if (output) | |
| 152 | writeFileSync(join(output, 'manifest.json'), `${JSON.stringify({ packages: archives }, null, 2)}\n`) | |
| 136 | 153 | console.log(`Verified ${packages.length} npm archives and ${specifiers.length} installed import/type entry points.`) |
| 137 | 154 | } |
| 138 | 155 | finally { |
| @@ -140,5 +157,9 @@ export async function verifyNpmPackages(buildOnly = false): Promise<void> { | ||
| 140 | 157 | } |
| 141 | 158 | } |
| 142 | 159 | |
| 143 | if (import.meta.main) | |
| 144 | await verifyNpmPackages(Bun.argv.includes('--build-only')) | |
| 160 | if (import.meta.main) { | |
| 161 | const index = Bun.argv.indexOf('--archive-dir') | |
| 162 | if (index >= 0 && (!Bun.argv[index + 1] || Bun.argv[index + 1]!.startsWith('--'))) | |
| 163 | throw new Error('--archive-dir requires an output path') | |
| 164 | await verifyNpmPackages(Bun.argv.includes('--build-only'), index >= 0 ? Bun.argv[index + 1] : undefined) | |
| 165 | } | |