77 */
88
99import { Bucket , Storage , File } from '@google-cloud/storage' ;
10- import { Browser , ArchiveType } from './browser' ;
10+ import { Browser } from './browser' ;
1111import { Platform } from './platform' ;
1212import { createTmpDir , downloadFileThroughStreaming } from './utils' ;
1313
1414import * as path from 'path' ;
15+ import { ArtifactType , BrowserArtifact } from './browser-artifact' ;
1516
1617/** Name of the Google Cloud Storage bucket for the browser mirror. */
1718const MIRROR_BUCKET_NAME = 'dev-infra-mirror' ;
@@ -21,17 +22,25 @@ export function getMirrorDirectoryForBrowserInstance<T>(browser: Browser<T>): st
2122 return `${ browser . name } /${ browser . revision } ` ;
2223}
2324
24- /** Uploads a browser platform artifact to the browser mirror. */
25+ /** Gets the destination file path for a given browser artifact. */
26+ export function getDestinationFilePath ( artifact : BrowserArtifact , platform : Platform ) : string {
27+ const versionMirrorDir = getMirrorDirectoryForBrowserInstance ( artifact . browser ) ;
28+ return `${ versionMirrorDir } /${ platform } /${ artifact . type } .${ artifact . extension } ` ;
29+ }
30+
31+ /**
32+ * Uploads a browser platform artifact to the browser mirror.
33+ *
34+ * @throws {Error } An error if the artifact already exists in the mirror.
35+ */
2536export async function uploadArtifactToMirror (
2637 bucket : Bucket ,
27- browser : Browser < unknown > ,
38+ artifact : BrowserArtifact ,
2839 platform : Platform ,
29- type : ArchiveType ,
3040 sourceFile : string ,
3141) : Promise < File > {
32- const versionMirrorDir = getMirrorDirectoryForBrowserInstance ( browser ) ;
3342 const [ file ] = await bucket . upload ( sourceFile , {
34- destination : ` ${ versionMirrorDir } / ${ platform } / ${ type } .zip` ,
43+ destination : getDestinationFilePath ( artifact , platform ) ,
3544 public : true ,
3645 } ) ;
3746
@@ -45,62 +54,67 @@ export async function uploadArtifactToMirror(
4554 */
4655export async function uploadBrowserArtifactsToMirror ( storage : Storage , browser : Browser < unknown > ) {
4756 const bucket = storage . bucket ( MIRROR_BUCKET_NAME ) ;
48- const versionMirrorDir = getMirrorDirectoryForBrowserInstance ( browser ) ;
49-
50- // Note that the `File#exists` method returns the following: `[boolean]`.
51- // https://googleapis.dev/nodejs/storage/latest/global.html#FileExistsResponse.
52- if ( ( await bucket . file ( versionMirrorDir ) . exists ( ) ) [ 0 ] ) {
53- throw Error ( 'Revision is already in the mirror. Remove the artifacts if you want to retry.' ) ;
54- }
55-
5657 const tmpDir = await createTmpDir ( { template : `${ browser . name } -${ browser . revision } -XXXXXX` } ) ;
5758 const downloadTasks : Promise < {
5859 platform : Platform ;
5960 filePath : string ;
60- type : ArchiveType ;
61+ artifact : BrowserArtifact ;
6162 } > [ ] = [ ] ;
6263
6364 for ( const platform of Object . values ( Platform ) ) {
64- const driverArchiveUrl = browser . getDownloadUrl ( platform , 'driver-bin' ) ;
65- const browserArchiveUrl = browser . getDownloadUrl ( platform , 'browser-bin' ) ;
66- const driverTmpPath = path . join ( tmpDir , 'driver.bin' ) ;
67- const browserTmpPath = path . join ( tmpDir , 'browser.bin' ) ;
65+ if ( ! browser . supports ( platform ) ) {
66+ continue ;
67+ }
68+
69+ const driverArtifact = browser . getArtifact ( platform , 'driver-bin' ) ;
70+ const browserArtifact = browser . getArtifact ( platform , 'browser-bin' ) ;
71+ const driverTmpPath = path . join ( tmpDir , `${ platform } -driver.${ driverArtifact . extension } ` ) ;
72+ const browserTmpPath = path . join ( tmpDir , `${ platform } -browser.${ browserArtifact . extension } ` ) ;
73+
74+ // We use the driver artifact (which is usually much smaller) to run a quick
75+ // sanity check upstream to ensure that the artifact does not yet exist upstream.
76+ const testDestinationFile = getDestinationFilePath ( driverArtifact , platform ) ;
77+ // Note that we cannot check directly for the directory to exist since GCP does
78+ // not support this. Hence we need to run this check for the actual files instead.
79+ if ( ( await bucket . file ( testDestinationFile ) . exists ( ) ) [ 0 ] ) {
80+ throw Error ( 'Revision is already in the mirror. Remove the artifacts if you want to retry.' ) ;
81+ }
6882
6983 downloadTasks . push (
70- downloadFileThroughStreaming ( browserArchiveUrl , browserTmpPath )
84+ downloadFileThroughStreaming ( browserArtifact . downloadUrl , browserTmpPath )
7185 . then ( ( ) => console . info ( `✅ Downloaded: ${ browser . name } - ${ platform } browser.` ) )
7286 . then ( ( ) => ( {
7387 platform,
7488 filePath : browserTmpPath ,
75- type : 'browser-bin' ,
89+ artifact : browserArtifact ,
7690 } ) ) ,
7791 ) ;
7892
7993 downloadTasks . push (
80- downloadFileThroughStreaming ( driverArchiveUrl , driverTmpPath )
94+ downloadFileThroughStreaming ( driverArtifact . downloadUrl , driverTmpPath )
8195 . then ( ( ) => console . info ( `✅ Downloaded: ${ browser . name } - ${ platform } driver.` ) )
8296 . then ( ( ) => ( {
8397 platform,
8498 filePath : driverTmpPath ,
85- type : 'driver-bin' ,
99+ artifact : driverArtifact ,
86100 } ) ) ,
87101 ) ;
88102 }
89103
90104 const tasks = await Promise . all ( downloadTasks ) ;
91- const uploadTasks : Promise < { platform : Platform ; type : ArchiveType ; file : File } > [ ] = [ ] ;
105+ const uploadTasks : Promise < { platform : Platform ; artifact : BrowserArtifact ; file : File } > [ ] = [ ] ;
92106
93107 console . info ( ) ;
94108 console . info ( 'Fetched all browser artifacts. Now uploading to mirror.' ) ;
95109 console . info ( ) ;
96110
97- for ( const { platform, filePath, type } of tasks ) {
111+ for ( const { platform, filePath, artifact } of tasks ) {
98112 uploadTasks . push (
99- uploadArtifactToMirror ( bucket , browser , platform , type , filePath ) . then ( ( file ) => {
100- console . log ( `✅ Uploaded: ${ platform } ${ type } ` ) ;
113+ uploadArtifactToMirror ( bucket , artifact , platform , filePath ) . then ( ( file ) => {
114+ console . log ( `✅ Uploaded: ${ platform } ${ artifact . type } ` ) ;
101115 console . log ( ` -> ${ file . publicUrl ( ) } ` ) ;
102116
103- return { platform, file, type } ;
117+ return { platform, file, artifact } ;
104118 } ) ,
105119 ) ;
106120 }
0 commit comments