@alepha/bucket-azure 0.10.5 → 0.10.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/providers/AzureFileStorageProvider.ts","../src/index.ts"],"sourcesContent":[],"mappings":";;;;;;;;cA0BM,yBAAS;gCAEb,aAAA,CAAA;;;EAFI,UAAA,GAEJ,SAGqB,OAHrB,CAG6B,MAH7B,CAAA,OAG2C,SAH3C,CAAA,CAAA,CAAA,CAAA;;;;AAFa;AAAA,cAWF,wBAAA,YAAoC,mBAXlC,CAAA;qBAK8B,GAAA,EAMP,eAAA,CACf,MAPsB;qBAAd,GAAA,EAAA;IAAR,4BAAA,EAAA,MAAA;EAAO,CAAA;EAAA,mBAAA,MAAA,EASJ,MATI;EAMjB,mBAAA,IAAA,EAIW,gBAJc;EAAA,mBAAA,UAAA,EAKN,MALM,CAAA,MAAA,EAKS,eALT,CAAA;qBACf,iBAAA,EAKgB,iBALhB;WAEG,OAAA,EAKA,sBALA;aACF,CAAA;qBACuB,OAAA,EAGC,aAAA,CASrB,cAZoB,CAAA,OAAA,CAAA;aAAf,CAAA,IAAA,EAAA,MAAA,CAAA,EAAA,MAAA;QACO,CAAA,UAAA,EAAA,MAAA,EAAA,IAAA,EAwC/B,QAxC+B,EAAA,MAAA,CAAA,EAAA,MAAA,CAAA,EA0CnC,OA1CmC,CAAA,MAAA,CAAA;UAEb,CAAA,UAAA,EAAA,MAAA,EAAA,MAAA,EAAA,MAAA,CAAA,EAqFkC,OArFlC,CAqF0C,QArF1C,CAAA;QAAsB,CAAA,UASrB,EAAA,MAAA,EAAA,MAAA,EAAA,MAAA,CAAA,EAoG+B,OApG/B,CAAA,OAAA,CAAA;QA6BnB,CAAA,UAAA,EAAA,MAAA,EAAA,MAAA,EAAA,MAAA,CAAA,EA8EkD,OA9ElD,CAAA,IAAA,CAAA;UAEJ,CAAA,SAAA,EA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A,EAAA,MAAA,CAAA,EAwBV,OAxBU,CAAA,OAAA,CAAA;EAAR,MAAA,CAAA,UAAA,EAAA,MAAA,EAAA,MAAA,EAAA,MAAA,CAAA,EA+BF,OA/BE,CAAA,IAAA,CAAA;EAwBF,QAAA,CAAA,SAAA,EAAA,MAAA,EAAA,MAAA,EAAA,MAAA,CAAA,EAmBL,eAnBK;EAOA,UAAA,qBAAA,CAAA,IAAA,EAAA,MAAA,CAAA,EA0BtD,OA1BsD,CA0B9C,eA1B8C,CAAA;EAYL,UAAA,QAAA,CAAA,CAAA,EAAA,MAAA;;;;;;;;;;cC/JzC,mBAAiB,aAAA,CAAA,QAW5B,aAAA,CAX4B"}
package/dist/index.js CHANGED
@@ -62,8 +62,7 @@ var AzureFileStorageProvider = class {
62
62
  }
