@basaltkit/storage-azure 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Machize Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # @basaltkit/storage-azure
2
+
3
+ An **Azure Blob Storage** driver for [`@basaltkit/storage`](https://www.npmjs.com/package/@basaltkit/storage): stores files in Azure Blob without changing your app code. You need this module when you run on Azure and want Blob Storage instead of S3, GCS, or local disk.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @basaltkit/storage-azure @azure/storage-blob
9
+ ```
10
+
11
+ `@azure/storage-blob` is a **peer dependency**.
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ import { storagePlugin } from '@basaltkit/storage'
17
+ import { AzureBlobStorageDriver } from '@basaltkit/storage-azure'
18
+
19
+ storagePlugin({
20
+ disks: {
21
+ uploads: {
22
+ driver: new AzureBlobStorageDriver({ container: 'uploads', connectionString: process.env.AZURE_STORAGE_CONNECTION_STRING }),
23
+ },
24
+ },
25
+ })
26
+ ```
27
+
28
+ Implements the `StorageDriver` contract — `put`, `get`, `exists`, `delete`, `list`, and **signed URLs** (SAS via `temporaryUrl`). Per-tenant isolation is automatic via `Disk`.
29
+
30
+ ## Testable without the cloud
31
+
32
+ The container is **injectable**, so the driver logic can be tested with a fake — no Azure needed:
33
+
34
+ ```ts
35
+ new AzureBlobStorageDriver({ container: 'c', client: fakeContainer })
36
+ ```
37
+
38
+ ## How it connects to other modules
39
+
40
+ - **`@basaltkit/storage`** — this is a driver for that package; the API (`Disk`, `storagePlugin`) comes from there.
41
+ - Sibling drivers: `S3StorageDriver` (in core) and [`@basaltkit/storage-gcs`](https://www.npmjs.com/package/@basaltkit/storage-gcs).
@@ -0,0 +1,55 @@
1
+ import { StorageDriver, PutOptions } from '@basaltkit/storage';
2
+
3
+ /** The subset of an `@azure/storage-blob` BlockBlobClient this driver uses. */
4
+ interface AzureBlobLike {
5
+ uploadData(data: Buffer, options?: {
6
+ blobHTTPHeaders?: {
7
+ blobContentType?: string;
8
+ };
9
+ }): Promise<unknown>;
10
+ downloadToBuffer(): Promise<Buffer>;
11
+ exists(): Promise<boolean>;
12
+ deleteIfExists(): Promise<{
13
+ succeeded: boolean;
14
+ }>;
15
+ generateSasUrl(options: {
16
+ permissions: string;
17
+ expiresOn: Date;
18
+ }): Promise<string>;
19
+ }
20
+ /** The subset of an `@azure/storage-blob` ContainerClient this driver uses. */
21
+ interface AzureContainerLike {
22
+ getBlockBlobClient(path: string): AzureBlobLike;
23
+ listBlobsFlat(options?: {
24
+ prefix?: string;
25
+ }): AsyncIterable<{
26
+ name: string;
27
+ }>;
28
+ }
29
+ interface AzureDriverOptions {
30
+ container: string;
31
+ connectionString?: string;
32
+ /** Injectable container — defaults to `@azure/storage-blob`. Tests pass a fake. */
33
+ client?: AzureContainerLike;
34
+ }
35
+ /**
36
+ * Azure Blob Storage driver for `@basaltkit/storage`. Uses `@azure/storage-blob`
37
+ * (an optional peer dependency) via an injectable container client, so its
38
+ * logic is unit-tested without touching Azure.
39
+ */
40
+ declare class AzureBlobStorageDriver implements StorageDriver {
41
+ private readonly options;
42
+ readonly name = "azure";
43
+ private containerPromise;
44
+ constructor(options: AzureDriverOptions);
45
+ put(path: string, content: Buffer | string, options?: PutOptions): Promise<void>;
46
+ get(path: string): Promise<Buffer>;
47
+ exists(path: string): Promise<boolean>;
48
+ delete(path: string): Promise<boolean>;
49
+ list(prefix: string): Promise<string[]>;
50
+ temporaryUrl(path: string, expiresInMs: number): Promise<string>;
51
+ disconnect(): Promise<void>;
52
+ private container;
53
+ }
54
+
55
+ export { type AzureBlobLike, AzureBlobStorageDriver, type AzureContainerLike, type AzureDriverOptions };
package/dist/index.js ADDED
@@ -0,0 +1,54 @@
1
+ // src/index.ts
2
+ import { StorageFileNotFoundError } from "@basaltkit/storage";
3
+ var isNotFound = (error) => error?.statusCode === 404 || error?.code === "BlobNotFound";
4
+ var AzureBlobStorageDriver = class {
5
+ constructor(options) {
6
+ this.options = options;
7
+ }
8
+ options;
9
+ name = "azure";
10
+ containerPromise;
11
+ async put(path, content, options) {
12
+ const data = Buffer.isBuffer(content) ? content : Buffer.from(content);
13
+ await (await this.container()).getBlockBlobClient(path).uploadData(data, options?.contentType !== void 0 ? { blobHTTPHeaders: { blobContentType: options.contentType } } : {});
14
+ }
15
+ async get(path) {
16
+ try {
17
+ return await (await this.container()).getBlockBlobClient(path).downloadToBuffer();
18
+ } catch (error) {
19
+ if (isNotFound(error)) throw new StorageFileNotFoundError(path);
20
+ throw error;
21
+ }
22
+ }
23
+ async exists(path) {
24
+ return (await this.container()).getBlockBlobClient(path).exists();
25
+ }
26
+ async delete(path) {
27
+ const result = await (await this.container()).getBlockBlobClient(path).deleteIfExists();
28
+ return result.succeeded;
29
+ }
30
+ async list(prefix) {
31
+ const names = [];
32
+ for await (const blob of (await this.container()).listBlobsFlat({ prefix })) names.push(blob.name);
33
+ return names;
34
+ }
35
+ async temporaryUrl(path, expiresInMs) {
36
+ return (await this.container()).getBlockBlobClient(path).generateSasUrl({ permissions: "r", expiresOn: new Date(Date.now() + expiresInMs) });
37
+ }
38
+ async disconnect() {
39
+ }
40
+ container() {
41
+ if (!this.containerPromise) {
42
+ this.containerPromise = this.options.client ? Promise.resolve(this.options.client) : (async () => {
43
+ if (!this.options.connectionString) throw new Error("connectionString is required for the Azure driver.");
44
+ const specifier = "@azure/storage-blob";
45
+ const mod = await import(specifier);
46
+ return mod.BlobServiceClient.fromConnectionString(this.options.connectionString).getContainerClient(this.options.container);
47
+ })();
48
+ }
49
+ return this.containerPromise;
50
+ }
51
+ };
52
+ export {
53
+ AzureBlobStorageDriver
54
+ };
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@basaltkit/storage-azure",
3
+ "version": "1.0.0",
4
+ "description": "Azure Blob Storage driver for @basaltkit/storage — put/get/list/delete/SAS URLs over @azure/storage-blob, with an injectable client for testing.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js"
11
+ }
12
+ },
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "dependencies": {
17
+ "@basaltkit/storage": "^1.0.0"
18
+ },
19
+ "peerDependencies": {
20
+ "@azure/storage-blob": "^12.0.0"
21
+ },
22
+ "devDependencies": {
23
+ "@types/node": "^22.15.0",
24
+ "tsup": "^8.4.0",
25
+ "typescript": "^5.8.0",
26
+ "vitest": "^3.1.0",
27
+ "@basaltkit/tsconfig": "^0.24.0"
28
+ },
29
+ "publishConfig": {
30
+ "access": "public"
31
+ },
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/Zebedeu/basalt.git",
35
+ "directory": "packages/storage-azure"
36
+ },
37
+ "homepage": "https://github.com/Zebedeu/basalt/tree/main/packages/storage-azure#readme",
38
+ "bugs": "https://github.com/Zebedeu/basalt/issues",
39
+ "keywords": [
40
+ "basalt",
41
+ "typescript",
42
+ "storage",
43
+ "azure",
44
+ "blob"
45
+ ],
46
+ "scripts": {
47
+ "build": "tsup src/index.ts --format esm --dts --clean",
48
+ "test": "vitest run",
49
+ "typecheck": "tsc --noEmit"
50
+ }
51
+ }