@@ -4,11 +4,13 @@ import {getRepoDetails, getReleaseDetails, createIssue, getOrgDetails} from "../
44import db from "../db.js" ;
55import { downloader } from "../utils/downloader.js" ;
66import { ZipUtils } from "../utils/zipUtils.js" ;
7- import { valid , lte } from "semver" ;
7+ import { valid , lte , clean } from "semver" ;
88import {
99 FIELD_RELEASE_ID , RELEASE_DETAILS_TABLE , EXTENSION_SIZE_LIMIT_MB , BASE_URL ,
10- EXTENSION_DOWNLOAD_DIR , PROCESSING_TIMEOUT_MS , EXTENSIONS_DETAILS_TABLE , FIELD_EXTENSION_ID
10+ EXTENSION_DOWNLOAD_DIR , PROCESSING_TIMEOUT_MS , EXTENSIONS_DETAILS_TABLE , FIELD_EXTENSION_ID , EXTENSIONS_BUCKET
1111} from "../constants.js" ;
12+ import fs from "fs" ;
13+ import { S3 } from "../s3.js" ;
1214
1315const RELEASE_STATUS_PROCESSING = "processing" ;
1416
@@ -156,15 +158,15 @@ async function _validateExtensionPackageJson(githubReleaseTag, packageJSON, repo
156158 const newOwner = `github:${ githubReleaseTag . owner } ` ;
157159 const releaseRef = `${ githubReleaseTag . owner } /${ githubReleaseTag . repo } /${ githubReleaseTag . tag } ` ;
158160 queryObj [ FIELD_EXTENSION_ID ] = packageJSON . name ;
159- let registryPKG = await db . getFromIndex ( EXTENSIONS_DETAILS_TABLE , queryObj ) ;
160- if ( ! registryPKG . isSuccess ) {
161+ let registryPKGJSON = await db . getFromIndex ( EXTENSIONS_DETAILS_TABLE , queryObj ) ;
162+ if ( ! registryPKGJSON . isSuccess ) {
161163 // unexpected error
162164 throw new Error ( "Error getting extensionPKG details from db: " + releaseRef ) ;
163165 }
164- registryPKG = registryPKG . documents . length === 1 ? registryPKG . documents [ 0 ] : null ;
166+ registryPKGJSON = registryPKGJSON . documents . length === 1 ? registryPKGJSON . documents [ 0 ] : null ;
165167 let error = "" ;
166- if ( registryPKG && registryPKG . owner !== newOwner ) {
167- let errorMsg = `Extension of the same name "${ packageJSON . name } " already exists (owned by https://github.com/${ registryPKG . owner . split ( ":" ) [ 1 ] } ). Please choose a different extension name.` ;
168+ if ( registryPKGJSON && registryPKGJSON . owner !== newOwner ) {
169+ let errorMsg = `Extension of the same name "${ packageJSON . name } " already exists (owned by https://github.com/${ registryPKGJSON . owner . split ( ":" ) [ 1 ] } ). Please choose a different extension name.` ;
168170 error = error + errorMsg ;
169171 issueMessages . push ( errorMsg ) ;
170172 throw { status : HTTP_STATUS_CODES . BAD_REQUEST ,
@@ -176,17 +178,18 @@ async function _validateExtensionPackageJson(githubReleaseTag, packageJSON, repo
176178 error = error + `\n${ errorMsg } ` ;
177179 issueMessages . push ( errorMsg ) ;
178180 }
179- if ( registryPKG ) {
180- for ( let versionInfo of registryPKG . versions ) {
181+ packageJSON . version = clean ( packageJSON . version ) ; // ' =v1.2.3 ' -> '1.2.3'
182+ if ( registryPKGJSON ) {
183+ for ( let versionInfo of registryPKGJSON . versions ) {
181184 if ( versionInfo . version === packageJSON . version ) {
182- let errorMsg = `Package version "${ packageJSON . version } " already published on ${ versionInfo . published } . Please update version number to above ${ registryPKG . metadata . version } .` ;
185+ let errorMsg = `Package version "${ packageJSON . version } " already published on ${ versionInfo . published } . Please update version number to above ${ registryPKGJSON . metadata . version } .` ;
183186 error = error + `\n${ errorMsg } ` ;
184187 issueMessages . push ( errorMsg ) ;
185188 break ;
186189 }
187190 }
188- if ( lte ( packageJSON . version , registryPKG . metadata . version ) ) {
189- let errorMsg = `Package version should be greater than ${ registryPKG . metadata . version } , but received "${ packageJSON . version } ".` ;
191+ if ( lte ( packageJSON . version , registryPKGJSON . metadata . version ) ) {
192+ let errorMsg = `Package version should be greater than ${ registryPKGJSON . metadata . version } , but received "${ packageJSON . version } ".` ;
190193 error = error + `\n${ errorMsg } ` ;
191194 issueMessages . push ( errorMsg ) ;
192195 }
@@ -202,30 +205,30 @@ async function _validateExtensionPackageJson(githubReleaseTag, packageJSON, repo
202205 ownershipVerifiedByGitHub = [ org . blog ] ;
203206 }
204207 // now create the new registry package json
205- registryPKG = registryPKG || {
208+ registryPKGJSON = registryPKGJSON || {
206209 "versions" : [ ] ,
207210 "totalDownloads" : 0 ,
208211 "recent" : { }
209212 } ;
210- registryPKG . metadata = packageJSON ;
211- registryPKG . owner = `github:${ githubReleaseTag . owner } ` ;
212- registryPKG . gihubStars = repoDetails . stargazers_count ;
213- registryPKG . ownerRepo = `https://github.com/${ githubReleaseTag . owner } /${ githubReleaseTag . repo } ` ;
214- registryPKG . ownershipVerifiedByGitHub = ownershipVerifiedByGitHub ;
215- registryPKG . versions . push ( {
216- "version" : packageJSON . version ,
213+ registryPKGJSON . metadata = packageJSON ;
214+ registryPKGJSON . owner = `github:${ githubReleaseTag . owner } ` ;
215+ registryPKGJSON . gihubStars = repoDetails . stargazers_count ;
216+ registryPKGJSON . ownerRepo = `https://github.com/${ githubReleaseTag . owner } /${ githubReleaseTag . repo } ` ;
217+ registryPKGJSON . ownershipVerifiedByGitHub = ownershipVerifiedByGitHub ;
218+ registryPKGJSON . versions . push ( {
219+ "version" : clean ( packageJSON . version ) ,
217220 "published" : new Date ( ) . toISOString ( ) ,
218221 "brackets" : packageJSON . engines . brackets ,
219222 "downloads" : 0
220223 } ) ;
221224
222- console . log ( registryPKG ) ;
225+ return registryPKGJSON ;
223226}
224227
225228async function _downloadAndValidateExtensionZip ( githubReleaseTag , extensionZipAsset , repoDetails , issueMessages ) {
226- const targetPath = `${ EXTENSION_DOWNLOAD_DIR } /${ githubReleaseTag . owner } _${ githubReleaseTag . repo } _${ githubReleaseTag . tag } _${ extensionZipAsset . name } ` ;
227- await downloader . downloadFile ( extensionZipAsset . browser_download_url , targetPath ) ;
228- let { packageJSON, error} = await ZipUtils . getExtensionPackageJSON ( targetPath ) ;
229+ const extensionZipPath = `${ EXTENSION_DOWNLOAD_DIR } /${ githubReleaseTag . owner } _${ githubReleaseTag . repo } _${ githubReleaseTag . tag } _${ extensionZipAsset . name } ` ;
230+ await downloader . downloadFile ( extensionZipAsset . browser_download_url , extensionZipPath ) ;
231+ let { packageJSON, error} = await ZipUtils . getExtensionPackageJSON ( extensionZipPath ) ;
229232 if ( error ) {
230233 issueMessages . push ( error ) ;
231234 throw { status : HTTP_STATUS_CODES . BAD_REQUEST ,
@@ -249,8 +252,8 @@ async function _downloadAndValidateExtensionZip(githubReleaseTag, extensionZipAs
249252 updatePublishErrors : true ,
250253 error} ;
251254 }
252- await _validateExtensionPackageJson ( githubReleaseTag , packageJSON , repoDetails , issueMessages ) ;
253- return targetPath ;
255+ const registryPKGJSON = await _validateExtensionPackageJson ( githubReleaseTag , packageJSON , repoDetails , issueMessages ) ;
256+ return { extensionZipPath , registryPKGJSON } ;
254257}
255258
256259async function _createGithubIssue ( release ) {
@@ -340,9 +343,20 @@ export async function publishGithubRelease(request, reply) {
340343 error : `Draft or PreRelease builds cannot be published.` } ;
341344 }
342345 const extensionZipAsset = _validateGitHubReleaseAssets ( newGithubReleaseDetails , issueMessages ) ;
343- extensionZipPath = await _downloadAndValidateExtensionZip ( githubReleaseTag , extensionZipAsset , repoDetails , issueMessages ) ;
346+ const { extensionZipPath, registryPKGJSON} =
347+ await _downloadAndValidateExtensionZip ( githubReleaseTag , extensionZipAsset , repoDetails , issueMessages ) ;
344348 // we should also in the future do a virus scan, but will rely on av in users machine for the time being
345349 // https://developers.virustotal.com/reference/files-scan by Google Cloud is available for non-commercial apps.
350+
351+ await S3 . uploadFile ( EXTENSIONS_BUCKET ,
352+ `extensions/${ registryPKGJSON . metadata . name } -${ registryPKGJSON . metadata . version } .zip` ,
353+ extensionZipPath ) ;
354+
355+ // cleanup
356+ setTimeout ( ( ) => {
357+ fs . unlink ( extensionZipPath , console . error ) ; // cleanup after we return. (But we don't check the result)
358+ } , 1000 ) ;
359+
346360 const response = {
347361 message : "done"
348362 } ;
@@ -352,6 +366,9 @@ export async function publishGithubRelease(request, reply) {
352366 if ( err . updatePublishErrors ) {
353367 _updatePublishErrors ( githubReleaseTag , issueMessages ) ; // dont await, background task
354368 }
369+ if ( extensionZipPath ) {
370+ fs . unlink ( extensionZipPath , console . error ) ; // cleanup after we return. (But we don't check the result)
371+ }
355372 if ( err . status ) {
356373 reply . status ( err . status ) ;
357374 return err . error ;
0 commit comments