@twin.org/blob-storage-connector-azure 0.0.1-next.10 → 0.0.1-next.12

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.
@@ -4,6 +4,7 @@ var node_stream = require('node:stream');
4
4
  var storageBlob = require('@azure/storage-blob');
5
5
  var core = require('@twin.org/core');
6
6
  var crypto = require('@twin.org/crypto');
7
+ var loggingModels = require('@twin.org/logging-models');
7
8
 
8
9
  // Copyright 2024 IOTA Stiftung.
9
10
  // SPDX-License-Identifier: Apache-2.0.
@@ -20,6 +21,11 @@ class AzureBlobStorageConnector {
20
21
  * Runtime name for the class.
21
22
  */
22
23
  CLASS_NAME = "AzureBlobStorageConnector";
24
+ /**
25
+ * The configuration for the connector.
26
+ * @internal
27
+ */
28
+ _config;
23
29
  /**
24
30
  * The Azure Service client.
25
31
  * @internal
@@ -41,9 +47,63 @@ class AzureBlobStorageConnector {
41
47
  core.Guards.stringValue(this.CLASS_NAME, "options.config.accountName", options.config.accountName);
42
48
  core.Guards.stringValue(this.CLASS_NAME, "options.config.accountKey", options.config.accountKey);
43
49
  core.Guards.stringValue(this.CLASS_NAME, "options.config.containerName", options.config.containerName);
50
+ this._config = options.config;
44
51
  this._azureBlobServiceClient = new storageBlob.BlobServiceClient((options.config.endpoint ?? "https://{accountName}.blob.core.windows.net/").replace("{accountName}", options.config.accountName), new storageBlob.StorageSharedKeyCredential(options.config.accountName, options.config.accountKey));
45
52
  this._azureContainerClient = this._azureBlobServiceClient.getContainerClient(options.config.containerName);
46
53
  }
54
+ /**
55
+ * Bootstrap the component by creating and initializing any resources it needs.
56
+ * @param nodeLoggingConnectorType The node logging connector type, defaults to "node-logging".
57
+ * @returns True if the bootstrapping process was successful.
58
+ */
59
+ async bootstrap(nodeLoggingConnectorType) {
60
+ const nodeLogging = loggingModels.LoggingConnectorFactory.getIfExists(nodeLoggingConnectorType ?? "node-logging");
61
+ try {
62
+ await nodeLogging?.log({
63
+ level: "info",
64
+ source: this.CLASS_NAME,
65
+ message: "containerCreating",
66
+ data: {
67
+ container: this._config.containerName
68
+ }
69
+ });
70
+ const exists = await this._azureContainerClient.exists();
71
+ if (exists) {
72
+ await nodeLogging?.log({
73
+ level: "info",
74
+ source: this.CLASS_NAME,
75
+ message: "containerExists",
76
+ data: {
77
+ container: this._config.containerName
78
+ }
79
+ });
80
+ }
81
+ else {
82
+ await this._azureContainerClient.create();
83
+ await nodeLogging?.log({
84
+ level: "info",
85
+ source: this.CLASS_NAME,
86
+ message: "containerCreated",
87
+ data: {
88
+ container: this._config.containerName
89
+ }
90
+ });
91
+ }
92
+ }
93
+ catch (err) {
94
+ await nodeLogging?.log({
95
+ level: "error",
96
+ source: this.CLASS_NAME,
97
+ message: "containerCreateFailed",
98
+ data: {
99
+ container: this._config.containerName
100
+ },
101
+ error: core.BaseError.fromError(err)
102
+ });
103
+ return false;
104
+ }
105
+ return true;
106
+ }
47
107
  /**
48
108
  * Set the blob.
49
109
  * @param blob The data for the blob.
@@ -1,7 +1,8 @@
1
1
  import { Readable } from 'node:stream';
2
2
  import { BlobServiceClient, StorageSharedKeyCredential } from '@azure/storage-blob';
3
- import { Guards, Converter, Urn, GeneralError } from '@twin.org/core';
3
+ import { Guards, BaseError, Converter, Urn, GeneralError } from '@twin.org/core';
4
4
  import { Sha256 } from '@twin.org/crypto';
5
+ import { LoggingConnectorFactory } from '@twin.org/logging-models';
5
6
 
6
7
  // Copyright 2024 IOTA Stiftung.
7
8
  // SPDX-License-Identifier: Apache-2.0.
@@ -18,6 +19,11 @@ class AzureBlobStorageConnector {
18
19
  * Runtime name for the class.
19
20
  */
20
21
  CLASS_NAME = "AzureBlobStorageConnector";
22
+ /**
23
+ * The configuration for the connector.
24
+ * @internal
25
+ */
26
+ _config;
21
27
  /**
22
28
  * The Azure Service client.
23
29
  * @internal
@@ -39,9 +45,63 @@ class AzureBlobStorageConnector {
39
45
  Guards.stringValue(this.CLASS_NAME, "options.config.accountName", options.config.accountName);
40
46
  Guards.stringValue(this.CLASS_NAME, "options.config.accountKey", options.config.accountKey);
41
47
  Guards.stringValue(this.CLASS_NAME, "options.config.containerName", options.config.containerName);
48
+ this._config = options.config;
42
49
  this._azureBlobServiceClient = new BlobServiceClient((options.config.endpoint ?? "https://{accountName}.blob.core.windows.net/").replace("{accountName}", options.config.accountName), new StorageSharedKeyCredential(options.config.accountName, options.config.accountKey));
43
50
  this._azureContainerClient = this._azureBlobServiceClient.getContainerClient(options.config.containerName);
44
51
  }
52
+ /**
53
+ * Bootstrap the component by creating and initializing any resources it needs.
54
+ * @param nodeLoggingConnectorType The node logging connector type, defaults to "node-logging".
55
+ * @returns True if the bootstrapping process was successful.
56
+ */
57
+ async bootstrap(nodeLoggingConnectorType) {
58
+ const nodeLogging = LoggingConnectorFactory.getIfExists(nodeLoggingConnectorType ?? "node-logging");
59
+ try {
60
+ await nodeLogging?.log({
61
+ level: "info",
62
+ source: this.CLASS_NAME,
63
+ message: "containerCreating",
64
+ data: {
65
+ container: this._config.containerName
66
+ }
67
+ });
68
+ const exists = await this._azureContainerClient.exists();
69
+ if (exists) {
70
+ await nodeLogging?.log({
71
+ level: "info",
72
+ source: this.CLASS_NAME,
73
+ message: "containerExists",
74
+ data: {
75
+ container: this._config.containerName
76
+ }
77
+ });
78
+ }
79
+ else {
80
+ await this._azureContainerClient.create();
81
+ await nodeLogging?.log({
82
+ level: "info",
83
+ source: this.CLASS_NAME,
84
+ message: "containerCreated",
85
+ data: {
86
+ container: this._config.containerName
87
+ }
88
+ });
89
+ }
90
+ }
91
+ catch (err) {
92
+ await nodeLogging?.log({
93
+ level: "error",
94
+ source: this.CLASS_NAME,
95
+ message: "containerCreateFailed",
96
+ data: {
97
+ container: this._config.containerName
98
+ },
99
+ error: BaseError.fromError(err)
100
+ });
101
+ return false;
102
+ }
103
+ return true;
104
+ }
45
105
  /**
46
106
  * Set the blob.
47
107
  * @param blob The data for the blob.
@@ -21,6 +21,12 @@ export declare class AzureBlobStorageConnector implements IBlobStorageConnector
21
21
  constructor(options: {
22
22
  config: IAzureBlobStorageConnectorConfig;
23
23
  });
24
+ /**
25
+ * Bootstrap the component by creating and initializing any resources it needs.
26
+ * @param nodeLoggingConnectorType The node logging connector type, defaults to "node-logging".
27
+ * @returns True if the bootstrapping process was successful.
28
+ */
29
+ bootstrap(nodeLoggingConnectorType?: string): Promise<boolean>;
24
30
  /**
25
31
  * Set the blob.
26
32
  * @param blob The data for the blob.
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/blob-storage-connector-azure - Changelog
2
2
 
3
- ## v0.0.1-next.10
3
+ ## v0.0.1-next.12
4
4
 
5
5
  - Initial Release
@@ -51,6 +51,30 @@ Runtime name for the class.
51
51
 
52
52
  ## Methods
53
53
 
54
+ ### bootstrap()
55
+
56
+ > **bootstrap**(`nodeLoggingConnectorType`?): `Promise`\<`boolean`\>
57
+
58
+ Bootstrap the component by creating and initializing any resources it needs.
59
+
60
+ #### Parameters
61
+
62
+ • **nodeLoggingConnectorType?**: `string`
63
+
64
+ The node logging connector type, defaults to "node-logging".
65
+
66
+ #### Returns
67
+
68
+ `Promise`\<`boolean`\>
69
+
70
+ True if the bootstrapping process was successful.
71
+
72
+ #### Implementation of
73
+
74
+ `IBlobStorageConnector.bootstrap`
75
+
76
+ ***
77
+
54
78
  ### set()
55
79
 
56
80
  > **set**(`blob`): `Promise`\<`string`\>
package/locales/en.json CHANGED
@@ -1,6 +1,14 @@
1
1
  {
2
+ "info": {
3
+ "azureBlobStorageConnector": {
4
+ "containerCreating": "Creating container \"{container}\"",
5
+ "containerCreated": "Created container \"{container}\"",
6
+ "containerExists": "Skipping create container \"{container}\" as it already exists"
7
+ }
8
+ },
2
9
  "error": {
3
10
  "azureBlobStorageConnector": {
11
+ "containerCreateFailed": "Creating container \"{container}\" failed",
4
12
  "namespaceMismatch": "The namespace in the urn \"{id}\" does not match the namespace of the blob storage \"{namespace}\"",
5
13
  "getBlobFailed": "Failed to get blob in Azure",
6
14
  "setBlobFailed": "Failed to set blob in Azure",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/blob-storage-connector-azure",
3
- "version": "0.0.1-next.10",
3
+ "version": "0.0.1-next.12",
4
4
  "description": "Blob Storage connector implementation using Azure",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,9 +15,10 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "@azure/storage-blob": "^12.25.0",
18
- "@twin.org/blob-storage-models": "0.0.1-next.10",
18
+ "@twin.org/blob-storage-models": "0.0.1-next.12",
19
19
  "@twin.org/core": "next",
20
20
  "@twin.org/crypto": "next",
21
+ "@twin.org/logging-models": "next",
21
22
  "@twin.org/nameof": "next"
22
23
  },
23
24
  "main": "./dist/cjs/index.cjs",