@@ -39,6 +39,44 @@ export type S3FallbackProviderOptions = {
3939 keyPrefix ?: string ;
4040} ;
4141
42+ export type GCSLegacyClient = {
43+ bucket ( name : string ) : {
44+ file ( path : string ) : {
45+ exists ( ) : Promise < [ boolean ] > ;
46+ download ( ) : Promise < [ Uint8Array ] > ;
47+ save ( body : string , options : { contentType : string } ) : Promise < unknown > ;
48+ } ;
49+ } ;
50+ } ;
51+
52+ export type GCSGoogleApisClient = {
53+ objects : {
54+ get (
55+ params : {
56+ bucket : string ;
57+ object : string ;
58+ alt ?: string ;
59+ } ,
60+ options ?: {
61+ responseType ?: "arraybuffer" ;
62+ } ,
63+ ) : Promise < {
64+ data : unknown ;
65+ } > ;
66+ insert ( params : {
67+ bucket : string ;
68+ name : string ;
69+ uploadType : "media" ;
70+ media : {
71+ mimeType : string ;
72+ body : string ;
73+ } ;
74+ } ) : Promise < unknown > ;
75+ } ;
76+ } ;
77+
78+ export type GCSFallbackProviderClient = GCSLegacyClient | GCSGoogleApisClient ;
79+
4280export type GCSFallbackProviderOptions = {
4381 /**
4482 * Bucket where snapshots are stored.
@@ -48,19 +86,12 @@ export type GCSFallbackProviderOptions = {
4886 /**
4987 * Optional GCS client. A default client is created when omitted.
5088 *
51- * TODO(next major): Replace this legacy `bucket().file()` client shape with
52- * a simpler object-store interface that doesn't mirror the deprecated
53- * `@google-cloud/storage` API.
89+ * Accepts either a legacy `bucket().file()` client or a generated
90+ * `@googleapis/storage` client.
91+ *
92+ * TODO(next major): Replace this with a simpler object-store interface.
5493 */
55- client ?: {
56- bucket ( name : string ) : {
57- file ( path : string ) : {
58- exists ( ) : Promise < [ boolean ] > ;
59- download ( ) : Promise < [ Uint8Array ] > ;
60- save ( body : string , options : { contentType : string } ) : Promise < unknown > ;
61- } ;
62- } ;
63- } ;
94+ client ?: GCSFallbackProviderClient ;
6495
6596 /**
6697 * Prefix for generated per-environment keys.
@@ -70,8 +101,6 @@ export type GCSFallbackProviderOptions = {
70101 keyPrefix ?: string ;
71102} ;
72103
73- type LegacyGCSClient = NonNullable < GCSFallbackProviderOptions [ "client" ] > ;
74-
75104type GCSObjectStore = {
76105 exists ( bucket : string , objectPath : string ) : Promise < boolean > ;
77106 download ( bucket : string , objectPath : string ) : Promise < Uint8Array > ;
@@ -227,15 +256,18 @@ async function createDefaultS3Client() {
227256 return new S3Client ( { } ) ;
228257}
229258
230- function createGCSObjectStore ( client : LegacyGCSClient ) : GCSObjectStore {
259+ function createLegacyGCSObjectStore ( client : GCSLegacyClient ) : GCSObjectStore {
231260 return {
232261 async exists ( bucket , objectPath ) {
233262 const [ exists ] = await client . bucket ( bucket ) . file ( objectPath ) . exists ( ) ;
234263 return exists ;
235264 } ,
236265
237266 async download ( bucket , objectPath ) {
238- const [ contents ] = await client . bucket ( bucket ) . file ( objectPath ) . download ( ) ;
267+ const [ contents ] = await client
268+ . bucket ( bucket )
269+ . file ( objectPath )
270+ . download ( ) ;
239271 return contents ;
240272 } ,
241273
@@ -245,19 +277,13 @@ function createGCSObjectStore(client: LegacyGCSClient): GCSObjectStore {
245277 } ;
246278}
247279
248- async function createDefaultGCSObjectStore ( ) : Promise < GCSObjectStore > {
249- const { auth, storage } = await import ( "@googleapis/storage" ) ;
250- const gcs = storage ( {
251- version : "v1" ,
252- auth : new auth . GoogleAuth ( {
253- scopes : [ "https://www.googleapis.com/auth/devstorage.read_write" ] ,
254- } ) ,
255- } ) ;
256-
280+ function createGoogleApisGCSObjectStore (
281+ client : GCSGoogleApisClient ,
282+ ) : GCSObjectStore {
257283 return {
258284 async exists ( bucket , objectPath ) {
259285 try {
260- await gcs . objects . get ( {
286+ await client . objects . get ( {
261287 bucket,
262288 object : objectPath ,
263289 } ) ;
@@ -271,7 +297,7 @@ async function createDefaultGCSObjectStore(): Promise<GCSObjectStore> {
271297 } ,
272298
273299 async download ( bucket , objectPath ) {
274- const response = await gcs . objects . get (
300+ const response = await client . objects . get (
275301 {
276302 bucket,
277303 object : objectPath ,
@@ -293,7 +319,7 @@ async function createDefaultGCSObjectStore(): Promise<GCSObjectStore> {
293319 } ,
294320
295321 async save ( bucket , objectPath , body , options ) {
296- await gcs . objects . insert ( {
322+ await client . objects . insert ( {
297323 bucket,
298324 name : objectPath ,
299325 uploadType : "media" ,
@@ -306,6 +332,39 @@ async function createDefaultGCSObjectStore(): Promise<GCSObjectStore> {
306332 } ;
307333}
308334
335+ function isGoogleApisGCSClient (
336+ client : GCSFallbackProviderClient ,
337+ ) : client is GCSGoogleApisClient {
338+ return (
339+ "objects" in client &&
340+ isObject ( client . objects ) &&
341+ typeof client . objects . get === "function" &&
342+ typeof client . objects . insert === "function"
343+ ) ;
344+ }
345+
346+ function createGCSObjectStore (
347+ client : GCSFallbackProviderClient ,
348+ ) : GCSObjectStore {
349+ if ( isGoogleApisGCSClient ( client ) ) {
350+ return createGoogleApisGCSObjectStore ( client ) ;
351+ }
352+
353+ return createLegacyGCSObjectStore ( client ) ;
354+ }
355+
356+ async function createDefaultGCSObjectStore ( ) : Promise < GCSObjectStore > {
357+ const { auth, storage } = await import ( "@googleapis/storage" ) ;
358+ return createGoogleApisGCSObjectStore (
359+ storage ( {
360+ version : "v1" ,
361+ auth : new auth . GoogleAuth ( {
362+ scopes : [ "https://www.googleapis.com/auth/devstorage.read_write" ] ,
363+ } ) ,
364+ } ) ,
365+ ) ;
366+ }
367+
309368export function createStaticFallbackProvider ( {
310369 flags,
311370} : StaticFallbackProviderOptions ) : FlagsFallbackProvider {
0 commit comments