@twin.org/blob-storage-connector-memory 0.0.3-next.10 → 0.0.3-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.
- package/dist/es/memoryBlobStorageConnector.js +42 -1
- package/dist/es/memoryBlobStorageConnector.js.map +1 -1
- package/dist/types/memoryBlobStorageConnector.d.ts +11 -0
- package/docs/changelog.md +28 -0
- package/docs/reference/classes/MemoryBlobStorageConnector.md +44 -0
- package/locales/en.json +8 -1
- package/package.json +3 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ContextIdHelper, ContextIdStore } from "@twin.org/context";
|
|
2
|
-
import { Converter, GeneralError, Guards, HealthStatus, Urn } from "@twin.org/core";
|
|
2
|
+
import { ComponentFactory, Converter, GeneralError, Guards, HealthStatus, Urn } from "@twin.org/core";
|
|
3
3
|
import { Sha256 } from "@twin.org/crypto";
|
|
4
4
|
/**
|
|
5
5
|
* Class for performing blob storage operations in-memory.
|
|
@@ -112,6 +112,47 @@ export class MemoryBlobStorageConnector {
|
|
|
112
112
|
}
|
|
113
113
|
return false;
|
|
114
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* Teardown the component and remove any resources it created.
|
|
117
|
+
* @param nodeLoggingComponentType The node logging component type.
|
|
118
|
+
* @returns True if the teardown process was successful.
|
|
119
|
+
*/
|
|
120
|
+
async teardown(nodeLoggingComponentType) {
|
|
121
|
+
const nodeLogging = ComponentFactory.getIfExists(nodeLoggingComponentType);
|
|
122
|
+
await nodeLogging?.log({
|
|
123
|
+
level: "info",
|
|
124
|
+
source: MemoryBlobStorageConnector.CLASS_NAME,
|
|
125
|
+
message: "storeTearingDown"
|
|
126
|
+
});
|
|
127
|
+
for (const key of Object.keys(this._store)) {
|
|
128
|
+
delete this._store[key];
|
|
129
|
+
}
|
|
130
|
+
await nodeLogging?.log({
|
|
131
|
+
level: "info",
|
|
132
|
+
source: MemoryBlobStorageConnector.CLASS_NAME,
|
|
133
|
+
message: "storeTornDown"
|
|
134
|
+
});
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Remove all blobs from the storage.
|
|
139
|
+
* @returns Nothing.
|
|
140
|
+
*/
|
|
141
|
+
async empty() {
|
|
142
|
+
try {
|
|
143
|
+
const contextIds = await ContextIdStore.getContextIds();
|
|
144
|
+
const partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);
|
|
145
|
+
const prefix = `${partitionKey ?? "root"}/`;
|
|
146
|
+
for (const key of Object.keys(this._store)) {
|
|
147
|
+
if (key.startsWith(prefix)) {
|
|
148
|
+
delete this._store[key];
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
catch (err) {
|
|
153
|
+
throw new GeneralError(MemoryBlobStorageConnector.CLASS_NAME, "emptyFailed", undefined, err);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
115
156
|
/**
|
|
116
157
|
* Get the memory store.
|
|
117
158
|
* @returns The store.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memoryBlobStorageConnector.js","sourceRoot":"","sources":["../../src/memoryBlobStorageConnector.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,EAAE,YAAY,EAAE,GAAG,EAAgB,MAAM,gBAAgB,CAAC;AAClG,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAI1C;;GAEG;AACH,MAAM,OAAO,0BAA0B;IACtC;;OAEG;IACI,MAAM,CAAU,SAAS,GAAW,QAAQ,CAAC;IAEpD;;OAEG;IACI,MAAM,CAAU,UAAU,gCAAgD;IAEjF;;;OAGG;IACc,oBAAoB,CAAY;IAEjD;;;OAGG;IACc,MAAM,CAA+B;IAEtD;;;OAGG;IACH,YAAY,OAAmD;QAC9D,IAAI,CAAC,oBAAoB,GAAG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GAAG,SAAS,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC;QACnD,MAAM,OAAO,GAAG,GAAG,YAAY,IAAI,MAAM,IAAI,WAAW,EAAE,CAAC;QAC3D,IAAI,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;YAC1B,OAAO,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;YAC5B,OAAO,IAAI,CAAC;QACb,CAAC;QACD,OAAO,KAAK,CAAC;IACd,CAAC;IAED;;;OAGG;IACI,QAAQ;QACd,OAAO,IAAI,CAAC,MAAM,CAAC;IACpB,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IBlobStorageConnector } from \"@twin.org/blob-storage-models\";\nimport { ContextIdHelper, ContextIdStore } from \"@twin.org/context\";\nimport { Converter, GeneralError, Guards, HealthStatus, Urn, type IHealth } from \"@twin.org/core\";\nimport { Sha256 } from \"@twin.org/crypto\";\nimport { nameof } from \"@twin.org/nameof\";\nimport type { IMemoryStorageConnectorConstructorOptions } from \"./models/IMemoryStorageConnectorConstructorOptions.js\";\n\n/**\n * Class for performing blob storage operations in-memory.\n */\nexport class MemoryBlobStorageConnector implements IBlobStorageConnector {\n\t/**\n\t * The namespace for the items.\n\t */\n\tpublic static readonly NAMESPACE: string = \"memory\";\n\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<MemoryBlobStorageConnector>();\n\n\t/**\n\t * The keys to use from the context ids to create partitions.\n\t * @internal\n\t */\n\tprivate readonly _partitionContextIds?: string[];\n\n\t/**\n\t * The storage for the in-memory items.\n\t * @internal\n\t */\n\tprivate readonly _store: { [id: string]: Uint8Array };\n\n\t/**\n\t * Create a new instance of MemoryBlobStorageConnector.\n\t * @param options The options for the connector.\n\t */\n\tconstructor(options?: IMemoryStorageConnectorConstructorOptions) {\n\t\tthis._partitionContextIds = options?.partitionContextIds;\n\t\tthis._store = {};\n\t}\n\n\t/**\n\t * Returns the class name of the component.\n\t * @returns The class name of the component.\n\t */\n\tpublic className(): string {\n\t\treturn MemoryBlobStorageConnector.CLASS_NAME;\n\t}\n\n\t/**\n\t * Returns the health status of the component.\n\t * @returns The health status of the component.\n\t */\n\tpublic async health(): Promise<IHealth[]> {\n\t\treturn [\n\t\t\t{\n\t\t\t\tsource: MemoryBlobStorageConnector.CLASS_NAME,\n\t\t\t\tstatus: HealthStatus.Ok,\n\t\t\t\tdescription: \"healthDescription\",\n\t\t\t\tdata: {\n\t\t\t\t\tstoredItemCount: Object.keys(this._store).length\n\t\t\t\t}\n\t\t\t}\n\t\t];\n\t}\n\n\t/**\n\t * Set the blob.\n\t * @param blob The data for the blob.\n\t * @returns The id of the stored blob in urn format.\n\t */\n\tpublic async set(blob: Uint8Array): Promise<string> {\n\t\tGuards.uint8Array(MemoryBlobStorageConnector.CLASS_NAME, nameof(blob), blob);\n\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\n\t\tconst id = Converter.bytesToHex(Sha256.sum256(blob));\n\n\t\tconst fullKey = `${partitionKey ?? \"root\"}/${id}`;\n\t\tthis._store[fullKey] = blob;\n\n\t\treturn `blob:${new Urn(MemoryBlobStorageConnector.NAMESPACE, id).toString()}`;\n\t}\n\n\t/**\n\t * Get the blob.\n\t * @param id The id of the blob to get in urn format.\n\t * @returns The data for the blob if it can be found or undefined.\n\t */\n\tpublic async get(id: string): Promise<Uint8Array | undefined> {\n\t\tUrn.guard(MemoryBlobStorageConnector.CLASS_NAME, nameof(id), id);\n\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\n\t\tconst urnParsed = Urn.fromValidString(id);\n\n\t\tif (urnParsed.namespaceMethod() !== MemoryBlobStorageConnector.NAMESPACE) {\n\t\t\tthrow new GeneralError(MemoryBlobStorageConnector.CLASS_NAME, \"namespaceMismatch\", {\n\t\t\t\tnamespace: MemoryBlobStorageConnector.NAMESPACE,\n\t\t\t\tid\n\t\t\t});\n\t\t}\n\n\t\tconst namespaceId = urnParsed.namespaceSpecific(1);\n\t\tconst fullKey = `${partitionKey ?? \"root\"}/${namespaceId}`;\n\t\treturn this._store[fullKey];\n\t}\n\n\t/**\n\t * Remove the blob.\n\t * @param id The id of the blob to remove in urn format.\n\t * @returns True if the blob was found.\n\t */\n\tpublic async remove(id: string): Promise<boolean> {\n\t\tUrn.guard(MemoryBlobStorageConnector.CLASS_NAME, nameof(id), id);\n\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\n\t\tconst urnParsed = Urn.fromValidString(id);\n\n\t\tif (urnParsed.namespaceMethod() !== MemoryBlobStorageConnector.NAMESPACE) {\n\t\t\tthrow new GeneralError(MemoryBlobStorageConnector.CLASS_NAME, \"namespaceMismatch\", {\n\t\t\t\tnamespace: MemoryBlobStorageConnector.NAMESPACE,\n\t\t\t\tid\n\t\t\t});\n\t\t}\n\n\t\tconst namespaceId = urnParsed.namespaceSpecific(1);\n\t\tconst fullKey = `${partitionKey ?? \"root\"}/${namespaceId}`;\n\t\tif (this._store[fullKey]) {\n\t\t\tdelete this._store[fullKey];\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\n\t/**\n\t * Get the memory store.\n\t * @returns The store.\n\t */\n\tpublic getStore(): { [id: string]: Uint8Array } {\n\t\treturn this._store;\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"memoryBlobStorageConnector.js","sourceRoot":"","sources":["../../src/memoryBlobStorageConnector.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,EACN,gBAAgB,EAChB,SAAS,EACT,YAAY,EACZ,MAAM,EACN,YAAY,EACZ,GAAG,EAEH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AAO,IAAI,CAAC;IACb,CAAC;IAED;;;OAGG;IACI,KAAK,CAAC,KAAK;QACjB,IAAI,CAAC;YACJ,MAAM,UAAU,GAAG,MAAM,cAAc,CAAC,aAAa,EAAE,CAAC;YACxD,MAAM,YAAY,GAAG,eAAe,CAAC,kBAAkB,CACtD,UAAU,EACV,IAAI,CAAC,oBAAoB,CACzB,CAAC;YACF,MAAM,MAAM,GAAG,GAAG,YAAY,IAAI,MAAM,GAAG,CAAC;YAC5C,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;gBAC5C,IAAI,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;oBAC5B,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;gBACzB,CAAC;YACF,CAAC;QACF,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACd,MAAM,IAAI,YAAY,CAAC,0BAA0B,CAAC,UAAU,EAAE,aAAa,EAAE,SAAS,EAAE,GAAG,CAAC,CAAC;QAC9F,CAAC;IACF,CAAC;IAED;;;OAGG;IACI,QAAQ;QACd,OAAO,IAAI,CAAC,MAAM,CAAC;IACpB,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IBlobStorageConnector } from \"@twin.org/blob-storage-models\";\nimport { ContextIdHelper, ContextIdStore } from \"@twin.org/context\";\nimport {\n\tComponentFactory,\n\tConverter,\n\tGeneralError,\n\tGuards,\n\tHealthStatus,\n\tUrn,\n\ttype IHealth\n} from \"@twin.org/core\";\nimport { Sha256 } from \"@twin.org/crypto\";\nimport type { ILoggingComponent } from \"@twin.org/logging-models\";\nimport { nameof } from \"@twin.org/nameof\";\nimport type { IMemoryStorageConnectorConstructorOptions } from \"./models/IMemoryStorageConnectorConstructorOptions.js\";\n\n/**\n * Class for performing blob storage operations in-memory.\n */\nexport class MemoryBlobStorageConnector implements IBlobStorageConnector {\n\t/**\n\t * The namespace for the items.\n\t */\n\tpublic static readonly NAMESPACE: string = \"memory\";\n\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<MemoryBlobStorageConnector>();\n\n\t/**\n\t * The keys to use from the context ids to create partitions.\n\t * @internal\n\t */\n\tprivate readonly _partitionContextIds?: string[];\n\n\t/**\n\t * The storage for the in-memory items.\n\t * @internal\n\t */\n\tprivate readonly _store: { [id: string]: Uint8Array };\n\n\t/**\n\t * Create a new instance of MemoryBlobStorageConnector.\n\t * @param options The options for the connector.\n\t */\n\tconstructor(options?: IMemoryStorageConnectorConstructorOptions) {\n\t\tthis._partitionContextIds = options?.partitionContextIds;\n\t\tthis._store = {};\n\t}\n\n\t/**\n\t * Returns the class name of the component.\n\t * @returns The class name of the component.\n\t */\n\tpublic className(): string {\n\t\treturn MemoryBlobStorageConnector.CLASS_NAME;\n\t}\n\n\t/**\n\t * Returns the health status of the component.\n\t * @returns The health status of the component.\n\t */\n\tpublic async health(): Promise<IHealth[]> {\n\t\treturn [\n\t\t\t{\n\t\t\t\tsource: MemoryBlobStorageConnector.CLASS_NAME,\n\t\t\t\tstatus: HealthStatus.Ok,\n\t\t\t\tdescription: \"healthDescription\",\n\t\t\t\tdata: {\n\t\t\t\t\tstoredItemCount: Object.keys(this._store).length\n\t\t\t\t}\n\t\t\t}\n\t\t];\n\t}\n\n\t/**\n\t * Set the blob.\n\t * @param blob The data for the blob.\n\t * @returns The id of the stored blob in urn format.\n\t */\n\tpublic async set(blob: Uint8Array): Promise<string> {\n\t\tGuards.uint8Array(MemoryBlobStorageConnector.CLASS_NAME, nameof(blob), blob);\n\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\n\t\tconst id = Converter.bytesToHex(Sha256.sum256(blob));\n\n\t\tconst fullKey = `${partitionKey ?? \"root\"}/${id}`;\n\t\tthis._store[fullKey] = blob;\n\n\t\treturn `blob:${new Urn(MemoryBlobStorageConnector.NAMESPACE, id).toString()}`;\n\t}\n\n\t/**\n\t * Get the blob.\n\t * @param id The id of the blob to get in urn format.\n\t * @returns The data for the blob if it can be found or undefined.\n\t */\n\tpublic async get(id: string): Promise<Uint8Array | undefined> {\n\t\tUrn.guard(MemoryBlobStorageConnector.CLASS_NAME, nameof(id), id);\n\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\n\t\tconst urnParsed = Urn.fromValidString(id);\n\n\t\tif (urnParsed.namespaceMethod() !== MemoryBlobStorageConnector.NAMESPACE) {\n\t\t\tthrow new GeneralError(MemoryBlobStorageConnector.CLASS_NAME, \"namespaceMismatch\", {\n\t\t\t\tnamespace: MemoryBlobStorageConnector.NAMESPACE,\n\t\t\t\tid\n\t\t\t});\n\t\t}\n\n\t\tconst namespaceId = urnParsed.namespaceSpecific(1);\n\t\tconst fullKey = `${partitionKey ?? \"root\"}/${namespaceId}`;\n\t\treturn this._store[fullKey];\n\t}\n\n\t/**\n\t * Remove the blob.\n\t * @param id The id of the blob to remove in urn format.\n\t * @returns True if the blob was found.\n\t */\n\tpublic async remove(id: string): Promise<boolean> {\n\t\tUrn.guard(MemoryBlobStorageConnector.CLASS_NAME, nameof(id), id);\n\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\n\t\tconst urnParsed = Urn.fromValidString(id);\n\n\t\tif (urnParsed.namespaceMethod() !== MemoryBlobStorageConnector.NAMESPACE) {\n\t\t\tthrow new GeneralError(MemoryBlobStorageConnector.CLASS_NAME, \"namespaceMismatch\", {\n\t\t\t\tnamespace: MemoryBlobStorageConnector.NAMESPACE,\n\t\t\t\tid\n\t\t\t});\n\t\t}\n\n\t\tconst namespaceId = urnParsed.namespaceSpecific(1);\n\t\tconst fullKey = `${partitionKey ?? \"root\"}/${namespaceId}`;\n\t\tif (this._store[fullKey]) {\n\t\t\tdelete this._store[fullKey];\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\n\t/**\n\t * Teardown the component and remove any resources it created.\n\t * @param nodeLoggingComponentType The node logging component type.\n\t * @returns True if the teardown process was successful.\n\t */\n\tpublic async teardown(nodeLoggingComponentType?: string): Promise<boolean> {\n\t\tconst nodeLogging = ComponentFactory.getIfExists<ILoggingComponent>(nodeLoggingComponentType);\n\n\t\tawait nodeLogging?.log({\n\t\t\tlevel: \"info\",\n\t\t\tsource: MemoryBlobStorageConnector.CLASS_NAME,\n\t\t\tmessage: \"storeTearingDown\"\n\t\t});\n\n\t\tfor (const key of Object.keys(this._store)) {\n\t\t\tdelete this._store[key];\n\t\t}\n\n\t\tawait nodeLogging?.log({\n\t\t\tlevel: \"info\",\n\t\t\tsource: MemoryBlobStorageConnector.CLASS_NAME,\n\t\t\tmessage: \"storeTornDown\"\n\t\t});\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * Remove all blobs from the storage.\n\t * @returns Nothing.\n\t */\n\tpublic async empty(): Promise<void> {\n\t\ttry {\n\t\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\t\tconst partitionKey = ContextIdHelper.combinedContextKey(\n\t\t\t\tcontextIds,\n\t\t\t\tthis._partitionContextIds\n\t\t\t);\n\t\t\tconst prefix = `${partitionKey ?? \"root\"}/`;\n\t\t\tfor (const key of Object.keys(this._store)) {\n\t\t\t\tif (key.startsWith(prefix)) {\n\t\t\t\t\tdelete this._store[key];\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(MemoryBlobStorageConnector.CLASS_NAME, \"emptyFailed\", undefined, err);\n\t\t}\n\t}\n\n\t/**\n\t * Get the memory store.\n\t * @returns The store.\n\t */\n\tpublic getStore(): { [id: string]: Uint8Array } {\n\t\treturn this._store;\n\t}\n}\n"]}
|
|
@@ -46,6 +46,17 @@ export declare class MemoryBlobStorageConnector implements IBlobStorageConnector
|
|
|
46
46
|
* @returns True if the blob was found.
|
|
47
47
|
*/
|
|
48
48
|
remove(id: string): Promise<boolean>;
|
|
49
|
+
/**
|
|
50
|
+
* Teardown the component and remove any resources it created.
|
|
51
|
+
* @param nodeLoggingComponentType The node logging component type.
|
|
52
|
+
* @returns True if the teardown process was successful.
|
|
53
|
+
*/
|
|
54
|
+
teardown(nodeLoggingComponentType?: string): Promise<boolean>;
|
|
55
|
+
/**
|
|
56
|
+
* Remove all blobs from the storage.
|
|
57
|
+
* @returns Nothing.
|
|
58
|
+
*/
|
|
59
|
+
empty(): Promise<void>;
|
|
49
60
|
/**
|
|
50
61
|
* Get the memory store.
|
|
51
62
|
* @returns The store.
|
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.0.3-next.12](https://github.com/iotaledger/twin-blob-storage/compare/blob-storage-connector-memory-v0.0.3-next.11...blob-storage-connector-memory-v0.0.3-next.12) (2026-05-11)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* typescript 6 update ([4eed54f](https://github.com/iotaledger/twin-blob-storage/commit/4eed54f5ce2dc697c06597269c97ad4cad108be5))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Dependencies
|
|
12
|
+
|
|
13
|
+
* The following workspace dependencies were updated
|
|
14
|
+
* dependencies
|
|
15
|
+
* @twin.org/blob-storage-models bumped from 0.0.3-next.11 to 0.0.3-next.12
|
|
16
|
+
|
|
17
|
+
## [0.0.3-next.11](https://github.com/iotaledger/twin-blob-storage/compare/blob-storage-connector-memory-v0.0.3-next.10...blob-storage-connector-memory-v0.0.3-next.11) (2026-05-08)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
* add empty and teardown methods ([#49](https://github.com/iotaledger/twin-blob-storage/issues/49)) ([cec6248](https://github.com/iotaledger/twin-blob-storage/commit/cec624809ffd2f2baa4b7b8cbf72a7247b8703ed))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Dependencies
|
|
26
|
+
|
|
27
|
+
* The following workspace dependencies were updated
|
|
28
|
+
* dependencies
|
|
29
|
+
* @twin.org/blob-storage-models bumped from 0.0.3-next.10 to 0.0.3-next.11
|
|
30
|
+
|
|
3
31
|
## [0.0.3-next.10](https://github.com/iotaledger/twin-blob-storage/compare/blob-storage-connector-memory-v0.0.3-next.9...blob-storage-connector-memory-v0.0.3-next.10) (2026-05-07)
|
|
4
32
|
|
|
5
33
|
|
|
@@ -158,6 +158,50 @@ True if the blob was found.
|
|
|
158
158
|
|
|
159
159
|
***
|
|
160
160
|
|
|
161
|
+
### teardown() {#teardown}
|
|
162
|
+
|
|
163
|
+
> **teardown**(`nodeLoggingComponentType?`): `Promise`\<`boolean`\>
|
|
164
|
+
|
|
165
|
+
Teardown the component and remove any resources it created.
|
|
166
|
+
|
|
167
|
+
#### Parameters
|
|
168
|
+
|
|
169
|
+
##### nodeLoggingComponentType?
|
|
170
|
+
|
|
171
|
+
`string`
|
|
172
|
+
|
|
173
|
+
The node logging component type.
|
|
174
|
+
|
|
175
|
+
#### Returns
|
|
176
|
+
|
|
177
|
+
`Promise`\<`boolean`\>
|
|
178
|
+
|
|
179
|
+
True if the teardown process was successful.
|
|
180
|
+
|
|
181
|
+
#### Implementation of
|
|
182
|
+
|
|
183
|
+
`IBlobStorageConnector.teardown`
|
|
184
|
+
|
|
185
|
+
***
|
|
186
|
+
|
|
187
|
+
### empty() {#empty}
|
|
188
|
+
|
|
189
|
+
> **empty**(): `Promise`\<`void`\>
|
|
190
|
+
|
|
191
|
+
Remove all blobs from the storage.
|
|
192
|
+
|
|
193
|
+
#### Returns
|
|
194
|
+
|
|
195
|
+
`Promise`\<`void`\>
|
|
196
|
+
|
|
197
|
+
Nothing.
|
|
198
|
+
|
|
199
|
+
#### Implementation of
|
|
200
|
+
|
|
201
|
+
`IBlobStorageConnector.empty`
|
|
202
|
+
|
|
203
|
+
***
|
|
204
|
+
|
|
161
205
|
### getStore() {#getstore}
|
|
162
206
|
|
|
163
207
|
> **getStore**(): `object`
|
package/locales/en.json
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
{
|
|
2
|
+
"info": {
|
|
3
|
+
"memoryBlobStorageConnector": {
|
|
4
|
+
"storeTearingDown": "Tearing down memory blob storage",
|
|
5
|
+
"storeTornDown": "Memory blob storage torn down"
|
|
6
|
+
}
|
|
7
|
+
},
|
|
2
8
|
"health": {
|
|
3
9
|
"memoryBlobStorageConnector": {
|
|
4
10
|
"healthDescription": "Memory blob storage connector is healthy, storing \"{storedItemCount}\" items"
|
|
@@ -6,7 +12,8 @@
|
|
|
6
12
|
},
|
|
7
13
|
"error": {
|
|
8
14
|
"memoryBlobStorageConnector": {
|
|
9
|
-
"namespaceMismatch": "The namespace in the urn \"{id}\" does not match the namespace of the blob storage \"{namespace}\""
|
|
15
|
+
"namespaceMismatch": "The namespace in the urn \"{id}\" does not match the namespace of the blob storage \"{namespace}\"",
|
|
16
|
+
"emptyFailed": "Failed to empty memory blob storage"
|
|
10
17
|
}
|
|
11
18
|
}
|
|
12
19
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/blob-storage-connector-memory",
|
|
3
|
-
"version": "0.0.3-next.
|
|
3
|
+
"version": "0.0.3-next.12",
|
|
4
4
|
"description": "Provides an in-memory connector for fast local testing and ephemeral blob workflows.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -14,10 +14,11 @@
|
|
|
14
14
|
"node": ">=20.0.0"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@twin.org/blob-storage-models": "0.0.3-next.
|
|
17
|
+
"@twin.org/blob-storage-models": "0.0.3-next.12",
|
|
18
18
|
"@twin.org/context": "next",
|
|
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/es/index.js",
|