@alepha/bucket-azure 0.12.1 → 0.13.1

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.
package/package.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "storage-blob"
11
11
  ],
12
12
  "author": "Nicolas Foures",
13
- "version": "0.12.1",
13
+ "version": "0.13.1",
14
14
  "type": "module",
15
15
  "engines": {
16
16
  "node": ">=22.0.0"
@@ -26,12 +26,12 @@
26
26
  "@azure/storage-blob": "^12.29.1"
27
27
  },
28
28
  "devDependencies": {
29
- "alepha": "0.12.1",
29
+ "alepha": "0.13.1",
30
30
  "tsdown": "^0.16.7",
31
31
  "vitest": "^4.0.14"
32
32
  },
33
33
  "peerDependencies": {
34
- "alepha": "0.12.1"
34
+ "alepha": "0.13.1"
35
35
  },
36
36
  "scripts": {
37
37
  "lint": "alepha lint",
@@ -1,27 +0,0 @@
1
- //#region rolldown:runtime
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") {
10
- for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
- key = keys[i];
12
- if (!__hasOwnProp.call(to, key) && key !== except) {
13
- __defProp(to, key, {
14
- get: ((k) => from[k]).bind(null, key),
15
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
- });
17
- }
18
- }
19
- }
20
- return to;
21
- };
22
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
23
- value: mod,
24
- enumerable: true
25
- }) : target, mod));
26
-
27
- //#endregion
package/dist/index.cjs DELETED
@@ -1,126 +0,0 @@
1
- let alepha = require("alepha");
2
- let alepha_bucket = require("alepha/bucket");
3
- let node_crypto = require("node:crypto");
4
- let node_stream = require("node:stream");
5
- let __azure_storage_blob = require("@azure/storage-blob");
6
- let alepha_datetime = require("alepha/datetime");
7
- let alepha_file = require("alepha/file");
8
- let alepha_logger = require("alepha/logger");
9
-
10
- //#region src/providers/AzureFileStorageProvider.ts
11
- const envSchema = alepha.t.object({ AZ_STORAGE_CONNECTION_STRING: alepha.t.string() });
12
- /**
13
- * Azure Blog Storage implementation of File Storage Provider.
14
- */
15
- var AzureFileStorageProvider = class {
16
- log = (0, alepha_logger.$logger)();
17
- env = (0, alepha.$env)(envSchema);
18
- alepha = (0, alepha.$inject)(alepha.Alepha);
19
- time = (0, alepha.$inject)(alepha_datetime.DateTimeProvider);
20
- fileSystem = (0, alepha.$inject)(alepha_file.FileSystemProvider);
21
- containers = {};
22
- blobServiceClient;
23
- options = {};
24
- constructor() {
25
- this.blobServiceClient = __azure_storage_blob.BlobServiceClient.fromConnectionString(this.env.AZ_STORAGE_CONNECTION_STRING, this.options);
26
- }
27
- onStart = (0, alepha.$hook)({
28
- on: "start",
29
- handler: async () => {
30
- for (const bucket of this.alepha.descriptors(alepha_bucket.$bucket)) {
31
- if (bucket.provider !== this) continue;
32
- const containerName = this.convertName(bucket.name);
33
- this.log.debug(`Prepare container '${containerName}' ...`);
34
- if (!this.containers[containerName]) this.containers[containerName] = await this.createContainerClient(containerName);
35
- this.log.info(`Container '${bucket.name}' OK`);
36
- }
37
- }
38
- });
39
- convertName(name) {
40
- return name.replaceAll("/", "-").toLowerCase();
41
- }
42
- async upload(bucketName, file, fileId) {
43
- fileId ??= this.createId();
44
- this.log.trace(`Uploading file '${file.name}' to bucket '${bucketName}' with id '${fileId}'...`);
45
- const block = this.getBlock(bucketName, fileId);
46
- const metadata = {
47
- name: file.name,
48
- type: file.type
49
- };
50
- if (file.filepath) await block.uploadFile(file.filepath, {
51
- metadata,
52
- blobHTTPHeaders: { blobContentType: file.type }
53
- });
54
- else if (file.size > 0) await block.uploadData(await file.arrayBuffer(), {
55
- metadata,
56
- blobHTTPHeaders: { blobContentType: file.type }
57
- });
58
- else await block.uploadStream(node_stream.Readable.from(file.stream()), file.size || void 0, 5, {
59
- metadata,
60
- blobHTTPHeaders: { blobContentType: file.type }
61
- });
62
- return fileId;
63
- }
64
- async download(bucketName, fileId) {
65
- this.log.trace(`Downloading file '${fileId}' from bucket '${bucketName}'...`);
66
- const blob = await this.getBlock(bucketName, fileId).download().catch((error) => {
67
- if (error instanceof Error) throw new alepha_bucket.FileNotFoundError("Error downloading file", { cause: error });
68
- throw error;
69
- });
70
- if (!blob.readableStreamBody) throw new alepha_bucket.FileNotFoundError("File not found - empty stream body");
71
- return this.fileSystem.createFile({
72
- stream: blob.readableStreamBody,
73
- ...blob.metadata,
74
- size: blob.contentLength
75
- });
76
- }
77
- async exists(bucketName, fileId) {
78
- this.log.trace(`Checking existence of file '${fileId}' in bucket '${bucketName}'...`);
79
- return await this.getBlock(bucketName, fileId).exists();
80
- }
81
- async delete(bucketName, fileId) {
82
- this.log.trace(`Deleting file '${fileId}' from bucket '${bucketName}'...`);
83
- try {
84
- await this.getBlock(bucketName, fileId).delete();
85
- } catch (error) {
86
- if (error instanceof Error) throw new alepha_bucket.FileNotFoundError("Error deleting file", { cause: error });
87
- throw error;
88
- }
89
- }
90
- getBlock(container, fileId) {
91
- const containerName = this.convertName(container);
92
- if (!this.containers[containerName]) throw new alepha_bucket.FileNotFoundError(`File '${fileId}' not found - container '${container}' does not exists`);
93
- return this.containers[containerName].getBlockBlobClient(fileId);
94
- }
95
- async createContainerClient(name) {
96
- const container = this.blobServiceClient.getContainerClient(name);
97
- await this.time.deadline((abortSignal) => container.createIfNotExists({ abortSignal }), [5, "seconds"]);
98
- return container;
99
- }
100
- createId() {
101
- return (0, node_crypto.randomUUID)();
102
- }
103
- };
104
-
105
- //#endregion
106
- //#region src/index.ts
107
- /**
108
- * Plugin for Alepha Bucket that provides Azure Blob Storage capabilities.
109
- *
110
- * @see {@link AzureFileStorageProvider}
111
- * @module alepha.bucket.azure
112
- */
113
- const AlephaBucketAzure = (0, alepha.$module)({
114
- name: "alepha.bucket.azure",
115
- services: [AzureFileStorageProvider],
116
- register: (alepha$1) => alepha$1.with({
117
- optional: true,
118
- provide: alepha_bucket.FileStorageProvider,
119
- use: AzureFileStorageProvider
120
- }).with(alepha_bucket.AlephaBucket)
121
- });
122
-
123
- //#endregion
124
- exports.AlephaBucketAzure = AlephaBucketAzure;
125
- exports.AzureFileStorageProvider = AzureFileStorageProvider;
126
- //# sourceMappingURL=index.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.cjs","names":["t","Alepha","DateTimeProvider","FileSystemProvider","BlobServiceClient","$bucket","Readable","FileNotFoundError","alepha","FileStorageProvider","AlephaBucket"],"sources":["../src/providers/AzureFileStorageProvider.ts","../src/index.ts"],"sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport { Readable } from \"node:stream\";\nimport {\n BlobServiceClient,\n type BlockBlobClient,\n type ContainerClient,\n type StoragePipelineOptions,\n} from \"@azure/storage-blob\";\nimport {\n $env,\n $hook,\n $inject,\n Alepha,\n type FileLike,\n type Static,\n t,\n} from \"alepha\";\nimport {\n $bucket,\n FileNotFoundError,\n type FileStorageProvider,\n} from \"alepha/bucket\";\nimport { DateTimeProvider } from \"alepha/datetime\";\nimport { FileSystemProvider } from \"alepha/file\";\nimport { $logger } from \"alepha/logger\";\n\nconst envSchema = t.object({\n AZ_STORAGE_CONNECTION_STRING: t.string(),\n});\n\ndeclare module \"alepha\" {\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 fileSystem = $inject(FileSystemProvider);\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 this.fileSystem.createFile({\n stream: 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 { $module } from \"alepha\";\nimport { AlephaBucket, FileStorageProvider } from \"alepha/bucket\";\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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