63
63
  async download(bucketName, fileId) {
64
64
  this.log.trace(`Downloading file '${fileId}' from bucket '${bucketName}'...`);
65
- const block = this.getBlock(bucketName, fileId);
66
- const blob = await block.download().catch((error) => {
65
+ const blob = await this.getBlock(bucketName, fileId).download().catch((error) => {
67
66
  if (error instanceof Error) throw new FileNotFoundError("Error downloading file", { cause: error });
68
67
  throw error;
69
68
  });
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/providers/AzureFileStorageProvider.ts","../src/index.ts"],"sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport { Readable } from \"node:stream\";\nimport {\n\t$bucket,\n\tFileNotFoundError,\n\ttype FileStorageProvider,\n} from \"@alepha/bucket\";\nimport {\n\t$env,\n\t$hook,\n\t$inject,\n\tAlepha,\n\ttype FileLike,\n\ttype Static,\n\tt,\n} from \"@alepha/core\";\nimport { DateTimeProvider } from \"@alepha/datetime\";\nimport { createFile } from \"@alepha/file\";\nimport { $logger } from \"@alepha/logger\";\nimport {\n\tBlobServiceClient,\n\ttype BlockBlobClient,\n\ttype ContainerClient,\n\ttype StoragePipelineOptions,\n} from \"@azure/storage-blob\";\n\nconst envSchema = t.object({\n\tAZ_STORAGE_CONNECTION_STRING: t.string(),\n});\n\ndeclare module \"@alepha/core\" {\n\tinterface Env extends Partial<Static<typeof envSchema>> {}\n}\n\n/**\n * Azure Blog Storage implementation of File Storage Provider.\n */\nexport class AzureFileStorageProvider implements FileStorageProvider {\n\tprotected readonly log = $logger();\n\tprotected readonly env = $env(envSchema);\n\tprotected readonly alepha = $inject(Alepha);\n\tprotected readonly time = $inject(DateTimeProvider);\n\tprotected readonly containers: Record<string, ContainerClient> = {};\n\tprotected readonly blobServiceClient: BlobServiceClient;\n\n\tpublic readonly options: StoragePipelineOptions = {};\n\n\tconstructor() {\n\t\tthis.blobServiceClient = BlobServiceClient.fromConnectionString(\n\t\t\tthis.env.AZ_STORAGE_CONNECTION_STRING,\n\t\t\tthis.options,\n\t\t);\n\t}\n\n\tprotected readonly onStart = $hook({\n\t\ton: \"start\",\n\t\thandler: async () => {\n\t\t\tfor (const bucket of this.alepha.descriptors($bucket)) {\n\t\t\t\tif (bucket.provider !== this) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tconst containerName = this.convertName(bucket.name);\n\n\t\t\t\tthis.log.debug(`Prepare container '${containerName}' ...`);\n\n\t\t\t\tif (!this.containers[containerName]) {\n\t\t\t\t\tthis.containers[containerName] =\n\t\t\t\t\t\tawait this.createContainerClient(containerName);\n\t\t\t\t}\n\n\t\t\t\tthis.log.info(`Container '${bucket.name}' OK`);\n\t\t\t}\n\t\t},\n\t});\n\n\tpublic convertName(name: string): string {\n\t\t// Azure Blob Storage does not allow uppercase letters in container names\n\t\treturn name.replaceAll(\"/\", \"-\").toLowerCase();\n\t}\n\n\tpublic async upload(\n\t\tbucketName: string,\n\t\tfile: FileLike,\n\t\tfileId?: string,\n\t): Promise<string> {\n\t\tfileId ??= this.createId();\n\n\t\tthis.log.trace(\n\t\t\t`Uploading file '${file.name}' to bucket '${bucketName}' with id '${fileId}'...`,\n\t\t);\n\n\t\tconst block = this.getBlock(bucketName, fileId);\n\n\t\tconst metadata = {\n\t\t\tname: file.name,\n\t\t\ttype: file.type,\n\t\t};\n\n\t\tif (file.filepath) {\n\t\t\tawait block.uploadFile(file.filepath, {\n\t\t\t\tmetadata,\n\t\t\t\tblobHTTPHeaders: {\n\t\t\t\t\tblobContentType: file.type,\n\t\t\t\t},\n\t\t\t});\n\t\t} else if (file.size > 0) {\n\t\t\tawait block.uploadData(await file.arrayBuffer(), {\n\t\t\t\tmetadata,\n\t\t\t\tblobHTTPHeaders: {\n\t\t\t\t\tblobContentType: file.type,\n\t\t\t\t},\n\t\t\t});\n\t\t} else {\n\t\t\tawait block.uploadStream(\n\t\t\t\tReadable.from(file.stream()),\n\t\t\t\tfile.size || undefined,\n\t\t\t\t5,\n\t\t\t\t{\n\t\t\t\t\tmetadata,\n\t\t\t\t\tblobHTTPHeaders: {\n\t\t\t\t\t\tblobContentType: file.type,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t);\n\t\t}\n\n\t\treturn fileId;\n\t}\n\n\tpublic async download(bucketName: string, fileId: string): Promise<FileLike> {\n\t\tthis.log.trace(\n\t\t\t`Downloading file '${fileId}' from bucket '${bucketName}'...`,\n\t\t);\n\t\tconst block = this.getBlock(bucketName, fileId);\n\n\t\tconst blob = await block.download().catch((error) => {\n\t\t\tif (error instanceof Error) {\n\t\t\t\tthrow new FileNotFoundError(\"Error downloading file\", { cause: error });\n\t\t\t}\n\n\t\t\tthrow error;\n\t\t});\n\n\t\tif (!blob.readableStreamBody) {\n\t\t\tthrow new FileNotFoundError(\"File not found - empty stream body\");\n\t\t}\n\n\t\treturn createFile(blob.readableStreamBody, {\n\t\t\t...blob.metadata,\n\t\t\tsize: blob.contentLength,\n\t\t});\n\t}\n\n\tpublic async exists(bucketName: string, fileId: string): Promise<boolean> {\n\t\tthis.log.trace(\n\t\t\t`Checking existence of file '${fileId}' in bucket '${bucketName}'...`,\n\t\t);\n\t\treturn await this.getBlock(bucketName, fileId).exists();\n\t}\n\n\tpublic async delete(bucketName: string, fileId: string): Promise<void> {\n\t\tthis.log.trace(`Deleting file '${fileId}' from bucket '${bucketName}'...`);\n\t\ttry {\n\t\t\tawait this.getBlock(bucketName, fileId).delete();\n\t\t} catch (error) {\n\t\t\tif (error instanceof Error) {\n\t\t\t\tthrow new FileNotFoundError(\"Error deleting file\", { cause: error });\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tpublic getBlock(container: string, fileId: string): BlockBlobClient {\n\t\tconst containerName = this.convertName(container);\n\n\t\tif (!this.containers[containerName]) {\n\t\t\tthrow new FileNotFoundError(\n\t\t\t\t`File '${fileId}' not found - container '${container}' does not exists`,\n\t\t\t);\n\t\t}\n\n\t\treturn this.containers[containerName].getBlockBlobClient(fileId);\n\t}\n\n\tprotected async createContainerClient(\n\t\tname: string,\n\t): Promise<ContainerClient> {\n\t\tconst container = this.blobServiceClient.getContainerClient(name);\n\n\t\tawait this.time.deadline(\n\t\t\t(abortSignal) => container.createIfNotExists({ abortSignal }),\n\t\t\t[5, \"seconds\"],\n\t\t);\n\n\t\treturn container;\n\t}\n\n\tprotected createId(): string {\n\t\treturn randomUUID();\n\t}\n}\n","import { AlephaBucket, FileStorageProvider } from \"@alepha/bucket\";\nimport { $module } from \"@alepha/core\";\nimport { AzureFileStorageProvider } from \"./providers/AzureFileStorageProvider.ts\";\n\nexport * from \"./providers/AzureFileStorageProvider.ts\";\n\n// ---------------------------------------------------------------------------------------------------------------------\n\n/**\n * Plugin for Alepha Bucket that provides Azure Blob Storage capabilities.\n *\n * @see {@link AzureFileStorageProvider}\n * @module alepha.bucket.azure\n */\nexport const AlephaBucketAzure = $module({\n\tname: \"alepha.bucket.azure\",\n\tservices: [AzureFileStorageProvider],\n\tregister: (alepha) =>\n\t\talepha\n\t\t\t.with({\n\t\t\t\toptional: true,\n\t\t\t\tprovide: FileStorageProvider,\n\t\t\t\tuse: AzureFileStorageProvider,\n\t\t\t})\n\t\t\t.with(AlephaBucket),\n});\n"],"mappings":";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WAAmB;AAC5B,SAAO;CACP;AACD;;;;;;;;;;AC3LD,MAAa,oBAAoB,QAAQ;CACxC,MAAM;CACN,UAAU,CAAC,yBAAyB;CACpC,WAAW,WACV,OACE,KAAK;EACL,UAAU;EACV,SAAS;EACT,KAAK;EACL,EACA,KAAK;CACR"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/providers/AzureFileStorageProvider.ts","../src/index.ts"],"sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport { Readable } from \"node:stream\";\nimport {\n $bucket,\n FileNotFoundError,\n type FileStorageProvider,\n} from \"@alepha/bucket\";\nimport {\n $env,\n $hook,\n $inject,\n Alepha,\n type FileLike,\n type Static,\n t,\n} from \"@alepha/core\";\nimport { DateTimeProvider } from \"@alepha/datetime\";\nimport { createFile } from \"@alepha/file\";\nimport { $logger } from \"@alepha/logger\";\nimport {\n BlobServiceClient,\n type BlockBlobClient,\n type ContainerClient,\n type StoragePipelineOptions,\n} from \"@azure/storage-blob\";\n\nconst envSchema = t.object({\n AZ_STORAGE_CONNECTION_STRING: t.string(),\n});\n\ndeclare module \"@alepha/core\" {\n interface Env extends Partial<Static<typeof envSchema>> {}\n}\n\n/**\n * Azure Blog Storage implementation of File Storage Provider.\n */\nexport class AzureFileStorageProvider implements FileStorageProvider {\n protected readonly log = $logger();\n protected readonly env = $env(envSchema);\n protected readonly alepha = $inject(Alepha);\n protected readonly time = $inject(DateTimeProvider);\n protected readonly containers: Record<string, ContainerClient> = {};\n protected readonly blobServiceClient: BlobServiceClient;\n\n public readonly options: StoragePipelineOptions = {};\n\n constructor() {\n this.blobServiceClient = BlobServiceClient.fromConnectionString(\n this.env.AZ_STORAGE_CONNECTION_STRING,\n this.options,\n );\n }\n\n protected readonly onStart = $hook({\n on: \"start\",\n handler: async () => {\n for (const bucket of this.alepha.descriptors($bucket)) {\n if (bucket.provider !== this) {\n continue;\n }\n\n const containerName = this.convertName(bucket.name);\n\n this.log.debug(`Prepare container '${containerName}' ...`);\n\n if (!this.containers[containerName]) {\n this.containers[containerName] =\n await this.createContainerClient(containerName);\n }\n\n this.log.info(`Container '${bucket.name}' OK`);\n }\n },\n });\n\n public convertName(name: string): string {\n // Azure Blob Storage does not allow uppercase letters in container names\n return name.replaceAll(\"/\", \"-\").toLowerCase();\n }\n\n public async upload(\n bucketName: string,\n file: FileLike,\n fileId?: string,\n ): Promise<string> {\n fileId ??= this.createId();\n\n this.log.trace(\n `Uploading file '${file.name}' to bucket '${bucketName}' with id '${fileId}'...`,\n );\n\n const block = this.getBlock(bucketName, fileId);\n\n const metadata = {\n name: file.name,\n type: file.type,\n };\n\n if (file.filepath) {\n await block.uploadFile(file.filepath, {\n metadata,\n blobHTTPHeaders: {\n blobContentType: file.type,\n },\n });\n } else if (file.size > 0) {\n await block.uploadData(await file.arrayBuffer(), {\n metadata,\n blobHTTPHeaders: {\n blobContentType: file.type,\n },\n });\n } else {\n await block.uploadStream(\n Readable.from(file.stream()),\n file.size || undefined,\n 5,\n {\n metadata,\n blobHTTPHeaders: {\n blobContentType: file.type,\n },\n },\n );\n }\n\n return fileId;\n }\n\n public async download(bucketName: string, fileId: string): Promise<FileLike> {\n this.log.trace(\n `Downloading file '${fileId}' from bucket '${bucketName}'...`,\n );\n const block = this.getBlock(bucketName, fileId);\n\n const blob = await block.download().catch((error) => {\n if (error instanceof Error) {\n throw new FileNotFoundError(\"Error downloading file\", { cause: error });\n }\n\n throw error;\n });\n\n if (!blob.readableStreamBody) {\n throw new FileNotFoundError(\"File not found - empty stream body\");\n }\n\n return createFile(blob.readableStreamBody, {\n ...blob.metadata,\n size: blob.contentLength,\n });\n }\n\n public async exists(bucketName: string, fileId: string): Promise<boolean> {\n this.log.trace(\n `Checking existence of file '${fileId}' in bucket '${bucketName}'...`,\n );\n return await this.getBlock(bucketName, fileId).exists();\n }\n\n public async delete(bucketName: string, fileId: string): Promise<void> {\n this.log.trace(`Deleting file '${fileId}' from bucket '${bucketName}'...`);\n try {\n await this.getBlock(bucketName, fileId).delete();\n } catch (error) {\n if (error instanceof Error) {\n throw new FileNotFoundError(\"Error deleting file\", { cause: error });\n }\n throw error;\n }\n }\n\n public getBlock(container: string, fileId: string): BlockBlobClient {\n const containerName = this.convertName(container);\n\n if (!this.containers[containerName]) {\n throw new FileNotFoundError(\n `File '${fileId}' not found - container '${container}' does not exists`,\n );\n }\n\n return this.containers[containerName].getBlockBlobClient(fileId);\n }\n\n protected async createContainerClient(\n name: string,\n ): Promise<ContainerClient> {\n const container = this.blobServiceClient.getContainerClient(name);\n\n await this.time.deadline(\n (abortSignal) => container.createIfNotExists({ abortSignal }),\n [5, \"seconds\"],\n );\n\n return container;\n }\n\n protected createId(): string {\n return randomUUID();\n }\n}\n","import { AlephaBucket, FileStorageProvider } from \"@alepha/bucket\";\nimport { $module } from \"@alepha/core\";\nimport { AzureFileStorageProvider } from \"./providers/AzureFileStorageProvider.ts\";\n\nexport * from \"./providers/AzureFileStorageProvider.ts\";\n\n// ---------------------------------------------------------------------------------------------------------------------\n\n/**\n * Plugin for Alepha Bucket that provides Azure Blob Storage capabilities.\n *\n * @see {@link AzureFileStorageProvider}\n * @module alepha.bucket.azure\n */\nexport const AlephaBucketAzure = $module({\n name: \"alepha.bucket.azure\",\n services: [AzureFileStorageProvider],\n register: (alepha) =>\n alepha\n .with({\n optional: true,\n provide: FileStorageProvider,\n use: AzureFileStorageProvider,\n })\n .with(AlephaBucket),\n});\n"],"mappings":";;;;;;;;;;AA0BA,MAAM,YAAY,EAAE,OAAO,EACzB,8BAA8B,EAAE,QAAQ,EACzC,CAAC;;;;AASF,IAAa,2BAAb,MAAqE;CACnE,AAAmB,MAAM,SAAS;CAClC,AAAmB,MAAM,KAAK,UAAU;CACxC,AAAmB,SAAS,QAAQ,OAAO;CAC3C,AAAmB,OAAO,QAAQ,iBAAiB;CACnD,AAAmB,aAA8C,EAAE;CACnE,AAAmB;CAEnB,AAAgB,UAAkC,EAAE;CAEpD,cAAc;AACZ,OAAK,oBAAoB,kBAAkB,qBACzC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;EACV,SAAS;EACT,KAAK;EACN,CAAC,CACD,KAAK,aAAa;CACxB,CAAC"}
package/package.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "storage-blob"
11
11
  ],
