/**
* Verify the latest release is reproducible
- * Works with versions 4.0.0-beta.2 and later
*/
-import chalk from "chalk";
-import * as Diff from "diff";
import { exec as nodeExec } from "node:child_process";
import crypto from "node:crypto";
import { createWriteStream } from "node:fs";
const CDN_URL = "https://code.jquery.com";
const REGISTRY_URL = "https://registry.npmjs.org/jquery";
-const rstable = /^(\d+\.\d+\.\d+)$/;
+const excludeFromCDN = [
+ /^package\.json$/,
+ /^jquery\.factory\./
+];
+
+const rjquery = /^jquery/;
async function verifyRelease( { version } = {} ) {
if ( !version ) {
console.log( `Verifying jQuery ${ version }...` );
let verified = true;
+ const matchingFiles = [];
+ const mismatchingFiles = [];
- // Only check stable versions against the CDN
- if ( rstable.test( version ) ) {
- await Promise.all(
- release.files.map( async( file ) => {
- const cdnContents = await fetch( new URL( file.name, CDN_URL ) ).then(
- ( res ) => res.text()
- );
- if ( cdnContents !== file.contents ) {
- console.log( `${ file.name } is different from the CDN:` );
- diffFiles( file.contents, cdnContents );
+ // Check all files against the CDN
+ await Promise.all(
+ release.files
+ .filter( ( file ) => excludeFromCDN.every( ( re ) => !re.test( file.name ) ) )
+ .map( async( file ) => {
+ const url = new URL( file.cdnName, CDN_URL );
+ const response = await fetch( url );
+ if ( !response.ok ) {
+ throw new Error(
+ `Failed to download ${
+ file.cdnName
+ } from the CDN: ${ response.statusText }`
+ );
+ }
+ const cdnContents = await response.text();
+ if ( cdnContents !== file.cdnContents ) {
+ mismatchingFiles.push( url.href );
verified = false;
+ } else {
+ matchingFiles.push( url.href );
}
} )
- );
- }
+ );
- // Check all releases against npm.
+ // Check all files against npm.
// First, download npm tarball for version
const npmPackage = await fetch( REGISTRY_URL ).then( ( res ) => res.json() );
// Check the tarball checksum
const tgzSum = await sumTarball( npmTarballPath );
if ( tgzSum !== release.tgz.contents ) {
- console.log( `${ version }.tgz is different from npm:` );
- diffFiles( release.tgz.contents, tgzSum );
+ mismatchingFiles.push( `npm:${ version }.tgz` );
verified = false;
+ } else {
+ matchingFiles.push( `npm:${ version }.tgz` );
}
await Promise.all(
);
if ( npmContents !== file.contents ) {
- console.log( `${ file.name } is different from the CDN:` );
- diffFiles( file.contents, npmContents );
+ mismatchingFiles.push( `npm:${ file.path }/${ file.name }` );
verified = false;
+ } else {
+ matchingFiles.push( `npm:${ file.path }/${ file.name }` );
}
} )
);
if ( verified ) {
- console.log( `jQuery ${ version } is reproducible!` );
+ console.log( `jQuery ${ version } is reproducible! All files match!` );
} else {
+ console.log();
+ for ( const file of matchingFiles ) {
+ console.log( `✅ ${ file }` );
+ }
+ console.log();
+ for ( const file of mismatchingFiles ) {
+ console.log( `❌ ${ file }` );
+ }
+
throw new Error( `jQuery ${ version } is NOT reproducible!` );
}
}
console.log( `Cloning jQuery ${ version }...` );
await rimraf( releaseFolder );
await mkdir( releaseFolder, { recursive: true } );
+
+ // Uses a depth of 2 so we can get the commit date of
+ // the commit used to build, which is the commit before the tag
await exec(
- `git clone -q -b ${ version } --depth=1 ${ SRC_REPO } ${ releaseFolder }`
+ `git clone -q -b ${ version } --depth=2 ${ SRC_REPO } ${ releaseFolder }`
);
// Install node dependencies
console.log( `Installing dependencies for jQuery ${ version }...` );
await exec( "npm ci", { cwd: releaseFolder } );
+ // Find the date of the commit just before the release,
+ // which was used as the date in the built files
+ const { stdout: date } = await exec( "git log -1 --format=%ci HEAD~1", {
+ cwd: releaseFolder
+ } );
+
// Build the release
console.log( `Building jQuery ${ version }...` );
const { stdout: buildOutput } = await exec( "npm run build:all", {
cwd: releaseFolder,
env: {
+
+ // Keep existing environment variables
+ ...process.env,
+ RELEASE_DATE: date,
VERSION: version
}
} );
console.log( packOutput );
// Get all top-level /dist and /dist-module files
- const distFiles = await readdir( path.join( releaseFolder, "dist" ), {
- withFileTypes: true
- } );
+ const distFiles = await readdir(
+ path.join( releaseFolder, "dist" ),
+ { withFileTypes: true }
+ );
const distModuleFiles = await readdir(
path.join( releaseFolder, "dist-module" ),
- {
- withFileTypes: true
- }
+ { withFileTypes: true }
);
const files = await Promise.all(
[ ...distFiles, ...distModuleFiles ]
.filter( ( dirent ) => dirent.isFile() )
- .map( async( dirent ) => ( {
- name: dirent.name,
- path: path.basename( dirent.parentPath ),
- contents: await readFile( path.join( dirent.parentPath, dirent.name ), "utf8" )
- } ) )
+ .map( async( dirent ) => {
+ const contents = await readFile(
+ path.join( dirent.parentPath, dirent.name ),
+ "utf8"
+ );
+ return {
+ name: dirent.name,
+ path: path.basename( dirent.parentPath ),
+ contents,
+ cdnName: dirent.name.replace( rjquery, `jquery-${ version }` ),
+ cdnContents: dirent.name.endsWith( ".map" ) ?
+
+ // The CDN has versioned filenames in the maps
+ convertMapToVersioned( contents, version ) :
+ contents
+ };
+ } )
);
// Get checksum of the tarball
return finished( stream );
}
-async function diffFiles( a, b ) {
- const diff = Diff.diffLines( a, b );
-
- diff.forEach( ( part ) => {
- if ( part.added ) {
- console.log( chalk.green( part.value ) );
- } else if ( part.removed ) {
- console.log( chalk.red( part.value ) );
- } else {
- console.log( part.value );
- }
- } );
-}
-
async function getLatestVersion() {
const { stdout: sha } = await exec( "git rev-list --tags --max-count=1" );
const { stdout: tag } = await exec( `git describe --tags ${ sha.trim() }` );
return shasum( unzipped );
}
+function convertMapToVersioned( contents, version ) {
+ const map = JSON.parse( contents );
+ return JSON.stringify( {
+ ...map,
+ file: map.file.replace( rjquery, `jquery-${ version }` ),
+ sources: map.sources.map( ( source ) => source.replace( rjquery, `jquery-${ version }` ) )
+ } );
+}
+
verifyRelease();