Skip to content
Source
Sign inSign up
samwillis/neon-durable-streams
Open History to see the last commit

neon-test-rolling-artifacts.mjs

76 lines5.1 KB

1import { createHash } from 'node:crypto'
2import { execFile } from 'node:child_process'
3import { cp, mkdir, mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises'
4import { tmpdir } from 'node:os'
5import { dirname, join, resolve } from 'node:path'
6import { promisify } from 'node:util'
7​
8// Build actual packed consumers, without cloud credentials in build processes.
9// Optional archived revision for research comparisons; CI builds only current.
10const baseline = process.argv[2]
11if (baseline !== undefined && !/^[a-f0-9]{40}$/.test(baseline)) throw new Error('Expected a full archived commit SHA')
12const stateFile = '.neon-test-state-rolling-artifacts.json'
13const root = resolve(import.meta.dirname, '..')
14const run = promisify(execFile)
15const buildEnv = Object.fromEntries(Object.entries(process.env).filter(([key]) => !/SECRET|TOKEN|PASSWORD|KEY|DATABASE|^NEON_|^AWS_|^TEST_|^DURABLE_|^NODE_OPTIONS$/.test(key)))
16buildEnv.GIT_TERMINAL_PROMPT = '0'
17const digest = (bytes) => createHash('sha256').update(bytes).digest('hex')
18const sourcePaths = ['src', 'migrations', 'package.json', 'pnpm-lock.yaml', 'tsconfig.json', 'tsconfig.build.json', 'README.md', 'NOTICE']
19const candidatePaths = [...sourcePaths, 'LICENSE', 'scripts/build.mjs']
20let baselinePaths = sourcePaths
21const directory = await mkdtemp(join(tmpdir(), 'durable-streams-rolling-'))
22try { await writeFile(stateFile, JSON.stringify({ directory }), { mode: 0o600, flag: 'wx' }) }
23catch (error) { await rm(directory, { recursive: true, force: true }); throw error }
24async function command(cmd, args, cwd = root) {
25 return (await run(cmd, args, { cwd, env: buildEnv, timeout: 240_000, maxBuffer: 8 * 1024 * 1024 })).stdout
27const prior = join(directory, 'baseline-source'), archive = join(directory, 'baseline.tar')
28if (baseline) {
29 try { await command('git', ['cat-file', '-e', `${baseline}^{commit}`]) }
30 catch { await command('git', ['fetch', '--no-tags', '--depth=1', 'https://github.com/samwillis/neon-durable-streams.git', baseline]) }
31 try { await command('git', ['cat-file', '-e', `${baseline}:scripts/build.mjs`]); baselinePaths = [...sourcePaths, 'scripts/build.mjs'] } catch { /* Archived revisions may use a direct tsc build. */ }
32 await mkdir(prior)
33 await command('git', ['archive', '--format=tar', '--output', archive, baseline, ...baselinePaths])
34 await command('tar', ['-xf', archive, '-C', prior])
35 await command('pnpm', ['install', '--frozen-lockfile', '--prefer-offline', '--ignore-scripts'], prior)
36 await command('pnpm', ['build'], prior)
38const currentCommit = (await command('git', ['rev-parse', 'HEAD'])).trim()
39const currentDirty = Boolean((await command('git', ['status', '--porcelain', '--untracked-files=all', '--', ...candidatePaths])).trim())
40const candidate = join(directory, 'current-source')
41await mkdir(candidate)
42for (const path of candidatePaths) { await mkdir(dirname(join(candidate, path)), { recursive: true }); await cp(join(root, path), join(candidate, path), { recursive: true }) }
43await command('pnpm', ['install', '--frozen-lockfile', '--prefer-offline', '--ignore-scripts'], candidate)
44await command('pnpm', ['build'], candidate)
45async function sourceDigest(source) {
46 const hash = createHash('sha256')
47 async function include(path) {
48 const absolute = join(source, path)
49 let entries
50 try { entries = await readdir(absolute) } catch (error) { if (error.code !== 'ENOTDIR') throw error }
51 if (entries) { for (const child of entries.sort()) await include(join(path, child)); return }
52 hash.update(path); hash.update('\0'); hash.update(await readFile(absolute)); hash.update('\0')
53 }
54 for (const path of source === candidate ? candidatePaths : baselinePaths) await include(path)
55 return hash.digest('hex')
57const artifacts = {}
58for (const [label, source, commit] of [...(baseline ? [['baseline', prior, baseline]] : []), ['current', candidate, currentCommit]]) {
59 const consumer = join(directory, label)
60 await mkdir(consumer)
61 await command('pnpm', ['pack', '--pack-destination', consumer], source)
62 const tarball = (await readdir(consumer)).find((path) => path.endsWith('.tgz'))
63 if (!tarball) throw new Error('Rolling fixture package was not produced')
64 await writeFile(join(consumer, 'package.json'), JSON.stringify({ private: true, type: 'module', dependencies: {
65 '@samwillis/durable-streams-neon': `file:./${tarball}`, pg: '8.23.1',
66 } }))
67 await command('pnpm', ['install', '--prefer-offline', '--ignore-scripts'], consumer)
68 artifacts[label] = { commit, sourceDirty: label === 'current' && currentDirty,
69 sourceSnapshotSha256: await sourceDigest(source),
70 packageRoot: join(consumer, 'node_modules/@samwillis/durable-streams-neon'), consumer,
71 tarballSha256: digest(await readFile(join(consumer, tarball))),
72 sourceLockSha256: digest(await readFile(join(source, 'pnpm-lock.yaml'))),
73 consumerLockSha256: digest(await readFile(join(consumer, 'pnpm-lock.yaml'))) }
75await writeFile(stateFile, JSON.stringify({ directory, ...(baseline ? { baselineSourceArchiveSha256: digest(await readFile(archive)) } : {}), artifacts }), { mode: 0o600 })
76console.log('Prepared independently installed packed package consumer(s).')