12
12
  "author": "Feunard",
13
- "version": "0.10.5",
13
+ "version": "0.10.7",
14
14
  "type": "module",
15
15
  "engines": {
16
16
  "node": ">=22.0.0"
@@ -23,16 +23,16 @@
23
23
  "src"
24
24
  ],
25
25
  "dependencies": {
26
- "@alepha/bucket": "0.10.5",
27
- "@alepha/core": "0.10.5",
28
- "@alepha/datetime": "0.10.5",
29
- "@alepha/file": "0.10.5",
30
- "@alepha/logger": "0.10.5",
31
- "@azure/storage-blob": "^12.29.0"
26
+ "@alepha/bucket": "0.10.7",
27
+ "@alepha/core": "0.10.7",
28
+ "@alepha/datetime": "0.10.7",
29
+ "@alepha/file": "0.10.7",
30
+ "@alepha/logger": "0.10.7",
31
+ "@azure/storage-blob": "^12.29.1"
32
32
  },
33
33
  "devDependencies": {
34
- "@biomejs/biome": "^2.2.5",
35
- "tsdown": "^0.15.6",
34
+ "@biomejs/biome": "^2.2.6",
35
+ "tsdown": "^0.15.9",
36
36
  "typescript": "^5.9.3",
37
37
  "vitest": "^3.2.4"
38
38
  },
