1414 * limitations under the License.
1515 */
1616
17- import https , { RequestOptions } from 'https' ;
18- import { URL } from 'url' ;
17+ import { RequestOptions } from 'https' ;
1918import { randomBytes } from 'crypto' ;
2019import fs from 'fs' ;
2120import * as path from 'path' ;
2221import { tmpdir } from 'os' ;
23- import { errorMessage , removeFile , zipDir } from './util' ;
22+
2423import {
2524 CredentialBody ,
2625 ExternalAccountClientOptions ,
2726 GoogleAuth ,
2827} from 'google-auth-library' ;
28+ import {
29+ errorMessage ,
30+ request ,
31+ removeFile ,
32+ } from '@google-github-actions/actions-utils' ;
33+
34+ import { zipDir } from './util' ;
2935
3036// Do not listen to the linter - this can NOT be rewritten as an ES6 import statement.
3137// eslint-disable-next-line @typescript-eslint/no-var-requires
3238const { version : appVersion } = require ( '../package.json' ) ;
3339
40+ // userAgent is the default user agent.
41+ const userAgent = `google-github-actions:deploy-cloud-functions/${ appVersion } ` ;
42+
3443// defaultBaseURL is the URL for Cloud Functions.
3544const defaultBaseURL = 'https://cloudfunctions.googleapis.com/v1' ;
3645
@@ -182,57 +191,20 @@ export class CloudFunctionsClient {
182191 * request is a high-level helper that returns a promise from the executed
183192 * request.
184193 */
185- // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
186- static request ( opts : RequestOptions , data ?: any ) : Promise < string > {
187- if ( ! opts . headers ) {
188- opts . headers = { } ;
189- }
190-
191- if ( ! opts . headers [ 'User-Agent' ] ) {
192- opts . headers [
193- 'User-Agent'
194- ] = `google-github-actions:deploy-cloud-functions/${ appVersion } ` ;
195- }
196-
197- return new Promise ( ( resolve , reject ) => {
198- const req = https . request ( opts , ( res ) => {
199- res . setEncoding ( 'utf8' ) ;
200-
201- let body = '' ;
202- res . on ( 'data' , ( data ) => {
203- body += data ;
204- } ) ;
205-
206- res . on ( 'end' , ( ) => {
207- if ( res . statusCode && res . statusCode >= 400 ) {
208- reject ( body ) ;
209- } else {
210- resolve ( body ) ;
211- }
212- } ) ;
213- } ) ;
214-
215- req . on ( 'error' , ( err ) => {
216- reject ( err ) ;
217- } ) ;
218-
219- if ( data == null ) {
220- req . end ( ) ;
221- return ;
222- }
223-
224- if (
225- typeof data === 'string' ||
226- data instanceof String ||
227- data instanceof Buffer
228- ) {
229- req . write ( data ) ;
230- req . end ( ) ;
231- return ;
232- }
233-
234- data . pipe ( req ) ;
235- } ) ;
194+ static async request (
195+ method : string ,
196+ url : string ,
197+ data ?: any , // eslint-disable-line @typescript-eslint/no-explicit-any
198+ opts ?: RequestOptions ,
199+ ) : Promise < string > {
200+ opts ||= { } ;
201+ opts . headers = Object . assign (
202+ {
203+ 'User-Agent' : userAgent ,
204+ } ,
205+ opts . headers ,
206+ ) ;
207+ return await request ( method , url , data , opts ) ;
236208 }
237209
238210 /**
@@ -272,23 +244,15 @@ export class CloudFunctionsClient {
272244 async #request(
273245 method : string ,
274246 url : string ,
275- opts ?: RequestOptions ,
276247 data ?: any , // eslint-disable-line @typescript-eslint/no-explicit-any
248+ opts ?: RequestOptions ,
277249 ) {
278- const u = new URL ( url ) ;
279- opts = Object . assign ( opts || { } , {
280- hostname : u . hostname ,
281- port : u . port ,
282- path : u . pathname + u . search ,
283- method : method ,
284- headers : { } ,
285- } ) ;
286-
287250 const authToken = await this . #auth. getAccessToken ( ) ;
288251 if ( ! authToken ) {
289252 throw new Error ( `Failed to get auth token for ${ method } ${ url } ` ) ;
290253 }
291254
255+ opts ||= { } ;
292256 opts . headers = Object . assign (
293257 {
294258 'Authorization' : `Bearer ${ authToken } ` ,
@@ -299,10 +263,11 @@ export class CloudFunctionsClient {
299263 ) ;
300264
301265 try {
302- const resp = await CloudFunctionsClient . request ( opts , data ) ;
266+ const resp = await request ( method , url , data , opts ) ;
303267 return JSON . parse ( resp ) ;
304268 } catch ( err ) {
305- throw new Error ( `Failed to ${ method } ${ url } : ${ errorMessage ( err ) } ` ) ;
269+ const msg = errorMessage ( err ) ;
270+ throw new Error ( `Failed to ${ method } ${ url } : ${ msg } ` ) ;
306271 }
307272 }
308273
@@ -368,7 +333,7 @@ export class CloudFunctionsClient {
368333 const parent = this . parentFromName ( resourceName ) ;
369334 const u = `${ this . #baseURL} /${ parent } /functions` ;
370335 const body = JSON . stringify ( cf ) ;
371- const resp : Operation = await this . #request( 'POST' , u , { } , body ) ;
336+ const resp : Operation = await this . #request( 'POST' , u , body ) ;
372337 const op = await this . #pollOperation( resp . name , {
373338 interval : 5 ,
374339 retries : timeout / 5 ,
@@ -460,7 +425,7 @@ export class CloudFunctionsClient {
460425
461426 const u = `${ this . #baseURL} /${ resourceName } ?updateMask=${ updateMasks } ` ;
462427 const body = JSON . stringify ( cf ) ;
463- const resp : Operation = await this . #request( 'PATCH' , u , { } , body ) ;
428+ const resp : Operation = await this . #request( 'PATCH' , u , body ) ;
464429 const op = await this . #pollOperation( resp . name , {
465430 interval : 5 ,
466431 retries : timeout / 5 ,
@@ -554,19 +519,17 @@ export class CloudFunctionsClient {
554519 async uploadSource ( uploadURL : string , zipPath : string ) : Promise < void > {
555520 const zipFile = fs . createReadStream ( zipPath ) ;
556521
557- const u = new URL ( uploadURL ) ;
558- const opts = {
559- hostname : u . hostname ,
560- port : u . port ,
561- path : u . pathname + u . search ,
562- method : 'PUT' ,
563- headers : {
564- 'content-type' : 'application/zip' ,
565- 'x-goog-content-length-range' : '0,104857600' ,
566- } ,
567- } ;
568-
569- await CloudFunctionsClient . request ( opts , zipFile ) ;
522+ try {
523+ await CloudFunctionsClient . request ( 'PUT' , uploadURL , zipFile , {
524+ headers : {
525+ 'content-type' : 'application/zip' ,
526+ 'x-goog-content-length-range' : '0,104857600' ,
527+ } ,
528+ } ) ;
529+ } catch ( err ) {
530+ const msg = errorMessage ( err ) ;
531+ throw new Error ( `Failed to upload source: ${ msg } ` ) ;
532+ }
570533 }
571534
572535 fullResourceName ( name : string ) : string {
0 commit comments