Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

release-artifacts.test.mjs

244 lines7.1 KB

1import assert from 'node:assert/strict'
2import { createHash } from 'node:crypto'
3import { execFileSync } from 'node:child_process'
4import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'
5import { tmpdir } from 'node:os'
6import { join } from 'node:path'
7import { test } from 'node:test'
8import { verifyReleaseArtifacts } from './verify-release-artifacts.mjs'
9​
10const pkg = { name: '@test/package', version: '1.0.0' }
11const registryURL = 'https://registry.example'
12const tarball = `${registryURL}/package.tgz`
13​
14async function archive(t, packed = pkg) {
15 const dir = await mkdtemp(join(tmpdir(), 'release-test-'))
16 t.after(() => rm(dir, { recursive: true, force: true }))
17 await mkdir(join(dir, 'package'))
18 await writeFile(join(dir, 'package/package.json'), JSON.stringify(packed))
19 const path = join(dir, 'package.tgz')
20 execFileSync('tar', ['-czf', path, '-C', dir, 'package/package.json'])
21 const bytes = await readFile(path)
22 return {
23 bytes,
24 integrity: `sha512-${createHash('sha512').update(bytes).digest('base64')}`,
25 }
27​
28function options(fetchImpl) {
29 let time = 0
30 const sleeps = []
31 return {
32 registryURL,
33 fetchImpl,
34 timeoutMs: 100,
35 pollMs: 10,
36 log: () => {},
37 now: () => time,
38 sleep: async (ms) => {
39 sleeps.push(ms)
40 time += ms
41 },
42 sleeps,
43 }
45​
46test('waits for metadata and the actual tarball, then verifies downloaded bytes', async (t) => {
47 const fixture = await archive(t)
48 let metadataReads = 0
49 let tarballReads = 0
50 const opts = options(async (url) => {
51 if (String(url) === tarball) {
52 tarballReads++
53 return tarballReads === 1
54 ? new Response('', { status: 404 })
55 : new Response(fixture.bytes)
56 }
57 metadataReads++
58 return metadataReads === 1
59 ? new Response('', { status: 404 })
60 : Response.json({
61 ...pkg,
62 dist: { tarball, integrity: fixture.integrity },
63 })
64 })
65 const verified = await verifyReleaseArtifacts([pkg], opts)
66 assert.equal(verified[0].integrity, fixture.integrity)
67 assert.equal(verified[0].bytes, fixture.bytes.length)
68 assert.equal(metadataReads, 3)
69 assert.equal(tarballReads, 2)
70 assert.deepEqual(opts.sleeps, [10, 10])
71})
72​
73test('metadata alone cannot complete a release and timeout names the missing package', async (t) => {
74 const fixture = await archive(t)
75 const opts = options(async (url) =>
76 String(url) === tarball
77 ? new Response('', { status: 404 })
78 : Response.json({
79 ...pkg,
80 dist: { tarball, integrity: fixture.integrity },
81 }),
82 )
83 await assert.rejects(
84 verifyReleaseArtifacts([pkg], opts),
85 /Release is not ready: @test\/package@1.0.0/,
86 )
87})
88​
89test('rejects corrupt tarballs instead of retrying them as unavailable', async (t) => {
90 const fixture = await archive(t)
91 const opts = options(async (url) =>
92 String(url) === tarball
93 ? new Response('corrupt')
94 : Response.json({
95 ...pkg,
96 dist: { tarball, integrity: fixture.integrity },
97 }),
98 )
99 await assert.rejects(
100 verifyReleaseArtifacts([pkg], opts),
101 /integrity mismatch/,
102 )
103 assert.deepEqual(opts.sleeps, [])
104})
105​
106test('rejects a validly hashed tarball containing the wrong package version', async (t) => {
107 const fixture = await archive(t, { ...pkg, version: '0.9.0' })
108 const opts = options(async (url) =>
109 String(url) === tarball
110 ? new Response(fixture.bytes)
111 : Response.json({
112 ...pkg,
113 dist: { tarball, integrity: fixture.integrity },
114 }),
115 )
116 await assert.rejects(
117 verifyReleaseArtifacts([pkg], opts),
118 /Packed identity mismatch/,
119 )
120})
121​
122test('honors rate limits without redownloading packages already verified', async (t) => {
123 const fixture = await archive(t)
124 const other = { name: '@test/other', version: '2.0.0' }
125 const otherFixture = await archive(t, other)
126 let firstDownloads = 0
127 let otherReads = 0
128 const opts = options(async (url) => {
129 if (String(url) === tarball) {
130 firstDownloads++
131 return new Response(fixture.bytes)
132 }
133 if (String(url) === `${registryURL}/other.tgz`)
134 return new Response(otherFixture.bytes)
135 if (String(url).includes('other')) {
136 otherReads++
137 if (otherReads === 1)
138 return new Response('', {
139 status: 429,
140 headers: { 'retry-after': '2' },
141 })
142 return Response.json({
143 ...other,
144 dist: {
145 tarball: `${registryURL}/other.tgz`,
146 integrity: otherFixture.integrity,
147 },
148 })
149 }
150 return Response.json({
151 ...pkg,
152 dist: { tarball, integrity: fixture.integrity },
153 })
154 })
155 opts.timeoutMs = 5000
156 assert.equal((await verifyReleaseArtifacts([pkg, other], opts)).length, 2)
157 assert.equal(firstDownloads, 1)
158 assert.equal(otherReads, 2)
159 assert.deepEqual(opts.sleeps, [2000])
160})
161​
162test('retries interrupted downloads but fails authentication errors immediately', async (t) => {
163 const fixture = await archive(t)
164 let downloads = 0
165 const opts = options(async (url) => {
166 if (String(url) === tarball) {
167 downloads++
168 if (downloads === 1)
169 return new Response(
170 new ReadableStream({
171 start(controller) {
172 controller.error(new TypeError('connection interrupted'))
173 },
174 }),
175 )
176 return new Response(fixture.bytes)
177 }
178 return Response.json({
179 ...pkg,
180 dist: { tarball, integrity: fixture.integrity },
181 })
182 })
183 assert.equal((await verifyReleaseArtifacts([pkg], opts)).length, 1)
184 assert.equal(downloads, 2)
185 const denied = options(async () => new Response('', { status: 401 }))
186 await assert.rejects(verifyReleaseArtifacts([pkg], denied), /HTTP 401/)
187 assert.deepEqual(denied.sleeps, [])
188})
189​
190test('rejects missing package output, unexpected registry identity and external tarball origins', async () => {
191 await assert.rejects(
192 verifyReleaseArtifacts(
193 [],
194 options(() => {}),
195 ),
196 /Expected published/,
197 )
198 await assert.rejects(
199 verifyReleaseArtifacts(
200 [pkg],
201 options(async () => Response.json({ ...pkg, version: 'wrong' })),
202 ),
203 /Registry identity/,
204 )
205 await assert.rejects(
206 verifyReleaseArtifacts(
207 [pkg],
208 options(async () =>
209 Response.json({
210 ...pkg,
211 dist: {
212 tarball: 'https://other.example/file.tgz',
213 integrity: `sha512-${'A'.repeat(86)}==`,
214 },
215 }),
216 ),
217 ),
218 /Unexpected tarball origin/,
219 )
220})
221​
222test('retries a metadata response whose body was interrupted', async (t) => {
223 const fixture = await archive(t)
224 let reads = 0
225 const opts = options(async (url) => {
226 if (String(url) === tarball) return new Response(fixture.bytes)
227 reads++
228 if (reads === 1)
229 return new Response(
230 new ReadableStream({
231 start(controller) {
232 controller.error(new TypeError('metadata connection interrupted'))
233 },
234 }),
235 )
236 return Response.json({
237 ...pkg,
238 dist: { tarball, integrity: fixture.integrity },
239 })
240 })
241 assert.equal((await verifyReleaseArtifacts([pkg], opts)).length, 1)
242 assert.equal(reads, 2)
243 assert.deepEqual(opts.sleeps, [10])
244})