package/src/index.ts CHANGED
@@ -13,14 +13,14 @@ export * from "./providers/AzureFileStorageProvider.ts";
13
13
  * @module alepha.bucket.azure
14
14
  */
15
15
  export const AlephaBucketAzure = $module({
16
- name: "alepha.bucket.azure",
17
- services: [AzureFileStorageProvider],
18
- register: (alepha) =>
19
- alepha
20
- .with({
21
- optional: true,
22
- provide: FileStorageProvider,
23
- use: AzureFileStorageProvider,
24
- })
25
- .with(AlephaBucket),
16
+ name: "alepha.bucket.azure",
17
+ services: [AzureFileStorageProvider],
18
+ register: (alepha) =>
19
+ alepha
20
+ .with({
21
+ optional: true,
22
+ provide: FileStorageProvider,
23
+ use: AzureFileStorageProvider,
24
+ })
25
+ .with(AlephaBucket),
26
26
  });
@@ -1,202 +1,202 @@
1
1
  import { randomUUID } from "node:crypto";
2
2
  import { Readable } from "node:stream";
3
3
  import {
4
- $bucket,
5
- FileNotFoundError,
6
- type FileStorageProvider,
4
+ $bucket,
5
+ FileNotFoundError,
6
+ type FileStorageProvider,
7
7
  } from "@alepha/bucket";
