Skip to content
This repository was archived by the owner on Jan 16, 2024. It is now read-only.

Commit 29b4bcd

Browse files
author
olinde
committed
feat: make gcs adapter initialize lazily
1 parent d9dd60f commit 29b4bcd

5 files changed

Lines changed: 28 additions & 17 deletions

File tree

src/adapters/gcs/generateURL.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { Storage } from '@google-cloud/storage'
33
import type { GenerateURL } from '../../types'
44

55
interface Args {
6-
gcs: Storage
6+
getStorageClient: () => Storage
77
bucket: string
88
}
99

1010
export const getGenerateURL =
11-
({ gcs, bucket }: Args): GenerateURL =>
11+
({ getStorageClient, bucket }: Args): GenerateURL =>
1212
({ filename, prefix = '' }) => {
1313
return decodeURIComponent(
14-
gcs.bucket(bucket).file(path.posix.join(prefix, filename)).publicUrl(),
14+
getStorageClient().bucket(bucket).file(path.posix.join(prefix, filename)).publicUrl(),
1515
)
1616
}

src/adapters/gcs/handleDelete.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { Storage } from '@google-cloud/storage'
33
import type { HandleDelete } from '../../types'
44

55
interface Args {
6-
gcs: Storage
6+
getStorageClient: () => Storage
77
bucket: string
88
}
99

10-
export const getHandleDelete = ({ gcs, bucket }: Args): HandleDelete => {
10+
export const getHandleDelete = ({ getStorageClient, bucket }: Args): HandleDelete => {
1111
return async ({ filename, doc: { prefix = '' } }) => {
12-
await gcs.bucket(bucket).file(path.posix.join(prefix, filename)).delete({
12+
await getStorageClient().bucket(bucket).file(path.posix.join(prefix, filename)).delete({
1313
ignoreNotFound: true,
1414
})
1515
}

src/adapters/gcs/handleUpload.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ interface Args {
88
bucket: string
99
acl?: 'Private' | 'Public'
1010
prefix?: string
11-
gcs: Storage
11+
getStorageClient: () => Storage
1212
}
1313

14-
export const getHandleUpload = ({ gcs, bucket, acl, prefix = '' }: Args): HandleUpload => {
14+
export const getHandleUpload = ({
15+
getStorageClient,
16+
bucket,
17+
acl,
18+
prefix = '',
19+
}: Args): HandleUpload => {
1520
return async ({ data, file }) => {
16-
const gcsFile = gcs.bucket(bucket).file(path.posix.join(prefix, file.filename))
21+
const gcsFile = getStorageClient().bucket(bucket).file(path.posix.join(prefix, file.filename))
1722
await gcsFile.save(file.buffer, {
1823
metadata: {
1924
contentType: file.mimeType,

src/adapters/gcs/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,23 @@ export interface Args {
1515
export const gcsAdapter =
1616
({ options, bucket, acl }: Args): Adapter =>
1717
({ collection, prefix }): GeneratedAdapter => {
18-
const gcs = new Storage(options)
18+
let storageClient: Storage | null = null
19+
const getStorageClient = () => {
20+
if (storageClient) return storageClient
21+
return (storageClient = new Storage(options))
22+
}
1923

2024
return {
2125
handleUpload: getHandleUpload({
2226
collection,
23-
gcs,
27+
getStorageClient,
2428
bucket,
2529
acl,
2630
prefix,
2731
}),
28-
handleDelete: getHandleDelete({ gcs, bucket }),
29-
generateURL: getGenerateURL({ gcs, bucket }),
30-
staticHandler: getHandler({ gcs, bucket, collection }),
32+
handleDelete: getHandleDelete({ getStorageClient, bucket }),
33+
generateURL: getGenerateURL({ getStorageClient, bucket }),
34+
staticHandler: getHandler({ getStorageClient, bucket, collection }),
3135
webpack: extendWebpackConfig,
3236
}
3337
}

src/adapters/gcs/staticHandler.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ import type { StaticHandler } from '../../types'
55
import { getFilePrefix } from '../../utilities/getFilePrefix'
66

77
interface Args {
8-
gcs: Storage
8+
getStorageClient: () => Storage
99
bucket: string
1010
collection: CollectionConfig
1111
}
1212

13-
export const getHandler = ({ gcs, bucket, collection }: Args): StaticHandler => {
13+
export const getHandler = ({ getStorageClient, bucket, collection }: Args): StaticHandler => {
1414
return async (req, res, next) => {
1515
try {
1616
const prefix = await getFilePrefix({ req, collection })
17-
const file = gcs.bucket(bucket).file(path.posix.join(prefix, req.params.filename))
17+
const file = getStorageClient()
18+
.bucket(bucket)
19+
.file(path.posix.join(prefix, req.params.filename))
1820

1921
const [metadata] = await file.getMetadata()
2022

0 commit comments

Comments
 (0)