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

Commit f333ff1

Browse files
authored
Merge pull request #20 from linde12/master
Lazily initialize storage clients
2 parents e7f5c2e + d9dd7ca commit f333ff1

15 files changed

Lines changed: 90 additions & 57 deletions

src/adapters/azure/handleDelete.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import type { HandleDelete } from '../../types'
55

66
interface Args {
77
collection: CollectionConfig
8-
containerClient: ContainerClient
8+
getStorageClient: () => ContainerClient
99
}
1010

11-
export const getHandleDelete = ({ containerClient }: Args): HandleDelete => {
11+
export const getHandleDelete = ({ getStorageClient }: Args): HandleDelete => {
1212
return async ({ filename, doc: { prefix = '' } }) => {
13-
const blockBlobClient = containerClient.getBlockBlobClient(path.posix.join(prefix, filename))
13+
const blockBlobClient = getStorageClient().getBlockBlobClient(path.posix.join(prefix, filename))
1414
await blockBlobClient.deleteIfExists()
1515
}
1616
}

src/adapters/azure/handleUpload.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,13 @@ import type { HandleUpload } from '../../types'
55

66
interface Args {
77
collection: CollectionConfig
8-
containerClient: ContainerClient
9-
allowContainerCreate: boolean
8+
getStorageClient: () => ContainerClient
109
prefix?: string
1110
}
1211

13-
export const getHandleUpload = ({
14-
allowContainerCreate,
15-
containerClient,
16-
prefix = '',
17-
}: Args): HandleUpload => {
18-
if (allowContainerCreate) {
19-
containerClient.createIfNotExists({ access: 'blob' })
20-
}
21-
12+
export const getHandleUpload = ({ getStorageClient, prefix = '' }: Args): HandleUpload => {
2213
return async ({ data, file }) => {
23-
const blockBlobClient = containerClient.getBlockBlobClient(
14+
const blockBlobClient = getStorageClient().getBlockBlobClient(
2415
path.posix.join(prefix, file.filename),
2516
)
2617

src/adapters/azure/index.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BlobServiceClient } from '@azure/storage-blob'
1+
import { BlobServiceClient, ContainerClient } from '@azure/storage-blob'
22
import type { Adapter, GeneratedAdapter } from '../../types'
33
import { getHandler } from './staticHandler'
44
import { getGenerateURL } from './generateURL'
@@ -13,22 +13,35 @@ export interface Args {
1313
allowContainerCreate: boolean
1414
}
1515

16-
export const azureBlobStorageAdapter =
17-
({ connectionString, containerName, baseURL, allowContainerCreate }: Args): Adapter =>
18-
({ collection, prefix }): GeneratedAdapter => {
16+
export const azureBlobStorageAdapter = ({
17+
connectionString,
18+
allowContainerCreate,
19+
containerName,
20+
baseURL,
21+
}: Args): Adapter => {
22+
let storageClient: ContainerClient | null = null
23+
const getStorageClient = () => {
24+
if (storageClient) return storageClient
1925
const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString)
20-
const containerClient = blobServiceClient.getContainerClient(containerName)
26+
return (storageClient = blobServiceClient.getContainerClient(containerName))
27+
}
28+
29+
const createContainerIfNotExists = () => {
30+
getStorageClient().createIfNotExists({ access: 'blob' })
31+
}
2132

33+
return ({ collection, prefix }): GeneratedAdapter => {
2234
return {
2335
handleUpload: getHandleUpload({
2436
collection,
25-
containerClient,
26-
allowContainerCreate,
37+
getStorageClient,
2738
prefix,
2839
}),
29-
handleDelete: getHandleDelete({ collection, containerClient }),
40+
handleDelete: getHandleDelete({ collection, getStorageClient }),
3041
generateURL: getGenerateURL({ containerName, baseURL }),
31-
staticHandler: getHandler({ containerClient, collection }),
42+
staticHandler: getHandler({ getStorageClient, collection }),
3243
webpack: extendWebpackConfig,
44+
...(allowContainerCreate && { onInit: createContainerIfNotExists }),
3345
}
3446
}
47+
}

src/adapters/azure/staticHandler.ts

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

77
interface Args {
8-
containerClient: ContainerClient
8+
getStorageClient: () => ContainerClient
99
collection: CollectionConfig
1010
}
1111

12-
export const getHandler = ({ containerClient, collection }: Args): StaticHandler => {
12+
export const getHandler = ({ getStorageClient, collection }: Args): StaticHandler => {
1313
return async (req, res, next) => {
1414
try {
1515
const prefix = await getFilePrefix({ req, collection })
16-
const blockBlobClient = containerClient.getBlockBlobClient(
16+
const blockBlobClient = getStorageClient().getBlockBlobClient(
1717
path.posix.join(prefix, req.params.filename),
1818
)
1919

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

src/adapters/s3/handleDelete.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import type * as AWS from '@aws-sdk/client-s3'
33
import type { HandleDelete } from '../../types'
44

55
interface Args {
6-
s3: AWS.S3
6+
getStorageClient: () => AWS.S3
77
bucket: string
88
}
99

10-
export const getHandleDelete = ({ s3, bucket }: Args): HandleDelete => {
10+
export const getHandleDelete = ({ getStorageClient, bucket }: Args): HandleDelete => {
1111
return async ({ filename, doc: { prefix = '' } }) => {
12-
await s3.deleteObject({
12+
await getStorageClient().deleteObject({
1313
Bucket: bucket,
1414
Key: path.posix.join(prefix, filename),
1515
})

0 commit comments

Comments
 (0)