8
8
  import {
9
- $env,
10
- $hook,
11
- $inject,
12
- Alepha,
13
- type FileLike,
14
- type Static,
15
- t,
9
+ $env,
10
+ $hook,
11
+ $inject,
12
+ Alepha,
13
+ type FileLike,
14
+ type Static,
15
+ t,
16
16
  } from "@alepha/core";
17
17
  import { DateTimeProvider } from "@alepha/datetime";
18
18
  import { createFile } from "@alepha/file";
19
19
  import { $logger } from "@alepha/logger";
20
20
  import {
21
- BlobServiceClient,
22
- type BlockBlobClient,
23
- type ContainerClient,
24
- type StoragePipelineOptions,
21
+ BlobServiceClient,
22
+ type BlockBlobClient,
23
+ type ContainerClient,
24
+ type StoragePipelineOptions,
25
25
  } from "@azure/storage-blob";
26
26
 
27
27
  const envSchema = t.object({
28
- AZ_STORAGE_CONNECTION_STRING: t.string(),
28
+ AZ_STORAGE_CONNECTION_STRING: t.string(),
29
29
  });
30
30
 
31
31
  declare module "@alepha/core" {
32
- interface Env extends Partial<Static<typeof envSchema>> {}
32
+ interface Env extends Partial<Static<typeof envSchema>> {}
33
33
  }
