@@ -83,33 +83,6 @@ type GCSObjectStore = {
8383 ) : Promise < unknown > ;
8484} ;
8585
86- type GCSStorageControlReadResponse = {
87- checksummedData ?: {
88- content ?: string | Uint8Array | ArrayBuffer ;
89- } ;
90- } ;
91-
92- type GCSStorageControlStub = {
93- getObject ( request : { bucket : string ; object : string } ) : Promise < [ unknown ] > ;
94- readObject ( request : { bucket : string ; object : string } ) : {
95- on ( event : "data" , listener : ( response : GCSStorageControlReadResponse ) => void ) : unknown ;
96- on ( event : "error" , listener : ( error : unknown ) => void ) : unknown ;
97- on ( event : "end" , listener : ( ) => void ) : unknown ;
98- } ;
99- writeObject (
100- callback : ( error : unknown , response ?: unknown ) => void ,
101- ) : {
102- on ( event : "error" , listener : ( error : unknown ) => void ) : unknown ;
103- write ( chunk : unknown ) : unknown ;
104- end ( ) : unknown ;
105- } ;
106- } ;
107-
108- type GCSStorageControlClient = {
109- bucketPath ( project : string , bucket : string ) : string ;
110- initialize ( ) : Promise < GCSStorageControlStub > ;
111- } ;
112-
11386export type RedisFallbackProviderOptions = {
11487 /**
11588 * Optional Redis client. When omitted, a client is created using `REDIS_URL`.
@@ -221,7 +194,6 @@ function parseSnapshot(raw: string) {
221194
222195function isNotFoundError ( error : any ) {
223196 return (
224- error ?. code === 5 ||
225197 error ?. code === 404 ||
226198 error ?. status === 404 ||
227199 error ?. response ?. status === 404 ||
@@ -274,17 +246,19 @@ function createGCSObjectStore(client: LegacyGCSClient): GCSObjectStore {
274246}
275247
276248async function createDefaultGCSObjectStore ( ) : Promise < GCSObjectStore > {
277- const { v2 } = await import ( "@google-cloud/storage-control" ) ;
278- const gcs = new v2 . StorageClient ( ) as unknown as GCSStorageControlClient ;
279- const stub = await gcs . initialize ( ) ;
280-
281- const bucketResourceName = ( bucket : string ) => gcs . bucketPath ( "_" , bucket ) ;
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+ } ) ;
282256
283257 return {
284258 async exists ( bucket , path ) {
285259 try {
286- await stub . getObject ( {
287- bucket : bucketResourceName ( bucket ) ,
260+ await gcs . objects . get ( {
261+ bucket,
288262 object : path ,
289263 } ) ;
290264 return true ;
@@ -297,81 +271,36 @@ async function createDefaultGCSObjectStore(): Promise<GCSObjectStore> {
297271 } ,
298272
299273 async download ( bucket , path ) {
300- const stream = stub . readObject ( {
301- bucket : bucketResourceName ( bucket ) ,
302- object : path ,
303- } ) ;
274+ const response = await gcs . objects . get (
275+ {
276+ bucket,
277+ object : path ,
278+ alt : "media" ,
279+ } ,
280+ {
281+ responseType : "arraybuffer" ,
282+ } ,
283+ ) ;
304284
305- return await new Promise < Uint8Array > ( ( resolve , reject ) => {
306- const chunks : Uint8Array [ ] = [ ] ;
307- let settled = false ;
308-
309- const fail = ( error : unknown ) => {
310- if ( settled ) return ;
311- settled = true ;
312- reject ( error ) ;
313- } ;
314-
315- stream . on ( "data" , ( response ) => {
316- const content = response ?. checksummedData ?. content ;
317- if ( content === undefined || content === null ) {
318- return ;
319- }
320-
321- if ( typeof content === "string" ) {
322- chunks . push ( Buffer . from ( content , "utf-8" ) ) ;
323- return ;
324- }
325- if ( content instanceof Uint8Array ) {
326- chunks . push ( content ) ;
327- return ;
328- }
329- if ( content instanceof ArrayBuffer ) {
330- chunks . push ( new Uint8Array ( content ) ) ;
331- return ;
332- }
333-
334- fail ( new TypeError ( "Unexpected GCS download response body format" ) ) ;
335- } ) ;
285+ if ( response . data instanceof Uint8Array ) {
286+ return response . data ;
287+ }
288+ if ( response . data instanceof ArrayBuffer ) {
289+ return new Uint8Array ( response . data ) ;
290+ }
336291
337- stream . on ( "error" , fail ) ;
338- stream . on ( "end" , ( ) => {
339- if ( settled ) return ;
340- settled = true ;
341- resolve ( Buffer . concat ( chunks . map ( ( chunk ) => Buffer . from ( chunk ) ) ) ) ;
342- } ) ;
343- } ) ;
292+ throw new TypeError ( "Unexpected GCS download response body format" ) ;
344293 } ,
345294
346295 async save ( bucket , path , body , options ) {
347- const content = Buffer . from ( body , "utf-8" ) ;
348-
349- await new Promise < void > ( ( resolve , reject ) => {
350- const stream = stub . writeObject ( ( error ) => {
351- if ( error ) {
352- reject ( error ) ;
353- return ;
354- }
355- resolve ( ) ;
356- } ) ;
357-
358- stream . on ( "error" , reject ) ;
359- stream . write ( {
360- writeObjectSpec : {
361- resource : {
362- bucket : bucketResourceName ( bucket ) ,
363- name : path ,
364- contentType : options . contentType ,
365- } ,
366- objectSize : content . byteLength ,
367- } ,
368- writeOffset : 0 ,
369- checksummedData : {
370- content,
371- } ,
372- finishWrite : true ,
373- } ) ;
374- stream . end ( ) ;
296+ await gcs . objects . insert ( {
297+ bucket,
298+ name : path ,
299+ uploadType : "media" ,
300+ media : {
301+ mimeType : options . contentType ,
302+ body,
303+ } ,
375304 } ) ;
376305 } ,
377306 } ;
0 commit comments