|
1 | 1 | import { describe, expect, it, vi } from 'vitest'; |
| 2 | +import { createHash } from 'node:crypto'; |
| 3 | +import { gzipSync } from 'node:zlib'; |
2 | 4 | import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
3 | 5 | import { join } from 'node:path'; |
4 | 6 | import { tmpdir } from 'node:os'; |
5 | 7 |
|
6 | | -const { consumerInstallSpecs, consumerSource, consumerTsConfig, packageTypePath, validateInstalledTypes, waitForRegistryAvailability, registryVersionVisible } = await import('../scripts/npm-consumer-smoke.mjs'); |
| 8 | +const { consumerInstallSpecs, consumerSource, consumerTsConfig, packageTypePath, validateInstalledTypes, waitForRegistryAvailability, registryVersionVisible, tarballAvailable, registryReleaseConsumable } = await import('../scripts/npm-consumer-smoke.mjs'); |
7 | 9 |
|
8 | 10 | describe('npm consumer smoke test (#330)', () => { |
9 | 11 | it('resolves the declared type entry for every exact package', () => { |
@@ -181,4 +183,142 @@ describe('npm consumer smoke test (#330)', () => { |
181 | 183 | expect(() => registryVersionVisible('@svforge/audit@2.2.0', 'npm', |
182 | 184 | fake(Object.assign(new Error('access denied'), { stderr: 'npm error code E401' })))).toThrow(/Registry lookup failed/); |
183 | 185 | }); |
| 186 | + |
| 187 | + describe('exact tarball availability (#628)', () => { |
| 188 | + const gzipBody = gzipSync(Buffer.from('test tarball payload')); |
| 189 | + const integrityOf = (body: Buffer) => `sha512-${createHash('sha512').update(body).digest('base64')}`; |
| 190 | + const tarballUrl = 'https://registry.npmjs.org/svforge/-/svforge-2.5.0.tgz'; |
| 191 | + const metadata = (url = tarballUrl, integrity = integrityOf(gzipBody)) => |
| 192 | + JSON.stringify({ 'dist.tarball': url, 'dist.integrity': integrity }); |
| 193 | + const npmReturning = (payload: string) => (() => payload) as unknown as NonNullable<Parameters<typeof tarballAvailable>[2]>['run']; |
| 194 | + const fetchReturning = (status: number, body: Buffer = gzipBody) => |
| 195 | + (async () => new Response(status === 200 ? new Uint8Array(body) : 'missing', { status })) as unknown as typeof fetch; |
| 196 | + |
| 197 | + it('accepts the exact tarball when the registry metadata and bytes agree', async () => { |
| 198 | + expect( |
| 199 | + await tarballAvailable('svforge@2.5.0', 'npm', { |
| 200 | + run: npmReturning(metadata()), |
| 201 | + fetchImpl: fetchReturning(200) |
| 202 | + }) |
| 203 | + ).toBe(true); |
| 204 | + }); |
| 205 | + |
| 206 | + it('retries only a verified 404 of the expected tarball', async () => { |
| 207 | + const log = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 208 | + try { |
| 209 | + expect( |
| 210 | + await tarballAvailable('svforge@2.5.0', 'npm', { |
| 211 | + run: npmReturning(metadata()), |
| 212 | + fetchImpl: fetchReturning(404) |
| 213 | + }) |
| 214 | + ).toBe(false); |
| 215 | + expect(log).toHaveBeenCalledWith(expect.stringContaining(`tarball ${tarballUrl} returned HTTP 404`)); |
| 216 | + } finally { |
| 217 | + log.mockRestore(); |
| 218 | + } |
| 219 | + }); |
| 220 | + |
| 221 | + it('treats a missing version (E404/ETARGET) as not-ready but never retries other npm failures', async () => { |
| 222 | + const log = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 223 | + try { |
| 224 | + const notFound = (() => { |
| 225 | + throw Object.assign(new Error('missing'), { stderr: 'npm error code E404' }); |
| 226 | + }) as unknown as NonNullable<Parameters<typeof tarballAvailable>[2]>['run']; |
| 227 | + expect(await tarballAvailable('svforge@2.5.0', 'npm', { run: notFound, fetchImpl: fetchReturning(200) })).toBe(false); |
| 228 | + |
| 229 | + const denied = (() => { |
| 230 | + throw Object.assign(new Error('denied'), { stderr: 'npm error code E401' }); |
| 231 | + }) as unknown as NonNullable<Parameters<typeof tarballAvailable>[2]>['run']; |
| 232 | + await expect( |
| 233 | + tarballAvailable('svforge@2.5.0', 'npm', { run: denied, fetchImpl: fetchReturning(200) }) |
| 234 | + ).rejects.toThrow(/Registry lookup failed/); |
| 235 | + } finally { |
| 236 | + log.mockRestore(); |
| 237 | + } |
| 238 | + }); |
| 239 | + |
| 240 | + it('rejects missing or invalid sha512 metadata before accepting a tarball', async () => { |
| 241 | + for (const integrity of [undefined, 'sha1-test', 'sha512-invalid']) { |
| 242 | + const response = JSON.stringify({ 'dist.tarball': tarballUrl, ...(integrity === undefined ? {} : { 'dist.integrity': integrity }) }); |
| 243 | + await expect( |
| 244 | + tarballAvailable('svforge@2.5.0', 'npm', { |
| 245 | + run: npmReturning(response), |
| 246 | + fetchImpl: fetchReturning(200) |
| 247 | + }) |
| 248 | + ).rejects.toThrow(/valid sha512 dist\.integrity/); |
| 249 | + } |
| 250 | + }); |
| 251 | + |
| 252 | + it('rejects truncated gzip data even when sha512 matches its bytes', async () => { |
| 253 | + const truncated = Buffer.from([0x1f, 0x8b, 0x08, 0, 0, 0, 0, 0]); |
| 254 | + await expect( |
| 255 | + tarballAvailable('svforge@2.5.0', 'npm', { |
| 256 | + run: npmReturning(metadata(tarballUrl, integrityOf(truncated))), |
| 257 | + fetchImpl: fetchReturning(200, truncated) |
| 258 | + }) |
| 259 | + ).rejects.toThrow(/invalid gzip payload/); |
| 260 | + }); |
| 261 | + |
| 262 | + it('fails hard on a checksum mismatch instead of retrying a broken artifact', async () => { |
| 263 | + await expect( |
| 264 | + tarballAvailable('svforge@2.5.0', 'npm', { |
| 265 | + run: npmReturning(metadata(tarballUrl, integrityOf(Buffer.from('other')))), |
| 266 | + fetchImpl: fetchReturning(200) |
| 267 | + }) |
| 268 | + ).rejects.toThrow(/integrity mismatch/); |
| 269 | + }); |
| 270 | + |
| 271 | + it('fails hard on unexpected HTTP statuses, network errors and non-gzip bodies', async () => { |
| 272 | + await expect( |
| 273 | + tarballAvailable('svforge@2.5.0', 'npm', { run: npmReturning(metadata()), fetchImpl: fetchReturning(503) }) |
| 274 | + ).rejects.toThrow(/HTTP 503/); |
| 275 | + |
| 276 | + const networkError = (async () => { |
| 277 | + throw new Error('ECONNRESET'); |
| 278 | + }) as unknown as typeof fetch; |
| 279 | + await expect( |
| 280 | + tarballAvailable('svforge@2.5.0', 'npm', { run: npmReturning(metadata()), fetchImpl: networkError }) |
| 281 | + ).rejects.toThrow(/Tarball fetch failed .*ECONNRESET/); |
| 282 | + |
| 283 | + await expect( |
| 284 | + tarballAvailable('svforge@2.5.0', 'npm', { |
| 285 | + run: npmReturning(metadata()), |
| 286 | + fetchImpl: fetchReturning(200, Buffer.from('<!doctype html>')) |
| 287 | + }) |
| 288 | + ).rejects.toThrow(/not a gzip artifact/); |
| 289 | + }); |
| 290 | + |
| 291 | + it('refuses to guess a tarball URL from incomplete metadata', async () => { |
| 292 | + await expect( |
| 293 | + tarballAvailable('svforge@2.5.0', 'npm', { run: npmReturning('{}'), fetchImpl: fetchReturning(200) }) |
| 294 | + ).rejects.toThrow(/no dist\.tarball/); |
| 295 | + }); |
| 296 | + |
| 297 | + it('waits for a tarball 404 to clear and then installs', async () => { |
| 298 | + let probes = 0; |
| 299 | + let waits = 0; |
| 300 | + await waitForRegistryAvailability({ packages: [{ name: 'svforge', version: '2.5.0' }] }, { |
| 301 | + maxAttempts: 4, |
| 302 | + intervalMs: 1, |
| 303 | + probe: async () => ++probes > 2, |
| 304 | + pause: async () => { |
| 305 | + waits++; |
| 306 | + } |
| 307 | + }); |
| 308 | + expect(probes).toBe(3); |
| 309 | + expect(waits).toBe(2); |
| 310 | + }); |
| 311 | + |
| 312 | + it('verifies metadata and bytes through the composed consumability probe', async () => { |
| 313 | + const fields: string[] = []; |
| 314 | + const run = ((_npm: string, args: string[]) => { |
| 315 | + fields.push(args[2]); |
| 316 | + return args[2] === 'version' ? '"2.5.0"' : JSON.stringify({ 'dist.tarball': tarballUrl, 'dist.integrity': integrityOf(gzipBody) }); |
| 317 | + }) as unknown as NonNullable<Parameters<typeof registryReleaseConsumable>[2]>['run']; |
| 318 | + const fetchImpl = (async () => new Response('missing', { status: 404 })) as unknown as typeof fetch; |
| 319 | + |
| 320 | + expect(await registryReleaseConsumable('svforge@2.5.0', 'npm', { run, fetchImpl })).toBe(false); |
| 321 | + expect(fields).toEqual(['version', 'dist.tarball']); |
| 322 | + }); |
| 323 | + }); |
184 | 324 | }); |
0 commit comments