34
34
 
35
35
  /**
36
36
  * Azure Blog Storage implementation of File Storage Provider.
37
37
  */
38
38
  export class AzureFileStorageProvider implements FileStorageProvider {
39
- protected readonly log = $logger();
40
- protected readonly env = $env(envSchema);
41
- protected readonly alepha = $inject(Alepha);
42
- protected readonly time = $inject(DateTimeProvider);
43
- protected readonly containers: Record<string, ContainerClient> = {};
44
- protected readonly blobServiceClient: BlobServiceClient;
45
-
46
- public readonly options: StoragePipelineOptions = {};
47
-
48
- constructor() {
49
- this.blobServiceClient = BlobServiceClient.fromConnectionString(
50
- this.env.AZ_STORAGE_CONNECTION_STRING,
51
- this.options,
52
- );
53
- }
54
-
55
- protected readonly onStart = $hook({
56
- on: "start",
57
- handler: async () => {
58
- for (const bucket of this.alepha.descriptors($bucket)) {
59
- if (bucket.provider !== this) {
60
- continue;
61
- }
62
-
63
- const containerName = this.convertName(bucket.name);
64
-
65
- this.log.debug(`Prepare container '${containerName}' ...`);
66
-
67
- if (!this.containers[containerName]) {
68
- this.containers[containerName] =
69
- await this.createContainerClient(containerName);
70
- }
71
-
72
- this.log.info(`Container '${bucket.name}' OK`);
73
- }
74
- },
75
- });
76
-
77
- public convertName(name: string): string {
78
- // Azure Blob Storage does not allow uppercase letters in container names
79
- return name.replaceAll("/", "-").toLowerCase();
80
- }
81
-
82
- public async upload(
83
- bucketName: string,
84
- file: FileLike,
85
- fileId?: string,
86
- ): Promise<string> {
87
- fileId ??= this.createId();
88
-
89
- this.log.trace(
90
- `Uploading file '${file.name}' to bucket '${bucketName}' with id '${fileId}'...`,
91
- );
92
-
93
- const block = this.getBlock(bucketName, fileId);
94
-
95
- const metadata = {
96
- name: file.name,
97
- type: file.type,
98
- };
99
-
100
- if (file.filepath) {
101
- await block.uploadFile(file.filepath, {
102
- metadata,
103
- blobHTTPHeaders: {
104
- blobContentType: file.type,
105
- },
106
- });
107
- } else if (file.size > 0) {
108
- await block.uploadData(await file.arrayBuffer(), {
109
- metadata,
110
- blobHTTPHeaders: {
111
- blobContentType: file.type,
112
- },
113
- });
114
- } else {
115
- await block.uploadStream(
116
- Readable.from(file.stream()),
117
- file.size || undefined,
118
- 5,
119
- {
120
- metadata,
121
- blobHTTPHeaders: {
122
- blobContentType: file.type,
123
- },
124
- },
125
- );
126
- }
127
-
128
- return fileId;
129
- }
130
-
131
- public async download(bucketName: string, fileId: string): Promise<FileLike> {
132
- this.log.trace(
133
- `Downloading file '${fileId}' from bucket '${bucketName}'...`,
134
- );
135
- const block = this.getBlock(bucketName, fileId);
136
-
137
- const blob = await block.download().catch((error) => {
138
- if (error instanceof Error) {
139
- throw new FileNotFoundError("Error downloading file", { cause: error });
140
- }
141
-
142
- throw error;
143
- });
144
-
145
- if (!blob.readableStreamBody) {
146
- throw new FileNotFoundError("File not found - empty stream body");
147
- }
148
-
149
- return createFile(blob.readableStreamBody, {
150
- ...blob.metadata,
151
- size: blob.contentLength,
152
- });
153
- }
154
-
155
- public async exists(bucketName: string, fileId: string): Promise<boolean> {
156
- this.log.trace(
157
- `Checking existence of file '${fileId}' in bucket '${bucketName}'...`,
158
- );
159
- return await this.getBlock(bucketName, fileId).exists();
160
- }
161
-
162
- public async delete(bucketName: string, fileId: string): Promise<void> {
163
- this.log.trace(`Deleting file '${fileId}' from bucket '${bucketName}'...`);
164
- try {
165
- await this.getBlock(bucketName, fileId).delete();
166
- } catch (error) {
167
- if (error instanceof Error) {
168
- throw new FileNotFoundError("Error deleting file", { cause: error });
169
- }
170
- throw error;
171
- }
172
- }
173
-
174
- public getBlock(container: string, fileId: string): BlockBlobClient {
175
- const containerName = this.convertName(container);
176
-
177
- if (!this.containers[containerName]) {
178
- throw new FileNotFoundError(
179
- `File '${fileId}' not found - container '${container}' does not exists`,
180
- );
181
- }
182
-
183
- return this.containers[containerName].getBlockBlobClient(fileId);
184
- }
185
-
186
- protected async createContainerClient(
187
- name: string,
188
- ): Promise<ContainerClient> {
189
- const container = this.blobServiceClient.getContainerClient(name);
190
-
191
- await this.time.deadline(
192
- (abortSignal) => container.createIfNotExists({ abortSignal }),
193
- [5, "seconds"],
194
- );
195
-
196
- return container;
197
- }
198
-
199
- protected createId(): string {
200
- return randomUUID();
201
- }
39
+ protected readonly log = $logger();
40
+ protected readonly env = $env(envSchema);
41
+ protected readonly alepha = $inject(Alepha);
42
+ protected readonly time = $inject(DateTimeProvider);
43
+ protected readonly containers: Record<string, ContainerClient> = {};
44
+ protected readonly blobServiceClient: BlobServiceClient;
45
+
46
+ public readonly options: StoragePipelineOptions = {};
47
+
48
+ constructor() {
49
+ this.blobServiceClient = BlobServiceClient.fromConnectionString(
50
+ this.env.AZ_STORAGE_CONNECTION_STRING,
51
+ this.options,
52
+ );
53
+ }
54
+
55
+ protected readonly onStart = $hook({
56
+ on: "start",
57
+ handler: async () => {
58
+ for (const bucket of this.alepha.descriptors($bucket)) {
59
+ if (bucket.provider !== this) {
60
+ continue;
61
+ }
62
+
63
+ const containerName = this.convertName(bucket.name);
64
+
65
+ this.log.debug(`Prepare container '${containerName}' ...`);
66
+
67
+ if (!this.containers[containerName]) {
68
+ this.containers[containerName] =
69
+ await this.createContainerClient(containerName);
70
+ }
71
+
72
+ this.log.info(`Container '${bucket.name}' OK`);
73
+ }
74
+ },
75
+ });
76
+
77
+ public convertName(name: string): string {
78
+ // Azure Blob Storage does not allow uppercase letters in container names
79
+ return name.replaceAll("/", "-").toLowerCase();
80
+ }
81
+
82
+ public async upload(
83
+ bucketName: string,
84
+ file: FileLike,
85
+ fileId?: string,
86
+ ): Promise<string> {
87
+ fileId ??= this.createId();
88
+
89
+ this.log.trace(
90
+ `Uploading file '${file.name}' to bucket '${bucketName}' with id '${fileId}'...`,
91
+ );
92
+
93
+ const block = this.getBlock(bucketName, fileId);
94
+
95
+ const metadata = {
96
+ name: file.name,
97
+ type: file.type,
98
+ };
99
+
100
+ if (file.filepath) {
101
+ await block.uploadFile(file.filepath, {
102
+ metadata,
103
+ blobHTTPHeaders: {
104
+ blobContentType: file.type,
105
+ },
106
+ });
107
+ } else if (file.size > 0) {
108
+ await block.uploadData(await file.arrayBuffer(), {
109
+ metadata,
110
+ blobHTTPHeaders: {
111
+ blobContentType: file.type,
112
+ },
113
+ });
114
+ } else {
115
+ await block.uploadStream(
116
+ Readable.from(file.stream()),
117
+ file.size || undefined,
118
+ 5,
119
+ {
120
+ metadata,
121
+ blobHTTPHeaders: {
122
+ blobContentType: file.type,
123
+ },
124
+ },
125
+ );
126
+ }
127
+
128
+ return fileId;
129
+ }
130
+
131
+ public async download(bucketName: string, fileId: string): Promise<FileLike> {
132
+ this.log.trace(
133
+ `Downloading file '${fileId}' from bucket '${bucketName}'...`,
134
+ );
135
+ const block = this.getBlock(bucketName, fileId);
136
+
137
+ const blob = await block.download().catch((error) => {
138
+ if (error instanceof Error) {
139
+ throw new FileNotFoundError("Error downloading file", { cause: error });
140
+ }
141
+
142
+ throw error;
143
+ });
144
+
145
+ if (!blob.readableStreamBody) {
146
+ throw new FileNotFoundError("File not found - empty stream body");
147
+ }
148
+
149
+ return createFile(blob.readableStreamBody, {
150
+ ...blob.metadata,
151
+ size: blob.contentLength,
152
+ });
153
+ }
154
+
155
+ public async exists(bucketName: string, fileId: string): Promise<boolean> {
156
+ this.log.trace(
157
+ `Checking existence of file '${fileId}' in bucket '${bucketName}'...`,
158
+ );
159
+ return await this.getBlock(bucketName, fileId).exists();
160
+ }
161
+
162
+ public async delete(bucketName: string, fileId: string): Promise<void> {
163
+ this.log.trace(`Deleting file '${fileId}' from bucket '${bucketName}'...`);
164
+ try {
165
+ await this.getBlock(bucketName, fileId).delete();
166
+ } catch (error) {
167
+ if (error instanceof Error) {
168
+ throw new FileNotFoundError("Error deleting file", { cause: error });
169
+ }
170
+ throw error;
171
+ }
172
+ }
173
+
174
+ public getBlock(container: string, fileId: string): BlockBlobClient {
175
+ const containerName = this.convertName(container);
176
+
177
+ if (!this.containers[containerName]) {
178
+ throw new FileNotFoundError(
179
+ `File '${fileId}' not found - container '${container}' does not exists`,
180
+ );
181
+ }
182
+
183
+ return this.containers[containerName].getBlockBlobClient(fileId);
184
+ }
185
+
186
+ protected async createContainerClient(
187
+ name: string,
188
+ ): Promise<ContainerClient> {
189
+ const container = this.blobServiceClient.getContainerClient(name);
190
+
191
+ await this.time.deadline(
192
+ (abortSignal) => container.createIfNotExists({ abortSignal }),
193
+ [5, "seconds"],
194
+ );
195
+
196
+ return container;
197
+ }
198
+
199
+ protected createId(): string {
200
+ return randomUUID();
201
+ }
202
202
  }